function
Big \(O\) notation is used to describe the performance or complexity of an algorithm. It specifically describes the worst-case scenario, and can be used to describe the execution time required or the space used by an algorithm.
Comparison computational complexity
def followed by the function name and input parameters in parenthesesreturn to specify the output value(s) of the functionTo call a function, use its name followed by input values in parentheses The function returns the output value(s), which can be stored in a variable or used directly
<function __main__.add_numbers(*args)>
def add_numbers(*args):
"""
Computes the sum of n numbers
Parameters:
args: A tuple of numbers
Returns:
int: The sum
"""
result = 0
for num in args:
result += num
return result
help(add_numbers)Help on function add_numbers in module __main__:
add_numbers(*args)
Computes the sum of n numbers
Parameters:
args: A tuple of numbers
Returns:
int: The sum
Python function that takes a list of numbers and returns their product
24
Write a Python function to find the maximum and minimum numbers in a list
(2, 4)
def find_min_max(numbers):
min_num = numbers[0] # Set the min to the first number in the list
max_num = numbers[0] # Set the max to the first number in the list
for num in numbers:
if num > max_num:
max_num = num
elif num < min_num:
min_num = num
return (min_num, max_num)
find_min_max ([2, 3, 4])(2, 4)
Write a Python function that takes a list of strings and returns the longest string.
'Barcelona'
def find_longest_string(strings):
# Set the longest string to first string in the list
longest_string = strings[0]
for string in strings:
# Compare the length of the current string to the longest string so far
if len(string) > len(longest_string):
longest_string = string
return longest_string
find_longest_string(["Vicky", "Cristina", "Barcelona"])'Barcelona'