Variables & Data Types in Python
Variables
Definition: A variable is a named container that stores a value in memory.In Python, you do not need to declare the type — it is automatically inferred.
Rules for Variable Names:
- Must start with a letter or underscore(
_). - Cannot start with a number.
- Can contain letters, numbers, and underscores.
- Case-sensitive (
ageandAgeare different). - Cannot be a Python keyword (
if,for,class, etc.).
Naming Conventions:
| Convention | Style | Example | Used For |
|---|---|---|---|
| snake_case | lowercase with underscores | student_name | Variables, functions |
| PascalCase | Each word capitalized | StudentName | Classes |
| UPPER_CASE | All uppercase | MAX_SIZE | Constants |
---
Data Types in Python
Python has several built-in data types. Understanding them is critical because the type of data determines what operations can be performed on it.
Numeric Types
| Type | Description | Example |
|---|---|---|
| int | Integer (whole numbers) | x = 10 |
| float | Decimal numbers | y = 3.14 |
| complex | Complex numbers | z = 2 + 3j |
Text Type
| Type | Description | Example |
|---|---|---|
| str | String (text) | name = "Alice" |
String Operations:
- Concatenation:
"Hello" + " " + "World"→"Hello World" - Repetition:
"Ha" * 3→"HaHaHa" - Slicing:
"Python"[0:3]→"Pyt" - Length:
len("Hello")→5 - Methods:
.upper(),.lower(),.strip(),.split(),.replace()
Boolean Type
| Type | Values | Example |
|---|---|---|
| bool | True or False | is_active = True |
---
Collection Data Types
1. List
Definition: An ordered, mutable (changeable), indexed collection that allows duplicate elements.
| Feature | Detail |
|---|---|
| Syntax | my_list = [1, 2, 3, "hello"] |
| Ordered | Yes (maintains insertion order) |
| Mutable | Yes (elements can be changed) |
| Duplicates | Allowed |
| Indexing | Zero-based (my_list[0] = first element) |
Common Methods: .append(), .remove(), .pop(), .sort(), .reverse(), .extend()
2. Tuple
Definition: An ordered, immutable (unchangeable), indexed collection.
| Feature | Detail |
|---|---|
| Syntax | my_tuple = (1, 2, 3) |
| Ordered | Yes |
| Mutable | No (cannot be changed after creation) |
| Duplicates | Allowed |
| Use Case | Storing fixed data (e.g., coordinates, RGB colors) |
3. Dictionary
Definition: An unordered (Python 3.7+ ordered), mutable collection of key-value pairs.
| Feature | Detail |
|---|---|
| Syntax | my_dict = {"name": "Alice", "age": 25} |
| Ordered | Yes (from Python 3.7+) |
| Mutable | Yes |
| Duplicates | Keys must be unique; values can repeat |
| Access | By key: my_dict["name"] → "Alice" |
Common Methods: .keys(), .values(), .items(), .get(), .update(), .pop()
4. Set
Definition: An unordered, mutable collection of unique elements. No duplicates allowed.
| Feature | Detail |
|---|---|
| Syntax | my_set = {1, 2, 3} |
| Ordered | No |
| Mutable | Yes (can add/remove elements) |
| Duplicates | Not allowed |
| Use Case | Removing duplicates, set operations (union, intersection) |
---
Comprehensive Comparison Table
| Type | Ordered | Mutable | Duplicates | Syntax | Use Case |
|---|---|---|---|---|---|
| List | ✅ Yes | ✅ Yes | ✅ Yes | [1, 2, 3] | General-purpose collection |
| Tuple | ✅ Yes | ⌠No | ✅ Yes | (1, 2, 3) | Fixed/constant data |
| Dictionary | ✅ Yes | ✅ Yes | Keys: ⌠| {"a": 1} | Key-value mappings |
| Set | ⌠No | ✅ Yes | ⌠No | {1, 2, 3} | Unique collections |
---
Type Conversion (Casting)
| Function | Converts To | Example |
|---|---|---|
int() | Integer | int("10") → 10 |
float() | Float | float("3.14") → 3.14 |
str() | String | str(100) → "100" |
list() | List | list((1,2,3)) → [1, 2, 3] |
tuple() | Tuple | tuple([1,2,3]) → (1, 2, 3) |
set() | Set | set([1,1,2]) → {1, 2} |
Checking Type
Use the type() function to check a variable's data type: type(42) → <class 'int'> type("hello") → <class 'str'>
Summary
- Variables are dynamically typed in Python — no type declaration needed.
- Python has numeric (int, float, complex), text (str), boolean (bool), and collection types.
- Lists are mutable and ordered; Tuples are immutable and ordered.
- Dictionaries store key-value pairs; Sets store only unique values.
- Type conversion functions (
int(),str(), etc.) allow flexible data manipulation.