{"id":7721,"date":"2021-02-01T19:10:45","date_gmt":"2021-02-01T13:40:45","guid":{"rendered":"https:\/\/pynative.com\/?p=7721"},"modified":"2021-08-31T10:52:35","modified_gmt":"2021-08-31T05:22:35","slug":"python-variables","status":"publish","type":"post","link":"https:\/\/pynative.com\/python-variables\/","title":{"rendered":"Python Variables"},"content":{"rendered":"\n<p>A <strong>variable is a reserved memory area (memory address) to store value<\/strong>. For example, we want to store an employee&#8217;s salary. In such a case, we can create a variable and store salary using it. Using that variable name, you can read or modify the salary amount.<\/p>\n\n\n\n<p>In other words, a variable is a value that varies according to the condition or input pass to the program. Everything in Python is treated as an object so every variable is nothing but an object in Python.<\/p>\n\n\n\n<p>A variable can be either <strong>mutable<\/strong> or <strong>immutable<\/strong>.&nbsp;If the variable&#8217;s value can change, the object is called mutable, while if the value cannot change, the object is called immutable. We will learn the difference between mutable and immutable types in the later section of this article.<\/p>\n\n\n\n<div class=\"wp-block-columns has-background is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\" style=\"background-color:#dfecf4\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong>Also, Solve<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/pynative.com\/python-variables-and-data-types-quiz\/\">Python variables and data type Quiz<\/a><\/li><li><a href=\"https:\/\/pynative.com\/python-basic-exercise-for-beginners\/\">Basic Python exercise for beginners<\/a><\/li><\/ul>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-yoast-seo-table-of-contents yoast-table-of-contents\"><h2>Table of contents<\/h2><ul><li><a href=\"#h-creating-a-variable\" data-level=\"2\">Creating a variable<\/a><\/li><li><a href=\"#h-changing-the-value-of-a-variable\" data-level=\"2\">Changing the value of a variable<\/a><\/li><li><a href=\"#h-create-number-string-list-variables\" data-level=\"2\">Create Number, String, List variables<\/a><ul><li><a href=\"#h-number\" data-level=\"3\">Number<\/a><ul><li><a href=\"#h-integer-variable\" data-level=\"4\">Integer variable<\/a><\/li><li><a href=\"#h-float-variable\" data-level=\"4\">Float variable<\/a><\/li><li><a href=\"#h-complex-type\" data-level=\"4\">Complex type<\/a><\/li><\/ul><\/li><li><a href=\"#h-string-variable\" data-level=\"3\">String variable<\/a><\/li><li><a href=\"#h-list-type-variable\" data-level=\"3\">List type variable<\/a><\/li><\/ul><\/li><li><a href=\"#h-get-the-data-type-of-variable\" data-level=\"2\">Get the data type of variable<\/a><\/li><li><a href=\"#h-delete-a-variable\" data-level=\"2\">Delete a variable<\/a><\/li><li><a href=\"#h-variable-s-case-sensitive\" data-level=\"2\">Variable&#8217;s case-sensitive<\/a><\/li><li><a href=\"#h-constant\" data-level=\"2\">Constant<\/a><ul><li><a href=\"#h-assigning-a-value-to-a-constant-in-python\" data-level=\"3\">Assigning a value to a constant in Python<\/a><\/li><\/ul><\/li><li><a href=\"#h-rules-and-naming-convention-for-variables-and-constants\" data-level=\"2\">Rules and naming convention for variables and constants<\/a><\/li><li><a href=\"#h-multiple-assignments\" data-level=\"2\">Multiple assignments<\/a><ul><li><a href=\"#h-assigning-a-single-value-to-multiple-variables\" data-level=\"3\">Assigning a single value to multiple variables<\/a><\/li><li><a href=\"#h-assigning-multiple-values-to-multiple-variables\" data-level=\"3\">Assigning multiple values to multiple variables<\/a><\/li><\/ul><\/li><li><a href=\"#h-variable-scope\" data-level=\"2\">Variable scope<\/a><ul><li><a href=\"#h-local-variable\" data-level=\"3\">Local variable<\/a><\/li><li><a href=\"#h-global-variable\" data-level=\"3\">Global variable<\/a><\/li><\/ul><\/li><li><a href=\"#h-object-variable-identity-and-references\" data-level=\"2\">Object\/Variable identity and references<\/a><ul><li><a href=\"#h-object-reference\" data-level=\"3\">Object Reference<\/a><\/li><\/ul><\/li><li><a href=\"#h-unpack-a-collection-into-a-variable\" data-level=\"2\">Unpack a collection into a variable<\/a><\/li><\/ul><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-creating-a-variable\">Creating a variable<\/h2>\n\n\n\n<p>Python programming language is dynamically typed, so there is no need to declare a variable before using it or declare the <a href=\"https:\/\/pynative.com\/python-data-types\/\">data type<\/a> of variable like in other programming languages. The declaration happens automatically when we assign a value to the variable.<\/p>\n\n\n\n<p><strong>Creating a variable and assigning a value<\/strong><\/p>\n\n\n\n<p>We can assign a value to the variable at that time variable is created. We can use the assignment operator <code>=<\/code> to assign a value to a variable.<\/p>\n\n\n\n<p>The operand, which is on the left side of the assignment operator, is a variable name. And the operand, which is the right side of the assignment operator, is the variable&#8217;s value.<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">&lt;code&gt;variable_name = variable_value&lt;\/code&gt;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code1\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">name = <span class=\"hljs-string\">\"John\"<\/span>  <span class=\"hljs-comment\"># string assignment<\/span>\nage = <span class=\"hljs-number\">25<\/span>  <span class=\"hljs-comment\"># integer assignment<\/span>\nsalary = <span class=\"hljs-number\">25800.60<\/span>  <span class=\"hljs-comment\"># float assignment<\/span>\n\nprint(name)  <span class=\"hljs-comment\"># John<\/span>\nprint(age)  <span class=\"hljs-comment\"># 25<\/span>\nprint(salary)  <span class=\"hljs-comment\"># 25800.6<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code1', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code1');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>In the above example, &#8220;John&#8221;, 25, 25800.60 are values that are assigned to <code>name<\/code>, <code>age<\/code>, and <code>salary<\/code> respectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-changing-the-value-of-a-variable\">Changing the value of a variable<\/h2>\n\n\n\n<p>Many programming languages are statically typed languages where the variable is initially declared with a specific type, and during its lifetime, it must always have that type.<\/p>\n\n\n\n<p>But in Python, variables are <strong>dynamically typed<\/strong>&nbsp;and not subject to the <strong>data type<\/strong> restriction. A variable may be assigned to a value of&nbsp;<strong>one type<\/strong>, and then later, we can also re-assigned a value of a <strong>different type<\/strong>. Let&#8217;s see the example.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code2\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">var = <span class=\"hljs-number\">10<\/span>\nprint(var)  <span class=\"hljs-comment\"># 10<\/span>\n<span class=\"hljs-comment\"># print its type<\/span>\nprint(type(var))  <span class=\"hljs-comment\"># &lt;class 'int'&gt;<\/span>\n\n<span class=\"hljs-comment\"># assign different integer value to var<\/span>\nvar = <span class=\"hljs-number\">55<\/span>\nprint(var)  <span class=\"hljs-comment\"># 55<\/span>\n\n<span class=\"hljs-comment\"># change var to string<\/span>\nvar = <span class=\"hljs-string\">\"Now I'm a string\"<\/span>\nprint(var)  <span class=\"hljs-comment\"># Now I'm a string<\/span>\n<span class=\"hljs-comment\"># print its type<\/span>\nprint(type(var))  <span class=\"hljs-comment\"># &lt;class 'str'&gt;<\/span>\n\n<span class=\"hljs-comment\"># change var to float<\/span>\nvar = <span class=\"hljs-number\">35.69<\/span>\nprint(var)  <span class=\"hljs-comment\"># 35.69<\/span>\n<span class=\"hljs-comment\"># print its type<\/span>\nprint(type(var))  <span class=\"hljs-comment\"># &lt;class 'float'&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code2', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code2');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-create-number-string-list-variables\">Create Number, String, List variables<\/h2>\n\n\n\n<p>We can create different types of variables as per our requirements. Let&#8217;s see each one by one.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-number\">Number<\/h3>\n\n\n\n<p>A number is a <a href=\"https:\/\/pynative.com\/python-data-types\/\">data type<\/a> to store numeric values. The object for the number will be created when we assign a value to the variable. In Python3, we can use the following three data types to store numeric values.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Int<\/li><li>float<\/li><li>complex<\/li><\/ol>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-integer-variable\">Integer variable<\/h4>\n\n\n\n<p>The&nbsp;<code>int<\/code> is a data type that returns integer type values (signed integers); they are also called&nbsp;<strong>ints<\/strong> or <strong>integers<\/strong>.&nbsp;The integer value can be&nbsp;positive&nbsp;or&nbsp;negative&nbsp;without a decimal point.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code3\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># create integer variable<\/span>\nage = <span class=\"hljs-number\">28<\/span>\nprint(age)  <span class=\"hljs-comment\"># 28<\/span>\nprint(type(age))  <span class=\"hljs-comment\"># &lt;class 'int'&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code3', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code3');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Note<\/strong>:&nbsp;We used the built-in Python method <code>type()<\/code> to check the variable type.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-float-variable\">Float variable<\/h4>\n\n\n\n<p>Floats are the values with the <strong>decimal point<\/strong> dividing the integer and the fractional parts of the number.&nbsp; Use float data type to store decimal values.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code4\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># create float variable<\/span>\nsalary = <span class=\"hljs-number\">10800.55<\/span>\nprint(salary)  <span class=\"hljs-comment\"># 10800.55<\/span>\nprint(type(salary))  <span class=\"hljs-comment\"># &lt;class 'float'&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code4', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code4');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>In the above example, the variable salary assigned to value 10800.55, which is a <code>float<\/code> value.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-complex-type\">Complex type<\/h4>\n\n\n\n<p>The <code>complex<\/code> is the numbers that come with the real and imaginary part. A complex number is in the form of a+bj, where a and b contain integers or floating-point values.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code5\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">a = <span class=\"hljs-number\">3<\/span> + <span class=\"hljs-number\">5j<\/span>\nprint(a)  <span class=\"hljs-comment\"># (3+5j)<\/span>\nprint(type(a))  <span class=\"hljs-comment\"># &lt;class 'complex'&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code5', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code5');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-string-variable\"><strong>String variable<\/strong><\/h3>\n\n\n\n<p>In Python, a string is a<strong> set of characters<\/strong>&nbsp;represented in quotation marks. Python allows us to define a string in either pair of&nbsp;<strong>single<\/strong>&nbsp;or<strong>&nbsp;double<\/strong> quotation marks. For example, to store a person&#8217;s name we can use a string type.<\/p>\n\n\n\n<p>To retrieve a piece of string from a given string, we can use to slice operator <code>[]<\/code> or <code>[:]<\/code>. Slicing provides us the subset of a string with an index starting from index 0 to index end-1.<\/p>\n\n\n\n<p>To concatenate the string, we can use&nbsp;<strong>the addition<code>(+)<\/code><\/strong> operator.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code6\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># create a variable of type string<\/span>\nstr = <span class=\"hljs-string\">'PYnative'<\/span>\n<span class=\"hljs-comment\"># prints complete string<\/span>\nprint(str)  <span class=\"hljs-comment\"># PYnative<\/span>\n\n<span class=\"hljs-comment\"># prints first character of the string<\/span>\nprint(str&#91;<span class=\"hljs-number\">0<\/span>])  <span class=\"hljs-comment\"># P<\/span>\n\n<span class=\"hljs-comment\"># prints characters starting from 2nd to 5th<\/span>\nprint(str&#91;<span class=\"hljs-number\">2<\/span>:<span class=\"hljs-number\">5<\/span>])  <span class=\"hljs-comment\"># nat<\/span>\n\n<span class=\"hljs-comment\"># length of string<\/span>\nprint(len(str))  <span class=\"hljs-comment\"># 8<\/span>\n\n<span class=\"hljs-comment\"># concatenate string<\/span>\nprint(str + <span class=\"hljs-string\">\"TEST\"<\/span>)  <span class=\"hljs-comment\"># PYnativeTEST<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code6', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code6');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-list-type-variable\">List type variable<\/h3>\n\n\n\n<p>If we want to represent&nbsp;<strong>a group of elements <\/strong>(or value) as a single entity, we should go for the list variable type. For example, we can use them to store student names. In the list, the insertion order of elements is preserved. That means, in which order elements are inserted in the list, the order will be intact.<\/p>\n\n\n\n<p><strong>Read<\/strong>: Complete Guide on <a href=\"https:\/\/pynative.com\/python-lists\/\">Python lists<\/a><\/p>\n\n\n\n<p>The list can be accessed in two ways, either positive or negative index.&nbsp; The list has the following characteristics:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>In the list&nbsp;insertion order of elements&nbsp;is preserved.<\/li><li>Heterogeneous (all types of data types <code>int<\/code>, <code>float<\/code>, <code>string<\/code>) elements are allowed.<\/li><li>Duplicates elements are permitted.<\/li><li>The list is&nbsp;mutable(can change).<\/li><li>Growable in nature means based on our requirement, we can increase or decrease the list&#8217;s size.<\/li><li>List elements should be enclosed within&nbsp;square brackets <code>[]<\/code>.<\/li><\/ol>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code7\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># create list<\/span>\nmy_list = &#91;<span class=\"hljs-string\">'Jessa'<\/span>, <span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-string\">'Kelly'<\/span>, <span class=\"hljs-number\">50<\/span>, <span class=\"hljs-number\">10.5<\/span>]\n<span class=\"hljs-comment\"># print entire list<\/span>\nprint(my_list)  <span class=\"hljs-comment\"># &#91;'Jessa', 10, 20, 'Kelly', 50, 10.5]<\/span>\n\n<span class=\"hljs-comment\"># Accessing 1st element of a list<\/span>\nprint(my_list&#91;<span class=\"hljs-number\">0<\/span>])  <span class=\"hljs-comment\"># 'Jessa'<\/span>\n\n<span class=\"hljs-comment\"># Accessing  last element of a<\/span>\nprint(my_list&#91;<span class=\"hljs-number\">-1<\/span>])  <span class=\"hljs-comment\"># 10.5<\/span>\n\n<span class=\"hljs-comment\"># access chunks of list data<\/span>\nprint(my_list&#91;<span class=\"hljs-number\">1<\/span>:<span class=\"hljs-number\">3<\/span>])  <span class=\"hljs-comment\"># &#91;10, 20]<\/span>\n\n<span class=\"hljs-comment\"># Modifying first element of a list<\/span>\nmy_list&#91;<span class=\"hljs-number\">0<\/span>] = <span class=\"hljs-string\">'Emma'<\/span>\nprint(my_list&#91;<span class=\"hljs-number\">0<\/span>])  <span class=\"hljs-comment\"># 'Emma'<\/span>\n\n<span class=\"hljs-comment\"># add one more elements to list<\/span>\nmy_list.append(<span class=\"hljs-number\">100<\/span>)\nprint(my_list)  <span class=\"hljs-comment\"># &#91;'Emma', 10, 20, 'Kelly', 50, 10.5, 100]<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code7', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code7');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-get-the-data-type-of-variable\">Get the data type of variable<\/h2>\n\n\n\n<p>No matter what is stored in a variable (object), a variable can be any type like <code>int<\/code>, <code>float<\/code>, <code>str<\/code>, <code>list<\/code>, <code>tuple<\/code>, <code>dict<\/code>, etc. There is a built-in function called <code>type()<\/code> to get the data type of any variable.<\/p>\n\n\n\n<p>The <code>type()<\/code> function has a simple and straightforward syntax.<\/p>\n\n\n\n<p><strong>Syntax of <code>type()<\/code> :<\/strong><\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">type(&lt;variable_name&gt;)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code8\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">a = <span class=\"hljs-number\">100<\/span>\nprint(type(a))  <span class=\"hljs-comment\"># class 'int'<\/span>\n\nb = <span class=\"hljs-number\">100.568<\/span>\nprint(type(b))  <span class=\"hljs-comment\"># class 'float'<\/span>\n\nstr1 = <span class=\"hljs-string\">\"PYnative\"<\/span>\nprint(type(str1))  <span class=\"hljs-comment\"># class 'str'<\/span>\n\nmy_list = &#91;<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">20.5<\/span>, <span class=\"hljs-number\">100<\/span>]\nprint(type(my_list))  <span class=\"hljs-comment\"># class 'list'<\/span>\n\nmy_set = {<span class=\"hljs-string\">'Emma'<\/span>, <span class=\"hljs-string\">'Jessa'<\/span>, <span class=\"hljs-string\">'Kelly'<\/span>}\nprint(type(my_set))  <span class=\"hljs-comment\"># class 'set'<\/span>\n\nmy_tuple = (<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">15<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">25<\/span>)\nprint(type(my_tuple))  <span class=\"hljs-comment\"># class 'tuple'<\/span>\n\nmy_dict = {<span class=\"hljs-number\">1<\/span>: <span class=\"hljs-string\">'William'<\/span>, <span class=\"hljs-number\">2<\/span>: <span class=\"hljs-string\">'John'<\/span>}\nprint(type(my_dict))  <span class=\"hljs-comment\"># class 'dict'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code8', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code8');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>If we want to get the name of the datatype only as output, then we can use the<code>__name__<\/code> attribute along with the <code>type()<\/code> function. See the following example where <code>__name__<\/code> attribute is used.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code9\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">my_list =&#91;<span class=\"hljs-number\">10<\/span>,<span class=\"hljs-number\">20<\/span>,<span class=\"hljs-number\">20.5<\/span>,<span class=\"hljs-string\">'Python'<\/span>,<span class=\"hljs-number\">100<\/span>]\n<span class=\"hljs-comment\"># It will print only datatype of variable<\/span>\nprint(type(my_list).__name__) <span class=\"hljs-comment\"># list<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code9', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code9');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-delete-a-variable\">Delete a variable<\/h2>\n\n\n\n<p>Use the <code>del<\/code> keyword to delete the variable. Once we delete the variable, it will not be longer accessible and eligible for the garbage collector.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code10\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">var1 = <span class=\"hljs-number\">100<\/span>\nprint(var1) <span class=\"hljs-comment\"># 100<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code10', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code10');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>Now, let&#8217;s delete <code>var1<\/code> and try to access it again.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code11\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">var1 = <span class=\"hljs-number\">100<\/span>\n<span class=\"hljs-keyword\">del<\/span> var1\nprint(var1)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code11', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code11');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">NameError: name 'var1' is not defined<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-variable-s-case-sensitive\">Variable&#8217;s case-sensitive<\/h2>\n\n\n\n<p>Python is a case-sensitive language. If we define a variable with names <code>a = 100<\/code> and <code>A =200<\/code> then, Python differentiates between <code>a<\/code> and <code>A<\/code>. These variables are treated as two different variables (or objects).<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code12\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">a = <span class=\"hljs-number\">100<\/span>\nA = <span class=\"hljs-number\">200<\/span>\n\n<span class=\"hljs-comment\"># value of a<\/span>\nprint(a)  <span class=\"hljs-comment\"># 100<\/span>\n<span class=\"hljs-comment\"># Value of A<\/span>\nprint(A)  <span class=\"hljs-comment\"># 200<\/span>\n\na = a + A\nprint(a)  <span class=\"hljs-comment\"># 300<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code12', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code12');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-constant\">Constant<\/h2>\n\n\n\n<p><strong>Constant<\/strong> is a variable or value that does not change, which means it remains the same and cannot be modified.&nbsp;But in the case of Python, the constant concept is&nbsp;<strong>not applicable<\/strong>. By convention, we can use only uppercase characters to define the constant variable if we don&#8217;t want to change it.<\/p>\n\n\n\n<p><strong>&nbsp;Example<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">MAX_VALUE = <span class=\"hljs-number\">500<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It is just convention, but we can change the value of <code>MAX_VALUE<\/code> variable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-assigning-a-value-to-a-constant-in-python\">Assigning a value to a constant in Python<\/h3>\n\n\n\n<p>As we see earlier, in the case of Python, the constant concept is not applicable. But if we still want to implement it, we can do it using the following way.<\/p>\n\n\n\n<p>The declaration and assignment of constant in Python done with the module. Module means Python file (<code>.py<\/code>) which contains variables, functions, and packages.<\/p>\n\n\n\n<p>So let&#8217;s create two modules, <code>constant.py<\/code>&nbsp;and <code>main.py<\/code>, respectively.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>In the <code>constant.py<\/code> file, we will declare two constant variables,&nbsp; <code>PI<\/code> and <code>TOTAL_AREA<\/code>.<\/li><li>import constant module In <code>main.py<\/code> file.<\/li><\/ul>\n\n\n\n<p>To create a <strong>constant module <\/strong>write the below code in the <code>constant.py<\/code> file.<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">PI = <span class=\"hljs-number\">3.14<\/span>\nTOTAL_AREA = <span class=\"hljs-number\">205<\/span> <\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Constants are declared with uppercase later variables and separating the words with an underscore.<\/p>\n\n\n\n<p>Create a&nbsp;<strong>main.py&nbsp;<\/strong>and write the below code in it.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> constant\n\nprint(constant.PI)\nprint(constant.TOTAL_AREA)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">3.14\n205<\/pre>\n\n\n\n<p><strong>Note<\/strong>: Constant concept is not available in Python. By convention, we define constants in an uppercase letter to differentiate from variables. But it does not prevent reassignment, which means we can change the value of a constant variable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-rules-and-naming-convention-for-variables-and-constants\">Rules and naming convention for variables and constants<\/h2>\n\n\n\n<p>A name in a Python program is called an identifier. An identifier can be a variable name, class name, function name, and module name.<\/p>\n\n\n\n<p>There are some rules to define variables in Python.<\/p>\n\n\n\n<p>In Python, there are some conventions and rules to define variables and constants that should follow.<\/p>\n\n\n\n<p><strong>Rule 1<\/strong>: The name of the variable and constant should have a combination of letters, digits, and underscore symbols.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Alphabet\/letters i.e., lowercase (a to z) or uppercase (A to Z)<\/li><li>Digits(0 to 9)<\/li><li>Underscore symbol (_)<\/li><\/ul>\n\n\n\n<p><strong>Example&nbsp;<\/strong><\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">total_addition\nTOTAL_ADDITION\ntotalAddition\nTotaladdition<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Rule 2<\/strong>: The variable name and constant name should make sense.<\/p>\n\n\n\n<p>Note: we should always create a meaningful variable name so it will be easy to understand. That is, it should be meaningful.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">x = <span class=\"hljs-string\">\"Jessa\"<\/span>  \nstudent_name = <span class=\"hljs-string\">\"Jessa\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It above example variable <strong><code>x<\/code><\/strong> does not make more sense, but <code>student_name<\/code>&nbsp;is a meaningful variable.<\/p>\n\n\n\n<p><strong>Rule 3:<\/strong> Don&#8217;t&#8217; use special symbols in a variable name<\/p>\n\n\n\n<p>For declaring variable and constant, we cannot use special symbols like $, #, @, %, !~, etc. If we try to declare names with a special symbol, Python generates an error<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code13\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">ca$h = <span class=\"hljs-number\">1000<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code13', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code13');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ca$h = 11\n      ^\nSyntaxError: invalid syntax<\/pre>\n\n\n\n<p><strong>Rule 4: <\/strong>&nbsp;Variable and constant should not start with digit letters.<\/p>\n\n\n\n<p>You will receive an error if you start a variable name with a digit. Let&#8217;s verify this using a simple example.<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-number\">1<\/span>studnet = <span class=\"hljs-string\">\"Jessa\"<\/span>\nprint(<span class=\"hljs-number\">1<\/span>studnet)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here Python will generate a syntax error at <code>1studnet<\/code>. instead of this, you can declare a variable like <code>studnet_1 = \"Jessa\"<\/code><\/p>\n\n\n\n<p><strong>Rule 5:<\/strong>&nbsp;Identifiers are case sensitive.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code14\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">total = <span class=\"hljs-number\">120<\/span>\nTotal = <span class=\"hljs-number\">130<\/span>\nTOTAL = <span class=\"hljs-number\">150<\/span>\nprint(total)\nprint(Total)\nprint(TOTAL)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code14', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code14');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">120\n130\n150<\/pre>\n\n\n\n<p>Here, Python makes a difference between these variables that is uppercase and lowercase, so that it will create three different variables <code>total<\/code>, <code>Total<\/code>, <code>TOTAL<\/code>.<\/p>\n\n\n\n<p><strong>Rule 6:<\/strong>&nbsp;To declare constant should use capital letters.<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-23\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">MIN_VALUE = <span class=\"hljs-number\">100<\/span>\nMAX_VALUE = <span class=\"hljs-number\">1000<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-23\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Rule 6:<\/strong> Use an underscore symbol for separating the words in a variable name<\/p>\n\n\n\n<p>If we want to declare variable and constant names having two words, use an underscore symbol for separating the words.<\/p>\n\n\n<pre id=\"syntax\" class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-24\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">current_temperature = <span class=\"hljs-number\">24<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-24\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-multiple-assignments\">Multiple assignments<\/h2>\n\n\n\n<p>In Python, there is no restriction to declare a variable before using it in the program. Python allows us to create a variable as and when required.<\/p>\n\n\n\n<p>We can do multiple assignments in two ways, either by assigning&nbsp;a <strong>single value to multiple variables<\/strong>&nbsp;or assigning&nbsp;<strong>multiple values to multiple variables<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-assigning-a-single-value-to-multiple-variables\">Assigning a single value to multiple variables<\/h3>\n\n\n\n<p>we can assign a single value to multiple variables simultaneously using the assignment operator <code>=<\/code>.<\/p>\n\n\n\n<p>Now, let&#8217;s create an example to assign the single value <code>10<\/code> to all three variables <code>a<\/code>, <code>b<\/code>, and <code>c<\/code>.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code15\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-25\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">a = b = c = <span class=\"hljs-number\">10<\/span>\nprint(a) <span class=\"hljs-comment\"># 10<\/span>\nprint(b) <span class=\"hljs-comment\"># 10<\/span>\nprint(c) <span class=\"hljs-comment\"># 10<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-25\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code15', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code15');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-assigning-multiple-values-to-multiple-variables\">Assigning multiple values to multiple variables<\/h3>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code16\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-26\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">roll_no, marks, name = <span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">70<\/span>, <span class=\"hljs-string\">\"Jessa\"<\/span>\nprint(roll_no, marks, name) <span class=\"hljs-comment\"># 10 20 Jessa<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-26\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code16', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code16');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>In the above example, two integer values 10 and 70 are assigned to variables <code>roll_no<\/code> and <code>marks<\/code>, respectively, and string literal, &#8220;Jessa,&#8221; is assigned to the variable <code>name<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-variable-scope\">Variable scope<\/h2>\n\n\n\n<p><strong>Scope<\/strong>: The scope of a variable refers to the places where we can access a variable.<\/p>\n\n\n\n<p>Depending on the scope, the variable can categorize into two types<strong>&nbsp;local variable<\/strong> and&nbsp;the<strong> global variable.<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-local-variable\">Local variable<\/h3>\n\n\n\n<p>A local variable is a variable that is accessible inside a block of code only where it is declared. That means, If we declare a variable inside a method, the scope of the local variable is limited to the method only. So it is not accessible from outside of the method. If we try to access it, we will get an error.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code17\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-27\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test1<\/span><span class=\"hljs-params\">()<\/span>:<\/span>  <span class=\"hljs-comment\"># defining 1st function<\/span>\n    price = <span class=\"hljs-number\">900<\/span>  <span class=\"hljs-comment\"># local variable<\/span>\n    print(<span class=\"hljs-string\">\"Value of price in test1 function :\"<\/span>, price)\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test2<\/span><span class=\"hljs-params\">()<\/span>:<\/span>  <span class=\"hljs-comment\"># defining 2nd function<\/span>\n    <span class=\"hljs-comment\"># NameError: name 'price' is not defined<\/span>\n    print(<span class=\"hljs-string\">\"Value price in test2 function:\"<\/span>, price)\n\n<span class=\"hljs-comment\"># call functions<\/span>\ntest1()\ntest2()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-27\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code17', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code17');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>In the above example, we created a function with the name <code>test1<\/code>. Inside it, we created a local variable price. Similarly, we created another function with the name <code>test2<\/code> and tried to access price, but we got an error <code>\"price is not defined\"<\/code> because its scope is limited to function <code>test1()<\/code>. This error occurs because we cannot access the local variable from outside the code block.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-global-variable\">Global variable<\/h3>\n\n\n\n<p>A Global variable is a variable that is defined outside of the method (block of code). That is accessible anywhere in the code file.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code18\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-28\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">price = <span class=\"hljs-number\">900<\/span>  <span class=\"hljs-comment\"># Global variable<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test1<\/span><span class=\"hljs-params\">()<\/span>:<\/span>  <span class=\"hljs-comment\"># defining 1st function<\/span>\n    print(<span class=\"hljs-string\">\"price in 1st function :\"<\/span>, price)  <span class=\"hljs-comment\"># 900<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test2<\/span><span class=\"hljs-params\">()<\/span>:<\/span>  <span class=\"hljs-comment\"># defining 2nd function<\/span>\n    print(<span class=\"hljs-string\">\"price in 2nd function :\"<\/span>, price)  <span class=\"hljs-comment\"># 900<\/span>\n\n<span class=\"hljs-comment\"># call functions<\/span>\ntest1()\ntest2()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-28\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code18', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code18');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>In the above example, we created a global variable price and tried to access it in <code>test1<\/code> and <code>test2<\/code>. In return, we got the same value because the global variable is accessible in the entire file.<\/p>\n\n\n\n<p><strong>Note<\/strong>: You must declare the global variable outside function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-object-variable-identity-and-references\">Object\/Variable identity and references<\/h2>\n\n\n\n<p>In Python, whenever we create an object, a number is given to it and uniquely identifies it. This number is nothing but a memory address of a variable&#8217;s value. Once the object is created, the identity of that object never changes.<\/p>\n\n\n\n<p>No two objects will have the same identifier. The Object is for eligible garbage collection when deleted. Python has a built-in function <code>id()<\/code> to get the memory address of a variable.<\/p>\n\n\n\n<p>For example, consider a library with many books (memory addresses) and many students (objects). At the beginning(when we start The Python program), all books are available. When a new student comes to the library (a new object created), the librarian gives him a book. Every book has a unique number (identity), and that id number tells us which book is delivered to the student (object)<\/p>\n\n\n\n<p>Example<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code19\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-29\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">n = <span class=\"hljs-number\">300<\/span>\nm = n\nprint(id(n)) <span class=\"hljs-comment\"># same memory address<\/span>\nprint(id(m)) <span class=\"hljs-comment\"># same memory address <\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-29\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code19', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code19');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>It returns the same address location because both variables share the same value. But if we assign <code>m<\/code> to some different value, it points to a different object with a different identity.<\/p>\n\n\n\n<p>See the following example<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code20\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-30\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">m = <span class=\"hljs-number\">500<\/span>\nn = <span class=\"hljs-number\">400<\/span>\nprint(<span class=\"hljs-string\">\"Memory address of m:\"<\/span>, id(m))  <span class=\"hljs-comment\"># 1686681059984<\/span>\nprint(<span class=\"hljs-string\">\"Memory address of n:\"<\/span>, id(n))  <span class=\"hljs-comment\"># 1686681059824<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-30\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code20', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code20');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>For <code>m = 500<\/code>, Python created an integer object with the value 500 and set <code>m<\/code> as a reference to it. Similarly, <code>n<\/code> is assigned to an integer object with the value <code>400<\/code> and sets n as a reference to it.&nbsp;Both variables have different identities.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-object-reference\">Object Reference<\/h3>\n\n\n\n<p>In Python, when we assign a value to a variable, we create an object and reference it.<\/p>\n\n\n\n<p>For example, <code>a=10<\/code>, here, an object with the value&nbsp; <code>10<\/code> is created in memory, and reference <code>a<\/code> now points to the memory address where the object is stored.<\/p>\n\n\n\n<p>Suppose we created <code>a=10<\/code>, <code>b=10<\/code>, and&nbsp; <code>c=10<\/code>, the value of the three variables is the same. Instead of creating <strong>three objects,<\/strong> Python creates only one object as&nbsp;<code>10<\/code>&nbsp;with references such as&nbsp;<code>a<\/code>,<code>b<\/code>,<code>c<\/code>.<\/p>\n\n\n\n<p>We can access the memory addresses of these variables using the <code>id()<\/code> method. <code>a<\/code>, <code>b<\/code> refers to the <strong>same address<\/strong>&nbsp;in memory, and <code>c<\/code>, <code>d<\/code>, <code>e<\/code> refers to the same address. See the following example for more details.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code21\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-31\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">a = <span class=\"hljs-number\">10<\/span>\nb = <span class=\"hljs-number\">10<\/span>\nprint(id(a))\nprint(id(b))\n\nc = <span class=\"hljs-number\">20<\/span>\nd = <span class=\"hljs-number\">20<\/span>\ne = <span class=\"hljs-number\">20<\/span>\nprint(id(c))\nprint(id(d))\nprint(id(e))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-31\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code21', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code21');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">140722211837248\n140722211837248\n140722211837568\n140722211837568\n140722211837568<\/pre>\n\n\n\n<p>Here, an object in memory initialized with the value 10 and reference added to it, the reference count increments by&nbsp;&#8216;1&#8217;.<\/p>\n\n\n\n<p>When Python executes the next statement that is <code>b=10<\/code>, since it is the same value 10, a new object will not be created because the same object in memory with value 10 available, so it has created another reference, <code>b<\/code>. Now, the reference count for value 10 is&nbsp;&#8216;2&#8217;.<\/p>\n\n\n\n<p>Again for&nbsp;<code>c=20<\/code>, a new object is created with reference&nbsp;&#8216;c&#8217;&nbsp;since it has a unique value (20).&nbsp;Similarly, for <code>d<\/code>, and <code>e<\/code>&nbsp;new objects will not be created because the object&nbsp;&#8217;20&#8217;&nbsp;already available. Now, only the reference count will be incremented.<\/p>\n\n\n\n<p>We can check the reference counts of every object by using the <code>getrefcount<\/code> function of a&nbsp; <code>sys<\/code> module. This function takes the object as an input and returns the number of references.<\/p>\n\n\n\n<p>We can pass variable, name, value, function, class as an input to <code>getrefcount()<\/code> and in return, it will give a reference count for a given object.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code22\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-32\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> sys\nprint(sys.getrefcount(a))\nprint(sys.getrefcount(c))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-32\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code22', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code22');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>See the following image for more details.<\/p>\n\n\n\n<div class=\"wp-block-image size-full wp-image-7723 is-style-default\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"824\" height=\"421\" src=\"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/python-object-id-reference.png\" alt=\"Python object id references\" class=\"wp-image-7723\" srcset=\"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/python-object-id-reference.png 824w, https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/python-object-id-reference-300x153.png 300w, https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/python-object-id-reference-768x392.png 768w\" sizes=\"auto, (max-width: 824px) 100vw, 824px\" \/><figcaption>Python object id references<\/figcaption><\/figure><\/div>\n\n\n\n<p>In the above picture, <code>a<\/code>, <code>b<\/code> pointing to the same memory address location (i.e., 140722211837248), and <code>c<\/code>, <code>d<\/code>, <code>e<\/code> pointing to the same memory address (i.e., 140722211837568 ). So reference count will be 2 and 3 respectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-unpack-a-collection-into-a-variable\">Unpack a collection into a variable<\/h2>\n\n\n\n<p><strong>Packing<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>In Python, we can create a tuple (or list) by packing a group of variables.<\/li><li>Packing can be used when we want to collect multiple values in a single variable. Generally, this operation is referred to as tuple packing.<\/li><\/ul>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code23\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-33\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">a = <span class=\"hljs-number\">10<\/span>\nb = <span class=\"hljs-number\">20<\/span>\nc = <span class=\"hljs-number\">20<\/span>\nd = <span class=\"hljs-number\">40<\/span>\ntuple1 = a, b, c, d <span class=\"hljs-comment\"># Packing tuple<\/span>\nprint(t) <span class=\"hljs-comment\"># (10, 20, 20, 40)<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-33\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code23', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code23');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>Here <code>a<\/code>, <code>b<\/code>, <code>c<\/code>, <code>d<\/code>&nbsp;are packed in the tuple <code>tuple1<\/code>.<\/p>\n\n\n\n<p><strong>Tuple unpacking<\/strong>&nbsp;is the reverse operation of <strong>tuple packing<\/strong>. We can unpack tuple and assign tuple values to different variables.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code24\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-34\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">tuple1 = (<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">30<\/span>, <span class=\"hljs-number\">40<\/span>)\na, b, c, d = tuple1\nprint(a, b, c, d)  <span class=\"hljs-comment\"># 10 20 30 40<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-34\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code24', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code24');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Note:<\/strong> When we are performing unpacking, the number of variables and the number of values should be the same. That is, the number of variables on the left side of the tuple must exactly match a number of values on the right side of the tuple. Otherwise, we will get a <strong>ValueError<\/strong>.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code25\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-35\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">a, b = <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>\nprint(a,b)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-35\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code25', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code25');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a, b = 1, 2, 3\nValueError: too many values to unpack (expected 2)<\/pre>\n\n\n\n<p><strong>Also, See<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/pynative.com\/python-class-variables\/\">Class Variables<\/a><\/li><li><a href=\"https:\/\/pynative.com\/python-instance-variables\/\">Instance variables<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>A variable is a reserved memory area (memory address) to store value. For example, we want to store an employee&#8217;s salary. In such a case, we can create a variable and store salary using it. Using that variable name, you can read or modify the salary amount. In other words, a variable is a value [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[22,1],"tags":[32],"class_list":{"0":"post-7721","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python","7":"category-basics","8":"tag-python-basics","9":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python Variables &#8211; PYnative<\/title>\n<meta name=\"description\" content=\"Variables in Python. Learn how to use variable to store values. create, modify, and delete variable. Learn different types of variables\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/pynative.com\/python-variables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Variables\" \/>\n<meta property=\"og:description\" content=\"Variables in Python. Learn how to use variable to store values. create, modify, and delete variable. Learn different types of variables\" \/>\n<meta property=\"og:url\" content=\"https:\/\/pynative.com\/python-variables\/\" \/>\n<meta property=\"og:site_name\" content=\"PYnative\" \/>\n<meta property=\"article:published_time\" content=\"2021-02-01T13:40:45+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-08-31T05:22:35+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/python-object-id-reference.png\" \/>\n<meta name=\"author\" content=\"Vishal\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@PyNative\" \/>\n<meta name=\"twitter:site\" content=\"@PyNative\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vishal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"15 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-variables\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-variables\\\/\"},\"author\":{\"name\":\"Vishal\",\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"headline\":\"Python Variables\",\"datePublished\":\"2021-02-01T13:40:45+00:00\",\"dateModified\":\"2021-08-31T05:22:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-variables\\\/\"},\"wordCount\":2603,\"commentCount\":22,\"publisher\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"image\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-variables\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/python-object-id-reference.png\",\"keywords\":[\"Python Basics\"],\"articleSection\":[\"Python\",\"Python Basics\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/pynative.com\\\/python-variables\\\/#respond\"]}],\"accessibilityFeature\":[\"tableOfContents\"]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-variables\\\/\",\"url\":\"https:\\\/\\\/pynative.com\\\/python-variables\\\/\",\"name\":\"Python Variables &#8211; PYnative\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-variables\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-variables\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/python-object-id-reference.png\",\"datePublished\":\"2021-02-01T13:40:45+00:00\",\"dateModified\":\"2021-08-31T05:22:35+00:00\",\"description\":\"Variables in Python. Learn how to use variable to store values. create, modify, and delete variable. Learn different types of variables\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-variables\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/pynative.com\\\/python-variables\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-variables\\\/#primaryimage\",\"url\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/python-object-id-reference.png\",\"contentUrl\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/python-object-id-reference.png\",\"width\":824,\"height\":421,\"caption\":\"Python object id references\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-variables\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/pynative.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\\\/\\\/pynative.com\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python Variables\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/pynative.com\\\/#website\",\"url\":\"https:\\\/\\\/pynative.com\\\/\",\"name\":\"PYnative\",\"description\":\"Python Programming\",\"publisher\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/pynative.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\",\"name\":\"Vishal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"url\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"contentUrl\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"width\":968,\"height\":1065,\"caption\":\"Vishal\"},\"logo\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\"},\"description\":\"Founder of PYnative.com. I am a Python developer and I love to write articles to help developers. | All the best for your future Python endeavors!\",\"sameAs\":[\"https:\\\/\\\/pynative.com\"]}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python Variables &#8211; PYnative","description":"Variables in Python. Learn how to use variable to store values. create, modify, and delete variable. Learn different types of variables","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/pynative.com\/python-variables\/","og_locale":"en_US","og_type":"article","og_title":"Python Variables","og_description":"Variables in Python. Learn how to use variable to store values. create, modify, and delete variable. Learn different types of variables","og_url":"https:\/\/pynative.com\/python-variables\/","og_site_name":"PYnative","article_published_time":"2021-02-01T13:40:45+00:00","article_modified_time":"2021-08-31T05:22:35+00:00","og_image":[{"url":"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/python-object-id-reference.png","type":"","width":"","height":""}],"author":"Vishal","twitter_card":"summary_large_image","twitter_creator":"@PyNative","twitter_site":"@PyNative","twitter_misc":{"Written by":"Vishal","Est. reading time":"15 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/pynative.com\/python-variables\/#article","isPartOf":{"@id":"https:\/\/pynative.com\/python-variables\/"},"author":{"name":"Vishal","@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"headline":"Python Variables","datePublished":"2021-02-01T13:40:45+00:00","dateModified":"2021-08-31T05:22:35+00:00","mainEntityOfPage":{"@id":"https:\/\/pynative.com\/python-variables\/"},"wordCount":2603,"commentCount":22,"publisher":{"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"image":{"@id":"https:\/\/pynative.com\/python-variables\/#primaryimage"},"thumbnailUrl":"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/python-object-id-reference.png","keywords":["Python Basics"],"articleSection":["Python","Python Basics"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/pynative.com\/python-variables\/#respond"]}],"accessibilityFeature":["tableOfContents"]},{"@type":"WebPage","@id":"https:\/\/pynative.com\/python-variables\/","url":"https:\/\/pynative.com\/python-variables\/","name":"Python Variables &#8211; PYnative","isPartOf":{"@id":"https:\/\/pynative.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/pynative.com\/python-variables\/#primaryimage"},"image":{"@id":"https:\/\/pynative.com\/python-variables\/#primaryimage"},"thumbnailUrl":"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/python-object-id-reference.png","datePublished":"2021-02-01T13:40:45+00:00","dateModified":"2021-08-31T05:22:35+00:00","description":"Variables in Python. Learn how to use variable to store values. create, modify, and delete variable. Learn different types of variables","breadcrumb":{"@id":"https:\/\/pynative.com\/python-variables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/pynative.com\/python-variables\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/pynative.com\/python-variables\/#primaryimage","url":"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/python-object-id-reference.png","contentUrl":"https:\/\/pynative.com\/wp-content\/uploads\/2021\/02\/python-object-id-reference.png","width":824,"height":421,"caption":"Python object id references"},{"@type":"BreadcrumbList","@id":"https:\/\/pynative.com\/python-variables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/pynative.com\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/pynative.com\/python\/"},{"@type":"ListItem","position":3,"name":"Python Variables"}]},{"@type":"WebSite","@id":"https:\/\/pynative.com\/#website","url":"https:\/\/pynative.com\/","name":"PYnative","description":"Python Programming","publisher":{"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/pynative.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f","name":"Vishal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","url":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","contentUrl":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","width":968,"height":1065,"caption":"Vishal"},"logo":{"@id":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg"},"description":"Founder of PYnative.com. I am a Python developer and I love to write articles to help developers. | All the best for your future Python endeavors!","sameAs":["https:\/\/pynative.com"]}]}},"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"Vishal","author_link":"https:\/\/pynative.com\/author\/vishal\/"},"_links":{"self":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts\/7721","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/comments?post=7721"}],"version-history":[{"count":0,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts\/7721\/revisions"}],"wp:attachment":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/media?parent=7721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/categories?post=7721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/tags?post=7721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}