Variables and Data Types in Python
"We will learn about variables in python, what are they and how are they useful. We will also dicuss about Data Types and dynamic nature of python."
By, Sarvpreet Singh
06-10-2025
Variables
We have stated our journey by getting stated with varibales. Variables are important concepts to understand in any programming language like python.
What are variables?
Variables are names that store values in memory. This is important as we might need to store some numbers for calculations, sentences, some sort of results etc.
Think of them as labeled boxes which can store particular type of values.
- int labeled boxes will store numbers.
- str (for string) labeled boxes will store characters or sentences. etc...
(The int, str above are data types btw. We have discussed them later in this section.)
Basic Sytax of variabels
Here's how you can use a variables in a python file/program. (Make a variables.py file if you haven't.)
Variable_name = Value
On the Left side we name the variable, and on right side of equal to sign = we type the value like 89, "Hello", true etc.
Example of variables
Here's some example that you can practise.
a = 67
name = "codeSarv"
is_it_fun = TrueWe have made three variables in the above code:
- Variable named as a is assigned a value 67 (integer)
- Variable named as name is assigned a value "codeSarv" (string)
- Variable named as is_it_fun is assigned a value True (bool)
Printing variables
Let's use these variables and try printing the values they contain. We will use a built-in function print(). Examples of print() function:
print("Hello world!")We have passed a string (group of characters in double quotes). When clicked run, the result is shown in terminal.
You can try printing anything like:
print("My name is Sarv.")
print("Nice to meet you!")But, to print a value that is contained in a variable, we need to type just the name of that variable within the parenthesis () of print() function without any quotes. Like this:
print(a)
print(name)
print(is_it_fun)Data Types
Now we will understand the concept of data types.
Data types define what kind of value a variable can hold (like number, string, boolean etc.) and what operations on it are allowed. (We will discuss about opertors later in course.)
Basic data types in python
- int: This is data type for integer numbers (Example: ..., -3, -2, -1, 0, 1, 2, ...)
- float: This is data type for floating point numbers or numbers with decimal point (Example: 2.34, -5.5, 0.22, etc...)
- str: This is data type for strings - group of characters enclosed in quotes '' or "" (Example: "Hello", "Sarv", "Nice to meet you!")
- bool: Data type that is used to store boolean data (Example: True, False)
- list: Data type of a data structure called list.
- tuple: Data type of a data structure called tuple.
- dict: Data type of a data structure called dictionary.
- set: Data type of a data structure called set.
(We will discuss the data structures: lists, tuples, dictionaries, and sets later in this course.)
Checking data type of a variable
We can check which data type the variable has. By printing the data type using a built-in function type().
print( type(a) )
print( type(name) )
print( type(is_it_fun) )Dynamically Typed Lanugage: Python
Python is a dynamically typed language, which means we don't need to specify the data types when declaring the variables (like we have to do in languages like c, c++, java).
The interpreter of python automatically detects the data type based on the value it is containing, at the run-time. So, you don't have to worry, just create variable and assign whatever value you want.