{"id":2058,"date":"2024-03-05T19:06:33","date_gmt":"2024-03-05T19:06:33","guid":{"rendered":"https:\/\/www.tutavo.com\/Kachina\/?p=2058"},"modified":"2024-03-16T22:57:02","modified_gmt":"2024-03-16T22:57:02","slug":"learn-python-notes-51-60","status":"publish","type":"post","link":"https:\/\/www.tutavo.com\/Kachina\/learn-python-notes-51-60\/","title":{"rendered":"Learn Python &#8211; NOTES 51 &#8211; 60"},"content":{"rendered":"<p>This follows the lesson plan at https:\/\/edube.org<\/p>\n<p><strong>Program 51 &#8211; Conjunctions, Disjunctions, and Negations<\/strong><\/p>\n<pre>\r\nnot (p and q) == (not p) or (not q)\r\nnot (p or q) == (not p) and (not q)\r\n\r\ncan be used in the abbreviated form known as op=\r\n    & (ampersand) - bitwise conjunction;\r\n    | (bar) - bitwise disjunction;\r\n    ~ (tilde) - bitwise negation;\r\n    ^ (caret) - bitwise exclusive or (xor)\r\n\r\nLet's make it easier:\r\n\r\n    & requires exactly two 1s to provide 1 as the result;\r\n    | requires at least one 1 to provide 1 as the result;\r\n    ^ requires exactly one 1 to provide 1 as the result.\r\n\r\n<\/pre>\n<p><strong>Program 52 &#8211; Arrays \/ Lists<\/strong><\/p>\n<pre>\r\nnumbers = [10, 5, 7, 2, 1]\r\nprint(\"List content:\", numbers)  # Printing original list content.\r\nnumbers[0] = 45\r\nprint(\"List content:\", numbers)  # Printing original list content.\r\nprint(len(numbers))\r\ndel numbers[3]\r\nprint(numbers)\r\nprint(numbers[3])\r\n      \r\nnumbers = [10, 5, 7, 2, 1]\r\nprint(\"Original list content:\", numbers)  # Printing original list content.\r\n\r\nnumbers[0] = 111\r\nprint(\"\\nPrevious list content:\", numbers)  # Printing previous list content.\r\n\r\nnumbers[1] = numbers[4]  # Copying value of the fifth element to the second.\r\nprint(\"Previous list content:\", numbers)  # Printing previous list content.\r\n\r\nprint(\"\\nList's length:\", len(numbers))  # Printing previous list length.\r\n\r\n###\r\n\r\ndel numbers[1]  # Removing the second element from the list.\r\nprint(\"New list's length:\", len(numbers))  # Printing new list length.\r\nprint(\"\\nNew list content:\", numbers)  # Printing current list content.\r\n\r\n###\r\n<\/pre>\n<p>print(numbers[-1])<br \/>\nThis will print the LAST number in a list.<br \/>\nprint(numbers[-2])<br \/>\nSimilarly, the element with an index equal to -2 is the<br \/>\nnext to the last element in the list.<\/p>\n<p><strong>Program 53 &#8211; More on Lists<\/strong><\/p>\n<pre>\r\nhat = [1, 2, 3, 4, 5]\r\nprint(hat)\r\nq = int(input(\"Change number 3 to any other number: \"))\r\nhat[2] = q\r\ndel(hat[4])\r\nprint(hat)\r\nprint(len(hat))\r\nprint(hat)\r\n\r\n<\/pre>\n<p><strong>Program 54 &#8211; Functions and Methods<\/strong><\/p>\n<pre>\r\nresult = function(arg) # Passes something back to the data\r\nresult = data.method(arg) # Method does something to create a value\r\n\r\nlist.append(value) # appends to the END of the list\r\nlist.insert(location, value) # inserts SOMEWHERE in the list\r\n\r\nnumbers = [111, 7, 2, 1]\r\nprint(len(numbers))\r\nprint(numbers)\r\n\r\n###\r\n\r\nnumbers.append(4)\r\n\r\nprint(len(numbers))\r\nprint(numbers)\r\n\r\n###\r\n\r\nnumbers.insert(0, 222) # NOTE:  This does not use brackets\r\nprint(len(numbers))\r\nprint(numbers)\r\nnumbers.insert(1, 333) # NOTE:  This does not use brackets\r\nprint(numbers)\r\n\r\n<\/pre>\n<p><strong>Program 55 &#8211; Working with Lists <\/strong><\/p>\n<pre>\r\nmy_list = []  # Creating an empty list.\r\n\r\nfor i in range(5):\r\n    my_list.append(i + 1)\r\n # appends to the end\r\n    print(my_list)\r\n\r\nmy_list2 = []  # Creating an empty list.\r\n\r\nfor i in range(5):\r\n    my_list2.insert(0, i + 1) # inserts as first char\r\n    print(my_list2)\r\n<\/pre>\n<p><strong>Program 56 &#8211; USING len() or just for loop<\/strong><\/p>\n<pre>\r\nmy_list = [10, 1, 8, 3, 5]\r\ntotal = 0\r\nfor i in my_list:\r\n    total += i\r\nprint(total)\r\n\r\nmy_list = [10, 1, 8, 3, 5]\r\ntotal = 0\r\nfor i in range(len(my_list)):\r\n    total += my_list[i]\r\nprint(total)\r\n\r\n<\/pre>\n<p><strong>Program 57 &#8211; Append, Delete, Insert<\/strong><\/p>\n<pre>\r\n\r\n# step 1\r\nbeatles = []\r\nprint(\"Step 1:\", beatles)\r\n\r\n\r\n# step 2\r\nbeatles.append(\"John\")\r\nbeatles.append(\"Paul\")\r\nbeatles.append(\"George\")\r\nprint(\"Step 2:\", beatles)\r\n\r\n# step 3\r\nfor i in range(2):\r\n    beatles.append(input(\"Names Stu and Pete: \"))\r\nprint(\"Step 3:\", beatles)\r\n\r\n# step 4\r\ndel beatles[4] \r\ndel beatles[3] \r\nprint(\"Step 4:\",beatles)\r\n\r\n# step 5\r\nbeatles.insert(0, \"Ringo\") # inserts as first char\r\nprint(\"Step 5:\", beatles)\r\n\r\n# testing list legth\r\nprint(\"The Fab\", len(beatles))\r\n\r\n# Ad Hoc Test\r\nprint(beatles[3])  # outputs: George\r\n\r\n# Ad Hoc Test\r\nbeatles.insert(0, \"Steve\")\r\nbeatles.append(\"Bob\")\r\nprint(beatles)  \r\n\r\n<\/pre>\n<p><strong>Program 58 &#8211; Bubble Sort<\/strong><\/p>\n<pre>\r\n\r\nmy_list = [8, 10, 6, 2, 4, 9, 15, 22, 3, 5]  # list to sort\r\nswapped = True  # It's a little fake, we need it to enter the while loop.\r\nprint(my_list)\r\nwhile swapped:\r\n    swapped = False  # no swaps so far\r\n    for i in range(len(my_list) - 1):\r\n        if my_list[i] > my_list[i + 1]:\r\n            swapped = True  # a swap occurred!\r\n            my_list[i], my_list[i + 1] = my_list[i + 1], my_list[i]\r\n\r\nprint(my_list)\r\n\r\n# OR\r\n\r\nsimple_sort = [8, 10, 6, 2, 4, 9, 15, 22, 3, 5]\r\nprint(simple_sort)\r\nsimple_sort.sort()\r\nprint(simple_sort)\r\nsimple_sort.reverse()\r\nprint(simple_sort)\r\n\r\n<\/pre>\n<p><strong>Program 59 &#8211; SORT STORED IN MEMORY SPACE &#8211; CAUTION<\/strong><\/p>\n<pre>\r\nlist_1 = [1]\r\nlist_2 = list_1\r\nlist_1[0] = 2\r\nprint(list_2) # PRINTS OUT A 2\r\n<\/pre>\n<p>You could say that:<br \/>\n1. the name of an ordinary variable is the name of its content;<br \/>\n2. the name of a list is the name of a memory location where the list is stored.<\/p>\n<pre>\r\nlist_1 = [1]\r\nlist_2 = list_1[:]\r\nlist_1[0] = 2\r\nprint(list_2) # PRINTS OUT A 1\r\n<\/pre>\n<p>This is called a slice.  It copies the contents, not the memory location.<\/p>\n<p>new_list = my_list[:] # Copies entire list contents<br \/>\nmy_list[0:end]   is the same as   my_list[:end]<br \/>\nmy_list[:3] copies data from 0, 1, and 2, but not 3<br \/>\nmy_list[start:]   is the same as   my_list[start:len(my_list)]<br \/>\nnew_list = my_list[3:]  copies everything from the 4th value to the end<\/p>\n<p>omitting both start and end makes a copy of the whole list:<br \/>\nmy_list = [10, 8, 6, 4, 2]<br \/>\nnew_list = my_list[:]<\/p>\n<p>You can delete with slices as well<br \/>\nmy_list = [10, 8, 6, 4, 2]<br \/>\ndel my_list[1:3]<br \/>\nprint(my_list)<\/p>\n<p>Deleting all the elements at once is possible too:<br \/>\nmy_list = [10, 8, 6, 4, 2]<br \/>\ndel my_list[:]<br \/>\nprint(my_list)<br \/>\nThe list becomes empty, and the output is: []<\/p>\n<p><strong>Program 60 &#8211; Does a list contain X Y Z <\/strong><\/p>\n<pre>\r\nelem in my_list #  returns True\r\nelem not in my_list # returns False\r\n\r\nmy_list = [0, 3, 12, 8, 2]\r\nprint(5 in my_list)\r\nprint(5 not in my_list)\r\nprint(12 in my_list)\r\n\r\n# Find largest value in a list\r\nmy_list = [17, 3, 11, 34, 5, 1, 9, 7, 15, 13]\r\nlargest = my_list[0]\r\nfor i in range(1, len(my_list)):\r\n    if my_list[i] > largest:\r\n        largest = my_list[i]\r\nprint(largest)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This follows the lesson plan at https:\/\/edube.org Program 51 &#8211; Conjunctions, Disjunctions, and Negations not (p and q) == (not p) or (not q) not (p or q) == (not p) and (not q) can be used in the abbreviated form known as op= &#038; (ampersand) &#8211; bitwise conjunction; | (bar) &#8211; bitwise disjunction; ~ &hellip; <a href=\"https:\/\/www.tutavo.com\/Kachina\/learn-python-notes-51-60\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Learn Python &#8211; NOTES 51 &#8211; 60&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[9],"tags":[],"class_list":["post-2058","post","type-post","status-publish","format-standard","hentry","category-educational"],"_links":{"self":[{"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/posts\/2058","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/comments?post=2058"}],"version-history":[{"count":21,"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/posts\/2058\/revisions"}],"predecessor-version":[{"id":2091,"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/posts\/2058\/revisions\/2091"}],"wp:attachment":[{"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/media?parent=2058"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/categories?post=2058"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tutavo.com\/Kachina\/wp-json\/wp\/v2\/tags?post=2058"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}