Python for Beginners | Jan 22, 2026

Python for Beginners 12: Return Values and Scope

A beginner-focused guide to Return Values and Scope, including 10 practical Python examples and 20 quiz questions with answer guidance.

Beginner-Friendly Overview

Return Values and Scope is a beginner-friendly milestone in Python. Think of it as learning one reliable tool before building a full toolbox.

In simple words, this lesson shows what the concept is, why it matters, and how to use it safely. The goal is clarity: you should be able to explain it to another beginner after reading.

Professional developers still rely on these basics every day. If you master this topic early, writing real-world code becomes much easier.

Detailed explanation of Return Values and Scope:
1. Start with the smallest working example.
2. Name variables clearly so your future self understands the code.
3. Make one change at a time and rerun the program.
4. Read errors calmly: the message usually points to the exact issue.
5. Prefer simple code over clever code when you are learning.

Beginner mindset:
You are not expected to memorize everything. You are expected to practice, make mistakes, and improve. Write code daily, even if it is only a few lines.

Hands-On Examples

10 examples
Example 1
Define a simple function
main.py
1 def greet(name):
2 return f'Hello, {name}'
3
4 print(greet('Akosua'))

Run this code, change one value, and observe the output.

Example 2
Function with default value
main.py
1 def welcome(name='Learner'):
2 return f'Welcome, {name}'
3
4 print(welcome())

Run this code, change one value, and observe the output.

Example 3
Return multiple values
main.py
1 def stats(a, b):
2 return a + b, a * b
3
4 s, p = stats(4, 6)
5 print(s, p)

Run this code, change one value, and observe the output.

Example 4
Keyword arguments
main.py
1 def profile(name, role):
2 print(name, role)
3
4 profile(role='Student', name='Kwame')

Run this code, change one value, and observe the output.

Example 5
Variable-length args
main.py
1 def total(*nums):
2 return sum(nums)
3
4 print(total(2, 3, 5))

Run this code, change one value, and observe the output.

Example 6
Use docstrings
main.py
1 def area(w, h):
2 'Return rectangle area.'
3 return w * h
4
5 print(area(3, 4))

Run this code, change one value, and observe the output.

Example 7
Lambda basics
main.py
1 double = lambda x: x * 2
2 print(double(9))

Run this code, change one value, and observe the output.

Example 8
Map with function
main.py
1 nums = [1, 2, 3]
2 print(list(map(lambda n: n * n, nums)))

Run this code, change one value, and observe the output.

Example 9
Filter with function
main.py
1 nums = [1, 2, 3, 4, 5]
2 print(list(filter(lambda n: n % 2 == 0, nums)))

Run this code, change one value, and observe the output.

Example 10
Function composition idea
main.py
1 def add_tax(price):
2 return price * 1.15
3
4 def format_price(v):
5 return f'GHS {v:.2f}'
6
7 print(format_price(add_tax(100)))

Run this code, change one value, and observe the output.

Quiz Section

20 questions
1. What problem does Return Values and Scope solve in everyday coding?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
2. Which beginner mistake appears most often when learning Return Values and Scope?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
3. How do you explain Return Values and Scope to someone who has never coded before?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
4. When should you avoid using Return Values and Scope and choose a simpler approach?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
5. What is one clear sign that you understood Return Values and Scope correctly?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
6. How does Return Values and Scope make code easier to read?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
7. How does Return Values and Scope help reduce bugs?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
8. What is a real-life analogy for Return Values and Scope?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
9. What happens if you skip learning Return Values and Scope and jump ahead?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
10. How can you practice Return Values and Scope in 15 minutes daily?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
11. Which Python built-in works nicely with Return Values and Scope?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
12. How do comments improve learning while practicing Return Values and Scope?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
13. What is the difference between theory and practical use in Return Values and Scope?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
14. How does Return Values and Scope support teamwork in real projects?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
15. What interview question can test beginner knowledge of Return Values and Scope?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
16. How can you test your code while practicing Return Values and Scope?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
17. What naming style makes Return Values and Scope examples easier to follow?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
18. How do you debug common errors while learning Return Values and Scope?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
19. What mini-project can strengthen your understanding of Return Values and Scope?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
20. What should be your next topic after learning Return Values and Scope?
Answer guide: A strong answer should define the concept, show a short code use case, and mention one common mistake to avoid.
Practice Homework

1. Re-type all examples without copy-paste.
2. Change values and predict the output before running.
3. Build one mini exercise that combines at least three examples.
4. Explain your solution in plain language.

WhatsApp