{"id":163,"date":"2024-04-05T11:04:04","date_gmt":"2024-04-05T11:04:04","guid":{"rendered":"https:\/\/paradoxa.me\/?p=163"},"modified":"2024-04-17T16:43:02","modified_gmt":"2024-04-17T16:43:02","slug":"py4e-python-course-by-dr-chuck","status":"publish","type":"post","link":"https:\/\/paradoxa.me\/index.php\/2024\/04\/05\/py4e-python-course-by-dr-chuck\/","title":{"rendered":"~ PY4E (Python Course by Dr.Chuck)"},"content":{"rendered":"<h1 style=\"text-align: center;\">PY4E &#8211; Python For Everybody<\/h1>\n<h2 style=\"text-align: center;\">Learning Python with Dr.Chuck free course<\/h2>\n<h3 style=\"text-align: center;\">First of all I would like to thank D. Chuck for sharing his knowledge by creating this free online course<\/h3>\n<pre style=\"text-align: center;\">NOTE: The following are just my study note.\r\n\r\n<\/pre>\n<h2>#1 &#8211; Why use a Programming language<\/h2>\n<p><span style=\"font-size: medium;\">Programmer write a sequence of instructions or code to solve problems.<br \/>\n<\/span><span style=\"font-size: medium;\">Computers takes typing literally so they will do not accept or recognize errors.<\/span><\/p>\n<h3><span style=\"font-size: medium;\">~ What the Hardware does:<\/span><\/h3>\n<p><span style=\"font-size: medium;\">&#8211; CPU ask for instruction<br \/>\n<\/span><span style=\"font-size: medium;\">&#8211; MEMORY RAM store the instructions (faster temporary storage)<br \/>\n<\/span><span style=\"font-size: medium;\">&#8211; SECONDARY MEMORY (HDD\u2019s or SSD\u2019s) Store the instruction (slow permanent storage)<br \/>\n<\/span><span style=\"font-size: medium;\">&#8211; MOTHERBOARD connects all the components<\/span><\/p>\n<h3><span style=\"font-size: medium;\">~ What the Language Software does : <\/span><\/h3>\n<p><span style=\"font-size: medium;\">&#8211; You write a file\/program<br \/>\n<\/span><span style=\"font-size: medium;\">&#8211; It get loaded into RAM<br \/>\n<\/span><span style=\"font-size: medium;\">&#8211; Python will translate the language so the CPU understand it<br \/>\n<\/span><span style=\"font-size: medium;\">&#8211; Once the program is terminated the python will translate back to you the answer<\/span><\/p>\n<h3><span style=\"font-size: medium;\">~ Python (ATOM text editor to write Python is recommended in the course)<\/span><\/h3>\n<p><span style=\"font-size: medium;\">You can run python inside the terminal, but generally you will type the script on a text editor and run it after you save the file with a .py extension.<br \/>\nIf you type a mistake python will display a \u201csyntax error\u201d message to let you know where the mistake was made.<\/span><\/p>\n<h3><span style=\"font-size: medium;\">~ Elements of python: <\/span><\/h3>\n<p><span style=\"font-size: medium;\">&#8211; Vocabulary\/words : reserved and variables<br \/>\n<\/span><span style=\"font-size: medium;\">&#8211; Sentence structure : valid syntax patterns<br \/>\n<\/span><span style=\"font-size: medium;\">&#8211; Story structure : construct a purposeful program<\/span><\/p>\n<h3><span style=\"font-size: medium;\">~ Vocabulary Reserved Words (you can\u2019t use the following for other purposes): <\/span><\/h3>\n<p><span style=\"font-size: medium;\"><em>False \u2013 class \u2013 return \u2013 is \u2013 finally \u2013 None \u2013 if \u2013 for \u2013 lambda \u2013 continue \u2013 True \u2013 def \u2013 from \u2013 while \u2013 nonlocal \u2013 and \u2013 del \u2013 global \u2013 not \u2013 with \u2013 as \u2013 elif \u2013 try \u2013 or \u2013 yield \u2013 assert \u2013 else \u2013 import \u2013 pass \u2013 break \u2013 except \u2013 in \u2013 raise.<\/em> <\/span><\/p>\n<h3><span style=\"font-size: medium;\">~ Sentences or Lines:<\/span><\/h3>\n<p><span style=\"font-size: medium;\">Example: (<strong>#<\/strong> value means is a comment and python skips what\u2019s after)<br \/>\n<\/span><em><span style=\"font-size: medium;\">x = 2\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0 \u00a0 #assignment statement<br \/>\n<\/span><span style=\"font-size: medium;\">x = x + 2\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #assignment with expression<br \/>\n<\/span><\/em><span style=\"font-size: medium;\"><em>print(x)\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #print statement<\/em> <\/span><\/p>\n<h3><span style=\"font-size: medium;\">~ Structure: <\/span><\/h3>\n<p><span style=\"font-size: medium;\">The script can be in a sequence (step after step), conditional (<strong>If <\/strong>followed variables), repeated or just few lines can be repeated over and over.<br \/>\nA program can combine different lines together.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2>#2 &#8211; Variables, Expression and Statement<\/h2>\n<h3><span style=\"font-size: medium;\">~ Constants:<\/span><\/h3>\n<p><span style=\"font-size: medium;\">constant are numbers, letters and strings (string use single quote (\u2018) or double quote (\u201c))<\/span><\/p>\n<h3><span style=\"font-size: medium;\">~ Variables:<\/span><\/h3>\n<p><span style=\"font-size: medium;\">Basically you ask python to save specific digit for determinate variables.<br \/>\n<\/span><span style=\"font-size: medium;\">Examples: <\/span><span style=\"font-size: medium;\"><em>x = 2<\/em> , but remember that if after you follow up with <em>x = 5<\/em> it will overwrite the new value over the old one.<br \/>\n<\/span><\/p>\n<p><span style=\"font-size: medium;\">Variables must start with letter or _ and can be written with letter, underscores and numbers (case sensitive)<br \/>\n<\/span><span style=\"font-size: medium;\">&#8211; Accepted Examples:\u00a0 <em>variables\u00a0\u00a0 variables21\u00a0\u00a0 _variables<\/em><br \/>\n&#8211; <\/span><span style=\"font-size: medium;\">Unaccepted Examples:\u00a0\u00a0 2<em>1variables \u00a0 @variables\u00a0\u00a0 varia.bles<\/em><br \/>\n&#8211; <\/span><span style=\"font-size: medium;\">Accepted but preferably to avoid:\u00a0 <em>vArIaBlElS \u00a0 Variables\u00a0\u00a0 VARIABLES<\/em><\/span><\/p>\n<p><span style=\"font-size: medium;\">But is good practice to use Mnemonic Variables Names (mnemonic = memory aid), so they are more understandable to everybody as variable.<br \/>\nInstead, for example, of using \u201cx\u201d we can use \u201c_hours\u201d if we refer to hours.<\/span><\/p>\n<h3><span style=\"font-size: medium;\">~ Sentences or Lines<\/span><\/h3>\n<p><span style=\"font-size: medium;\">Assignment statement:<br \/>\n<\/span><span style=\"font-size: medium;\">we use = to assign variables <em>x = 2<\/em> or <em>x = 2 + x * 3 \/ 4<\/em><br \/>\nthey can be numeric expression made with: (), % (remainder) ** (power of), * \/, + &#8211; . In order of importance, and if they are the same level it goes from left to right.<\/span><\/p>\n<p><strong><span style=\"font-size: medium;\">~ Type<\/span><\/strong><\/p>\n<p><span style=\"font-size: medium;\">Python recognize difference between numbers and string. But it can no compute an operation with string and number (trackback\/type error).<br \/>\nBy writing <b>type<\/b>, python will describe which type the constant is: \u2018int\u2019=1 \u2018float\u2019=1.0 \u2018str\u2019=one .<\/span><\/p>\n<p><span style=\"font-size: medium;\">But you can convert for example:<br \/>\n<em>float(1) \/ 1 = 1.0 \/ 1 = 1.0<\/em><\/span><\/p>\n<p><span style=\"font-size: medium;\">You can convert <em>when possible<\/em> with int() or float() like the example above.<\/span><\/p>\n<p><strong><span style=\"font-size: medium;\">~ Input<\/span><\/strong><\/p>\n<p><span style=\"font-size: medium;\">when we type <b>input()<\/b>, python will stop as is asking the user a question (that will return as a string) and after the user answer it will complete the code.<br \/>\nExample:<br \/>\n<em>_name = input(\u2018who are you? \u2019)\u00a0\u00a0 \u00a0 \u00a0\u00a0 #here user will digit their name and press enter<\/em><br \/>\n<\/span><span style=\"font-size: medium;\"><em>print(\u2018Welcome\u2019, _name)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0\u00a0 #It will print: Welcome , Username<\/em><br \/>\n<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2>#3 &#8211; Conditional Execution<\/h2>\n<h3><span style=\"font-size: medium;\">~ Conditional statement<\/span><\/h3>\n<p><span style=\"font-size: medium;\">We use <strong>if<\/strong> to create a condition follow by the following simbols: <strong>&lt;<\/strong> (less than), <strong>&lt;=<\/strong> (less or equal), <strong>==<\/strong> (equal), <strong>&gt;=<\/strong> (greater or equal), <strong>&gt;<\/strong> (greater) and <strong>!=<\/strong> (not equal)<br \/>\n<\/span><span style=\"font-size: medium;\">Example:<br \/>\n<em>~x = 5<\/em><br \/>\n<em>~if x &lt; 10:<\/em><br \/>\n<\/span><em><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 print(\u2018Smaller\u2019)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # The space in front is an indent<br \/>\n<\/span><span style=\"font-size: medium;\">~if x &gt; 20:<br \/>\n~\u00a0\u00a0\u00a0 print(\u2018Bigger\u2019)<br \/>\n<\/span><span style=\"font-size: medium;\">~print(\u2018Finito\u2019)<\/span><\/em><\/p>\n<h3><span style=\"font-size: medium;\">~ Block and indentations (4 spaces or 1 tab in atom text)<\/span><\/h3>\n<p><span style=\"font-size: medium;\">A block is a set of lines maintained with indentations after <b>if<\/b> or <b>for<\/b>.<br \/>\nThe indentation is composed by 4 spaces but in some software as Atom you can type the Tab, It&#8217;s important that the indentation has the same length to be part of the same block.<br \/>\n<\/span><\/p>\n<h4><span style=\"font-size: medium;\">One indentation:<\/span><\/h4>\n<p><em><span style=\"font-size: medium;\">~x = 5<br \/>\n~if &gt; 1<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 print (\u2018bigger than one\u2019)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #increase indentation<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 print (\u2018still bigger\u2019)<br \/>\n<\/span><span style=\"font-size: medium;\">~print (\u2018done with 2\u2019)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #decrease indentation<\/span><\/em><\/p>\n<h4><span style=\"font-size: medium;\">Double indentation or nested decision<\/span><\/h4>\n<p><em><span style=\"font-size: medium;\">~x = 5<br \/>\n~if &gt; 1<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 print (\u2018bigger than one\u2019)<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print (\u2018still bigger\u2019)<br \/>\n~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # empty line are not considered by python<br \/>\n~print (\u2018finito\u2019)<\/span><\/em><\/p>\n<h4><span style=\"font-size: medium;\">Two-way decision<br \/>\n<\/span><\/h4>\n<p>To use a two-way decision you need to use <strong>else <\/strong>, which also close the code : this one or that one<br \/>\n<em><span style=\"font-size: medium;\">~x = 4<br \/>\n~if x &gt; 2 :<br \/>\n~\u00a0\u00a0\u00a0 print(\u2018Bigger\u2019)<br \/>\n~else :<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 print(\u2018Smaller\u2019)<br \/>\n~<br \/>\n~<\/span><\/em><em><span style=\"font-size: medium;\">print(\u2018finito\u2019)<\/span><\/em><\/p>\n<h4><span style=\"font-size: medium;\">Multi-way decision<br \/>\n<\/span><\/h4>\n<p>They also called puzzle and are made with <strong>elif <\/strong>, you can have multiple elif but be sure that one condition don&#8217;t match the following condition<br \/>\n<em><span style=\"font-size: medium;\">~x = 4<br \/>\n<\/span><span style=\"font-size: medium;\">~if x &lt; 2 :<br \/>\n~\u00a0\u00a0\u00a0 print(\u2018small\u2019)<br \/>\n<\/span><span style=\"font-size: medium;\">~elif x &lt; 10 :<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 print(\u2018medium\u2019)<br \/>\n<\/span><span style=\"font-size: medium;\">~else :<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 print(\u2018large\u2019)<br \/>\n~<br \/>\n~<\/span><\/em><em><span style=\"font-size: medium;\">print(\u2018finito\u2019)<\/span><\/em><\/p>\n<h4><span style=\"font-size: medium;\">Try \/ Except structure<\/span><\/h4>\n<p><span style=\"font-size: medium;\">It surrounds a dangerous section of code (only one line recommended) so if for some reason blows, python will still run the comand till the end.<br \/>\n<\/span><em><span style=\"font-size: medium;\">~astr = \u2018hello bob\u2019<br \/>\n~try:<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 istr = int (astr) \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #you can\u2019t convert letters in numbers so the code blows up<br \/>\n<\/span><span style=\"font-size: medium;\">~except:<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 istr = -1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #but it will use -1 instead<br \/>\n<\/span><span style=\"font-size: medium;\">~print (\u2018First\u2019, istr)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # so the code will print : First -1<\/span><\/em><\/p>\n<p><em><span style=\"font-size: medium;\"><br \/>\n~astr = \u2018123\u2019<br \/>\n~try:<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 istr = int (astr)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #you can convert letters in numbers so the code will print<br \/>\n<\/span><span style=\"font-size: medium;\">~except:<br \/>\n~\u00a0\u00a0\u00a0 istr = -1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #no need to read this line now because the code worked<br \/>\n<\/span><span style=\"font-size: medium;\">~print (\u2018Second\u2019, istr)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #and will print : Second 123<\/span><\/em><\/p>\n<p>&nbsp;<\/p>\n<h2>#4 &#8211; Function<\/h2>\n<p><span style=\"font-size: medium;\">Only write function if they actually required, for 20 lines of code they are probably not necessary<\/span><\/p>\n<h3>~ def<\/h3>\n<p><span style=\"font-size: medium;\">Def function = define code<br \/>\nIt defines a set reusable code to avoid typing it over and over inside the file<\/span><\/p>\n<p><em><span style=\"font-size: medium;\">~def ciao<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 print(\u2018hello\u2019)<br \/>\n~ciao()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #hello<br \/>\n~print(\u2018how are you\u2019)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #how are you<br \/>\n~ciao()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #hello<\/span><\/em><\/p>\n<p><em><span style=\"font-size: medium;\">~def greet(lang) :<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 if lang == \u2018es\u2019\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #if the user type es it will print hola<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(\u2018hola\u2019)<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 elif lang == \u2018fr\u2019\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #if the user type fr it will print bonjour<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(\u2018bonjour\u2019)<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 else:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #if the user type any other language it will ~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print hello<br \/>\n<\/span><span style=\"font-size: medium;\">~print(\u2018hello\u2019)<\/span><\/em><\/p>\n<h3><span style=\"font-size: medium;\">~ Return value<br \/>\n<\/span><\/h3>\n<p>It used with the <strong>return <\/strong>instead of the <strong>print <\/strong>command:<\/p>\n<p><em><span style=\"font-size: medium;\">~def greet(lang) :<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 if lang == \u2018es\u2019\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #if the user type es it will return hola<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return \u2018hola\u2019<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 elif lang == \u2018fr\u2019\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #if the user type fr it will return bonjour<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return \u2018bonjour\u2019<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 else:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #if the user type any other language it will ~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return hello<br \/>\n<\/span><span style=\"font-size: medium;\">~return \u2018hello\u2019<\/span><\/em><\/p>\n<h3><span style=\"font-size: medium;\">~ Max function<\/span><\/h3>\n<p><span style=\"font-size: medium;\">Python assign a value of the letters and pick up the higher value ones with the <strong>max <\/strong>function<br \/>\n<\/span><\/p>\n<p><em><span style=\"font-size: medium;\">~big = max(\u2018hello world\u2019)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # \u2018hello world\u2019 is an argument<br \/>\n<\/span><span style=\"font-size: medium;\">~print(big)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # it will print w<\/span><\/em><\/p>\n<p>&nbsp;<\/p>\n<h2>#5 &#8211; Loops &amp; Iterations<\/h2>\n<h3>~ while<\/h3>\n<p><span style=\"font-size: medium;\">While is similar to the <strong>if<\/strong> statement but it will run till becomes F<strong>alse<\/strong>,<br \/>\nIt&#8217;s called also iteration variable and indefinite loop.<br \/>\nAvoid to create infinite iteration as the computer will keep spinning, on the other and,<br \/>\nZero Trip loop are loops that don\u2019t run.<\/span><\/p>\n<p><em><span style=\"font-size: medium;\">~n = 5<br \/>\n<\/span><span style=\"font-size: medium;\">~while n &gt; 0 :\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #it will keep running the question till is n=0<br \/>\n~\u00a0\u00a0\u00a0 print(n)<br \/>\n~\u00a0\u00a0\u00a0 n = n \u2013 1<br \/>\n~print(\u2018finito\u2019)<\/span><\/em><\/p>\n<h3>~ break<\/h3>\n<p><span style=\"font-size: medium;\">break will break the loop once the user put the selected input<br \/>\n<\/span><\/p>\n<p><em><span style=\"font-size: medium;\">~while True :<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0 \u00a0 line = (\u2018&gt; \u2019)<br \/>\n<\/span><span style=\"font-size: medium;\">~ \u00a0\u00a0 if line == \u2018done\u2019 :<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0 break \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #loops breaks when user types done<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 print(line)<br \/>\n<\/span><span style=\"font-size: medium;\">~print (\u2018finito\u2019)<\/span><\/em><\/p>\n<h3>~ continue<\/h3>\n<p><span style=\"font-size: medium;\">continue will skip everything after and go back to the specified line<\/span><\/p>\n<p><em><span style=\"font-size: medium;\">~while True:<br \/>\n~ \u00a0\u00a0 line = (\u2018&gt; \u2019)<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 if line[0] == \u2018#\u2019 :<br \/>\n<\/span><span style=\"font-size: medium;\">~ \u00a0 \u00a0 \u00a0\u00a0 continue \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0 #when user types # it will go back to line0<br \/>\n~\u00a0\u00a0\u00a0 if line == \u2018done\u2019 :<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break<br \/>\n<\/span><\/em><span style=\"font-size: medium;\"><em>~\u00a0\u00a0\u00a0 print(line)<\/em><br \/>\n<em>~print (\u2018finito\u2019)<\/em><br \/>\n<\/span><\/p>\n<h3>~ for<\/h3>\n<p><span style=\"font-size: medium;\">for is used to set definite loops, imagine the code being a contract with python where it has to run once the list of items<\/span><\/p>\n<p><em><span style=\"font-size: medium;\">~for y in [5, 4, 2, 3,1]<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 print(y)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #it will print once each 5,4,2,3,1<br \/>\n~print(\u2018finito\u2019)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #once completed will print finito<br \/>\n<\/span><\/em><\/p>\n<p><em><span style=\"font-size: medium;\">~x = [\u2018afternoon\u2019, \u2018bye\u2019, \u2018morning\u2019]<br \/>\n~for y in x :<br \/>\n<\/span><span style=\"font-size: medium;\">~ \u00a0\u00a0 print(\u2018good\u2019 , y)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #print good afternoon, good bye, good morning<br \/>\n<\/span><span style=\"font-size: medium;\">~print(\u2018finito\u2019)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #when finish the loop finito<\/span><\/em><\/p>\n<h3>~ Largest Number<\/h3>\n<p><span style=\"font-size: medium;\">Largest number can be found by assigning one variable <em>&gt;<\/em> the other<\/span><\/p>\n<p><em><span style=\"font-size: medium;\">~largest = -1<br \/>\n~print (\u2018inizio\u2019, largest)<br \/>\n<\/span><span style=\"font-size: medium;\">~for xyz in [5, 9, 2, 8, 10, 3, 4] :<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 if xyz &gt; largest :<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 largest = xyz\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #replace -1 with 5, 9, 10 in order. Skips lower numbers<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 print(largest, xyz)<br \/>\n<\/span><span style=\"font-size: medium;\">~print(\u2018fine\u2019, largest) \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #fine, 10<\/span><\/em><\/p>\n<h3>~ Counting using loops<\/h3>\n<p><em><span style=\"font-size: medium;\">~ripetizioni = 0<br \/>\n<\/span><span style=\"font-size: medium;\">~somma = 0<br \/>\n<\/span><span style=\"font-size: medium;\">~print(\u2018inizio\u2019, ripetizioni, somma)<br \/>\n<\/span><span style=\"font-size: medium;\">~for numeri in [9, 41, 12, 3, 74, 15] :<br \/>\n<\/span><span style=\"font-size: medium;\">~ \u00a0\u00a0 ripetizioni = ripetizioni + 1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #how many loops<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 somma = somma + numeri\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #total sum<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 print(ripetizioni, somma, numeri)<br \/>\n<\/span><\/em><span style=\"font-size: medium;\"><em>~print(\u2018Dopo\u2019, ripetizioni, somma, somma \/ ripetizioni) \u00a0 \u00a0 #show count, sum and average value<\/em> <\/span><\/p>\n<h3>~ Filter using if<\/h3>\n<p><em><span style=\"font-size: medium;\">~print(\u2018Prima\u2019)<br \/>\n<\/span><span style=\"font-size: medium;\">~for valore in [9, 41, 12, 3, 74, 15] :<br \/>\n<\/span><span style=\"font-size: medium;\">~ \u00a0\u00a0 if valore &gt; 20:<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(\u2018Large number\u2019, valore)<br \/>\n<\/span><span style=\"font-size: medium;\">~print(\u2018Dopo\u2019)<\/span><\/em><\/p>\n<h3>~ Boolean Variable<\/h3>\n<p><span style=\"font-size: medium;\">Variable that return only values of True or False<\/span><\/p>\n<p><em><span style=\"font-size: medium;\">~cerca = False<br \/>\n~print(\u2018Prima\u2019, cerca)<br \/>\n<\/span><span style=\"font-size: medium;\">~for valore in [4, 756, 2, 78] :<br \/>\n~ \u00a0\u00a0 if valore == 2 :<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 cerca = True \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #when value 2 is found, become True<br \/>\n~\u00a0\u00a0\u00a0 print(cerca, valore)<br \/>\n<\/span><span style=\"font-size: medium;\">~print(\u2018Dopo\u2019, cerca)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # print True<\/span><\/em><\/p>\n<h3>~ for &amp; is None<\/h3>\n<p><span style=\"font-size: medium;\">We can create a better way to find values (largest or smallest) by introducing <strong>is<\/strong> followed by <strong>None<\/strong>.<br \/>\nThe <strong>is<\/strong> operator (or <strong>is not<\/strong>) is stronger than <strong>==<\/strong> because the value has to be exactly the same (by number and type).<br \/>\n<strong>None<\/strong> is a constant and means emptiness.<\/span><\/p>\n<p><em><span style=\"font-size: medium;\">~piccolo = None<br \/>\n~for valore in [3, 2, 6, 8, 1] :<br \/>\n~ if piccolo is None :\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #no initial value is consider<br \/>\n<\/span><span style=\"font-size: medium;\">~ piccolo = valore \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #start with the first value in the list<br \/>\n<\/span><span style=\"font-size: medium;\">~ elif valore &lt; piccolo :<br \/>\n~ piccolo = valore\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #replace the value with the smallest<br \/>\n~ print piccolo, valore<br \/>\n~<br \/>\n<\/span><span style=\"font-size: medium;\">~print(\u2018dopo\u2019, piccolo)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #dopo, 1<\/span><\/em><\/p>\n<p>&nbsp;<\/p>\n<h2>#6 &#8211; Strings<\/h2>\n<p><span style=\"font-size: medium;\">A string is a sequence of characters written between <strong>\u2018 \u2019<\/strong> or <strong>\u201c \u201d<\/strong>.<br \/>\nEven if contains numbers, is still consider a string; although we can convert numbers in a string with <strong>int()<\/strong>.<br \/>\nWe can use the <strong>+<\/strong> symbols to concatenate multiple strings.<br \/>\nBecause it gives us more control we tend to read data in strings and then convert if necessary.<br \/>\nWe can also index the strings by using the <strong>[ ]<\/strong> starting with 0,1,2&#8230;:<\/span><\/p>\n<p><em><span style=\"font-size: medium;\">~nazione = \u2018united kingdom\u2019<br \/>\n~lettera = nazione[0]\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #the 0 number means the first letter<br \/>\n<\/span><span style=\"font-size: medium;\">~print(letter)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #In this case will be u (if we put 4 it will be t)<\/span><\/em><\/p>\n<p><em>~x = 3<\/em><br \/>\n<em><span style=\"font-size: medium;\">~y = [x \u2013 1]\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #we CAN\u2019T index beyond the length of the string<br \/>\n<\/span><span style=\"font-size: medium;\">~print(y)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #i<\/span><\/em><\/p>\n<h3>~ len<\/h3>\n<p><span style=\"font-size: medium;\"><strong>len<\/strong> measure the length of the string<\/span><\/p>\n<p><em><span style=\"font-size: medium;\">~nazione = \u2018united kingdom\u2019<br \/>\n~x = len(nazione)<br \/>\n<\/span><span style=\"font-size: medium;\">~print(x)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #14<\/span><\/em><\/p>\n<h3>~ Creating loops to display the string<\/h3>\n<p><em><span style=\"font-size: medium;\">~nazione = \u2018united kingdom\u2019<br \/>\n<\/span><span style=\"font-size: medium;\">~indice = 0<br \/>\n~while indice &lt; len(nazione) :<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 lettera = nazione[indice]<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 print(indice, lettera)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #0u<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 indice = indice + 1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #looping back inside the index: 1n, 2i, 3t\u2026<\/span><\/em><\/p>\n<p><em><span style=\"font-size: medium;\">~nazione = \u2018united kingdom\u2019<br \/>\n~for lettera in nazione:<br \/>\n<\/span><span style=\"font-size: medium;\">~\u00a0\u00a0\u00a0 print(lettera)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #same result as above but in an simpler and elegant way<\/span><\/em><\/p>\n<p><span style=\"font-size: medium;\">obviously we can loop and count as in the previous chapter too<\/span><\/p>\n<h3>~ Extrapolate from the string<\/h3>\n<p><span style=\"font-size: medium;\">To extrapolate part of the string use<strong> [x : y]<\/strong>\u00a0 which means from, to (but not including the last digit)<\/span><\/p>\n<p><em><span style=\"font-size: medium;\">~nazione = \u2018united kingdom\u2019<br \/>\n<\/span><span style=\"font-size: medium;\">~print(nazione[0:4])\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #unit (not include the 4<sup>th<\/sup> )<br \/>\n<\/span><span style=\"font-size: medium;\">~print(nazione[7:50])\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0 \u00a0 #kingdom (you can do it even if exceed)<br \/>\n~print(nazione[:6])\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #united (you can leave blank first and last)<\/span><\/em><\/p>\n<h3>~ Concatenation<\/h3>\n<p><em><span style=\"font-size: medium;\">~x = good<br \/>\n<\/span><span style=\"font-size: medium;\">~y = x + \u2018morning\u2019<br \/>\n~print(y)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #goodmorning (without space)<br \/>\n<\/span><span style=\"font-size: medium;\">~z = x + \u2018 \u2019 + \u2018morning\u2019<br \/>\n<\/span><span style=\"font-size: medium;\">~print(z)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #good morning (with space)<\/span><\/em><\/p>\n<h3>~ in<\/h3>\n<p><em><span style=\"font-size: medium;\">~x = \u2018united kingdom\u2019<br \/>\n<\/span><span style=\"font-size: medium;\">~\u2018nit\u2019 in x\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #True<br \/>\n<\/span><span style=\"font-size: medium;\">~\u2018b\u2019 in x\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #False<\/span><\/em><\/p>\n<h3>~ .lower &amp; .upper<\/h3>\n<p><em><span style=\"font-size: medium;\">~x = Ciao<br \/>\n~y = x.lower()<br \/>\n<\/span><span style=\"font-size: medium;\">~print(y) \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0 #ciao (all lower case)<br \/>\n~z = x.upper<br \/>\n~print(z)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #CIAO<\/span><\/em><\/p>\n<h3>~ .find<\/h3>\n<p><em><span style=\"font-size: medium;\">~x = Ciao<br \/>\n~y = x.find(\u2018a\u2019)<br \/>\n~print(y)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #2 (index position)<br \/>\n~z = x.find(\u2018m\u2019)<br \/>\n<\/span><span style=\"font-size: medium;\">~print(z)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #-1 (no character found)<\/span><\/em><\/p>\n<h3>~ .replace<\/h3>\n<p><em><span style=\"font-size: medium;\">~x = buon giorno<br \/>\n~y = x.replace(\u2018giorno\u2019 , \u2018pomeriggio\u2019)<br \/>\n~print(y)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #buon pomeriggio<br \/>\n~z = x.replace(\u2018g\u2019 , \u2018KK\u2019)<br \/>\n<\/span><span style=\"font-size: medium;\">~print(z)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #buon pomeriKKio<\/span><\/em><\/p>\n<h3>~ Stripping withspaces<\/h3>\n<p><span style=\"font-size: medium;\">Whitespaces are referred to spaces \u2018 \u2019, tab \u2018\u00a0\u00a0\u00a0 \u2019 and all the blanks.<\/span><\/p>\n<p><em><span style=\"font-size: medium;\">~x = \u2018 \u00a0\u00a0 ciao \u00a0\u00a0 \u2019<br \/>\n~x.lstrip()<br \/>\n<\/span><span style=\"font-size: medium;\">~print(x)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #\u2018ciao \u00a0\u00a0 \u2019<br \/>\n~x.rstrip()<br \/>\n~print(x)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #\u2018 \u00a0\u00a0 ciao\u2019<br \/>\n~x.strip()<br \/>\n~print(x)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #\u2018ciao\u2019<\/span><\/em><\/p>\n<h3>~ Prefixes<\/h3>\n<p><span style=\"font-size: medium;\">Basically asks does the string starts with &#8230;?<\/span><\/p>\n<p><em><span style=\"font-size: medium;\">~x = \u2018ciao come stai?\u2019<br \/>\n~x.startwith(\u2018ciao\u2019)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #True (is case sensitive)<\/span><\/em><\/p>\n<h3>~ Strings variables<\/h3>\n<p><span style=\"font-size: medium;\">Check the python documentation where\u00a0 it will explain in details at<br \/>\n( <a href=\"https:\/\/docs.python.org\/3\/library\/stdtypes.html#string-methods\" target=\"_blank\" rel=\"noopener\">https:\/\/docs.python.org\/3\/library\/stdtypes.html#string-methods<\/a> ).<br \/>\nO<\/span><span style=\"font-size: medium;\">r to check which one is availible from python:<\/span><\/p>\n<p><em><span style=\"font-size: medium;\">~x = Ciao<br \/>\n~type(x)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #\u2018str\u2019 (classified that it is a string)<br \/>\n~dir(x) \u00a0 \u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # will shows all the variables<\/span><\/em><\/p>\n<p>&nbsp;<\/p>\n<h2>#7 &#8211; Files (secondary memory)<\/h2>\n<h3>~ Text Files<\/h3>\n<p>Text file are considered a sequence of lines.<br \/>\nWe need to tell Python which file we want to use and what we will be doing with it.<br \/>\nFor this we use the <strong>open() <\/strong>function, that will return a &#8220;file handle&#8221; which is a variable use to perform operations on the file.<br \/>\n(same concept as <em>File -&gt; Open<\/em> in word).<\/p>\n<p>Example: ~fhandle = open(&#8216;filename&#8217;, &#8216;mode&#8217;) . Where mode is optional and it will be &#8216;<strong>r&#8217; <\/strong>(read) or &#8216;<strong>w&#8217; <\/strong>(write). Filename on the other hand is considered as a string.<\/p>\n<p><em>~fhand = open(&#8216;file.txt&#8217;, &#8216;r&#8217;)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #fhand is a wrapper<\/em><br \/>\n<em>~print(fhand)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #will show wrapper, name, mode, encoding<\/em><\/p>\n<p>If file doesen&#8217;t exist : traceback<\/p>\n<h3>~ \\n<\/h3>\n<p><strong>\\n<\/strong> means newline (where the line ends) and it&#8217;s considere only 1 characther<\/p>\n<p><em>~x = &#8216;y\\nz&#8217;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #will print y (new line) z<\/em><br \/>\n<em>~len(x)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #3<\/em><\/p>\n<h3>~ Reading file<\/h3>\n<p><em>~fhand = open(&#8216;file.txt&#8217;)<\/em><br \/>\n<em>~for riga in fhand:<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 print(riga)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #read all the line in the file<\/em><\/p>\n<p><em>~fhand = open(&#8216;file.txt&#8217;)<br \/>\n<\/em><em>~inp = fhand.read()<\/em><br \/>\n<em>~print(len(inp))\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #will show the number of character<br \/>\n~print (inp[:20]\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #will sho the first 20 character<br \/>\n<\/em><\/p>\n<h3>~ Counting line<\/h3>\n<p><em>~fhand = open(&#8216;file.txt&#8217;)<br \/>\n~conta = 0<br \/>\n<\/em><em>~for riga in fhand:<br \/>\n~\u00a0\u00a0\u00a0 conta = conta + 1<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 print(&#8216;numero righe:&#8217;, conta)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #numero righe : 100<br \/>\n<\/em><\/p>\n<h3>~ Searching<\/h3>\n<p><em>~fhand = open(&#8216;file.txt&#8217;)<br \/>\n~for riga in fhand:<br \/>\n~\u00a0\u00a0\u00a0 line = line.rstrip()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #to remove the \\n<br \/>\n~\u00a0\u00a0\u00a0 if riga.startwith(&#8216;From:&#8217;) :<br \/>\n~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(riga)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #will print all lines that start with from<br \/>\n<\/em><\/p>\n<p>or by using if not<\/p>\n<p><em>~fhand = open(&#8216;file.txt&#8217;)<br \/>\n~for riga in fhand:<br \/>\n~\u00a0\u00a0\u00a0 line = line.rstrip()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #to remove the \\n<br \/>\n~\u00a0\u00a0\u00a0 if not riga.startwith(&#8216;From:&#8217;) :<br \/>\n~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 continue<br \/>\n~\u00a0\u00a0\u00a0 print(riga)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #will print all lines that start with from<\/em><\/p>\n<p>or to run the same program to different file we can add the following line above using input:<\/p>\n<p>~fname = input(&#8216;file.txt&#8217;)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #file.txt now is a variable<\/p>\n<h3>~ Bad names file or if they don&#8217;t exist<\/h3>\n<p>If the file file.txt doesn&#8217;t exist we will have a TB error. to prevent:<\/p>\n<p><em>~fname = input(&#8216;file.txt&#8217;)<br \/>\n~try:<br \/>\n~\u00a0\u00a0\u00a0 fhand = open(fname<\/em><em>)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #if file exist will go to the following block<br \/>\n~except:<br \/>\n~\u00a0\u00a0\u00a0 print(&#8216;file cannot be opened:&#8217;, fname)<br \/>\n~\u00a0\u00a0\u00a0 quit()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #if doesn&#8217;t will print file cann&#8230; and then quit<br \/>\n~<br \/>\n~conta = 0<br \/>\n~for riga in fhand:<br \/>\n~\u00a0\u00a0\u00a0 conta = conta + 1<br \/>\n~\u00a0\u00a0\u00a0 print(&#8216;numero righe:&#8217;, conta)<br \/>\n<\/em><\/p>\n<p>&nbsp;<\/p>\n<h2>#8 &#8211; List []<\/h2>\n<p>List are basically collection of data. Programming can be divided in two, Algorithms (set of rules to solve problems) and Data Structures (organizing data).<\/p>\n<p>Python can run numbers, strings and Int as follows: <em>x = [&#8216;ciao&#8217;, 3, 4.5] #it always start from 0 (not 1)<br \/>\n<\/em>Even list of lists<em> x = [5, [2, 3], 9] <\/em>and empty list<em> []<br \/>\n<\/em><\/p>\n<p>Stirngs are Immutable but list are mutable<\/p>\n<h3>~ in (works the same as in string)<\/h3>\n<h3>~ len<\/h3>\n<p><em>~x = [1, 3, 5, &#8216;bob&#8217;]<\/em><br \/>\n<em>~print(len(x))\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #4<\/em><\/p>\n<h3>~ range<\/h3>\n<p><em>~x = [1, 3, 5, &#8216;bob&#8217;, 5]<\/em><br \/>\n<em>~print (range(len(x)))\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #[0, 1, 2, 3, 4]<\/em><\/p>\n<p><em>~print(range(4))\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #[0, 1, 2, 3]<\/em><\/p>\n<p>can be use with for<br \/>\n<em>~x = [1, 4, 5]<\/em><br \/>\n<em>~for y in x :<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 print(&#8216;num:&#8217;, y)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #num: 1\u00a0\u00a0\u00a0 num: 4\u00a0\u00a0\u00a0 num: 5<\/em><br \/>\n<em>~<\/em><br \/>\n<em>~for z in range(len(x)) :<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 y = x[z]<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 print(&#8216;num:&#8217;, y)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #num: 1\u00a0\u00a0\u00a0 num: 4\u00a0\u00a0\u00a0 num: 5 (same result)<\/em><\/p>\n<h3>~ concatenating<\/h3>\n<p><em>~a = [2, 4, 6]<\/em><br \/>\n<em>~b = [3, 5,]<\/em><br \/>\n<em>~c = a + b<\/em><br \/>\n<em>~print(c)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0 #[2, 4, 6, 3, 5]<\/em><\/p>\n<h3>~ sliced<\/h3>\n<p><em>~a = [2, 4, 6]<\/em><br \/>\n<em>~a[:1]\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #[2, 4]<\/em><\/p>\n<h3># list methods<\/h3>\n<p>shows all the stuff we can do with the a list<br \/>\n<em>~x = list<\/em><br \/>\n<em>~type(x)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #&lt;type &#8216;list&#8217;&gt; <\/em><br \/>\n<em>~dir(x)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #[&#8216;append&#8217;, &#8216;count&#8217;, &#8216;extend&#8217;, &#8216;index&#8217;, &#8216;insert&#8217;, &#8216;pop&#8217;, &#8216;remove&#8217;, &#8216;reverse&#8217;, &#8216;sort&#8217;]<\/em><\/p>\n<h3>~ append<\/h3>\n<p><em>~x = list()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #to create an empty list (<strong>list<\/strong> is a reserved word) <\/em><br \/>\n<em>~x.append(&#8216;1&#8217;)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #to insert 1 in the list<\/em><\/p>\n<h3>~ sort<\/h3>\n<p><em>~x = [&#8216;m&#8217;, &#8216;a&#8217;, &#8216;q&#8217;]<\/em><br \/>\n<em>~x.sort()<br \/>\n<\/em><em>~print(x)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #[&#8216;a&#8217;, &#8216;m&#8217;, &#8216;q&#8217;]<\/em><\/p>\n<h3>~ built-in function<\/h3>\n<p><em>~x = [1, 3, 5, 9, 2]<br \/>\n~print(len(x))\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #5<br \/>\n~print(max(x))\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #9<br \/>\n~print(min(x))\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #1<br \/>\n~print(sum(x))\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #21<br \/>\n~print(sum(x)\/len(x))\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #4.2<br \/>\n<\/em><\/p>\n<pre># check 4:06:05 to see some examples<\/pre>\n<h3>~ Split<\/h3>\n<p>String and list are related to each other. the following example is a double split to extract the mail.<\/p>\n<p><em>~x = &#8216;da email@provider.com date&#8217;<\/em><br \/>\n<em>~y = x.split()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #in this case it only consider whitespaces (does NOT count ,.:; etc&#8230;)<\/em><br \/>\n<em>~print(y)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #[&#8216;da&#8217;, &#8217;email@provider.com&#8217;, &#8216;date&#8217;] convert the original in different strings<br \/>\n~mail = y[1]\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #it refer to the string 1 in this case\u00a0 &#8217;email@provider.com&#8217;<br \/>\n<\/em><em>~z = x.split(@)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #in this case it split with e<\/em><br \/>\n<em>~print(z)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #[&#8217;email&#8217;, &#8216; provider&#8217;] convert the original in different strings<br \/>\n~print(z[0])\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #it will print email<br \/>\n<\/em><\/p>\n<h3>~ guardian pattern and how to fix the issues<\/h3>\n<p>Original code, it printed some but it TB at some point and we don&#8217;t know how:<\/p>\n<p><em>~abc = open(&#8216;file.txt&#8217;)<\/em><br \/>\n<em>~<\/em><br \/>\n<em>~for riga in abc:<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 riga = riga.rstrip()<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 prl = riga.split()<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 if prl[0] != &#8216;da&#8217; :<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 continue<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 print(prl[2])<\/em><\/p>\n<p>To start finding the problem we need to insert <strong>print() <\/strong>till we find where it TB<\/p>\n<p><em>~abc = open(&#8216;file.txt&#8217;)<\/em><br \/>\n<em>~<\/em><br \/>\n<em>~for riga in abc:<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 riga = riga.rstrip()<\/em><br \/>\n<strong><em>~\u00a0\u00a0\u00a0 print(&#8216;riga&#8217;, riga) \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #to check why<\/em><\/strong><br \/>\n<em>~\u00a0\u00a0\u00a0 prl = riga.split()<\/em><br \/>\n<strong><em>~\u00a0\u00a0\u00a0 print(&#8216;parola&#8217;, prl\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #to check why<\/em><\/strong><br \/>\n<em>~\u00a0\u00a0\u00a0 if prl[0] != &#8216;da&#8217; :<br \/>\n<strong>~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(&#8216;ignore&#8217;)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #to check why<\/strong><\/em><br \/>\n<em>~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 continue<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 print(prl[2])<\/em><\/p>\n<p>After running the program we discovered that in this case the problem was that some line didn&#8217;t have words on them.<\/p>\n<p><em>~abc = open(&#8216;file.txt&#8217;)<\/em><br \/>\n<em>~<\/em><br \/>\n<em>~for riga in abc:<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 riga = riga.rstrip()<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 print(&#8216;riga&#8217;, riga)<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 prl = riga.split()<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 print(&#8216;parola&#8217;, prl<br \/>\n~\u00a0\u00a0\u00a0 <strong># Guardian pattern<\/strong><br \/>\n<strong>~\u00a0\u00a0\u00a0 if len(prl) &lt; 1 :<\/strong><br \/>\n<strong>~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 continue\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #if the line has less the one word -&gt; continue<\/strong><\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 if prl[0] != &#8216;da&#8217; :<br \/>\n~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(&#8216;ignore&#8217;)<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 continue<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 print(prl[2])<\/em><\/p>\n<p>or another way like the following<\/p>\n<p><em>~abc = open(&#8216;file.txt&#8217;)<\/em><br \/>\n<em>~<\/em><br \/>\n<em>~for riga in abc:<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 riga = riga.rstrip()<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 print(&#8216;riga&#8217;, riga)<br \/>\n<strong>~\u00a0\u00a0\u00a0 if riga == &#8216; &#8216; :<\/strong><br \/>\n<strong>~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 continue\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #if the line is a blank skip the line<\/strong><\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 prl = riga.split()<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 print(&#8216;parola&#8217;, prl<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 if prl[0] != &#8216;da&#8217; :<br \/>\n~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(&#8216;ignore&#8217;)<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 continue<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 print(prl[2])<\/em><\/p>\n<p>coming back to the guardian line we can get ready of the extra, and make the guardian stronger, in this case if we try to print (in the last piece of code) a line with less than 2 words it will TB<\/p>\n<p><em>~abc = open(&#8216;file.txt&#8217;)<\/em><br \/>\n<em>~<\/em><br \/>\n<em>~for riga in abc:<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 riga = riga.rstrip()<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 prl = riga.split()<\/em><em><br \/>\n<strong>~\u00a0\u00a0\u00a0 # Guardian a little stronger<\/strong><\/em><br \/>\n<strong><em>~\u00a0\u00a0\u00a0 if len(prl) &lt; 3 :\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #by changing\u00a0 to 3 it will skip all the line will less than 3<\/em><\/strong><br \/>\n<strong><em>~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 continue<\/em><\/strong><br \/>\n<em>~\u00a0\u00a0\u00a0 if prl[0] != &#8216;da&#8217; :<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 continue<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 print(prl[2])<\/em><\/p>\n<p>or we can compound the guardian like this<\/p>\n<p><em>~abc = open(&#8216;file.txt&#8217;)<\/em><br \/>\n<em>~<\/em><br \/>\n<em>~for riga in abc:<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 riga = riga.rstrip()<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 prl = riga.split()<\/em><em><br \/>\n<strong>~\u00a0\u00a0\u00a0 # Guardian in a compund statement<\/strong><\/em><br \/>\n<strong><em>~\u00a0\u00a0\u00a0 if len(prl) &lt; 3 or prl[0] != &#8216;da&#8217; :\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #be careful as it runs in order<\/em><\/strong><br \/>\n<strong><em>~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 continue<\/em><\/strong><br \/>\n<em>~\u00a0\u00a0\u00a0 print(prl[2])<\/em><\/p>\n<p>&nbsp;<\/p>\n<h2>#9 &#8211; dictionaries or Collections<\/h2>\n<p>list: linear collection of value (in order), index are [number]<br \/>\ndictionaries: &#8220;database&#8221; of value each with it&#8217;s own value (no specific order), index {word}<br \/>\nIf you look for a key that doesn\/t exist will result in TB<\/p>\n<h3>~ create dictionaries<\/h3>\n<p><em>~x = dict()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0 #create a dictionaries or you can use: <strong>x = { }<\/strong><\/em><br \/>\n<em>~x[&#8216;pr&#8217;] = 3\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 #add 3 and name it pr<\/em><br \/>\n<em>~x[&#8216;dpp&#8217;] = 6\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0\u00a0 #add 6 and name it dpp<\/em><br \/>\n<em>~print(x)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #{&#8216;pr&#8217; : 3, &#8216;dpp&#8217; : 6}<\/em><br \/>\n<em>~print(x[&#8216;dpp&#8217;])\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0\u00a0 #6<\/em><br \/>\n<em>~x[&#8216;dpp&#8217;] = x[&#8216;dpp&#8217;] + 1 \u00a0\u00a0 #to edit dpp<\/em><br \/>\n<em>~print(x)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #{&#8216;pr&#8217; : 3, &#8216;dpp&#8217; : 7}<\/em><\/p>\n<h3>~ counting from string<\/h3>\n<p><em>~x = dict()<\/em><br \/>\n<em>~x[&#8216;primo&#8217;] = 1<\/em><br \/>\n<em>~x[&#8216;sesto&#8217;] = 1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0 #assign a number 1 to a word to start the count<\/em><br \/>\n<em>~print(x)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #x = {&#8216;primo&#8217;: 1, &#8216;sesto&#8217;: 1}<\/em><br \/>\n<em>~x[&#8216;sesto&#8217;] = x[&#8216;sesto&#8217;] + 1<\/em><br \/>\n<em>~print(x)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #x = {&#8216;primo&#8217;: 1, &#8216;sesto&#8217;: 2}<\/em><\/p>\n<p>on an existing one<\/p>\n<p><em>~x = dict()<br \/>\n~lista = [&#8216;casa&#8217;, &#8216;topo&#8217;, &#8216;cane&#8217;, &#8216;topo&#8217;, &#8216;topo&#8217;]<br \/>\n~for nome in lista :<br \/>\n~\u00a0\u00a0\u00a0 if nome not in x:<br \/>\n~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 x[nome] = 1<br \/>\n~\u00a0\u00a0\u00a0 else :<br \/>\n~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 x[nome] = x[nome] + 1<br \/>\n~print(x)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #{&#8216;casa&#8217;: 1, &#8216;topo&#8217;: 3, &#8216;cane&#8217;: 1}<br \/>\n<\/em><\/p>\n<h3>~ get<\/h3>\n<p>Used to simplify <strong>if&#8230;else&#8230; <\/strong>with one line only<\/p>\n<p><em>~x = dict()<br \/>\n~lista = [&#8216;casa&#8217;, &#8216;topo&#8217;, &#8216;cane&#8217;, &#8216;topo&#8217;, &#8216;topo&#8217;]<br \/>\n~for nome in lista :<br \/>\n~\u00a0\u00a0\u00a0<strong> x[nome] = x.get(nome, 0) +1<\/strong><br \/>\n~print(x)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #{&#8216;casa&#8217;: 1, &#8216;topo&#8217;: 3, &#8216;cane&#8217;: 1}<\/em><\/p>\n<h3>~ a counting pattern program in general<\/h3>\n<p>~x = dict()<br \/>\n~print (&#8216;enter line of text:&#8217;)<br \/>\n~riga = input(&#8216; &#8216;)<br \/>\n~<br \/>\n~lista = riga.split()<br \/>\n~<br \/>\n~print(&#8216;words:&#8217;, lista)<br \/>\n~<br \/>\n~print(&#8216;counting&#8230;&#8217;)<br \/>\n<em>~for nome in lista :<br \/>\n~\u00a0\u00a0\u00a0 x[nome] = x.get(nome, 0) +1<br \/>\n~print(x)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/em><\/p>\n<h3>~ retrieving lists of keys<\/h3>\n<p>~x = <em>{&#8216;casa&#8217;: 1, &#8216;topo&#8217;: 3, &#8216;cane&#8217;: 1}<\/em><br \/>\n~print(<strong>list<\/strong>(x))\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 # [<em>&#8216;casa&#8217;: 1, &#8216;topo&#8217;: 3, &#8216;cane&#8217;: 1]<\/em><br \/>\n~print(x.<strong>keys{}<\/strong>)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # [<em>&#8216;casa&#8217;: 1, &#8216;topo&#8217;: 3, &#8216;cane&#8217;: 1]<br \/>\n~print(x.<strong>values{}<\/strong>)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # [1, 3, 1]<br \/>\n~print(x.<strong>items()<\/strong>)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # [(&#8216;casa&#8217;: 1), (&#8216;topo&#8217;: 3), (&#8216;cane&#8217;: 1)]<\/em><\/p>\n<h3>~ two iteration variables<\/h3>\n<p>~x = <em>{&#8216;casa&#8217;: 1, &#8216;topo&#8217;: 3, &#8216;cane&#8217;: 1}<br \/>\n~for aa,bb in x.items() :<br \/>\n~\u00a0\u00a0\u00a0 print(aa, bb)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #[(aa: bb), (&#8216;aa: bb), (aa: bb)]<\/em><\/p>\n<h2>#a counting program with what we learned so far<\/h2>\n<p><em>~file = input(&#8216;enter file:&#8217;)<\/em><br \/>\n<em>~handle = open(file)<\/em><br \/>\n<em>~<\/em><br \/>\n<em>~conto = dict()<\/em><br \/>\n<em>~for riga in handle:<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 nome = riga.split()<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 for parola in nome:<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 conto[parola] = conto.get(parola, 0) +1<\/em><br \/>\n<em>~<\/em><br \/>\n<em>~grandeconto = None<\/em><br \/>\n<em>~grandeparola = None<\/em><br \/>\n<em>~for parola,conto in conto.items() :<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 if grandeconto is None or conto &gt; grandeconto :<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 grandeparola = parola<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 grandeconto = conto<\/em><br \/>\n<em>~<\/em><br \/>\n<em>~print(grandeparola, grandeconto)<\/em><\/p>\n<p>&nbsp;<\/p>\n<h2>#10 &#8211; Tuples<\/h2>\n<p>Tuples are the fast and efficient counterparts of directories and lists, that is because they are immutable and therefore can&#8217;t be modified.<br \/>\nThey use <strong>() <\/strong>and they can also be more than one variable (on the left) like so (as long as the list on the right have the same number): <em>(x, y) = (4, &#8216;tom&#8217;)<br \/>\n<\/em>Or in the case of directories they assume the pair (key and value for example).<\/p>\n<p>Tuples can be compared <strong>&lt;&gt; True False<\/strong> and it will do that by the first difference python finds (from left to right).<\/p>\n<p><em>~fhand = open(&#8216;file.txt&#8217;)<\/em><br \/>\n<em>~cnt = dict()<\/em><br \/>\n<em>~for riga in fhand:<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 prle = line.split()<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 for pr in prle:<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 cnt[pr] = cnt.get(pr, 0) +1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #extrapolate each word<\/em><br \/>\n<em>~<\/em><br \/>\n<em>~lst = list()<\/em><br \/>\n<em>~for k, v in cnt.items() :<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 ntup = (v, k)<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 lst.append(ntup)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #create a tuple inside a dict word, word count with all the word<\/em><br \/>\n<em>~<\/em><br \/>\n<em>~lst = sorted(lst, reverse=True)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #reverse : word count, word<\/em><br \/>\n<em>~<\/em><br \/>\n<em>~for v, k in lst[:5] :<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 print(k, v)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #print top 5 used word and count<\/em><\/p>\n<p>simplified way<em><br \/>\n<\/em><\/p>\n<p><em>~fhand = open(&#8216;file.txt&#8217;)<\/em><br \/>\n<em>~cnt = dict()<\/em><br \/>\n<em>~for riga in fhand:<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 prle = line.split()<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 for pr in prle:<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 cnt[pr] = cnt.get(pr, 0) +1<\/em><br \/>\n<em>~<\/em><br \/>\n<em><strong>~print( sorted( [ (v,k) for k,v in cnt.items() ] ) )\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #reduces the code lines<\/strong> <\/em><br \/>\n<em>~<\/em><br \/>\n<em>~for v, k in lst[:5] :<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 print(k, v)<\/em><\/p>\n<p>&nbsp;<\/p>\n<h2>#11 &#8211; Regular expression<\/h2>\n<pre>Note: they are not essential for using python but it's worth knowing them\r\nI will eventually come back to them but for now:<\/pre>\n<p>Regular expression (regex or regexp) provide a concise way of looking for strings text files.<br \/>\nThey use their own characters and written in a formal language<\/p>\n<p>They are not part of standard python; to use them type <strong>import re<\/strong><br \/>\nSome example : <strong>re.search()<\/strong> which is similar to find() and <strong>re.findall()<\/strong> combination between find() and slicing<\/p>\n<h3>~Quick Guide for regex<\/h3>\n<p>https:\/\/www.py4e.com\/lectures3\/Pythonlearn-11-Regex-Handout.txt<\/p>\n<p>^\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #matches the <em>beginning<\/em> of the line<br \/>\n$\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #matches the <em>end<\/em> of the line<br \/>\n.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #matches <em>any<\/em> character<br \/>\n\\s\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #matches <em>whitespaces<\/em><br \/>\n\\S\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #matches any <em>non whitespaces<br \/>\n<\/em>*\u00a0<em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #Repeats <\/em>a character zero or more times [longer string]<br \/>\n*? \u00a0<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #Repeats <\/em>a character zero or more times (non-greedy) [shorter string]<br \/>\n+ \u00a0\u00a0<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #Repeats <\/em>a character one or more times<br \/>\n+? \u00a0\u00a0<em> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #Repeats <\/em>a character one or more times (non-greedy)<br \/>\n[aeiou]\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #matches a single characther in the listed <em>set<\/em> <em><br \/>\n<\/em>[^XYZ]\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #matches a single characther <em>not in<\/em> the listed <em>set<\/em><br \/>\n[a-z0-9]\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #the set of character can include a <em>range<\/em><br \/>\n(\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #indicates where string <em>extraction<\/em> is to start<br \/>\n)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #indicates where string <em>extraction<\/em> is to end<\/p>\n<p>wild card characters<\/p>\n<p>.*\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #matches any characters many number of time<\/p>\n<p>&nbsp;<\/p>\n<h2>#12 &#8211; Networked Programs<\/h2>\n<pre>[6:22:40]<\/pre>\n<pre>*Dr. Book on the subject: Introduction To Networking<\/pre>\n<p>&nbsp;<\/p>\n<p>We operate on the Transport layer of the TCP\/IP protocol.<br \/>\nTCP connection, called also Sockets are the endpoint of the bidirectional inter-process communication flows across the internet.<br \/>\nTCP port number are basically extension of the process(eg: 10.10.10.10:80). There is a maximum of 65535 ports<br \/>\nThere are some common TCP ports such as:<\/p>\n<ul>\n<li>[21] FTP = File tranfer<\/li>\n<li>[22] SSH = Secure login<\/li>\n<li>[23] Telnet = Login<\/li>\n<li>[25] SMTP = Mail<\/li>\n<li>[53] DNS = Domain Name<\/li>\n<li>[80] HTTP<\/li>\n<li>[109\/110] POP = Mail Retrivial<\/li>\n<li>[143\/220\/993] IMAP = Mail Retrivial<\/li>\n<\/ul>\n<h3>~Sockets in Python<\/h3>\n<p>Python as built-in support for TCP sockets<\/p>\n<p><em>~import sockets\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #You need to import the library<\/em><br \/>\n<em>~mysock = socket.socket(socket.AF_INET, socket.sock_stream)\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0\u00a0 #Create a socket<\/em><br \/>\n<em>~mysock.connect( (&#8216;data.py4e.org&#8217;, 80) )\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #Extend the socket (it can have no answer)<\/em><\/p>\n<h3>~Application Protocol<\/h3>\n<p>Application Layer1 &#8211;&gt; Transport Layer1 &#8212;-&gt; Transport Layer2 &#8211;&gt; Application Layer2 [for transferring data]<\/p>\n<p>Using HTTP (Read read the RFC to know the rules of the protocol)<\/p>\n<h3>~Writing a Web Browser<\/h3>\n<p><em>~import socket<\/em><br \/>\n<em>~<\/em><br \/>\n<em>~mysock = socket.socket(socket.AF_INET, socket.sock_stream)<br \/>\n~mysock.connect( (&#8216;data.py4e.org&#8217;, 80) )<br \/>\n~cmd = &#8216;GET http:\/\/data.py4e.org\/romeo.txt HTTP\/1.0\\n\\n&#8217;.encode()\u00a0\u00a0\u00a0\u00a0 #specified data to go accross<br \/>\n~mysock.send(cmd)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #send data<br \/>\n~<br \/>\n~while True:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #start receiving data<br \/>\n~\u00a0\u00a0\u00a0 data = mysock.recv(512)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #at 512 characters per block<br \/>\n~\u00a0\u00a0\u00a0 if (len(data) &lt; 1):<br \/>\n~\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #if data less than 1 means it&#8217;s done so break<br \/>\n~\u00a0\u00a0\u00a0 print(data.decode())\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #print the decoded version of the data<br \/>\n~mysock.close()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #close the connection<br \/>\n<\/em><\/p>\n<h3>~Character and String<\/h3>\n<p>Till now we used the ASCII (American Standard Code for Information Interchange) , which uses Latin characters.<br \/>\nIt&#8217;s 1Byte or 8bits per character, and they are sorted by order.<br \/>\nTo check the number value associate with each character (https:\/\/en.wikipedia.org\/wiki\/ASCII):<br \/>\n<em>print(ord(&#8216;H&#8217;))\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #72\u00a0\u00a0\u00a0\u00a0 H was assigned with number 72<\/em><\/p>\n<p>Different from ASCII we have UNICODE which can represent every character on the world.<br \/>\nthe problem is that uses UTF-32 Fix length for each character (4bytes or 32bits).<br \/>\nThey were able to compressed down to UTF-16 fixed length (2bytes) although still too large.<br \/>\nAnd finally we have UTF-8 with uses dynamic length, from 8 to 32bits. Because of that it also compatible with ASCII and is the most used on the web.<\/p>\n<p>Because we are sending bytes, when we are working with Python we need to <strong>encode <\/strong>(in UTF-8) data to send, and when we receive data we need to\u00a0<strong>decode\u00a0<\/strong>before printing them.<br \/>\n<em>string <strong>&#8211;encode&#8211;&gt;<\/strong> bytes\u00a0\u00a0\u00a0\u00a0\u00a0 ||\u00a0\u00a0\u00a0\u00a0 bytes <strong>&#8211;decode&#8211;&gt;<\/strong> string<\/em><\/p>\n<h3>~urllib (easier HTTP)<\/h3>\n<p><em>~import urllib.request, urllib.parse, urllib.error\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #import libraries<\/em><br \/>\n<em>~<\/em><br \/>\n<em>~fhand = urllib.request.urlopen(&#8216;http:\/\/data.py4e.org\/romeo.txt&#8217;)\u00a0\u00a0\u00a0\u00a0 #open, encode and loop<\/em><br \/>\n<em>~for line in fhand:<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 print(line.decode().strip())\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #decode the received data<\/em><\/p>\n<p>you can also use it for .html to read web pages<\/p>\n<p><em>~import urllib.request, urllib.parse, urllib.error\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/em><br \/>\n<em>~<\/em><br \/>\n<em>~fhand = urllib.request.urlopen(&#8216;http:\/\/data.py4e.org\/romeo.<strong>html<\/strong>&#8216;)\u00a0\u00a0 <\/em><br \/>\n<em>~for line in fhand:<\/em><br \/>\n<em>~\u00a0\u00a0\u00a0 print(line.decode().strip())<br \/>\n<\/em><\/p>\n<h3>~web scraping or crawling<\/h3>\n<p>Note: be carefull as not every site allow you to use a program to watch their website<\/p>\n<p><strong>Beautiful Soup <\/strong>is a program that crawl the pages. It compensate all the error can occurr by retriving webpages.<br \/>\nNote: Beutiful soup has to be installed ( https:\/\/pypi.python.org\/pypi\/beautifulsoup4 )<\/p>\n<p><em>~import urllib.request, urllib.parse, urllib.error\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #import libraries<\/em><br \/>\n<em>~from bs4 import BeautifulSoup\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #import BS<br \/>\n~<br \/>\n~url = input(&#8216;Enter -&#8216;)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #url to crawl<br \/>\n~html = urllib.request.urlopen().read()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #open and read the html<br \/>\n~soup = BeautifulSoup(html, &#8216;html.parser&#8217;)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #retrive data trough BS<br \/>\n~<br \/>\n~tags = soup(&#8216;a&#8217;)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #retrive anchor tags<br \/>\n~for tag in tags:<br \/>\n~\u00a0\u00a0\u00a0 print(tag.get(&#8216;href&#8217;, None))<\/em><\/p>\n<p>&nbsp;<\/p>\n<h2>#13 &#8211; Using Web Servicies<\/h2>\n<pre>[7:24:10]<\/pre>\n<pre>To be continued............<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>PY4E &#8211; Python For Everybody Learning Python with Dr.Chuck free course First of all I would like to thank D. Chuck for sharing his knowledge by creating this free online [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":16,"footnotes":""},"categories":[33,19,37],"tags":[36,34,20],"class_list":["post-163","post","type-post","status-publish","format-standard","hentry","category-python","category-software","category-study-notes","tag-code","tag-python","tag-software"],"_links":{"self":[{"href":"https:\/\/paradoxa.me\/index.php\/wp-json\/wp\/v2\/posts\/163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/paradoxa.me\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/paradoxa.me\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/paradoxa.me\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/paradoxa.me\/index.php\/wp-json\/wp\/v2\/comments?post=163"}],"version-history":[{"count":43,"href":"https:\/\/paradoxa.me\/index.php\/wp-json\/wp\/v2\/posts\/163\/revisions"}],"predecessor-version":[{"id":223,"href":"https:\/\/paradoxa.me\/index.php\/wp-json\/wp\/v2\/posts\/163\/revisions\/223"}],"wp:attachment":[{"href":"https:\/\/paradoxa.me\/index.php\/wp-json\/wp\/v2\/media?parent=163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/paradoxa.me\/index.php\/wp-json\/wp\/v2\/categories?post=163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/paradoxa.me\/index.php\/wp-json\/wp\/v2\/tags?post=163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}