🧠 Python Datatypes (Lecture 5)
🎥 Reference
🟢 Introduction to Python Data Types
Python is dynamically typed, meaning you don't need to declare the data type
of a variable.
Data types determine the kind of operations that can be done on a value.
Python supports multiple built-in data types.
🔢 Numbers
Used to store numeric values.
Types:
o int: Whole numbers (e.g. 5)
o float: Decimal numbers (e.g. 3.14)
o complex: Complex numbers (e.g. 2 + 3j)
Python supports arbitrarily large integers.
Supports exponentiation using ** operator:
print(2 ** 100)
🧵 Strings
A sequence of characters enclosed in single (') or double (") quotes.
Immutable — cannot be changed after creation.
Support indexing, slicing, and negative indexing:
Code:
s = "Python"
print(s[0]) # 'P'
print(s[-1]) # 'n'
📃 Lists
A collection of items, mutable, ordered, and can contain elements of different
types.
Created using square brackets [].
Code:
my_list = [1, "hello", 3.5]
my_list[0] = 10 # You can modify items
🔐 Tuples
Like lists, but immutable — once created, their elements can’t be changed.
Use parentheses () to define:
Code:
my_tuple = (1, 2, "hi")
📖 Dictionaries
Unordered collection of key-value pairs.
Keys must be unique and immutable.
Created using curly braces {}:
Code:
my_dict = {"name": "Hassan", "age": 18}
print(my_dict["name"]) # Output: Hassan
🧠 Bonus Concepts
Python allows operations with large numbers, negative indices, and dynamic
typing.
Lists, tuples, and dictionaries are core to storing structured data in Python.
Strings and tuples are immutable; lists and dictionaries are mutable.