{"id":1938,"date":"2019-06-18T02:23:25","date_gmt":"2019-06-18T02:23:25","guid":{"rendered":"http:\/\/wiki.thomasandsofia.com\/?p=1938"},"modified":"2019-06-18T17:31:56","modified_gmt":"2019-06-18T17:31:56","slug":"section-16-advanced-python-objects-and-data-structures","status":"publish","type":"post","link":"https:\/\/wiki.thomasandsofia.com\/?p=1938","title":{"rendered":"Section 16: Advanced Python Objects and Data Structures"},"content":{"rendered":"<p><a href=\"http:\/\/wiki.thomasandsofia.com\/?p=1929\">&lt; Section 15<\/a> | Section 17 &gt;<\/p>\n<h1>Advanced Numbers<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3650896?start=0#questions\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3650896?start=0#questions<\/a><\/p>\n<h3>Hexadecimal hex()<\/h3>\n<p>Values are returned as strings<\/p>\n<pre>hex(246)<\/pre>\n<p>0xf6<\/p>\n<pre>hex(256)<\/pre>\n<p>0x200<\/p>\n<h3>Binary bin()<\/h3>\n<p>Values are returned as strings<\/p>\n<pre>bin(1234)<\/pre>\n<p>0b10011010010<\/p>\n<h3>pow()<\/h3>\n<p>Looks like PHP to me&#8230;<\/p>\n<pre>2**3 == pow(2,3)<\/pre>\n<h3>abs()<\/h3>\n<pre>abs(-4)<\/pre>\n<p>4<\/p>\n<h3>round()<\/h3>\n<p>Rounds to 0 places by default<\/p>\n<pre>round(4.75,1)<\/pre>\n<p>4.8<\/p>\n<pre>round(4.75)<\/pre>\n<p>5<\/p>\n<h1>Advanced Strings<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3512768#questions\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3512768#questions<\/a><\/p>\n<h2>Case, Count and Find<\/h2>\n<h3>.capitalize()<\/h3>\n<p>Capitalizes the first character in the string.<\/p>\n<pre>s = \"hello world. how are you today?\"\r\ns.capitalize()<\/pre>\n<p>Hello world. how are you today?<\/p>\n<h3>.upper()<\/h3>\n<p>Converts all characters to UPPER case<\/p>\n<pre>s.upper()<\/pre>\n<p>HELLO WORLD. HOW ARE YOU TODAY?<\/p>\n<h3>.lower()<\/h3>\n<p>Converts all characters to lower case<\/p>\n<pre>s.lower()<\/pre>\n<p>hello world. how are you today?<\/p>\n<h3>.title()<\/h3>\n<pre>s.title()<\/pre>\n<p>Hello World. How Are You Today?<\/p>\n<h3>.isalnum()<\/h3>\n<p>Check if all chars are alpha-numeric<\/p>\n<pre>'Hello'.isalnum()<\/pre>\n<p>True<\/p>\n<pre>'Hello!'.isalnum()<\/pre>\n<p>False<\/p>\n<h3>.isalpha()<\/h3>\n<p>Check if all chars are alpha-numeric<\/p>\n<pre>'Hello'.isalnum()<\/pre>\n<p>True<\/p>\n<pre>'Hello123'.isalnum()<\/pre>\n<p>False<\/p>\n<h3>.islower()<\/h3>\n<pre>'hello'.islower()<\/pre>\n<p>True<\/p>\n<pre>'Hello'.islower()<\/pre>\n<p>False<\/p>\n<h3>.isspace()<\/h3>\n<pre>'\\t \\t'.isspace()<\/pre>\n<p>True<\/p>\n<pre>'Hello'.isspace()<\/pre>\n<p>False<\/p>\n<h3>.istitle()<\/h3>\n<pre>\"Hello World. How Are You Today?\".istitle()<\/pre>\n<p>True<\/p>\n<pre>\"Hello world. How Are You Today?\".istitle()<\/pre>\n<p>False<\/p>\n<h3>.isupper()<\/h3>\n<pre>'HELLO\".isuppoer()<\/pre>\n<p>True<\/p>\n<pre>'HELLo\".isuppoer()<\/pre>\n<p>False<\/p>\n<h3>.islower()<\/h3>\n<pre>'hello.islower()<\/pre>\n<p>True<\/p>\n<pre>'HELLo\".islower()<\/pre>\n<p>False<\/p>\n<h3>.endswith()<\/h3>\n<pre>s = 'Hello'\r\ns.endswidth('o')<\/pre>\n<p>True<br \/>\n(This is the same as)<\/p>\n<pre>s[-1]=='o'<\/pre>\n<p>True<\/p>\n<h3>.count()<\/h3>\n<p>Count recurrence of a character in the string<\/p>\n<pre>s.count('o')<\/pre>\n<p>5<\/p>\n<h3>.find()<\/h3>\n<p>Find the first occurrence of a character in the string<\/p>\n<pre>s.find('o')<\/pre>\n<p>4<\/p>\n<h2>Formatting<\/h2>\n<h3>.center(count, char)<\/h3>\n<pre>s = 'Hello world'\r\ns.center(20,'.')<\/pre>\n<p>&#8230;.Hello world&#8230;..<\/p>\n<h3>.expandtabs()<\/h3>\n<pre>print(\"hello\/thi\")\r\n\"hello\/thi\".expandtabs()<\/pre>\n<pre>hello    hi\r\nhello    hi<\/pre>\n<h2>Regular Expressions Methods<\/h2>\n<h3>.split()<\/h3>\n<pre>s = \"hello world. how are you today?\"\r\ns.split()<\/pre>\n<p>[&#8216;hello&#8217;, &#8216;world.&#8217;, &#8216;how&#8217;, &#8216;are&#8217;, &#8216;you&#8217;, &#8216;today?&#8217;]<\/p>\n<pre>s.split('o')<\/pre>\n<p>[&#8216;hell&#8217;, &#8216; w&#8217;, &#8216;rld. h&#8217;, &#8216;w are y&#8217;, &#8216;u t&#8217;, &#8216;day?&#8217;]<\/p>\n<h3>.partition()<\/h3>\n<p>Splits only at the first occurrence. Returns the first part, the separator, and the trailing end.<\/p>\n<pre>s = \"hello world. how are you today?\"\r\ns.partition('o')<\/pre>\n<p>(&#8216;hell&#8217;, &#8216;o&#8217;, &#8216; world. how are you today?&#8217;)<\/p>\n<pre>s.partition('!')<\/pre>\n<p>(&#8216;hello world. how are you today?&#8217;, &#8221;, &#8221;)<\/p>\n<h1>Advanced Sets<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3650898#questions\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3650898#questions<\/a><\/p>\n<p>Sets will not take any duplicate items<\/p>\n<h3>.add()<\/h3>\n<pre>s = set()\r\ns.add(1)\r\ns.add(2)\r\ns.add(1)\r\ns<\/pre>\n<p>{1, 2}<\/p>\n<h3>.clear()<\/h3>\n<pre>s.clear()\r\ns<\/pre>\n<p>set()<\/p>\n<h3>.copy<\/h3>\n<pre>s = {1, 2, 3}\r\nsc = s.copy()\r\ns.add(4)\r\nsc<\/pre>\n<p>{1, 2, 3}<\/p>\n<h3>.difference()<\/h3>\n<pre>s.difference(sc)<\/pre>\n<p>{4}<\/p>\n<h3>.difference_update()<\/h3>\n<pre>s1 = {1,2,3}\r\ns2={1,4,5}\r\ns1.difference_update(s2)\r\ns1<\/pre>\n<p>{2, 3}<\/p>\n<h3>.discard()<\/h3>\n<p>Removes an item from the set if it exists<\/p>\n<pre>s = {1, 2, 3, 4}\r\ns.discard(2)\r\ns<\/pre>\n<p>{1, 3, 4}<\/p>\n<h3>.intersection()<\/h3>\n<p>Returns the common elements in 2 or more sets<\/p>\n<pre>s1 = {1, 2, 3, 4}\r\ns2 = {2, 3, 4, 5, 6}\r\ns3 = {2, 4, 6, 8}\r\ns1.intersection(s2, s3)<\/pre>\n<p>{2, 4}<\/p>\n<h3>.intersection_update<\/h3>\n<p>Updates the current set to equal the intersection of all sets tested.<\/p>\n<pre>s1 = {1, 2, 3, 4}\r\ns2 = {2, 3, 4, 5, 6}\r\ns3 = {2, 4, 6, 8}\r\ns1.intersection_update(s2, s3)\r\ns1<\/pre>\n<p>{2, 4}<\/p>\n<h3>.isdisjoint()<\/h3>\n<p>Returns True if there are no matching elements<\/p>\n<pre>s1 = {1, 2, 3, 4}\r\ns2 = {5, 6, 7, 8}\r\ns3 = {3, 4, 5, 6}\r\ns1.isdisjoint(s2)<\/pre>\n<p>True<\/p>\n<pre>s1.isdisjoint(s3)<\/pre>\n<p>False<\/p>\n<h3>.issubset()<\/h3>\n<pre>s1 = {1, 2}\r\ns2 = {1, 2, 3, 4, 5}\r\ns3 = {3, 4, 5, 6}\r\ns1.issubset(s2)<\/pre>\n<p>True<\/p>\n<pre>s1.issubset(s3)<\/pre>\n<p>False<\/p>\n<h3>.issuperset()<\/h3>\n<pre>s1 = {1, 2}\r\ns2 = {1, 2, 3, 4, 5}\r\ns3 = {3, 4, 5, 6}\r\ns2.issuperset(s1)<\/pre>\n<p>True<\/p>\n<pre>s3.issuperset(s1)<\/pre>\n<p>False<\/p>\n<h3>.symmetric_difference()<\/h3>\n<p>Returns a set of all items that are unique to both sets.<br \/>\n(Opposite of .intersection())<\/p>\n<pre>s1 = {0, 1, 2}\r\ns2 = {1, 2, 3, 4, 5}\r\ns1.symmetric_difference(s2)<\/pre>\n<p>{0, 3, 4, 5}<\/p>\n<h3>.symmetric_difference()<\/h3>\n<p>Returns a set of all items that are unique to both sets<\/p>\n<pre>s1 = {0, 1, 2}\r\ns2 = {1, 2, 3, 4, 5}\r\ns1.symmetric_difference_update(s2)\r\ns1<\/pre>\n<p>{0, 3, 4, 5}<\/p>\n<h3>.union()<\/h3>\n<p>Returns all elements in all sets<\/p>\n<pre>s1 = {1, 2}\r\ns2 = {1, 2, 3, 4, 5}\r\ns3 = {3, 4, 5, 6}\r\ns1.union(s2, s3)<\/pre>\n<p>{1, 2, 3, 4, 5, 6}<\/p>\n<h3>.update()<\/h3>\n<p>Sets the current set = all elements in all sets<\/p>\n<pre>s1 = {1, 2}\r\ns2 = {1, 2, 3, 4, 5}\r\ns3 = {3, 4, 5, 6}\r\ns1.update(s2, s3)\r\ns1<\/pre>\n<p>{1, 2, 3, 4, 5, 6}<\/p>\n<h1>Advanced Dictionaries<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3512774#questions\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3512774#questions<\/a><\/p>\n<h2>Dictionary Comprehension<\/h2>\n<p>Not common. Difficult to get keys that are not based on the values<br \/>\n{ key: value }<\/p>\n<pre>mydict = {x: x**2 for x in range(10)}\r\nmydict<\/pre>\n<p>{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}<\/p>\n<h3>.keys()<\/h3>\n<p>Returns the keys<br \/>\n<b>This can also be used without the function. Keys are returned by default<\/b><\/p>\n<pre>d = {'k1':1, 'k2':2}\r\nfor k in d:\r\n    print( k)<\/pre>\n<p>k1<br \/>\nk2<\/p>\n<h3>.items()<\/h3>\n<p>Returns (key, value) tuples<\/p>\n<pre>d = {'k1':1, 'k2':2}\r\nfor k in d:\r\n    print( k)<\/pre>\n<p>(&#8216;k1&#8217;, 1)<br \/>\n(&#8216;k2&#8217;, 2)<\/p>\n<h3>.values()<\/h3>\n<p>Returns the values<\/p>\n<pre>d = {'k1':1, 'k2':2}\r\nfor k in d.values():\r\n    print( k)<\/pre>\n<p>1<br \/>\n2<\/p>\n<h1>Advanced Lists<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3512772#questions\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3512772#questions<\/a><\/p>\n<h3>.append()<\/h3>\n<pre>mylist = [1, 2, 3]\r\nmylist.append(4)\r\nmylist<\/pre>\n<p>[1, 2, 3, 4]<\/p>\n<h3>.count()<\/h3>\n<p>Returns the number of times an element occurs in a list<\/p>\n<pre>mylist = [1, 2, 2, 3, 2]\r\nmylist.count(2)<\/pre>\n<p>3<\/p>\n<h3>.extend()<\/h3>\n<p>Almost the same as append, but appends a list instead of adding it as an item.<br \/>\n.append() example:<\/p>\n<pre>mylist = [1, 2, 3]\r\nmylist.append([4, 5])\r\nprint(mylist)<\/pre>\n<p>[1, 2, 3, [4, 5]]<br \/>\n.extend() example:<\/p>\n<pre>mylist = [1, 2, 3]\r\nmylist.extend([4, 5])\r\nprint(mylist)<\/pre>\n<p>[1, 2, 3, 4, 5]<\/p>\n<h3>.index()<\/h3>\n<p>Returns the first index of a value in the list<br \/>\n<b>Note: returns an error if the search value is not in the list.<\/b><\/p>\n<pre>mylist = [4, 5, 5, 8, 9]\r\nmylist.index(5)<\/pre>\n<p>1<\/p>\n<h3>.insert(index, object)<\/h3>\n<p>Inserts the object at the index provided.<\/p>\n<pre>mylist = [4, 5, 5, 8, 9]\r\nmylist.insert(2,'hello')\r\nmylist<\/pre>\n<p>[4, 5, &#8216;hello&#8217;, 5, 8, 9]<br \/>\n<b>Note: if the index provided is &gt; the last index, the value will be appended to the list.<\/b><\/p>\n<pre>mylist = [4, 5, 5, 8, 9]\r\nmylist.insert(20,'hello')\r\nmylist<\/pre>\n<p>[4, 5, 5, 8, 9, &#8216;hello&#8217;]<\/p>\n<h3>.pop()<\/h3>\n<p>Pops off and returns the list item in the list<\/p>\n<pre>mylist = [4, 5, 5, 8, 9]\r\nprint(mylist.pop())\r\nmylist<\/pre>\n<p>9<br \/>\n[4, 5, 5, 8]<\/p>\n<p>You can also pop a specific item from the list by it&#8217;s index.<\/p>\n<pre>mylist = [4, 5, 5, 8, 9]\r\nprint(mylist.pop(3))\r\nmylist<\/pre>\n<p>8<br \/>\n[4, 5, 5, 9]<\/p>\n<h3>.remove()<\/h3>\n<p>Removes the first occurrance of a value in the list<\/p>\n<pre>mylist = [4, 5, 5, 8, 9, 8]\r\nmylist.remove(8)\r\nmylist<\/pre>\n<p>[4, 5, 5, 9, 8]<\/p>\n<h3>.reverse()<\/h3>\n<p>Reverses the order of the items in place (index values are changed!)<\/p>\n<pre>mylist = [4, 5, 5, 8, 9, 8]\r\nmylist.reverse()\r\nmylist<\/pre>\n<p>[8, 9, 8, 5, 5, 4]<\/p>\n<h3>.sort()<\/h3>\n<p>Sorts ascending the items in place. (Index values are changed!)<\/p>\n<pre>mylist = [8, 9, 8, 5, 5, 4]\r\nmylist.sort()\r\nmylist<\/pre>\n<p>[4, 5, 5, 8, 8, 9]<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&lt; Section 15 | Section 17 &gt; Advanced Numbers https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/3650896?start=0#questions Hexadecimal hex() Values are returned as strings hex(246) 0xf6 hex(256) 0x200 Binary bin() Values are returned as strings bin(1234) 0b10011010010 pow() Looks like PHP to me&#8230; 2**3 == pow(2,3) abs() abs(-4) 4 round() Rounds to 0 places by default round(4.75,1) 4.8 round(4.75) 5 Advanced Strings ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/wiki.thomasandsofia.com\/?p=1938\" 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-1938","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\/1938","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=1938"}],"version-history":[{"count":10,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1938\/revisions"}],"predecessor-version":[{"id":1950,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1938\/revisions\/1950"}],"wp:attachment":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1938"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1938"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1938"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}