{"id":215,"date":"2020-11-10T09:40:09","date_gmt":"2020-11-10T09:40:09","guid":{"rendered":"http:\/\/lamiaiftekhar.com\/?page_id=215"},"modified":"2020-11-10T09:40:09","modified_gmt":"2020-11-10T09:40:09","slug":"20-minutes-python-day-8","status":"publish","type":"page","link":"https:\/\/lamiaiftekhar.com\/index.php\/20-minutes-python-day-8\/","title":{"rendered":"20-Minutes Python: Day 8"},"content":{"rendered":"<body>\n<p class=\"wp-block-paragraph\">In the\u00a0 last session, we learned how to do operations using integer and float variables.\u00a0\u00a0<br><br>We can do some operations with string variables too!\u00a0\u00a0 Let\u2019s take a look.<\/p>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\"><li>Define two strings w1 and w2 , once with the string \u2018Carol\u2019 and the other with \u2018Tuesday\u2019.<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Now write and run the following:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(w1+\u201d and \u201c+w2)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note the added space before and after \u2018and\u2019 to print out a syntactically proper phrase.\u00a0\u00a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2. Run the following and see what happens!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>a_word = \u2018hello!\u2019<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>an_Echo = a_word*10<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(an_Echo)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3. Do the same with below:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>tolerable= \u2018Are we there yet? \u2018<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>annoying = tolerable*30<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(annoying)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">4. Try running the following\u00a0 and see what happens.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>a=1<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>b=2<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>c = \u2018buckle my shoe\u2019<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(a+b+c)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">5. Clearly, we cannot add integers to strings.\u00a0\u00a0\u00a0 But we can do other good stuff with string and numbers \u2013 its ok.\u00a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s start with the following.\u00a0 Write and run.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>powerSource = \u2018Grayskull\u2019<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(\u2018He-man has the power of %s\u2019\u00a0 %powerSource)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">6. \u00a0\u00a0 Write and run:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>name = \u2018Kenpachi\u2019<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>number = 13<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print( \u201c%s is the captain of Squad %d\u201d %(name, number))<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">7. These are called \u2018string formatting\u2019 .\u00a0 The % operation is NOT A MODULO, but used to format a set of variables\u00a0 that are grouped together in a \u2018tuple\u2019\u00a0 [e.g.\u00a0 (name, number)] .\u00a0 %d, %s are called argument specifiers.\u00a0\u00a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u00a0 We can use %f as an argument specifier for floating\u00a0 point (decimal) numbers. Also we can use\u00a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u00a0\u00a0\u00a0 %.&lt;number of digits&gt;f\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 to set\u00a0 the specific number of digits after the decimal point of a floating number.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Write and run the following<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>piValue = 3.14159<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(\u201cThe constant \u2018pi\u2019 is also known as the Archimedes\u2019 constant and has the value %f\u201d %piValue)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(\u201cThe constant \u2018pi\u2019 is also known as the Archimedes\u2019 constant and has the value %.4f\u201d %piValue)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(\u201cThe constant \u2018pi\u2019 is also known as the Archimedes\u2019 constant and has the value %.2f\u201d %piValue)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Exercise:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Print the sentence\u00a0\u00a0 (no underline \u2013 just print the sentence normally) :\u00a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Mushfiqur Rahim<\/strong><strong> is a <\/strong><strong>33<\/strong><strong>-year-old <\/strong><strong>Bangladeshi<\/strong><strong> cricketer with a batting average of <\/strong><strong>36.31<\/strong><strong> in ODI.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But extract the\u00a0 four underlined parts from stored variables. You can choose your own variable names.<\/p>\n<\/body>","protected":false},"excerpt":{"rendered":"<p>In the\u00a0 last session, we learned how to do operations using integer and float variables.\u00a0\u00a0 We can do some operations with string variables too!\u00a0\u00a0 Let\u2019s take a look. Define two strings w1 and w2 , once with the string \u2018Carol\u2019 and the other with \u2018Tuesday\u2019. Now write and run the following: print(w1+\u201d and \u201c+w2) Note [&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-215","page","type-page","status-publish","hentry","entry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/pages\/215","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=215"}],"version-history":[{"count":1,"href":"https:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/pages\/215\/revisions"}],"predecessor-version":[{"id":216,"href":"https:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/pages\/215\/revisions\/216"}],"wp:attachment":[{"href":"https:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/media?parent=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}