Mastering Python Variables and Data Types in Easy Steps
Python supports several built-in data types, and each occupies different memory space depending on the system architecture. Below is a breakdown of common data types, examples, and estimated memory usage.
In Python, variables are used to store data that can be referenced and manipulated in a program. Variables are automatically created when you assign a value—no explicit declaration is needed to reserve memory.

Example 1
#Declare a variable rakesh and assign a values 100
rakesh=100
#write a code for declaring and assigning a variables
age=32
height=6.6
name="rakesh"
is_student=True
print("age:",age)
print("height:",height)
print("Name:",name)
Variable Naming Convention
- variable names should be descriptive
- variable must start with a letter and contains letter, number and underscores .
- variable name case sensitive
Example 2
#valid variable names
first_name="rakesh"
last_name="kumar"
#Invalid variable name
2age=36
@name="rakesh"
#case sensitivity
name="rakesh"
Name="rakesh"
Understanding variables types
python is dynamically type of variable is determined at runtime
age=36
height=6.6
name="rakesh"
print(type(name))
#Type checking and conversion
type(height)
age=36
print(type(age))
#type conversion
age_str=str(age)
print(age_str)
print(type(age_str))
name="rakesh" # we can not convert in int because its completely string
int(name) # invalid literal for int() with "krish"
Dynamic typing
python allows the type of a variables to change as the program execute
var=10
print(var, type(var))
var="Hello"
print(var, type(var))
var=3.14
print(var, type(var))
Example : Write a program for addition , subtraction and multiplication
num1=36
num2=46
sum=num1+num2
difference = num2-num1
product= num1*num2
print("sum:",sum)
print("difference:",difference)
print("products:",products)
2. Data Types in python
Data Types are a classification of data which tell the compiler or interpreter how the programmer intends to use the data .determine the type of operations that can be performed on the data .the values that the data can take and the amount of memory needed to store the data .
Importance of Datatypes in programming
- Data types play a crucial role in optimizing storage efficiency by structuring and managing data in a way that maximizes performance and minimizes resource usage.
- Data type help in performing correct operations on data .
- By properly utilizing data types, developers can significantly enhance program stability. As a result, this minimizes errors and prevents potential bugs during execution, ultimately leading to more reliable and efficient software.
1️⃣ Integer (int
):
- The
int
type is used to store whole numbers, which means:- No decimals or fractions
- It can be positive (
25
), negative (-10
), or zero (0
)
Examples:
x = 100 # positive integer
y = -45 # negative integer
z = 0 # zero
x = 10 # Integer
print(type(x))
Memory and Performance Details:
🔸 Dynamic Memory Allocation:
- Python automatically adjusts the amount of memory used based on the size of the number.
- Unlike some languages (like C or Java) that use fixed sizes (
int32
,int64
), Python integers can grow very large, limited only by your computer’s memory.
🔸 Behind the Scenes:
- Internally, Python utilizes a sophisticated structure that not only stores the value but also incorporates metadata. As a result, this ensures efficient data management, improves accessibility, and enhances overall performance. Furthermore, this design enables seamless interactions between different data types, making Python a highly versatile programming language.
- So, a small integer like
5
might take around 28 bytes on a 64-bit system — much more than just 4 bytes like in C.
Check Memory Usage:
import sys
print(sys.getsizeof(x)) # Output: 28 bytes (varies
2️⃣ Float (float
)
A float
stores decimal numbers — numbers that have a fractional part.
- Example:
3.14
,-0.001
,10.0
Memory and Performance Details:
- Python uses 64 bits (or 8 bytes) to store a float internally.
- 1 bit is for the sign (positive or negative)
- 11 bits are for the exponent
Example:
y = 10.5 # Float
print(type(y)) # Output: <class 'float'>
#check memory
print(sys.getsizeof(y)) # Output: 24 bytes (varies)
#calculate body mass index
weight = 75.0 # in kilograms
height = 1.65 # in meters
bmi = weight / (height ** 2)
print(round(bmi, 2)) # Output: 27.55
3️⃣ String (str):
A string not only stores text but also holds words, sentences, names, or any sequence of characters. Additionally, it allows for efficient manipulation and retrieval, making it essential for handling textual data in programming.
In Python, strings are created using quotes:
name = "Rakesh"
greeting = 'Hello, world!'
Stores text characters (Unicode format)
- Python stores all text in Unicode format by default.
- Unicode is a standard that supports almost all characters and symbols from all languages (English, Hindi, Chinese, emojis, etc.).
Example:
city = "München" # German character ü
symbol = "₹" # Indian Rupee symbol
emoji = "😊"
Dynamic Size — depends on length
- Python strings can grow or shrink as needed.
- The memory used by a string is not fixed — it depends on:
- Number of characters
name = "Python"
print(type(name)) # Output: <class 'str'>
Check Memory Usage:
print(sys.getsizeof(name)) # Output: 55 bytes (depends on length)
4️⃣ Boolean (bool
)
A Boolean variable in Python can only store two possible values True or False .
True # Represents "Yes", "Correct", or "On"
False # Represents "No", "Wrong", or "Off"
Why use Booleans?
Boolean variables are used to:
- Make decisions in your code
- Control logic like
if
,while
, etc. - Check conditions (like comparisons)
Boolean Values in Memory
- In Python,
True
is internally represented as the number1
False
is represented as0
print(True + 1) # Output: 2
print(False + 5) # Output: 5
print(sys.getsizeof(fruits)) # Output: 80+ bytes (varies)