Python Variables, Identifiers, and Data Types – A Complete Beginner's Guide
Python Variables, Identifiers, and Data Types – A Complete Beginner's Guide
When you write your first Python program, three concepts are absolutely essential — variables, identifiers, and data types. These are the foundation of every program you will ever write. In this guide, you will learn what variables are, how to name them correctly using identifier rules, and how Python classifies different kinds of data — all explained with clear examples, outputs, and real-life scenarios.
Table of Contents
- What is a Variable in Python?
- How to Create a Variable
- Dynamic Typing in Python
- Multiple Assignment and Variable Swapping
- What is an Identifier?
- Rules for Naming Identifiers
- Good Naming Practices
- What are Data Types in Python?
- SVDT – Single Value Data Types
- MVDT – Multi Value Data Types
- How to Check a Data Type
- Quick Comparison – All Data Types
- FAQ
1. What is a Variable in Python?
A variable is a name given to a memory location where data is stored. Think of it like a labeled box — you put a value inside the box, give the box a name, and use that name whenever you need the value later.
In real life, you keep sugar in a jar and water in a bottle. In Python, you keep data in variables.
# Real-life data stored in variables name = "Rohan" age = 21 city = "Mandya" print(name) print(age) print(city)
Rohan 21 Mandya
=. There is no need to declare it first or mention its type — Python handles all of that automatically.2. How to Create a Variable
Creating a variable in Python is straightforward. Use the assignment operator = to store a value.
# Syntax: variable_name = value x = 10 price = 99.5 city = "Bengaluru" is_active = True
Using variables makes your code readable, reusable, and easy to change.
# Without variable – hardcoded, difficult to update print(500 * 12) print(500 + 100) # With variable – clear and reusable salary = 500 bonus = 100 print(salary * 12) print(salary + bonus)
6000 600 6000 600
3. Dynamic Typing in Python
Python is a dynamically typed language. This means you do not need to declare a variable's data type — Python figures it out automatically based on the value you assign. You can also reassign a variable to a completely different type without any error.
x = 10 print(x, type(x)) # int x = 3.14 print(x, type(x)) # float x = "Hello" print(x, type(x)) # str x = True print(x, type(x)) # bool
10 <class 'int'> 3.14 <class 'float'> Hello <class 'str'> True <class 'bool'>
int x = 10, Python simply writes x = 10 and handles the rest.4. Multiple Assignment and Variable Swapping
Python allows you to assign values to multiple variables in a single line, which makes your code cleaner.
# Assign same value to multiple variables a = b = c = 0 print(a, b, c) # Assign different values in one line x, y, z = 10, 20, 30 print(x, y, z) # Swap two variables without a temp variable a = 5 b = 10 a, b = b, a print(f"a = {a}, b = {b}")
0 0 0 10 20 30 a = 10, b = 5
a, b = b, a is a Python shortcut. In other languages, you need a temporary variable to do the same thing — Python does it in one clean line.5. What is an Identifier?
An identifier is the name you give to a variable, function, class, or any other element in Python. It helps Python know what you are referring to in your code. Without identifiers, Python would have no way to distinguish one piece of data from another.
age = 21 # age is the identifier total_marks = 450 # total_marks is the identifier def calculate_sum(): # calculate_sum is a function identifier pass class StudentData: # StudentData is a class identifier pass
6. Rules for Naming Identifiers
Python has specific rules for naming identifiers. Breaking these rules causes a SyntaxError or NameError. Learn these rules once and they will stay with you forever.
Must start with a letter or underscore
No special characters except _
No spaces in the name
Keywords cannot be identifiers
Python is case-sensitive
This is one of the most important rules — age, Age, and AGE are three completely different identifiers in Python.
age = 20 Age = 30 AGE = 40 print(age) # 20 print(Age) # 30 print(AGE) # 40
20 30 40
Name but are using name, Python treats them as two different variables.Side-by-side — valid vs invalid identifiers
1student = "Rohan" user-name = "test" total marks = 100 class = "Python" my@value = 5
student1 = "Rohan" user_name = "test" total_marks = 100 class_name = "Python" my_value = 5
7. Good Naming Practices
Beyond the rules, good programmers also follow conventions that make code easy to read and understand by others.
# Use descriptive names — not single letters for real variables s = 450 # Bad – what is s? student_marks = 450 # Good – clear meaning # Use snake_case for variables and functions total_price = 1200 calculate_gst = True # Use PascalCase for class names class StudentRecord: pass # Use UPPER_CASE for constants MAX_SPEED = 120 PI = 3.14159
snake_case for variables and functions, PascalCase for class names, and UPPER_CASE for constants. Following these makes your code professional and readable.8. What are Data Types in Python?
A data type tells Python what kind of value a variable is holding and what operations are allowed on it. You cannot add a number to a string directly — Python needs to know the type of data to handle it correctly.
age = 21 # int – whole number price = 99.5 # float – decimal number name = "Rohan" # str – text is_active = True # bool – True or False
Python classifies all data types into two broad categories:
Store only one value at a time int, float, complex, bool
Store multiple values in one variable str, list, tuple, set, dict
9. SVDT – Single Value Data Types
Single value data types store exactly one value at a time. Python has four SVDT types.
Integer
Whole numbers, positive or negative, no decimal
Float
Numbers with a decimal point
Complex
Numbers with real and imaginary parts
Boolean
Only two possible values — True or False
# int – whole numbers age = 21 marks = -5 print(age, type(age)) # float – decimal numbers price = 99.5 temperature = -2.3 print(price, type(price)) # complex – real + imaginary c = 2 + 3j print(c, type(c)) # bool – True or False is_logged_in = True is_paid = False print(is_logged_in, type(is_logged_in))
21 <class 'int'> 99.5 <class 'float'> (2+3j) <class 'complex'> True <class 'bool'>
bool is actually a subclass of int. That means True == 1 and False == 0 in Python. You can use them in arithmetic: True + True = 2.10. MVDT – Multi Value Data Types
Multi value data types can store multiple values inside a single variable. Each type has different properties — some are ordered, some allow duplicates, and some are mutable (changeable) while others are not.
String
Text — sequence of characters, immutable
List
Ordered, mutable, allows duplicates
Tuple
Ordered, immutable, allows duplicates
Set
Unordered, mutable, no duplicates
Dictionary
Key-value pairs, ordered (Python 3.7+)
# str – text name = "Rohan" print(name, type(name)) # list – ordered, changeable marks = [80, 85, 90, 80] print(marks, type(marks)) # tuple – ordered, cannot be changed coordinates = (10.5, 20.3) print(coordinates, type(coordinates)) # set – unique values only numbers = {1, 2, 2, 3, 3} print(numbers, type(numbers)) # dict – key-value pairs student = {"name": "Rohan", "age": 21, "marks": 90} print(student, type(student))
Rohan <class 'str'>
[80, 85, 90, 80] <class 'list'>
(10.5, 20.3) <class 'tuple'>
{1, 2, 3} <class 'set'>
{'name': 'Rohan', 'age': 21, 'marks': 90} <class 'dict'>{1, 2, 2, 3, 3} automatically removed the duplicates and stored only {1, 2, 3}. Sets never allow repeated values — this makes them useful for removing duplicates from a list.11. How to Check a Data Type
Use the built-in type() function to find out what data type a variable holds. This is especially useful when debugging or when you receive data from user input or external sources.
values = [10, 3.14, "Python", True, [1,2], (3,4), {5,6}, {"a":1}] for v in values: print(f"{str(v):15} → {type(v).__name__}")
10 → int
3.14 → float
Python → str
True → bool
[1, 2] → list
(3, 4) → tuple
{5, 6} → set
{'a': 1} → dict12. Quick Comparison – All Data Types
| Category | Type | Example | Mutable? | Allows Duplicates? |
|---|---|---|---|---|
| SVDT | int | 21 | No | — |
| SVDT | float | 99.5 | No | — |
| SVDT | complex | 2+3j | No | — |
| SVDT | bool | True | No | — |
| MVDT | str | "Python" | No | Yes |
| MVDT | list | [1, 2, 3] | Yes | Yes |
| MVDT | tuple | (1, 2, 3) | No | Yes |
| MVDT | set | {1, 2, 3} | Yes | No |
| MVDT | dict | {"a": 1} | Yes | Keys: No |
- A variable is a named memory location that stores a value
- Python is dynamically typed — no need to declare the type manually
- Multiple assignment allows
x, y, z = 1, 2, 3in one line - An identifier is the name given to a variable, function, or class
- Identifiers cannot start with a digit, contain spaces, or use keywords
- Python is case-sensitive —
age,Age, andAGEare three different variables - SVDT holds one value — int, float, complex, bool
- MVDT holds multiple values — str, list, tuple, set, dict
- Use
type()to check what data type any variable holds
Frequently Asked Questions (FAQ)
Q1. Do I need to declare a variable before using it in Python?
No. In Python, a variable is created the moment you assign a value to it. There is no separate declaration step. Just write age = 21 and the variable is ready to use.
Q2. What is the difference between a list and a tuple in Python?
Both store multiple ordered values. The key difference is mutability — a list is mutable (you can add, remove, or change items). A tuple is immutable (once created, its values cannot be changed). Use tuples for data that should not change, like coordinates.
Q3. Why does Python not require you to declare the data type?
Python uses dynamic typing — it infers the data type from the value you assign. When you write x = 10, Python automatically knows x is an integer. This makes Python faster to write and easier for beginners to learn.
Q4. What is the difference between a set and a list in Python?
A list is ordered and allows duplicate values. A set is unordered and automatically removes duplicates. Sets are faster for checking membership (using in), while lists are better when order and duplicates matter.
Q5. Can a variable name start with an underscore in Python?
Yes. A variable can start with an underscore, like _total or __name. Single underscore variables are often used for temporary or private values. Double underscore names have special meaning in Python classes.
Q6. What is the difference between a dictionary and a set in Python?
Both use curly braces {}, but they are different. A set stores individual unique values like {1, 2, 3}. A dictionary stores key-value pairs like {"name": "Rohan"}. An empty {} creates a dict, not a set — use set() for an empty set.
Found this post helpful? Share it with a friend who is just starting Python. Drop your questions in the comments — happy to help!
Nice explanation
ReplyDelete