This guide covers essential Python concepts with detailed explanations and code examples for Lists, Dictionaries, Loops, and Functions.
A list is an ordered, mutable (changeable) collection of elements. It can store multiple data types.
# Creating a list
fruits = ["apple", "banana", "cherry", "mango"]
# Mixed data types
random_list = [10, "hello", 3.14, True]
# Empty list
empty_list = []fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: apple (first element)
print(fruits[-1]) # Output: cherry (last element)fruits = ["apple", "banana", "cherry"]
# Changing an element
fruits[1] = "orange"
print(fruits) # Output: ['apple', 'orange', 'cherry']
# Adding elements
fruits.append("grapes") # Adds to the end
print(fruits)
fruits.insert(1, "blueberry") # Adds at index 1
print(fruits)
# Removing elements
fruits.remove("apple") # Removes by value
print(fruits)
deleted_item = fruits.pop(2) # Removes item at index 2
print(f"Removed: {deleted_item}")numbers = [0, 1, 2, 3, 4, 5, 6, 7]
print(numbers[2:5]) # Output: [2, 3, 4] (index 2 to 4)
print(numbers[:4]) # Output: [0, 1, 2, 3] (start to index 3)
print(numbers[3:]) # Output: [3, 4, 5, 6, 7] (index 3 to end)
print(numbers[::2]) # Output: [0, 2, 4, 6] (step of 2)A dictionary is an unordered collection of key-value pairs.
# Creating a dictionary
student = {
"name": "John",
"age": 20,
"grade": "A",
"subjects": ["Math", "Science"]
}
# Accessing values
print(student["name"]) # Output: John
print(student.get("age")) # Output: 20student["age"] = 21 # Modifying an existing key
student["city"] = "New York" # Adding a new key
print(student)del student["grade"] # Removes 'grade' key
print(student)
removed_value = student.pop("age") # Removes and returns the value
print(f"Removed Age: {removed_value}")for key, value in student.items():
print(f"{key}: {value}")Used to iterate over sequences like lists, strings, and dictionaries.
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(f"I like {fruit}") Looping with range()
for i in range(5): # Loops from 0 to 4
print(i)Looping Through a Dictionary
student = {"name": "John", "age": 20, "grade": "A"}
for key, value in student.items():
print(f"{key}: {value}")Used when the number of iterations is unknown.
x = 0
while x < 5:
print(f"x is {x}")
x += 1 # Incrementing xbreak: Stops the loop.continue: Skips the current iteration.
for num in range(10):
if num == 5:
break # Stops when num is 5
print(num)
for num in range(5):
if num == 2:
continue # Skips 2
print(num)A function is a reusable block of code that performs a specific task.
def greet():
print("Hello, welcome!")
greet() # Calling the functiondef greet_user(name):
print(f"Hello, {name}!")
greet_user("Alice") # Output: Hello, Alice!def add(a, b):
return a + b
result = add(3, 5)
print(result) # Output: 8def greet(name="Guest"):
print(f"Hello, {name}!")
greet() # Output: Hello, Guest!
greet("Alex") # Output: Hello, Alex!square = lambda x: x * x
print(square(4)) # Output: 16
add = lambda a, b: a + b
print(add(3, 7)) # Output: 10โ
Lists โ Ordered, mutable collection. Supports indexing, slicing, modification.
โ
Dictionaries โ Unordered, key-value pairs. Fast lookups.
โ
Loops โ for and while loops for iteration. Use break & continue.
โ
Functions โ Code reusability, parameters, return values, default arguments, lambda functions.