0% found this document useful (0 votes)
0 views

Functions in Python

Uploaded by

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

Functions in Python

Uploaded by

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

P1::

def add_two_numbers(a, b):


return a + b

# Example usage:
result = add_two_numbers(5, 3)
print("Sum:", result)

P2::

def max_of_two(a, b):


if a > b:
return a
else:
return b

# Example usage:
result = max_of_two(10, 20)
print("Maximum:", result)

P3::

def max_of_two(a, b):


if a > b:
return a
else:
return b

# Example usage:
result = max_of_two(10, 20)
print("Maximum:", result)

P4::

def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n-1)

# Example usage:
result = factorial(5)
print("Factorial of 5:", result)

p5::

def is_palindrome(s):
return s == s[::-1]

# Example usage:
result = is_palindrome("radar")
print("Is 'radar' a palindrome?", result)

P6::
def sum_of_list(lst):
total = 0
for num in lst:
total += num
return total

# Example usage:
numbers = [1, 2, 3, 4, 5]
result = sum_of_list(numbers)
print("Sum of list:", result)

P7::

def string_length(s):
length = 0
for char in s:
length += 1
return length

# Example usage:
result = string_length("Hello")
print("Length of 'Hello':", result)

P8::

def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32

# Example usage:
result = celsius_to_fahrenheit(25)
print("25°C in Fahrenheit is:", result)

P9::

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

# Example usage:
result = is_prime(11)
print("Is 11 prime?", result)

P10::

def gcd(a, b):


while b:
a, b = b, a % b
return a

# Example usage:
result = gcd(48, 18)
print("GCD of 48 and 18 is:", result)

P11::

def check_number(n):
if n > 0:
return "Positive"
elif n < 0:
return "Negative"
else:
return "Zero"

# Example usage:
result = check_number(-5)
print("The number is:", result)

P12::

import math

def area_of_circle(radius):
return math.pi * (radius ** 2)

# Example usage:
result = area_of_circle(5)
print("Area of the circle is:", result)

p13::

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

# Example usage:
result = count_vowels("Hello World")
print("Number of vowels:", result)

p14::

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

# Example usage:
result = count_vowels("Hello World")
print("Number of vowels:", result)
P15::

def is_leap_year(year):
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
return True
else:
return False

# Example usage:
result = is_leap_year(2024)
print("Is 2024 a leap year?", result)

P16::

def min_of_three(a, b, c):


return min(a, b, c)

# Example usage:
result = min_of_three(10, 5, 7)
print("Minimum of the three numbers is:", result)

P17::

def sum_of_digits(n):
total = 0
while n > 0:
digit = n % 10
total += digit
n = n // 10
return total

# Example usage:
result = sum_of_digits(1234)
print("Sum of digits:", result)

P18::

def longest_word(words):
longest = ""
for word in words:
if len(word) > len(longest):
longest = word
return longest

# Example usage:
words = ["apple", "banana", "cherry", "strawberry"]
result = longest_word(words)
print("Longest word:", result)

P19::

def average_of_list(lst):
total = sum(lst)
count = len(lst)
return total / count
# Example usage:
result = average_of_list([10, 20, 30, 40, 50])
print("Average:", result)

You might also like