0% found this document useful (0 votes)
54 views20 pages

Cs Rishav

The document contains code snippets for various Python programs including: 1) Displaying the Fibonacci series up to a given number. 2) Simulating a dice roll and allowing the user to roll multiple times. 3) A menu driven arithmetic calculator performing operations like addition, subtraction etc. 4) Other programs for reading and processing text files, implementing stack operations, searching employee records in a database, and validating phone numbers.

Uploaded by

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

Cs Rishav

The document contains code snippets for various Python programs including: 1) Displaying the Fibonacci series up to a given number. 2) Simulating a dice roll and allowing the user to roll multiple times. 3) A menu driven arithmetic calculator performing operations like addition, subtraction etc. 4) Other programs for reading and processing text files, implementing stack operations, searching employee records in a database, and validating phone numbers.

Uploaded by

Rishav Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

'''WAP to display Fibonacci series upto "n" numbers'''

first=0
second=1
num=int(input("how many Fibonacci numbers you want to display : "))
if num<=0:
print("Please Enter positive Integer ")
else:
print(first,end=" ")
print(second,end=" ")
for i in range(2,num):
third=first+second
first=second
second=third
print(third, end=" ")

how many Fibonacci numbers you want to display : 10


0 1 1 2 3 5 8 13 21 34
'''WAP to generate random number b/w 1 to 6 to simulate the Dice'''
import random
while True:
choice=input("\n Do you want to roll the Dice (Y/N) : ")
no=random.randint(1,6)
if choice=="y" or choice=="Y":
print('='*25)
print("\n Your NUMBER is : ",no,"\n")
print('='*25)
else:
break

Do you want to roll the Dice (Y/N) : y


=========================
Your NUMBER is : 6
=========================
Do you want to roll the Dice (Y/N) : n
'''Write a Menu driven program to perform Arithmetic operation
based on user's choice'''
print('''For Addition press "+"\n''')
print('''For Subtraction press "-"\n''')
print('''For Multiplication press "*"\n''')
print('''For Division press "/"\n''')
a=int(input("Enter first number: "))
b=int(input("Enter second number: "))
operation=input("\nEnter your Choice (means you input any one
from +,-,*,/): ")
if operation =='+':
print("Addition =" ,a+b)
elif operation == '-':
print("Subtraction =",a-b)
elif operation == '*':
print("Multiplication =",a*b)
elif operation == '/':
print("Division =",a/b)
else:
print("Please enter valid Operator")
== RESTART: C:/Users/RAA LAB/Desktop/practical file/practical 4.py ==

For Addition press "+"


For Subtraction press "-"
For Multiplication press "*"
For Division press "/"
Enter first number: 6
Enter second number: 5
Enter your Choice (means you input any one from +,-,*,/): *
Multiplication = 30

== RESTART: C:/Users/RAA LAB/Desktop/practical file/practical 4.py ==

For Addition press "+"


For Subtraction press "-"
For Multiplication press "*"
For Division press "/"
Enter first number: 6
Enter second number: 7
Enter your Choice (means you input any one from +,-,*,/): +
Addition = 13
'''wap a python program to read a text file line by line and display
each word separated by “#”'''
f=open("story.txt",'r')
contents=f.readlines()
for line in contents:
words= line.split()
for w in words:
print(i + '#', end= ' ')
print("")
f.close()

Python# is# fun.#


Python# is# high-level# programming# language.#
It# has# 35# keywords.#
'''wap a python program to read text file and display the number of
vowels/consonants/lower case/
upper case characters'''
f=open(r"C:\Users\RAA LAB\Desktop\story.txt",'r')
contents=f.read()
vowels=consonants=lowercase=uppercase=0
for ch in contents:
if ch in 'aeiouAEIOU':
vowels=vowels+1
if ch in 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVXYZ':
consonants=consonants+1
if ch.islower():
lowercase=lowercase+1
if ch.isupper():
uppercase=uppercase+1

f.close()
print("The total numbers of vowels in the file : ",vowels)
print("The total numbers of consonants in the file : ",consonants)
print("The total numbers of lowercase in the file : ",lowercase)
print("The total numbers of uppercase in the file : ",uppercase)
The total numbers of vowels in the file : 19
The total numbers of consonants in the file : 41
The total numbers of lowercase in the file : 57
The total numbers of uppercase in the file : 3
'''WAP to read lines from a text file "sample.txt" and copy those lines
into another file which are starting
with an alphabet "a" or "A"'''
f1=open(r"C:\Users\RAA LAB\Desktop\story.txt","r")
f2=open(r"C:\Users\RAA LAB\Desktop\new.txt","w")
while True:
line=f1.readline()
if line == '':
break
if line[0]=='a' or line[0]=='A':
f2.write(line)
print("All lines which are starting with character 'a' or 'A' has been
copied\
successfully into new.txt")
f1.close()
f2.close()
All lines which are starting with character 'a' or 'A' has been copied
successfully into new.txt
'''Write a MENU driven program to find Factorial and sum of list of
numbers using list'''
def factorial(no):
f=1
if no<0:
print("Sorry, we cannot take factorial for a Negative Number ")
elif no==0:
print("The Factorial of 0 is 1")
else:
for i in range (1,no+1):
f=f*i
print("The Factorial os ",no,'is :',f)
def Sum_list(L):
sum=0
for i in range(n):
sum=sum+L[i]
print("The sum of List : ",sum)
#MAIN PROGRAM

print("1. To find Factorial")


print("2. To find Sum of List Elements")
opt=int(input("Enter your Choice : "))
if opt==1:
n=int(input("Enter a number to find Factorial : "))
factorial(n)
elif opt==2:
L=[]
n=int(input("Enter how many elements you want to store in List? :
"))
for i in range(n):
ele=int(input("Enter n elements : "))
L.append(ele)
Sum_list(L)

1. To find Factorial
2. To find Sum of List Elements
Enter your Choice : 1
Enter a number to find Factorial : 6
The Factorial os 6 is : 720
1. To find Factorial
2. To find Sum of List Elements
Enter your Choice : 2
Enter how many elements you want to store in List? : 3
Enter n elements : 21
Enter n elements : 24
Enter n elements : 1
The sum of List : 46
‘’’Write a program to implement STACK operation’’’
def PUSH():
ele=int(input("Enter the element you want to push: "))
stack.append(ele)
def POP():
if stack==[]:
print("Stack is empty/underflow ")
else:
print("The deleted element is :",stack.pop())
def PEEK():
top=len(stack)-1
print("the top most element of stack is :stack[top]")
def DISPLAY():
top=len(stack)-1
print("The stack elements are :")
for i in range(top,-1,-1):
print(stack[i])
#main program
stack=[]
option='y'
while option=='y' or option == 'Y':
print("Stack operations ")
print("*********************")
print("1. PUSH")
print("2. POP")
print("3. PEEK")
print("4. DISPLAY")
print("*********************")
option=int(input("Enter Option or choice : "))
if option==1:
PUSH()
elif option==2:
POP()
elif option==3:
PEEK
elif option==4:
DISPLAY()
else:
print("Enter valid choice/option")
option=input("Do you want to perform another stack operation
(y/n) : ")

Stack operations
*********************
1. PUSH
2. POP
3. PEEK
4. DISPLAY
*********************
Enter Option or choice : 1
Enter the element you want to push: 56
Do you want to perform another stack operation (y/n) : y
Stack operations
*********************
1. PUSH
2. POP
3. PEEK
4. DISPLAY
*********************

Enter Option or choice : 4


The stack elements are :
45
56
Do you want to perform another stack operation (y/n) : n
'''To write a python program to integrate with Python to search an
Employee
using EMPID and display the record if present in already existing
table EMP,
if not display the appropriate message'''
import mysql.connector
con=mysql.connector.connet(host='localhost',username='root',pass
word='root', database='employees')
if con.is_connected():
cur=con.cursor()
print("*************************************")
print("Welcome to Employee Search Screen")
print("*************************************")
no=int(input("Enter the employee number to search : "))
Query="SELECT * FROM EMP WHERE EMPID={}".format(no)
cur.execute(Query)
data=cur.fetchone()
if data!=None:
print(data)
else:
print("Record not found!!!")
con.close()
'''WAP that prompts for a phoen number of 10 digits and two dashes, with
dashes
after the area code and the next three nubmers. for example, 017-555-1211 is
a
legal input. Display if the phone number entered is valid format or not and
display if the phone number is valid or not (i.e. contains just the digits and
dash at specific places.)'''
phNo=input("Enter the phone number: ")
length=len(phNo)
if length == 12 \
and phNo[3] == '-'\
and phNo[7] == '-'\
and phNo[:3].isdigit()\
and phNo[4:7].isdigit()\
and phNo[8:].isdigit():
print("Valid Phone Number")
else:
print("Invalid Phone Number")

Enter the phone number: 014-554-6587


Valid Phone Number
'''WAP to find frequencies of all elements of a list. Also, print the list
of unique elements in the list and
duplicate elements in the given list'''
lst=eval(input("Enter list : "))
length=len(lst)
unique_list=[]
duplicate_list=[]
count=i=0
while i<length:
element=lst[i]
count=1
if element not in unique_list and element not in duplicate_list:
i+=1
for j in range(i,length):
if element==lst[j]:
count+=1
else:
print("Element",element, "frequency : ",count)
if count==1:
unique_list.append(element)
else:
duplicate_list.append(element)
else:
i+=1
print("Original list",lst)
print("Unique elements list",unique_list)
print("Duplicates elements list",duplicate_list)

Enter list : [1,2,3,1,4,2,5,6]


Element 1 frequency : 2
Element 2 frequency : 2
Element 3 frequency : 1
Element 4 frequency : 1
Element 5 frequency : 1
Element 6 frequency : 1
Original list [1, 2, 3, 1, 4, 2, 5, 6]
Unique elements list [3, 4, 5, 6]
Duplicates elements list [1, 2]
'''WAP to read email IDs of n number of students and store them in a
tuple. Create two new tuples, one
to store only the usernames from the email IDs and second to store
domain names from the email IDs.
Print all three tuple at the end of the program.'''
lst=[]
n=int(input("How many students: "))
for i in range(1,n+1):
email=input("Enter email id of students"+str(i)+" : ")
lst.append(email)
etuple=tuple(lst)
lst1=[]
lst2=[]
for i in range(n):
email=etuple[i].split('@')
lst1.append(email[0])
lst2.append(email[1])
unameTuple=tuple(lst1)
dnameTuple=tuple(lst2)
print("students email ids : ")
print(etuple)
print("User name tuple: ")
print(unameTuple)
print("Domain name tuple: ")
print(dnameTuple)

How many students: 3


Enter email id of students1 : [email protected]
Enter email id of students2 : [email protected]
Enter email id of students3 : [email protected]
students email ids :
('[email protected]', '[email protected]',
'[email protected]')
User name tuple:
('tushar40', 'anjali44', 'amritkumar')
Domain name tuple:
('gmail.com', 'yahoo.com', 'gmail.com')

You might also like