0% found this document useful (0 votes)
6 views1 page

Python Functions

The document contains Python functions that demonstrate various programming concepts. It includes functions for adding numbers, finding the square of a number, checking if a number is even or odd, reversing a string, checking if a number is prime, and generating a Fibonacci series. Each function is followed by a print statement to display the results of their execution.

Uploaded by

prabh2007426
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)
6 views1 page

Python Functions

The document contains Python functions that demonstrate various programming concepts. It includes functions for adding numbers, finding the square of a number, checking if a number is even or odd, reversing a string, checking if a number is prime, and generating a Fibonacci series. Each function is followed by a print statement to display the results of their execution.

Uploaded by

prabh2007426
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

1 program to illustrate use of functions

def add_numbers(a, b):


return a + b

# Function to find square of a number


def square(num):
return num * num

# Function to check even or odd


def check_even_odd(num):
if num % 2 == 0:
return f"{num} is Even"
else:
return f"{num} is Odd"

# Main program
print("Addition:", add_numbers(5, 3))
print("Square:", square(4))
print(check_even_odd(7))

2 TO reverse a string
def reverse_string(s):
return s[::-1]

print("Reversed string:", reverse_string("Python"))

3 Check if number is prime or not


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

print("Is 13 prime?", is_prime(13))

4 fibonacci series
def fibonacci(n):
a, b = 0, 1
series = []
for _ in range(n):
[Link](a)
a, b = b, a + b
return series

print("Fibonacci series (first 7 terms):", fibonacci(7))

You might also like