{"id":1816,"date":"2019-06-03T12:02:46","date_gmt":"2019-06-03T12:02:46","guid":{"rendered":"http:\/\/wiki.thomasandsofia.com\/?p=1816"},"modified":"2019-06-05T20:07:40","modified_gmt":"2019-06-05T20:07:40","slug":"section-3-python-object-and-data-structure-basics","status":"publish","type":"post","link":"https:\/\/wiki.thomasandsofia.com\/?p=1816","title":{"rendered":"Section 3: Python Object and Data Structure Basics"},"content":{"rendered":"<h1>Section 3 Menu<\/h1>\n<p><a href=\"http:\/\/wiki.thomasandsofia.com\/?p=1805\">&lt; Section 2<\/a><br \/>\n<a href=\"http:\/\/wiki.thomasandsofia.com\/?p=1848\">Section 4 &gt;<\/a><\/p>\n<ol start=\"10\">\n<li>Introduction to Python Data Types<\/li>\n<li>Python Numbers<\/li>\n<li>Numbers FAQ<\/li>\n<li>Variable Assignments<\/li>\n<li>Introduction to Strings<\/li>\n<li>Indexing and slicing with Strings<\/li>\n<li>String Properties and Methods<\/li>\n<li>Strings FAQ<\/li>\n<li>Print Formatting with Strings<\/li>\n<li>Print Formatting FAQs<\/li>\n<li>Lists in Python<\/li>\n<li>Lists FAQ<\/li>\n<li>Dictionaries in Python<\/li>\n<li>Dictionaries FAQ<\/li>\n<li>Tuples with Python<\/li>\n<li>Sets in Python<\/li>\n<li>Booleans in Python<\/li>\n<li>I\/O with Basic Files in Python<\/li>\n<li>Resources for More Basic Practice<\/li>\n<li>Python Objects and Data Structures Assessment Test Overviews<\/li>\n<li>Python Objects and Data Structures Assessment Test Solutions<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h1>My Comments<\/h1>\n<h2>Coding Conventions<\/h2>\n<p>Ref &#8216;PEP8&#8217;: <a href=\"https:\/\/www.python.org\/dev\/peps\/pep-0008\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.python.org\/dev\/peps\/pep-0008\/<\/a><\/p>\n<h1>Introduction to Python Data Types<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9388518#content\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9388518#content<\/a><\/p>\n<p>Overview of the values<\/p>\n<p><a href=\"http:\/\/wiki.thomasandsofia.com\/wp-content\/uploads\/2019\/06\/datatypes.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1822\" src=\"http:\/\/wiki.thomasandsofia.com\/wp-content\/uploads\/2019\/06\/datatypes.png\" alt=\"\" width=\"1123\" height=\"511\" srcset=\"https:\/\/wiki.thomasandsofia.com\/wp-content\/uploads\/2019\/06\/datatypes.png 1123w, https:\/\/wiki.thomasandsofia.com\/wp-content\/uploads\/2019\/06\/datatypes-300x137.png 300w, https:\/\/wiki.thomasandsofia.com\/wp-content\/uploads\/2019\/06\/datatypes-768x349.png 768w, https:\/\/wiki.thomasandsofia.com\/wp-content\/uploads\/2019\/06\/datatypes-1024x466.png 1024w, https:\/\/wiki.thomasandsofia.com\/wp-content\/uploads\/2019\/06\/datatypes-150x68.png 150w\" sizes=\"auto, (max-width: 1123px) 100vw, 1123px\" \/><\/a><br \/>\n* Boolean type &#8216;None&#8217; = null? It is the value returned if there is no return value&#8230;.?<\/p>\n<h1>Python Numbers<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9388520#content\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9388520#content<\/a><\/p>\n<ul>\n<li>% = Mod (Moulo) aka remainder\n<ul>\n<li>7 % 4 = 3<\/li>\n<li>50 % 5 = 0\n<ul>\n<li>Good way to check if a value cleanly divides<\/li>\n<li>Also good to check if a value is even or odd\n<ul>\n<li>21 % 2 = 1<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>** = Exponent\n<ul>\n<li>2**3=8<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>Coding Exercise 1<\/h2>\n<p>Write an expression that = 100<\/p>\n<p>10*10<\/p>\n<h1>Numbers FAQ<\/h1>\n<p><strong>1. What&#8217;s the difference between floating point and an integer?<\/strong><\/p>\n<p>An integer has no decimals in it, a floating point number can display digits past the decimal point.<\/p>\n<p><strong>2. Why doesn&#8217;t 0.1+0.2-0.3 equal 0.0 ?<\/strong><\/p>\n<p>This has to do with floating point accuracy and computer&#8217;s abilities to represent numbers in memory. For a full breakdown, check out: <a href=\"https:\/\/docs.python.org\/2\/tutorial\/floatingpoint.html\" target=\"_blank\" rel=\"noopener\">https:\/\/docs.python.org\/2\/tutorial\/floatingpoint.html<\/a><\/p>\n<h1>Variable Assignments<\/h1>\n<h2>Naming Rules<\/h2>\n<ul>\n<li>Cannot start with a number<\/li>\n<li>Cannot contain spaces<\/li>\n<li>Cannot use any of the following characters:\n<ul>\n<li>: &#8216; &#8221; , &lt; &gt; \/ ? | \\ ( ) ! @ # $ % ^ &amp; * ~ &#8211; +<\/li>\n<li>Best practice all variables are lower case\n<ul>\n<li>Exception is Global Variables which should be all CAPS<\/li>\n<\/ul>\n<\/li>\n<li>Avoid using variables that have special meanings, like <strong>list<\/strong> and <strong>str<\/strong><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>Variable Typing<\/h2>\n<ul>\n<li>Python uses <strong>Dynamic Typing<\/strong>\n<ul>\n<li>You can reassign variable to different types:\n<ul>\n<li>my_var = 5<\/li>\n<li>my_var = &#8220;hello&#8221;<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>Some other languages do not allow this\n<ul>\n<li>These are known as <strong>Statically Typed<\/strong><\/li>\n<li>Example for C++\n<ul>\n<li>int my_var = 5;<\/li>\n<li>my_var = &#8220;hello&#8221;;\n<ul>\n<li>This results in an error!<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>Pros and Cons to Dynamic Typing<\/h3>\n<ul>\n<li>Pros\n<ul>\n<li>Very easy to work with<\/li>\n<li>Faster development time<\/li>\n<\/ul>\n<\/li>\n<li>Cons\n<ul>\n<li>May result in bugs for unexpected data types<\/li>\n<li>Need to be aware of <strong>type()<\/strong><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>type()<\/h4>\n<ul>\n<li>a = 5<\/li>\n<li>type(a)\n<ul>\n<li>int<\/li>\n<\/ul>\n<\/li>\n<li>a = 5.1<\/li>\n<li>type(a)\n<ul>\n<li>float<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h1>Introduction to Strings<\/h1>\n<h2>Indexing<\/h2>\n<ul>\n<li>Character: h e l l o<\/li>\n<li>Index: 0 1 2 3 4<\/li>\n<li>Reverse Index: 0 -4 -3 -2 -1<\/li>\n<li>my_str = &#8220;hello&#8221;\n<ul>\n<li>my_str[1]\n<ul>\n<li>e<\/li>\n<\/ul>\n<\/li>\n<li>my_str[-1]\n<ul>\n<li>o<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>Slicing<\/h2>\n<ul>\n<li>Used to grab a sub-section of a string<\/li>\n<li>[start:stop:step]\n<ul>\n<li>start = start index (Default = <strong>0<\/strong>)<\/li>\n<li>stop = up to, but not including this index (Default = len())<\/li>\n<li>step = &#8216;jump&#8217; size (Default = <strong>1<\/strong>)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h1>Indexing and slicing with Strings<\/h1>\n<ul>\n<li>&#8216;abcdefghijk'[2]\n<ul>\n<li>c<\/li>\n<\/ul>\n<\/li>\n<li>my_str = &#8216;abcdefghijk&#8217;<\/li>\n<li>len(my_str)\n<ul>\n<li>11<\/li>\n<\/ul>\n<\/li>\n<li>my_str[2]\n<ul>\n<li>c<\/li>\n<\/ul>\n<\/li>\n<li>my_str[-2]\n<ul>\n<li>j<\/li>\n<\/ul>\n<\/li>\n<li>my_str[2:] (no stop = all remaining characters)\n<ul>\n<li>cdefghijk<\/li>\n<\/ul>\n<\/li>\n<li>my_str[:3] (no start = start with 0)\n<ul>\n<li>abc<\/li>\n<\/ul>\n<\/li>\n<li>my_str[2:4]\n<ul>\n<li>cd<\/li>\n<\/ul>\n<\/li>\n<li>my_str[4:-5]\n<ul>\n<li>ef<\/li>\n<\/ul>\n<\/li>\n<li>my_str[::] (start at the beginning and go to the end)\n<ul>\n<li>abcdefghijk<\/li>\n<\/ul>\n<\/li>\n<li>my_str[::2]\n<ul>\n<li>acegik<\/li>\n<\/ul>\n<\/li>\n<li>my_str[::3]\n<ul>\n<li>adgj<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>Reversing a string<\/h3>\n<p>Note: When using a negative step size, the start and stop positions are also reversed<\/p>\n<ul>\n<li>my_str[::-1] (Reverse a string)\n<ul>\n<li>kjihgfedcba<\/li>\n<\/ul>\n<\/li>\n<li>my_str[1::-1] (start = index 1, stop = through start of string, -1 = backwards)\n<ul>\n<li>ba<\/li>\n<\/ul>\n<\/li>\n<li>my_str[4:0:-1]\n<ul>\n<li>edcb<\/li>\n<\/ul>\n<\/li>\n<li>my_str[-2,7,-1]\n<ul>\n<li>ji<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h1>String Properties and Methods<\/h1>\n<h2>Immutability<\/h2>\n<p>You cannot directly assign new values to string indexes<\/p>\n<pre>name = 'Sam'\r\nname[0] = 'P'<\/pre>\n<p>TypeError: &#8216;str&#8217; object does not support item assignment<\/p>\n<h2>Concatenation<\/h2>\n<h3>Addition<\/h3>\n<p>Use a + symbol to concatenate (add together)<\/p>\n<pre>name = 'Sam'\r\npam = 'P' + name[1:]\r\nprint(pam)<\/pre>\n<p>Pam<\/p>\n<h3>Mutiplication<\/h3>\n<pre>letter = 'z'\r\nletter = letter * 5\r\nprint(letter)<\/pre>\n<p>zzzzz<\/p>\n<h2>Caution using Dynamic Types<\/h2>\n<pre>2 + 3<\/pre>\n<p>5<\/p>\n<pre>'2' + '3'<\/pre>\n<p>23<\/p>\n<h2>String Objects and Methods<\/h2>\n<pre>x = 'Hello World'\r\nprint(x.upper())\r\nprint(x)<\/pre>\n<p>HELLO WORLD<br \/>\nHello World<\/p>\n<pre>x = 'Hello World'\r\nx = x.upper()\r\nprint(x)<\/pre>\n<p>HELLO WORLD<\/p>\n<pre>x = 'Hello World'\r\nprint(x.lower())<\/pre>\n<p>hello world<\/p>\n<h3>Split<\/h3>\n<pre>x = 'Hello World'\r\nx.split()<\/pre>\n<p>[&#8216;Hello&#8217;, &#8216;World&#8217;]<\/p>\n<pre>x = 'Hello World'\r\nx.split('l')<\/pre>\n<p>[&#8216;He&#8217;, &#8221;, &#8216;o Wor&#8217;, &#8216;d&#8217;]<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h1>Strings FAQ<\/h1>\n<h1>Print Formatting with Strings<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9388532#content\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9388532#content<\/a><br \/>\nGreat resource: <a href=\"https:\/\/pyformat.info\/\" target=\"_blank\" rel=\"noopener\">https:\/\/pyformat.info\/<\/a><\/p>\n<h2>.format()<\/h2>\n<h3>Strings<\/h3>\n<p>&#8220;String here {} then also {}&#8221;.format(var1, var2)<\/p>\n<pre>print('This is an {} string'.format('INSERTED'))<\/pre>\n<p>This is an INSERTED string<\/p>\n<h3>Indexing<\/h3>\n<pre>print('The {2} {1} {0}'.format('fox', 'brown', 'quick'))<\/pre>\n<p>The quick brown fox<\/p>\n<pre>print('The {0} {0} {0}'.format('fox', 'brown', 'quick'))<\/pre>\n<p>The fox fox fox<\/p>\n<h3>Variables<\/h3>\n<pre>print('The {q} {b} {f}'.format(f='fox', b='brown', q='quick'))<\/pre>\n<p>The quick brown fox<\/p>\n<pre>print('The {f} {f} {f}'.format(f='fox', b='brown', q='quick'))<\/pre>\n<p>The fox fox fox<\/p>\n<h3>Floating Point Numbers<\/h3>\n<p>value:.f<\/p>\n<pre>result = 100\/777\r\nprint(result)\r\nprint(\"The result was {r:1.3f}\".format(r=result)\r\nprint(\"The result was {r:10.3f}\".format(r=result)<\/pre>\n<p>0.1287001287001287<br \/>\nThe result was 0.129<br \/>\nThe result was \u00a0\u00a0\u00a0\u00a0\u00a00.129<\/p>\n<h2>Print(f&#8221; &#8220;)<\/h2>\n<pre>husband = 'Thomas'\r\nwife = 'Sofia'\r\nprint(f\"{husband} is married to {wife}.\")<\/pre>\n<p>Thomas is married to Sofia.<\/p>\n<h1>Print Formatting FAQs<\/h1>\n<p>https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3810438#content<\/p>\n<p><strong>Print Formatting FAQS<\/strong><\/p>\n<p><strong class=\"redactor-inline-converted\">1.) I imported print from the __future__ module, now print isn&#8217;t working. What happened?<\/strong><\/p>\n<p>This is because once you import from the __future__ module in Python 2.7, a print statement will no longer work, and print must then use a print() function. Meaning that you must use<\/p>\n<p><strong class=\"redactor-inline-converted\">print(&#8216;Whatever you were going to print&#8217;)<\/strong><\/p>\n<p>or if you are using some formatting:<\/p>\n<p><strong class=\"redactor-inline-converted\">print(&#8216;This is a string with an {p}&#8217;.format(p=&#8217;insert&#8217;<\/strong><strong class=\"redactor-inline-converted\">))<\/strong><\/p>\n<p>The __future__ module allows you to use Python3 functionality in a Python2 environment, but some functionality is overwritten (such as the print statement, or classic division when you import division).<\/p>\n<p>Since we are using Jupyter Notebooks, once you so the import, all cells will require the use if the print() function. You will have to restart Python or start a new notebook to regain the old functionality back.<\/p>\n<h1>Lists in Python<\/h1>\n<p>Numerically indexed array containing a variety of values and objects.<br \/>\n* Lists are MUTABLE! You can change an index at will. (Unlike strings, which are immutable.)<\/p>\n<pre>husband = 'Thomas'\r\nmy_list=[husband, 53, 'String']\r\nprint(len(my_list))\r\nprint(my_list[0])<\/pre>\n<p>3<br \/>\nThomas<\/p>\n<h2>Slicing and Indexing<\/h2>\n<p>Works EXACTLY the same as strings<\/p>\n<pre>husband = 'Thomas'\r\nmy_list=[husband, 53, 'String']\r\nprint(my_list[:2])<\/pre>\n<p>[&#8216;Thomas&#8217;, 53]<\/p>\n<h2>Modifying Lists<\/h2>\n<h3>Concatenate<\/h3>\n<pre>list1 = ['one', 2, 'three']\r\nlist2 = ['4', 'Five']\r\nlist3 = list1 +  list2\r\nprint(list3)<\/pre>\n<p>[&#8216;one&#8217;, 2, &#8216;three&#8217;, &#8216;4&#8217;, &#8216;Five&#8217;]<\/p>\n<h3>.append()<\/h3>\n<pre>list3.append(6)\r\nprint(list3)<\/pre>\n<p>[&#8216;one&#8217;, 2, &#8216;three&#8217;, &#8216;4&#8217;, &#8216;Five&#8217;, 6]<\/p>\n<h3>.pop()<\/h3>\n<p>Removes and returns an index from list. (Default = -1)<\/p>\n<pre>my_list = ['one', 2, 'three', '4', 'Five', 6]\r\nprint(my_list.pop())\r\nprint(my_list)<\/pre>\n<p>6<br \/>\n[&#8216;one&#8217;, 2, &#8216;three&#8217;, &#8216;4&#8217;, &#8216;Five&#8217;]<\/p>\n<pre>my_list = ['one', 2, 'three', '4', 'Five', 6]\r\nprint(my_list.pop(1))\r\nprint(my_list)<\/pre>\n<p>2<br \/>\n[&#8216;one&#8217;, &#8216;three&#8217;, &#8216;4&#8217;, &#8216;Five&#8217;, 6]<\/p>\n<pre>my_list = ['one', 2, 'three', '4', 'Five', 6]\r\nprint(my_list.pop(-2))\r\nprint(my_list)<\/pre>\n<p>five<br \/>\n[&#8216;one&#8217;, &#8216;three&#8217;, &#8216;4&#8217;, 6]<\/p>\n<h2>Sorting<\/h2>\n<p>* Sorting is &#8216;in place&#8217;. That is:<br \/>\nIt alters the actual array.<br \/>\nIt does NOT return a value (None)<\/p>\n<pre>alpha = ['z', 'e', 'r', 't', 'm']\r\nalpha.sort()\r\nprint(alpha)<\/pre>\n<p>[&#8216;e&#8217;, &#8216;m&#8217;, &#8216;r&#8217;, &#8216;t&#8217;, &#8216;z&#8217;]<\/p>\n<pre>alpha = ['z', 'e', 'R', 't', 'm']\r\nalpha.sort()\r\nprint(alpha)<\/pre>\n<p>[&#8216;R&#8217;, &#8216;e&#8217;, &#8216;m&#8217;, &#8216;t&#8217;, &#8216;z&#8217;]<\/p>\n<pre>alpha = ['z', 'e', 'R', 't', 'm']\r\nbeta = alpha.sort()\r\nprint(beta)\r\ntype(beta)<\/pre>\n<p>NoneType<\/p>\n<pre>numlist = [3, 2, 0, 4, 1]\r\nnumlist.sort()\r\nprint(numlist)<\/pre>\n<p>[0, 1, 2, 3, 4]<\/p>\n<h3>.reverse()<\/h3>\n<pre>numlist = [3, 2, 0, 4, 1]\r\nnumlist.reverse()\r\nprint(numlist)<\/pre>\n<p>[1, 4, 0, 2, 3]<\/p>\n<h1>Lists FAQ<\/h1>\n<p><strong>1. How do I index a nested list? For example if I want to grab 2 from [1,1,[1,2]]?<\/strong><\/p>\n<p>You would just add another set of brackets for indexing the nested list, for example: my_list[2][1] . We&#8217;ll discover later on more nested objects and you will be quizzed on them later!<\/p>\n<p>&nbsp;<\/p>\n<h1>Dictionaries in Python<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9388536#content\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9388536#content<\/a><\/p>\n<p>Dictionaries are associative arrays, i.e. key:value pairs.<\/p>\n<pre>my_dict = {'key1':'value1', 'key1':2, 'key3':3.333, 'key4':[0, 1, 2]}<\/pre>\n<p>Dictionaries<\/p>\n<ul>\n<li>Keys should always, but do not have to, be strings.<\/li>\n<li>Grouped using curly brackets<\/li>\n<\/ul>\n<p>Dictionaries vs Lists<\/p>\n<ul>\n<li>Dictionaries\n<ul>\n<li>Objects retrieved by key name<\/li>\n<li>These are unordered and cannot be sorted<\/li>\n<\/ul>\n<\/li>\n<li>Lists\n<ul>\n<li>Objects retrieved by location<\/li>\n<li>Ordered sequence can be indexed or sliced.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>Methods<\/h2>\n<h3>.keys()<\/h3>\n<pre>d = {'key1':1, 'key2':2, 'key3':'Three'}\r\nprint(d.keys())<\/pre>\n<p>dict_keys([&#8216;key1&#8217;, &#8216;key2&#8217;, &#8216;key3&#8217;])<\/p>\n<h3>.values()<\/h3>\n<pre>d = {'key1':1, 'key2':2, 'key3':'Three'}\r\nprint(d.values())<\/pre>\n<p>dict_values([1, 2, &#8216;Three&#8217;])<\/p>\n<h3>.items()<\/h3>\n<pre>d = {'key1':1, 'key2':2, 'key3':'Three'}\r\nprint(d.items())<\/pre>\n<p>dict_items([(&#8216;key1&#8217;, 1), (&#8216;key2&#8217;, 2), (&#8216;key3&#8217;, &#8216;Three&#8217;)])<\/p>\n<ul>\n<li>Notice the return sets are in parenthesis.\u00a0 These are Tuples!<\/li>\n<\/ul>\n<h2>Working with Objects<\/h2>\n<pre>d={'key':['b','c','a']}\r\nprint(d['key'][2].upper())<\/pre>\n<p>A<\/p>\n<pre>d={'key':['b','c','a']}\r\nd['key'].sort()\r\nprint(d['key'])<\/pre>\n<p>[&#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;]<\/p>\n<p>&nbsp;<\/p>\n<h1>Dictionaries FAQ<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3729816#content\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3729816#content<\/a><\/p>\n<p><strong>1. Do dictionaries keep an order? How do I print the values of the dictionary in order?<\/strong><\/p>\n<p>Dictionaries are mappings and do not retain order! If you do want the capabilities of a dictionary but you would like ordering as well, check out the <strong>ordereddict<\/strong> object lecture later on in the course!<\/p>\n<h1>Tuples with Python<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9388540#content\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9388540#content<\/a><\/p>\n<ul>\n<li>Tuples use parenthesis: (1, 2, 3)<\/li>\n<li>Similar to lists, but are immutable. Once an element is inside a tuple, it cannot be reassigned.<\/li>\n<li>You can slice and index the same as a list but you cannot reorder!<\/li>\n<\/ul>\n<p>Tuples are useful in advanced programming for passing objects when you must ensure the data integrity.<\/p>\n<h2>Immutability Example<\/h2>\n<pre>t=('S', 'o', 'f', 'i','a')\r\nt[2] = 'ph'<\/pre>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\nTypeError Traceback (most recent call last)<br \/>\nin<br \/>\n1 t=(&#8216;S&#8217;, &#8216;o&#8217;, &#8216;f&#8217;, &#8216;i&#8217;,&#8217;a&#8217;)<br \/>\n&#8212;-&gt; 2 t[2] = &#8216;ph&#8217;<\/p>\n<p>TypeError: &#8216;tuple&#8217; object does not support item assignment<\/p>\n<h2>Indexing and Slicing example<\/h2>\n<pre>t=('S', 'o', 'f', 'i','a')\r\nt[1:4]<\/pre>\n<p>(&#8216;o&#8217;, &#8216;f&#8217;, &#8216;i&#8217;)<\/p>\n<h2>Methods<\/h2>\n<h3>.count()<\/h3>\n<p>Counts the number of occurrences of a value in the tuple.<\/p>\n<pre>t = ('a', 'b', 'a', 'c')\r\nt.count('a')<\/pre>\n<p>2<\/p>\n<h3>.index()<\/h3>\n<p>Returns the first index occurrance of a value<\/p>\n<pre>t = ('a', 'b', 'a', 'c')\r\nt.index('a')<\/pre>\n<p>0<\/p>\n<p>&nbsp;<\/p>\n<h1>Sets in Python<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9388544#content\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9388544#content<\/a><\/p>\n<ul>\n<li>Sets are unordered collections of unique elements\n<ul>\n<li>There are no indexes<\/li>\n<\/ul>\n<\/li>\n<li>There can only be one representative of the same object\n<ul>\n<li>Each value in the set must be unique.\u00a0 There are no duplucates<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>Set Example<\/h2>\n<pre>myset = set()\r\nmyset<\/pre>\n<p>set()<\/p>\n<pre>myset.add(1)\r\nmyset<\/pre>\n<p>{1}<\/p>\n<pre>myset.add(2)\r\nmyset<\/pre>\n<p>{1, 2}<\/p>\n<pre>myset.add(2)\r\nmyset<\/pre>\n<p>{1, 2}<\/p>\n<h2>Usage<\/h2>\n<pre>mylist = [1, 2, 3, 1, 2, 3, 1, 2, 3, 4]\r\nset(mylist)<\/pre>\n<p>{1, 2, 3, 4}<\/p>\n<pre>set('Mississippi')<\/pre>\n<p>{&#8216;M&#8217;, &#8216;i&#8217;, &#8216;p&#8217;, &#8216;s&#8217;}<br \/>\n* Notice the values are NOT in the order they are in the array!<\/p>\n<h1>Booleans in Python<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9388546#content\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9388546#content<\/a><\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>True or False<\/li>\n<li>Also allows &#8216;None&#8217;\n<ul>\n<li>Used to defined &#8216;undefined&#8217; variables<\/li>\n<\/ul>\n<\/li>\n<li>First letter MUST be capitalized, otherwise it is a variable.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>True and False examples<\/h2>\n<pre>1 &gt; 2<\/pre>\n<p>False<\/p>\n<pre>1 == 1<\/pre>\n<p>True<\/p>\n<h2>None examples<\/h2>\n<pre>x<\/pre>\n<p>NameError: name &#8216;x&#8217; is not defined<\/p>\n<pre>x = None<\/pre>\n<p>(Nothing displayed)<br \/>\n18% Complete<\/p>\n<h1>I\/O with Basic Files in Python<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9388552?start=30#content\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9388552?start=30#content<\/a><\/p>\n<h3>Setup<\/h3>\n<h4>Note:<\/h4>\n<p>Get the current working directory of your Jupyter Notebook using the &#8216;pwd&#8217; command<\/p>\n<pre>pwd<\/pre>\n<p>&#8216;C:\\\\Users\\\\user1\\\\path\\\\to\\\\jupyter\\\\notebook&#8217;<\/p>\n<p>Create a text file with a few lines in it.\u00a0 In Jupyter Notebooks, use the following:<\/p>\n<pre>%%writefile myfile.txt\r\nHello this is a text file.\r\nThis is the second line\r\nand this is on the third line<\/pre>\n<p>Writing myfile.txt<\/p>\n<h3>Open the file<\/h3>\n<pre>myfile = open('myfile.txt')<\/pre>\n<h2>Methods<\/h2>\n<h3>.read()<\/h3>\n<pre>myfile.read()<\/pre>\n<p>&#8216;Hello this is a text file.\\nThis is the second line\\nand this is on the third line&#8217;<br \/>\nThis method sets the read location to the end of the file. Trying to read again results in an empty string<\/p>\n<pre>myfile.read()<\/pre>\n<p>&#8221;<br \/>\nSee .seek() below.<\/p>\n<h3>.seek()<\/h3>\n<p>Sets the read position and returns that value<\/p>\n<pre>myfile.seek(0)<\/pre>\n<p>0<\/p>\n<pre>myfile.seek(0)\r\nmyfile.read()<\/pre>\n<p>&#8216;Hello this is a text file.\\nThis is the second line\\nand this is on the third line&#8217;<\/p>\n<pre>myfile.seek(4)\r\nmyfile.read()<\/pre>\n<p>&#8216;o this is a text file.\\nThis is the second line\\nand this is on the third line&#8217;<\/p>\n<h3>.readlines()<\/h3>\n<pre>myfile.seek(0)\r\nmyfile.readlines()<\/pre>\n<p>[&#8216;Hello this is a text file.\\n&#8217;,<br \/>\n&#8216;This is the second line\\n&#8217;,<br \/>\n&#8216;and this is on the third line&#8217;]<\/p>\n<p>&lt;h2&gt;File Locations&lt;\/h2&gt;<\/p>\n<p>If you want to open files at another location on your computer, simply pass in the entire file path.<\/p>\n<h3>Windows<\/h3>\n<p>You will need to use a double backslash &#8216;\\\\&#8217; to prevent Pyton from reading it as an escape character<\/p>\n<pre>myfile = open(\"C:\\\\Users\\\\user1\\\\path\\\\to\\\\folder\\\\myfile.txt\")<\/pre>\n<h3>Mac and Linux<\/h3>\n<p>Use slashes as you normally do.<\/p>\n<pre>myfile = open(\"\/home\/user1\/path\/to\/folder\/myfile.txt\")<\/pre>\n<h2>Closing Files<\/h2>\n<p>It is IMPORTANT that you remember to close the file! Failure to do so will leave the file open and others will not be able to alter it.<\/p>\n<h3>.close()<\/h3>\n<pre>myfile.close()<\/pre>\n<h3>with \/ as<\/h3>\n<pre>with open('myfile.txt' as my_new_file:\r\n    contents = my_newe_file.read()\r\ncontents.read()<\/pre>\n<p>&#8216;Hello this is a text file.\\nThis is the second line\\nand this is on the third line&#8217;<br \/>\nThis method closes the file as soon as the &#8216;ny_new_file&#8217; object is created<\/p>\n<h2>Writing to a file<\/h2>\n<pre>with open('myfile.txt, mode='w') as myfile:\r\n    contents = myfile.read()<\/pre>\n<p>UnsupporedOperation: not readable<br \/>\n* the file was ONLY opened for WRITE mode!<\/p>\n<h2>Understanding mode=<\/h2>\n<ul>\n<li>mode=&#8217;r&#8217; &#8211; read only<\/li>\n<li>mode=&#8217;w&#8217; &#8211; write only (will overwrite files or create new!)<\/li>\n<li>mode=&#8217;a&#8217; &#8211; append (will add on to files)<\/li>\n<li>mode=&#8217;r+&#8217; &#8211; reading and writing<\/li>\n<li>mode=&#8217;w+&#8217; &#8211; reading and writing (overwrites existing files or creates a new file)<\/li>\n<\/ul>\n<h3>Create a new file<\/h3>\n<pre>%%writefile my_new_file.txt\r\nONE ON FIRST\r\nTWO ON SECOND\r\nTHREE ON THIRD<\/pre>\n<p>Writing my_new_file.txt<\/p>\n<h3>mode=&#8217;r&#8217;<\/h3>\n<pre>with open('my_new_file.txt', mode='r') as f:\r\n    print(f.read())<\/pre>\n<p>ONE ON FIRST<br \/>\nTWO ON SECOND<br \/>\nTHREE ON THIRD<\/p>\n<h3>mode=&#8217;a&#8217;<\/h3>\n<pre>with open('my_new_file.txt', mode='a') as f:\r\n    f.write('\\nFOUR ON FORTH'))\r\nwith open('my_new_file.txt', mode='r') as f:\r\n    print(f.read())<\/pre>\n<p>ONE ON FIRST<br \/>\nTWO ON SECOND<br \/>\nTHREE ON THIRD<br \/>\nFOUR ON FORTH<\/p>\n<h1>Resources for More Basic Practice<\/h1>\n<p>Before you begin your assessment, I wanted to point out some helpful links for practice (don&#8217;t worry about being able to do these exercises, I just want you to be aware of the links so you can visit them later, since we still haven&#8217;t discussed functions, you won&#8217;t be able to utilize a lot of these resources yet!):<\/p>\n<p><strong>Basic Practice:<\/strong><\/p>\n<p><a href=\"http:\/\/codingbat.com\/python\" target=\"_blank\" rel=\"noopener\">http:\/\/codingbat.com\/python<\/a><\/p>\n<p><strong>More Mathematical (and Harder) Practice:<\/strong><\/p>\n<p><a href=\"https:\/\/projecteuler.net\/archives\" target=\"_blank\" rel=\"noopener\">https:\/\/projecteuler.net\/archives<\/a><\/p>\n<p><strong>List of Practice Problems:<\/strong><\/p>\n<p><a href=\"http:\/\/www.codeabbey.com\/index\/task_list\" target=\"_blank\" rel=\"noopener\">http:\/\/www.codeabbey.com\/index\/task_list<\/a><\/p>\n<p><strong>A SubReddit Devoted to Daily Practice Problems:<\/strong><\/p>\n<p><a href=\"https:\/\/www.reddit.com\/r\/dailyprogrammer\" target=\"_blank\" rel=\"noopener\">https:\/\/www.reddit.com\/r\/dailyprogrammer<\/a><\/p>\n<p><strong>A very tricky website with very few hints and touch problems (Not for beginners but still interesting)<\/strong><\/p>\n<p><a href=\"http:\/\/www.pythonchallenge.com\/\" target=\"_blank\" rel=\"noopener\">http:\/\/www.pythonchallenge.com\/<\/a><\/p>\n<p>Course 20% Complete<\/p>\n<h1>Python Objects and Data Structures Assessment Test Overviews<\/h1>\n<p>&nbsp;<\/p>\n<h1>Python Objects and Data Structures Assessment Test Solutions<\/h1>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Section 3 Menu &lt; Section 2 Section 4 &gt; Introduction to Python Data Types Python Numbers Numbers FAQ Variable Assignments Introduction to Strings Indexing and slicing with Strings String Properties and Methods Strings FAQ Print Formatting with Strings Print Formatting FAQs Lists in Python Lists FAQ Dictionaries in Python Dictionaries FAQ Tuples with Python Sets ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/wiki.thomasandsofia.com\/?p=1816\" title=\"read more...\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[],"class_list":["post-1816","post","type-post","status-publish","format-standard","hentry","category-python-bootcamp-0-to-hero"],"_links":{"self":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1816","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1816"}],"version-history":[{"count":28,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1816\/revisions"}],"predecessor-version":[{"id":1855,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1816\/revisions\/1855"}],"wp:attachment":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}