Learn Python variables with this beginner-friendly guide. Understand variable naming rules, assignments, and operations with examples and exercises. Perfect for students and professionals starting their Python journey.
“The only way to do great work is to love what you do.”
– Steve Jobs
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
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: 14
length = 10.5
, width = 4.2
The area of the rectangle with length 10.5 and width 4.2 is: 44.1
Function 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: Odd
num = -2
The number -2 is: Even
num = 0
The number 0 is: Even
You can assign default values to parameters, which makes them optional when calling the function.
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
greet
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
Which of the following will cause a syntax error due to incorrect indentation in Python?
A)
print("Hello World!")
B)
def my_function():
print("Hello World!")
C)
if x == 10:
print("x is 10")
D)
x = 10
Answer: B
Parameters are defined by the names that appear in a function definition, whereas arguments are the values actually passed to a function when calling it. Parameters define what kind of arguments a function can accept.
See also the FAQ
question of Python Documentation
on the difference between arguments and parameters.
def greet(name): # 'name' is a parameter
print("Hello,", name + "!")
greet("Alice") # "Alice" is an argument
In this example:
name
is a parameter in the function greet
."Alice"
is an argument passed to the function when it’s called.To summarize:
Think of it like this: