Python: Variables & Data Types

Variables

Variables are like containers for data (i.e. they hold the data) and the value of variable can vary throughout the program.

Note: Python variables are dynamically typed so no need to specifically write data type of variable like other languages C, C++, Java, etc. It takes data type based on assigned literals on the variable.

Declaration of Variable

Variable name is also called as identifier. It contains alphanumeric characters, underscore but no space or special characters allowed. The first letter should be alphabetical character or underscore.

Syntax: var_name = literal_value

where var_name is the name given to container which holds the value specified as literal_value

Example: distance=10

In the above example, distance is the container that holds the value 10 which can change during the execution of the program.

Some more example of variable name:

  • Variable store different types of values
class_strength = 96 #int
_subject = "Science" # String
Marks = 76.3 # Float
  • Invalid variable declaration
student name ="Arvind" #space in variable name
1st_mark = 99.6 #start with numeric value in variable name

Data Types

Python supports all these data  types: 

CategoryData TypeExample
Numericint124
Numeric long1247171381763817
Numeric with a decimal pointfloat124.46
Numeric with a decimal point double124124.32395324
AlphanumericcharA
Alphanumeric stringHello
BooleanbooleanTrue, False

Why Python is Dynamically Typed Language?

Python is called a Dynamically typed language because on time of declaring variable datatype was not mentioned and python considers these datatype based on assigned literal value.

Example:

distance=96 #line 1
print(distance, type(distance)) #line 2

Output :

96 <class ‘int’>

In line 1, variable distance is assigned a value 96 which is an integer, so the data type of variable num is an integer in line 1 and second-line 2 statement printing value of the variable and it’s type i.e int

Note: To check the datatype of the variable we can use type(var_name) which in turn returns the data type of the variable.

Data Types

Python support different type of Data Types this required specific size memory for storage.

  • Int
  • Float
  • Complex
  • String
  • Boolean

Types of Data

Python support all these types of data and data structure to store values. where data structure required to handle different types of storage for a collection of objects.

  • Numbers
  • String
  • List
  • Set
  • Tuple
  • Dictionary

We will discuss about all these type data and it’s storage structure in further section.