Python
Python
Python
Ans)
# WAP to show functionality of a basic calculator using functions.
#Made by Nishant
# program to add
def add(x, y):
return x + y
# program to subtract
def subtract(x, y):
return x - y
# program to multiply
def multiply(x, y):
return x * y
# program to divide
def divide(x, y):
return x / y
# printing parts
print("Select Operations")
print("Press 1 - Add No.s")
print("Press 2 - Subtract No.s")
print("Press 3 - Multiply No.s")
print("Press 4 - Divide No.s")
print("Press 5 - Continue")
# Programing Parts
while True:
ch = int(input("Choose operation 1/2/3/4/5: "))
# Selection The Program
if ch == 1:
num1 = float(input("Enter First No.: "))
num2 = float(input("Enter Second No.: "))
print(num1, '+', num2, '=', add(num1, num2))
elif ch == 2:
num1 = float(input("Enter First No.: "))
num2 = float(input("Enter Second No.: "))
print(num1, '-', num2, '=', subtract(num1, num2))
elif ch == 3:
num1 = float(input("Enter First No.: "))
num2 = float(input("Enter Second No.: "))
print(num1, '*', num2, '=', multiply(num1, num2))
elif ch == 4:
num1 = float(input("Enter First No.: "))
num2 = float(input("Enter Second No.: "))
print(num1, '/', num2, '=', divide(num1, num2))
elif ch == 5:
ans = input("Do You Want To End Calculator (Y/N): ")
if ans.upper() == 'Y':
break
OUTPUT
2. WAP USING a function in python which accept a number from user to return True, if the
number is a prime number else return False. Use this function to print all prime
numbers from 1 to 100.
Ans) # WAP USING a function in python which accept a number from user to
return True,
# if the number is a prime number else return False.
# Use this function to print all prime numbers from 1
# Made By Nishant
num = int(input("Enter a number: "))
# If given number is greater than 1 and less then 100
if 1 < num < 100:
# Iterate from 2 to n / 2
for i in range(2, int(num / 2) + 1):
# If num is divisible by any number between
# 2 and n / 2, it is not prime
if (num % i) == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")
else:
print(num, "is not a prime number")
OUTPUT
3. WAP USING a function in python which accept a list of marks of students and return the
Ans) # Python Program to find Total, Average, and Percentage of Five Subjects
# Made By Nishant
l = []
while True:
# Student's Details Program
name = input("Enter Name Of Student: ")
Class = int(input("Enter Class: "))
roll = int(input("Enter Roll no.: "))
# Adding Marks
english = float(input("Please enter English Marks: "))
math = float(input("Please enter Math score: "))
computers = float(input("Please enter Computer Marks: "))
physics = float(input("Please enter Physics Marks: "))
chemistry = float(input("Please enter Chemistry Marks: "))
# Creating A List and Add marks and and divide by 5 for average marks
data = [name, Class, roll, english, math, computers, physics, chemistry]
total = english + math + computers + physics + chemistry
average = total / 5
percentage = (total / 500) * 100
# printing section
print("\nTotal Marks = %.2f" % total)
print("Average Marks = %.2f" % average)
print("Marks Percentage = %.2f" % percentage)
# adding more or not
ans = input("Do You Want To add more (Y/N): ")
if ans.upper() == 'N':
break
OUTPUT
4. WAP to read a text file “myfile.txt” line by line and display each word separated by a #.
Ans) # WAP to read a text file “myfile.txt” line by line and display each
word separated by a #.
# Made By Nishant
fh = open(r"myfile.txt", "r")
item = []
a = ""
while True:
a = fh.readline()
words = a.split()
for j in words:
item.append(j)
if a == "":
break
print("\t\n#".join(item))
OUTPUT
5. WAP to read a text file “myfile.txt” and display the number of vowels/ consonants/
Ans) # WAP to read a text file “myfile.txt” and display the number of vowels/
consonants/
# uppercase/ lowercase characters in the file.
# Made By Nishant
Ans) # Remove all the lines that contain the character `a' in a file and
write it to another file.
# Made By Nishant
# Create a New File and write something.
fo=open("hp.txt","w")
fo.write("Harry Potter\n")
fo.write("There is a difference in all harry potter books\nWe can see it as
harry grows\nthe books were written by J.K rowling ")
fo.close()
fo=open('hp.txt','r')
fi=open('writehp.txt','w')
l=fo.readlines()
for i in l:
if 'a' in i:
i=i.replace('a','')
fi.write(i)
fi.close()
fo.close()
OUTPUT
7. Write a program to create a text file and print the lines starting with ‘T’ or ‘P’. (Both
Ans) # Write a program to create a text file and print the lines starting
with ‘T’ or ‘P’. (Both uppercase and lowercase).
# Made by Nishant
# Read Any Text File And Make New File For Print
fin = open("myfile.txt", "r")
fout = open("story.txt", "w")
# Read All Lines
s = fin.readlines()
# Programing Area
for i in s:
if 'p' in i:
fout.write(i)
fin.close()
fout.close()
8. Read a text file to print the frequency of the word ‘He’ and ‘She’ found in the file.
Ans) f = open("heshe.txt","r")
count = 0
county = 0
t = f.read()
p = t.split()
for i in p:
if i == "he":
count = count + 1
elif i == "she":
county = county + 1
print("Frequency of 'he' is: ", count)
print("Frequency of 'she' is: ", county)
OUTPUT
9. Create a binary file with name and roll number. Search for a given roll number and
Ans) # Create a binary file with name and roll number. Search for a given
roll number and
# display the name, if not found display appropriate message.
import pickle
the marks.
12. Read a CSV file (containing item no, name, rate, QOH) from hard disc and print all the
13. Create a CSV file by entering user-id and password, read and search the password for
given userid.
Ans) # Create a CSV file by entering user-id and password, read and search
the password for given userid.
import csv
(simulates a dice). Throw the two dices for 10 times and print their total.
Ans) # Write a random number generator that generates random numbers between
1 and 6 (simulates a dice). Throw the two dices for 10 times and print their
total.
import random
min1 = 1
max2 = 6
roll_again = "y"
while roll_again.upper() == "Y":
print("Rolling the dice...")
val = random.randint(min1, max2)
print("You get... :", val)
roll_again = input("Roll the dice again? (y/n)...")
OUTPUT
15. WAP in Python to demonstrate linear search.
17. WAP to pass an integer list as stack to a function and push only those elements in the