In Python, a function is a block of code that performs a specific task and can be called using a name. Functions help you organize your code, make it more readable, and reusable.
Here's an example of a simple function in Python:
def greet(name):
print("Hello, " + name)
greet("John") # prints "Hello, John"
In this example, the greet function takes in a parameter called name and prints a greeting message using the value of name.
To define a function in Python, you use the def keyword followed by the function name and a list of parameters in parentheses. The function code is indented and is executed when the function is called.
You can call a function by using its name followed by a set of parentheses, and passing any required arguments inside the parentheses.
You can also define default values for function parameters, which allows you to call the function without specifying a value for that parameter. If you don't specify a value for a parameter with a default value, the default value will be used.
def greet(name, greeting="Hello"):
print(greeting + ", " + name)
greet("John") # prints "Hello, John"
greet("John", "Hi") # prints "Hi, John"
Functions can also return a value using the return statement. For example:
def add(a, b):
return a + b
result = add(3, 4) # 7
Here are 15 examples of Python functions:
1) A function to calculate the area of a circle.
def circle_area(radius):
return 3.14 * radius * radius
area = circle_area(2) # prints 12.56
2) A function to check if a number is prime.
def is_prime(n):
if n < 2:
return False
for i in range(2, n):
if n % i == 0:
return False
return True
print(is_prime(2)) # True
print(is_prime(3)) # True
print(is_prime(4)) # False
3) A function to reverse a string.
def reverse_string(s):
return s[::-1]
print(reverse_string("hello")) # olleh
4) A function to calculate the factorial of a number.
def factorial(n):
if n == 0:
return 1
return n * factorial(n-1)
print(factorial(5)) # 120
5) A function to check if a string is a palindrome.
def is_palindrome(s):
return s == s[::-1]
print(is_palindrome("racecar")) # True
print(is_palindrome("hello")) # False
6) A function to generate a list of prime numbers.
def generate_primes(n):
primes = []
for i in range(2, n+1):
is_prime = True
for j in range(2, i):
if i % j == 0:
is_prime = False
break
if is_prime:
primes.append(i)
return primes
print(generate_primes(10)) # [2, 3, 5, 7]
7) A function to calculate the Fibonacci sequence.
def fibonacci(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n-1) + fibonacci(n-2)
print(fibonacci(10)) # 55
8) A function to sort a list of numbers.
def bubble_sort(numbers):
for i in range(len(numbers)-1):
for j in range(len(numbers)-1-i):
if numbers[j] > numbers[j+1]:
numbers[j], numbers[j+1] = numbers[j+1], numbers[j]
return numbers
print(bubble_sort([5, 2, 7, 1, 3])) # [1, 2, 3, 5, 7]
9) A function to calculate the average of a list of numbers.
def average(numbers):
return sum(numbers) / len(numbers)
print(average([1, 2, 3])) # 2
10) A function to check if a string is a palindrome.
def is_palindrome(s):
s = s.lower()
s = s.replace(" ", "")
return s == s[::-1]
print(is_palindrome("A man a plan a canal Panama")) # True
print(is_palindrome("racecar")) # True
print(is_palindrome("Hello world")) # False
11) A function to find the maximum value in a list.
def max_value(numbers):
return max(numbers)
print(max_value([1, 2, 3, 4, 5])) # 5
12) A function to check if a number is even.
def is_even(n):
return n % 2 == 0
print(is_even(2)) # True
print(is_even(3)) # False
13) A function to calculate the greatest common divisor (GCD) of two numbers.
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
print(gcd(10, 15)) # 5
14) A function to convert a list of strings to a single string.
def list_to_string(lst):
return " ".join(lst)
print(list_to_string(["This", "is", "a", "string"])) # "This is a string"
15) A function to check if a string starts with a specific prefix.
def starts_with(s, prefix):
return s.startswith(prefix)
print(starts_with("Hello world", "He")) # True
print(starts_with("Hello world", "he")) # False