Python

Variables & Data Types in Python

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.

1. The Theory: Names vs. Memory Locations

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.

A. The Assignment Process

When you write x = 10, three things happen:

  1. Python creates an integer object in memory with the value 10.

  2. Python creates a name (variable) called x.

  3. Python links the name x to that specific integer object.

B. Dynamic Typing and Re-binding

Because variables are just labels, you can point a label to a completely different type of data later in the program.

Python
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.

2. Standard Data Types

Python has several built-in data types categorized into groups:

A. Numeric Types

  • 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).

B. Sequence Types

  • str (String): A sequence of Unicode characters (e.g., "Python").

  • list: An ordered, mutable (changeable) collection.

  • tuple: An ordered, immutable (unchangeable) collection.

C. Mapping & Set Types

  • dict (Dictionary): A collection of key-value pairs (e.g., {"name": "Alice"}).

  • set: An unordered collection of unique items.

D. Boolean Type

  • bool: Represents True or False.

3. Long Theory: Mutability and Identity

One of the most important theoretical concepts in Python is the difference between Mutable and Immutable objects.

A. Immutable Objects (Cannot Change)

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.

B. Mutable Objects (Can Change)

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().

4. Practical Code Example

Python
# 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

5. Naming Rules (Snake Case)

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).

6. Summary Table for EMBLAb

Data TypeDescriptionMutable?Example
intWhole numbers.No10
floatDecimal numbers.No10.5
strTextual data.No"Hi"
listOrdered collection.Yes[1, 2]
dictKey-Value pairs.Yes{"id": 1}
boolLogical values.NoTrue
Upcoming Course
Upcoming Course
Learn More
Instructor Tips
Instructor Tips
View Tips
Join Community
Join Community
Join Now