Master functions in Python with this beginner-friendly guide. Learn how to define functions, use return statements, default and keyword arguments, and solve practical coding tasks with real-world examples.
A function is a block of reusable code that performs a specific task. It’s reusable, which means you can call it multiple times in your program. This helps to organize your code, make it more readable, and avoid repetition.
Why Do We Use Functions?
We use functions in Python for several reasons:
To define a function, you use the def keyword followed by the function name, parentheses for parameters, and a colon. The code block that defines the function is indented.
Syntax:
def function_name(parameters):
# Function body
# Code to be executed
def greet(name):
print("Hello,", name + "!")
# Calling the function
greet("Ahmad") # Output: Hello, Ahmad!
Explanation:
def greet(name): defines a function named greet that takes one parameter, name.print("Hello,", name + "!") is the function body, which prints a greeting message using the provided name.greet("Ahmad") calls the function with the argument “Ahmad”.Key Points:
return statement.return keyword.def add(x, y):
return x + y
result = add(3, 5)
print(result) # Output: 8
Video: Learn How to Use Default Parameters in Function Definition
def greet(name, message="Hello"):
print(f"{message}, {name}!")
greet("Alice") # Uses default message "Hello"
greet("Alice", "Hi") # Overrides default with "Hi"
def multiply(a, b):
return a * b
result = multiply(b=3, a=5) # You can specify arguments in any order
Function Requirements:
calculate_area that takes two parameters: length and width.Input:
Output:
Expected Output
The area of the rectangle with length 5 and width 3 is: 15
Additional Test Cases
length = 7, width = 2
The area of the rectangle with length 7 and width 2 is: 14length = 10.5, width = 4.2
The area of the rectangle with length 10.5 and width 4.2 is: 44.1Function Requirements:
is_even that takes one parameter: number."Even" if the number is even, and "Odd" if the number is odd.Input:
Output:
"Even" or "Odd"Expected Output
The number 4 is: Even
Encourage beginners to test the function with various numbers:
num = 7
The number 7 is: Oddnum = -2
The number -2 is: Evennum = 0
The number 0 is: Evengreet that takes a name as an argument and a greeting message with a default value of “Hello”. If no greeting is provided, the function should use “Hello.”greet("Alice") should output "Hello, Alice!" and greet("Alice", "Good morning") should output "Good morning, Alice!"calc_price that accepts price, tax=0.05, and discount=0. Calculate the final price after applying tax and discount. Test with various keyword arguments to see how changes affect the result.calc_price(100), calc_price(100, discount=0.1), calc_price(100, tax=0.07, discount=0.1)
video: Guard Statements in Python: Explained Simply
def greet():
print("Hello World!")
greeting = greet
A)
print("Hello World!")
B)
def my_function():
print("Hello World!")
C)
if x == 10:
print("x is 10")
D)
x = 10
Answer: B
Tutorials, Roadmaps, Bootcamps & Visualization Projects