Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Write a python script to take input for 2 numbers and an

operator (+ , – , * , / ). Based on the operator calculate and print


the result?

a=int(input("Enter 1st no "))


b=int(input("Enter 2nd no "))
op=input("Enter the operator (+,-,,/) ")
if(op=="+"):
c=a+b
print("Sum = ",c)
elif(op=="*"):
c=a*b
print("Product = ",c)
elif(op=="-"):
if(a>b):
c=a-b
else:
c=b-a
print("Difference = ",c)
elif(op=="/"):
c=a/b
print("Division = ",c)
else:
print("Invalid operator")

Write a python script to take input for a number and print its
factorial?

n=int(input("Enter any no "))


i=1
f=1
while(i<=n):
f=f*i
i=i+1
print("Factorial = ",f)
Write a python script to take input for a number check if the
entered number is Armstrong or not.

n=int(input("Enter the number to check : "))


n1=n
s=0
while(n>0):
d=n%10;
s=s + (d *d * d)
n=int(n/10)
if s==n1:
print("Armstrong Number")
else:
print("Not an Armstrong Number")
Write a python program to maintain book details like book
code, book title and price using stacks data structures?
(implement push(), pop() and traverse() functions)
"""
push
pop
traverse
"""
book=[]
def push():
bcode=input("Enter bcode ")
btitle=input("Enter btitle ")
price=input("Enter price ")
bk=(bcode,btitle,price)
book.append(bk)
def pop():
if(book==[]):
print("Underflow / Book Stack in empty")
else:
bcode,btitle,price=book.pop()
print("poped element is ")
print("bcode ",bcode," btitle ",btitle," price ",price)
def traverse():
if not (book==[]):
n=len(book)
for i in range(n-1,-1,-1):
print(book[i])
else:
print("Empty , No book to display")
while True:
print("1. Push")
print("2. Pop")
print("3. Traversal")
print("4. Exit")
ch=int(input("Enter your choice "))
if(ch==1):
push()
elif(ch==2):
pop()
elif(ch==3):
traverse()
elif(ch==4):
print("End")
break
else:
print("Invalid choice")
output

1. Push
2. Pop
3. Traversal
4. Exit
Enter your choice 1
Enter bcode 101
Enter btitle python
Enter price 254
1. Push
2. Pop
3. Traversal
4. Exit
Enter your choice 3
(‘101’, ‘python’, ‘254’)
1. Push
2. Pop
3. Traversal
4. Exit
Enter your choice
Write a python program to read a file named “article.txt”, count
and print the following:
(i). length of the file(total characters in file)
(ii).total alphabets
(iii). total upper case alphabets
(iv). total lower case alphabets
(v). total digits
(vi). total spaces
(vii). total special characters
def count():
a=0
ua=0
la=0
d=0
sp=0
spl=0
with open("story.txt") as f:
while True:
c=f.read(1)
if not c:
break
print(c,end='')
if((c>='A' and c<='Z') or (c>='a' and c<='z')):
a=a+1
if((c>='A' and c<='Z') or (c>='a' and c<='z')):
a=a+1
if(c>='A' and c<='Z'):
ua=ua+1
else:
la=la+1
elif(c>='0' and c<='9'):
d=d+1
elif(c==' '):
sp=sp+1
else:
spl=spl+1
print("total alphabets ",a)
print("total upper case alphabets ",ua)
print("total lower case alphabets ",la)
print("total digits ",d)
print("total spaces ",sp)
print("total special characters ",spl)
# function calling
count()
Write a python program to read a file named “story.txt”, count
and print total words starting with “a” or “A” in the file?
def count_words():
w=0
with open("story.txt") as f:
for line in f:
for word in line.split():
if(word[0]=="a" or word[0]=="A"):
print(word)
w=w+1
print("total words starting with 'a' are ",w)
# function calling
count_words()

Write a python program to read a file named “story.txt”, count


and print total lines starting with vowels in the file?
filepath = 'story.txt'
vowels="AEIOUaeiou"
with open(filepath) as fp:
line = fp.readline()
cnt = 1
while line:
if(line[0] in vowels):
#print(line)
print("Line {}: {}".format(cnt, line.strip()))
cnt=cnt+1
line = fp.readline()

You might also like