All Courses

Python Variables and Data Types

Pallavi Dhotre

2 years ago

Python Variables and Data Types | insideAIML
Table of content
  • Introduction to Variables
  • Python Variables and Data Types
  • Data Type and Type Conversion of Variables in Python
  • Summary

Introduction to Variables

          In this article, we are going to learn about python variables and data types. In general, variables are used to store the value. Variable can store only one value at a time. The value may change during the runtime. We can assign multiple values to a variable but one value at a time. Whenever we assign some value to the variable that is stored in the main memory. We can also say that variable is nothing but a location or space in the main memory. Python variables and data types are important aspects in programming because we need to declare the variable before using it. But we don’t write the data type as python automatically recognizes the type of the data by observing the value assigned to it.
Let’s understand this concept with a simple example. There is a bookshelf. There are various compartments on the shelf. You can put the number of books on the shelf and can take out any book from the shelf. In this case, we can consider the bookshelf as the main memory and the compartments on the shelf as a variable name. The number of books we store in the compartment can be considered as a value assigned to the variable that may change frequently.
In python we can assign value to a variable in the following way:
X=10
There are some rules for naming a variable they are as follow:
  • The variable name can have alphanumeric characters; only no special symbol except underscore ( _ ) is allowed. E.g. student_name1 is a valid variable name
  • Python is a case-sensitive language that’s why different cases have a different meanings. E.g. name and NAME will be considered as different variables.
  • Space between the variable names is not allowed. E.g. student name is not a valid variable name. Instead, you can write it as a student_name
  • Variable names should always start with a letter or an underscore. You cannot start a variable name with a number. E.g. name1 or _name1 is a valid variable name but 1name is invalid.

Python Variables and Data Types

Python Variable | insideaiml
          There are different types of variables in python. Python supports various data types. Following are some common types of variables in python
  • Integer/long data type in python: Python2 supports integers and long int. In Python 3 there is no restriction on the size of integer that means it provides an unlimited size so there is no long data type in python 3. Any positive or negative number is considered as an Integer value. We can also say that any whole number or a number without a fraction part is known as Integer. E.g. 10,-6 these numbers are integers. Python declares a variable type integer in the following way:
x = 10

y = 20
  • Real/float: Any positive or negative number with a fraction is considered as a float or real value. E.g. 10.25, -4.34 these numbers are float (also called real) We can declare a variable of type float or real in the following way:
X=-4.56
  • Boolean: The variable that contains only boolean values in python ( True or False as a value) is known as Boolean variables. Boolean can hold only one value at a time. We can declare Boolean values in python in the following way:
Flag=True
  • String: anything that is enclosed within a single quote or double quote is known as a string. E.g. “Hello word”, ‘10 is my roll number’. These two sentences are strings as they are enclosed in the double quote and single quote. We can declare a variable of type string in the following way:
Address= “Pune, Maharashtra”
  • List: In some cases, we need to store more than one value under the same variable name. But variables can hold only one value at a time. To overcome this problem there is a solution called a list. Lists can hold multiple values under the same variable name. The list is one of the powerful data structures used in Python. The location of the elements stored in the list is also known as the index. List index always starts with 0. The advantage of using a list is that they are mutable which means they can be modified or can be changed. We can declare a list in the following ways:
             To declare an empty list syntax is list_name=[]
             
color=[]
             To declare a list with values of similar data type syntax is:
 
 list_name=[value1,value2,value3]
 
 color=['red','green','blue']
 
 marks=[60,75,80]
            To declare a list with values of different data type syntax is:
list_name=[value1,value2,value3]

info=["name",80,13000.0]
  • Tuple: Same as list tuples are used to store multiple items under a single variable name. The only difference between lists and tuples is that lists are mutable; they can be modified, but tuples are immutable; they cannot be modified. Once a tuple is declared its value cannot be changed during the runtime. We can declare a tuple in the following ways. To declare a tuple with only one element we have to give a comma after the element otherwise it will not be recognized as a tuple in python.
color=('red',)
              To declare a tuple with values of similar data type syntax is:
 tuple_name=(value1,value2,value3)

 color=('red','green','blue')

 marks=(60,75,80)

             To declare a tuple with values of different data type syntax is: 
 tuple_name=(value1,value2,value3)

 info=("name",80,13000.0)
  • Dictionary: Another powerful and most used data structure in Python is dictionaries. In dictionaries, data is stored in the form of key and value pairs. This is also a mutable type of data structure that means we can modify the elements in the dictionary. Dictionary can have one or more than one key and value pairs. We can store the elements in the form key: value where colon ‘:’ is a separator that separates the keys from the values. Elements in the dictionary are separated by a comma and everything is enclosed within the curly brackets {}. We can declare a dictionary in the following way:
data={'Name':'Ram','Mob_No':6787998845,'Salary':45000}

Data Type and Type Conversion of Variables in Python

        The good thing about using python variables and data types is we don’t need to write the data type while declaring the variable in python. Python automatically recognizes the type of data assigned to the variable by observing the values assigned to the variable. let’s deep dive into python variables and data types with the following example:
x= 10 in this example 10 is a whole number so the data type is an integer.
X=10.5 in this example 10.5 is a number with the fractional part so the data type is float
X= “10 is my roll number” in this example value is enclosed within the double quotes so the data type is a string.
X= True in this example value assigned is in True False format so the data type is Boolean
X=[10,20] in this example data is enclosed within a square bracket so the data type is a list
X=(10,20) in this example data is enclosed within a round bracket so the data type is a tuple
X={‘No1’:10, ‘No2’:20} in this example data is enclosed within the curly bracket so the data type is a dictionary.
We can use the type() method to check type of variable in python. To print the type of the class or data type we need to pass the variable name as a parameter to the method. For example:
print(type(x))using this statement the type of the class will be displayed on the screen. So this is all about the python variables and data types. Now let’s see how to change the data type of the variable in python?
We can also change the python variables and data types in the following ways
Python Data Type Conversion | insideAIML
Following are some examples to change the python variables and data types of the variables:
  • String to integer:
          We can convert python str to int in the following way
a=input() #default type is string

a=int(a) #converted to python str to int
  • Integer to float:
          Python int to float conversion is done in the following way
 a=int(input()) #data type is int

 a=float(a) #converted to float
  • List to a tuple: 
          While converting the python list to tuple you need to write a tuple keyword followed by   (List_name). See the example given below:
x=['red','green','blue'] #list

y=tuple(x) #converting list to tuple

Summary

          In this article, we have learned about the python variables and data types of the variables used in Python. Also, we have learned the rules used for naming the variables. We learned about various data types and how to check the type of variable in python. Also, to change the python variables and data types of the variables (type conversion).
            
We hope you enjoyed the article. If you have any related queries regarding the python variables and data types, feel free to ask in the comment section below.
          
Liked what you read? Then don’t break the spree. Visit our blog page to read more awesome articles. 
Or if you are into videos, then we have an amazing Youtube channel as well. Visit our InsideAIML Youtube  Channel to learn all about Artificial Intelligence, Deep Learning, Data Science and Machine Learning. 
Keep Learning. Keep Growing. 
           

Submit Review