{"id":2010,"date":"2024-01-30T00:00:07","date_gmt":"2024-01-30T00:00:07","guid":{"rendered":"https:\/\/www.tutavo.com\/Kachina\/?p=2010"},"modified":"2024-03-03T04:26:10","modified_gmt":"2024-03-03T04:26:10","slug":"learn-python-notes-31-40","status":"publish","type":"post","link":"https:\/\/www.tutavo.com\/Kachina\/learn-python-notes-31-40\/","title":{"rendered":"Learn Python &#8211; NOTES 31 &#8211; 40"},"content":{"rendered":"<p><strong>Program 31 -Break and Continue<br \/>\n<\/strong><\/p>\n<p><strong><span style=\"font-family: terminal, monaco, monospace; font-size: 10pt;\"># break &#8211; example<\/span><\/strong><br \/>\n<strong><span style=\"font-family: terminal, monaco, monospace; font-size: 10pt;\">print(&#8220;The break instruction:&#8221;)<\/span><\/strong><br \/>\n<strong><span style=\"font-family: terminal, monaco, monospace; font-size: 10pt;\">for i in range(1, 6):<\/span><\/strong><br \/>\n<strong><span style=\"font-family: terminal, monaco, monospace; font-size: 10pt;\">if i == 3:<\/span><\/strong><br \/>\n<strong><span style=\"font-family: terminal, monaco, monospace; font-size: 10pt;\">break<\/span><\/strong><br \/>\n<strong><span style=\"font-family: terminal, monaco, monospace; font-size: 10pt;\">print(&#8220;Inside the loop.&#8221;, i)<\/span><\/strong><br \/>\n<strong><span style=\"font-family: terminal, monaco, monospace; font-size: 10pt;\">print(&#8220;Outside the loop.&#8221;)<\/span><\/strong><\/p>\n<p><strong><span style=\"font-family: terminal, monaco, monospace; font-size: 10pt;\"># continue &#8211; example<br \/>\n<\/span><span style=\"font-family: terminal, monaco, monospace; font-size: 10pt;\">print(&#8220;\\nThe continue instruction:&#8221;)&nbsp; #NOTE: Puts a space between<\/span><\/strong><br \/>\n<strong><span style=\"font-family: terminal, monaco, monospace; font-size: 10pt;\">for i in range(1, 6):<\/span><\/strong><br \/>\n<strong><span style=\"font-family: terminal, monaco, monospace; font-size: 10pt;\">if i == 3:<\/span><\/strong><br \/>\n<strong><span style=\"font-family: terminal, monaco, monospace; font-size: 10pt;\">continue<\/span><\/strong><br \/>\n<strong><span style=\"font-family: terminal, monaco, monospace; font-size: 10pt;\">print(&#8220;Inside the loop.&#8221;, i)<\/span><\/strong><br \/>\n<strong><span style=\"font-family: terminal, monaco, monospace; font-size: 10pt;\">print(&#8220;Outside the loop.&#8221;)<\/span><\/strong><\/p>\n<p><strong>Program 32 &#8211; Kind of a Bubble Sort one Step at a Time<br \/>\n<\/strong><\/p>\n<p><strong><span style=\"font-family: courier new, courier, monospace; font-size: 10pt;\">largest_number = -99999999 # Initialize variable<\/span><\/strong><br \/>\n<strong><span style=\"font-family: courier new, courier, monospace; font-size: 10pt;\">counter = 0 # Initialize counter<\/span><\/strong><\/p>\n<p><strong><span style=\"font-family: courier new, courier, monospace; font-size: 10pt;\">while True:<\/span><\/strong><br \/>\n<strong><span style=\"font-family: courier new, courier, monospace; font-size: 10pt;\">number = int(input(&#8220;Enter a number or type -1 to end program: &#8220;))<\/span><\/strong><br \/>\n<strong><span style=\"font-family: courier new, courier, monospace; font-size: 10pt;\">if number == -1:<\/span><\/strong><br \/>\n<strong><span style=\"font-family: courier new, courier, monospace; font-size: 10pt;\">break<\/span><\/strong><br \/>\n<strong><span style=\"font-family: courier new, courier, monospace; font-size: 10pt;\">counter += 1<\/span><\/strong><br \/>\n<strong><span style=\"font-size: 10pt;\"><span style=\"font-family: courier new, courier, monospace;\">if number &gt; largest_number:<br \/>\n<\/span><span style=\"font-family: courier new, courier, monospace;\">largest_number = number<br \/>\n# Does not keep all numbers in memory<br \/>\n<\/span><span style=\"font-family: courier new, courier, monospace;\"># only a single number after evaluation<\/span><\/span><\/strong><\/p>\n<p><strong><span style=\"font-family: courier new, courier, monospace; font-size: 10pt;\">if counter != 0:<\/span><\/strong><br \/>\n<strong><span style=\"font-family: courier new, courier, monospace; font-size: 10pt;\">print(&#8220;The largest number is&#8221;, largest_number)<\/span><\/strong><br \/>\n<strong><span style=\"font-family: courier new, courier, monospace; font-size: 10pt;\">else:<\/span><\/strong><br \/>\n<strong><span style=\"font-family: courier new, courier, monospace; font-size: 10pt;\">print(&#8220;You haven&#8217;t entered any number.&#8221;)<\/span><\/strong><\/p>\n<p><strong>Program 33 -Same as Program 32, but using CONTINUE<br \/>\n<\/strong><\/p>\n<p><strong><span style=\"font-size: 10pt; font-family: courier new, courier, monospace;\">largest_number = -99999999<\/span><\/strong><br \/>\n<strong><span style=\"font-size: 10pt; font-family: courier new, courier, monospace;\">counter = 0<\/span><\/strong><\/p>\n<p><strong><span style=\"font-size: 10pt; font-family: courier new, courier, monospace;\">number = int(input(&#8220;Enter a number or type -1 to end program: &#8220;))<\/span><\/strong><\/p>\n<p><strong><span style=\"font-size: 10pt; font-family: courier new, courier, monospace;\">while number != -1:<\/span><\/strong><br \/>\n<strong><span style=\"font-size: 10pt; font-family: courier new, courier, monospace;\">if number == -1:<\/span><\/strong><br \/>\n<strong><span style=\"font-size: 10pt; font-family: courier new, courier, monospace;\">continue<\/span><\/strong><br \/>\n<strong><span style=\"font-size: 10pt; font-family: courier new, courier, monospace;\">counter += 1<\/span><\/strong><\/p>\n<p><strong><span style=\"font-size: 10pt; font-family: courier new, courier, monospace;\">if number &gt; largest_number:<\/span><\/strong><br \/>\n<strong><span style=\"font-size: 10pt; font-family: courier new, courier, monospace;\">largest_number = number<\/span><\/strong><br \/>\n<strong><span style=\"font-size: 10pt; font-family: courier new, courier, monospace;\">number = int(input(&#8220;Enter a number or type -1 to end program: &#8220;))<\/span><\/strong><\/p>\n<p><strong><span style=\"font-size: 10pt; font-family: courier new, courier, monospace;\">if counter:<\/span><\/strong><br \/>\n<strong><span style=\"font-size: 10pt; font-family: courier new, courier, monospace;\">print(&#8220;The largest number is&#8221;, largest_number)<\/span><\/strong><br \/>\n<strong><span style=\"font-size: 10pt; font-family: courier new, courier, monospace;\">else:<\/span><\/strong><br \/>\n<strong><span style=\"font-size: 10pt; font-family: courier new, courier, monospace;\">print(&#8220;You haven&#8217;t entered any number.&#8221;)<\/span><\/strong><\/p>\n<p><strong>Program 34 &#8211; Practice program for BREAK<br \/>\n<\/strong><\/p>\n<p><strong><span style=\"font-family: courier new, courier, monospace; font-size: 10pt;\">my_word = &#8220;xxx&#8221;<\/span><\/strong><br \/>\n<strong><span style=\"font-family: courier new, courier, monospace; font-size: 10pt;\">sentence = &#8220;&#8221;<\/span><\/strong><\/p>\n<p><strong><span style=\"font-family: courier new, courier, monospace; font-size: 10pt;\">print (&#8220;xxx to quit\\n&#8221;)<\/span><\/strong><\/p>\n<p><strong><span style=\"font-family: courier new, courier, monospace; font-size: 10pt;\">while True:<\/span><\/strong><br \/>\n<strong><span style=\"font-family: courier new, courier, monospace; font-size: 10pt;\">one_word = input(&#8220;Enter a word: &#8220;)<\/span><\/strong><br \/>\n<strong><span style=\"font-family: courier new, courier, monospace; font-size: 10pt;\">if one_word == &#8220;xxx&#8221;:<\/span><\/strong><br \/>\n<strong><span style=\"font-family: courier new, courier, monospace; font-size: 10pt;\">print (&#8220;\\n&#8221; + sentence) <\/span><\/strong><br \/>\n<strong><span style=\"font-family: courier new, courier, monospace; font-size: 10pt;\">break<\/span><\/strong><br \/>\n<strong><span style=\"font-family: courier new, courier, monospace; font-size: 10pt;\">sentence = sentence + &#8221; &#8221; + one_word<\/span><\/strong><\/p>\n<p><strong>Program 35 &#8211; Practice program for CONTINUE<br \/>\n<\/strong><\/p>\n<p>REDO LABS previous lab (3.1.2.10) AND previous lab (3.1.2.11)<br \/>\nI DO NOT GET CONTINUE AT ALL<\/p>\n<p><strong>Program 36 -Import Sys and Exit and While<br \/>\n<\/strong><\/p>\n<p>import sys<\/p>\n<p>i = 1<br \/>\nwhile i &lt; 5:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp; print(i)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp; i += 1<br \/>\nelse:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp; sys.exit()<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp; print(&#8220;else:&#8221;, i)<\/p>\n<p>&nbsp;<\/p>\n<p>for i in range(5):<br \/>\nprint(i)<br \/>\nelse:<br \/>\ni+=1<br \/>\nprint(&#8220;else:&#8221;, i)<\/p>\n<p>print (&#8220;\\nSecond Test\\n&#8221;)<\/p>\n<p>i = 111<br \/>\nfor i in range(2, 1):<br \/>\nprint(i)<br \/>\nelse:<br \/>\nprint(&#8220;else:&#8221;, i)<\/p>\n<p><strong>Program 37 -Temperature Conversion Program<br \/>\n<\/strong><\/p>\n<p>import sys<br \/>\nMyExit = &#8220;Exit&#8221;<\/p>\n<p>while MyExit != &#8220;X&#8221;:<br \/>\n&nbsp;&nbsp;&nbsp; MyTitle = &#8220;Temperature Conversion Program&#8221;<br \/>\n&nbsp;&nbsp;&nbsp; MyInstructions = &#8220;Enter C if convertion from C to F.\\n&#8221;<br \/>\n&nbsp;&nbsp;&nbsp; MyInstructions = MyInstructions + &#8220;Enter F if converting from F to C\\n&#8221;<br \/>\n&nbsp;&nbsp;&nbsp; MyInstructions = MyInstructions + &#8220;Enter X to Exit&#8221;<br \/>\n&nbsp;&nbsp;&nbsp; print (&#8220;\\n&#8221;,MyTitle,&#8221;\\n&#8221;)<br \/>\n&nbsp;&nbsp;&nbsp; print (MyInstructions+&#8221;\\n&#8221;)<br \/>\n&nbsp;&nbsp;&nbsp; MyPath=input(&#8220;Enter C or F: &#8220;)<br \/>\n&nbsp;&nbsp;&nbsp; if MyPath == &#8220;C&#8221;:<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; StartTemp=int(input(&#8220;Enter temperature: &#8220;))<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; MyOutput = (StartTemp*1.8)+32<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; ResultPath=&#8221;F&#8221;<br \/>\n&nbsp;&nbsp;&nbsp; elif MyPath == &#8220;F&#8221;:<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; StartTemp=int(input(&#8220;Enter temperature: &#8220;))<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; MyOutput = (StartTemp-32) * (5\/9)<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; ResultPath=&#8221;C&#8221;<br \/>\n&nbsp;&nbsp;&nbsp; else:<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; sys.exit()<br \/>\n&nbsp;&nbsp;&nbsp; print(StartTemp, MyPath, &#8220;is the same as&#8221;,MyOutput,ResultPath)<\/p>\n<p><strong>Program 38 &#8211; A look at Printing<br \/>\n<\/strong><\/p>\n<ul>\n<li><code class=\"codep \">name = input(<mark>\"Enter your name: \"<\/mark>) # Your Name<\/code><\/li>\n<li><code class=\"codep \">print(\"Hello, \" + name + \". Nice to meet you!\") #<\/code><code class=\"codep \">Hello, XXXX. Nice to meet you!<\/code><code class=\"codep \"><\/code><\/li>\n<li><code class=\"codep \">print(\"Hello, \",name ,\". Nice to meet you!\") # Hello, XXXX . Nice to meet you!<\/code><\/li>\n<li><code class=\"codep syntax-color\"><\/code><span class=\"ace_keyword\">print<\/span><span class=\"ace_paren ace_lparen\">(<\/span><span class=\"ace_string\">&#8220;<\/span><span class=\"ace_constant ace_language ace_escape\">\\n<\/span><span class=\"ace_string\">Press Enter to end the program.&#8221;<\/span><span class=\"ace_paren ace_rparen\">)<\/span><code class=\"codep syntax-color\"><\/code><span class=\"ace_support ace_function\"><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; input<\/span><span class=\"ace_paren ace_lparen\">(<\/span><span class=\"ace_paren ace_rparen\">)<\/span><code class=\"codep syntax-color\"><\/code><span class=\"ace_keyword\"><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print<\/span><span class=\"ace_paren ace_lparen\">(<\/span><span class=\"ace_string\">&#8220;THE END.&#8221;<\/span><span class=\"ace_paren ace_rparen\">)<\/span><\/li>\n<li><code class=\"codep syntax-color\"><\/code><span class=\"ace_identifier\">num_1<\/span> <span class=\"ace_keyword ace_operator\">=<\/span> <span class=\"ace_support ace_function\">input<\/span><span class=\"ace_paren ace_lparen\">(<\/span><span class=\"ace_string\">&#8220;Enter the first number: &#8220;<\/span><span class=\"ace_paren ace_rparen\">)<\/span> <span class=\"ace_comment\"># Enter 12<br \/>\n<\/span><span class=\"ace_identifier\">num_2<\/span> <span class=\"ace_keyword ace_operator\">=<\/span> <span class=\"ace_support ace_function\">input<\/span><span class=\"ace_paren ace_lparen\">(<\/span><span class=\"ace_string\">&#8220;Enter the second number: &#8220;<\/span><span class=\"ace_paren ace_rparen\">)<\/span> <span class=\"ace_comment\"># Enter 21<br \/>\n<\/span><span class=\"ace_keyword\">print<\/span><span class=\"ace_paren ace_lparen\">(<\/span><span class=\"ace_identifier\">num_1<\/span> <span class=\"ace_keyword ace_operator\">+<\/span> <span class=\"ace_identifier\">num_2<\/span><span class=\"ace_paren ace_rparen\">)<\/span> <span class=\"ace_comment\"># the program returns 1221<\/span><\/p>\n<div class=\"ace_line\">&nbsp;<\/div>\n<\/li>\n<li>\n<div class=\"ace_line\">You can also multiply (<code>*<\/code> \u2012 replication) strings, e.g.:<br \/>\n<span class=\"ace_identifier\">my_input<\/span> <span class=\"ace_keyword ace_operator\">=<\/span> <span class=\"ace_support ace_function\">input<\/span><span class=\"ace_paren ace_lparen\">(<\/span><span class=\"ace_string\">&#8220;Enter something: &#8220;<\/span><span class=\"ace_paren ace_rparen\">)<\/span> <span class=\"ace_comment\"># Example input: hello<br \/>\n<\/span><span class=\"ace_keyword\">print<\/span><span class=\"ace_paren ace_lparen\">(<\/span><span class=\"ace_identifier\">my_input<\/span> <span class=\"ace_keyword ace_operator\">*<\/span> <span class=\"ace_constant ace_numeric\">3<\/span><span class=\"ace_paren ace_rparen\">)<\/span> <span class=\"ace_comment\"># Expected output: hellohellohello<\/span><\/div>\n<\/li>\n<\/ul>\n<p><code class=\"codep \"><\/code><\/p>\n<p><strong>Program 39 -using f to format and braces to create dictionary<\/strong><\/p>\n<p>start_hour = int(input(&#8220;Enter the starting hour (0-23): &#8220;))<br \/>\nstart_minute = int(input(&#8220;Enter the starting minute (0-59): &#8220;))<br \/>\nduration_minutes = int(input(&#8220;Enter the duration in minutes: &#8220;))<\/p>\n<p>total_minutes = start_hour * 60<br \/>\ntotal_minutes = total_minutes + start_minute<br \/>\ntotal_minutes = total_minutes + duration_minutes<br \/>\n# OR total_minutes = start_hour * 60 + start_minute + duration_minutes<\/p>\n<p>print(total_minutes)<br \/>\nprint(total_minutes\/60)<br \/>\nend_hour = total_minutes \/\/ 60 # Drops the remainder<br \/>\nprint(end_hour)<br \/>\nend_minute = total_minutes % 60 # Leaves only the remainder<br \/>\nprint(str(end_minute)+&#8221;\\n&#8221;)<br \/>\nprint(f&#8221;The event will end at {end_hour % 24}:{end_minute:02d}&#8221;)<br \/>\n# print({end_hour % 24}:{end_minute:02d}) # THIS GENERATES AN ERROR<br \/>\n# print(f{end_hour % 24}:{end_minute:02d}) # THIS GENERATES AN ERROR<br \/>\nprint(&#8220;{end_hour % 24}:{end_minute:02d}&#8221;) # {} PRINTS EVERYTHING IN QUOTES<br \/>\nprint(f&#8221;{end_hour % 24}:{end_minute:02d}&#8221;) # {} CREATES A DICTIONARY<\/p>\n<p><strong>Program 40 &#8211; True False == &nbsp;&nbsp; != &nbsp;&nbsp; &lt;= &nbsp; &nbsp; &gt;=<br \/>\n<\/strong><\/p>\n<p>n = int(input(&#8220;Enter a number:&#8221;))<br \/>\nprint(n&gt;=100)<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Program 31 -Break and Continue # break &#8211; example print(&#8220;The break instruction:&#8221;) for i in range(1, 6): if i == 3: break print(&#8220;Inside the loop.&#8221;, i) print(&#8220;Outside the loop.&#8221;) # continue &#8211; example print(&#8220;\\nThe continue instruction:&#8221;)&nbsp; #NOTE: Puts a space between for i in range(1, 6): if i == 3: continue print(&#8220;Inside the loop.&#8221;, i) &hellip; <a href=\"https:\/\/www.tutavo.com\/Kachina\/learn-python-notes-31-40\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Learn Python &#8211; NOTES 31 &#8211; 40&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[9],"tags":[],"class_list":["post-2010","post","type-post","status-publish","format-standard","hentry","category-educational"],"_links":{"self":[{"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/posts\/2010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/comments?post=2010"}],"version-history":[{"count":15,"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/posts\/2010\/revisions"}],"predecessor-version":[{"id":2032,"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/posts\/2010\/revisions\/2032"}],"wp:attachment":[{"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/media?parent=2010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/categories?post=2010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/tags?post=2010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}