{"id":217,"date":"2020-11-10T09:44:03","date_gmt":"2020-11-10T09:44:03","guid":{"rendered":"http:\/\/lamiaiftekhar.com\/?page_id=217"},"modified":"2020-11-10T09:46:39","modified_gmt":"2020-11-10T09:46:39","slug":"217-2","status":"publish","type":"page","link":"http:\/\/lamiaiftekhar.com\/index.php\/217-2\/","title":{"rendered":"20-Minutes Python: Day 9"},"content":{"rendered":"<body>\n<p class=\"wp-block-paragraph\">Today we learn about lists!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Remember arrays from C++ ? \ud83d\ude00\u00a0 If not, no worries \u2013 just think of any \u2018To do\u2019 list or\u00a0 \u2018shopping\u00a0 list\u2019!\u00a0 A list\u00a0 is\u00a0 a collection of variables \u2013 and there is no limit in the number of variables!\u00a0 You can have a list of two items to a thousand items and beyond!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also,\u00a0 a list can have any type of variables in it \u2013 an integer list, a string list or even mix and match lists (lists containing different variable type).\u00a0 Because you do not have to explicitly define variables in Python like many other language \u2013 making lists\u00a0 is very easy \u2013 just throw in whatever variables you need to directly in your new list!<\/p>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\"><li>Let\u2019s create some lists.\u00a0 Type and run:<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>list1 = [3,2,1]<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(list1)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>PPG = [ \u2018Sugar\u2019, \u2018Spice\u2019, \u2018Everything Nice\u2019]<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(PPG)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>MR = [\u2018Mushfiqur Rahman\u2019, \u2018Bangladeshi\u2019, 33, 36.31]<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print (MR)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2. We can add more items to an already created list using \u2018append()\u2019<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>PPG.append(\u2018Chemical X\u2019)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(PPG)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>list1.append(\u201cLet\u2019s go!\u201d)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(list1)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This fancy\u00a0 \u2018thing1.thing2()\u2019\u00a0\u00a0 system is a cool stuff in Python where thing 1 is called an \u2018object\u2019 or \u2018class\u2019 and thing2 is called a method.\u00a0 So the general format\u00a0 is\u00a0\u00a0\u00a0 object.method().\u00a0\u00a0\u00a0\u00a0\u00a0 We use \u2018methods\u2019\u00a0 to modify an \u2018object\u2019.\u00a0 We will learn more about these later.\u00a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3. We can also create a list the following way.\u00a0\u00a0 This may look cumbersome \u2013 but this is for the times you do not know what are the items that are\u00a0 going to be in a list from the beginning. So you\u00a0 start with an empty list and\u00a0 you just add data as you go.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>chaptersCompleted = []<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>chaptersCompleted.append(\u2018print\u2019)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>chaptersCompleted.append(\u2018variables\u2019)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>chaptersCompleted.append(\u2018strings\u2019)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(\u2018I completed the following topics in first week:\u00a0 %s\u2019 %chaptersCompleted)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">4. How do we extract an item from the list?\u00a0 We specify the index in square brackets!\u00a0 Please note that indexing starts form 0.\u00a0 So first item in a list is positioned at index 0, second item at index 1, third item at index 2 and so on.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>secondNumber = list1[1]<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(secondNumber)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Print(\u2018The age of Mushfiqur Rahim is %d\u2019\u00a0 %(MR[2]))<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">5. Try accessing an index that is bigger that a list\u2019s size.\u00a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(PPG[10])<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What happens?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is one of the most common problems\u00a0 because we often have an n-size list and then use list[n]\u00a0\u00a0 to extract the last item and get an out-of-range error.\u00a0 But the last item is actually at n-1!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">6. To get the size of your list ( number of items in your list) you can use the function len().\u00a0\u00a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(len(PPG))<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>print(PPG)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note that len() is a <em>function<\/em> (has the capacity to take in arguments) ,\u00a0 not a <em>method<\/em> ( methods come after a \u2018dot\u2019 and takes no arguments).\u00a0 We will do functions later.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Exercise<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Make a list of four or five or six\u00a0 shows you plan\u00a0 to watch and print the following .<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>I\u2019ve prepared a list of <\/strong><strong><em>\u00a0{# of items in your list<\/em><\/strong><strong>}\u00a0 shows to watch and am very excited to start with <\/strong><strong><em>{first item in your list}<\/em><\/strong><strong>!\u00a0<\/strong><\/p>\n<\/body>","protected":false},"excerpt":{"rendered":"<p>Today we learn about lists! Remember arrays from C++ ? \ud83d\ude00\u00a0 If not, no worries \u2013 just think of any \u2018To do\u2019 list or\u00a0 \u2018shopping\u00a0 list\u2019!\u00a0 A list\u00a0 is\u00a0 a collection of variables \u2013 and there is no limit in the number of variables!\u00a0 You can have a list of two items to a thousand [&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-217","page","type-page","status-publish","hentry","entry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/pages\/217","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/comments?post=217"}],"version-history":[{"count":2,"href":"http:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/pages\/217\/revisions"}],"predecessor-version":[{"id":220,"href":"http:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/pages\/217\/revisions\/220"}],"wp:attachment":[{"href":"http:\/\/lamiaiftekhar.com\/index.php\/wp-json\/wp\/v2\/media?parent=217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}