👋 Work With Me

I help startups and teams build production-ready apps with Django, Flask, and FastAPI.

Let’s Talk →

I'm always excited to take on new projects and collaborate with innovative minds.

Address

No 7 Street E, Federal Low-cost Housing Estate, Kuje, Abuja 903101, Federal Capital Territory

Social Links

Tutorials

Python Tutorial Series – Lesson 11: Functions in Python

Python Tutorial for Beginners (Lesson 11): Learn functions in Python with examples, parameters, return values, default arguments, keyword arguments, lambda functions, and exercises.

Python Tutorial Series – Lesson 11: Functions in Python

Welcome back to the Python Tutorial Series for Beginners! 🚀

In Lesson 10, we learned about loops and how they help us repeat tasks efficiently.

Now in Lesson 11, we’ll dive into functions – the building blocks of organized and reusable code.

By the end of this lesson, you’ll understand:

  • What functions are and why they’re important
  • How to define and call a function
  • Function parameters and return values
  • Default arguments and keyword arguments
  • Variable scope in functions
  • Lambda (anonymous) functions
  • Real-world use cases
  • Beginner exercises

🔹 What are Functions?

👉 A function is a block of code that performs a specific task and can be reused whenever needed.

💡 Real-life example:

  • A vending machine: You press a button (call function) → it dispenses a drink (returns output).

In Python, we use the def keyword to create functions.


🔹 1. Defining and Calling a Function

 
def greet():
   print("Hello, welcome to Python!")
   
# Calling the function
greet()

👉 Output:

 
Hello, welcome to Python!

🔹 2. Function Parameters

Functions can accept parameters (inputs).

 
def greet(name):
   print(f"Hello, {name}!")
   
greet("Alice")
greet("Bob")

👉 Output:

 
Hello, Alice!
Hello, Bob!

🔹 3. Return Values

Functions can also return results using the return keyword.

 
def add(a, b):
   return a + b
   
result = add(5, 3)
print("Sum =", result)
👉 Output: Sum = 8

🔹 4. Default Arguments

You can assign default values to parameters.

 
def greet(name="Guest"):
   print(f"Hello, {name}!")
   
greet()
greet("Charlie")

👉 Output:

 
Hello, Guest!
Hello, Charlie!

🔹 5. Keyword Arguments

You can specify arguments by name for clarity.

 
def introduce(name, age):
   print(f"My name is {name}, and I am {age} years old.")
   
introduce(age=25, name="David")

👉 Output:

 
My name is David, and I am 25 years old.

🔹 6. Variable Scope

Variables inside a function are local and cannot be accessed outside.

 
def my_function():
   x = 10  # local variable
   print("Inside function:", x)
   
my_function()
print("Outside function:", x)  # ❌ Error

👉 Output:

 
Inside function: 10
NameError: name 'x' is not defined

🔹 7. Lambda (Anonymous Functions)

Lambda functions are small, one-line functions.

 
square = lambda x: x * x
print(square(5))

👉 Output: 25

Useful for short operations like sorting, filtering, or quick calculations.


🔹 8. Real-World Use Cases

Calculator Function

 
def calculator(a, b, operation):
   if operation == "add":
       return a + b
   elif operation == "subtract":
       return a - b
   elif operation == "multiply":
       return a * b
   elif operation == "divide":
       return a / b
   else:
       return "Invalid operation"
       
print(calculator(10, 5, "add"))
print(calculator(10, 5, "multiply"))

Reusable Greeting Function

 
def welcome_user(username):
   print(f"Welcome back, {username}!")

Filtering with Lambda

 
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers)

👉 Output: [2, 4, 6]


🔹 Exercises for Beginners

Try these in a new file (lesson11.py):

  1. Write a function that prints “Good Morning” when called.
  2. Create a function that takes two numbers and returns their product.
  3. Write a function that checks if a number is even or odd.
  4. Create a function with a default parameter for age (e.g., 18).
  5. Challenge: Write a function that takes a list of numbers and returns the maximum number.

🎯 Recap

In this lesson, you learned:
✅ Functions help organize and reuse code
✅ Parameters and return values make functions flexible
✅ Default and keyword arguments give more control
✅ Variable scope prevents accidental conflicts
✅ Lambda functions provide quick one-liners
✅ Functions are essential in real-world programming

Next up: Lesson 12 – Modules and Packages in Python 📦 (where we learn how to organize large projects).

💼 Need a Developer?

I'm Kingsley Odume, a Django, Flask, and FastAPI developer with experience building SaaS platforms, APIs, and modern web apps. If you're a recruiter or business owner looking for a reliable software developer, let's connect!

🚀 Hire Me

Python, Python tutorials, Python Beginners series, Python Step-byStep
4 min read
Aug 20, 2025
By Kingsley Odume
Share

Leave a comment

Your email address will not be published. Required fields are marked *

Related posts

Aug 29, 2025 • 3 min read
Step-by-Step: Building a SaaS App with Django + Stripe Payments

Learn how to build a real-world SaaS app with Django and Stripe paymen...

Aug 21, 2025 • 3 min read
Final Wrap-Up: Python Tutorial Series for Beginners

Congratulations on completing the Python Tutorial Series! 🚀 In this w...

Aug 20, 2025 • 4 min read
Final Lesson: Mini Project – Putting It All Together in Python

In this final lesson, we’ll bring all those concepts together in a Min...

Your experience on this site will be improved by allowing cookies. Cookie Policy