Until now you stored one value in one variable
What if you need to store 100 values?
You are not making 100 variables
That is what lists are for
This lecture covers:
Please open Telegram to view this post
VIEW IN TELEGRAM
A list is a collection of values stored in one variable
You create it with square brackets
fruits = ["apple", "banana", "mango"]
numbers = [1, 2, 3, 4, 5]
mixed = ["Ahmed", 22, True, 9.99] # lists can hold any types
Each item has an index — a position number starting from 0
fruits = ["apple", "banana", "mango"]
print(fruits[0]) # apple
print(fruits[1]) # banana
print(fruits[2]) # mango
print(fruits[-1]) # mango — negative index counts from the end
This trips up beginners — the first item is index 0, not 1
Always remember — lists start at 0
Please open Telegram to view this post
VIEW IN TELEGRAM
Updating an item:
fruits = ["apple", "banana", "mango"]
fruits[1] = "grape"
print(fruits) # ['apple', 'grape', 'mango']
Adding items:
fruits.append("orange") # adds to the end
fruits.insert(1, "kiwi") # adds at index 1Removing items:
fruits.remove("apple") # removes by value
fruits.pop() # removes last item
fruits.pop(0) # removes item at index 0
del fruits[2] # deletes item at index 2Checking if something is in a list:
if "mango" in fruits:
print("Found it")
Please open Telegram to view this post
VIEW IN TELEGRAM
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
print(len(numbers)) # 8 — how many items
print(sorted(numbers)) # [1, 1, 2, 3, 4, 5, 6, 9] — sorted copy
print(numbers.count(1)) # 2 — how many times 1 appears
print(sum(numbers)) # 31 — adds all numbers
print(min(numbers)) # 1
print(max(numbers)) # 9
numbers.reverse() # reverses in place
numbers.sort() # sorts in place
Slicing — getting a portion of a list:
fruits = ["apple", "banana", "mango", "grape", "kiwi"]
print(fruits[1:3]) # ['banana', 'mango'] — index 1 to 2
print(fruits[:3]) # ['apple', 'banana', 'mango'] — from start to 2
print(fruits[2:]) # ['mango', 'grape', 'kiwi'] — from index 2 to end
Please open Telegram to view this post
VIEW IN TELEGRAM
This is where lists and loops combine — you will do this constantly
users = ["Ahmed", "Sara", "Ali", "Fatima"]
for user in users:
print(f"Hello {user}!")
If you also need the index:
for index, user in enumerate(users):
print(f"{index + 1}. {user}")
Output:
1. Ahmed
2. Sara
3. Ali
4. Fatima
enumerate() is incredibly useful — remember it
Please open Telegram to view this post
VIEW IN TELEGRAM
A tuple is exactly like a list but you cannot change it after creating it
You create it with round brackets instead of square
coordinates = (25.2048, 55.2708) # latitude, longitude
rgb = (255, 0, 0) # red color
You can read from it the same way as a list:
print(coordinates[0]) # 25.2048
But you cannot change it:
coordinates[0] = 30 # ERROR — tuples are immutable
When to use a tuple vs a list:
Please open Telegram to view this post
VIEW IN TELEGRAM
Watch this after reading through all the posts
Python Full Course 2024 — freeCodeCamp
Covers lists, for loops with lists, and string concatenation
Please open Telegram to view this post
VIEW IN TELEGRAM
Build a simple to-do list program:
todos = []
todos.append("Learn Python")
todos.append("Build a Telegram bot")
todos.append("Deploy my first project")
print("Your To-Do List:")
for index, task in enumerate(todos):
print(f"{index + 1}. {task}")
print(f"Total tasks: {len(todos)}")
Then extend it — ask the user to add their own tasks using input() in a loop
Stop when they type "done"
Then print the full list
Screenshot your output
Bonus — let the user also delete a task by number
Please open Telegram to view this post
VIEW IN TELEGRAM
Lists store items by position — index 0, 1, 2...
But sometimes you need to store data by name
Like a real dictionary — look up a word, get its meaning
That is exactly what Python dictionaries do
This lecture covers:
Please open Telegram to view this post
VIEW IN TELEGRAM
A dictionary stores data as key-value pairs
Instead of an index number you use a key — a name you choose
user = {
"name": "Ahmed",
"age": 22,
"city": "Dubai",
"is_admin": False
}Accessing values by key:
print(user["name"]) # Ahmed
print(user["age"]) # 22
Safer way using .get() — returns None instead of crashing if key does not exist:
print(user.get("email")) # None
print(user.get("email", "N/A")) # N/A — default valuePlease open Telegram to view this post
VIEW IN TELEGRAM
Adding a new key:
user["email"] = "ahmed@gmail.com"
Updating an existing key:
user["age"] = 23
Deleting a key:
del user["city"]
user.pop("is_admin")
Checking if a key exists:
if "email" in user:
print("Has email")
print(len(user)) # number of keys
Please open Telegram to view this post
VIEW IN TELEGRAM
user = {"name": "Ahmed", "age": 22, "city": "Dubai"}
# loop through keys only
for key in user:
print(key)
# loop through values only
for value in user.values():
print(value)
# loop through both — most useful
for key, value in user.items():
print(f"{key}: {value}")Output of last loop:
name: Ahmed
age: 22
city: Dubai
Please open Telegram to view this post
VIEW IN TELEGRAM
Dictionaries can contain other dictionaries
This is how real user data is usually structured
users = {
"ahmed": {
"age": 22,
"is_admin": True
},
"sara": {
"age": 19,
"is_admin": False
}
}
print(users["ahmed"]["age"]) # 22
print(users["sara"]["is_admin"]) # FalseYou will see this exact pattern constantly when working with APIs and bots
Telegram sends you user data in this format
Please open Telegram to view this post
VIEW IN TELEGRAM
A set is like a list but:
tags = {"python", "bots", "coding", "python"} # duplicate python
print(tags) # {'python', 'bots', 'coding'} — duplicate removedMost common use case — removing duplicates from a list:
numbers = [1, 2, 2, 3, 3, 3, 4]
unique = list(set(numbers))
print(unique) # [1, 2, 3, 4]
Checking membership is also faster with sets than lists
Use sets when you need unique items and do not care about order
Please open Telegram to view this post
VIEW IN TELEGRAM
Watch this after reading through all the posts
Python Full Course 2024 — freeCodeCamp
Covers dictionaries, iterating over dictionaries, and associating values
Please open Telegram to view this post
VIEW IN TELEGRAM
Build a simple contacts manager using a dictionary:
contacts = {
"Ahmed": "050-1234567",
"Sara": "055-7654321",
"Ali": "052-1112233"
}
# print all contacts
for name, number in contacts.items():
print(f"{name}: {number}")
# search for a contact
search = input("Search name: ").strip().title()
if search in contacts:
print(f"Number: {contacts[search]}")
else:
print("Contact not found")Extend it — let the user add a new contact and delete one
Screenshot your output
Bonus — store each contact as a nested dict with number and email
Please open Telegram to view this post
VIEW IN TELEGRAM