{"id":14239,"date":"2021-02-16T16:15:16","date_gmt":"2021-02-16T10:30:16","guid":{"rendered":"http:\/\/206.189.201.21\/?p=14239"},"modified":"2021-10-04T07:41:55","modified_gmt":"2021-10-04T01:56:55","slug":"variables-in-python","status":"publish","type":"post","link":"https:\/\/www.theclickreader.com\/variables-in-python\/","title":{"rendered":"Variables in Python"},"content":{"rendered":"\n\n\n\n\n<p>Variables are the building blocks of a Python program and we will be using them a lot. Therefore, let us discuss them in-depth in this lesson.<\/p>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Python Variable?<\/h2>\n\n\n\n<p>A variable in Python is a symbolic name that points to the location where data is stored in memory. Variables are created or declared whenever a value is assigned to a character or sequence of characters in Python.<\/p>\n\n\n\n<p>In the example below, the value 10 is being pointed by the variable called <code>val_num<\/code>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Declaring the variable called val_num with the value 10\nval_num = 10\n\n# Printing the value pointed by the variable called val_num\nprint(val_num)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">OUTPUT:\n10<\/pre>\n\n\n\n<p>Since the variable val_num is just a container pointing to where the data is stored in memory, the variable can be assigned to another variable as well. This passes the reference of the value to the assigned variable as well.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Assigning the variable val_num with the value 10\nval_num = 10\n\n# Assigning the variable sec_val_num with the value 10\nsec_val_num = val_num\n\n# Printing the value pointed by the variable sec_val_num\nprint(sec_val_num)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">OUTPUT:\n10<\/pre>\n\n\n\n<p>We can also change which value a variable points to as show in the example below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Assigning the variable val_num with the value 10\nval_num = 10\n\n# Assigning the variable val_num with the value 20\nval_num = 20\n\n# Printing the value pointed by the variable val_num\nprint(val_num)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">OUTPUT:\n20<\/pre>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Best practices and rules for naming a variable in Python<\/h2>\n\n\n\n<p>In order to properly name a variable in Python, we must follow a set of rules like in any other programming language. The rules and best practices are as follows:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Variables names should only consist of a combination of lowercase letters (a-z) or uppercase letters (A-Z) or digits (0-9) or an underscore (_). <\/li><li>A meaningful word should be used as a variable name. For example, <code>age<\/code> is a meaningful variable name that explains that the variable holds a reference to an age value.<\/li><li>Variable names should not start with a digit.<\/li><li>Variable names consisting of two or more words should have an underscore (_) in between them. For example, <code>date_of_birth<\/code> is a variable name that holds a reference to a date of birth value.<\/li><li>Variable names should not use special symbols other than an underscore (_). <\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Keywords in Python<\/h2>\n\n\n\n<p>Python keywords are case-sensitive words in Python that have a Python-specific meaning. Such keywords cannot be used as variable names and are reserved by the Python programming language.<\/p>\n\n\n\n<p>Since keywords define the syntax and structure of Python, they are large in number and remembering all of them is very hard when starting out. The correct way to memorize such keywords is by actually using them in Python and we will be doing so throughout the course. But, for now, here are some example Python keywords along with their meaning:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Python Keyword<\/strong><\/td><td><strong>Meaning<\/strong><\/td><\/tr><tr><td>def<\/td><td>Used for creating a function in Python<\/td><\/tr><tr><td>global<\/td><td>Used for declaring a global variable in Python<\/td><\/tr><tr><td>class<\/td><td>Used for creating a class in Python<\/td><\/tr><tr><td>import<\/td><td>Used for importing libraries in Python<\/td><\/tr><tr><td>True<\/td><td>Used for denoting a boolean<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Again, you do not have to worry what the above keywords mean right now. We will cover them throughout this course.<\/p>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Constant in Python?<\/h2>\n\n\n\n<p>A constant in Python is a variable whose value is not meant to be changed. The general practice for declaring a constant is by using uppercase letters for the variable name.<\/p>\n\n\n\n<p>In the example below, PI is a constant whose value is not meant to be changed.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Declaring a constant called PI with the value 3.14\nPI = 3.14\n\n# Printing the value pointed by the constant called PI\nprint(PI)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">OUTPUT:\n3.14<\/pre>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<p>Now you know the most basic concepts related to variables in Python. We will build on these concepts starting from the next lesson of this course.<\/p>\n\n\n\n<div class=\"tm-button-wrapper\" style=\"float:right\">\n<a href=\"\/data-types-and-operations-in-python\/\" class=\"tm-button-link tm-button style-flat tm-button-nm icon-right navigation-btn-lesson\" role=\"button\" id=\"main-button\" style=\"text-align:center;\">\n\n<div class=\"button-content-wrapper\">\n\n<span class=\"button-text\" style=\"color:white\">Next Lesson<\/span>\n<span class=\"button-icon\" style=\"color:white\"> <i class=\"far fa-arrow-circle-right\"><\/i> <\/span>\n\n  <\/div>\n  <\/a>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Variables are the building blocks of a Python program and we will be using them a lot. Therefore, let us discuss them in-depth in this lesson. What is a Python Variable? A variable in Python is a symbolic name that points to the location where data is stored in memory. Variables are created or declared [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":14304,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[105],"tags":[],"class_list":["post-14239","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-programming-for-newbies"],"_links":{"self":[{"href":"https:\/\/www.theclickreader.com\/wp-json\/wp\/v2\/posts\/14239","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.theclickreader.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.theclickreader.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.theclickreader.com\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.theclickreader.com\/wp-json\/wp\/v2\/comments?post=14239"}],"version-history":[{"count":0,"href":"https:\/\/www.theclickreader.com\/wp-json\/wp\/v2\/posts\/14239\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.theclickreader.com\/wp-json\/wp\/v2\/media\/14304"}],"wp:attachment":[{"href":"https:\/\/www.theclickreader.com\/wp-json\/wp\/v2\/media?parent=14239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.theclickreader.com\/wp-json\/wp\/v2\/categories?post=14239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.theclickreader.com\/wp-json\/wp\/v2\/tags?post=14239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}