

{"id":5329,"date":"2017-12-19T07:58:54","date_gmt":"2017-12-19T02:28:54","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=5329"},"modified":"2026-04-13T11:56:03","modified_gmt":"2026-04-13T06:26:03","slug":"python-variables-and-data-types","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-variables-and-data-types\/","title":{"rendered":"Python Variables and Data Types &#8211; A complete guide for beginners"},"content":{"rendered":"<p>In this Python tutorial, we will learn about Python variables and data types being used in Python.<\/p>\n<p>We will also learn about converting one data type to another in Python and local and global variables in Python.<\/p>\n<p>So, let\u2019s begin with Python variables and data types Tutorial.<\/p>\n<h3>What are Python Variables?<a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/12\/python-variables-2-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-73702 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/12\/python-variables-2-1.jpg\" alt=\"variables in python\" width=\"552\" height=\"370\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/12\/python-variables-2-1.jpg 552w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/12\/python-variables-2-1-150x101.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/12\/python-variables-2-1-300x201.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/12\/python-variables-2-1-520x349.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/12\/python-variables-2-1-272x182.jpg 272w\" sizes=\"auto, (max-width: 552px) 100vw, 552px\" \/><\/a><\/h3>\n<p><span style=\"font-weight: 400\">Python lets you <\/span><b>store information in variables<\/b><span style=\"font-weight: 400\">. Think of a variable as a named box where you keep data while your program runs. You choose a short, clear name, then use the equal sign (<\/span><span style=\"font-weight: 400\">=<\/span><span style=\"font-weight: 400\">) to place a value inside that box.<\/span><\/p>\n<p>For example, if you store 7 in a variable, later, you can store \u2018Dinosaur\u2019.<\/p>\n<h4>1. Python Variables Naming Rules<\/h4>\n<p>There are certain rules for what you can name a variable(called an identifier).<\/p>\n<ul>\n<li>Variables in Python can only begin with a letter(A-Z\/a-z) or an underscore(_).<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; 9lives=9<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">SyntaxError: invalid syntax<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; flag=0\r\n&gt;&gt;&gt; flag<\/pre>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; _9lives='cat'\r\n&gt;&gt;&gt; _9lives<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>&#8216;cat&#8217;<\/strong><\/div>\n<ul>\n<li>The rest of the identifier may contain letters(A-Z\/a-z), underscores(_), and numbers(0-9).<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; year2='Sophomore'\r\n&gt;&gt;&gt; year2<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>&#8216;Sophomore&#8217;<\/strong><\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; _$$=7<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>SyntaxError: invalid syntax<\/strong><\/div>\n<ul>\n<li>Python is case-sensitive, and so are Python identifiers. Name and name are two different identifiers.<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; name='Ayushi'\r\n&gt;&gt;&gt; name<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>&#8216;Ayushi&#8217;<\/strong><\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; Name<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\nFile &#8220;&lt;pyshell#21&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\nName<br \/>\nNameError: name &#8216;Name&#8217; is not defined<\/div>\n<ul>\n<li>Reserved words (keywords) cannot be used as identifier names.<\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td width=\"104\">and<\/td>\n<td width=\"104\">def<\/td>\n<td width=\"104\">False<\/td>\n<td width=\"104\">import<\/td>\n<td width=\"104\">not<\/td>\n<td width=\"104\">True<\/td>\n<\/tr>\n<tr>\n<td width=\"104\">as<\/td>\n<td width=\"104\">del<\/td>\n<td width=\"104\">finally<\/td>\n<td width=\"104\">in<\/td>\n<td width=\"104\">or<\/td>\n<td width=\"104\">try<\/td>\n<\/tr>\n<tr>\n<td width=\"104\">assert<\/td>\n<td width=\"104\">elif<\/td>\n<td width=\"104\">for<\/td>\n<td width=\"104\">is<\/td>\n<td width=\"104\">pass<\/td>\n<td width=\"104\">while<\/td>\n<\/tr>\n<tr>\n<td width=\"104\">break<\/td>\n<td width=\"104\">else<\/td>\n<td width=\"104\">from<\/td>\n<td width=\"104\">lambda<\/td>\n<td width=\"104\">print<\/td>\n<td width=\"104\">with<\/td>\n<\/tr>\n<tr>\n<td width=\"104\">class<\/td>\n<td width=\"104\">except<\/td>\n<td width=\"104\">global<\/td>\n<td width=\"104\">None<\/td>\n<td width=\"104\">raise<\/td>\n<td width=\"104\">yield<\/td>\n<\/tr>\n<tr>\n<td width=\"104\">continue<\/td>\n<td width=\"104\">exec<\/td>\n<td width=\"104\">if<\/td>\n<td width=\"104\">nonlocal<\/td>\n<td width=\"104\">return<\/td>\n<td width=\"104\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>2. Assigning and Reassigning Python Variables<\/h4>\n<p>To assign a value to Python variables, you don\u2019t need to declare its type.<\/p>\n<p>You name it according to the rules stated in section 2a, and type the value after the equal sign(=).<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; age=7\r\n&gt;&gt;&gt; print(age)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>7<\/strong><\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; age='Dinosaur'\r\n&gt;&gt;&gt; print(age)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>Dinosaur<\/strong><\/div>\n<p>However, age=Dinosaur doesn\u2019t make sense. Also, you cannot use Python variables before assigning it a value.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; name<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>Traceback (most recent call last):<\/strong><br \/>\n<strong>File &#8220;&lt;pyshell#8&gt;&#8221;, line 1, in &lt;module&gt;<\/strong><br \/>\n<strong>name<\/strong><br \/>\n<strong>NameError: name &#8216;name&#8217; is not defined<\/strong><\/div>\n<p>You can\u2019t put the identifier on the right-hand side of the equal sign, though. The following code causes an error.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; 7=age<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>SyntaxError: can&#8217;t assign to literal<\/strong><\/div>\n<p>Neither can you assign Python variables to a keyword.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; False=choice<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>SyntaxError: can&#8217;t assign to keyword<\/strong><\/div>\n<h4>3. Multiple Assignment<\/h4>\n<p>You can assign values to multiple Python variables in one statement.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; age,city=21,'Indore'\r\n&gt;&gt;&gt; print(age,city)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>21 Indore<\/strong><\/div>\n<p>Or you can assign the same value to multiple Python variables.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; age=fav=7\r\n&gt;&gt;&gt; print(age,fav)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>7 7<\/strong><\/div>\n<p>This is how you assign values to Python Variables<\/p>\n<h4>4. Swapping Variables<\/h4>\n<p>Swapping means interchanging values. To swap Python variables, you don\u2019t need to do much.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a,b='red','blue'\r\n&gt;&gt;&gt; a,b=b,a\r\n&gt;&gt;&gt; print(a,b)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">blue red<\/div>\n<h4>5. Deleting Variables<\/h4>\n<p>You can also delete Python variables using the keyword \u2018del\u2019.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a='red'\r\n&gt;&gt;&gt; del a\r\n&gt;&gt;&gt; a<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>Traceback (most recent call last):<\/strong><br \/>\n<strong>File &#8220;&lt;pyshell#39&gt;&#8221;, line 1, in &lt;module&gt;<\/strong><br \/>\n<strong>a<\/strong><br \/>\n<strong>NameError: name &#8216;a&#8217; is not defined<\/strong><\/div>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/12\/python-variable-and-data-types.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-73690 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/12\/python-variable-and-data-types.jpg\" alt=\"Python variables and data types\" width=\"802\" height=\"420\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/12\/python-variable-and-data-types.jpg 802w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/12\/python-variable-and-data-types-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/12\/python-variable-and-data-types-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/12\/python-variable-and-data-types-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/12\/python-variable-and-data-types-520x272.jpg 520w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/a><\/p>\n<h3>Python Data Types<\/h3>\n<p>Different kinds of data need different boxes. Python calls these kinds data types. The most common are numbers, text (called strings), lists, and True\/False values (called booleans). Knowing the right type helps Python decide what actions are allowed.<\/p>\n<p>You can check a variable\u2019s type with the built-in type() function. If you mix the wrong types, Python shows an error. For smooth coding, always know what type you have and what type you need before you combine data.<\/p>\n<p>Python supports the following data types.<\/p>\n<h4>1. Python Numbers<\/h4>\n<p>There are four numeric data types in Python.<\/p>\n<p><strong>a. int in Pythin<\/strong><\/p>\n<p>int stands for integer. The integer data type in Python holds signed integers. We can use the type() function to find which class it belongs to.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=-7\r\n&gt;&gt;&gt; type(a)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>&lt;class &#8216;int&#8217;&gt;<\/strong><\/div>\n<p>An integer can be of any length, with the only limitation being the available memory.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=9999999999999999999999999999999\r\n&gt;&gt;&gt; type(a)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>&lt;class &#8216;int&#8217;&gt;<\/strong><\/div>\n<p><strong>b. float in Python<\/strong><\/p>\n<p>The float data type in Python holds floating-point real values. An int can only store the number 3, but a float can store 3.25 if you want.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=3.0\r\n&gt;&gt;&gt; type(a)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>&lt;class &#8216;float&#8217;&gt;<\/strong><\/div>\n<p><strong>c. long<\/strong><\/p>\n<p>Long Data type in Python holds a long integer of unlimited length. But this construct does not exist in Python 3.x.<\/p>\n<p><strong>d. complex<\/strong><\/p>\n<p>A complex data type in Python holds a complex number. A complex number looks like this: a+bj Here, a and b are the real parts of the number, and j is imaginary.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=2+3j\r\n&gt;&gt;&gt; type(a)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>&lt;class &#8216;complex&#8217;&gt;<\/strong><\/div>\n<p>Use the isinstance() function to tell if Python variables belong to a particular class. It takes two parameters- the variable\/value, and the class.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; print(isinstance(a,complex))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>True<\/strong><\/div>\n<h4>2. Python Strings<\/h4>\n<p>A string is a sequence of characters. Python does not have a char data type, unlike C++ or Java. You can delimit a string using single quotes or double quotes.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; city='Ahmedabad'\r\n&gt;&gt;&gt; city<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>&#8216;Ahmedabad&#8217;<\/strong><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; city=\"Ahmedabad\"\r\n&gt;&gt;&gt; city<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>&#8216;Ahmedabad&#8217;<\/strong><\/div>\n<p><strong><span style=\"font-family: Verdana, Geneva, sans-serif\">a. Spanning a String Across Lines in Python<\/span><\/strong><\/p>\n<p>To span a string across multiple lines, you can use triple quotes.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; var=\"\"\"If\r\nonly\"\"\"\r\n&gt;&gt;&gt; var<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>&#8216;If\\n\\tonly&#8217;<\/strong><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; print(var)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>If<\/strong><br \/>\n<strong>Only<\/strong><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; \"\"\"If\r\nonly\"\"\"<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>&#8216;If\\n\\tonly&#8217;<\/strong><\/div>\n<p>As you can see, the quotes preserved the formatting (\\n is the escape sequence for newline, \\t is for tab).<\/p>\n<p><strong>b. Displaying Part of a String in Python<\/strong><\/p>\n<p>You can display a character from a string using its index in the string. Remember, indexing starts with 0.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; lesson='disappointment'\r\n&gt;&gt;&gt; lesson[0]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>&#8216;d&#8217;<\/strong><\/div>\n<p>You can also display a burst of characters in a string using the slicing operator [].<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; lesson[5:10]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>&#8216;point&#8217;<\/strong><\/div>\n<p>This prints the characters from 5 to 9.<\/p>\n<p><strong>c. String Formatters in Python<\/strong><\/p>\n<p>String formatters in Python allow us to print characters and values at once. You can use the % operator.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; x=10;\r\n&gt;&gt;&gt;\u00a0printer=\"Dell\"\r\n&gt;&gt;&gt; print(\"I just printed %s pages to the printer %s\" % (x, printer))\r\n\r\n<\/pre>\n<p>Or you can use the format method.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; print(\"I just printed {0} pages to the printer {1}\".format(x, printer))\r\n&gt;&gt;&gt; print(\"I\u00a0 just printed {x} pages to the printer {printer}\".format(x=7, printer=\"Dell\"))<\/pre>\n<p>A third option is to use f-strings.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; print(f\"I just printed {x} pages to the printer {printer}\")<\/pre>\n<p><strong>d. String Concatenation in Python<\/strong><\/p>\n<p>You can concatenate(join) strings in Python.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a='10'\r\n&gt;&gt;&gt; print(a+a)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>1010<\/strong><\/div>\n<p>However, you cannot concatenate values of different types.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; print('10'+10)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p><strong>Traceback (most recent call last):<\/strong><strong>File &#8220;&lt;pyshell#89&gt;&#8221;, line 1, in &lt;module&gt;;<\/strong><\/p>\n<p><strong>print(&#8217;10&#8217;+10)<\/strong><\/p>\n<p><strong>TypeError: must be str, not int<\/strong><\/p>\n<\/div>\n<h4>3. Python Lists<\/h4>\n<p>A list is a collection of values. Remember, it may contain different types of values.<\/p>\n<p>To define a list in Python, you must put values separated by commas in square brackets. You don\u2019t need to declare a type for a list either.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; days=['Monday','Tuesday',3,4,5,6,7]\r\n&gt;&gt;&gt; days<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>[&#8216;Monday&#8217;, &#8216;Tuesday&#8217;, 3, 4, 5, 6, 7]<\/strong><\/div>\n<p><strong>a. Slicing a List in Python<\/strong><\/p>\n<p>You can slice a list in python the way you\u2019d slice a string- with the slicing operator.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; days[1:3]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>[&#8216;Tuesday&#8217;, 3]<\/strong><\/div>\n<p>Indexing for a list begins with 0, like for a string. Python doesn\u2019t have arrays.<\/p>\n<p><strong>b. Finding the Length of a List in Python<\/strong><\/p>\n<p>Python supports an inbuilt function to calculate the length of a list.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; len(days)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>7<\/strong><\/div>\n<p><strong>c. Reassigning Elements of a List<\/strong><\/p>\n<p>A list is mutable in Python. This means that you can reassign elements later on.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; days[2]='Wednesday'\r\n&gt;&gt;&gt; days<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>[&#8216;Monday&#8217;, &#8216;Tuesday&#8217;, &#8216;Wednesday&#8217;, 4, 5, 6, 7]<\/strong><\/div>\n<p><strong>d. Iterating on the List in Python<\/strong><\/p>\n<p>To iterate over the list in Python, we can use the for loop. By iterating, we can access each element one by one, which is very helpful when we need to perform some operations on each element of a list.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">nums = [1,2,5,6,8]\r\nfor n in nums:\r\n    print(n)\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<br \/>\n2<br \/>\n5<br \/>\n6<br \/>\n8<\/div>\n<p><strong>e. Multidimensional Lists<\/strong><\/p>\n<p>A list may have more than one dimension. Have a detailed look at this in DataFlair&#8217;s tutorial on<a href=\"https:\/\/data-flair.training\/blogs\/python-list-examples\/\"><em><strong> Python Lists<\/strong><\/em><\/a>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=[[1,2,3],[4,5,6]]\r\n&gt;&gt;&gt; a<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>[[1, 2, 3], [4, 5, 6]]<\/strong><\/div>\n<h4>4. Python Tuples<\/h4>\n<p>A tuple in Python is like a list. You declare it using parentheses instead.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; subjects=('Physics','Chemistry','Maths')\r\n&gt;&gt;&gt; subjects<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>(&#8216;Physics&#8217;, &#8216;Chemistry&#8217;, &#8216;Maths&#8217;)<\/strong><\/div>\n<p><strong>a. Accessing and Slicing a Tuple in Python<\/strong><\/p>\n<p>You access a tuple in Python the same way as you\u2019d access a list. The same goes for slicing it.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; subjects[1]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>&#8216;Chemistry&#8217;<\/strong><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; subjects[0:2]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>(&#8216;Physics&#8217;, &#8216;Chemistry&#8217;)<\/strong><\/div>\n<p><strong>b. A tuple is Immutable<\/strong><\/p>\n<p><em><strong>Python tuple<\/strong><\/em> is immutable. Once declared, you can\u2019t change its size or elements.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; subjects[2]='Biology'<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>Traceback (most recent call last):<\/strong><br \/>\n<strong>File &#8220;&lt;pyshell#107&gt;&#8221;, line 1, in &lt;module&gt;<\/strong><br \/>\n<strong>subjects[2]=&#8217;Biology&#8217;<\/strong><br \/>\n<strong>TypeError: &#8216;tuple&#8217; object does not support item assignment<\/strong><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; subjects[3]='Computer Science'<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>Traceback (most recent call last):<\/strong><br \/>\n<strong>File &#8220;&lt;pyshell#108&gt;&#8221;, line 1, in &lt;module&gt;<\/strong><br \/>\n<strong>subjects[3]=&#8217;Computer Science&#8217;<\/strong><br \/>\n<strong>TypeError: &#8216;tuple&#8217; object does not support item assignment<\/strong><\/div>\n<h4>5. Python Dictionaries<\/h4>\n<p>A dictionary in Python holds key-value pairs. Declare it in curly braces, with pairs separated by commas. Separate keys and values by a colon(:).<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; person={'city':'Ahmedabad','age':7}\r\n&gt;&gt;&gt; person<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>{&#8216;city&#8217;: &#8216;Ahmedabad&#8217;, &#8216;age&#8217;: 7}<\/strong><\/div>\n<p>The type() function works with dictionaries too.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; type(person)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>&lt;class &#8216;dict&#8217;&gt;<\/strong><\/div>\n<p><strong>a. Accessing a Value in a Dictionary<\/strong><\/p>\n<p>To access a value in a dictionary, you mention the key in square brackets.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; person['city']<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>&#8216;Ahmedabad&#8217;<\/strong><\/div>\n<p><strong>b. Reassigning Elements in Dictionary<\/strong><\/p>\n<p>You can reassign a value to a key.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; person['age']=21\r\n&gt;&gt;&gt; person['age']<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>21<\/strong><\/div>\n<p><strong>c. List of Keys<\/strong><\/p>\n<p>Use the keys() function to get a list of keys in the dictionary.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; person.keys()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>dict_keys([&#8216;city&#8217;, &#8216;age&#8217;])<\/strong><\/div>\n<h4>6. bool<\/h4>\n<p><span style=\"font-family: Verdana, Geneva, sans-serif;font-size: 16px;font-weight: inherit\">A Boolean value can be True or False.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=2&gt;1\r\n&gt;&gt;&gt; type(a)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>&lt;class &#8216;bool&#8217;&gt;<\/strong><\/div>\n<h4>7. Python Sets<\/h4>\n<p><span style=\"font-family: Verdana, Geneva, sans-serif;font-weight: inherit\">A set can have a list of values. In Python, a set is defined using curly braces.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a={1,2,3}\r\n&gt;&gt;&gt; a<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>{1, 2, 3}<\/strong><\/div>\n<p>It returns only one instance of any value present more than once.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a={1,2,2,3}\r\n&gt;&gt;&gt; a<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>{1, 2, 3}<\/strong><\/div>\n<p>However, a set is unordered, so it doesn\u2019t support indexing.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a[2]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>Traceback (most recent call last):<\/strong><br \/>\n<strong>File &#8220;&lt;pyshell#127&gt;&#8221;, line 1, in &lt;module&gt;<\/strong><br \/>\n<strong>a[2]<\/strong><br \/>\n<strong>TypeError: &#8216;set&#8217; object does not support indexing<\/strong><\/div>\n<p>Also, it is mutable. You can change its elements or add more. Use the add() and remove() methods to do so.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a={1,2,3,4}\r\n&gt;&gt;&gt; a<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>{1, 2, 3, 4}<\/strong><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a.remove(4)\r\n&gt;&gt;&gt; a<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>{1, 2, 3}<\/strong><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a.add(4)\r\n&gt;&gt;&gt; a<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>{1, 2, 3, 4}<\/strong><\/div>\n<h3>Type Conversion in Python<\/h3>\n<p>Since Python is a dynamically-typed language, you may want to convert a value into another type. Python supports a list of functions for the same.<\/p>\n<h4>1. int() in Python<\/h4>\n<p>Python int() function converts the value into an int.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; int(3.7)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>3<\/strong><\/div>\n<p>Notice how it truncated 0.7 instead of rounding the number off to 4. You can also turn a Boolean into an int.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; int(True)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>1<\/strong><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; int(False)<\/pre>\n<p>However, you cannot turn a string into an int. It throws an error.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; int(\"a\")<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>Traceback (most recent call last):<\/strong><br \/>\n<strong>File &#8220;&lt;pyshell#135&gt;&#8221;, line 1, in &lt;module&gt;;<\/strong><br \/>\n<strong>int(&#8220;a&#8221;)<\/strong><br \/>\n<strong>ValueError: invalid literal for int() with base 10: &#8216;a&#8217;<\/strong><\/div>\n<p>However, if the string has only numbers, then you can.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; int(\"77\")<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>77<\/strong><\/div>\n<h4>2. float() in Python<\/h4>\n<p>Python float() function converts the value into a float.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; float(7)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>7.0<\/strong><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; float(7.7)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>7.7<\/strong><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; float(True)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p><strong>1.0<\/strong><\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; float(\"11\")<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>11.0<\/strong><\/div>\n<p>You can also use \u2018e\u2019 to denote an exponential number.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; float(\"2.1e-2\")<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>0.021<\/strong><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; float(2.1e-2)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>0.021<\/strong><\/div>\n<p>However, this number works even without the float() function.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 2.1e-2<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>0.021<\/strong><\/div>\n<h4>3. str() in Python<\/h4>\n<p>Python str() function converts the value into a string.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; str(2.1)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>&#8216;2.1&#8217;<\/strong><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; str(7)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>&#8216;7&#8217;<\/strong><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; str(True)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>&#8216;True&#8217;<\/strong><\/div>\n<p>You can also convert a list, a tuple, a set, or a dictionary into a string.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; str([1,2,3])<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>&#8216;[1, 2, 3]&#8217;<\/strong><\/div>\n<h4>4. bool() in Python<\/h4>\n<p>Python bool() function converts the value into a boolean.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; bool(3)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>True<\/strong><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; bool(0)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>False<\/strong><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; bool(True)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>True<\/strong><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; bool(0.1)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>True<\/strong><\/div>\n<p>You can convert a list into a Boolean.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; bool([1,2])<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>True<\/strong><\/div>\n<p>The function returns False for empty constructs.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; bool()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>False<\/strong><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; bool([])<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>False<\/strong><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; bool({})<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>False<\/strong><\/div>\n<p>None is a keyword in Python that represents an absence of value.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; bool(None)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>False<\/strong><\/div>\n<h4>5. set() in Python<\/h4>\n<p>python set() function converts the value into a set.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; set([1,2,2,3])<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>{1, 2, 3}<\/strong><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; set({1,2,2,3})<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>{1, 2, 3}<\/strong><\/div>\n<h4>6. list() in Python<\/h4>\n<p>Python list() function converts the value into a list.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; del list\r\n&gt;&gt;&gt; list(\"123\")<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>[&#8216;1&#8217;, &#8216;2&#8217;, &#8216;3&#8217;]<\/strong><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list({1,2,2,3})<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>[1, 2, 3]<\/strong><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list({\"a\":1,\"b\":2})<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>[&#8216;a&#8217;, &#8216;b&#8217;]<\/strong><\/div>\n<p>However, the following raises an error.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list({a:1,b:2})<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>Traceback (most recent call last):<\/strong><br \/>\n<strong>File &#8220;&lt;pyshell#173&gt;&#8221;, line 1, in &lt;module&gt;;<\/strong><br \/>\n<strong>list({a:1,b:2})<\/strong><br \/>\n<strong>TypeError: unhashable type: &#8216;set&#8217;<\/strong><\/div>\n<h4>7. tuple() in Python<\/h4>\n<p>Python tuple() function converts the value into a tuple.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; tuple({1,2,2,3})<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>(1, 2, 3)<\/strong><\/div>\n<p>You can try your own combinations. Also, try composite functions.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; tuple(list(set([1,2])))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>(1, 2)<\/strong><\/div>\n<h3>Python Local and Global Variables<\/h3>\n<p>Another classification of Python variables is based on scope.<\/p>\n<h4>1. Python Local Variables<\/h4>\n<p>When you declare a variable in a function, class, or so on, it is only visible in that scope. If you call it outside of that scope, you get an \u2018undefined\u2019 error.<\/p>\n<ul>\n<li>Local variables in Python can be accessed only within a particular function.<\/li>\n<li>It executes when a function is called in which the local variable is defined.<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; def func1():\r\n\tuvw=2\r\n\tprint(uvw)\r\n&gt;&gt;&gt; func1()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<p><strong>2<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; uvw<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>Traceback (most recent call last):<\/strong><br \/>\n<strong>File &#8220;&lt;pyshell#76&gt;&#8221;, line 1, in &lt;module&gt;<\/strong><br \/>\n<strong>uvw<\/strong><br \/>\n<strong>NameError: name &#8216;uvw&#8217; is not defined[\/php]<\/strong><\/div>\n<p>Here, the variable uvw is local to the function func1().<\/p>\n<h4>2. Global Variables in Python<\/h4>\n<p>When you declare a variable outside any context\/scope, it is visible in the whole program.<\/p>\n<ul>\n<li>It can be accessed anywhere in the entire program.<\/li>\n<li>If the value of a global variable is changed, it will affect the whole program.<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; xyz=3\r\n&gt;&gt;&gt; def func2():\r\n\txyz=0\r\n\txyz+=1\r\n\tprint(xyz)\r\n&gt;&gt;&gt; func2()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>1<\/strong><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; xyz<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>3<\/strong><\/div>\n<p>You can use the \u2018global\u2019 keyword when you want to treat a variable as global in a local scope.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; foo=1\r\n&gt;&gt;&gt; def func2():\r\n\tglobal foo\r\n\tfoo=3\r\n\tprint(foo)\r\n&gt;&gt;&gt; func2()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>3<\/strong><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; foo<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\"><strong>3<\/strong><\/div>\n<div><\/div>\n<h3>Frequently asked Python Interview Questions on Python Variables and Datatypes?<\/h3>\n<div>\n<p>1. What are variables and data types in Python?<\/p>\n<p>2. What is type () in Python?<\/p>\n<p>3. What are Local and Global variables in Python?<\/p>\n<p>4. Explain various naming rules for Python Variables.<\/p>\n<p>5. How to display part of a string?<\/p>\n<h3>Summary<\/h3>\n<p>In this tutorial on Python Variables and Data Types, we learned about different variables in Python and data types in Python with examples.<\/p>\n<p>We looked at the naming rules, and defining and deleting them. Then we saw different data types in python &#8211; numbers, strings, lists, dictionaries, tuples, sets, and many more.<\/p>\n<p>We also learned how to convert one variable type into another and local and global variables in Python.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this Python tutorial, we will learn about Python variables and data types being used in Python. We will also learn about converting one data type to another in Python and local and global&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":73690,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[10460,21575,21574,16455,10917,36628,15343],"class_list":["post-5329","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python-data-types","tag-python-global-variable","tag-python-local-variable","tag-python-variables","tag-python-variables-and-data-types","tag-type-conversion-in-python","tag-variables-in-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Variables and Data Types - A complete guide for beginners - DataFlair<\/title>\n<meta name=\"description\" content=\"In this Python tutorial, we will learn about variables and data types that are used in Python. Let\u2019s begin with Python 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:\/\/data-flair.training\/blogs\/python-variables-and-data-types\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Variables and Data Types - A complete guide for beginners - DataFlair\" \/>\n<meta property=\"og:description\" content=\"In this Python tutorial, we will learn about variables and data types that are used in Python. Let\u2019s begin with Python variables.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-variables-and-data-types\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2017-12-19T02:28:54+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-13T06:26:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/12\/python-variable-and-data-types.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Variables and Data Types - A complete guide for beginners - DataFlair","description":"In this Python tutorial, we will learn about variables and data types that are used in Python. Let\u2019s begin with Python 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:\/\/data-flair.training\/blogs\/python-variables-and-data-types\/","og_locale":"en_US","og_type":"article","og_title":"Python Variables and Data Types - A complete guide for beginners - DataFlair","og_description":"In this Python tutorial, we will learn about variables and data types that are used in Python. Let\u2019s begin with Python variables.","og_url":"https:\/\/data-flair.training\/blogs\/python-variables-and-data-types\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2017-12-19T02:28:54+00:00","article_modified_time":"2026-04-13T06:26:03+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/12\/python-variable-and-data-types.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-variables-and-data-types\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-variables-and-data-types\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Python Variables and Data Types &#8211; A complete guide for beginners","datePublished":"2017-12-19T02:28:54+00:00","dateModified":"2026-04-13T06:26:03+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-variables-and-data-types\/"},"wordCount":2122,"commentCount":18,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-variables-and-data-types\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/12\/python-variable-and-data-types.jpg","keywords":["python data types","python global variable","python local variable","Python Variables","python variables and data types","type conversion in python","variables in python"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-variables-and-data-types\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-variables-and-data-types\/","url":"https:\/\/data-flair.training\/blogs\/python-variables-and-data-types\/","name":"Python Variables and Data Types - A complete guide for beginners - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-variables-and-data-types\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-variables-and-data-types\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/12\/python-variable-and-data-types.jpg","datePublished":"2017-12-19T02:28:54+00:00","dateModified":"2026-04-13T06:26:03+00:00","description":"In this Python tutorial, we will learn about variables and data types that are used in Python. Let\u2019s begin with Python variables.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-variables-and-data-types\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-variables-and-data-types\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-variables-and-data-types\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/12\/python-variable-and-data-types.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/12\/python-variable-and-data-types.jpg","width":802,"height":420,"caption":"Python variables and data types"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-variables-and-data-types\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Variables and Data Types &#8211; A complete guide for beginners"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/5329","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=5329"}],"version-history":[{"count":22,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/5329\/revisions"}],"predecessor-version":[{"id":147571,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/5329\/revisions\/147571"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/73690"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=5329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=5329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=5329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}