Python Variables and Data Types Explained with Examples (Beginner's Guide 2026)
Python Variables and Data Types with Examples | Python Tutorial
Introduction
Variables are one of the most fundamental concepts in Python
programming. A variable stores data that can be used and modified throughout
your program. Python supports multiple built-in data types, making it easy to
work with numbers, text, collections, and boolean values.
In this tutorial, you'll learn what variables are, how to
declare them, different Python data types, naming rules, type conversion, and
practical examples suitable for beginners.
Table of Contents
- What
are Variables?
- Variable
Naming Rules
- Creating
Variables
- Multiple
Variable Assignment
- Python
Data Types
- Numeric
Data Types
- String
Data Type
- Boolean
Data Type
- List
- Tuple
- Dictionary
- Set
- Checking
Data Type using type()
- Type
Conversion
- Complete
Example
- Advantages
- Best
Practices
- Interview
Questions
- FAQs
- Conclusion
What are Variables?
A variable is a container used to store data values.
Unlike many programming languages, Python does not require
declaring the variable type.
Example
name = "John"
age = 25
salary = 35000.50
Python automatically determines the data type.
Variable Naming Rules
Variables should follow these rules:
✔ Start with a letter or
underscore
name = "John"
_age = 22
✔ Cannot start with a number
❌ Wrong
1name = "John"
✔ Only letters, numbers, and
underscore are allowed.
student_name
student1
✔ Variable names are
case-sensitive.
Name = "John"
name = "David"
Both are different variables.
Creating Variables
Example
city = "Mumbai"
marks = 95
height = 5.9
print(city)
print(marks)
print(height)
Output
Mumbai
95
5.9
Multiple Variable Assignment
Assign multiple variables at once.
x, y, z = 10, 20, 30
print(x)
print(y)
print(z)
Output
10
20
30
Assign Same Value
a = b = c = 100
print(a)
print(b)
print(c)
Output
100
100
100
Python Data Types
Python provides several built-in data types.
|
Data Type |
Description |
Example |
|
int |
Integer |
10 |
|
float |
Decimal Number |
10.5 |
|
complex |
Complex Number |
3+4j |
|
str |
Text/String |
"Hello" |
|
bool |
True/False |
True |
|
list |
Ordered Collection |
[1,2,3] |
|
tuple |
Immutable Collection |
(1,2,3) |
|
set |
Unique Values |
{1,2,3} |
|
dict |
Key-Value Pair |
{"name":"John"} |
Numeric Data Types
Integer (int)
age = 20
print(age)
Output
20
Float
price = 199.99
print(price)
Output
199.99
Complex
number = 5 + 7j
print(number)
Output
(5+7j)
String Data Type
Strings store text.
name = "Python"
print(name)
Output
Python
Boolean Data Type
Boolean stores only two values.
isPythonEasy = True
print(isPythonEasy)
Output
True
List
Lists are ordered and mutable.
fruits = ["Apple", "Mango",
"Banana"]
print(fruits)
Output
['Apple', 'Mango', 'Banana']
Tuple
Tuple is ordered but immutable.
colors = ("Red", "Blue",
"Green")
print(colors)
Output
('Red', 'Blue', 'Green')
Dictionary
Stores data as key-value pairs.
student = {
"name":
"John",
"age":
20,
"course": "Python"
}
print(student)
Output
{'name': 'John', 'age': 20, 'course': 'Python'}
Set
Stores unique values.
numbers = {1,2,3,4,5}
print(numbers)
Output
{1,2,3,4,5}
Checking Data Type using type()
x = 100
y = 15.5
z = "Python"
print(type(x))
print(type(y))
print(type(z))
Output
<class 'int'>
<class 'float'>
<class 'str'>
Type Conversion
Convert one data type into another.
age = "25"
print(int(age))
Output
25
Convert integer to string
number = 100
print(str(number))
Output
100
Example
name = "Amit"
age = 22
height = 5.8
isStudent = True
print("Name :", name)
print("Age :", age)
print("Height :", height)
print("Student :", isStudent)
print(type(name))
print(type(age))
print(type(height))
print(type(isStudent))
Output
Name : Amit
Age : 22
Height : 5.8
Student : True
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
Explanation
In this program:
- name
stores a string.
- age stores
an integer.
- height
stores a floating-point number.
- isStudent
stores a boolean value.
- type()
returns the data type of each variable.
Advantages of Variables and Data Types
- Easy
data storage
- Improves
code readability
- Supports
dynamic typing
- Multiple
built-in data types
- Easy
data manipulation
- Better
memory management
- Useful
for calculations and decision-making
- Makes
programs flexible and reusable
Best Practices
- Use
meaningful variable names.
- Follow
snake_case naming.
- Avoid
reserved keywords.
- Keep
variable names descriptive.
- Use
constants for fixed values.
- Choose
the correct data type.
- Convert
data types only when necessary.
Python Interview Questions
1. What is a variable in Python?
A variable stores data values in memory.
2. Does Python require variable declaration?
No.
3. What does type() do?
It returns the data type of a variable.
4. Difference between List and Tuple?
List is mutable, Tuple is immutable.
5. Difference between int and float?
- int
→ whole number
- float
→ decimal number
6. What is Boolean?
A data type containing True or False.
7. Which data type stores key-value pairs?
Dictionary.
8. Which data type stores unique values?
Set.
9. Can Python variables change data type?
Yes.
Example:
x = 10
x = "Hello"
10. What is dynamic typing?
Python automatically detects the variable's data type during
runtime.
Frequently Asked Questions (FAQs)
1. What is a variable in Python?
A variable is a name that stores a value in memory.
2. Which symbol is used to assign a value?
The assignment operator (=).
3. Is Python case-sensitive?
Yes, name and Name are different.
4. Can a variable store different data types?
Yes. Python is dynamically typed.
5. What are Python's main data types?
int, float, str, bool, list, tuple, set, dict, and complex.
6. What is the difference between list and set?
Lists preserve order and allow duplicates; sets store only
unique values and do not guarantee order.
7. What is a dictionary?
A collection of key-value pairs.
8. How do I check a variable's type?
Use the type() function.
9. What is type conversion?
Changing one data type into another using functions like
int(), float(), or str().
10. Why are variables important?
They allow programs to store, retrieve, and manipulate data
efficiently.
Conclusion
Python variables and data types are essential building blocks for every Python programmer. Understanding how to create variables, use different data types, and perform type conversion will help you write efficient and readable code. Mastering these concepts prepares you for more advanced topics like operators, conditional statements, loops, functions, and object-oriented programming.
Comments
Post a Comment