{"id":8162,"date":"2021-01-29T18:00:11","date_gmt":"2021-01-29T12:30:11","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=8162"},"modified":"2021-02-16T15:28:43","modified_gmt":"2021-02-16T09:58:43","slug":"python-variable-types-and-assignment","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/python-variable-types-and-assignment\/","title":{"rendered":"Python Variable Types and Assignment"},"content":{"rendered":"\n<p>Variables are a key concept in Python. They can be seen as containers that store data in Python. Variables can be defined in a more pythonic way, as a location in memory that feeds in data for the python interpreter to process.&nbsp;<\/p>\n\n\n\n<p>As an illustration, if a book is kept on a shelf, the book is the data while the shelf is the variable.<\/p>\n\n\n\n<p>In this tutorial, you will learn:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>What variables are in Python?<\/li><li>How to assign a variable.<\/li><li>How to concatenate a variable to a string.<\/li><li>The variable types in Python.<\/li><li>What global and local variables are in Python.<\/li><li>How to delete a variable in Python.<\/li><\/ul>\n\n\n\n<p>Let\u2019s jump into it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Variable Names in Python<\/h2>\n\n\n\n<p>The variable name is basically hinged on the data type it stores. There are different kinds of data types in Python including integers, floats, strings, dictionaries, lists, tuples, sets, etc. The variables that store these data types are most likely letters. It is good practice to ensure that your variable name is as descriptive as possible.<\/p>\n\n\n\n<p>Rather than using x to store a list of car names, you can rather name the variable as car_names. Next, let us see how to assign variable in Python<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Variable Assignment in Python<\/h2>\n\n\n\n<p>When you wish to assign a variable to a data type, you use the equal to sign (=). As a matter of fact, a single equal to sign is called the assignment operator in Python.<\/p>\n\n\n\n<p>Let\u2019s say we wish to name the name of students in a class.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">student_names = ['Mosh', 'Vijay', 'Tobi', 'Steph']student_names = ['Mosh', 'Vijay', 'Tobi', 'Steph']\n<\/pre>\n\n\n\n<p>Observe the variable name that was declared, student_names. Variable names should always be lowercase. In situations where you would require more than one word, the words should be separated with an underscore.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Redeclare a Variable in Python<\/h2>\n\n\n\n<p>Python is an interpreter and not a compiler. This implies that the codes are usually read line by line. If you have assigned a variable to some data type, you can reassign the variable to another data or data type in a different line. When python sees this, it overwrites the previous assignment and uses the latest date\/data type. In the bookshelf illustration, it is as though the books on the shelf were changed to something else. The same bookshelf but different data. Let\u2019s see an example.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#assign variable names\nstudent_names = ['Mosh', 'Vijay', 'Tobi', 'Steph']\n#reassign variable names\nstudent_names = ('Phil', 'Rahul', 'Janet', 'Mo')\n&nbsp;\nprint(student_names)<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p><code>('Phil', 'Rahul', 'Janet', 'Mo')<\/code><\/p>\n\n\n\n<p>As seen, the variable has been reassigned.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">String Concatenation with a Variable in Python<\/h2>\n\n\n\n<p>You already know that you can print a declared variable to the terminal using the print function. In addition to that, you can concatenate a variable to a string and print the joint result on the terminal.<\/p>\n\n\n\n<p>There is one caveat, however. The declared variable must be as a string. If you attempt to convert a string to an integer, it throws an error. See an example below<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">number = 2\nprint('The declared variable was ' + number)<\/pre>\n\n\n\n<p>Output:\n<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><img fetchpriority=\"high\" decoding=\"async\" width=\"528\" height=\"246\" src=\"https:\/\/lh5.googleusercontent.com\/-uVgNkMU-JnGvL3clRrcOa-ZIO2oCaQLDfj7KuFwTEKoUiq5rrVVczchM_vyHOVXtL2cDqKqW1C-itt30yTb7r3oRnHmNlU8ADME21sooHV446dKjqpN4M5vyptpr1mutlJm-CM\" alt=\"\" title=\"\"><\/pre>\n\n\n\n<p>So how do we fix this? We must typecast the variable to a string to concatenate a variable to a string. See the example below.<\/p>\n\n\n\n<p>#typecast the integer to a string<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">number = str(2)\nprint('The declared variable was ' + number)<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p><code>The declared variable was 2<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Two Types of Variables in Python<\/h2>\n\n\n\n<p>Variables in Python have two broad classifications.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Global variable<\/li><li>Local variable<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Global variables<\/h3>\n\n\n\n<p>Global variables are variables defined in a program but not within a function. They are variables that remain in force anywhere in the program, including inside a function. We\u2019d see a coding example momentarily. For now, let&#8217;s understand what a local variable is.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Local Variables<\/h3>\n\n\n\n<p>Local variables are variables that are declared inside a function. It is important to point out that a local variable is only in force when the function is called. If you attempt to print a variable inside a function, without calling the function, it would return a \u2018NameError: the variable is not defined\u2019.<\/p>\n\n\n\n<p>Lets see an example<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#declare a global variable\nprint(variable_1)\ndef a_function():\n\u00a0\u00a0\u00a0 print('Printing the global variable inside the function: ', end='')\n\u00a0\u00a0\u00a0 print(variable_1)\n\u00a0\u00a0\u00a0 #declare a local variable\n\u00a0\u00a0\u00a0 variable_2 = 'infosys'\n\u00a0\u00a0\u00a0 print('Printing the local variable inside the function: ', end='')\n\u00a0\u00a0\u00a0 print(variable_2)\nprint('Printing the global variable: ', end='')\nprint('Printing the local variable outside the function: ', end='')\nprint(variable_2)<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Let\u2019s unpack what\u2019s going on.<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>We defined a global variable variable_1<\/li><li>We printed it on the terminal<\/li><li>We defined a function a_function()<\/li><li>We printed the printed variable_1<\/li><li>We defined another variable, variable_2 in the function. Since variable_2 is defined in a function, it is a local variable.<\/li><li>We printed variable inside the function<\/li><li>Finally, we printed variable_2 outside the function. Since variable_2 is a local function, we do not expect it to hold effect outside the function. Hence, this step should return a \u2018variable_2 not found error\u2019.<\/li><\/ul>\n\n\n\n<p>Let\u2019s run the program.<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/8rfnbUodIs_ZmmRIvTudGT7gophZk5l_nHTuNslOHWZc_lAkmxJC14iJuYqHcjCfqEPB2f9KuSoDCVuujPZCi6F7xXGjg4F7SEfhcEmVzuqS_LnvvwfnbY-7eUAcdwdCvCJna6k\" width=\"624\" height=\"108\" alt=\"\" title=\"\"><\/p>\n\n\n\n<p>As expected, it throws an error.<\/p>\n\n\n\n<p>The second scenario would be to call the function and see if variable_1 (which was not defined inside the function) will be printed.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#declare a global variable\nvariable_1 = 'H2k'\nprint('Printing the global variable: ', end='')\nprint(variable_1)\ndef a_function():\n\u00a0\u00a0\u00a0 print('Printing the global variable inside the function: ', end='')\n\u00a0\u00a0\u00a0 print(variable_1)\n\u00a0\u00a0\u00a0 #declare a local variable\n\u00a0\u00a0\u00a0 variable_2 = 'infosys'\n\u00a0\u00a0\u00a0 print('Printing the local variable inside the function: ', end='')\n\u00a0\u00a0\u00a0 print(variable_2)\n#call the function\na_function()<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/FAAPPEb4j1dtT226Hm7S63o7SIlnsEAgDk8Q3kiWpCrcZeKmAbbt6kRAF4jGcFBggBGIDnPLS_zX07R-FrfQ9BlHjYBYiUs3saFRolbD4lzhTIRXMjGgz5mJjyh-Y6TM9MGREAY\" width=\"605\" height=\"91\" alt=\"\" title=\"\"><\/p>\n\n\n\n<p>As seen, the variable_1 (the global variable) was printed in the function. Even though it was defined outside the function, it can still be called in the function. This is why it is called a global function<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Deleting a Variable<\/h2>\n\n\n\n<p>In python, you can delete a variable using the del statement. Whether local or global, the <strong>del <\/strong>statement deletes the variable from the memory of the interpreter. Check this out.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#declare a variable\nvariable_1 = 'H2k'\n#delete the variable\ndel variable_1\nprint(variable_1)<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/i6MNt9ZjjTzrsDGduhCA4WU3CRDNpvthQAIkYLUyZVT7XRbAHVs-M-U33rB6V3uU1-nkqfchsvHRQR9Ng6Bmp22h9i7K6jQSSjLMutbzJbD6A_8VQYpiRtP9rkNM-LZynzrly4k\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p>As seen, the variable has been deleted from memory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">In summary<\/h2>\n\n\n\n<p>In this tutorial, you have learned what a variable is and the two types of variables in Python. You learned that a global variable is declared anywhere in the program but not inside a function. It thus holds true anywhere it is called, even inside a function.<\/p>\n\n\n\n<p>In contrast, a local variable is called inside a function and can only hold true when the function is called. Lastly, you have discovered how to delete a variable using the del statement in Python.<\/p>\n\n\n\n<p>If you have any questions, please leave them in the comment section and I\u2019d do my best to answer them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Variables are a key concept in Python. They can be seen as containers that store data in Python. Variables can be defined in a more pythonic way, as a location in memory that feeds in data for the python interpreter to process.&nbsp; As an illustration, if a book is kept on a shelf, the book [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8168,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[342],"tags":[],"class_list":["post-8162","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8162","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=8162"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8162\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/8168"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=8162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=8162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=8162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}