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 topicsWorked 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]