Recording the history of Nellore

Uncategorized

Python Exercise6

Write a function to read each char from the word if the char matches with the word find the index of that char,store it in indices return indices

 def get_indices(word,char):

    indicies=[]

    for x in range(len(word)):

        if word[x]==char:

            indicies.append(x)

    print(indicies)

get_indices(“apple”,”p”)

 

Write a function to calculate the factorial of a given number

def factorial(n):

    fact=1

    for i in range(2,n+1):

        fact=fact*i

    return fact

print(factorial(5))

 

Write a function to find fibonacci series

def fib(x):

    a, b = 0, 1

    while a < x:

        print(a)

        a, b = b, a+b

print( “Fibonacci series..” )

fib(50)

 

Program make a simple calculator that can add, subtract, multiply and divide using functions

Select operation.

1.Add

2.Subtract

3.Multiply

4.Divide

Enter choice(1/2/3/4):

 

# This function adds two numbers

def add(x, y):

   return x + y

 # This function subtracts two numbers

def subtract(x, y):

   return x – y

 # This function multiplies two numbers

def multiply(x, y):

   return x * y

 # This function divides two numbers

def divide(x, y):

   return x / y

print(“Select operation.”)

print(“1.Add”)

print(“2.Subtract”)

print(“3.Multiply”)

print(“4.Divide”)

# Take input from the user

choice = input(“Enter choice(1/2/3/4):”)

num1 = int(input(“Enter first number: “))

num2 = int(input(“Enter second number: “))

if choice == ‘1’:

   print(num1,”+”,num2,”=”, add(num1,num2))

elif choice == ‘2’:

   print(num1,”-“,num2,”=”, subtract(num1,num2))

elif choice == ‘3’:

   print(num1,”*”,num2,”=”, multiply(num1,num2))

elif choice == ‘4’:

   print(num1,”/”,num2,”=”, divide(num1,num2))

else:

   print(“Invalid input”)

 

Write a program to calculate the average of number in a given list

Note: Take input from the user,ie., Number of elements to be inserted:

and take the input for n number of times

 

def average(rep):

    li = []

    for numb in range(rep):

        elem = int(input(“Enter Element : “))  

        li.append(elem)               #appending element to the list

    average=sum(li)/rep               #Finding the average

    print(“Average of the list: “,round(average,2))

 

average(int(input(“Enter number of elements to be inserted: “)))
 

 

 

Back to Main Page

Visit www.mongofactory.com to know more about the trainer.

Leave a Reply

Discover more from Nellorean

Subscribe now to keep reading and get access to the full archive.

Continue reading