{"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":"\n<p class=\"wp-block-paragraph\">Let&#8217;s do some Booleans!<\/p>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\"><li>First let&#8217;s see how Boolean variables work in Python.&nbsp; Variables that are of Boolean type have only two values &#8216;True&#8217; or &#8216;False&#8217;<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Type and run:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>&nbsp;a_Bool = 3&gt;4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Abul&nbsp; = 6&lt;6.3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Ab_Wool =&nbsp; &#8216;cat&#8217;==&#8217;cat&#8217;<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>a_bull =&nbsp; &#8216;cow&#8217;==&#8217;bull&#8217;<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(a_Bool, Abul, Ab_Wool, a_bull)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">All the statements on the right side of each equation becomes a&nbsp; value of either &#8216;True&#8217; or &#8216;False&#8217;&nbsp; after you work them out.&nbsp; And those variables on the left side store only that &#8216;True&#8217; or &#8216;False&#8217; value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Three is&nbsp; NOT greater than 4. So this statement is &#8216;False&#8217;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Six is indeed less than six point three. So this statement is &#8216;True&#8217;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The two strings &#8216;cat&#8217; are exactly the same string. So the &#8216;cat&#8217;==&#8217;cat&#8217;&nbsp; is &#8216;True&#8217;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The\u00a0 string &#8216;cow&#8217; is not the same string as &#8216;bull&#8217;.\u00a0 So the statement equating them is &#8216;False&#8217;\u00a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2. >, &lt; and == are all comparison operators.\u00a0 Note that when we are assigning values to a variable, we use single operation &#8216;=&#8217;.\u00a0 But when we are comparing we use double &#8216;==&#8217;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If we want to make a statement using NOT EQUAL TO , we use !=<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Type and run:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>zila = 64<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(zila&gt;30)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(zila!=8)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(zila==&#8217;zila&#8217;)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(&#8221; \\&#8221;Bangladesh has more that 30 zilas\\&#8221; : %s &#8221;&nbsp; %(zila&gt;30))<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(&#8221; \\&#8221;Bangladesh has 8 divisions, not zilas\\&#8221; : %s &#8221;&nbsp; %(zila!=8))<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(&#8221; \\&#8221;The boolean variable zila is the same as the string &#8216;zila&#8217;\\&#8221; : %s &#8221;&nbsp; %(zila==&#8217;zila&#8217;))<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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 &#8211; but that is the best option &#8211; because Boolean variables have no argument specifier such as %b of\u00a0 their own<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3. There is this handy operator called &#8216;in&#8217; which can be used to check if an item exists in\u00a0 a list.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Type and run:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>dutyRoster =[&#8216;Mikasa&#8217;, &#8216;Arm<\/strong><strong>i<\/strong><strong>n&#8217;]<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(&#8216;Mikasa&#8217; in dutyRoster)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(&#8216;Arm<\/strong><strong>i<\/strong><strong>n&#8217; in dutyRoster)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(&#8220;Mikasa has duty today?&nbsp; &#8211; %s&#8221;&nbsp; %(&#8216;Mikasa&#8217; in dutyRoster))<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(&#8220;Eren has duty today?\u00a0 &#8211; %s&#8221;\u00a0 %(&#8216;Eren&#8217; in dutyRoster))<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">4. The &#8216;==&#8217; operator matches\u00a0 the values of two variables .<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;The &#8216;is&#8217; operation matches&nbsp; the &#8216;instances&#8217;&nbsp; &#8211; aka, whether those&nbsp; two variables are the EXACT same variable.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Type and run<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>avatar= [&#8216;earth&#8217;, &#8216;fire&#8217;, &#8216;air&#8217;, &#8216;water&#8217;]<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>frozen2 =[&#8216;earth&#8217;, &#8216;fire&#8217;, &#8216;air&#8217;, &#8216;water&#8217;]<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(avatar==frozen2)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(avatar is frozen2)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What do you get<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">5. In the above, avatar and frozen2\u00a0 are two variables with same exact VALUE\u00a0\u00a0 so\u00a0 first print function evaluates as &#8216;True&#8217; but they are NOT the SAME VARIABLE so\u00a0 the second statement evaluates as &#8216;False&#8217;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>To summarize :<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><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 class=\"wp-block-paragraph\"><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 class=\"wp-block-paragraph\">Let&#8217;s work on this a little more to really internalize the concept.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Type and run<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>a= [2,1]<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>b= [2,1]<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>c=a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(id(a))<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(id(b))<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(id(c))<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(a==b)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(a is b)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(a is c)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>c = c+a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(a is c<\/strong><em>)<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s do some Booleans! First let&#8217;s see how Boolean variables work in Python.&nbsp; Variables that are of Boolean type have only two values &#8216;True&#8217; or &#8216;False&#8217; Type and run: &nbsp;a_Bool = 3&gt;4 Abul&nbsp; = 6&lt;6.3 Ab_Wool =&nbsp; &#8216;cat&#8217;==&#8217;cat&#8217; a_bull =&nbsp; &#8216;cow&#8217;==&#8217;bull&#8217; 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,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":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}]}}