P1 - Write a Python function to find the maximum of three numbers.

In [ ]:
def max_of_three(x, y, z):
    if x >= y and x >= z:
        print("Maximum is 1st")
        return x
    elif y >= x and y >= z:
        print("Maximum is 2nd")
        return y
    else:
        return z

m = max_of_three(3, 6, -5)  # Output: 6

m*6
max_of_three(1,5,-6)
Maximum is 2nd
Maximum is 2nd
Out[ ]:
5
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 

P2 - Write a Python function to sum all the numbers in a list.

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

print(sum_list([8, 2, 3, 0, 7]))  # Output: 20
20
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 

P3 - Write a Python function to multiply all the numbers in a list.

In [ ]:
def multiply_list(numbers):
    product = 1
    for num in numbers:
        product *= num
    return product

print(multiply_list([8, 2, 3, -1, 7]))  # Output: -336
-336
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 

P4 - Write a Python program to reverse a string.

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

print(reverse_string("1234abcd"))  # Output: "dcba4321"
dcba4321
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 

P5 - Can you write a function that can take any number of numeric arguments and return their total?

In [ ]:
def sum_numbers(*args):
    total = 0
    for num in args:
        total += num
    return total

print(sum_numbers(1, 2, 3))  # Output: 6
print(sum_numbers(10, 20, 30, 40))  # Output: 100
print(sum_numbers(5))  # Output: 5
6
100
5
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 

P6 - Write a Python function that checks whether a passed string is a palindrome or not.

In [ ]:
",".join(["A","B","C"])
Out[ ]:
'A,B,C'
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
def is_palindrome(s):
    return s == s[::-1]

print(is_palindrome("madam"))  # Output: True
print(is_palindrome("nurses run"))  # Output: True
print(is_palindrome("hello"))  # Output: False
True
False
False
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 

P7 - Write a Python function that takes a number as a parameter and checks whether the number is prime or not.

In [ ]:
def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, n):
        if n % i == 0:
            return False
    return True

print(is_prime(7))  # Output: True
print(is_prime(10))  # Output: False
True
False
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 

P8 - Sum of n Natural Numbers

In [ ]:
def sum_natural(n):
    if n == 1:
        return n  # Base case
    else:
        return n + sum_natural(n - 1)  # Recursive case

print(sum_natural(3))  # Output: 6 (1 + 2 + 3)
6
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 

P9 - Fibonacci with Recursion

In [ ]:
def fibonacci_series(n):
    if n <= 0:
        return []
    elif n == 1:
        return [0]
    else:
        series = [0, 1]
        while len(series) < n:
            series.append(series[-1] + series[-2])
        return series

# Recursive version for nth Fibonacci (as per mathematical equation):
def fib(n):
    if n == 0 or n == 1:
        return n
    else:
        return fib(n-1) + fib(n-2)

print(fibonacci_series(6))  # Output: [0, 1, 1, 2, 3, 5]

# For nth Fibonacci:
print(fib(6))  # Output: 8
[0, 1, 1, 2, 3, 5]
8
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]: