diff --git a/CodeExamples/CodeExamples.ipynb b/CodeExamples/CodeExamples.ipynb index 7cf00fd3b958ba1ab35ed337ffe28acdcdcba5a9..11baacdf0d45990f48dd3bbbdf109d36112c3715 100644 --- a/CodeExamples/CodeExamples.ipynb +++ b/CodeExamples/CodeExamples.ipynb @@ -12,7 +12,8 @@ "\n", "- ## Conditionals \n", "\n", - "Set of examples to demonstrate conditional statements (if elif else)\n", + "Set of examples to demonstrate conditional statements (if elif else). \n", + "\n", "Take these and work / modify them and explore what happens:\n", "\n", "> [OneWayConditional](OneWayConditional.ipynb): Simple one way (if) conditional.\n", @@ -29,7 +30,29 @@ "\n", "> [FunctionCall](FunctionCall.ipynb): Example with function calls if conditional tests, a bit tricky, but important concepts here.\n", "\n", - "- File\n", + "- ## File\n", + "\n", + "Set of examples for reading and writing to simple text file.\n", + "\n", + "> [ReadTextFile](ReadTextFile.ipynb): Read a text file and print out again (discussed \"\\n\" problem with print()).\n", + "\n", + "> [WriteFile](WriteFile.ipynb): Writes a bit of text (and culture) to a file. (this write / overwrites \"burns.txt\").\n", + "\n", + "> [RangeLooptoFile](RangeLooptoFile.ipynb): Write data to a file from a a loop.\n", + "\n", + "> [LineCount](LineCount.ipynb): Open a file and countv the number of lines (with error trap for wrong filename).\n", + "\n", + "> [ImageShow](ImageShow.ipynb): Read and display an image file using Matplotlib.\n", + "\n", + "Data supplied\n", + "\n", + "> burns.txt\t\tA bit of culture !!! \n", + "> cosdata.txt\t\tOutput from RangeLooptoFile.py \n", + "> sample.txt\t\tData for Checkpoint 4 \n", + "> stones.jpg\t\tStones on Scottish beach \n", + "\n", + "\n", + "\n", "- Format\n", "- Functions\n", "- Further Functions\n", @@ -52,12 +75,15 @@ "- [ExtractSublist](ExtractSublist.ipynb)\n", "- [FlightPlot](FlightPlot.ipynb)\n", "- [FloatFormat](FloatFormat.ipynb)\n", + "- [FunctionCall](FunctionCall.ipynb)\n", "- [FunctionPlot](FunctionPlot.ipynb)\n", "- [GaussianNoisePlot](GaussianNoisePlot.ipynb)\n", "- [GuessGame](GuessGame.ipynb)\n", "- [Hello](Hello.ipynb)\n", + "- [ImageShow](ImageShow.ipynb)\n", "- [InsideSphere](InsideSphere.ipynb)\n", "- [IntegerFormat](IntegerFormat.ipynb)\n", + "- [LineCount](LineCount.ipynb)\n", "- [MakeandAppendList](MakeandAppendList.ipynb)\n", "- [OneWayConditional](OneWayConditional.ipynb)\n", "- [PiExample](PiExample.ipynb)\n", diff --git a/CodeExamples/ImageShow.ipynb b/CodeExamples/ImageShow.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..bfbb1fc6d65efe88f857e0e899925bb7df0b2d29 --- /dev/null +++ b/CodeExamples/ImageShow.ipynb @@ -0,0 +1,60 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "6838b2de", + "metadata": {}, + "source": [ + "# Hello" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4200d243", + "metadata": {}, + "outputs": [], + "source": [ + "\"\"\"\n", + " Example to read a image file using the matplotlib image module\n", + " and display it. It will work with all common image formats\n", + "\n", + " Try with the supplied \"stones.jpg\" file. (stones on a Scottish beach)\n", + "\"\"\"\n", + "\n", + "import matplotlib.pyplot as plt\n", + "import matplotlib.image as img # Import image module\n", + "\n", + "def main():\n", + " filename = str(input(\"Graphics file : \"))\n", + "\n", + " image = img.imread(filename) # Read the jpeg\n", + " plt.imshow(image) # render it in plot axis\n", + " plt.show() # display it\n", + "\n", + "main()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/CodeExamples/LineCount.ipynb b/CodeExamples/LineCount.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..29a4efc068bc0cf9e4cc06118b007e8243e43d07 --- /dev/null +++ b/CodeExamples/LineCount.ipynb @@ -0,0 +1,74 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "46844364", + "metadata": {}, + "source": [ + "# Cos Plot" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a64ddd0e", + "metadata": {}, + "outputs": [], + "source": [ + "\"\"\"\n", + " Example program to open a file and count the number of lines in the file.\n", + " This example also show how to trap the most common (and annoying) problem of typing\n", + " the wrong filename. If you are new to coding, ignore this and come back an look at it\n", + " at the end of the course.\n", + "\n", + " Try with \"burns.txt\"\n", + "\"\"\"\n", + "\n", + "\n", + "\n", + "def main():\n", + "\n", + " # Prompt for file name and open it for reading\n", + " # This has a more compplex stucture to trap errors\n", + "\n", + " while True: # Go into infinite loop\n", + " try: # try: test for errors\n", + " file = str(input(\"Filename : \")) # Prompt for name\n", + " filein = open(file, \"r\") # Open file\n", + " break # If here, success, so break out of loop\n", + " except: # Catch the error, so failed to open file\n", + " print(\"Failed to open file : {0:s}\".format(file)) # Print error message then back to loop\n", + "\n", + " # Read in the data to a list of strings.\n", + " data = filein.readlines()\n", + " filein.close() # Close file, optional, but good pratcice\n", + "\n", + " print(\"Number of lines in file is :\" + str(len(data)))\n", + "\n", + "# Run the program\n", + "main()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}