From bc497e19faead5bacc1ae256539db0e3bce924ed Mon Sep 17 00:00:00 2001 From: cprutean <cip.pruteanu@ed.ac.uk> Date: Fri, 26 Jul 2024 15:01:20 +0100 Subject: [PATCH] Update 8 files - /CodeExamples/RootTwo.ipynb - /CodeExamples/SortFunction.ipynb - /CodeExamples/SphereVolume.ipynb - /CodeExamples/TemperatureConverter.ipynb - /CodeExamples/ThreeWayConditional.ipynb - /CodeExamples/TwoWayConditional.ipynb - /CodeExamples/WhileLooptoList.ipynb - /CodeExamples/WriteFile.ipynb --- CodeExamples/RootTwo.ipynb | 39 ++++++++++--------------- CodeExamples/SortFunction.ipynb | 19 +++++------- CodeExamples/SphereVolume.ipynb | 7 ++--- CodeExamples/TemperatureConverter.ipynb | 30 +++++++++---------- CodeExamples/ThreeWayConditional.ipynb | 38 ++++++++++++------------ CodeExamples/TwoWayConditional.ipynb | 23 +++++++-------- CodeExamples/WhileLooptoList.ipynb | 33 +++++++++------------ CodeExamples/WriteFile.ipynb | 34 ++++++++++----------- 8 files changed, 96 insertions(+), 127 deletions(-) diff --git a/CodeExamples/RootTwo.ipynb b/CodeExamples/RootTwo.ipynb index 51f5497..79c83ab 100644 --- a/CodeExamples/RootTwo.ipynb +++ b/CodeExamples/RootTwo.ipynb @@ -20,38 +20,31 @@ " It also inclues the correct way to do this.\n", " \n", " Try with 2.0 and 4.0 and a random set of others and see what happens.\n", - " (then don't put this is a program) \n", + " (then don't put this in a program) \n", " \n", "\"\"\"\n", "\n", "import math\n", "\n", - "def main():\n", + "# Read a number \n", + "x = float(input(\"Value : \"))\n", + "y = math.sqrt(x)**2\n", "\n", - " # make \n", - " \n", - " x = float(input(\"Value : \"))\n", - " y = math.sqrt(x)**2\n", - "\n", - " if y == x:\n", - " print(\"math.sqrt({0:4.2f})**2 = {0:4.2} so maths works\".format(x))\n", - " else:\n", - " print(\"math.sqrt({0:4.2f})**2 != {0:4.2} so maths fails\".format(x))\n", + "if y == x:\n", + " print(\"math.sqrt({0:4.2f})**2 = {0:4.2} so maths works\".format(x))\n", + "else:\n", + " print(\"math.sqrt({0:4.2f})**2 != {0:4.2} so maths fails\".format(x))\n", "\n", - " delta = y - x\n", - " print(\"Difference between the numbers is : \" + str(delta))\n", + "delta = y - x\n", + "print(\"Difference between the numbers is : \" + str(delta))\n", " \n", - " # Here this the correct way to do this with the math.isclose() function.\n", - " # this ONLY works on Python 3.5 or newer\n", + "# Here is the correct way to do this with the math.isclose() function.\n", + "# this ONLY works in Python 3.5 or newer\n", " \n", - " if math.isclose(x,y):\n", - " print(\"The math.isclose() test works\")\n", - " else:\n", - " print(\"The math.isclose() test fails\")\n", - " \n", - "# Execute the program\n", - "\n", - "main()" + "if math.isclose(x,y):\n", + " print(\"The math.isclose() test works\")\n", + "else:\n", + " print(\"The math.isclose() test fails\")" ] } ], diff --git a/CodeExamples/SortFunction.ipynb b/CodeExamples/SortFunction.ipynb index eca22a1..b0fe9ea 100644 --- a/CodeExamples/SortFunction.ipynb +++ b/CodeExamples/SortFunction.ipynb @@ -23,19 +23,14 @@ " ls.sort()\n", " return ls \n", "\n", - "def main():\n", + "vals = [2,9,1,5,6,3,0]\n", + "nl = sortit(vals)\n", + "print(str(vals))\n", + "print(str(nl))\n", "\n", - " vals = [2,9,1,5,6,3,0]\n", - " nl = sortit(vals)\n", - " print(str(vals))\n", - " print(str(nl))\n", - "\n", - " vals.append(99)\n", - " print(str(vals))\n", - " print(str(nl))\n", - "\n", - "\n", - "main()" + "vals.append(99)\n", + "print(str(vals))\n", + "print(str(nl))" ] } ], diff --git a/CodeExamples/SphereVolume.ipynb b/CodeExamples/SphereVolume.ipynb index 15419c9..cf1d530 100644 --- a/CodeExamples/SphereVolume.ipynb +++ b/CodeExamples/SphereVolume.ipynb @@ -51,7 +51,7 @@ " points = int(input(\"Number of points : \")) # Ask for initial conditions\n", " radius = float(input(\"Radius of sphere : \"))\n", " \n", - " # Counters for number inside and look variable\n", + " # Counters for number inside and look variable\n", " inside, p = 0 , 0\n", " while p <= points: # Loop over number of trial points\n", " x,y,z = random_point(radius) # Random point in cube round sphere\n", @@ -59,9 +59,8 @@ " inside += 1\n", " p += 1 \n", "\n", - " # Estimated volume is given by ratio of number of points inside\n", - " # times volume of cube round sphere\n", - " #\n", + " # Estimated volume is given by ratio of number of points inside\n", + " # times volume of cube round sphere\n", " estimate = float(inside)/float(points)*math.pow(2*radius,3)\n", " vol = 4.0*math.pi*math.pow(radius,3)/3.0\n", "\n", diff --git a/CodeExamples/TemperatureConverter.ipynb b/CodeExamples/TemperatureConverter.ipynb index a98450c..a4ef839 100644 --- a/CodeExamples/TemperatureConverter.ipynb +++ b/CodeExamples/TemperatureConverter.ipynb @@ -16,38 +16,34 @@ "outputs": [], "source": [ "\"\"\"\n", - " Simple converter between Fahrenheit and centigrade using functions\n", + " Simple converter between Fahrenheit and Celsius degrees using functions\n", "\"\"\"\n", "\n", "def fahrenheit(t):\n", " \"\"\"\n", - " Convert centigrade to fahrenheit\n", + " Convert Celsius to Fahrenheit\n", " \"\"\"\n", " return 1.8*t + 32\n", "\n", - "def centegrade(t):\n", + "def celsius(t):\n", " \"\"\"\n", - " Convert fahrenheith to centegrade\n", + " Convert Fahrenheit to Celsius\n", " \"\"\"\n", " return (t - 32.0)/1.8\n", "\n", "\n", - "def main(): # Main program\n", + "# Main program\n", + "freezing = 0.0\n", + "boiling = 100.0\n", "\n", - " freezing = 0.0\n", - " boiling = 100.0\n", + "print(\"Water freezes as : \" + str(freezing) + \" C or \" + str(fahrenheit(freezing)) + \" F\")\n", + "print(\"Water boils as : \" + str(boiling) + \" C or \" + str(fahrenheit(boiling)) + \" F\")\n", "\n", - " print(\"Water freezes as : \" + str(freezing) + \" C or \" + str(fahrenheit(freezing)) + \" F\")\n", - " print(\"Water boils as : \" + str(boiling) + \" C or \" + str(fahrenheit(boiling)) + \" F\")\n", - "\n", - " normalbody = 98.6\n", - " deathvalley = 134.0\n", + "normalbody = 98.6\n", + "deathvalley = 134.0\n", " \n", - " print(\"Normal body temperature is : \" + str(normalbody) + \" F or \" + str(centegrade(normalbody)) + \" C\")\n", - " print(\"Death Valley record is : \" + str(deathvalley) + \" F or \" + str(centegrade(deathvalley)) + \" C\")\n", - "\n", - "\n", - "main()" + "print(\"Normal body temperature is : \" + str(normalbody) + \" F or \" + str(celsius(normalbody)) + \" C\")\n", + "print(\"Death Valley record is : \" + str(deathvalley) + \" F or \" + str(celsius(deathvalley)) + \" C\")" ] } ], diff --git a/CodeExamples/ThreeWayConditional.ipynb b/CodeExamples/ThreeWayConditional.ipynb index 29c7d1a..361f3e7 100644 --- a/CodeExamples/ThreeWayConditional.ipynb +++ b/CodeExamples/ThreeWayConditional.ipynb @@ -21,28 +21,26 @@ "\n", "import math\n", "\n", - "def main():\n", - " a = float(input(\"Give a float : \"))\n", - " if a > 4.0 :\n", - " # Inside if\n", - " b = 4.0 - math.sqrt(a)\n", - " a = 2.0*(b - 3.0)\n", - " print(\"if statements executed\")\n", - " elif a > 0 :\n", - " # Inside else is\n", - " b = 1.0 + math.sqrt(a)\n", - " a = (b - 1.0)\n", - " print(\"elif statements executed\")\n", - " else :\n", - " # Inside else\n", - " b = 4.0 - 3.0*math.sqrt(-a)\n", - " a = 7.0*(b - 3.0)\n", - " print(\"else statements executed\")\n", + "a = float(input(\"Give a float : \"))\n", "\n", - " # Outside conditional\n", - " print(\"The value of a is : \" + str(a) + \" and b is : \" + str(b))\n", + "if a > 4.0 :\n", + " # Inside if\n", + " b = 4.0 - math.sqrt(a)\n", + " a = 2.0*(b - 3.0)\n", + " print(\"if statements executed\")\n", + "elif a > 0 :\n", + " # Inside else is\n", + " b = 1.0 + math.sqrt(a)\n", + " a = (b - 1.0)\n", + " print(\"elif statements executed\")\n", + "else :\n", + " # Inside else\n", + " b = 4.0 - 3.0*math.sqrt(-a)\n", + " a = 7.0*(b - 3.0)\n", + " print(\"else statements executed\")\n", "\n", - "main()" + "# Outside conditional\n", + "print(\"The value of a is : \" + str(a) + \" and b is : \" + str(b))" ] } ], diff --git a/CodeExamples/TwoWayConditional.ipynb b/CodeExamples/TwoWayConditional.ipynb index c0ba098..eab776d 100644 --- a/CodeExamples/TwoWayConditional.ipynb +++ b/CodeExamples/TwoWayConditional.ipynb @@ -19,21 +19,18 @@ " A Two way conditional example \n", "\"\"\"\n", "\n", - "def main():\n", - " a = int(input(\"Give an integer : \"))\n", + "a = int(input(\"Give an integer : \"))\n", "\n", - " if a < 10 :\n", - " # Inside true of conditional\n", - " print(\"The integer you gave was less that 10, give a bigger one.\")\n", - " a = int(input(\"Give a bigger integer : \"))\n", - " else:\n", - " # Inside false of conditional\n", - " print(\"The integer you gave was big enough.\")\n", + "if a < 10 :\n", + " # Inside true of conditional\n", + " print(\"The integer you gave was less that 10, give a bigger one.\")\n", + " a = int(input(\"Give a bigger integer : \"))\n", + "else:\n", + " # Inside false of conditional\n", + " print(\"The integer you gave was big enough.\")\n", "\n", - " # Outside conditional\n", - " print (\"The integer you gave was : \" + str(a))\n", - "\n", - "main()" + "# Outside conditional\n", + "print (\"The integer you gave was : \" + str(a))" ] } ], diff --git a/CodeExamples/WhileLooptoList.ipynb b/CodeExamples/WhileLooptoList.ipynb index e295a2b..bfe2cb0 100644 --- a/CodeExamples/WhileLooptoList.ipynb +++ b/CodeExamples/WhileLooptoList.ipynb @@ -22,27 +22,22 @@ "\n", "import math\n", "\n", - "def main():\n", - " n_point = 50\n", - " delta_theta = 2.0*math.pi/n_point # theta increment\n", - " theta = 0.0 # theta start\n", - " theta_data = [] # empty list \n", - " cos_data = [] # empty list\n", + "n_point = 50\n", + "delta_theta = 2.0*math.pi/n_point # theta increment\n", + "theta = 0.0 # theta start\n", + "theta_data = [] # empty list \n", + "cos_data = [] # empty list\n", "\n", + "while theta < 2.0*math.pi: # while loop\n", + " cos = math.cos(theta)\n", + " theta_data.append(theta) # Add values to list\n", + " cos_data.append(cos)\n", + " theta += delta_theta # increment theta\n", "\n", - " while theta < 2.0*math.pi: # while loop\n", - " cos = math.cos(theta)\n", - " theta_data.append(theta) # Add values to list\n", - " cos_data.append(cos)\n", - " theta += delta_theta # increment theta\n", - "\n", - " # Do a default print (not very tidy output, replace this and \n", - " # a loop and make it nicer)\n", - " print(str(theta_data))\n", - " print(str(cos_data))\n", - "\n", - "\n", - "main()" + "# Do a default print (not very tidy output, replace this and \n", + "# a loop and make it nicer)\n", + "print(str(theta_data))\n", + "print(str(cos_data))" ] } ], diff --git a/CodeExamples/WriteFile.ipynb b/CodeExamples/WriteFile.ipynb index 30fdaa0..0e0cc4a 100644 --- a/CodeExamples/WriteFile.ipynb +++ b/CodeExamples/WriteFile.ipynb @@ -19,27 +19,23 @@ " Example to open and write text strings, (and add a bit of culture)\n", "\"\"\"\n", "\n", - "def main():\n", - " fileout = open(\"burns.txt\",\"w\")\n", + "fileout = open(\"burns.txt\",\"w\")\n", "\n", - " fileout.write(\"When chapman billies leave the street,\\n\")\n", - " fileout.write(\"And drouthy neebors, neebors meet,\\n\")\n", - " fileout.write(\"As market-days are wearing late,\\n\")\n", - " fileout.write(\"An' folk begin to tak the gate;\\n\")\n", - " fileout.write(\"While we sit bousing at the nappy,\\n\")\n", - " fileout.write(\"And getting fou and unco' happy,\\n\")\n", - " fileout.write(\"We think na on the lang Scots miles,\\n\")\n", - " fileout.write(\"The mosses, waters, slaps and styles,\\n\")\n", - " fileout.write(\"That lie between us and our hame,\\n\")\n", - " fileout.write(\"Whare sits our sulky sullen dame,\\n\")\n", - " fileout.write(\"Gathering her brows like gathering storm,\\n\")\n", - " fileout.write(\"Nursing her wrath to keep it warm.\\n\")\n", - " fileout.write(\"\\nRobert Burns\\n\")\n", + "fileout.write(\"When chapman billies leave the street,\\n\")\n", + "fileout.write(\"And drouthy neebors, neebors meet,\\n\")\n", + "fileout.write(\"As market-days are wearing late,\\n\")\n", + "fileout.write(\"An' folk begin to tak the gate;\\n\")\n", + "fileout.write(\"While we sit bousing at the nappy,\\n\")\n", + "fileout.write(\"And getting fou and unco' happy,\\n\")\n", + "fileout.write(\"We think na on the lang Scots miles,\\n\")\n", + "fileout.write(\"The mosses, waters, slaps and styles,\\n\")\n", + "fileout.write(\"That lie between us and our hame,\\n\")\n", + "fileout.write(\"Whare sits our sulky sullen dame,\\n\")\n", + "fileout.write(\"Gathering her brows like gathering storm,\\n\")\n", + "fileout.write(\"Nursing her wrath to keep it warm.\\n\")\n", + "fileout.write(\"\\nRobert Burns\\n\")\n", "\n", - " fileout.close()\n", - "\n", - "\n", - "main()" + "fileout.close()" ] } ], -- GitLab