{"id":129,"date":"2021-08-22T14:26:58","date_gmt":"2021-08-22T20:26:58","guid":{"rendered":"https:\/\/thenontechieguy.com\/?p=129"},"modified":"2021-08-22T14:28:06","modified_gmt":"2021-08-22T20:28:06","slug":"working-with-python-data-structures-part-1","status":"publish","type":"post","link":"https:\/\/thenontechieguy.com\/index.php\/2021\/08\/22\/working-with-python-data-structures-part-1\/","title":{"rendered":"Working with Python Data Structures-Part 1"},"content":{"rendered":"\n<p>Python Data Structures help you organize data and use it in the applications you intend to build using Python language.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Python provides the following data structures:<\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>List<\/li><li>Tuples<\/li><li>Dictionaries<\/li><li>Sets<\/li><\/ul>\n\n\n\n<p>We will review each in more detail and also try to see what could be the practical applications for each of these data structures.<\/p>\n\n\n\n<p>Before we look at the different data structures, we will look at the concept of Strings.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Strings:<\/h4>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<ul class=\"wp-block-list\"><li>A String is an immutable data type (means it cannot be changed)<\/li><li>It is a sequence of Unicode characters wrapped inside single, double, or triple quotes.<\/li><li>Examples of strings are &#8216;Today is Saturday&#8217;, &#8220;Today is a Holiday&#8221;, &#8220;&#8221; Tomorrow is Sunday&#8221;&#8221;&#8221;<\/li><li>Use print command to print a string<ul><li>e.g. string1= &#8216; Today is Saturday&#8217; <\/li><li>print(string1)<\/li><li>Output- &#8216; Today is Saturday&#8217;<\/li><\/ul><\/li><li>Use length function to determine the length of the string as shown in below screenshot<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/thenontechieguy.com\/wp-content\/uploads\/2021\/08\/Python-len-function-1024x576.png\" alt=\"\" class=\"wp-image-131\" srcset=\"https:\/\/thenontechieguy.com\/wp-content\/uploads\/2021\/08\/Python-len-function-1024x576.png 1024w, https:\/\/thenontechieguy.com\/wp-content\/uploads\/2021\/08\/Python-len-function-300x169.png 300w, https:\/\/thenontechieguy.com\/wp-content\/uploads\/2021\/08\/Python-len-function-768x432.png 768w, https:\/\/thenontechieguy.com\/wp-content\/uploads\/2021\/08\/Python-len-function-1536x864.png 1536w, https:\/\/thenontechieguy.com\/wp-content\/uploads\/2021\/08\/Python-len-function.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>Strings support indexing- which means you can retrieve a certain character from the string<\/li><li>Use below syntax:<ul><li>IF Day=&#8217;Saturday&#8217;, then Day[1]= a- note that index starts at 0 and 1st character of this string can be retrieved as Day[0]= S<\/li><li>Note: use print statement to print the results<\/li><\/ul><\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/thenontechieguy.com\/wp-content\/uploads\/2021\/08\/image-2-1024x576.png\" alt=\"\" class=\"wp-image-132\" srcset=\"https:\/\/thenontechieguy.com\/wp-content\/uploads\/2021\/08\/image-2-1024x576.png 1024w, https:\/\/thenontechieguy.com\/wp-content\/uploads\/2021\/08\/image-2-300x169.png 300w, https:\/\/thenontechieguy.com\/wp-content\/uploads\/2021\/08\/image-2-768x432.png 768w, https:\/\/thenontechieguy.com\/wp-content\/uploads\/2021\/08\/image-2-1536x864.png 1536w, https:\/\/thenontechieguy.com\/wp-content\/uploads\/2021\/08\/image-2.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>Note that Python supports negative indexing. To retrieve the last character of a string, use -1<\/li><li>Using the above example, Day[-1]=y and Day[-2]=a<\/li><\/ul>\n\n\n\n<p><\/p>\n<\/div><\/div>\n\n\n\n<p>In this post, we will look at the concept of &#8216;Lists&#8217; in Python in more details<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Lists in Python- A Basic Understanding: <\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>A list is a collection of comma separated values written within square brackets<\/li><li>Example of a list in python:<ul><li>even_numbers_greater_than_zero=[2,4,6,8,10]<\/li><\/ul><\/li><li>A few of the important characteristics of Lists are as below:<ul><li>Lists may contain items of same or different types (e.g. int, float, string  etc.). <\/li><li>homogeneous_list= [2,7,9,11,13]<\/li><\/ul><ul><li>heterogenous_list=[&#8220;yes&#8221;, 1, &#8220;True&#8221;]- note that the list has different types- integers and strings in this case<\/li><li>Lists can be indexed<ul><li>even_numbers_greater_than_zero=[2,4,6,8,10]<\/li><li>even_numbers_greater_than_zero[2]=6 #indexing returns the required item<\/li><\/ul><\/li><li>Lists can be sliced<ul><li>even_numbers_greater_than_zero[-3:]= [6,8,10] #slicing returns last 3 items of the list to create a new list<\/li><\/ul><\/li><li>Lists can support concatenation<ul><li>even_numbers_greater_than_zero=[2,4,6,8,10]<\/li><li>even_numbers_greater_than_zero_additional= [12,14,16,18]<\/li><li>print (even_numbers_greater_than_zero+even_numbers_greater_than_zero_additional)=[2,4,6,8,10,12,14,16,18]- creates a new list which contains a new list with items from both of the above lists<\/li><\/ul><\/li><li>Lists are mutable- which means it is possible to change its content<ul><li>even_numbers_greater_than_zero=[2,4,6,8,10]<\/li><li>even_numbers_greater_than_zero[2]= 7<\/li><li>even_numbers_greater_than_zero=[2,4,7,8,10] #the 3rd element in this list is modified based on the above statement<\/li><\/ul><\/li><li>Lists support len function<ul><li>While using the len function with lists, it lists down the number of items in the list<\/li><li>len(even_numbers_greater_than_zero)= 5<\/li><\/ul><\/li><li>It is possible to create nested lists<ul><li>even_numbers_greater_than_zero=[2,4,6,8,10]<\/li><li>odd_numbers_greater_than_zero=[1,3,5,7,9]<\/li><li>numbers= [even_numbers_greater_than_zero, odd_numbers_greater_than_zero]<\/li><li>print(numbers)=[[2,4,6,8,10], [1,3,5,7,9]]  #Python created a list of nested lists<\/li><\/ul><\/li><\/ul><\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"> Lists in Python- When to use?<\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>Use a list when you want to store different kinds of data type that may need to be changed in future<\/li><li>Use a list when you want to manipulate the data using slicing, concatenating, updating, deleting items in the list.<\/li><\/ul>\n\n\n\n<p>The above is my basic understanding of Lists as I teach myself the concept of &#8216;Lists&#8217; as a part of Python data structures.<\/p>\n\n\n\n<p>In subsequent posts, I will try to cover my understanding of other data structures in Python.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">References:<\/h3>\n\n\n\n<p>I am using the official Python documentation for learning Python<\/p>\n\n\n\n<p>Below is the link for reference (I am working on Python 3.9.6 version)<\/p>\n\n\n\n<p><a href=\"https:\/\/docs.python.org\/3.9\/\">https:\/\/docs.python.org\/3.9\/<\/a><\/p>\n\n\n\n<p>For Python Setup, refer to my below posts:<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-thenontechieguy wp-block-embed-thenontechieguy\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"sPCAT7WuTE\"><a href=\"https:\/\/thenontechieguy.com\/index.php\/2021\/08\/17\/getting-started-with-python\/\">Getting started with Python!<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Getting started with Python!&#8221; &#8212; thenontechieguy\" src=\"https:\/\/thenontechieguy.com\/index.php\/2021\/08\/17\/getting-started-with-python\/embed\/#?secret=sPCAT7WuTE\" data-secret=\"sPCAT7WuTE\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-thenontechieguy wp-block-embed-thenontechieguy\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"Nttha1dgMu\"><a href=\"https:\/\/thenontechieguy.com\/index.php\/2021\/08\/16\/is-your-laptop-data-science-ready\/\">Is your Laptop &#8216;Data Science&#8217; Ready?<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Is your Laptop &#8216;Data Science&#8217; Ready?&#8221; &#8212; thenontechieguy\" src=\"https:\/\/thenontechieguy.com\/index.php\/2021\/08\/16\/is-your-laptop-data-science-ready\/embed\/#?secret=Nttha1dgMu\" data-secret=\"Nttha1dgMu\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Python Data Structures help you organize data and use it in the applications you intend to build using Python language. Python provides the following data&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[14,12],"tags":[],"class_list":["post-129","post","type-post","status-publish","format-standard","hentry","category-data-structures","category-python"],"_links":{"self":[{"href":"https:\/\/thenontechieguy.com\/index.php\/wp-json\/wp\/v2\/posts\/129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thenontechieguy.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thenontechieguy.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thenontechieguy.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thenontechieguy.com\/index.php\/wp-json\/wp\/v2\/comments?post=129"}],"version-history":[{"count":4,"href":"https:\/\/thenontechieguy.com\/index.php\/wp-json\/wp\/v2\/posts\/129\/revisions"}],"predecessor-version":[{"id":136,"href":"https:\/\/thenontechieguy.com\/index.php\/wp-json\/wp\/v2\/posts\/129\/revisions\/136"}],"wp:attachment":[{"href":"https:\/\/thenontechieguy.com\/index.php\/wp-json\/wp\/v2\/media?parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thenontechieguy.com\/index.php\/wp-json\/wp\/v2\/categories?post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thenontechieguy.com\/index.php\/wp-json\/wp\/v2\/tags?post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}