Q.1 Write a program to create a simple calculator.
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
operator = input("Enter operation (+, -, *, /): ")
if operator == '+':
print("Result: ",num1 + num2)
elif operator == '-':
print("Result: ",num1 - num2)
elif operator == '*':
print("Result: ",num1 * num2)
elif operator == '/':
print("Result: ",num1 / num2)
else:
print("Invalid operator")
Q.2 Write a program to calculate the factorial of a number.
num = int(input("Enter a number: "))
factorial = 1
for i in range(1, num + 1):
factorial = factorial* i
print("The factorialof",num,"is",factorial)
Q.3 Write a program to calculate the sum of first n natural numbers.
num = int(input("How many natural numbers do you want to add? "))
sum = 0
for i in range(num+1):
sum+=i
print(sum)
Q.4 Write a program to calculate the simple interest.
P = float(input("Enter the principal amount: "))
R = float(input("Enter the rate of interest: "))
T = float(input("Enter the time (in years): "))
SI = (P * R * T) / 100
print("The simple interest is: ",SI)
Q. 5 Write a program to swap two numbers with and without using a third variable.
# Program to swap two numbers without using a third variable
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c=a
a=b
b=c
print("After swapping, the first number is: ",a," the second number is: ",b)
#.....OR.....
# Program to swap two numbers without using a third variable
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
a, b = b, a
print("After swapping, the first number is: ",a," the second number is: ",b)
Q6. Write a program to check the largest of the 3 numbers Program
a=int(input("enter a number A"))
b=int(input("enter a number B"))
c=int(input("enter a number C"))
if(a>=b) and (a>=c):
largest=a
elif(b>=a) and (b>=c):
largest=b
else:
largest=c
print('the largest number is',largest)
Q.7 Write a program to generate the first n terms of Fibonacci series
0,1,1,2,3,5,8,13,21…. .
n= int(input("How many terms to be printed? "))
a=0
b=1
print(a , end=" ")
print(b , end=" ")
# first two terms already printed. So Start with 3rd term
for i in range(3,n+1):
c=a+b
print(c , end=" ")
a=b
b=c
Q.8 Write a program to check whether its a prime number
a=int(input('enter a number A:'))
if a > 1:
for i in range(2, (a//2)+1):
if (a % i) == 0:
print(a, "is not a prime number")
break
else:
print(a, "is a prime number")
else:
print(a, "is not a prime number")
Q.9 WAP to Input the elements of a list from the user and find the maximum value
stored in a list
a = []
# Get the number of elements
n = int(input("Enter the number of elements: "))
# Append elements to the list
for i in range(n):
element = int(input("Enter element : "))
[Link](element)
print("List:", a)
# Assuming first element is largest.
largest = a[0]
# Iterate through list and find largest
for val in a:
if val > largest:
# If current element is greater than largest
# update it
largest = val
print("the largest number is:",largest)
Q.10 Write a program to search for a value in the list.
a=[12,4,10,9,7]
length=len(a)
element=int(input("Enter the number to be searched: "))
for i in range(0,length):
if element==a[i]:
print(" This number is found at index",i)
break
else:
print("the entered number is not found in list")
Q.11 Write a program to add the elements of two list.
list1 = [12, 9, 4, 7, 8]
list2 = [3, 5, 6, 12, 10]
new_list = []
for i in range(len(list1)):
new_list.append(list1[i] + list2[i])
# printing resultant list
print ("Resultant list is:", new_list)
[Link] a program to calculate mean ,median , mode using NUMPY.
import numpy as np
a = [1,2,2,4,5,6]
print([Link](a))
a = [1,2,2,4,5,6]
print([Link](a))
a = [1,2,2,2,4,5,6,6]
values,counts = [Link](a, return_counts=True)
# unique method provides the unique values and their count from array .
# [Link](a) can also be used .
mode = values[[Link](counts)]
print(mode)
Q.13 Create a 2 Dimensional array and perform following tasks.
a. Multiply each element of array by 2 and print the result.
b. Display the maximum value from the array.
c. Display row wise maximum value in the array.
d. Display column wise maximum value in the array.
e. Display the shape (rows,columns) and size of array.
import numpy as np
arr= [Link]([[2,5,4,6],[3,8,4,5]])
new=(arr*2)
print(new)
print([Link]())
print([Link](axis=1)) # 1 is x axis
print([Link](axis=0)) # 0 is y axis
print([Link]) # size of array is the product of no. of rows and columns
print([Link]) # shape of array is represented as no. of rows X columns
Q.14 Write a program to display a line chart with labeled x axis and y axis. Also use
proper chart titles.
import [Link] as plt
x=[1,2,3]
y=[5,7,4]
[Link](x,y,label="(x,y)",marker="*")
[Link]("numbers")
[Link]("values")
[Link]("LINE CHART")
[Link]()
Output
Q.15 Display an orange coloured bar graph showing the choice of favourite book
among the readers. Use appropriate chart title and labels.
import [Link] as plt
books=["Python","c++","Java","Perl"]
popularity=[20,18,14,16]
# Now use function [Link](x category ,y category,color="orange")
[Link](books,popularity,color="orange")
[Link]("Books")
[Link]("Popularity (No. of readers)")
[Link]("FAVOURITE BOOK")
[Link]()
Output