{"id":286,"date":"2020-08-19T18:41:35","date_gmt":"2020-08-19T23:41:35","guid":{"rendered":"https:\/\/www.brezeale.com\/?p=286"},"modified":"2020-09-18T11:24:21","modified_gmt":"2020-09-18T16:24:21","slug":"python-programming-a-self-learning-guide","status":"publish","type":"post","link":"https:\/\/www.brezeale.com\/?p=286","title":{"rendered":"Python Programming: A Self-Learning Guide"},"content":{"rendered":"\n<p>Note: If you already know how to program in another language, feel that the guide below is too slow for you, or just want to see more code examples then you may want to check out my <a href=\"https:\/\/www.brezeale.com\/?p=600\" target=\"_blank\" rel=\"noreferrer noopener\">Python Crash Course<\/a>.<\/p>\n\n\n\n<p>The purpose of this page is to guide someone wishing to learn to program using the Python programming language.  Each section consists of<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>a link to a video that teaches an element of the language<\/li><li>sample problems for you to try.  <span class=\"has-inline-color has-vivid-red-color\">My solutions to the problems can be found at the bottom of the page. <\/span> It&#8217;s OK if your solution isn&#8217;t the same as mine.<\/li><\/ul>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<p>Once you reach the stage where you understand statements, conditionals, loops, and functions, I also include<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>a link to a page in which I discuss my thinking process as I design a program to accomplish some task. That page also contains a link to a video with more details.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<p>Note that the practice problems build upon your knowledge.  Some of the practice problems assume you worked the earlier problems.<\/p>\n\n\n\n<p>Tip: In order to learn to program, you have to be willing to try to write programs.  In the beginning this may consist of doing very simple things or taking existing code and modifying it to change its functionality.  As you gain experience and confidence, you need to work to write code from scratch.<\/p>\n\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">Introduction<\/h3>\n\n\n\n<p>start here: <a href=\"https:\/\/www.youtube.com\/watch?v=pDD05iFNqLU\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.youtube.com\/watch?v=pDD05iFNqLU<\/a><\/p>\n\n\n\n<p>Now you need to install the necessary software:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Microsoft Windows: <a rel=\"noreferrer noopener\" href=\"https:\/\/www.youtube.com\/watch?v=E4c2iINkgPQ\" target=\"_blank\">https:\/\/www.youtube.com\/watch?v=E4c2iINkgPQ<\/a><\/li><li>Mac OS or Linux: you probably already have Python installed, so watch  <a rel=\"noreferrer noopener\" href=\"https:\/\/www.youtube.com\/watch?v=sPnZemxvT7U\" target=\"_blank\">https:\/\/www.youtube.com\/watch?v=sPnZemxvT7U<\/a>  <\/li><li>(optional) I use the vim editor on Linux in my videos.  If you want a better understanding of what I am doing, then watch <a rel=\"noreferrer noopener\" href=\"https:\/\/www.youtube.com\/watch?v=VbvSgDWLF9A\" target=\"_blank\">https:\/\/www.youtube.com\/watch?v=VbvSgDWLF9A<\/a> <\/li><\/ul>\n\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">Statements<\/h3>\n\n\n\n<p>start here: <a rel=\"noreferrer noopener\" href=\"https:\/\/www.youtube.com\/watch?v=pIIaSrxSDSc\" target=\"_blank\">https:\/\/www.youtube.com\/watch?v=pIIaSrxSDSc<\/a><\/p>\n\n\n\n<p>Now try the following practice problems:<\/p>\n\n\n\n<p><strong>Practice #1:<\/strong> The following program finds the area of a rectangle.  Modify it to find the area of a triangle.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(&quot;I will calculate the area of a rectangle for you.&quot;)\nsideA = int( input(&quot;Enter the length of one side: &quot;) )\nsideB = int( input(&quot;Enter the length of an adjacent side: &quot;) )\n\narea = sideA * sideB\n\nprint(&quot;The area is {}&quot;.format(area))\n<\/pre><\/div>\n\n\n<p><strong>Practice #2: <\/strong>Write a program that prompts the user for a number and then uses it to evaluate the following mathematical function: f(x) = 3x ^2 + 5x &#8211; 6.  Note that to raise x to the nth power, you write x**n.<\/p>\n\n\n\n<p><strong>Practice #3: <\/strong>Write a program that will prompt the user for a temperature in Fahrenheit and then convert it to Celsius. &nbsp;You may recall that the formula is C = (5\/9) * (F &#8211; 32) .  Assume the Fahrenheit temperature is an integer, but print the Celsius temperature to two decimal places.<\/p>\n\n\n\n<p><strong>Practice #4: <\/strong>Write a program that produces a random integer in the range of 1 to 100 and prints it out.  At the top of your file you should have the lines (you can choose a variable name other than <code>value<\/code>).<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport random\nvalue = random.randint(1, 100)\n<\/pre><\/div>\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">Conditionals<\/h3>\n\n\n\n<p>start here: <a href=\"https:\/\/www.youtube.com\/watch?v=a6zvvSIGP7s\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.youtube.com\/watch?v=a6zvvSIGP7s<\/a><\/p>\n\n\n\n<p><strong>Practice #1: <\/strong>Compare the two sets of conditionals below.   Do you expect them to produce the same output (other than the version number printed at the beginning)?<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#  version 1:\nx = 5\n    \nif x &lt; 4:\n    print(&quot;1: %d is less than 4&quot; % x)\nelif x &lt; 6:\n    print(&quot;1: %d is less than 6&quot; % x)\nelif x &lt; 8:\n    print(&quot;1: %d is less than 8&quot; % x)\nelif x &lt; 10: \n    print(&quot;1: %d is less than 10&quot; % x)\nelse:\n    print(&quot;1: %d is greater than or equal to 10&quot; % x)\n\n#  version 2:\nx = 5 \n    \nif x &lt; 4:\n    print(&quot;2: %d is less than 4&quot; % x)\nif x &lt; 6:\n    print(&quot;2: %d is less than 6&quot; % x)\nif x &lt; 8:\n    print(&quot;2: %d is less than 8&quot; % x)\nif x &lt; 10: \n    print(&quot;2: %d is less than 10&quot; % x)\nelse:\n    print(&quot;2: %d is greater than or equal to 10&quot; % x)\n<\/pre><\/div>\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<p><strong>Practice #2: <\/strong>Modify the temperature conversion program in the Statements section to state whether or not water would boil at the temperature given. Your output might look like this:<\/p>\n<\/div><\/div>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nWhat is the temperature in Fahrenheit? 100\nA temperature of 100 degrees Fahrenheit is 37 in Celsius\nWater does not boil at this temperature (under typical conditions).\n<\/pre><\/div>\n\n\n<p><strong>Practice #3:<\/strong> Write a program that prompts the user for their age.  If they are younger than 18 years old, only print &#8220;you are a child&#8221;.  If they are 18 or older, print &#8220;you are NOT a child&#8221;.  If they are at least 67 years old, also print &#8220;enjoy your retirement&#8221;.  Your output might look like this (this is several runs of the program):<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nage?  14\nyou are a child\n\nage?  18\nyou are NOT a child\n \nage?  50\nyou are NOT a child\n\nage?  67\nyou are NOT a child\nenjoy your retirement\n<\/pre><\/div>\n\n\n<p><strong>Practice #4:<\/strong> In the Statements section your wrote a program to generate a random integer in the range of 1 to 100.  Extend the program now to generate two random integers in the range of 10 to 20, print each of the integers generated, and then print which is smallest and which is largest (or that they are equal).  Note that you will only use a single <code>import<\/code> statement.  Here is sample output from multiple runs of the program:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nthe integers generated are 13 and 10\nsmallest = 10, largest = 13\n\nthe integers generated are 20 and 20\nthey are equal\n\nthe integers generated are 10 and 16\nsmallest = 10, largest = 16\n<\/pre><\/div>\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">Loops<\/h3>\n\n\n\n<p>start here: <a href=\"https:\/\/www.youtube.com\/watch?v=opTdpsld-gw\">https:\/\/www.youtube.com\/watch?v=opTdpsld-gw<\/a><\/p>\n\n\n\n<p><strong>Practice #1:<\/strong> Write a program that prompts the user for two positive integers, Start and Stop, and then sums the integers from Start to Stop, inclusive. &nbsp;Perform some basic error checking.  If either number is negative or zero or Start is greater than Stop, tell the user there is an error and exit.<\/p>\n\n\n\n<p>Output from a couple of runs is:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nEnter a positive integer: 1\nEnter a positive integer greater than or equal to the first: 5\nThe sum from 1 to 5 is 15\n\nEnter a positive integer: 34\nEnter a positive integer greater than or equal to the first: 33\n\nERROR: invalid input\n<\/pre><\/div>\n\n\n<p><strong>Practice #2:<\/strong> Write a program that prompts the user for two positive integers, Start and Stop, and then counts the number of even integers in the range of Start to Stop. &nbsp;Print the even integers in this range. &nbsp;Output might look like this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nEnter a starting integer:  8\nEnter a stopping integer:  19\n8\n10\n12\n14\n16\n18\nThere are 6 even integers between 8 and 19\n<\/pre><\/div>\n\n\n<p><strong>Practice #3:<\/strong> Write a program that repeatedly prompts a user for an integer and then prints the integer, stopping once the integer is greater than 10.<\/p>\n\n\n\n<p><strong>Practice #4:<\/strong> Write a program that repeatedly prompts a user for an integer and then prints the integer, stopping once the integer is &gt; 10 or the user has provided 5 integers.<\/p>\n\n\n\n<p><strong>Practice #5:<\/strong> Write a program that prompts the user for grades and then prints the average grade. The program will continue to prompt for grades until a negative numbers (an invalid grade) is given. &nbsp;Here is sample output.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ngrade? 0\ngrade? 1\ngrade? 2\ngrade? 3\ngrade? -8\naverage of 4 grades is 1.50\n\ngrade? 1\ngrade? 2\ngrade? 3\ngrade? 4\ngrade? 5\ngrade? -234\naverage of 5 grades is 3.00\n<\/pre><\/div>\n\n\n<p><strong>Practice #6:<\/strong> Write a program that picks a random integer in the range of 1 to 100. &nbsp;It then prompts the user for a guess of the value, with hints of &#8216;too high&#8217; or &#8216;too low&#8217; from the program. &nbsp;The program continues to run until the user guesses the integer.  Sample output might look like this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntrue value: 74\nwhat is your guess?  30\n  too low\nwhat is your guess?  80\n  too high\nwhat is your guess?  60\n  too low\nwhat is your guess?  75\n  too high\nwhat is your guess?  74\nyou guessed it!!!\n<\/pre><\/div>\n\n\n<p>When designing this program, consider the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>printing the randomly generated number at the beginning will help you test your program.<\/li><li>what condition will the loop use to keep running until the user guesses correctly?<\/li><li>for test purposes, you might choose to have the loop run a predetermined number of times.<\/li><\/ul>\n\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">Bits, Bytes, and Number Bases<\/h3>\n\n\n\n<p>This video discusses the internal representation of numbers.  While you can do a lot without knowing about bits, if you continue programming at some point you will want to know about this to have a better understanding of the internals of a computer.<\/p>\n\n\n\n<p>start here: <a href=\"https:\/\/www.youtube.com\/watch?v=aJ5V_UL_NHo\">https:\/\/www.youtube.com\/watch?v=aJ5V_UL_NHo<\/a><\/p>\n\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">Functions<\/h3>\n\n\n\n<p>start here: <a href=\"https:\/\/www.youtube.com\/watch?v=gp9_YuTT0kU\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.youtube.com\/watch?v=gp9_YuTT0kU<\/a><\/p>\n\n\n\n<p>design example:  <a href=\"https:\/\/www.brezeale.com\/?p=137\" target=\"_blank\" rel=\"noreferrer noopener\">generating a multiplication table<\/a><\/p>\n\n\n\n<p>Many people struggle to write functions.  Remember that many functions receive one or more input values and return an answer.  Try thinking about the input and output of your function. Also, while this should only be a short-term solution, if you are struggling with the function try writing all of the code together and then move the appropriate parts to a function.<\/p>\n\n\n\n<p><strong>Practice #1: <\/strong> A positive integer n is a <a rel=\"noreferrer noopener\" href=\"https:\/\/en.wikipedia.org\/wiki\/Perfect_number\" target=\"_blank\">perfect number<\/a> if the sum of the divisors of n (excluding n itself) is equal to n.  For example, 6 has divisors 1, 2, 3, and 6 and 6 = 1 + 2 + 3.  Write a program to prompt the user for an integer n and then calls a function that returns <code>True<\/code> if n is a perfect number and <code>False<\/code> if it is not a perfect number.  Print the result based on the value returned by the function. &nbsp;Here are several sample runs.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nn?  4\nn?  6\n6 is a perfect number\nn?  10\nn?  28\n28 is a perfect number\n<\/pre><\/div>\n\n\n<p><strong>Practice #<\/strong>2<strong>: <\/strong> Write a program that finds the perfect numbers in the range of 1 to 10,001 using the function that you created in the previous problem.  Here is sample output.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n6 is a perfect number\n28 is a perfect number\n496 is a perfect number\n8128 is a perfect number\n<\/pre><\/div>\n\n\n<p><strong>Practice #3: <\/strong> Write a program to prompt the user for an integer n and then calls a function that when given n prints n asterisks. &nbsp;Here are several sample runs.  Hint: remember that by default print() adds a newline, so if you don&#8217;t want it you will need to suppress it.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nn?  1\n*\n\nn?  2\n**\n\nn?  5\n*****\n\nn?  10\n**********\n<\/pre><\/div>\n\n\n<p><strong>Practice #4: <\/strong> Write a program to prompt the user for an integer n and then calls a function that when given n prints n spaces followed by n asterisks. &nbsp;Here are several sample runs.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nn?  1\n *\n\nn?  2\n  **\n\nn?  3\n   ***\n\nn?  10\n          **********\n<\/pre><\/div>\n\n\n<p><strong>Practice #5: <\/strong> Write a program that prompts the user for an integer n and then calls a function (passing n to it) that prints a pyramid n levels high from asterisks. &nbsp;Here are several sample runs.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nhow tall should the pyramid be?  5\n    *  \n   * *  \n  * * *  \n * * * *  \n* * * * *  \n\nhow tall should the pyramid be?  10\n         *  \n        * *  \n       * * *  \n      * * * *  \n     * * * * *  \n    * * * * * *  \n   * * * * * * *  \n  * * * * * * * *  \n * * * * * * * * *  \n* * * * * * * * * *  \n<\/pre><\/div>\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">Lists and Tuples<\/h3>\n\n\n\n<p>start here: <a href=\"https:\/\/www.youtube.com\/watch?v=rx-c1DGugWw\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.youtube.com\/watch?v=rx-c1DGugWw<\/a><\/p>\n\n\n\n<p>design example: <a href=\"https:\/\/www.brezeale.com\/?p=209\">simulating a perfect shuffle<\/a><\/p>\n\n\n\n<p>In my video I failed to discuss tuples.  Tuples are similar to lists in the sense that they are and ordered collection of objects; the primary distinction is that tuples are <em>immutable<\/em> and therefore cannot be changed. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\naList  = &#x5B;10, 20, 30]\naTuple = (10, 20, 20)\n\nprint( type(aList) )\nprint( type(aTuple) )\nprint()\n\nprint(aList&#x5B;0])\nprint(aTuple&#x5B;0])\n\naList&#x5B;0] = 99\n# aTuple&#x5B;0] = 99  # tuples are immutable, so you can&#039;t do this\n\n&quot;&quot;&quot;  output\n&lt;class &#039;list&#039;&gt;\n&lt;class &#039;tuple&#039;&gt;\n\n10\n10\n&quot;&quot;&quot;\n<\/pre><\/div>\n\n\n<p><strong>Practice #1:<\/strong> Write a function that when given a 2-dimensional list will print the list in its original shape. &nbsp;For example, if given the list<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndataList = &#x5B; &#x5B; 1,  2,  3,  4],\n             &#x5B; 5,  6,  7,  8],\n             &#x5B; 9, 10, 11, 12] ]\n<\/pre><\/div>\n\n\n<p>the function will print<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n 1   2   3   4 \n 5   6   7   8 \n 9  10  11  12 \n<\/pre><\/div>\n\n\n<p>If you have a 2-dimensional list called <code>myList<\/code>, then to find the number of rows in the list you can use <code>len(myList) <\/code>and to find the number of elements in each row (assuming they are all the same) you can use <code>len(myList[0])<\/code>.<\/p>\n\n\n\n<p><strong>Practice #2:<\/strong> Write a function that when given a 1-dimensional list will sum the values in the list and return the sum.  Check your work by applying Python&#8217;s <code>sum()<\/code> function to your list.<\/p>\n\n\n\n<p><strong>Practice #3:<\/strong> Write a function that when given a 1-dimensional list will sum the values in the list if they are multiples of 3 and return the sum.<\/p>\n\n\n\n<p><strong>Practice #4:<\/strong> Write a function that when given a 2-dimensional list will sum and print the values of each row.  The printing should take place in the function.<\/p>\n\n\n\n<p><strong>Practice #5:<\/strong> Write a function that when given a 2-dimensional list will return a list of the column sums.  Assume that each row has the same number of elements.  For example, given the list<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = &#x5B;&#x5B;1, 2, 3, 10],\n     &#x5B;4, 5, 6, 10],\n     &#x5B;7, 8, 9, 10]]\n<\/pre><\/div>\n\n\n<p>the returned value should be<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;12, 15, 18, 30]\n<\/pre><\/div>\n\n\n<p><strong>Practice #6:<\/strong> Python&#8217;s <code>range()<\/code> function returns a <code>range<\/code> object.  Without using the <code>range<\/code> function, write <code>myrange()<\/code> that will be called using the form <code>myrange(a, b)<\/code> and the function returns a list with the integers from a to less than b. &nbsp;If you want more of a challenge, add a third parameter, c, that is the step size for the values in the list.  You can assume that a will be less than or equal to b.<\/p>\n\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">Strings<\/h3>\n\n\n\n<p>start here: <a href=\"https:\/\/www.youtube.com\/watch?v=Lw0_BS29rrY\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.youtube.com\/watch?v=Lw0_BS29rrY<\/a><\/p>\n\n\n\n<p>design example: <a href=\"https:\/\/www.brezeale.com\/?p=278\">converting numbers to words<\/a><\/p>\n\n\n\n<p><strong>Practice #1:<\/strong> Write a function that will receive a string of comma-separated words and count how many of the words have more than five letters. &nbsp;The function will return the count when finished.  For example,<br><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ns = &quot;cat,dog,horse,elephant,giraffe&quot;\nprint(wordCount( s ))   # prints 2\n<\/pre><\/div>\n\n\n<p><strong>Practice #2:<\/strong> Write a function that will receive a string and a delimiter and print each token in the string as well as the number of characters in the token.<br><\/p>\n\n\n\n<p><strong>Practice #3:<\/strong> Write a function that receives a string that consists of any combination of spaces, digits, uppercase or lowercase letters. &nbsp;The function should count the number of uppercase letters and return this count.<br><\/p>\n\n\n\n<p><strong>Practice #4:<\/strong> Write a function that receives a string that consists of any combination of spaces, digits, uppercase or lowercase letters. &nbsp;The function will print the number of spaces, the number of digit characters, the number of uppercase characters, and the number of lowercase characters.<br><\/p>\n\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">File Input\/Output<\/h3>\n\n\n\n<p>start here:  <a href=\"https:\/\/www.youtube.com\/watch?v=TGnubSfgA1s\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.youtube.com\/watch?v=TGnubSfgA1s<\/a><\/p>\n\n\n\n<p>design example #1:  <a href=\"https:\/\/www.brezeale.com\/?p=396\" target=\"_blank\" rel=\"noreferrer noopener\">printing a class schedule<\/a><\/p>\n\n\n\n<p>design example #2: <a rel=\"noreferrer noopener\" href=\"https:\/\/www.brezeale.com\/?p=411\" target=\"_blank\">downloading and plotting stock prices with moving average<\/a><\/p>\n\n\n\n<p><strong>Practice #1:<\/strong> Write a program to write the integers 1 to 10 to a file, one number per line.  Then have the program read the file, summing the integers.<\/p>\n\n\n\n<p><strong>Practice #2:<\/strong> Use an editor to create a file in which each line consists of two integers, separated by a comma.  For example, the file might be<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n4,6\n8,2\n44,44\n21,-3\n50,99\n<\/pre><\/div>\n\n\n<p>Then write a program to read this CSV-formatted file and, for each line, prints the largest number on the line.<\/p>\n\n\n\n<p><strong>Practice #3:<\/strong> Read a CSV file in which each line consists of numbers separated by commas. &nbsp;Sum the numbers in the last column.  Don&#8217;t assume each line consists of the same quantity of numbers.<\/p>\n\n\n\n<p><strong>Practice #4:<\/strong> Read a CSV file in which each line consists of animal names separated by commas. &nbsp;Print the names sorted (remember that there is a list method for sorting).  The file to use is<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ncat,dog,horse\nrabbit,mouse\nbat,rat,giraffe,gorilla\nelephant\n<\/pre><\/div>\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">Dictionaries<\/h3>\n\n\n\n<p>start here: <a href=\"https:\/\/www.youtube.com\/watch?v=-1E8iU_Amyk\">https:\/\/www.youtube.com\/watch?v=-1E8iU_Amyk<\/a><\/p>\n\n\n\n<p>design example: <a href=\"https:\/\/www.brezeale.com\/?p=658\" target=\"_blank\" rel=\"noreferrer noopener\">Determining Which Degree Courses a Student is Eligible to Enroll In<\/a><\/p>\n\n\n\n<p><strong>Practice #1:<\/strong> Create a dictionary with people&#8217;s names as keys and their ages as values and print each person&#8217;s name and age.<\/p>\n\n\n\n<p><strong>Practice #2:<\/strong> Modify the previous program to only print the names and ages of people over 20 years old.<\/p>\n\n\n\n<p><strong>Practice #3:<\/strong> Modify the previous program to find the youngest person in the dictionary.  Print the person&#8217;s age and name.<\/p>\n\n\n\n<p><strong>Practice #4:<\/strong> Write a program that stores the following key\/value pairs in a dictionary and then produces the sum of the values corresponding to keys that would come after the letter &#8216;f&#8217; if sorted (you are not being asked to actually sort).<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&quot;rabbit&quot; : 7\n   &quot;dog&quot; : 42\n &quot;mouse&quot; : 55\n   &quot;cat&quot; : -23\n &quot;horse&quot; : 13\n<\/pre><\/div>\n\n\n<p>Pr<strong>actice #5:<\/strong> Store the names and ages from the first problem in a CSV file and read in the file, creating a dictionary from it.  Print the names and ages, sorted by name.  Finally, print the sum of the ages.<\/p>\n\n\n\n<p>Pr<strong>actice #6:<\/strong> We have the following data:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nName  Age  Weight\n-------------------\nTom    43   185\nJohn   50   170\nSusan  35   125\n<\/pre><\/div>\n\n\n<p>Store this data in a dictionary with the name as the key. &nbsp;Write a function that when given a dictionary of this form and either the word &#8220;age&#8221; or &#8220;weight&#8221; will print the name of the user with the largest age or weight (depending on the word passed).  You can assume that age and weight will be the only two things stored for each name, but don&#8217;t make assumptions about the number of entries in the dictionary.<\/p>\n\n\n\n<br>\n\n\n\n<h2 class=\"wp-block-heading\">Solutions to Practice Problems<\/h2>\n\n\n\n<p><strong>&#8212;&#8212;&#8212;-  Statements, Practice #1<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(&quot;I will calculate the area of a triangle for you.&quot;)\nw = int( input(&quot;Enter the width: &quot;) )\nh = int( input(&quot;Enter the height: &quot;) )\n\narea = (w*h)\/2\n\nprint(&quot;The area is {} square units&quot;.format(area))\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;-  Statements, Practice #2<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nx = int( input(&quot;Enter a number: &quot;) )\n\nf = 3 * x**2 + 5*x - 6\nprint(&quot;f({}) = {}&quot;.format(x, f))\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;-  Statements, Practice #<\/strong>3<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nf = int( input(&quot;Enter a temperature in Fahrenheit: &quot;) )\n\nc =  (f - 32)*5\/9\nprint(&quot;{} degrees Fahrenheit = {:.2f} degrees Celsius&quot;.format(f, c))\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;-  Statements, Practice #<\/strong>4<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport random\n\nvalue = random.randint(1, 100)\nprint(value)\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;-  Conditionals, Practice #<\/strong>1<\/p>\n\n\n\n<p>No.  The top code contains <code>elif<\/code> statements and so each is only evaluated if all previous conditionals are <code>False<\/code>.  In the bottom version, each <code>if<\/code> statement is independent of the others and each is evaluated.<\/p>\n\n\n\n<p><strong>&#8212;&#8212;&#8212;-  Conditionals, Practice #<\/strong>2<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nf = int( input(&quot;Enter a temperature in Fahrenheit: &quot;) )\n\nc =  (f - 32)*5\/9\n\nif c &gt;= 100 :\n    print(&quot;water normally boils at {:.2f} degrees Celsius&quot;.format(c))\nelse :\n    print(&quot;{:.2f} degrees Celsius is not hot enough to boil water&quot;.format(c))\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;-  Conditionals, Practice #<\/strong>3<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nage = int( input(&quot;age? &quot;) )\n\nif age &lt; 18 :\n    print(&quot;you are only a child&quot;)\nelse :\n    print(&quot;you are NOT a child&quot;)\n\n    if age &gt;= 67 :\n        print(&quot;enjoy your retirement&quot;)\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;-  Conditionals, Practice #<\/strong>4<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport random\n\na = random.randint(10, 20)\nb = random.randint(10, 20)\n\nprint(&quot;the integers generated are %d and %d&quot; % (a, b))\n\nif a &lt; b :\n    print(&quot;smallest = %d, largest = %d&quot; % (a, b))\nelif a &gt; b :\n    print(&quot;smallest = %d, largest = %d&quot; % (b, a))\nelse :\n    print(&quot;they are equal&quot;)\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;-  Loops, Practice #<\/strong>1<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstart = int( input(&quot;Enter a positive integer: &quot;) )\nstop = int( input(&quot;Enter a positive integer greater than or equal to the first: &quot;) )\n\nif start &lt; 1 or stop &lt; 1 or start &gt; stop :\n    print(&quot;\\n ERROR: invalid input&quot;)\nelse :\n    mysum = 0\n    i = start\n    while i &lt;= stop :\n        mysum += i\n        i += 1\n\n    print(&quot;The sum from %d to %d is %d&quot; % (start, stop, mysum))\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;-  Loops, Practice #<\/strong>2<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstart = int( input(&quot;Enter a positive integer: &quot;) )\nstop = int( input(&quot;Enter a positive integer greater than or equal to the first: &quot;) )\n\nif start &lt; 1 or stop &lt; 1 or start &gt; stop :\n    print(&quot;\\nERROR: invalid input&quot;)\nelse :\n    even = 0\n    i = start\n    while i &lt;= stop :\n        if i%2 == 0 :\n           print(i)\n           even += 1\n        i += 1\n\n    print(&quot;The are %d even integers between %d and %d&quot; % \n          (even, start, stop))\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;-  Loops, Practice #<\/strong>3<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nwhile True :\n    number = int( input(&quot;enter an integer: &quot;) )\n\n    if number &gt; 10 :\n        break\n\n    print(number)\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;-  Loops, Practice #<\/strong>4<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ncount = 0\nwhile count &lt; 5 :\n    number = int( input(&quot;enter an integer: &quot;) )\n\n    if number &gt; 10 :\n        break\n\n    count += 1\n    print(number)\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;-  Loops, Practice #5<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ngrade = 0\ncount = 0\nmysum = 0\n\nwhile grade &gt;= 0 :\n    grade = int( input(&quot;grade? &quot;) )\n\n    if grade &gt;= 0 :\n        count += 1\n        mysum += grade\n\nprint(&quot;average of {} grades is {:.2f}&quot;.format(count, mysum\/count))\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;-  Loops, Practice #<\/strong>6<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport random\n\nvalue = random.randint(1, 100)\nprint(&quot;true value: %d (for testing)&quot; % value)\n\nguess = -99  # initial value to get loop going\n\nwhile value != guess :\n    guess = int( input(&quot;guess? &quot;) )\n\n    if guess &gt; value :\n        print(&quot;  too high&quot;)\n    if guess &lt; value :\n        print(&quot;  too low&quot;)\n\nprint(&quot;you guessed it!!!&quot;)\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;-  Functions, Practice #<\/strong>1<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef perfectCheck( n ) :\n    mysum = 0  \n    for i in range(1, n) :\n        if n%i == 0 :\n            mysum += i\n\n    if n == mysum :\n        return True\n    else :\n        return False\n\n####  main  ####\nnumber = int( input(&quot;enter an integer: &quot;) )\n\nif perfectCheck(number) :\n    print(&quot;{} is a perfect number&quot;.format(number))\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;-  Functions, Practice #<\/strong>2<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef perfectCheck( n ) :\n    mysum = 0  \n    for i in range(1, n) :\n        if n%i == 0 :\n            mysum += i\n\n    if n == mysum :\n        return True\n    else :\n        return False\n\n####  main  ####\nfor i in range(1, 10001) :\n    if perfectCheck(i) :\n        print(&quot;{} is a perfect number&quot;.format(i))\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;-  Functions, Practice #<\/strong>3<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef printStars( n ) :\n    for i in range( n ) :\n        print(&quot;*&quot;, end=&quot;&quot;)\n\n    print()\n\n####  main  ####\nnumber = int( input(&quot;enter a nonnegative integer: &quot;) )\n\nprintStars(number)\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;-  Functions, Practice #<\/strong>4<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef printStars( n ) :\n    for i in range( n ) :\n        print(&quot; &quot;, end=&quot;&quot;)\n\n    for i in range( n ) :\n        print(&quot;*&quot;, end=&quot;&quot;)\n\n    print()\n\n####  main  ####\nnumber = int( input(&quot;enter a nonnegative integer: &quot;) )\n\nprintStars(number)\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;-  Functions, Practice #<\/strong>5<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef printStars( n, spaces ) :\n    for i in range( spaces-n ) :\n        print(&quot; &quot;, end=&quot;&quot;)\n\n    for i in range( n ) :\n        print(&quot;* &quot;, end=&quot;&quot;)  # notice the space after *\n\n    print()\n\n####  main  ####\nnumber = int( input(&quot;enter a nonnegative integer: &quot;) )\n\nfor i in range( 1, number+1 ) :\n    printStars(i, number)\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;-  Lists, Practice #<\/strong>1, version 1<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef printList( d ) :\n    for row in d :       # get row\n        for col in row : # get column within row\n            print(&quot;%2d &quot; % col, end=&quot;&quot;)\n        print()\n\n    print()\n\n####  main  ####\n# test with different sizes\na = &#x5B;&#x5B;1, 2, 3],\n     &#x5B;4, 5, 6]]\n\nb = &#x5B;&#x5B; 1,  2,  3,  4,  5],\n     &#x5B; 6,  7,  8,  9, 10],\n     &#x5B;11, 12, 13, 14, 15]]\n\nprintList(a)\nprintList(b)\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;- Lists, Practice #<\/strong>1, version 2<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef printList( d ) :\n    rows = len( d )\n    cols = len( d&#x5B;0] )\n    for r in range( rows) :       \n        for c in range( cols ) :\n            print(&quot;%2d &quot; % d&#x5B;r]&#x5B;c], end=&quot;&quot;)\n        print()\n\n    print()\n\n####  main  ####\n# test with different sizes\na = &#x5B;&#x5B;1, 2, 3],\n     &#x5B;4, 5, 6]]\n\nb = &#x5B;&#x5B; 1,  2,  3,  4,  5],\n     &#x5B; 6,  7,  8,  9, 10],\n     &#x5B;11, 12, 13, 14, 15]]\n\nprintList(a)\nprintList(b)\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;- Lists, Practice #<\/strong>2<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef sumList( d ) :\n    mySum = 0\n    for i in d :\n        mySum += i\n\n    return mySum\n\n####  main  ####\n# test with different sizes\na = &#x5B;1, 2, 3]\nb = &#x5B; 1,  2,  3,  4,  5]\n\nprint(sumList(a))\nprint(sumList(b))\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;- Lists, Practice #<\/strong>3<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef sumList( d ) :\n    mySum = 0\n    for i in d :\n        if i%3 == 0 :\n            mySum += i\n\n    return mySum\n\n####  main  ####\n# test with different sizes\na = &#x5B;1, 2, 3, 9, 81]\nb = &#x5B; 1,  2,  3,  4,  5, 6, 7]\n\nprint(sumList(a))\nprint(sumList(b))\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;- Lists, Practice #<\/strong>4<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef sumRow( d ) :\n    for row in d :\n        mySum = 0\n        for col in row :\n            mySum += col\n        print(&quot;row sum = {}&quot;.format(mySum) )\n\n\n####  main  ####\na = &#x5B;&#x5B;1, 2], \n     &#x5B;3, 4, 5, 6],\n     &#x5B;7, 8, 9]]\n\nsumRow(a)\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;- Lists, Practice #<\/strong>5<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef colSums( d ) :\n    rows = len(d)\n    cols = len(d&#x5B;0])\n    sums = &#x5B;]\n\n    for c in range(cols) :  # process each column\n        mySum = 0\n        for r in range(rows) :\n            mySum += d&#x5B;r]&#x5B;c]\n        sums.append(mySum)\n\n    return sums\n\n####  main  ####\na = &#x5B;&#x5B;1, 2, 3, 10],\n     &#x5B;4, 5, 6, 10],\n     &#x5B;7, 8, 9, 10]]\n\nprint( colSums(a) )\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;- Lists, Practice #<\/strong>6<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef myRange( a, b, c=1 ) :\n    # since an initial value is given to c, we\n    #   can leave c out in the function call if\n    #   we are OK with its initial value\n    out = &#x5B;]\n\n    while a &lt; b :\n        out.append( a )\n        a += c\n\n    return out\n\n####  main  ####\nprint( myRange(2, 5) )\nprint( myRange(5, 5) )\nprint( myRange(4, 10) )\nprint( myRange(4, 10, 2) )\nprint( myRange(4, 10, 3) )\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;- Strings, Practice #<\/strong>1<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef wordCount( s ) :\n    count = 0\n\n    tokens = s.split(&#039;,&#039;)\n    for t in tokens :\n        print(t)   # uncomment for testing\n        if len(t) &gt; 5 :\n            count += 1\n    \n    return count\n\n\n####  main  ####\ns = &quot;cat,dog,horse,elephant,giraffe&quot;\n\nprint(wordCount( s ))\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;- Strings, Practice #<\/strong>2<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef printTokens( s, d ) :\n    tokens = s.split(d)\n    for t in tokens :\n        print(&quot;{} has {} characters&quot;.format(t, len(t)))\n\n    print()\n    \n\n####  main  ####\ns = &quot;cat,dog,horse,elephant,giraffe&quot;\n\nprintTokens( s, &#039;,&#039; )\nprintTokens( s, &#039;o&#039; )\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;- Strings, Practice #<\/strong>3<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef countUpper( s ) :\n    count = 0\n\n    i = 0\n    while i &lt; len(s) :\n        if &#039;A&#039; &lt;= s&#x5B;i] and s&#x5B;i] &lt;= &#039;Z&#039; :\n            count += 1\n\n        i += 1\n    \n    return count\n\n####  main  ####\ns = &quot;lower 123 UPPER mIxEd&quot;\n\nprint(countUpper( s ))\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;- Strings, Practice #<\/strong>4<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef countChars( s ) :\n    # we could have separate variables for each counter\n    #   but using a list allows me to loop through the\n    #   counts when printing\n\n    classes = &#x5B;&quot;lowercase&quot;, &quot;uppercase&quot;, &quot;digits&quot;, &quot;spaces&quot;]\n    counts = &#x5B;0, 0, 0, 0]\n\n    i = 0\n    while i &lt; len(s) :\n        if &#039;a&#039; &lt;= s&#x5B;i] and s&#x5B;i] &lt;= &#039;z&#039; :\n            counts&#x5B;0] += 1\n        elif &#039;A&#039; &lt;= s&#x5B;i] and s&#x5B;i] &lt;= &#039;Z&#039; :\n            counts&#x5B;1] += 1\n        elif &#039;0&#039; &lt;= s&#x5B;i] and s&#x5B;i] &lt;= &#039;9&#039; :\n            counts&#x5B;2] += 1\n        elif s&#x5B;i] == &#039; &#039; :\n            counts&#x5B;3] += 1\n\n        i += 1\n\n    for i in range( len(counts) ) :\n        print(&quot;{} = {}&quot;.format(classes&#x5B;i], counts&#x5B;i]))\n    \n\n####  main  ####\ns = &quot;lower 1234 UPPER mIxEd&quot;\n\ncountChars( s )\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;- Files, Practice #<\/strong>1<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfp = open(&quot;practice.txt&quot;, &quot;w&quot;)\n\nfor i in range(1, 11) :\n    fp.write(&quot;%d\\n&quot; % i)\n\nfp.close()\n\n\nfp = open(&quot;practice.txt&quot;, &quot;r&quot;)\nmysum = 0\n\nfor line in fp :\n    mysum += int(line)\n\nfp.close()\n\nprint(&quot;sum = %d&quot; % mysum)\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;- Files, Practice #<\/strong>2<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfp = open(&quot;practice.txt&quot;, &quot;r&quot;)\n\nfor line in fp :\n    # if I know the number of tokens, I can assign\n    #   each to a variable name instead of getting\n    #   a list\n    left, right = line.strip().split(&#039;,&#039;)\n    left, right = int(left), int(right)\n\n    if left &gt; right :\n        print(&quot;%d &gt; %d&quot; % (left, right))\n    elif left &lt; right :\n        print(&quot;%d &gt; %d&quot; % (right, left))\n    else :\n        print(&quot;%d = %d&quot; % (right, left))\n\nfp.close()\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;- Files, Practice #<\/strong>3<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfp = open(&quot;practice.txt&quot;, &quot;r&quot;)\n\nmysum = 0\n\nfor line in fp :\n    tokens = line.strip().split(&#039;,&#039;)\n\n    x = int(tokens&#x5B;-1])\n    mysum += x\n    print(&quot;adding {}&quot;.format(x))\n\nfp.close()\n\nprint(&quot;sum = {}&quot;.format(mysum))\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;- Files, Practice #<\/strong>4<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfp = open(&quot;practice.txt&quot;, &quot;r&quot;)\n\nanimals = &#x5B; ]\n\nfor line in fp :\n    tokens = line.strip().split(&#039;,&#039;)\n    for t in tokens :\n        animals.append(t)\n\nfp.close()\n\nanimals.sort()\n\nfor a in animals :\n    print(a)\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;- Dictionaries, Practice #1<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nd = {&quot;Ralph&quot; : 7,\n     &quot;Suzy&quot; : 42,\n     &quot;Mike&quot; : 55,\n     &quot;Allison&quot; : 13}\n\nfor k in d :\n    print(&quot;{} is {} years old&quot;.format(k, d&#x5B;k]))\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;- Dictionaries, Practice #<\/strong>2<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nd = {&quot;Ralph&quot; : 7,\n     &quot;Suzy&quot; : 42,\n     &quot;Mike&quot; : 55,\n     &quot;Allison&quot; : 13}\n\nfor k in d :\n    if d&#x5B;k] &gt; 20 :\n        print(&quot;{} is {} years old&quot;.format(k, d&#x5B;k]))\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;- Dictionaries, Practice #<\/strong>3<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nd = {&quot;Ralph&quot; : 77,\n     &quot;Suzy&quot; : 42,\n     &quot;Mike&quot; : 55,\n     &quot;Allison&quot; : 63}\n\n&quot;&quot;&quot;\n    there are several ways to do this\n    this way requires no assumptions about the\n    initial value of the min age\n&quot;&quot;&quot;\n\ninitialize = True\n\nfor k in d :\n    if initialize :  # check if first person\n        minName = k\n        minAge  = d&#x5B;k]\n        initialize = False\n    elif d&#x5B;k] &lt; minAge :\n        minName = k\n        minAge  = d&#x5B;k]\n\nprint(&quot;{} is {} years old&quot;.format(minName, minAge) )\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;- Dictionaries, Practice #<\/strong>4<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nd = {&quot;rabbit&quot; : 7,\n     &quot;dog&quot; : 42,\n     &quot;mouse&quot; : 55,\n     &quot;cat&quot; : -23,\n     &quot;horse&quot; : 13}\n\nmysum = 0\nfor k in d :\n    if k &gt; &#039;f&#039; :\n        mysum += d&#x5B;k]\n\nprint(&quot;sum = %d&quot; % mysum)\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;- Dictionaries, Practice #5<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nd = {}\n\nfp = open(&quot;names.csv&quot;, &quot;r&quot;)\nfor line in fp :\n    name, age = line.strip().split(&#039;,&#039;)\n    d&#x5B;name] = int(age)\n\nfp.close()\n\nmysum = 0\nkeys = sorted( d.keys() )\n\nfor k in keys :\n    print(&quot;{} is {} years old&quot;.format(k, d&#x5B;k]))\n    mysum += d&#x5B;k]\n\nprint(&quot;\\nthe sum of the ages is %d&quot; % mysum)\n<\/pre><\/div>\n\n\n<p><strong>&#8212;&#8212;&#8212;- Dictionaries, Practice #<\/strong>6<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef findMax(d, term) :\n    mymax = 0\n\n    for k in d :\n        if d&#x5B;k]&#x5B;term] &gt; mymax :\n            mymax = d&#x5B;k]&#x5B;term]\n\n    print(&quot;max %s is %d&quot; % (term, mymax) )\n\n#####  main  #####\nd = {&quot;Tom&quot;   : {&quot;age&quot; : 43, &quot;weight&quot; : 185},\n     &quot;John&quot;  : {&quot;age&quot; : 50, &quot;weight&quot; : 170},\n     &quot;Susan&quot; : {&quot;age&quot; : 35, &quot;weight&quot; : 125} }\n\nfindMax(d, &quot;age&quot;)\nfindMax(d, &quot;weight&quot;)\n<\/pre><\/div>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Note: If you already know how to program in another language, feel that the guide below is too slow for you, or just want to see more code examples then you may want to check out my Python Crash Course. The purpose of this page is to guide someone wishing to learn to program using the Python programming language. Each section consists of a link to a video that teaches an element of the language sample problems for you to&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.brezeale.com\/?p=286\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[9,5],"tags":[],"class_list":["post-286","post","type-post","status-publish","format-standard","hentry","category-programming","category-python-programming"],"_links":{"self":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts\/286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=286"}],"version-history":[{"count":10,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts\/286\/revisions"}],"predecessor-version":[{"id":685,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts\/286\/revisions\/685"}],"wp:attachment":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}