This is Module 2, where we explore how Python stores and organizes information. Understanding data types is the foundation for building any complex application.
Python supports three distinct numerical types:
int: Whole numbers (e.g., 10, -5).float: Decimal numbers (e.g., 10.5, 3.14).complex: Numbers with a real and imaginary part.Example:
Python
x = 10 # int
y = 3.14 # float
z = 2 + 3j # complex (j represents √-1)
Strings are sequences of characters wrapped in quotes.
[start:stop:step]..upper(), .lower(), and .replace().Example:
Python
text = "Python"
print(text[0]) # 'P'
print(text[0:2]) # 'Py' (Up to but not including index 2)
print(text.upper()) # 'PYTHON'
Lists are ordered, mutable (changeable) collections that can hold different data types.
Example:
Python
fruits = ["apple", "banana"]
fruits.append("cherry") # Adds to the end
fruits[0] = "blueberry" # Changes "apple" to "blueberry"
print(fruits) # ['blueberry', 'banana', 'cherry']
Tuples are like lists, but they are immutable. Once created, you cannot change them. They are used for data that shouldn’t change, like coordinates or configuration settings.
Example:
Python
point = (10, 20)
# point[0] = 15 # This would cause an Error!
Sets are unordered collections of unique elements. They are great for removing duplicates.
Example:
Python
nums = {1, 2, 2, 3, 4}
print(nums) # Output: {1, 2, 3, 4} (Duplicates removed)
Dictionaries store data in Key-Value pairs. Think of a real dictionary: the word is the “key” and the definition is the “value.”
Example:
Python
user = {"name": "Alice", "age": 25}
print(user["name"]) # Output: Alice
This is a critical concept in Python. It defines whether an object’s value can be changed after it is created.
| Category | Data Types |
| Mutable (Can change) | List, Dictionary, Set |
| Immutable (Cannot change) | Int, Float, String, Tuple |
Python manages memory automatically using a Garbage Collector.