Python / Guided lesson

Learn Python with Functions and Loops

Explore Python programming through simple, practical examples. Learn to work with variables, lists, loops, conditions, and functions while developing problem-solving skills.

Browse all Python topics

Worked example

Python is a high-level programming language known for its readable syntax. Functions are defined using def, lists store multiple values, and for loops allow you to process each item. Python uses indentation to define code blocks.

def find_even_numbers(numbers):
    even_numbers = []

    for number in numbers:
        if number % 2 == 0:
            even_numbers.append(number)

    return even_numbers

numbers = [1, 2, 3, 4, 5, 6, 7, 8]

result = find_even_numbers(numbers)

print("Even Numbers:", result)

Expected output

Even Numbers: [2, 4, 6, 8]

Check your understanding

What is the difference between a Python list and a tuple?

Show answer

A list is mutable, meaning its elements can be modified after creation. A tuple is immutable, meaning its elements cannot be changed after creation. Lists use square brackets [], while tuples generally use parentheses ().

Try it yourself

Modify the function to return only the odd numbers from the list.

Show solution
def find_odd_numbers(numbers):
    odd_numbers = []

    for number in numbers:
        if number % 2 != 0:
            odd_numbers.append(number)

    return odd_numbers

print(find_odd_numbers([1, 2, 3, 4, 5, 6, 7, 8]))

# Output: [1, 3, 5, 7]

Python / Full curriculum

All Python Topics

Choose a topic to study the explanation, code, output, questions, and practice exercises.

Lesson 1 / 56

Python · Lesson 1 of 56

Introduction and Hello World

1. Understand the idea

Python is a language for giving instructions to a computer. You write a line, Python reads it, and the computer follows it. Start by learning how to show a message on the screen.

Think of a real-life example

Imagine typing a short message onto the classroom projector. The computer displays exactly what you ask it to show.

2. Read the example

Read the code slowly. Before checking the output, try to guess what will happen.

print("Hello, AskBun!")

What happens in this code?

The print command takes the text between quotation marks and displays Hello, AskBun! The quotation marks mark the start and end of the message.

Expected output

Hello, AskBun!

3. A mistake to avoid

Write print with a small p and put words inside quotes. Without quotes, Python thinks Hello is a variable name.

New words explained simply
function
A named set of steps you can call again.

4. Check your understanding

What does print() do?

It writes its arguments to standard output.

5. Try it yourself

Try the task on your own first. It is okay to get it wrong—that is part of learning. Then open the solution to compare.

Print your own name.

Back to all subjects