{"id":198,"date":"2020-11-07T11:11:50","date_gmt":"2020-11-07T11:11:50","guid":{"rendered":"http:\/\/lamiaiftekhar.com\/?page_id=198"},"modified":"2020-11-07T11:11:50","modified_gmt":"2020-11-07T11:11:50","slug":"20-minutes-python-day-10","status":"publish","type":"page","link":"https:\/\/lamiaiftekhar.com\/index.php\/20-minutes-python-day-10\/","title":{"rendered":"20-Minutes Python: Day 10"},"content":{"rendered":"<body>\n<p>Let\u2019s do some Booleans!<\/p>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\"><li>First let\u2019s see how Boolean variables work in Python.\u00a0 Variables that are of Boolean type have only two values \u2018True\u2019 or \u2018False\u2019<\/li><\/ol>\n\n\n\n<p>Type and run:<\/p>\n\n\n\n<p><strong>\u00a0a_Bool = 3&gt;4<\/strong><\/p>\n\n\n\n<p><strong>Abul\u00a0 = 6&lt;6.3<\/strong><\/p>\n\n\n\n<p><strong>Ab_Wool =\u00a0 \u2018cat\u2019==\u2019cat\u2019<\/strong><\/p>\n\n\n\n<p><strong>a_bull =\u00a0 \u2018cow\u2019==\u2019bull\u2019<\/strong><\/p>\n\n\n\n<p><strong>print(a_Bool, Abul, Ab_Wool, a_bull)<\/strong><\/p>\n\n\n\n<p>All the statements on the right side of each equation becomes a\u00a0 value of either \u2018True\u2019 or \u2018False\u2019\u00a0 after you work them out.\u00a0 And those variables on the left side store only that \u2018True\u2019 or \u2018False\u2019 value.<\/p>\n\n\n\n<p>Three is\u00a0 NOT greater than 4. So this statement is \u2018False\u2019<\/p>\n\n\n\n<p>Six is indeed less than six point three. So this statement is \u2018True\u2019<\/p>\n\n\n\n<p>The two strings \u2018cat\u2019 are exactly the same string. So the \u2018cat\u2019==\u2019cat\u2019\u00a0 is \u2018True\u2019<\/p>\n\n\n\n<p>The\u00a0 string \u2018cow\u2019 is not the same string as \u2018bull\u2019.\u00a0 So the statement equating them is \u2018False\u2019\u00a0<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>2. &gt;, &lt; and == are all comparison operators.\u00a0 Note that when we are assigning values to a variable, we use single operation \u2018=\u2019.\u00a0 But when we are comparing we use double \u2018==\u2019<\/p>\n\n\n\n<p>If we want to make a statement using NOT EQUAL TO , we use !=<\/p>\n\n\n\n<p>Type and run:<\/p>\n\n\n\n<p><strong>zila = 64<\/strong><\/p>\n\n\n\n<p><strong>print(zila&gt;30)<\/strong><\/p>\n\n\n\n<p><strong>print(zila!=8)<\/strong><\/p>\n\n\n\n<p><strong>print(zila==\u2019zila\u2019)<\/strong><\/p>\n\n\n\n<p><strong>print(\u201d \\\u201dBangladesh has more that 30 zilas\\\u201d : %s \u201d\u00a0 %(zila&gt;30))<\/strong><\/p>\n\n\n\n<p><strong>print(\u201d \\\u201dBangladesh has 8 divisions, not zilas\\\u201d : %s \u201d\u00a0 %(zila!=8))<\/strong><\/p>\n\n\n\n<p><strong>print(\u201d \\\u201dThe boolean variable zila is the same as the string \u2018zila\u2019\\\u201d : %s \u201d\u00a0 %(zila==\u2019zila\u2019))<\/strong><\/p>\n\n\n\n<p>Note that although a Boolean variable and a string cannot be equal (like in the third sentence) , we can use the argument specifier of string,\u00a0 %s,\u00a0 when using Boolean variables.\u00a0 Weird, I know \u2013 but that is the best option \u2013 because Boolean variables have no argument specifier such as %b of\u00a0 their own<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>3. There is this handy operator called \u2018in\u2019 which can be used to check if an item exists in\u00a0 a list.<\/p>\n\n\n\n<p>Type and run:<\/p>\n\n\n\n<p><strong>dutyRoster =[\u2018Mikasa\u2019, \u2018Arm<\/strong><strong>i<\/strong><strong>n\u2019]<\/strong><\/p>\n\n\n\n<p><strong>print(\u2018Mikasa\u2019 in dutyRoster)<\/strong><\/p>\n\n\n\n<p><strong>print(\u2018Arm<\/strong><strong>i<\/strong><strong>n\u2019 in dutyRoster)<\/strong><\/p>\n\n\n\n<p><strong>print(\u201cMikasa has duty today?\u00a0 \u2013 %s\u201d\u00a0 %(\u2018Mikasa\u2019 in dutyRoster))<\/strong><\/p>\n\n\n\n<p><strong>print(\u201cEren has duty today?\u00a0 \u2013 %s\u201d\u00a0 %(\u2018Eren\u2019 in dutyRoster))<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>4. The \u2018==\u2019 operator matches\u00a0 the values of two variables .<\/p>\n\n\n\n<p>\u00a0The \u2018is\u2019 operation matches\u00a0 the \u2018instances\u2019\u00a0 \u2013 aka, whether those\u00a0 two variables are the EXACT same variable.\u00a0<\/p>\n\n\n\n<p>Type and run<\/p>\n\n\n\n<p><strong>avatar= [\u2018earth\u2019, \u2018fire\u2019, \u2018air\u2019, \u2018water\u2019]<\/strong><\/p>\n\n\n\n<p><strong>frozen2 =[\u2018earth\u2019, \u2018fire\u2019, \u2018air\u2019, \u2018water\u2019]<\/strong><\/p>\n\n\n\n<p><strong>print(avatar==frozen2)<\/strong><\/p>\n\n\n\n<p><strong>print(avatar is frozen2)<\/strong><\/p>\n\n\n\n<p>What do you get<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>5. In the above, avatar and frozen2\u00a0 are two variables with same exact VALUE\u00a0\u00a0 so\u00a0 first print function evaluates as \u2018True\u2019 but they are NOT the SAME VARIABLE so\u00a0 the second statement evaluates as \u2018False\u2019<\/p>\n\n\n\n<p><em>To summarize :<\/em><\/p>\n\n\n\n<p><em>The <\/em><strong><em>==<\/em><\/strong><em> operator compares the values of both the operands and checks for value equality. Whereas <\/em><strong><em>is<\/em><\/strong><em> operator checks whether both the operands refer to the same object or not.<\/em><\/p>\n\n\n\n<p><em>From &lt;<\/em><a href=\"https:\/\/www.geeksforgeeks.org\/difference-operator-python\/\"><em>https:\/\/www.geeksforgeeks.org\/difference-operator-python\/<\/em><\/a><em>&gt;<\/em><\/p>\n\n\n\n<p>Let\u2019s work on this a little more to really internalize the concept.<\/p>\n\n\n\n<p>Type and run<\/p>\n\n\n\n<p><strong>a= [2,1]<\/strong><\/p>\n\n\n\n<p><strong>b= [2,1]<\/strong><\/p>\n\n\n\n<p><strong>c=a<\/strong><\/p>\n\n\n\n<p><strong>print(id(a))<\/strong><\/p>\n\n\n\n<p><strong>print(id(b))<\/strong><\/p>\n\n\n\n<p><strong>print(id(c))<\/strong><\/p>\n\n\n\n<p><strong>print(a==b)<\/strong><\/p>\n\n\n\n<p><strong>print(a is b)<\/strong><\/p>\n\n\n\n<p><strong>print(a is c)<\/strong><\/p>\n\n\n\n<p><strong>c = c+a<\/strong><\/p>\n\n\n\n<p><strong>print(a is c<\/strong><em>)<\/em><\/p>\n\n\n\n<p><\/p>\n<\/body>","protected":false},"excerpt":{"rendered":"<p>Let\u2019s do some Booleans! First let\u2019s see how Boolean variables work in Python.\u00a0 Variables that are of Boolean type have only two values \u2018True\u2019 or \u2018False\u2019 Type and run: \u00a0a_Bool = 3&gt;4 Abul\u00a0 = 6&lt;6.3 Ab_Wool =\u00a0 \u2018cat\u2019==\u2019cat\u2019 a_bull =\u00a0 \u2018cow\u2019==\u2019bull\u2019 print(a_Bool, Abul, Ab_Wool, a_bull) All the statements on the right side of each equation [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"class_list":["post-198","page","type-page","status-publish","hentry","entry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/pages\/198","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/comments?post=198"}],"version-history":[{"count":1,"href":"https:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/pages\/198\/revisions"}],"predecessor-version":[{"id":199,"href":"https:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/pages\/198\/revisions\/199"}],"wp:attachment":[{"href":"https:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/media?parent=198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}