0% found this document useful (0 votes)
5 views8 pages

Python Programs for Basic Calculations

The document contains a series of Python programming exercises aimed at performing various tasks such as calculating squares and cubes, finding the largest number, and performing arithmetic operations based on user input. It also includes file handling operations like reading from and writing to text files, counting specific characters, and filtering lines based on conditions. Each exercise is accompanied by sample code and expected output to illustrate the functionality.

Uploaded by

chopraneerav6
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

Python Programs for Basic Calculations

The document contains a series of Python programming exercises aimed at performing various tasks such as calculating squares and cubes, finding the largest number, and performing arithmetic operations based on user input. It also includes file handling operations like reading from and writing to text files, counting specific characters, and filtering lines based on conditions. Each exercise is accompanied by sample code and expected output to illustrate the functionality.

Uploaded by

chopraneerav6
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd

Name : Aryan

Class : 12th Non-Medical [Aryabhatta]

[Link] : 15
Q1) Python program to input a number,calculate and print its square and cube

num = int(input("Enter any Number : "))

cube = num ** 3

square = num ** 2

print("Square of", num, ":", square)

print("Cube of", num, ":", cube)

OUTPUT => Enter any Number : 5

Square of 5 : 25

Cube of 5 : 125

Q2) Python program to input two numbers,calculate their sum, product and difference.

num1 = int(input("Enter First Number : "))

num2 = int(input("Enter Second Number : "))

sum = num1 + num2

prod = num1 * num2

diff = num1 - num2

print("Sum of",num1,"&",num2,":",sum)

print("Difference between",num1,"&",num2,":",diff)

print("Product of",num1,"&",num2,":",prod)

OUTPUT => Enter First Number : 20

Enter Second Number : 2

Sum of 20 & 2 : 22

Difference between 20 & 2 : 18

Product of 20 & 2 : 40
Q3) Python program to input three numbers,check and print the largest number.

num1 = int(input("Enter First Number : "))

num2 = int(input("Enter Second Number : "))

num3 = int(input("Enter Third Number : "))

largest = num1

if num2 > largest:

largest = num2

if num3 > largest:

largest = num3

print(largest,"is the largest of the three Numbers.")

OUTPUT => Enter First Number : 62

Enter Second Number : 45

Enter Third Number : 75

75 is the largest of the three Numbers.

Q4) Python program to input two numbers and an operator(+,-,*,/).Based on operator calculate and print the
result.

num1 = int(input("Enter First Number : "))

num2 = int(input("Enter Second Number : "))

op = input("Choose any Operator [ + , - , * , /] : ")

if op == '+':

print("Sum of", num1, "&", num2, ":", (num1 + num2))

elif op == '-':

print("Difference between", num1, "&", num2, ":", (num1 - num2))

elif op == '*':

print("Product of", num1, "&", num2, ":", (num1 * num2))

elif op == '/':

print("Division of", num1, "by", num2, ":", (num1 / num2))


else:

print("Invalid Operator!")

OUTPUT => Enter First Number : 45

Enter Second Number : 5

Choose any Operator [ + , - , * , /] : +

Sum of 45 & 5 : 50

Q5) Python program to input a number and print its table.

num = int(input("Enter any Number : "))

for i in range(1,11):

print(num, "*", i, "=", (num * i))

OUTPUT=>

Enter any Number : 9

9*1=9

9 * 2 = 18

................

9 * 10 = 90

Q6) Python program to input a number and print its factorial.

num = int(input("Enter any Number : "))

fact = 1

for i in range(1,num+1):

fact=fact*i

print("Factorial of", num, ":",fact)

OUTPUT => Enter any Number : 5

Factorial of 5 : 120

Q7) Python program to input a year and check whether the year is leap or not

year = int(input("Enter Year : "))

if year % 4 == 0:
print(year, "is a leap year")

else:

print(year, "is not a leap year")

OUTPUT => Enter Year : 2025

2025 is not a leap year

Q8) Python program to calculate simple interest using a function interest() that can receive amount, time and
rate and returns calculated simple interest. Do specify default values for rate and time as 20% and 3 years
respectively.

def interest(amount, rate = 20, time = 3):

return (amount * rate * time) / 100

amount = int(input("Enter Principal Amount : "))

rate = int(input("Enter Rate of interest : "))

time = int(input("Enter time Period [Years] : "))

print("Simple Interest : ",interest(amount,rate,time))

OUTPUT => Enter Principal Amount : 200

Enter Rate of interest : 13

Enter time Period [Years] : 5

Simple Interest : 130.0

Q9) Python program to read a text file line by line and display each word separated by'#'

f=open("[Link]")

l = [Link]()

for i in l:

print([Link](" ","#"),end="")

OUTPUT => hello#world#

this#is#a#pyhton#program

Q10) Python program to read a the content from [Link] line by line and display the same on screen

f=open("[Link]")

print([Link]())
OUTPUT => hello world

this is a pyhton program

Q11) Write a method in python to read [Link] and display those lines starting by A

f=open("[Link]")

for i in [Link]():

if [Link]("A"):

print(i)

OUTPUT =>Apple

Q12) Write a method in python to count the [Link] line in [Link] which are starting with A

f=open("[Link]")

c=0

for i in [Link]():

if [Link]("a"):

c+=1

print("Number of lines starting with A : ",c)

OUTPUT =>1

Q13) Write a method in python to read lines from [Link] and display those starting with A or D

f=open("[Link]")

for i in [Link]():

if [Link]("A") or [Link]("D"):

print(i)

OUTPUT =>Apple

Q14) Write a method in python BIGLINES() to read lines from [Link] and display those lines which are bigger
than 50 characters

def biglines():

f=open("[Link]")

for i in [Link]():
if len(i)>50:

print(i)

biglines()

OUTPUT =>HELLO WORLD THIS A LINE WHICH HAVE MORE THAN 50 CHARACTERS AND WILL BE PRINTED ON
SCREEN

Q15) Write a method in python to count total number of words in a file

def words():

f=open("[Link]")

print("No. of Words : ",len(str([Link]()).split(" ")))

words()

OUTPUT =>7

Q16) Write a method in python to read lines from a text file [Link] to find and display occurence of word class

def countclass():

f=open("[Link]")

c=0

for i in str([Link]()).split():

if i == "class":

c+=1

print("Class is found : ",c,"times")

countclass()

OUTPUT =>Class is found : 3 times

Q17) Write a method BIGWORDS() in python to read content from a text file and display the occurence of those
words which are having 5 or more characters

def bigwords():

f=open("[Link]")

c=0

for i in str([Link]()).split():

if len(i)>4:
c+=1

bigwords()

OUTPUT =>4

Q18) Write a method ISUPTOCOUNT() in python to read content from a text file and display the occurence of
those word is,up or to

def isuptocount():

f=open("[Link]")

c=0

for i in str([Link]()).split():

if i in ['is','up','to']:

c+=1

print(c)

OUTPUT =>1

Q19) Write a program to read a text file and display the number of vowels/constants/uppercase/lowercase
characters in file

f=open("[Link]")

l=[0,0,0,0]

for i in [Link]().replace(" ",""):

if i in ['a','e','i','o','u','A','E','I','O','U']:

l[0]+=1

else:

l[1]+=1

if [Link]():

l[2]+=1

if [Link]():

l[3]+=1

print("Vowels : ",l[0])

print("Consonants : ",l[1])
print("Uppercase characters : ",l[2])

print("Lowercase characters : ",l[3])

OUTPUT => Vowels : 9

Consonants : 21

Uppercase characters : 0

Lowercase characters : 30

Q20) Write a program to remove all the lines that contain the character a in a file and write it to another file

f=open("[Link]")

buffer = [Link]()

f1=[]

f2=[]

[Link]()

fn1=open("[Link]","w")

fn2=open("[Link]","w")

for i in buffer:

if 'a' in i:

f1+=[i]

else:

f2+=[i]

[Link](f1)

[Link](f2)

[Link]()

[Link]()

You might also like