Learning how to define and call functions, understanding parameters, return values, and scope. Here are 25 questions related to defining and calling functions, understanding parameters, return values,
1
What is a function in Python?
defgreet():print("Hello, World!")greet()
2
How do you define a function with parameters?
defadd(a,b):return a + bresult =add(3,5)print(result)
def print_numbers(*args):
for number in args:
print(number)
print_numbers(1, 2, 3, 4, 5)
def show_info(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
show_info(name="Alice", age=25, city="New York")
def outer_function():
outer_var = "I am outside!"
def inner_function():
inner_var = "I am inside!"
print(outer_var)
inner_function()
# print(inner_var) # This will cause an error
outer_function()
square = lambda x: x ** 2
print(square(4))
def operate_on_numbers(func, a, b):
return func(a, b)
def add(x, y):
return x + y
result = operate_on_numbers(add, 3, 4)
print(result)
def multiply(a, b):
return a * b
def double_and_multiply(x):
doubled = x * 2
return multiply(doubled, 3)
result = double_and_multiply(4)
print(result)
def calculate_square(x):
return x ** 2
def print_square(x):
print(x ** 2)
print(calculate_square(5)) # Returns the value
print_square(5) # Prints the value
def safe_divide(a, b):
try:
result = a / b
except ZeroDivisionError:
return "Cannot divide by zero"
return result
print(safe_divide(10, 2))
print(safe_divide(10, 0))
def greet(name: str, age: int) -> str:
return f"Hello {name}, you are {age} years old."
print(greet("Alice", 30))
def square(x):
return x ** 2
numbers = [1, 2, 3, 4, 5]
squares = [square(x) for x in numbers]
print(squares)
def outer_function():
outer_var = "I am outer"
def inner_function():
nonlocal outer_var
outer_var = "I am modified"
print(outer_var)
inner_function()
print(outer_var)
outer_function()
def outer_function(x):
def inner_function(y):
return x + y
return inner_function
add_five = outer_function(5)
print(add_five(3))
def process_data(data, callback):
result = callback(data)
return result
def add_ten(value):
return value + 10
print(process_data(5, add_ten))
def print_info(*args, **kwargs):
print("Arguments:", args)
print("Keyword Arguments:", kwargs)
def callback(x, y):
return x + y
print_info(1, 2, 3, a=4, b=5)
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
print(factorial(5))
import time
def timed_function():
start_time = time.time()
# Code to time
time.sleep(2)
end_time = time.time()
print(f"Execution time: {end_time - start_time} seconds")
timed_function()
def decorator_func(func):
def wrapper():
print("Something is happening before the function is called.")
func()
print("Something is happening after the function is called.")
return wrapper
@decorator_func
def say_hello():
print("Hello!")
say_hello()
from typing import Union
def greet(name: str) -> str:
return f"Hello, {name}!"
def greet(name: str, age: Union[int, None] = None) -> str:
if age:
return f"Hello, {name}. You are {age} years old."
return f"Hello, {name}!"
print(greet("Alice"))
print(greet("Alice", 30))
def add(a, b):
"""
Add two numbers together.
Parameters:
a (int or float): The first number.
b (int or float): The second number.
Returns:
int or float: The sum of a and b.
"""
return a + b
print(add.__doc__)