In Python, Variables are reserved memory locations used to store data. Unlike many other languages, Python is dynamically typed, meaning you don't need to declare the type of a variable when you create it; the interpreter infers the type based on the value you assign.
Theoretically, a Python variable is not a "container" that holds a value, but rather a label or a reference that points to an object in memory.
When you write x = 10, three things happen:
Python creates an integer object in memory with the value 10.
Python creates a name (variable) called x.
Python links the name x to that specific integer object.
Because variables are just labels, you can point a label to a completely different type of data later in the program.
data = 10 # 'data' points to an integer data = "Hello" # 'data' now points to a string
Theory: This flexibility makes Python very fast to write but requires the developer to be careful, as the "type" of a variable can change unexpectedly.
Python has several built-in data types categorized into groups:
int: Whole numbers (e.g., 5, -10). In Python 3, integers have arbitrary precision (they can be as large as your memory allows).
float: Numbers with decimal points (e.g., 3.14, -0.01).
complex: Complex numbers (e.g., 3 + 5j).
str (String): A sequence of Unicode characters (e.g., "Python").
list: An ordered, mutable (changeable) collection.
tuple: An ordered, immutable (unchangeable) collection.
dict (Dictionary): A collection of key-value pairs (e.g., {"name": "Alice"}).
set: An unordered collection of unique items.
bool: Represents True or False.
One of the most important theoretical concepts in Python is the difference between Mutable and Immutable objects.
Types like int, float, str, and tuple are immutable.
The Theory: If you have s = "Hello" and you try to change the first letter, Python won't allow it. Instead, if you do s = "Yello", Python creates a new string object and moves the label s to point to it. The old "Hello" object is eventually cleaned up by the Garbage Collector.
Types like list, dict, and set are mutable.
The Theory: If you have a list my_list = [1, 2], you can add a third item without creating a new list object. The object itself is modified in place.
The Identity: You can use the id() function to check the unique memory address of an object. For mutable objects, the id() remains the same after modification; for immutable objects, any "change" results in a new id().
# Variable Assignment age = 25 # int price = 19.99 # float name = "EMBLAb" # str is_active = True # bool # Checking types using type() print(type(age)) # <class 'int'> print(type(name)) # <class 'str'> # Mutability Demonstration my_list = [1, 2, 3] old_id = id(my_list) my_list.append(4) new_id = id(my_list) print(f"List: {my_list}") print(f"IDs match? {old_id == new_id}") # True: Modified in place
Theoretically, Python follows the PEP 8 style guide for naming variables:
Must start with a letter or underscore _.
Cannot start with a number.
Can only contain alphanumeric characters and underscores (A-z, 0-9, and _).
Snake Case: Use underscores to separate words for readability (e.g., user_registration_date).
| Data Type | Description | Mutable? | Example |
| int | Whole numbers. | No | 10 |
| float | Decimal numbers. | No | 10.5 |
| str | Textual data. | No | "Hi" |
| list | Ordered collection. | Yes | [1, 2] |
| dict | Key-Value pairs. | Yes | {"id": 1} |
| bool | Logical values. | No | True |
Copyright ©2025. All Rights Reserved Emblab THE RAVE INNOVATION