13.1 Introduction to Functions
Introduction to Functions is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def greet():
print("Hello from a function!")
greet()
Output
Hello from a function!
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.2 Why Functions Are Important
Why Functions Are Important is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def welcome():
print("Welcome")
welcome()
welcome()
Output
Welcome
Welcome
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.3 Defining Functions
Defining Functions is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def say_hi():
print("Hi")
print("Function defined")
Output
Function defined
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.4 Calling Functions
Calling Functions is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def ring():
print("Ding!")
ring()
Output
Ding!
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.5 Function Parameters
Function Parameters is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def greet(name):
print("Hello", name)
greet("Sara")
Output
Hello Sara
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.6 Function Arguments
Function Arguments is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def add(a, b):
print(a + b)
add(4, 6)
Output
10
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.7 Positional Arguments
Positional Arguments is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def show(name, city):
print(name, city)
show("Omar", "Toronto")
Output
Omar Toronto
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.8 Keyword Arguments
Keyword Arguments is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def show(name, age):
print(name, age)
show(age=20, name="Mina")
Output
Mina 20
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.9 Default Arguments
Default Arguments is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def greet(name, message="Hello"):
print(message, name)
greet("Lina")
Output
Hello Lina
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.10 Arbitrary Positional Arguments: *args
Arbitrary Positional Arguments: *args is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def show(*args):
print(args)
show("a", "b", "c")
Output
('a', 'b', 'c')
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.11 Arbitrary Keyword Arguments: **kwargs
Arbitrary Keyword Arguments: **kwargs is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def profile(**kwargs):
print(kwargs)
profile(name="Ali", city="Vaughan")
Output
{'name': 'Ali', 'city': 'Vaughan'}
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.12 Mixing Argument Types
Mixing Argument Types is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def show_message(message):
return message
print(show_message("Functions are useful"))
Output
Functions are useful
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.13 Argument Ordering Rules
Argument Ordering Rules is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def show_message(message):
return message
print(show_message("Functions are useful"))
Output
Functions are useful
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.14 Return Values
Return Values is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def multiply(a, b):
return a * b
print(multiply(4, 5))
Output
20
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.15 Returning Multiple Values
Returning Multiple Values is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def values():
return 3, 7
a, b = values()
print(a, b)
Output
3 7
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.16 Functions Without Return Values
Functions Without Return Values is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def announce():
print("Start")
print(announce())
Output
Start
None
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.17 The pass Statement
The pass Statement is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def future():
pass
print("Program continues")
Output
Program continues
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.18 Nested Functions
Nested Functions is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def outer():
def inner():
print("Inside")
inner()
outer()
Output
Inside
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.19 Functions as Objects
Functions as Objects is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def hi():
print("Hi")
f = hi
f()
Output
Hi
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.20 Passing Functions as Arguments
Passing Functions as Arguments is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def double(n):
return n * 2
def apply(fn, value):
return fn(value)
print(apply(double, 6))
Output
12
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.21 Returning Functions
Returning Functions is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def choose():
def greet():
print("Welcome")
return greet
f = choose()
f()
Output
Welcome
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.22 Higher-Order Functions
Higher-Order Functions is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def square(n):
return n * n
print(list(map(square, [1, 2, 3])))
Output
[1, 4, 9]
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.23 Pure and Impure Functions
Pure and Impure Functions is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def add(a, b):
return a + b
print(add(2, 3))
Output
5
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.24 Recursive Functions
Recursive Functions is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def countdown(n):
if n == 0:
print("Go!")
else:
print(n)
countdown(n - 1)
countdown(3)
Output
3
2
1
Go!
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.25 Base Cases
Base Cases is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def factorial(n):
if n == 1:
return 1
return n * factorial(n - 1)
print(factorial(4))
Output
24
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.26 Tail Recursion Concepts
Tail Recursion Concepts is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def show_message(message):
return message
print(show_message("Functions are useful"))
Output
Functions are useful
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.27 Function Annotations
Function Annotations is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def add(a: int, b: int) -> int:
return a + b
print(add(2, 4))
Output
6
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.28 Type Hints
Type Hints is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def greet(name: str) -> str:
return "Hello " + name
print(greet("Sara"))
Output
Hello Sara
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.29 Positional-Only Parameters
Positional-Only Parameters is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def divide(a, b, /):
return a / b
print(divide(10, 2))
Output
5.0
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.30 Keyword-Only Parameters
Keyword-Only Parameters is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def connect(*, host, port):
print(host, port)
connect(host="localhost", port=8000)
Output
localhost 8000
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.31 Common Function Errors
Common Function Errors is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def greet(name):
print("Hello", name)
greet("Ali")
Output
Hello Ali
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.32 Function Design Best Practices
Function Design Best Practices is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def calculate_total(price, quantity):
return price * quantity
print(calculate_total(5, 3))
Output
15
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.33 Chapter Practice Exercises
Chapter Practice Exercises is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def square(n):
return n * n
print(square(5))
Output
25
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.
13.34 Chapter Mini Project
Chapter Mini Project is an important part of learning Python functions. A function groups related instructions under one clear name so the code can be reused and understood more easily. This topic shows beginners how information enters a function, how Python performs the requested work, and how a useful result may be produced. Practising this idea helps you build organized, readable, testable, and maintainable programs.
Example
def add_task(tasks, task):
tasks.append(task)
return tasks
my_tasks = []
print(add_task(my_tasks, "Study Python"))
Output
['Study Python']
Output explanation: Python reads the function definition first and then follows the function call. The supplied values are matched to the parameters, the indented instructions run, and the displayed or returned result produces the output shown above.