{"id":2578,"date":"2020-08-17T16:39:15","date_gmt":"2020-08-17T16:39:15","guid":{"rendered":"https:\/\/idiagress.com\/vinaaypatil\/?p=2578"},"modified":"2023-10-28T07:19:38","modified_gmt":"2023-10-28T07:19:38","slug":"1-4-strings-lists-and-directories","status":"publish","type":"post","link":"https:\/\/idiagress.com\/vinaaypatil\/2020\/08\/17\/1-4-strings-lists-and-directories\/","title":{"rendered":"1.4 Strings, Lists and Directories"},"content":{"rendered":"\n<p>In Python, the default data type for the &#8216;input&#8217; command is a <strong>string<\/strong>. <\/p>\n\n\n\n<p>A string is essentially a collection of characters, which can include letters, symbols, numbers, and more. It&#8217;s important to note that when you input numbers using the &#8216;input&#8217; command, they are stored as characters within a string. This means that you cannot perform mathematical operations on them directly.<\/p>\n\n\n\n<p>Let&#8217;s understand strings with a straightforward Python program, &#8216;<strong>strings.py<\/strong>&#8216;:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>_name=input(\"Enter Name :\") \nprint(_name)<span class=\"Apple-converted-space\">\u00a0<\/span><\/code><\/pre>\n\n\n\n<p>When you run this program, you will get the same string that you entered as the output. This showcases the string data type&#8217;s role in capturing and displaying text-based information in Python programs<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/simple_string_progam_python.png\"><img loading=\"lazy\" decoding=\"async\" width=\"562\" height=\"80\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/simple_string_progam_python.png\" alt=\"\" class=\"wp-image-2579\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/simple_string_progam_python.png 562w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/simple_string_progam_python-300x43.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/simple_string_progam_python-335x48.png 335w\" sizes=\"auto, (max-width: 562px) 100vw, 562px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>In Python, when we input data using the &#8216;input&#8217; function, the input is stored as a collection of characters within a string. Each character in the string is assigned a unique position number, starting with zero at the leftmost character and increasing by 1 as we move rightward through the string. This concept is technically known as &#8220;<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>string indexing<\/strong><\/mark>.&#8221;<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><b>Index number<\/b><\/td><td><b>0<\/b><\/td><td><b>1<\/b><\/td><td><b>2<\/b><\/td><td><b>3<\/b><\/td><td><b>4<\/b><\/td><td><b>5<\/b><\/td><td><b>6<\/b><\/td><td><b>7<\/b><\/td><td><b>8<\/b><\/td><td><b>9<\/b><\/td><td><b>10<\/b><\/td><\/tr><tr><td>String<span class=\"Apple-converted-space\">&nbsp;<\/span><\/td><td>p<\/td><td>u<span class=\"Apple-converted-space\">&nbsp;<\/span><\/td><td>r<\/td><td>u<\/td><td>s<\/td><td>h<\/td><td>o<\/td><td>t<\/td><td>t<\/td><td>a<\/td><td>m<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>String indexing allows us to access and manipulate individual characters within a string by referring to their specific positions, making it a fundamental concept in string processing and manipulation<\/p>\n\n\n\n<p>This indexing gives us ability to interact with individual members of the string, for example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>_name&#091;0]\n\nthis will give us \u00a0 \u00a0 \u00a0 _name&#091;0] = p<\/code><\/pre>\n\n\n\n<p>We can also extract part of the string, for example, if we want to assign the nickname as the first four letters of the name,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>_name&#091;0:4]\n\nthis will give us \u00a0 \u00a0 \u00a0_name&#091;0:4] = puru<\/code><\/pre>\n\n\n\n<p>If you observe, the &#8216;4th&#8217; index member of the string is &#8216;s&#8217;. <\/p>\n\n\n\n<p>However, when we extract [0:4], &#8216;s&#8217; was not included in the result. <\/p>\n\n\n\n<p>This is because the selection process excludes the upper-bound value. When we extract [0:4], it retrieves characters at indices [0, 1, 2, and 3]. In this case, &#8216;p,&#8217; &#8216;u,&#8217; &#8216;r,&#8217; and &#8216;u&#8217; are extracted, excluding the character at the upper bound (index 4). <\/p>\n\n\n\n<p>This behavior is a fundamental aspect of Python&#8217;s string indexing and slicing<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>We can also <span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><b>\u2018add\u2019<span class=\"Apple-converted-space\">&nbsp;<\/span><\/b> <span class=\"Apple-converted-space\">&nbsp; <\/span>two strings with a straightforward <span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><b>\u2018+\u2019<\/b> <span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>operator. For example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>_name=input(\"Enter Name<span class=\"Apple-converted-space\">\u00a0 <\/span>\")\n_middle=input(\"Enter middle name \")\n_fullname=_name+_middle\n<b>print<\/b>(_fullname)<\/code><\/pre>\n\n\n\n<p>the output we will get for this \u2018simple_string.py\u2019 is<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/string_addition_python.png\"><img loading=\"lazy\" decoding=\"async\" width=\"593\" height=\"100\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/string_addition_python.png\" alt=\"\" class=\"wp-image-2580\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/string_addition_python.png 593w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/string_addition_python-300x51.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/string_addition_python-335x56.png 335w\" sizes=\"auto, (max-width: 593px) 100vw, 593px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>An observation, empty spaces need to be defined explicitly, because addition is a simple combination of the strings. So a better command is \u2014<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>_fullname = _name+\" \"+_middle<\/code><\/pre>\n\n\n\n<p>Now, the string<span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>\u2018_fullname\u2019<span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>has become long. (11 characters + space + 6 characters ), 18 characters long. Suppose, we are required to extract the last four letters <span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>\u2018xman\u2019 <span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>, going by usual indexing (left to right), it will be a long count. To make things easy, python uses negative indexing from right to left, as shown below &#8212;<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><b>Index<\/b><\/td><td><b>0<\/b><\/td><td><b>1<\/b><\/td><td><b>2<\/b><\/td><td><b>3<\/b><\/td><td><b>4<\/b><\/td><td><b>5<\/b><\/td><td><b>6<\/b><\/td><td><b>7<\/b><\/td><td><b>8<\/b><\/td><td><b>9<\/b><\/td><td><b>10<\/b><\/td><td><b>11<\/b><\/td><td><b>12<\/b><\/td><td><b>13<\/b><\/td><td><b>14<\/b><\/td><td><b>15<\/b><\/td><td><b>16<\/b><\/td><td><b>17<\/b><\/td><\/tr><tr><td>String<\/td><td>p<\/td><td>u<\/td><td>r<\/td><td>u<\/td><td>s<\/td><td>h<\/td><td>o<\/td><td>t<\/td><td>t<\/td><td>a<\/td><td>m<\/td><td><span class=\"Apple-converted-space\">&nbsp;<\/span><\/td><td>l<\/td><td>a<\/td><td>x<\/td><td>m<\/td><td>a<\/td><td>n<\/td><\/tr><tr><td><b>Reverse<\/b><\/td><td><b>-18<\/b><\/td><td><b>-17<\/b><\/td><td><b>-16<\/b><\/td><td><b>-15<\/b><\/td><td><b>-14<\/b><\/td><td><b>-13<\/b><\/td><td><b>-12<\/b><\/td><td><b>-11<\/b><\/td><td><b>-10<\/b><\/td><td><b>-9<\/b><\/td><td><b>-8<\/b><\/td><td><b>-7<\/b><\/td><td><b>-6<\/b><\/td><td><b>-5<\/b><\/td><td><b>-4<\/b><\/td><td><b>-3<\/b><\/td><td><b>-2<\/b><\/td><td><b>-1<\/b><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Thus, to extract <span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>\u2018<b>xman<\/b>\u2019 <span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span>from the full string, we can use <span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp;<\/span><b> _fullname[-4,-1]<\/b><\/p>\n\n\n\n<p>However, remember the <span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><b>\u2018upper-bound\u2019<\/b><span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>exclusion,<span class=\"Apple-converted-space\">\u00a0 <\/span>thus<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><b>\u2018-1\u2019<\/b> <span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>will get excluded and our output will be<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><b>_fullname&#091;-4, -1] = xma<\/b><\/code><\/pre>\n\n\n\n<p>to get around this, we will not give the upper-bound<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><b>_fullname&#091;-4 : ] = xman<\/b><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Let\u2019s combine and illustrate our string operations in a <span class=\"Apple-converted-space\">\u00a0 <\/span><b>\u2018string_indexing.py<\/b>\u2019<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/string_indexing_python.png\"><img loading=\"lazy\" decoding=\"async\" width=\"649\" height=\"512\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/string_indexing_python.png\" alt=\"\" class=\"wp-image-2581\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/string_indexing_python.png 649w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/string_indexing_python-300x237.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/string_indexing_python-116x92.png 116w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/string_indexing_python-335x264.png 335w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/string_indexing_python-600x473.png 600w\" sizes=\"auto, (max-width: 649px) 100vw, 649px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Moving further, it&#8217;s important to consider that the output of our program should adhere to English language rules, which typically require the first letter of a proper noun (such as a name) to be in uppercase (capitalized). While a typical user might provide input in lowercase, our program should be able to correct such basic mistakes. To achieve this, Python provides string functions.<\/p>\n\n\n\n<p>For instance, to convert the first letter of a string to uppercase, we can use the &#8216;capitalize()&#8217; function. The typical syntax for using functions is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string_name.function_name(arguments_if_any)<\/code><\/pre>\n\n\n\n<p>In the case of the &#8216;capitalize()&#8217; function, it can be implemented for the input string as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>_name.capitalize()\n<\/code><\/pre>\n\n\n\n<p>Let\u2019s combine what we have learnt so far into a new program<span class=\"Apple-converted-space\">&nbsp; <\/span>\u2018<strong>strings_functions.py<\/strong>\u2019<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/string_functions_program_python.png\"><img loading=\"lazy\" decoding=\"async\" width=\"735\" height=\"625\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/string_functions_program_python.png\" alt=\"\" class=\"wp-image-2582\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/string_functions_program_python.png 735w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/string_functions_program_python-300x255.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/string_functions_program_python-335x285.png 335w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/string_functions_program_python-600x510.png 600w\" sizes=\"auto, (max-width: 735px) 100vw, 735px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>In the program, you&#8217;ll notice an interesting operation. After using the &#8216;capitalize()&#8217; function, we could have created a new variable to store the capitalized name<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>_name = input(\"Enter First Name: \")\n_capitalized_name = _name.capitalize()\n<\/code><\/pre>\n\n\n\n<p>However, creating too many intermediate variables can lead to confusion and is unnecessary if we have no further use for the original variable. Therefore, we use the technique of reassigning the variable to rewrite itself:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>_name = _name.capitalize()\n<\/code><\/pre>\n\n\n\n<p>This ability to rewrite the same variable with its modified value is extremely useful. It not only reduces the number of variables but also has broader applications, including creating loops and more, which we will explore in later chapters<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"vw-infobox\"><div class=\"vw-infobox-inner\"><h4 class=\"vw-infobox-title\"><span>Exercise<\/span><\/h4><div class=\"vw-infobox-content\"><\/p>\n<ul>\n<li style=\"text-align: left;\">Create a python program, which will ask the user to input \u2014 name, height, and weight, and give the below output<\/li>\n<\/ul>\n<p style=\"text-align: left;\">Issac H 140 cm W 50 kg<\/p>\n<ul style=\"text-align: left;\">\n<li>Create a python program prompts the user for a word, and prints out the number of letters in the word<\/li>\n<\/ul>\n<p style=\"text-align: left;\">Input \u2014 orange, resultant, narrow, booth, wisdom<\/p>\n<ul style=\"text-align: left;\">\n<li>Create a python program that prompts a user to enter his favorite football team, prints the following output<\/li>\n<\/ul>\n<p style=\"text-align: left;\">ARSENAL are a good football team<\/p>\n<ul style=\"text-align: left;\">\n<li>An automated program is used to create user names for students. The username will be the first three letters of the first name, followed by the last two letters of the last name.<\/li>\n<li>Modify previous program (no 4), such that, the first three letters of the username are in uppercase, and the last two are in lower case<\/li>\n<\/ul>\n<p style=\"text-align: left;\"><i>Hint: function to count the number of letters is variable name\u2022count( ). To convert all letters into upper case the function is variable name\u2022upper( ) and similarly for lowercase is variable name\u2022lower( )<\/i><\/p>\n<p><\/div><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><b>Lists<\/b><\/h2>\n\n\n\n<p>While the &#8216;string&#8217; data type exclusively handles characters, real-world applications often require interactions between different data types, such as student names, roll numbers, birth dates, exam grades, and attendance percentages. To handle this diversity of data types, we have &#8216;lists&#8217; in Python.<\/p>\n\n\n\n<p>Unlike &#8216;strings,&#8217; &#8216;lists&#8217; use square brackets [ ] to define their elements. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>_information = &#091;\"P L Deshpande\", 1919, \"November\", 8, \"Mumbai\"]<\/code><\/pre>\n\n\n\n<p>so if we look at index of _information, we will have<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><b>Index No<\/b><\/td><td><b>0<\/b><\/td><td><b>1<\/b><\/td><td><b>2<\/b><\/td><td><b>3<\/b><\/td><td><b>4<\/b><\/td><\/tr><tr><td>List<span class=\"Apple-converted-space\">&nbsp;<\/span><\/td><td>P L Deshpande<\/td><td>1919<\/td><td>November<\/td><td>8<\/td><td>Mumbai<\/td><\/tr><tr><td>Data type<\/td><td>String<\/td><td>Integer<\/td><td>String<\/td><td>Integer<\/td><td>String<\/td><\/tr><tr><td><b>Reverse<\/b><\/td><td><b>-5<\/b><\/td><td><b>-4<\/b><\/td><td><b>-3<\/b><\/td><td><b>-2<\/b><\/td><td><b>-1<\/b><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Similar to how we access portions of a string using indexing, we can access elements of a list in Python. For example, in the list &#8216;_information,&#8217; &#8216;_information[2]&#8217; would yield &#8216;November.&#8217;<\/p>\n\n\n\n<p>Now, using these concepts, let&#8217;s create a program that implements lists. We&#8217;ll call it &#8216;<strong>basic_list.py<\/strong>.&#8217; This program will help us understand how to work with lists in Python effectively.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/lists_basic_program_python.png\"><img loading=\"lazy\" decoding=\"async\" width=\"681\" height=\"707\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/lists_basic_program_python.png\" alt=\"\" class=\"wp-image-2583\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/lists_basic_program_python.png 681w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/lists_basic_program_python-289x300.png 289w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/lists_basic_program_python-323x335.png 323w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/lists_basic_program_python-600x623.png 600w\" sizes=\"auto, (max-width: 681px) 100vw, 681px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Much like strings, lists in Python come equipped with a variety of functions, including handy ones like <code>count()<\/code> and <code>append()<\/code>. We&#8217;ll explore these functions in greater detail as we advance in our programming journey.<\/p>\n\n\n\n<p>Moreover, in the chapters to come, we&#8217;ll delve into more complex ideas, such as lists within lists. This opens up exciting possibilities for organizing and structuring data.<\/p>\n\n\n\n<p>Think of lists as your virtual file cabinets, where you can systematically arrange different data types with specifically designated positions. This orderly and versatile approach allows you to efficiently store and manage a diverse array of information in your Python programs.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><b>Dictionaries<span class=\"Apple-converted-space\">&nbsp;<\/span><\/b><\/h2>\n\n\n\n<p>In our previous program, &#8216;basic_list.py,&#8217; prompting the user to enter the month in words can feel counter-intuitive. Once we ask the user to input the date as a number, logically, the month input should also be in numeric form.<\/p>\n\n\n\n<p>Analyzing this further, we have 12 months in a year, which constitutes a limited dataset. Therefore, we should be able to link a month to its corresponding number. This is where &#8216;dictionaries&#8217; come into play.<\/p>\n\n\n\n<p>Dictionaries in Python are defined using curly braces { }. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>_calendar = {1: \"January\", 2: \"February\"}<\/code><\/pre>\n\n\n\n<p>In this dictionary, the number &#8216;1&#8217; corresponds to &#8216;January,&#8217; and the number &#8216;2&#8217; corresponds to &#8216;February.&#8217;<\/p>\n\n\n\n<p>Dictionaries don&#8217;t have indexing in the traditional sense because each member has a corresponding member. To access a particular value, you only need to provide the key, and Python will return the corresponding value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>_calendar&#091;2]<\/code><\/pre>\n\n\n\n<p>This command will search for the value &#8216;2&#8217; in the dictionary and, when found, will retrieve the corresponding value, which, in this case, is &#8216;February.&#8217; Dictionaries are a powerful data structure for mapping one set of values to another in Python<\/p>\n\n\n\n<p>Let\u2019s understand this using an example \u2014 \u2018<strong>dictionary_basic.py<\/strong>\u2019<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"741\" height=\"757\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/basic_dictionary_program_with_output_python.png\" alt=\"\" class=\"wp-image-2584\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/basic_dictionary_program_with_output_python.png 741w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/basic_dictionary_program_with_output_python-294x300.png 294w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/basic_dictionary_program_with_output_python-328x335.png 328w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/08\/basic_dictionary_program_with_output_python-600x613.png 600w\" sizes=\"auto, (max-width: 741px) 100vw, 741px\" \/><\/figure>\n<\/div>\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>In the above program, we used a specific syntax, &#8216;*&#8217;<\/p>\n\n\n\n<p>The command to print the entire list is <code><strong>print(*list_name)<\/strong><\/code>. In the program, this syntax is used as <strong><code>print(*initialsname)<\/code>.<\/strong><\/p>\n\n\n\n<p>It&#8217;s essential to understand that a dictionary is a correspondence between any two data types. In the program, the integer &#8216;1&#8217; corresponds to the string &#8216;January&#8217;. However, correspondence can exist between any two data types. For instance:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>_information = {\"Name\": \"Hawking\", \"Birth Year\": 1942, \"Birth Month\": \"January\"}<\/code><\/pre>\n\n\n\n<p>In this dictionary, &#8220;Name&#8221; corresponds to &#8220;Hawking,&#8221; &#8220;Birth Year&#8221; corresponds to 1942, and &#8220;Birth Month&#8221; corresponds to &#8220;January.&#8221; So, if we call <code>_information[\"Birth Year\"]<\/code>, we will get the value 1942.<\/p>\n\n\n\n<p>It&#8217;s crucial to note that correspondence in dictionaries is one-way, from left to right. This means that if we search for &#8220;Birth Year,&#8221; we&#8217;ll get 1942. However, attempting a reverse search by looking for &#8220;1942&#8221; to find &#8220;Birth Year&#8221; will result in an error.&#8221;<\/p>\n\n\n<div class=\"vw-infobox\"><div class=\"vw-infobox-inner\"><h4 class=\"vw-infobox-title\"><span>Exercise<\/span><\/h4><div class=\"vw-infobox-content\"><\/p>\n<ul>\n<li>Create a directory data named &#8216;_information&#8217; for a student, which stores the student&#8217;s roll number, student&#8217;s name, and grade level. Prompt for inputs and get the following output<\/li>\n<\/ul>\n<p>Racheal Roll No 14 Grade A<\/p>\n<ul>\n<li>Create a directory of roll numbers for the following students, 1-Newton, 2-Galileo, 3-Planck, 4-Heisenberg, 5-Bohr. And then when the user inputs a roll number, the name of the student is returned<\/li>\n<\/ul>\n<p>Enter roll no5 Student name is Bohr<\/p>\n<ul>\n<li>Create a program to prompt for the First name and last name of a person. Using this data create the following information points, User Name = First three letters of first name + Last two letters of the last name, Password = First letter of the last name in upper case + count of letters in the name + last letter of first name + count of the letters of the last name. The following output is desired for the name &#8216;Albert Einstein&#8217;<\/li>\n<\/ul>\n<p>Enter Name: Albert Username is Albin<\/p>\n<ul>\n<li>Create a python program that can store name, account number, and account balance, and when the user inputs the account number, it reveals the account balance.<\/li>\n<\/ul>\n<p>Enter Account Number1409 Account Balance is<span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 <\/span>23000<\/p>\n<ul>\n<li>In the previous program, input two account details (Einstein, Acc no 1409, AccBalance 23000) and (Newton, Acc no 1211, Acc Balance 9500). Use the essence of exercise problem 3 to create unique user names and passwords for each of them. Then create a program that asks for the user name, and it gives out the account number.<\/li>\n<\/ul>\n<p><\/div><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>In this chapter, we explored the fundamental concepts of Python&#8217;s string data type and introduced the idea of string indexing. We learned that when you input data using the &#8216;input&#8217; command, it&#8217;s stored as a collection of characters within a string. Each character in the string is assigned a unique position number, starting with zero at the leftmost character and increasing as you move rightward. We delved into string indexing, which allows us to interact with individual characters within a string, and slicing, which enables us to extract specific portions of a string.<\/p>\n\n\n\n<p>We also discussed string operations such as concatenation using the &#8216;+&#8217; operator. It&#8217;s essential to note that strings are case-sensitive, and the default data type for &#8216;input&#8217; is a string. To adhere to English language rules, Python provides string functions like &#8216;capitalize()&#8217; to convert the first letter of a string to uppercase.<\/p>\n\n\n\n<p>The chapter presented several exercises to practice these concepts, which included creating programs to format and process user input, working with string indices, and utilizing string functions.<\/p>\n\n\n\n<p>We also touched on the use of lists, which are essential for handling different data types and organizing information. Lists allow you to store and manage diverse data in an orderly manner, just like a virtual file cabinet.<\/p>\n\n\n\n<p>Lastly, we introduced dictionaries as a way to create correspondences between different data types. Dictionaries are powerful for mapping one set of values to another, allowing you to create meaningful relationships in your Python programs.<\/p>\n\n\n\n<p>Throughout the chapter, you had the opportunity to practice and apply these concepts through exercises that gradually increased in complexity. These exercises provided hands-on experience in working with strings, lists, and dictionaries, and they set the stage for more advanced programming concepts in the chapters to come.<\/p>\n\n\n\n<p><a class='vw-button vw-button--purple ' href='https:\/\/idiagress.com\/vinaaypatil\/2020\/08\/09\/1-3-linear-equations-using-python-algorithms\/' target='_self'>Prev Chapter<\/a> &nbsp; &nbsp;<a class='vw-button vw-button-- ' href='https:\/\/idiagress.com\/vinaaypatil\/first-steps-in-python\/' target='_self'>Course Homepage<\/a> &nbsp; <a class='vw-button vw-button--pink  vw-button--arrow' href='https:\/\/idiagress.com\/vinaaypatil\/?p=2599&#038;preview=true' target='_self'>Next Chapter<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Python, the default data type for the &#8216;input&#8217; command is a string. A string is essentially a collection of characters, which can include letters, symbols, numbers, and more. It&#8217;s important to note that when you input numbers using the &#8216;input&#8217; command, they are stored as characters within a string. This means that you cannot &#8230;<\/p>\n","protected":false},"author":2,"featured_media":2762,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[412],"tags":[414,416,415,413,408],"class_list":["post-2578","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-first-steps-in-python","tag-directories-in-python","tag-functions-in-python","tag-indexing-in-python","tag-lists-in-python","tag-strings-in-python"],"_links":{"self":[{"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/posts\/2578","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/comments?post=2578"}],"version-history":[{"count":5,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/posts\/2578\/revisions"}],"predecessor-version":[{"id":3732,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/posts\/2578\/revisions\/3732"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/media\/2762"}],"wp:attachment":[{"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/media?parent=2578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/categories?post=2578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/tags?post=2578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}