Assignment python
Computing Fundamentals using Python
   1. Basic function to add two numbers
   def sum1(n1,n2):#default arguments #1
     sum=n1+n2
     return sum
   s=sum1(5,10)
   print ("sum of two numbers",s)
   2. Function to check if a number is even or odd
   def check(x):#2
     if x%2==0:
       print("it is an even number",x)
     else:
       print("it is an odd nnumber",x)
   check(34)
def fact(x):#3
  if x==0:
    return 1
  return x*fact(x-1)
print(fact(5))
3.Function to calculate the factorial of a number
def fact(x):#3
  if x==0:
    return 1
  return x*fact(x-1)
print(fact(5))
4. Function to reverse a string
def rev(string):
  rev=string[::-1]
  return rev
print(rev("siri"))
5. Function to count the number of vowels in a string
def count_vowels(s):
  vowels=['A','E','I','O','U','a','e','i','o','u']
  return len ([char for char in s if char in vowels])
string="bca course"
total_vowels=count_vowels(string)
print("total vowels",total_vowels)
6. Function to find the maximum among three numbers
def find_max(a,b,c):#6
  return max (a,b,c)
print(find_max(12,30,2))
7. Function to generate a list of prime numbers up to a given number
def print_primes(n):
  for num in range (2,n+1):
    if num>1:
      for i in range (2,num):
        if (num% i )==0:
          break
      else:
        print(num)
print_primes(30)
8. Function to calculate the power of a number
def CalculatePower(N,X):
  P=1
  for i in range(1, X+1):
    P=P*N
  return P
N,X=2,3
print(CalculatePower(N,X))
N,X=3,4
print(CalculatePower(N,X))
9. Function to check if a string is a palindrome
def isPalindrome(s):
  return s == s[::-1]
s = "malayalam"
ans = isPalindrome(s)
if ans:
  print("Yes")
else:
  print("No")
10. Function to calculate the Fibonacci sequence up to n terms
FibArray = [0, 1]
def fibonacci(n):
  # Check is n is less
  # than 0
  if n < 0:
    print("Incorrect input")
  # Check is n is less
  # than len(FibArray)
  elif n < len(FibArray):
    return FibArray[n]
  else:
    FibArray.append(fibonacci(n - 1) + fibonacci(n - 2))
    return FibArray[n]
# Driver Program
print(fibonacci(9))
11. Function to check if a number is prime
def is_prime(n):
  if n <= 1:
    return False
  # Check divisibility from 2 to n-1
  for i in range(2, n):
    if n % i == 0:
         return False
  return True
n = 11
print(is_prime(n))
12. Function to calculate the sum of digits in a number
def getSum(n):
  sum = 0
  for digit in str(n):
   sum += int(digit)
  return sum
n = 12345
print(getSum(n))
13. Function to find the second largest number in a list
def find_second_largest(numbers):
  return sorted(set(numbers))[-2]
# Example usage
numbers = [10, 20, 4, 45, 99]
print("The second largest number is:", find_second_largest(numbers))
14. Implement a function to perform matrix multiplication
def matrix_multiply(A, B):
  # Get the dimensions of the matrices
  rows_A = len(A)
  cols_A = len(A[0])
  rows_B = len(B)
  cols_B = len(B[0])
  # Initialize the result matrix with zeros
  result = [[0 for _ in range(cols_B)] for _ in range(rows_A)]
  # Perform matrix multiplication
  for i in range(rows_A):
     for j in range(cols_B):
       for k in range(cols_A):
             result[i][j] += A[i][k] * B[k][j]
  return result
# Example usage
A = [[1, 2],
   [3, 4]]
B = [[5, 6],
   [7, 8]]
result = matrix_multiply(A, B)
for row in result:
  print(row)
15. Function to generate a password of given length
import random
import string
def generate_password(length):
  charecters=string.ascii_letters+string.digits+string.punctuation
  return ''.join(random.choice(charecters)for _ in range(length))
print(generate_password(13))