- Functions and Modules
- Functions in Python
- Basic Function Structure
- Functions with Return Values
- Functions for Multiplication
- More Complex Functions
- Functions with Variable Numbers of Parameters
- Default Parameter Values
- Keyword Arguments
- Checking Even Numbers
- Filtering Even Numbers
- Grouping with Variable Parameters
- Function Example: Print Full Name
- 2. Lambda Functions
- 3. Higher-Order Functions
- 4. Function as a Parameter
- Function summary
- Modules
- Generating Random Values with the
random
Library
- Functions in Python
In Python, functions are reusable blocks of code that perform specific tasks. They can accept input, called parameters, and return results. Let's break down some examples to understand how functions work.
A function is defined using the def
keyword, followed by the function name, parentheses (which may include parameters), and a colon. The function body contains the code to execute.
Example:
def do_something(something):
print(f'I am {something}.')
Here, do_something
is a function that takes a parameter (something
) and prints it.
Usage:
do_something('teaching')
do_something('learning')
Output:
I am teaching.
I am learning.
Functions can return values using the return
statement.
Example:
def add_two_nums(x, y):
return x + y
Here, add_two_nums
returns the sum of x
and y
.
Usage:
print(add_two_nums('3', '4')) # Concatenates strings
print(add_two_nums(-3, -97)) # Adds two integers
Output:
34
-100
You can also perform other operations, like multiplication.
Example:
def multiply_two_nums(x, y):
return x * y
Usage:
print(multiply_two_nums('love', 3)) # Repeats the string 'love' three times
print(multiply_two_nums(100, 30)) # Multiplies two numbers
Output:
lovelovelove
3000
You can define functions that take multiple parameters and perform operations.
Example:
def linear_equation(x, y, k):
return 2 * x - y + k
This function calculates the result of the equation 2x - y + k
.
Usage:
print(linear_equation(2, 4, 10))
Output:
10
Python functions can accept a variable number of parameters using *args
.
Example:
def sum_all_nums(*params):
return sum(params)
Usage:
print(sum_all_nums(1, 2, 3, 4, 5, -15))
Output:
0
You can assign default values to parameters. If the function is called without providing that parameter, it will use the default value.
Example:
def calculate_weight(mass, gravity=9.81):
return mass * gravity
Usage:
print(calculate_weight(74.5)) # Uses default gravity
print(calculate_weight(74.5, 1.62)) # Custom gravity (e.g., Moon)
print(calculate_weight(74.5, 3.71)) # Custom gravity (e.g., Mars)
Output:
730.245
120.69000000000001
276.395
You can pass arguments by name, which allows you to specify them in any order.
Example:
def do_something(name, width, height, age, color):
return f'{name} {width} {height} {age} {color}'
Usage:
print(do_something(age=250, width=45, height=1.72, name='Asabeneh', color='Brown'))
Output:
Asabeneh 45 1.72 250 Brown
You can write simple functions to perform logical checks, like determining whether a number is even.
Example:
def is_even(n):
return n % 2 == 0
Usage:
print(is_even(0))
print(is_even(3))
Output:
True
False
You can filter a list of numbers to get only the even ones.
Example:
def filter_evens(lst):
return [num for num in lst if num % 2 == 0]
Usage:
nums = [-6, 0, 5, 2, 7, -4]
print(filter_evens(nums))
Output:
[-6, 0, 2, -4]
You can group different types of input using *args
and **kwargs
for variable positional and keyword arguments.
Example:
def generate_groups(team, country, *args):
print(team, args)
for i in args:
print(i)
Usage:
generate_groups('Team-1', 'Finland', 'Asabeneh', 'Brook', 'David', 'Eyob')
Output:
Team-1 ('Asabeneh', 'Brook', 'David', 'Eyob')
Asabeneh
Brook
David
Eyob
Let's create a function to print the full name by taking two parameters: first_name
and last_name
.
Example:
def print_fullname(first_name, last_name):
full_name = f'{first_name} {last_name}'
print(full_name)
Usage:
print_fullname('Asab', 'Yeta')
Output:
Asab Yeta
Lambda functions are anonymous, single-line functions. They are used for short operations without needing to define a full function.
square = lambda x: x ** 2
add_three_nums = lambda x, y, z: x + y + z
linear_equation = lambda a, b, c: 2 * a + 3 * b + c
Examples:
print(square(2)) # Output: 4
print(add_three_nums(99, 1, 100)) # Output: 200
A higher-order function either takes a function as an argument or returns another function.
make_cube
is a higher-order function because it takes another function,make_square
, as a parameter to calculate the cube of a number.
def make_cube(n, make_square):
return n * make_square(n)
Example:
print(make_cube(3, make_square)) # Output: 27
higher_order_function
accepts a function type and returns the corresponding function based on the input, such assquare
,cube
, orabsolute
.
def higher_order_function(type):
if type == 'square':
return make_square
elif type == 'cube':
return make_cube
elif type == 'absolute':
return absolute
Example:
print(higher_order_function('square')(5)) # Output: 25
print(higher_order_function('cube')(5, make_square)) # Output: 125
The higher_order_function
function shows how a function can be passed as an argument to another function. It’s demonstrated using sum_numbers
and a custom higher-order function that sums a list of numbers:
def higher_order_function(f, lst):
return f(lst)
Example:
result = higher_order_function(sum_numbers, [1, 2, 3, 4, 5])
This sums the list of numbers, returning 15
.
This code provides an overview of how to work with keyword arguments, lambda expressions, and higher-order functions in Python, illustrating their versatility and power in functional programming.
Functions are an essential part of programming in Python. They allow code reuse, make programs easier to understand, and help to organize logic into smaller, manageable pieces. With the examples provided, you can see various use cases for defining and using functions in Python.
In Python, modularity is a key feature that helps organize code and improve reusability. This reading material will cover how to work with custom modules and some useful standard Python libraries, including math
and random
. The code examples provided below will guide you through practical implementations of these concepts.
To keep code clean and manageable, you can create custom modules that contain specific functions. Below, we import a custom module custom_modules
and another module countries
, which contains various utility functions like sum_all_nums
, add_two_nums
, and filter_country_with_land
.
# Import functions from custom modules
from custom_modules import sum_all_nums, add_two_nums, multiply_two_nums, is_even, linear_equation, filter_country_with_land as filter_country, filter_evens, filter_positive_evens
from countries import countries
Once imported, these functions can be used directly in your script, making the code easier to read and maintain.
# Example usage of custom functions
print(sum_all_nums(2, 3, 4, 5, 6, -10)) # Sums up the numbers
print(add_two_nums(2, 3)) # Adds two numbers
print(multiply_two_nums(6, -10)) # Multiplies two numbers
print(is_even(10)) # Checks if the number is even
print(linear_equation(2, 3, 6)) # Solves a linear equation
print(filter_country(countries)) # Filters countries that contain the word 'land'
The math
library is a built-in Python module providing many mathematical functions. Below are some functions we imported from math
and their usage:
from math import pi, factorial as f, sqrt, floor, ceil
# Calculate the area of a circle
def calculate_area_circle(radius):
return pi * radius ** 2
pi
: A constant representing the value of π (used in circle calculations).factorial
: Computes the factorial of a number.sqrt
: Returns the square root of a number.floor
: Rounds down to the nearest integer.ceil
: Rounds up to the nearest integer.
print(floor(9.81)) # Output: 9
print(ceil(9.81)) # Output: 10
print(sqrt(9)) # Output: 3.0
print(sqrt(2)) # Output: 1.4142135623730951
Python's random
library provides methods to generate random numbers and choices. Here's how to explore and use some common random
functions:
import random
# Display all available functions in the random module
print(dir(random))
# Example: random.choice picks a random character from a string
print(random.choice('abcde'))
# random.randint generates a random integer within a specified range
print(random.randint(0, 10))
Let's create a function, generate_random_id
, that generates random alphanumeric IDs of any length (default is 6 characters).
def generate_random_id(n=6):
characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
random_id = ''
for _ in range(n):
random_id += random.choice(characters)
return random_id
print(generate_random_id(2)) # Generates a 2-character random ID
print(generate_random_id()) # Default 6-character ID
print(generate_random_id(7)) # Generates a 7-character ID
print(generate_random_id(24)) # Generates a 24-character ID
This function is useful when you need unique identifiers for users or other entities in your application.
In this tutorial, we explored:
- Custom Modules: How to import and use functions from custom modules for better code organization.
math
Library: Various mathematical functions, such as calculating areas, square roots, and rounding numbers.random
Library: Generating random numbers, choosing random elements, and creating random IDs.
By modularizing your code and using built-in libraries like math
and random
, you can write cleaner, more efficient Python programs.