0% found this document useful (0 votes)
3 views2 pages

Functions in Python

The document contains a series of Python functions that perform various tasks, including adding two numbers, calculating factorials, generating Fibonacci series, checking even or odd numbers, finding the maximum of three numbers, checking for prime numbers, calculating simple interest, counting vowels in a string, reversing a string, and summing elements in a list. Each function is accompanied by example usage and output. This serves as a basic reference for common programming tasks in Python.

Uploaded by

ajaiadvocate37
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Functions in Python

The document contains a series of Python functions that perform various tasks, including adding two numbers, calculating factorials, generating Fibonacci series, checking even or odd numbers, finding the maximum of three numbers, checking for prime numbers, calculating simple interest, counting vowels in a string, reversing a string, and summing elements in a list. Each function is accompanied by example usage and output. This serves as a basic reference for common programming tasks in Python.

Uploaded by

ajaiadvocate37
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Q1: Function to Add Two Numbers

def add(a, b):


return a + b
# Example
result = add(10, 5)
print("Sum:", result)

2. Function to Find Factorial


def factorial(n):
fact = 1
for i in range(1, n + 1):
fact *= i
return fact

print("Factorial of 5:", factorial(5))

3. Recursive Function to Find Fibonacci Series


def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)

for i in range(10):
print(fibonacci(i), end=" ")

Output:
0 1 1 2 3 5 8 13 21 34

4. Function to Check Even or Odd


def even_odd(num):
if num % 2 == 0:
return "Even"
else:
return "Odd"
num=int(input(“enter no:”))
print("num is", even_odd(num))

5. Function to Find Maximum of Three Numbers


def maximum(a, b, c):
if a >= b and a >= c:
return a
elif b >= a and b >= c:
return b
else:
return c

print("Maximum is:", maximum(3, 9, 5))

6. Function to Check Prime Number


def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n/2) + 1):
if n % i == 0:
return False
return True

print("13 is prime:", is_prime(13))

7. Function to Calculate Simple Interest


def simple_interest(p, r, t):
return (p * r * t) / 100

print("Simple Interest:", simple_interest(1000, 5, 2))

8. Function to Count Vowels in a String


def count_vowels(s):
vowels = "aeiouAEIOU"
count = 0
for char in s:
if char in vowels:
count += 1
return count

print("Vowel count:", count_vowels("Python Programming"))

9. Function to Reverse a String


def reverse_string(s):
return s[::-1]

print("Reversed:", reverse_string("ChatGPT"))

10. Function to Find Sum of List Elements


def sum_list(numbers):
total = 0
for num in numbers:
total += num
return total

print("Sum of list:", sum_list([1, 2, 3, 4, 5]))

You might also like