Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
In this tutorial, you'll learn how to create and use functions in Python. Functions are reusable blocks of code that perform a specific task. They help to organize your code, make it more modular, and reduce repetition.
To define a function in Python, use the def
keyword, followed by the function name, a pair of parentheses containing any input parameters, and a colon. The function's body is indented, and should contain the code to be executed when the function is called.
def greet(): print("Hello, World!") # Call the function greet()
You can pass input values to a function using parameters. Parameters are specified within the parentheses in the function definition, and you can pass arguments to the function when calling it.
def greet(name): print(f"Hello, {name}!") # Call the function with an argument greet("Alice")
You can specify default values for parameters, which are used if no argument is provided for that parameter when calling the function.
def greet(name="World"): print(f"Hello, {name}!") # Call the function without an argument greet() # Call the function with an argument greet("Alice")
To return a value from a function, use the return
keyword, followed by the value or expression you want to return.
def add(a, b): return a + b # Call the function and store the result result = add(5, 3) print(result) # Output: 8
A function can return multiple values using a tuple.
def divide(a, b): quotient = a // b remainder = a % b return quotient, remainder # Call the function and store the results quot, rem = divide(10, 3) print(f"Quotient: {quot}, Remainder: {rem}")
A docstring is a brief description of a function's purpose and usage. It is placed as the first statement within the function body, enclosed in triple quotes.
def greet(name): """ This function greets the person passed as a parameter. :param name: The name of the person to greet """ print(f"Hello, {name}!") # Call the function with an argument greet("Alice")
In this tutorial, you learned how to create and use functions in Python. Functions are an essential part of programming, allowing you to write reusable, modular code that is easy to maintain and understand.
How to define a function in Python:
def greet(name): print(f"Hello, {name}!") # Call the function greet("Alice")
Function arguments and parameters in Python:
def add_numbers(a, b): return a + b result = add_numbers(3, 4) print(result)
Python function return statement:
def square(x): return x ** 2 result = square(5) print(result)
Default arguments in Python functions:
def power(x, exponent=2): return x ** exponent result1 = power(3) result2 = power(3, 3) print(result1, result2)
Keyword arguments in Python functions:
def greet_person(name, greeting="Hello"): print(f"{greeting}, {name}!") greet_person(name="Alice")
Python lambda functions:
lambda
.square = lambda x: x ** 2 result = square(4) print(result)
Recursive functions in Python:
def factorial(n): if n == 0 or n == 1: return 1 else: return n * factorial(n-1) result = factorial(5) print(result)
Variable scope in Python functions:
global_variable = 10 def my_function(): local_variable = 5 print(global_variable, local_variable) my_function()
First-class functions in Python:
def square(x): return x ** 2 # Assign function to a variable my_function = square result = my_function(3) print(result)
Anonymous functions in Python:
lambda
.add = lambda x, y: x + y result = add(3, 4) print(result)
Python function decorators:
def my_decorator(func): def wrapper(): print("Something is happening before the function is called.") func() print("Something is happening after the function is called.") return wrapper @my_decorator def say_hello(): print("Hello!") say_hello()
Built-in functions in Python:
result = len([1, 2, 3]) print(result)
Python map()
and filter()
functions:
map()
and filter()
for iterable transformations.numbers = [1, 2, 3, 4, 5] squared_numbers = map(lambda x: x ** 2, numbers) filtered_numbers = filter(lambda x: x % 2 == 0, numbers) print(list(squared_numbers)) print(list(filtered_numbers))
Function chaining in Python:
def square(x): return x ** 2 def add_one(x): return x + 1 result = add_one(square(3)) print(result)
Function closures in Python:
def outer_function(message): def inner_function(): print(message) return inner_function my_closure = outer_function("Hello, closure!") my_closure()
Function annotations in Python:
def add_numbers(a: int, b: int) -> int: return a + b result = add_numbers(3, 4) print(result)