-
Define a function that prints Hello Python! on the console.
'' print_hello_python(): print("Hello Python!")
-
Modify the function that accepts a string and prints it on the console.
def print_string(''): print(input_string) print_string("Hello Python!")
-
This function takes a number and doubles it. Return the doubled value.
def double_number(number): result = number * 2 '' result result = double_number(5) print('The doubled number is:', result)
-
Combine the math operation and return into a single line instead of two lines.
def double_number(number): # result = number * 2 '' result = double_number(5) print('The doubled number is:', result)
-
How does this function work?
def is_positive(number): if number > 0: return True else: return False
-
What is the output of this code?
def check_stock_availability(quantity): if quantity > 0: return "Product is in stock." else: return "Product is out of stock." result = check_stock_availability(12) print(result)
-
What is the output of this code?
def check_password_strength(password): if len(password) >= 8: return "Strong password" else: return "Weak password" password = "P@ssw0rd" result = check_password_strength(password) print(result)
-
What is the output of this code?
def calculate_shipping_cost(weight, destination): if weight <= 0: return "Invalid weight" if destination == "domestic": shipping_cost = 0.5 * weight return f"Domestic shipping: ${shipping_cost}" if destination == "international": shipping_cost = 1.5 * weight return f"International shipping: ${shipping_cost}" return "Invalid destination" weight = 2.5 destination = "international" result = calculate_shipping_cost(weight, destination) print(result)
-
What does this function return?
def check_number(number): if number > 0: return 'Positive' elif number < 0: return 'Negative' return 'Zero' result = check_number(0) print(result)
-
What is the output of this code?
def calculate_shipping_cost(weight, destination): if weight <= 0: return "Invalid weight" if destination == "domestic": shipping_cost = 0.5 * weight return f"Domestic shipping: ${shipping_cost}" if destination == "international": shipping_cost = 1.5 * weight return f"International shipping: ${shipping_cost}" return "Invalid destination." weight = 2 destination = "internationl" result = calculate_shipping_cost(weight, destination) print(result)
-
This function generates a username based on first name, last name and separator. What is the output of this code?
def generate_username(first_name, last_name, separator): username = f'{first_name}{separator}{last_name}' return username first_name = 'Emma' last_name = 'Smith' result = generate_username(first_name, last_name, '_') print(result)
-
This function generates a username based on the first name, last name, and separator. Modify the function to utilize an underline as the optional value.
def generate_username(first_name, last_name, ''): username = f'{first_name}{separator}{last_name}' return username first_name = 'Emma' last_name = 'Smith' result = generate_username(first_name, last_name, '.') print(result)
-
What is the output of this code?
def generate_username(first_name, last_name, separator = '_'): username = f'{first_name}{separator}{last_name}' return username first_name = 'Emma' last_name = 'Smith' result = generate_username(first_name, last_name) print(result)
-
What is the output of this code?
def generate_username(first_name, last_name, separator = '_'): username = f'{first_name}{separator}{last_name}' return username first_name = 'Emma' last_name = 'Smith' result = generate_username(first_name, last_name, '.') print(result)
Python Exercises for Write Your Functions
If you've faced difficulties with these exercises, take a look at Write Your Functions Module on my Python course.
You can also share 'Python Write Your Functions' Exercises on your:
X (Twitter) - Telegram - Linkedin - Facebook - or on your favorite platform...