Functions are reusable blocks of code that perform specific tasks. They are a fundamental building block of Python programming, allowing you to organize your code, improve readability, and promote reusability.
We'll learn about the following topics:
In programming, a function is a reusable block of code that performs a specific task. It takes input arguments (optional) and returns an output value (optional). Functions provide a way to organize code into logical and reusable units, making it easier to manage and maintain your program.
def function_name(arguments): Function body Code block to perform specific tasks Return statement (optional)
def greet():
print('Hello, there!')
type(greet)
function
Here, we defined a function named greet without any arguments. The function body consists of a single line that prints the greeting message.
greet()
Hello, there!
def greet(name):
print(f'Hello, {name}!')
Now, we can call the greet() function by passing a name as an argument.
greet('Pegah')
Hello, Pegah!
def greet(name):
return f'Hello, {name}!'
Now, we can call the greet() function and capture its return value:
message = greet('Pegah')
print(message)
Hello, Pegah!
def add_numbers(a, b):
return a + b
We can call the add_numbers() function by passing two numbers:
result = add_numbers(5, 7)
print(result)
12
def greet(name='there'):
return f'Hello, {name}!'
Now, we can call the greet() function without providing an argument:
message = greet()
print(message)
Hello, there!
If we provide an argument, it will override the default value:
message = greet('Pegah')
print(message)
Hello, Pegah!
def multiply(a, b):
result = a * b
return result
product = multiply(3, 4)
print(product)
12
Here, the variable result is defined within the multiply() function and can't be accessed outside of it. However, the variable product is defined outside of any function and can be printed.
def even_check(number):
return number % 2 == 0
even_check(25)
False
def check_even_list(num_list):
for number in num_list:
if number % 2 != 0:
return False
return True
check_even_list([2,6,12]), check_even_list([9,5,7])
(True, False)
def check_even_list(num_list):
even_numbers = []
for number in num_list:
if number % 2 == 0:
even_numbers.append(number)
else:
pass
return even_numbers
check_even_list([2,5,7])
[2]
def player_guess():
guess = ''
while guess not in ['0','1','2']:
guess = input('Pick a number: 0, 1, or 2: ')
return int(guess)
player_guess()
Pick a number: 0, 1, or 2: 3 Pick a number: 0, 1, or 2: 2
2
The provided Python code defines a function named player_guess() that prompts the user to input a number between 0 and 2. It continues to prompt the user until a valid input is provided.
Nested functions in Python refer to the concept of defining a function inside another function. In other words, a function can be declared and defined within the body of another function. These nested functions are also known as inner functions.
def outer_function():
print('This is the outer function.')
def inner_function():
print('This is the inner function.')
inner_function()
outer_function()
This is the outer function. This is the inner function.
In the example above, we have an outer function named outer_function(). Inside this function, there is another function called inner_function(). The inner_function() is defined and declared within the body of the outer_function().
def outer_function(x):
def inner_function(y):
return x + y
result = inner_function(5)
return result
result = outer_function(10)
print(result)
15
map
: The map() function in Python is a built-in function that applies a given function to each item of an iterable (like a list, tuple, or string) and returns a new iterable.map(function, iterable)
def get_root(num):
return num**0.5
nums = [4, 9, 16, 25, 36]
map(get_root, nums)
<map at 0x24d2c29bd30>
To get the results, either iterate through map() or cast the results to a list.
list(map(get_root, nums))
[2.0, 3.0, 4.0, 5.0, 6.0]
filter
: The filter() function in Python is a built-in function that creates a new iterator object containing elements from an iterable that satisfy a given function.filter(function, iterable)
def get_even(num):
return num % 2 == 0
filter(get_even, nums)
<filter at 0x24d2c2b2040>
list(filter(get_even, nums))
[4, 16, 36]
lambda
: A lambda function is a small, anonymous function defined using the lambda keyword. It's often used for simple, one-line functions that are needed temporarily.lambda arguments: expression
add = lambda x, y: x + y
result = add(5, 3)
print(result)
8
In this example, we've defined a lambda function add that takes two arguments x and y and returns their sum. We then call the function and print the result.
numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(lambda x: x**2, numbers))
print(squared_numbers)
[1, 4, 9, 16, 25]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers)
[2, 4]
sorted_numbers = sorted(numbers, key=lambda x: x % 2)
print(sorted_numbers)
[2, 4, 1, 3, 5]
str_list = ['apple', 'banana', 'peach', 'melon']
reverse_str = list(map(lambda x: x[::-1], str_list))
print(reverse_str)
['elppa', 'ananab', 'hcaep', 'nolem']
all
: The all() function in Python is a built-in function that returns True if all elements in an iterable are true, otherwise it returns False.all(iterable)
numbers = [1, 2, 3, 4, 5]
all_positive = all(num > 0 for num in numbers)
print(all_positive)
True
numbers = [1, 2, -3, 4, 5]
all_positive = all(num > 0 for num in numbers)
print(all_positive)
False
any
: any() is a built-in function in Python that takes an iterable (like a list, tuple, or dictionary) as input and returns True if any of the elements in the iterable are True. If all elements are False or the iterable is empty, it returns False.any(iterable)
numbers = [-1, -2, -3, -4, -5]
atleast_one_positive = any(num > 0 for num in numbers)
print(atleast_one_positive)
False
*args
: *args in Python functions is a special syntax used to pass a variable number of arguments to a function. These arguments are collected into a tuple and can be accessed within the function using the args variable.def myfunc(*args):
return sum(args) / 2
myfunc(25, 60, 35)
60.0
The term args is just a convention; you can use any word as long as it's preceded by an asterisk.
def myfunc(*nums):
return sum(nums) / 2
myfunc(25, 60, 35)
60.0
You can combine args with other parameters, but the args parameter must be the last parameter in the function definition.
def calculate_total(price, *quantities):
total = price
for quantity in quantities:
total += price * quantity
return total
price = 10
quantities = (5, 3, 2)
total_cost = calculate_total(price, *quantities)
print(total_cost)
110
*kargs
: *kargs in Python is a special syntax used to pass a variable number of keyword arguments to a function. These arguments are collected into a dictionary, where the keys are the argument names and the values are the corresponding values.def print_kwargs(**kwargs):
for key, value in kwargs.items():
print(key, value)
print_kwargs(name="Alice", age=30, city="New York")
name Alice age 30 city New York