Learn Function in Python with 10 Exmaple
At 6/13/2023
In Python, a function is defined using the following syntax:
def function_name(parameters):
# Function body
# Statements
# Return statement (optional)
Let's break down each component of the function syntax:
def: This keyword is used to define a function in Python.
function_name: This is the name of the function you want to define. Choose a descriptive name that reflects the purpose of the function.
parameters: These are optional input values that you can pass to the function. Parameters are enclosed in parentheses and can be separated by commas if there are multiple parameters.
: (colon): It is essential to include a colon at the end of the function definition line. It indicates the start of the function body.
Function body: This is the block of code that makes up the function. It consists of one or more statements that define the functionality of the function.
Return statement (optional): The return statement is used to specify the value that the function should return. It is optional, and if omitted, the function will return None by default.
Here's an example of a simple function that adds two numbers and returns the result:
def add_numbers(a, b):
sum = a + b
return sum
In this example, the function is named add_numbers, and it takes two parameters a and b. The function calculates the sum of a and b and returns the result using the return statement.
You can call the function by using its name and passing the required arguments:
result = add_numbers(5, 3)
print(result) # Output: 8
This code calls the add_numbers function with arguments 5 and 3, and the returned result (8) is stored in the result variable, which is then printed to the console.
Here are 10 examples of Python functions that demonstrate different functionalities:
Simple Addition Function:
def add_numbers(a, b):
return a + b
result = add_numbers(5, 3)
print(result) # Output: 8
String Length Function:
def get_string_length(string):
return len(string)
length = get_string_length("Hello, World!")
print(length) # Output: 13
Even or Odd Function:
def check_even_odd(number):
if number % 2 == 0:
return "Even"
else:
return "Odd"
result = check_even_odd(7)
print(result) # Output: Odd
Max Function:
def find_max(numbers):
return max(numbers)
maximum = find_max([10, 5, 8, 20, 3])
print(maximum) # Output: 20
Factorial Function:
def calculate_factorial(number):
factorial = 1
for i in range(1, number + 1):
factorial *= i
return factorial
result = calculate_factorial(5)
print(result) # Output: 120
String Reversal Function:
def reverse_string(string):
return string[::-1]
reversed_str = reverse_string("Hello, World!")
print(reversed_str) # Output: "!dlroW ,olleH"
List Sum Function:
def sum_list(numbers):
return sum(numbers)
total = sum_list([1, 2, 3, 4, 5])
print(total) # Output: 15
Unique Elements Function:
def get_unique_elements(elements):
return list(set(elements))
unique = get_unique_elements([1, 2, 2, 3, 4, 4, 5])
print(unique) # Output: [1, 2, 3, 4, 5]
Temperature Conversion Function:
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
fahrenheit = celsius_to_fahrenheit(25)
print(fahrenheit) # Output: 77.0
File Reader Function:
def read_file(file_path):
with open(file_path, 'r') as file:
contents = file.read()
return contents
file_contents = read_file("sample.txt")
print(file_contents) # Output: Contents of the file
These examples cover a range of functionalities such as arithmetic operations, string manipulation, list operations, and file handling. Feel free to modify them or use them as a starting point for your own functions.