Python ยท Lesson 45

Python How-Tos, Examples, Exercises, Challenges, Syllabus, Study Plan & Interview Prep

47 phases  ยท  Build: Lesson 45 Project

๐Ÿ‘‹ Welcome to Lesson 45

Welcome to Lesson 45 ยท the grand consolidation lesson of your Python journey.

By now you have learned individual Python building blocks: variables, loops, functions, lists, dictionaries, and more. This lesson brings everything together.

Here is what you will accomplish in this lesson:

  • Learn how to solve common real-world Python problems (remove duplicates, reverse a string, add numbers)
  • Work through practical Python examples drawn from real usage
  • Complete beginner exercises to test your understanding
  • Take a quiz to identify what you truly know
  • Tackle challenges that stretch your thinking
  • Follow a structured Python syllabus ยท a complete map of Python topics
  • Use a study plan to learn Python in an organised schedule
  • Prepare for Python interview questions so you are job-ready
  • Understand bootcamp and training pathways so you know how to continue growing

๐Ÿ’ก This lesson assumes you know nothing. Every concept is taught from scratch. Even if you have seen some of these before, this lesson deepens your understanding through new examples and exercises.


๐Ÿ“š 47 phases๐Ÿ—๏ธ Lesson 45 Project๐Ÿ GitHub Repo
Phase 1 of 47
๐Ÿ“– Lesson Introduction

Welcome to Lesson 45 ยท the grand consolidation lesson of your Python journey.

By now you have learned individual Python building blocks: variables, loops, functions, lists, dictionaries, and more. This lesson brings everything together.

Here is what you will accomplish in this lesson:

  • Learn how to solve common real-world Python problems (remove duplicates, reverse a string, add numbers)
  • Work through practical Python examples drawn from real usage
  • Complete beginner exercises to test your understanding
  • Take a quiz to identify what you truly know
  • Tackle challenges that stretch your thinking
  • Follow a structured Python syllabus ยท a complete map of Python topics
  • Use a study plan to learn Python in an organised schedule
  • Prepare for Python interview questions so you are job-ready
  • Understand bootcamp and training pathways so you know how to continue growing

๐Ÿ’ก This lesson assumes you know nothing. Every concept is taught from scratch. Even if you have seen some of these before, this lesson deepens your understanding through new examples and exercises.


โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ“– Lesson Introduction. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 2 of 47
๐Ÿ”‘ Prerequisite Concepts

Before diving in, make sure you understand these terms. They are explained simply below.

TermSimple Meaning
VariableA named box that stores a value, like name = "Alice"
ListAn ordered collection of items, like [1, 2, 3]
StringA piece of text surrounded by quotes, like "hello"
LoopA set of instructions that repeats, like for x in list:
FunctionA reusable block of code given a name, like def greet():
IntegerA whole number like 5 or 100
PrintThe command that displays output on screen: print("hello")

If any of these feel shaky, don't worry ยท each one is demonstrated freshly throughout this lesson.


# Part 1: Python How-To Guides

These "how-to" sections answer the most common practical Python questions beginners ask.


โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ”‘ Prerequisite Concepts. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 3 of 47
๐Ÿ”ท How-To 1: Remove Duplicates from a List

What is a duplicate?

A duplicate is an item that appears more than once in a list.

Example:

code
fruits = ["apple", "banana", "apple", "cherry", "banana"]

Here, "apple" appears twice and "banana" appears twice. Those are duplicates.

Why remove duplicates?

In real-world data ยท like survey responses, sensor readings, or product lists ยท duplicates often appear by mistake. Removing them gives you a clean, unique set of values.

Real-world analogy: Imagine a sign-up sheet where the same person signed their name three times. You only need to count them once. Removing duplicates is like counting each person only once.


Method 1: Using a set()

A set in Python is a collection that automatically removes duplicates. Sets do not allow repeated values.

python
# Step 1: Start with a list that has duplicates
my_list = [1, 2, 3, 2, 1, 4, 5, 3]

# Step 2: Convert it to a set (duplicates disappear automatically)
unique_set = set(my_list)

# Step 3: Convert it back to a list if you need a list
unique_list = list(unique_set)

print(unique_list)

Expected Output:

code
[1, 2, 3, 4, 5]

โš ๏ธ Important: Sets are unordered. The output may appear in a different sequence each time you run the program. If order matters, use Method 2 below.


Breaking Down the Code Line by Line

LineWhat it does
my_list = [1, 2, 3, 2, 1, 4, 5, 3]Creates a list with some duplicate numbers
unique_set = set(my_list)Converts the list to a set ยท Python automatically discards duplicates
unique_list = list(unique_set)Converts the set back to a list so you can use list features again
print(unique_list)Displays the result on screen

Method 2: Using a Loop (Preserves Order)

If the order of items matters, use a loop to manually build a new list without duplicates.

python
# Original list with duplicates
my_list = ["banana", "apple", "banana", "cherry", "apple"]

# New empty list to collect unique items
unique_list = []

# Go through each item in the original list
for item in my_list:
    # Only add the item if it's NOT already in unique_list
    if item not in unique_list:
        unique_list.append(item)

print(unique_list)

Expected Output:

code
['banana', 'apple', 'cherry']

Notice the order is preserved ยท "banana" comes first because it appeared first.


Line-by-Line Explanation

LineWhat it does
unique_list = []Creates an empty list that will hold results
for item in my_list:Loops through every item in the original list one by one
if item not in unique_list:Checks if this item has already been added
unique_list.append(item)If not already added, add it now
print(unique_list)Shows the final deduplicated list

Method 3: Using dict.fromkeys() (Preserves Order, Very Compact)

python
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]

unique_list = list(dict.fromkeys(my_list))

print(unique_list)

Expected Output:

code
[3, 1, 4, 5, 9, 2, 6]

dict.fromkeys() creates a dictionary from the list (dictionaries can't have duplicate keys), then list() converts it back. This is a clean, Pythonic one-liner.


๐Ÿค” Thinking Prompt

What happens if you run set(["a", "a", "b"]) ยท how many items will be in the result? Why?


๐Ÿ” Quick Recap ยท Remove Duplicates

MethodPreserves Order?Best For
set()โŒ NoWhen order doesn't matter
Loop with not inโœ… YesWhen order matters
dict.fromkeys()โœ… YesCompact, readable code

โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ”ท How-To 1: Remove Duplicates from a List. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 4 of 47
๐Ÿ”ท How-To 2: Reverse a String

What does "reverse a string" mean?

It means flipping the text so the last character becomes the first.

  • Original: "hello"
  • Reversed: "olleh"

Why would you reverse a string?

  • Checking if a word is a palindrome (reads the same forwards and backwards, like "racecar")
  • Text processing and data manipulation
  • Puzzle and interview challenges
  • Game mechanics (secret codes, encryption basics)

Method 1: Slicing (The Pythonic Way)

Python's slice notation lets you extract parts of a string using [start:stop:step].

To reverse: use [::-1] ยท which means "start from end, go backwards, step by -1."

python
# Original string
text = "Hello, World!"

# Reverse using slicing
reversed_text = text[::-1]

print(reversed_text)

Expected Output:

code
!dlroW ,olleH

Understanding [::-1] Step by Step

Think of a string as a row of numbered seats:

code
H  e  l  l  o
0  1  2  3  4
  • [start:stop:step] = [::-1]
  • No start โ†’ begin from wherever the step takes you (the end, since step is negative)
  • No stop โ†’ go all the way to the other end
  • -1 step โ†’ move one position backwards each time

So Python visits: o, l, l, e, H โ†’ producing "olleH".


Method 2: Using the reversed() Function

python
text = "Python"

# reversed() returns an iterator, join() combines the characters
reversed_text = "".join(reversed(text))

print(reversed_text)

Expected Output:

code
nohtyP
  • reversed(text) ยท creates a reverse iterator over the characters
  • "".join(...) ยท joins all the characters together with no separator between them

Method 3: Using a Loop (Good for Understanding)

python
text = "science"
reversed_text = ""

for char in text:
    reversed_text = char + reversed_text  # each new char goes to the FRONT

print(reversed_text)

Expected Output:

code
ecneics

Each loop iteration prepends the current character to the front of reversed_text.


Palindrome Check ยท Practical Application

python
word = "racecar"

if word == word[::-1]:
    print(word, "is a palindrome!")
else:
    print(word, "is NOT a palindrome.")

Expected Output:

code
racecar is a palindrome!

Try it with "python" ยท what do you get?


๐Ÿค” Thinking Prompt

What is "12345"[::-1]? Write it out by hand first, then test it.


โœ๏ธ Your Task
- Game mechanics (secret codes, encryption basics) ยท
Phase 5 of 47
๐Ÿ”ท How-To 3: Add Two Numbers

What does "add two numbers" mean in Python?

This is the simplest form of arithmetic in Python. You use the + operator.

python
x = 5
y = 3
result = x + y
print(result)

Expected Output:

code
8

Simple! But let's go deeper.


Adding Numbers Entered by the User

In real programs, you usually don't know the numbers in advance ยท the user types them in.

python
# Ask the user to type two numbers
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")

# IMPORTANT: input() always returns a STRING, not a number
# We must convert to int (whole number) or float (decimal)
sum_result = int(num1) + int(num2)

print("The sum is:", sum_result)

Example Interaction:

code
Enter first number: 12
Enter second number: 7
The sum is: 19

Why Must We Convert with int()?

When a user types "5" on the keyboard, Python stores it as the text "5", not the number 5.

Text + Text in Python does concatenation (joining), not addition:

python
print("5" + "3")   # Output: 53  โ† wrong! This joins strings
print(5 + 3)       # Output: 8   โ† correct! This adds numbers

So int("5") converts the text "5" into the number 5.


Adding Floating-Point (Decimal) Numbers

python
a = 4.5
b = 2.3

total = a + b
print("Total:", total)

Expected Output:

code
Total: 6.8

Use float() instead of int() when decimals are needed:

python
num1 = float(input("Enter first decimal: "))
num2 = float(input("Enter second decimal: "))
print("Sum:", num1 + num2)

Adding Numbers in a List

Sometimes you want to add all numbers in a collection:

python
numbers = [10, 20, 30, 40, 50]
total = sum(numbers)
print("Total:", total)

Expected Output:

code
Total: 150

sum() is a built-in Python function that adds all values in a list.


# Part 2: Python Examples ยท Core Concepts Demonstrated

This section covers the most important Python examples that every beginner must know. These are drawn directly from real Python practice and professional usage.


โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ”ท How-To 3: Add Two Numbers. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 6 of 47
๐Ÿ”ท Example Category 1: Print and Variables

Printing Text

python
print("Hello, World!")

Output:

code
Hello, World!

Storing and Using Variables

python
name = "Alice"
age = 25
city = "London"

print(name, "is", age, "years old and lives in", city)

Output:

code
Alice is 25 years old and lives in London

Multiple Data Types

python
text = "Python"          # String
whole_number = 42        # Integer
decimal = 3.14           # Float
is_fun = True            # Boolean

print(type(text))
print(type(whole_number))
print(type(decimal))
print(type(is_fun))

Output:

code
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>

โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ”ท Example Category 1: Print and Variables. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 7 of 47
๐Ÿ”ท Example Category 2: String Operations

String Length

python
sentence = "Hello, Python!"
print(len(sentence))

Output:

code
14

Uppercase and Lowercase

python
word = "Python"
print(word.upper())
print(word.lower())

Output:

code
PYTHON
python

String Slicing

python
text = "Programming"
print(text[0:4])    # First 4 characters
print(text[-3:])    # Last 3 characters

Output:

code
Prog
ing

String Replacement

python
sentence = "I love Java"
new_sentence = sentence.replace("Java", "Python")
print(new_sentence)

Output:

code
I love Python

String Split

python
data = "Alice,Bob,Charlie,Diana"
names = data.split(",")
print(names)

Output:

code
['Alice', 'Bob', 'Charlie', 'Diana']

โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ”ท Example Category 2: String Operations. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 8 of 47
๐Ÿ”ท Example Category 3: Lists

Create and Access a List

python
fruits = ["apple", "banana", "cherry"]
print(fruits[0])    # First item
print(fruits[-1])   # Last item
print(fruits[1:3])  # Slice: items at index 1 and 2

Output:

code
apple
cherry
['banana', 'cherry']

Add, Remove, Sort

python
numbers = [3, 1, 4, 1, 5, 9, 2, 6]

numbers.append(7)        # Add to end
numbers.remove(1)        # Remove first occurrence of 1
numbers.sort()           # Sort ascending

print(numbers)

Output:

code
[1, 2, 3, 4, 5, 6, 7, 9]

Loop Through a List

python
colours = ["red", "green", "blue"]

for colour in colours:
    print("Colour:", colour)

Output:

code
Colour: red
Colour: green
Colour: blue

โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ”ท Example Category 3: Lists. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 9 of 47
๐Ÿ”ท Example Category 4: Dictionaries

Create and Access

python
student = {
    "name": "Bob",
    "age": 20,
    "grade": "A"
}

print(student["name"])
print(student["grade"])

Output:

code
Bob
A

Loop Through a Dictionary

python
person = {"name": "Carol", "city": "Paris", "job": "Engineer"}

for key, value in person.items():
    print(key, "โ†’", value)

Output:

code
name โ†’ Carol
city โ†’ Paris
job โ†’ Engineer

โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ”ท Example Category 4: Dictionaries. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 10 of 47
๐Ÿ”ท Example Category 5: Conditionals (if/elif/else)

Basic If-Else

python
temperature = 35

if temperature > 30:
    print("It's hot outside!")
elif temperature > 20:
    print("It's warm.")
else:
    print("It's cool.")

Output:

code
It's hot outside!

Grade Calculator

python
score = 78

if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
elif score >= 60:
    grade = "D"
else:
    grade = "F"

print("Grade:", grade)

Output:

code
Grade: C

โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ”ท Example Category 5: Conditionals (if/elif/else). Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 11 of 47
๐Ÿ”ท Example Category 6: Loops

for Loop with range()

python
for i in range(1, 6):
    print("Step", i)

Output:

code
Step 1
Step 2
Step 3
Step 4
Step 5

while Loop

python
count = 0

while count < 5:
    print("Count:", count)
    count += 1  # count = count + 1

Output:

code
Count: 0
Count: 1
Count: 2
Count: 3
Count: 4

Loop with break and continue

python
for num in range(1, 11):
    if num == 5:
        continue       # Skip number 5
    if num == 8:
        break          # Stop at number 8
    print(num)

Output:

code
1
2
3
4
6
7

โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ”ท Example Category 6: Loops. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 12 of 47
๐Ÿ”ท Example Category 7: Functions

Basic Function

python
def greet(name):
    print("Hello,", name + "!")

greet("Alice")
greet("Bob")

Output:

code
Hello, Alice!
Hello, Bob!

Function with Return Value

python
def add(a, b):
    return a + b

result = add(10, 25)
print("Sum:", result)

Output:

code
Sum: 35

Function with Default Parameter

python
def greet(name, message="Welcome!"):
    print(name + ":", message)

greet("Alice")
greet("Bob", "Nice to meet you!")

Output:

code
Alice: Welcome!
Bob: Nice to meet you!

โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ”ท Example Category 7: Functions. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 13 of 47
๐Ÿ”ท Example Category 8: Lambda Functions

A lambda is a small, anonymous (nameless) one-line function.

python
# Regular function
def square(x):
    return x * x

# Equivalent lambda
square_lambda = lambda x: x * x

print(square(5))         # Output: 25
print(square_lambda(5))  # Output: 25

Lambdas are useful when you need a quick, short function without defining a full def block.


โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ”ท Example Category 8: Lambda Functions. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 14 of 47
๐Ÿ”ท Example Category 9: Classes and Objects

Define a Class

python
class Dog:
    def __init__(self, name, breed):
        self.name = name
        self.breed = breed

    def bark(self):
        print(self.name, "says: Woof!")

# Create objects from the class
dog1 = Dog("Rex", "Labrador")
dog2 = Dog("Bella", "Poodle")

dog1.bark()
dog2.bark()
print(dog1.breed)

Output:

code
Rex says: Woof!
Bella says: Woof!
Labrador

โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ”ท Example Category 9: Classes and Objects. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 15 of 47
๐Ÿ”ท Example Category 10: File Handling

Write to a File

python
with open("notes.txt", "w") as file:
    file.write("Hello, this is a note.\n")
    file.write("Python file handling is easy!")

Read from a File

python
with open("notes.txt", "r") as file:
    content = file.read()
    print(content)

Output:

code
Hello, this is a note.
Python file handling is easy!

# Part 3: Python Exercises

Exercises are how you truly learn. Reading is not enough ยท you must write code yourself.

The following exercises are arranged by difficulty: beginner โ†’ intermediate โ†’ applied.


โœ๏ธ Your Task
Exercises are how you truly learn. Reading is not enough ยท you must write code yourself. The following exercises are arranged by difficulty: beginner โ†’ intermediate โ†’ applied. ยท
Phase 16 of 47
โœ๏ธ Exercise Set 1: Variables and Print

Exercise 1.1 ยท Store and Display Your Info

Objective: Create variables for your name, age, and favourite colour and print them in a sentence.

Expected Output:

code
My name is Sam, I am 22 years old, and my favourite colour is blue.

Starter Template:

python
name = ___
age = ___
colour = ___

print("My name is", ___, "I am", ___, "years old, and my favourite colour is", ___ + ".")

Solution:

python
name = "Sam"
age = 22
colour = "blue"

print("My name is", name + ", I am", age, "years old, and my favourite colour is", colour + ".")

Exercise 1.2 ยท Calculate Your Birth Year

python
current_year = 2025
age = int(input("How old are you? "))
birth_year = current_year - age
print("You were probably born in", birth_year)

Example Interaction:

code
How old are you? 20
You were probably born in 2005

โœ๏ธ Your Task
`python current_year = 2025 age = int(input("How old are you? ")) birth_year = current_year - age print("You were probably born in", birth_year) ` Example Interaction: ` How old are you? 20 You were probably born in 2005 ` ยท
Phase 17 of 47
โœ๏ธ Exercise Set 2: String Exercises

Exercise 2.1 ยท Reverse and Check Palindrome

Task: Ask the user to enter a word. Check if it is a palindrome.

python
word = input("Enter a word: ").lower()

if word == word[::-1]:
    print(word, "is a palindrome.")
else:
    print(word, "is not a palindrome.")

Example 1:

code
Enter a word: level
level is a palindrome.

Example 2:

code
Enter a word: hello
hello is not a palindrome.

Exercise 2.2 ยท Count Vowels in a String

python
text = input("Enter a sentence: ")
vowels = "aeiouAEIOU"
count = 0

for char in text:
    if char in vowels:
        count += 1

print("Number of vowels:", count)

Example:

code
Enter a sentence: Hello World
Number of vowels: 3

โœ๏ธ Your Task
Task: Ask the user to enter a word. Check if it is a palindrome. `python word = input("Enter a word: ").lower() if word == word[::-1]: print(word, "is a palindrome.") else: print(word, "is not a palindrome.") ` Example 1: ` Enter a word: level level is a palindrome. ` Example 2: ` Enter a word: hello hello is not a palindrome. ` ยท
Phase 18 of 47
โœ๏ธ Exercise Set 3: List Exercises

Exercise 3.1 ยท Find the Largest Number

python
numbers = [45, 12, 78, 34, 90, 23]

largest = numbers[0]

for num in numbers:
    if num > largest:
        largest = num

print("Largest number:", largest)

Output:

code
Largest number: 90

Exercise 3.2 ยท Remove Duplicates and Sort

python
data = [5, 3, 8, 3, 1, 5, 9, 1, 7]

unique_data = list(set(data))
unique_data.sort()

print("Cleaned and sorted:", unique_data)

Output:

code
Cleaned and sorted: [1, 3, 5, 7, 8, 9]

Exercise 3.3 ยท Sum and Average of a List

python
scores = [85, 92, 76, 88, 91, 67, 73]

total = sum(scores)
average = total / len(scores)

print("Total:", total)
print("Average:", round(average, 2))

Output:

code
Total: 572
Average: 81.71

โœ๏ธ Your Task
`python numbers = [45, 12, 78, 34, 90, 23] largest = numbers[0] for num in numbers: if num > largest: largest = num print("Largest number:", largest) ` Output: ` Largest number: 90 ` ยท
Phase 19 of 47
โœ๏ธ Exercise Set 4: Loop Exercises

Exercise 4.1 ยท Multiplication Table

python
number = int(input("Enter a number for its multiplication table: "))

for i in range(1, 11):
    print(number, "x", i, "=", number * i)

Example (number = 7):

code
7 x 1 = 7
7 x 2 = 14
...
7 x 10 = 70

Exercise 4.2 ยท FizzBuzz (Classic Interview Challenge)

Rules:

  • For multiples of 3, print "Fizz"
  • For multiples of 5, print "Buzz"
  • For multiples of both, print "FizzBuzz"
  • Otherwise, print the number
python
for i in range(1, 31):
    if i % 3 == 0 and i % 5 == 0:
        print("FizzBuzz")
    elif i % 3 == 0:
        print("Fizz")
    elif i % 5 == 0:
        print("Buzz")
    else:
        print(i)

Output (first 15 lines):

code
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz

โœ๏ธ Your Task
`python number = int(input("Enter a number for its multiplication table: ")) for i in range(1, 11): print(number, "x", i, "=", number * i) ` Example (number = 7): ` 7 x 1 = 7 7 x 2 = 14 ... 7 x 10 = 70 ` ยท
Phase 20 of 47
โœ๏ธ Exercise Set 5: Function Exercises

Exercise 5.1 ยท Temperature Converter

python
def celsius_to_fahrenheit(celsius):
    return (celsius * 9/5) + 32

def fahrenheit_to_celsius(fahrenheit):
    return (fahrenheit - 32) * 5/9

print(celsius_to_fahrenheit(100))   # Expected: 212.0
print(fahrenheit_to_celsius(32))    # Expected: 0.0

Exercise 5.2 ยท Simple Calculator Function

python
def calculate(a, b, operation):
    if operation == "add":
        return a + b
    elif operation == "subtract":
        return a - b
    elif operation == "multiply":
        return a * b
    elif operation == "divide":
        if b != 0:
            return a / b
        else:
            return "Error: Cannot divide by zero"
    else:
        return "Unknown operation"

print(calculate(10, 5, "add"))       # 15
print(calculate(10, 5, "subtract"))  # 5
print(calculate(10, 5, "multiply"))  # 50
print(calculate(10, 5, "divide"))    # 2.0
print(calculate(10, 0, "divide"))    # Error: Cannot divide by zero

# Part 4: Python Quiz ยท Test Your Knowledge

This quiz tests key Python concepts. Try answering each question before revealing the answer.


Quiz Question 1

What is the output of this code?

python
x = 10
y = 3
print(x % y)

<details> <summary>Click to reveal answer</summary>

Answer: 1

The % operator is the modulus (remainder) operator. 10 รท 3 = 3 remainder 1. So 10 % 3 = 1. </details>


Quiz Question 2

What does this code print?

python
text = "Hello"
print(text[1])

<details> <summary>Click to reveal answer</summary>

Answer: e

Python uses zero-based indexing. Index 0 = H, index 1 = e. </details>


Quiz Question 3

What is the output?

python
numbers = [1, 2, 3, 4, 5]
print(numbers[-1])

<details> <summary>Click to reveal answer</summary>

Answer: 5

Negative indexing counts from the end. -1 refers to the last element, which is 5. </details>


Quiz Question 4

What does len("Python") return?

<details> <summary>Click to reveal answer</summary>

Answer: 6

len() counts characters. P-y-t-h-o-n = 6 characters. </details>


Quiz Question 5

What is the output?

python
for i in range(0, 10, 2):
    print(i)

<details> <summary>Click to reveal answer</summary>

Answer:

code
0
2
4
6
8

range(0, 10, 2) starts at 0, ends before 10, and steps by 2 (every other number). </details>


Quiz Question 6

True or False: In Python, a list can hold items of different data types.

<details> <summary>Click to reveal answer</summary>

Answer: TRUE

A Python list can contain strings, integers, floats, booleans, and even other lists:

python
mixed = [1, "hello", 3.14, True]

</details>


Quiz Question 7

What is the output?

python
def mystery(n):
    return n * 2 + 1

print(mystery(4))

<details> <summary>Click to reveal answer</summary>

Answer: 9

mystery(4) = 4 * 2 + 1 = 8 + 1 = 9. </details>


Quiz Question 8

What does type(3.14) return?

<details> <summary>Click to reveal answer</summary>

Answer: <class 'float'>

3.14 has a decimal point, making it a floating-point number (float). </details>


Quiz Question 9

What is the output?

python
my_dict = {"a": 1, "b": 2, "c": 3}
print(my_dict["b"])

<details> <summary>Click to reveal answer</summary>

Answer: 2

Dictionary values are accessed using their key. The key "b" maps to the value 2. </details>


Quiz Question 10

Which of these correctly defines a Python function?

code
A) function greet():
B) def greet():
C) define greet():
D) func greet():

<details> <summary>Click to reveal answer</summary>

Answer: B) def greet():

In Python, functions are defined using the keyword def. </details>


# Part 5: Python Challenges

Challenges are harder problems that require you to combine multiple concepts. These simulate real interview and problem-solving scenarios.


โœ๏ธ Your Task
`python def celsius_to_fahrenheit(celsius): return (celsius * 9/5) + 32 def fahrenheit_to_celsius(fahrenheit): return (fahrenheit - 32) * 5/9 print(celsius_to_fahrenheit(100)) # Expected: 212.0 print(fahrenheit_to_celsius(32)) # Expected: 0.0 ` ยท
Phase 21 of 47
๐Ÿ† Challenge 1: Count Word Frequency

Problem: Given a sentence, count how many times each word appears.

python
sentence = "the cat sat on the mat the cat"

words = sentence.split()
frequency = {}

for word in words:
    if word in frequency:
        frequency[word] += 1
    else:
        frequency[word] = 1

for word, count in frequency.items():
    print(word, ":", count)

Output:

code
the : 3
cat : 2
sat : 1
on : 1
mat : 1

โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ† Challenge 1: Count Word Frequency. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 22 of 47
๐Ÿ† Challenge 2: Fibonacci Sequence

Problem: Print the first n numbers of the Fibonacci sequence.

What is Fibonacci? Each number is the sum of the two before it: 0, 1, 1, 2, 3, 5, 8, 13, 21...

python
def fibonacci(n):
    a, b = 0, 1
    result = []
    for _ in range(n):
        result.append(a)
        a, b = b, a + b
    return result

print(fibonacci(10))

Output:

code
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ† Challenge 2: Fibonacci Sequence. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 23 of 47
๐Ÿ† Challenge 3: Check Prime Number

Problem: Write a function that returns True if a number is prime, otherwise False.

Prime number: A number divisible only by 1 and itself (e.g., 2, 3, 5, 7, 11).

python
def is_prime(n):
    if n < 2:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

# Test it
for num in range(2, 20):
    if is_prime(num):
        print(num, "is prime")

Output:

code
2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime

โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ† Challenge 3: Check Prime Number. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 24 of 47
๐Ÿ† Challenge 4: Flatten a Nested List

Problem: Given a list containing sublists, create a single flat list.

python
nested = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]

flat = []
for sublist in nested:
    for item in sublist:
        flat.append(item)

print(flat)

Output:

code
[1, 2, 3, 4, 5, 6, 7, 8, 9]

One-liner using list comprehension:

python
flat = [item for sublist in nested for item in sublist]
print(flat)

โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ† Challenge 4: Flatten a Nested List. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 25 of 47
๐Ÿ† Challenge 5: Anagram Checker

Problem: Check if two words are anagrams (contain the same letters).

python
def are_anagrams(word1, word2):
    return sorted(word1.lower()) == sorted(word2.lower())

print(are_anagrams("listen", "silent"))   # True
print(are_anagrams("hello", "world"))     # False
print(are_anagrams("Triangle", "Integral"))  # True

Output:

code
True
False
True

sorted() converts a string to a sorted list of characters. If two words have the same characters in the same frequency, their sorted forms are identical.


โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ† Challenge 5: Anagram Checker. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 26 of 47
๐Ÿ† Challenge 6: Find Missing Number

Problem: Given a list of numbers from 1 to n with one missing, find the missing number.

python
def find_missing(numbers, n):
    expected_sum = n * (n + 1) // 2
    actual_sum = sum(numbers)
    return expected_sum - actual_sum

nums = [1, 2, 4, 5, 6, 7, 8, 9, 10]
print("Missing number:", find_missing(nums, 10))

Output:

code
Missing number: 3

How it works: The formula n*(n+1)/2 gives the sum of all numbers from 1 to n. Subtract the actual sum to find the gap.


# Part 6: Python Practice ยท Building Real Skills

Practice is deliberate repetition with feedback. This section gives you structured practice tasks.


โœ๏ธ Your Task
Practice is deliberate repetition with feedback. This section gives you structured practice tasks. ยท
Phase 27 of 47
๐Ÿ“ Practice Task 1: Student Grade Tracker

Scenario: You are building a simple grade-tracking tool for a teacher.

python
students = {}

# Add students and their scores
students["Alice"] = [85, 92, 78, 90]
students["Bob"] = [70, 65, 80, 75]
students["Carol"] = [95, 98, 92, 97]

# Calculate and display averages
for name, scores in students.items():
    average = sum(scores) / len(scores)
    print(name + ":", round(average, 1), "average")

Output:

code
Alice: 86.2 average
Bob: 72.5 average
Carol: 95.5 average

Enhancement: Add a grade letter (A/B/C/D/F) based on the average.


โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ“ Practice Task 1: Student Grade Tracker. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 28 of 47
๐Ÿ“ Practice Task 2: Shopping Cart Calculator
python
cart = {
    "Milk": 1.50,
    "Bread": 2.00,
    "Eggs": 3.25,
    "Butter": 4.75,
    "Coffee": 6.99
}

total = 0
print("=== Shopping Cart ===")

for item, price in cart.items():
    print(f"  {item}: ยฃ{price:.2f}")
    total += price

tax = total * 0.20  # 20% VAT
grand_total = total + tax

print("-------------------")
print(f"Subtotal: ยฃ{total:.2f}")
print(f"Tax (20%): ยฃ{tax:.2f}")
print(f"Total: ยฃ{grand_total:.2f}")

Output:

code
=== Shopping Cart ===
  Milk: ยฃ1.50
  Bread: ยฃ2.00
  Eggs: ยฃ3.25
  Butter: ยฃ4.75
  Coffee: ยฃ6.99
-------------------
Subtotal: ยฃ18.49
Tax (20%): ยฃ3.70
Total: ยฃ22.19

โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ“ Practice Task 2: Shopping Cart Calculator. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 29 of 47
๐Ÿ“ Practice Task 3: Number Guessing Game
python
import random

secret = random.randint(1, 100)
attempts = 0

print("Guess the number between 1 and 100!")

while True:
    guess = int(input("Your guess: "))
    attempts += 1

    if guess < secret:
        print("Too low! Try higher.")
    elif guess > secret:
        print("Too high! Try lower.")
    else:
        print(f"Correct! You got it in {attempts} attempts.")
        break

# Part 7: Python Syllabus ยท Your Complete Learning Map

This is the official map of all Python topics, organised from beginner to advanced. Use this as your learning checklist.


โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ“ Practice Task 3: Number Guessing Game. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 30 of 47
๐Ÿ“š Module 1: Python Basics (Beginner)
#TopicDescription
1.1Python IntroductionWhat Python is, where it's used, how to install
1.2Python SyntaxIndentation, comments, basic structure
1.3VariablesCreating and naming variables
1.4Data Typesstr, int, float, bool, list, dict, tuple, set
1.5NumbersArithmetic operators: +, -, , /, //, %, *
1.6StringsCreating, slicing, formatting, methods
1.7BooleansTrue/False, comparison operators
1.8OperatorsArithmetic, assignment, comparison, logical
1.9Type CastingConverting between types: int(), str(), float()
1.10User Inputinput(), reading from the keyboard

โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ“š Module 1: Python Basics (Beginner). Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 31 of 47
๐Ÿ“š Module 2: Control Flow
#TopicDescription
2.1If...ElseConditional branching
2.2elifMultiple condition branches
2.3Nested IfIf inside if
2.4While LoopRepeat while a condition is True
2.5For LoopIterate over sequences
2.6range()Generating number sequences
2.7breakExit a loop early
2.8continueSkip to next iteration
2.9passPlaceholder for empty code blocks

โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ“š Module 2: Control Flow. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 32 of 47
๐Ÿ“š Module 3: Data Structures
#TopicDescription
3.1ListsOrdered, mutable sequences
3.2TuplesOrdered, immutable sequences
3.3SetsUnordered, unique collections
3.4DictionariesKey-value pairs
3.5List ComprehensionCompact list creation
3.6Dictionary ComprehensionCompact dictionary creation

โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ“š Module 3: Data Structures. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 33 of 47
๐Ÿ“š Module 4: Functions
#TopicDescription
4.1FunctionsDefining and calling functions
4.2ArgumentsPositional, keyword, default parameters
4.3*argsVariable number of positional arguments
4.4**kwargsVariable number of keyword arguments
4.5ReturnReturning values from functions
4.6LambdaAnonymous one-line functions
4.7RecursionFunctions that call themselves
4.8ScopeLocal vs global variables

โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ“š Module 4: Functions. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 34 of 47
๐Ÿ“š Module 5: Object-Oriented Programming (OOP)
#TopicDescription
5.1ClassesBlueprints for objects
5.2ObjectsInstances of a class
5.3__init__The constructor method
5.4MethodsFunctions inside a class
5.5InheritanceOne class extending another
5.6PolymorphismSame method, different behaviour
5.7EncapsulationHiding internal data
5.8AbstractionHiding complexity
5.9IteratorsObjects with __iter__ and __next__

โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ“š Module 5: Object-Oriented Programming (OOP). Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 35 of 47
๐Ÿ“š Module 6: Working with Files and Errors
#TopicDescription
6.1File Open/ReadReading files with open()
6.2File WriteWriting to files
6.3File DeleteRemoving files with os module
6.4Try/ExceptHandling errors gracefully
6.5RaiseTriggering custom errors
6.6FinallyCode that always runs

โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ“š Module 6: Working with Files and Errors. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 36 of 47
๐Ÿ“š Module 7: Advanced Python
#TopicDescription
7.1ModulesImporting standard and third-party modules
7.2DatesWorking with datetime module
7.3MathUsing the math module
7.4JSONParsing and generating JSON data
7.5RegExPattern matching with re module
7.6PIPInstalling external packages
7.7Virtual EnvironmentsIsolated project environments
7.8NumPyNumerical computing library
7.9PandasData analysis library
7.10MatplotlibData visualisation library

# Part 8: Python Study Plan ยท Learn Python in 8 Weeks

This structured plan takes you from zero to confident Python programmer in 8 weeks, studying approximately 1 ยท 2 hours per day.


โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ“š Module 7: Advanced Python. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 37 of 47
๐Ÿ—“๏ธ Week 1: Python Foundations
DayTopicGoal
Day 1Install Python + Write First ProgramPrint "Hello, World!"
Day 2Variables & Data TypesStore and display different types of data
Day 3Strings & String MethodsManipulate text
Day 4Numbers & Math OperationsPerform calculations
Day 5User Input & Type CastingBuild interactive programs
Day 6Review + Practice ExercisesReinforce Week 1 topics
Day 7Mini-Project: Personal Profile PrinterApply all Week 1 concepts

Week 1 Mini-Project:

python
name = input("Enter your name: ")
age = int(input("Enter your age: "))
city = input("Enter your city: ")
hobby = input("Enter your hobby: ")

print("\n=== Your Profile ===")
print("Name:", name)
print("Age:", age)
print("City:", city)
print("Hobby:", hobby)
print("In 10 years you'll be:", age + 10)

โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ—“๏ธ Week 1: Python Foundations. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 38 of 47
๐Ÿ—“๏ธ Week 2: Control Flow
DayTopicGoal
Day 8If/Elif/ElseMake decisions in code
Day 9Comparison & Logical OperatorsCombine conditions
Day 10For Loops + range()Repeat actions
Day 11While LoopsLoop with conditions
Day 12Break, Continue, PassControl loop flow
Day 13Review + ExercisesFizzBuzz, grade calculator
Day 14Mini-Project: Number Guessing GameApply control flow

โœ๏ธ Your Task
| Day 14 | Mini-Project: Number Guessing Game | Apply control flow | ยท
Phase 39 of 47
๐Ÿ—“๏ธ Week 3: Data Structures
DayTopicGoal
Day 15Lists (Create, Access, Modify)Store ordered data
Day 16List MethodsSort, append, remove
Day 17Tuples & SetsImmutable and unique collections
Day 18DictionariesKey-value data storage
Day 19Nested Data StructuresDicts inside lists, etc.
Day 20Review + ExercisesRemove duplicates, word count
Day 21Mini-Project: Student Grade TrackerUse lists and dicts

โœ๏ธ Your Task
| Day 21 | Mini-Project: Student Grade Tracker | Use lists and dicts | ยท
Phase 40 of 47
๐Ÿ—“๏ธ Week 4: Functions
DayTopicGoal
Day 22Defining FunctionsCreate reusable code blocks
Day 23Arguments & ReturnPass data in and out
Day 24Default & Keyword ArgsFlexible function calls
Day 25args and *kwargsVariable arguments
Day 26Lambda FunctionsQuick anonymous functions
Day 27Scope & RecursionUnderstand variable visibility
Day 28Mini-Project: Calculator AppBuild a functional tool

โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ—“๏ธ Week 4: Functions. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 41 of 47
๐Ÿ—“๏ธ Week 5: Object-Oriented Programming
DayTopicGoal
Day 29Classes and ObjectsCreate blueprints and instances
Day 30__init__ and MethodsObject initialisation
Day 31InheritanceExtend existing classes
Day 32PolymorphismFlexible method behaviour
Day 33Encapsulation & PropertiesProtect data
Day 34Review + ExercisesBuild a Bank Account class
Day 35Mini-Project: Library SystemBooks, Members, Borrowing

โœ๏ธ Your Task
| Day 35 | Mini-Project: Library System | Books, Members, Borrowing | ยท
Phase 42 of 47
๐Ÿ—“๏ธ Week 6: Files, Errors & Modules
DayTopicGoal
Day 36Reading & Writing FilesPersist data to disk
Day 37Try/Except Error HandlingPrevent crashes
Day 38Standard Library Modulesmath, random, datetime, os
Day 39JSON DataRead/write structured data
Day 40RegEx BasicsPattern matching in strings
Day 41Review + PracticeFile-based programs
Day 42Mini-Project: CSV Contact BookStore contacts in a file

โœ๏ธ Your Task
| Day 42 | Mini-Project: CSV Contact Book | Store contacts in a file | ยท
Phase 43 of 47
๐Ÿ—“๏ธ Week 7: Intermediate Python
DayTopicGoal
Day 43List ComprehensionsCompact list operations
Day 44Dictionary ComprehensionsCompact dictionary operations
Day 45Generators & IteratorsMemory-efficient sequences
Day 46DecoratorsModify function behaviour
Day 47Context Managers (with)Resource management
Day 48Virtual Environments & PIPManage packages
Day 49Mini-Project: Data Filter ToolProcess and clean data

โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ—“๏ธ Week 7: Intermediate Python. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 44 of 47
๐Ÿ—“๏ธ Week 8: Real-World Python Applications
DayTopicGoal
Day 50NumPy BasicsArrays and maths
Day 51Pandas BasicsDataFrames and data analysis
Day 52Matplotlib BasicsCharts and visualisations
Day 53Web Scraping (requests + BeautifulSoup)Collect web data
Day 54APIs (requests library)Connect to external services
Day 55Introduction to FlaskBuild a simple web app
Day 56Final Capstone ProjectBuild a complete data-driven app

# Part 9: Python Interview Questions ยท Be Job-Ready

These are the most commonly asked Python questions in technical job interviews. Each includes a clear answer and code where relevant.


โœ๏ธ Your Task
Practise what you just learned about ๐Ÿ—“๏ธ Week 8: Real-World Python Applications. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 45 of 47
๐ŸŽฏ Beginner-Level Interview Questions

Q1: What is Python and what are its main uses?

Answer: Python is a high-level, interpreted, general-purpose programming language created by Guido van Rossum in 1991. It is known for its clean, readable syntax.

Main uses:

  • Web development (Django, Flask)
  • Data analysis (Pandas, NumPy)
  • Machine learning (TensorFlow, scikit-learn)
  • Automation and scripting
  • Scientific computing
  • Game development
  • Cybersecurity

Q2: What are Python's key features?

Answer:

  • Readable syntax ยท looks almost like plain English
  • Interpreted ยท runs line by line, no need to compile
  • Dynamically typed ยท no need to declare variable types
  • Object-oriented ยท supports classes and objects
  • Large standard library ยท hundreds of built-in modules
  • Cross-platform ยท runs on Windows, Mac, Linux
  • Open-source ยท free to use and distribute

Q3: What is the difference between a list and a tuple?

FeatureListTuple
Syntax[1, 2, 3](1, 2, 3)
Mutable?โœ… Yes (can change)โŒ No (fixed)
SpeedSlowerFaster
Use caseData that changesFixed data (coordinates, config)
python
my_list = [1, 2, 3]
my_list[0] = 99    # Works fine

my_tuple = (1, 2, 3)
my_tuple[0] = 99   # TypeError! Tuples are immutable

Q4: What is the difference between == and is?

  • == checks if two values are equal
  • is checks if two variables point to the same object in memory
python
a = [1, 2, 3]
b = [1, 2, 3]

print(a == b)   # True  (same values)
print(a is b)   # False (different objects in memory)

c = a
print(a is c)   # True  (same object)

Q5: What are Python's built-in data types?

CategoryTypes
Textstr
Numericint, float, complex
Sequencelist, tuple, range
Mappingdict
Setset, frozenset
Booleanbool
Binarybytes, bytearray
NoneNoneType

Q6: What is None in Python?

None is Python's way of representing nothing or no value. It is similar to null in other languages.

python
result = None

if result is None:
    print("No result yet")

Q7: What is a dictionary in Python?

A dictionary stores key-value pairs. Keys must be unique and immutable. Values can be anything.

python
person = {
    "name": "Alice",
    "age": 30,
    "job": "Engineer"
}

print(person["name"])     # Alice
print(person.get("age"))  # 30
person["city"] = "London" # Add a new key

Q8: Explain the difference between append() and extend()

  • append() adds one item to the end of a list
  • extend() adds all items from another iterable
python
list1 = [1, 2, 3]
list1.append([4, 5])
print(list1)   # [1, 2, 3, [4, 5]] โ† nested list added

list2 = [1, 2, 3]
list2.extend([4, 5])
print(list2)   # [1, 2, 3, 4, 5] โ† items added individually

Q9: What is a list comprehension?

A compact way to create a new list from an existing one.

Traditional loop:

python
squares = []
for x in range(1, 6):
    squares.append(x ** 2)

List comprehension:

python
squares = [x ** 2 for x in range(1, 6)]
print(squares)   # [1, 4, 9, 16, 25]

Q10: What is the difference between break, continue, and pass?

python
# break: exits the loop entirely
for i in range(10):
    if i == 5:
        break
    print(i)        # Prints 0 1 2 3 4

# continue: skips the current iteration, continues loop
for i in range(10):
    if i == 5:
        continue
    print(i)        # Prints 0 1 2 3 4 6 7 8 9

# pass: does nothing, placeholder
for i in range(3):
    pass            # No output, but no error either

โœ๏ธ Your Task
Practise what you just learned about ๐ŸŽฏ Beginner-Level Interview Questions. Open your editor, type the examples above by hand, modify them, and observe what changes.
Phase 46 of 47
๐ŸŽฏ Intermediate Interview Questions

Q11: What is a lambda function?

A lambda is a small, unnamed function defined with the lambda keyword.

python
double = lambda x: x * 2
print(double(5))    # 10

# Common use: sort a list of tuples by second element
pairs = [(1, 'b'), (3, 'a'), (2, 'c')]
pairs.sort(key=lambda pair: pair[1])
print(pairs)   # [(3, 'a'), (1, 'b'), (2, 'c')]

Q12: What is the difference between args and *kwargs?

  • *args ยท collects extra positional arguments into a tuple
  • kwargs ยท collects extra keyword** arguments into a dictionary
python
def show_args(*args):
    for item in args:
        print(item)

show_args(1, 2, 3)   # Prints 1, 2, 3 each on a new line

def show_kwargs(**kwargs):
    for key, value in kwargs.items():
        print(key, "=", value)

show_kwargs(name="Alice", age=25)
# name = Alice
# age = 25

Q13: What is a Python decorator?

A decorator is a function that wraps another function to modify its behaviour without changing its code.

python
def my_decorator(func):
    def wrapper():
        print("Before the function runs")
        func()
        print("After the function runs")
    return wrapper

@my_decorator
def say_hello():
    print("Hello!")

say_hello()

Output:

code
Before the function runs
Hello!
After the function runs

Q14: What is the difference between shallow copy and deep copy?

  • Shallow copy: Copies the outer object but shares inner objects
  • Deep copy: Copies everything, including nested objects
python
import copy

original = [[1, 2], [3, 4]]

shallow = copy.copy(original)
deep = copy.deepcopy(original)

original[0][0] = 99

print(original)   # [[99, 2], [3, 4]]
print(shallow)    # [[99, 2], [3, 4]] โ† affected!
print(deep)       # [[1, 2], [3, 4]]  โ† NOT affected

Q15: What is a Python generator?

A generator produces values one at a time using yield instead of return. It is memory-efficient for large datasets.

python
def count_up(n):
    for i in range(n):
        yield i

gen = count_up(5)

for value in gen:
    print(value)

Output:

code
0
1
2
3
4

Unlike a list, which creates all values at once, a generator creates each value only when needed.


Q16: Explain inheritance in Python.

Inheritance allows a child class to inherit attributes and methods from a parent class.

python
class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        print(self.name, "makes a sound")

class Dog(Animal):    # Dog inherits from Animal
    def speak(self):  # Override parent method
        print(self.name, "says: Woof!")

class Cat(Animal):
    def speak(self):
        print(self.name, "says: Meow!")

dog = Dog("Rex")
cat = Cat("Whiskers")

dog.speak()   # Rex says: Woof!
cat.speak()   # Whiskers says: Meow!

Q17: What is the GIL (Global Interpreter Lock)?

The GIL is a mutex (lock) in CPython (the standard Python implementation) that allows only one thread to execute Python bytecode at a time. This means Python threads do not run truly in parallel for CPU-bound tasks.

Impact:

  • Good for I/O-bound tasks (file reading, web requests)
  • Limiting for CPU-heavy parallel computing
  • Solution: Use multiprocessing module for true parallelism

Q18: What is the difference between range() and xrange()?

  • In Python 3, xrange() no longer exists. range() behaves like Python 2's xrange().
  • range() in Python 3 is lazy ยท it generates numbers on demand without creating a full list in memory.
python
r = range(1_000_000)  # Does NOT create 1 million numbers in memory immediately
print(r[0])   # 0 โ€” only computed when accessed

Q19: How does Python manage memory?

Python uses:

  1. Reference counting ยท tracks how many variables point to an object
  2. Garbage collection ยท cleans up objects with circular references
  3. Memory pools ยท reuses freed memory for efficiency

When no variable references an object, Python automatically frees its memory.


Q20: What is __init__ in a class?

__init__ is the constructor method ยท it runs automatically when an object is created from a class.

python
class Car:
    def __init__(self, brand, model, year):
        self.brand = brand
        self.model = model
        self.year = year

    def describe(self):
        print(f"{self.year} {self.brand} {self.model}")

car1 = Car("Toyota", "Corolla", 2022)
car1.describe()   # 2022 Toyota Corolla

# Part 10: Mini-Project ยท Python Practice Consolidator

This mini-project combines everything from this lesson into one working program.


โœ๏ธ Your Task
Impact: - Good for I/O-bound tasks (file reading, web requests) - Limiting for CPU-heavy parallel computing - Solution: Use multiprocessing module for true parallelism ยท
Phase 47 of 47
๐Ÿ› ๏ธ Project: Student Data Processor

Goal: Build a program that:

  1. Stores student names and their test scores
  2. Removes any duplicate names
  3. Calculates averages
  4. Assigns letter grades
  5. Displays a formatted report
  6. Finds the top student

Stage 1: Setup Data

python
# Stage 1: Create student data
students_raw = [
    {"name": "Alice", "scores": [85, 92, 78]},
    {"name": "Bob", "scores": [70, 65, 80]},
    {"name": "Carol", "scores": [95, 98, 91]},
    {"name": "Alice", "scores": [85, 92, 78]},   # duplicate
    {"name": "Dave", "scores": [60, 55, 70]},
]

print("Raw student count:", len(students_raw))

Output:

code
Raw student count: 5

Stage 2: Remove Duplicate Names

python
# Stage 2: Remove duplicate students by name
seen_names = []
students = []

for student in students_raw:
    if student["name"] not in seen_names:
        seen_names.append(student["name"])
        students.append(student)

print("After removing duplicates:", len(students), "students")

Output:

code
After removing duplicates: 4 students

Stage 3: Calculate Averages and Assign Grades

python
# Stage 3: Add average and grade to each student
def get_grade(average):
    if average >= 90:
        return "A"
    elif average >= 80:
        return "B"
    elif average >= 70:
        return "C"
    elif average >= 60:
        return "D"
    else:
        return "F"

for student in students:
    avg = sum(student["scores"]) / len(student["scores"])
    student["average"] = round(avg, 1)
    student["grade"] = get_grade(avg)

Stage 4: Display Report

python
# Stage 4: Print formatted report
print("\n" + "="*40)
print("      STUDENT PERFORMANCE REPORT")
print("="*40)

for student in students:
    print(f"Name:    {student['name']}")
    print(f"Scores:  {student['scores']}")
    print(f"Average: {student['average']}")
    print(f"Grade:   {student['grade']}")
    print("-"*40)

Stage 5: Find Top Student

python
# Stage 5: Find top performer
top_student = max(students, key=lambda s: s["average"])

print(f"\n๐Ÿ† Top Student: {top_student['name']}")
print(f"   Average: {top_student['average']} | Grade: {top_student['grade']}")

Complete Output:

code
Raw student count: 5
After removing duplicates: 4 students

========================================
      STUDENT PERFORMANCE REPORT
========================================
Name:    Alice
Scores:  [85, 92, 78]
Average: 85.0
Grade:   B
----------------------------------------
Name:    Bob
Scores:  [70, 65, 80]
Average: 71.7
Grade:   C
----------------------------------------
Name:    Carol
Scores:  [95, 98, 91]
Average: 94.7
Grade:   A
----------------------------------------
Name:    Dave
Scores:  [60, 55, 70]
Average: 61.7
Grade:   D
----------------------------------------

๐Ÿ† Top Student: Carol
   Average: 94.7 | Grade: A

Optional Enhancements

  • Save the report to a .txt file
  • Add a function to reverse each student's name (practice string reversal)
  • Sort students by average (highest first)
  • Calculate class average across all students
  • Count how many students passed (grade C or above)

# Part 11: Common Beginner Mistakes

These are the most frequent errors Python beginners make. Learn them now to avoid frustration later.


Mistake 1: Forgetting to Convert Input

python
# โŒ Wrong
age = input("Enter age: ")
next_year = age + 1        # TypeError: can only concatenate str (not "int") to str

# โœ… Correct
age = int(input("Enter age: "))
next_year = age + 1
print(next_year)

Mistake 2: Off-by-One in range()

python
# โŒ Wrong โ€” wants to print 1 to 10 but...
for i in range(10):
    print(i)    # Prints 0 to 9! Not 1 to 10.

# โœ… Correct
for i in range(1, 11):
    print(i)    # Prints 1 to 10

Mistake 3: Modifying a List While Iterating

python
# โŒ Wrong โ€” skips items
nums = [1, 2, 3, 4, 5]
for n in nums:
    if n % 2 == 0:
        nums.remove(n)   # Unexpected behaviour

# โœ… Correct โ€” iterate over a copy
nums = [1, 2, 3, 4, 5]
for n in nums[:]:
    if n % 2 == 0:
        nums.remove(n)

print(nums)   # [1, 3, 5]

Mistake 4: Using = Instead of == in Conditions

python
# โŒ Wrong โ€” assignment, not comparison
if x = 5:     # SyntaxError

# โœ… Correct โ€” comparison
if x == 5:
    print("x is 5")

Mistake 5: Forgetting Indentation

python
# โŒ Wrong
def greet():
print("Hello")   # IndentationError

# โœ… Correct
def greet():
    print("Hello")

Python uses indentation (spaces or tabs) to define code blocks. This is mandatory, not optional.


Mistake 6: Confusing Append vs Extend

python
# โŒ If you want to add items individually
my_list = [1, 2, 3]
my_list.append([4, 5])     # Adds a nested list: [1, 2, 3, [4, 5]]

# โœ… Use extend to add items individually
my_list = [1, 2, 3]
my_list.extend([4, 5])     # Adds items: [1, 2, 3, 4, 5]

Mistake 7: Mutable Default Arguments in Functions

python
# โŒ Wrong โ€” the list persists between calls!
def add_item(item, lst=[]):
    lst.append(item)
    return lst

print(add_item("a"))   # ['a']
print(add_item("b"))   # ['a', 'b'] โ† Bug! Expected ['b']

# โœ… Correct โ€” use None as default
def add_item(item, lst=None):
    if lst is None:
        lst = []
    lst.append(item)
    return lst

# Part 12: Reflection Questions

Use these questions to deepen your understanding. Write your answers in a notebook or try them in code.

  1. What are three different ways to remove duplicates from a list? When would you choose one over another?
  1. What is the difference between text[::-1] and reversed(text)? When might you prefer each?
  1. Why does Python's input() always return a string? What would happen if you forgot to convert it?
  1. You have a list [1, 2, 3, 4, 5]. What does my_list[-2] return? Why?
  1. What is the difference between a for loop and a while loop? Give a real-world example where each is more appropriate.
  1. Why might a set be faster than a list for checking if an item exists?
  1. In an interview, you are asked to write FizzBuzz. What is your approach? Write it out.
  1. What is the purpose of __init__ in a class? What would happen if you left it out?
  1. If you were building a data analysis pipeline, which Python libraries would you use and why?
  1. Looking at the 8-week study plan: which week do you think will be the hardest for you? Why? What can you do to prepare?

# Part 13: Completion Checklist

Use this checklist to confirm you have mastered Lesson 45.

  • [ ] I can remove duplicates from a list using at least two different methods
  • [ ] I can reverse a string using slicing [::-1]
  • [ ] I can check if a word is a palindrome
  • [ ] I can add two numbers entered by the user (handling type conversion)
  • [ ] I can write a for loop and a while loop
  • [ ] I can define a function with parameters and a return value
  • [ ] I can use a dictionary to store and retrieve key-value data
  • [ ] I completed at least 5 exercises from Part 3
  • [ ] I attempted at least 3 challenges from Part 5
  • [ ] I answered at least 5 quiz questions correctly
  • [ ] I completed (or planned) the Student Data Processor mini-project
  • [ ] I reviewed the full Python syllabus and know where I am in it
  • [ ] I have a personal study schedule based on the 8-week plan
  • [ ] I reviewed the interview questions and can answer 10+ without help
  • [ ] I identified my 3 most common beginner mistakes and know how to fix them

# Lesson Summary

Congratulations! You have completed Lesson 45 ยท the most comprehensive consolidation lesson of your Python journey.

Here is everything you covered:

SectionWhat You Learned
How-To: Remove Duplicatesset(), loops, dict.fromkeys()
How-To: Reverse a String[::-1], reversed(), loops, palindrome check
How-To: Add Two Numbersinput(), int(), float(), sum()
Python ExamplesStrings, lists, dicts, loops, functions, classes, files
ExercisesVariables, strings, lists, loops, functions
Quiz10 knowledge-check questions with answers
ChallengesWord frequency, Fibonacci, prime check, anagram, missing number
Practice TasksGrade tracker, shopping cart, number game
SyllabusComplete 7-module Python learning map
Study Plan8-week schedule from beginner to advanced
Interview Questions20 beginner-to-intermediate Q&As
Mini-ProjectStudent Data Processor combining all concepts
Common Mistakes7 critical beginner errors with fixes

๐ŸŽ“ Next Steps:

  • Follow the 8-week study plan systematically
  • Practice on real coding platforms (HackerRank, LeetCode, CodeWars)
  • Build personal projects that solve real problems you care about
  • Review Python documentation at docs.python.org
  • Keep this lesson as a reference ยท return to it whenever you need a reminder

You now have the knowledge, the practice, the roadmap, and the interview preparation to move forward as a confident Python programmer. The rest is practice. Write code every day, even if it's just 15 minutes. Small daily actions build unstoppable skill.

โœ๏ธ Your Task
- Sort students by average (highest first) - Calculate class average across all students - Count how many students passed (grade C or above) ยท # Part 11: Common Beginner Mistakes These are the most frequent errors Python beginners make. Learn them now to avoid frustration later. ยท
๐Ÿ—๏ธ Build It โ€” Mini Project
Lesson 45 Project

# My Python Project print("Hello from Techbase!") # Write your code below:

starter.py
# My Python Project
print("Hello from Techbase!")

# Write your code below:

Lesson 45 complete! ๐ŸŽ‰

You covered: