Python Variables and Data Types
In Python, variables are used to store data values. Every program you write will use variables to store numbers, text, and other types of data. Understanding variables and data types is one of the most important steps in learning Python.
If you are new to Python, read: Python Syntax and First Program Explained before continuing.
What is a Variable in Python?
A variable is a name given to a value. Python automatically detects the data type when you assign a value. You do not need to declare the type explicitly.
name = "John"
age = 25
print(name)
print(age)
Output:
Rules for Naming Variables
- Variable names must start with a letter or underscore
- They cannot start with a number
- They are case-sensitive
- They cannot use reserved keywords (like if, for, while)
my_name = "Alice"
_age = 30
Python Data Types
Python has several built-in data types. The most commonly used are:
- int (Integer)
- float (Decimal numbers)
- str (String/Text)
- bool (Boolean)
- list
- tuple
- set
- dict (Dictionary)
1. Integer (int)
x = 10
y = -5
print(x)
print(y)
Output:
2. Float (Decimal Numbers)
price = 19.99
print(price)
Output:
3. String (str)
Strings are used to store text.
message = "Hello Python"
print(message)
Output:
4. Boolean (bool)
Boolean values represent True or False.
is_active = True
print(is_active)
Output:
Checking Data Type using type()
x = 5
print(type(x))
Output:
Type Casting in Python
Type casting converts one data type into another.
x = "10"
y = int(x)
print(y)
print(type(y))
Output:
Multiple Variable Assignment
a, b, c = 1, 2, 3
print(a, b, c)
Output:
Conclusion
Variables and data types are the building blocks of Python programming. Once you understand how to store data and identify data types, you are ready to move to the next step: Python Operators.
Next article: Python Operators Explained with Examples