CBSE Today - Computer Science Tutorials, Projects Tips and ...

 # Program : 1# Program to implement all user defined functions of a listch='y'L = [1,2,3,4,2,3,2,4,5,6,71,1,10,12]while ch == 'y' or ch=='Y': print("1. append in list") print("2. Insert in a list") print("3. Find length of list") print("4. To count occurence of an element") print("5. Extend a list") print("6. Find the sum of list elements") print("7. Find the index of given element") print("8. Find the minimum element in a list") print("9. Find the maximum element in a list") print("10.To pop element") print("11.To remove element") print("12.To use del ") print("13.To reverse a list") print("14.To sort a list") x = int(input("Enter choice = ")) if x == 1: print("Original list ", L) val = int(input("Enter a value to be appended -> ")) L.append(val) print("List after append ", L) if x == 2: val = int(input("Enter a value to be inserted -> ")) loc = int(input("Enter a location to insert -> ")) L.insert(loc,val) print("Original list ", L) print("List after insert ",L) if x == 3: n = len(L) print("Length of list ",n) if x == 4: val = int(input("Enter a value whose occurence want to count -> ")) n = L.count(val) print(val," occur ",n," times in list", L) if x == 5: M = [100,200,300,4000] L.extend(M) print("List after extend ",L) if x == 6: n = sum(L) print("Sum of list elements ",n) if x == 7: print(L) print("1. To give index when no stariing and ending value given") print("2. To give index after given staring index value") print("3. To give index after given staring and ending index value") y = int(input("Enter choice -> ")) if y == 1: val = int(input("Enter a value whose index you want -> ")) n = L.index(val) print("index of ", val," is ", n) if y == 2: val = int(input("Enter a value whose index you want -> ")) loc = int(input("Enter a staring location -> ")) n = L.index(val,loc) print("index of ", val," is ", n) if y == 3: val = int(input("Enter a value whose index you want -> ")) loc = int(input("Enter a staring location -> ")) end = int(input("Enter a ending location -> ")) n = L.index(val,loc,end) print("index of ", val," is ", n) if x == 8: n = min(L) print("Minimum element of list ",n) if x == 9: n = max(L) print(" maximum element of list ",n) if x == 10: print("1. To pop last element ") print("2. To pop a particular index") y = int(input("Enter the choice")) if y == 1: print("List before pop ",L) n = L.pop() print("List after pop ",L) if y == 2: loc = int(input("Enter a location to pop -> ")) print("List before pop ",L) n = L.pop(loc) print("List after pop ",L) if x == 11: print("List before remove ",L) val = int(input("Enter a value want to remove -> ")) L.remove(val) print("List after remove ",L) if x == 12: print("1. To delete complete list") print("2. To delete a particular element from list") y = int(input("Enter the choice")) if y == 1: print("List before del ",L) del L print("List after del ",L) if y == 2: print("List before pop ",L) loc = int(input("Enter a location of element to delete -> ")) del L[loc] print("List after del ",L) if x == 13: print("List before reverse",L) L.reverse() print("List after reverse ",L) if x == 14: print("List before sort",L) L.sort() print("List in ascending order ",L) L.sort(reverse=True) print("List in descending order ",L) ch = input("\nDo you want to continue Y/N = ") # Program : 2# Program to show various logical operation on Listdef search(): L = [] M = [] N= [ ] n = int(input("enter how many elements ")) for i in range(n): x = int(input("enter a number")) L.append(x) f=0 x = int(input("enter element to be searched ")) for i in range(n): if L[i]== x: f=1 if f==0: print("Element does not exits") else: print("Element exists")def highest(): L = [] n = int(input("Enter N")) for i in range(n): x = int(input("Enter value")) L.append(x) max = L[0] for i in L: if i>max: max = i print("Greatest is ",max)def Exchange(): A = [2, 4, 1, 6, 7, 9, 23, 10,55,77] N = len(A) print ("The initial array is:", A) if N%2==0: for i in range(int(N/2)): # int(N/2): converting float to int Tmp = A[i] A[i] = A[(int(N/2)) + i] A[int(N/2) + i] = Tmp print ("The exchanged array is", A) else: print("N should be Even No")def selection(): L = [5,1,5,11,4,9,6,13,2] n = len(L) for i in range(n): min1 = L[i] pos = i for j in range (i+1,n): if L[j] < min1: min1 = L[j] pos = j t=L[i] L[i]=L[pos] L[pos]=t print(L)ch='y'while ch == 'y' or ch=='Y': print("1. Linear search = ") print("2. find highest element in a list") print("3. Exchange first half with second half of a list") print("4. Sort List using selection sort") x = int(input("Enter choice = ")) if x == 1: search() if x == 2: highest() if x == 3: Exchange() if x == 4: selection() ch = input("\nDo you want to continue Y/N = ")# Program : 3 # Program to show various logical operation on nested Listdef col_sum(): L = [] M=int(input("How many rows in a list")) N = int(input("How many columns in a list")) for i in range(M): Row = [] for j in range(N): x = int(input("Enter value for row ")) Row.append(x) L.append(Row) for i in range(M): for j in range(N): print(L[i][j],end=" ") print() s=0 s1=0 for i in range(N): s=0 for j in range(M): s = s + L[j][i] print("Sum of " ,i," Column is ",s)def diagnol_sum(): L = [] M=int(input("How many rows in a list")) N = int(input("How many columns in a list")) for i in range(M): Row = [] for j in range(N): x = int(input("Enter value for row ")) Row.append(x) L.append(Row) for i in range(M): for j in range(N): print(L[i][j],end=" ") print() s=0 s1=0 for i in range(M): for j in range(N): if i==j: s =s + L[i][j] if i== (N -j- 1): s1 = s1 + L[i][j] print("Sum of first diagonal " ,s) print("Sum of second diagonal " ,s1)def Transpose(): A = [[2,3,1,5,0],[7,1,5,3,1],[2,5,7,8,1],[0,1,5,0,1],[3,4,9,1,5]] B = [[ 0 for x in range(5)]for x in range(5)] print(B) print ("The original array is") for i in range(5): for j in range(5): print ((A[i][j]),end=" ") print() for i in range(len(A)): for j in range(len(A)): B[j][i] = A[i][j] print ("The Transpose is : ") for i in range(len(A)): for j in range(len(A)): print ((B[i][j]), end=" ") print()ch='y'while ch == 'y' or ch=='Y': print("1. To find the sum of column element ") print("2. To find the sum of diagnal elements") print("3. To find transpose of a matrix") x = int(input("Enter choice = ")) if x == 1: col_sum() if x == 2: diagnol_sum() if x == 3: Transpose() ch = input("\nDo you want to continue Y/N = ") # Program : 4# Program to implement all user defined functions of Dictionarych='y'Stud = {'Name':'Arun','Class':12}while ch == 'y' or ch=='Y': print("1. creating a dictionary") print("2. Updating dictionary") print("3. Printing Dictinary keys") print("4. Printing Dictonary values") print("5. Printing Dictionary items") print("6. Printing the value of given key") print("7. Find Dictionary length") print("8. Printing key and value using loop") print("9. Printing key and value using loop and items() function") print("10.Check in and not in membership operator") print("11.Update dictionary") print("12.Use del function") print("13.Use pop function") print("14.Use popitem() function") print("15.Use clear function") print("16.copy dictionary") print("17.Sort keys") print("18.Use fromkeys function") x = int(input("Enter choice = ")) if x == 1: ch1 = 'y' while ch1=='y': key = input("Enter a key -> ") val = input("Enter a value of key -> ") Stud[key] = val print(Stud) ch1 = input("Do You want to continue Y/N ") if x == 2: ch1 = 'y' while ch1=='y': key = input("Enter a key -> ") val = input("Enter a value of key -> ") Stud.update({key:val}) print(Stud) ch1 = input("Do You want to continue Y/N ") if x == 3: print("Dictinary keys ",Stud.keys()) if x == 4: print("Dictinary keys ",Stud.values()) if x == 5: print("Dictinary keys ",Stud.items()) if x == 6: key = input("Enter key whose value required ") print(Stud[key]) print(Stud.get(key)) if x == 7: print("Lenth of Dictionary ",len(Stud)) if x == 8: for key in Stud: print(key, " value is ", Stud[key]) if x == 9: for key, value in Stud.items(): print({key},":",{value}) for key in Stud.key(): print(key) for value in Stud.values(): print(value) if x == 10: print('Name' in Stud) print('Address' not in Stud) print('Fees' in Stud) if x == 11: val = input("Enter a new name -> ") Stud['Name'] = val print(Stud) Stud.update({'Name':'Rahul'}) print(Stud) if x == 12: print("1. To delete a particular element from list") print("2. To delete complete list") y = int(input("Enter the choice")) if y == 1: key = input("Enter a key to be deleted -> ") del Stud[key] print(Stud) if y == 2: del Stud print(Stud) #this will cause an error because "Stud" no longer exists. if x == 13: key = input("Enter a key to be popped -> ") Stud.pop(key) print(Stud) if x == 14: Stud.popitem() print(Stud) if x == 15: Stud.clear() print(Stud) if x == 16: print("Stud ", Stud) Stud1 = Stud print("Stud1 = ",Stud1) Stud1['Name'] = 'Rohan' print("Stud after change in Stud1 ",Stud) Stud2 = Stud.copy() Stud2['Name'] = 'Romi' print("stud after change is Stud2 ",Stud) Stud3 = dict(Stud) Stud3['Name'] = 'Vimal' print("Stud after change in Stud 3 ",Stud) if x == 17: print(sorted(Stud)) if x == 18: x = ('key1', 'key2', 'key3') y = 0,1,2 thisdict = dict.fromkeys(x, y) #value if optional default value is none print(thisdict) ch = input("\nDo you want to continue Y/N = ") # Program 5# Program to perform logical operation on stringc=0ch='y'v=c=d=s=0while ch == 'y' or ch=='Y': print("1. Enter string ") print("2. Count no of spaces") print("3. Count vowel,consonants, digit and special characters") print("4. Count and display no of words starting with A or a ") print("5. Longest word in a string ") print("6. Display the word in which maximum time alpahbet e appear") x = int(input("Enter choice = ")) if x == 1: St = input("Enter a string -> ") print("Entered string is -> ", St) if x == 2: for i in St: if i == ' ': c=c+1 print("No of spaces = ",c) if x == 3: for i in range(len(St)): if St[i] in "aeiouAEIOU": v = v + 1 elif St[i] in "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstuvwxyz": c = c + 1 elif St[i] in "0123456789": d = d + 1 else: s = s + 1 print("No of vowels ",v) print("No of consonants ",c) print("No of digits ",d) print("No of special character",s) if x == 4: v=0 L = St.split() print(L) for i in range(len(L)): if L[i][0] == 'A' or L[i][0] == 'a': print(L[i]) v = v + 1 print("No of words starting with A or a ",v) if x == 5: v = 0 L = St.split() print(L) S = " " for i in range(len(L)): if len(L[i])>v: v = len(L[i]) S = L[i] print("longest word ", S, " Length is ", v) if x == 6: v=0 L= St.split() print(L) S= " " for i in range(len(L)): m = L[i].count('e') if m > v: v = m S = L[i] print("Word ", S, " is containing e ", v, "times") ch = input("\nDo you want to continue Y/N = ") #Program 6 c=0ch='y'v=c=d=s=0while ch == 'y' or ch=='Y': print("1. Enter string ") print("2. check it is plaindrome or not") print("3. create a string which is reverse of original string") print("4. Print each word in reverse order ") x = int(input("Enter choice = ")) if x == 1: St = input("Enter a string -> ") print("Entered string is -> ", St) if x == 2: L = len(St) j = L-1 f=0 for i in range(L): if St[i] != St[j]: f=1 j=j-1 if f==1: print("String is not palindrome") else: print("String is palindrome") if x == 3: L = len(St) - 1 St1 = "" for i in range(L,-1,-1): St1 = St1 + St[i] print("Reversed string is ", St1) if x == 4: L = St.split() x = len(L) for i in L: for j in range(len(i)-1, -1, -1): print(i[j],end="") ch = input("\nDo you want to continue Y/N = ") # Program 7# Program to implement inbuilt functions on stringc=0ch='y'v=c=d=s=0while ch == 'y' or ch=='Y': print("1. Find length of string ") print("2. To check whether string conatin only alphanumeric charater or not") print("3. To check string contain only alphabets") print("4. to check String contain only numeric values") print("5. to check String contain only space values") print("6. to check String contain only upper case values") print("7. to check String contain only lower case values") print("8. to check first letter of a string is capital or not") x = int(input("Enter choice = ")) if x == 1: St = input("Enter a string -> ") print("Entered string is -> ", St) print("Length = ",len(St)) if x == 2: St = input("Enter a string -> ") if St.isalnum(): print("Only alphabets and numeric values in string") else: print("Special char also present") if x == 3: St = input("Enter a string -> ") if St.isalpha(): print("Only alphabets in string") else: print("Special char and numeric values are also present") if x == 4: St = input("Enter a string -> ") if St.isdigit(): print("Only digits in string") else: print("Special char and alphabet are also present") if x == 5: St = input("Enter a string -> ") if St.isspace(): print("Only space in string") else: print("other chars also present") if x == 6: St = input("Enter a string -> ") if St.isupper(): print("Only upper case characters in string") else: print("lower case also present") if x == 7: St = input("Enter a string -> ") if St.islower(): print("Only lower case characters") else: print("Upper case also present") if x == 8: St = input("Enter a string -> ") if St.istitle(): print("First letter of all words is in upper case") else: print("Not in upper case") ch = input("\nDo you want to continue Y/N = ") #Program 8# Program to implement inbuilt functions on stringc=0ch='y'v=c=d=s=0while ch == 'y' or ch=='Y': print("1. Convert string in upper case ") print("2. Convert string in lower case") print("3. Capitalize the first letter of string") print("4. Count the occurence of substring in a string") print("5. Find the first occurence of substring in a string") print("6. Replace a substring with another string") print("7. Swap the cases") print("8. To partition a string ") print("9. To join list into string ") print("10. To split a string") print("11. To apply lstrip") print("12. To apply rstrip") print("13. To apply strip") x = int(input("Enter choice = ")) if x == 1: St = input("Enter a string -> ") print("Entered string is -> ", St) St1 = St.upper() print("Upper case = ",St.upper()) print("Upper Case = ",St1) print("Entered string after applying upper case -> ", St) if x == 2: St = input("Enter a string -> ") print("Entered string is -> ", St) St1 = St.lower() print("lower case = ",St.lower()) print("lower Case = ",St1) if x == 3: St = input("Enter a string -> ") print("Entered string is -> ", St) St1 = St.capitalize() print("After changing first letter in upper case = ",St.capitalize()) print("After changing first letter in upper case = ",St1) if x == 4: St = input("Enter a string -> ") sub = input("Enter substring -> ") x = St.count(sub,10,20) print(sub," is appearing ",x," number of times in ") if x == 5: St = input("Enter a string -> ") sub = input("Enter substring -> ") x = St.find(sub,10,22) print(sub," is appearing at ",x," index no ") if x == 6: St = input("Enter a string -> ") old = input("Enter a old substring -> ") new = input("Enter a new substring -> ") print(" string after replacement - > ", St.replace(old,new,3)) if x == 7: St = input("Enter a string -> ") print("Changes string - > ",St.swapcase()) print("string after swapcase- > ",St) if x == 8: St = input("Enter a string -> ") sep = input("Enter a seprator -> ") print(" String after partition ", St.partition(sep)) if x == 9: L = ("I ","like "," ice", " cream") St= "*" print(St.join(L)) if x == 10: St = input("Enter a string -> ") sep = input("Enter a seprator -> ") print(" String after split without seprator ", St.split()) print(" String after split with separator ", St.split(sep)) print(" String after split with separator and max limit ", St.split(sep,2)) if x == 11: St = input("Enter a string -> ") sep = input("Enter a character -> ") print(" String before lstrip ", St) print(" String after lstrip ", St.lstrip()) print(" String after partition ", St.lstrip(sep)) if x == 12: St = input("Enter a string -> ") sep = input("Enter a character -> ") print(" String after partition ", St.rstrip()) print(" String after rstrip ", St.rstrip(sep)) if x == 13: St = input("Enter a string -> ") sep = input("Enter a character -> ") print(" String after partition ", St.strip()) print(" String after strip ", St.strip(sep)) ch = input("\nDo you want to continue Y/N = ") # Program 9 # Program to create and read text filech = 'y'while ch=='y' or ch == 'Y': print("1. To create file ") print("2. To read file ") print("3. To create a file by replacing space with # character") print("4. To display newly created file") x = int(input("enter choice")) if x == 1: file1 = open("MyFile.txt","w") L = ["I liKe #$% python 1 4 progreeamming\n"," lIke @# 6 *# $! SQeeL\n","and likeeeE &* Networking"] file1.writelines(L) file1.close() if x == 2: file1 = open("MyFile.txt","r") St = file1.read() print(St) file1.close() if x == 3 : file1 = open("MyFile.txt","r") file2 = open("temp.txt","w") St = file1.read() for i in St: if i ==' ': file2.write('#') else: file2.write(i) file1.close() file2.close() if x == 4: file1 = open("temp.txt","r") St = file1.read() print(St) file1.close() ch = input("Do You want to continue Y/N ") # Program 10# Program to implement all operation on binary fileimport pickleimport osch = 'y'while ch == 'y' or ch == 'Y': print("1. To create a file of students ") print("2. To read a Binary File ") print("3. To Print report stream wise ") print("4. To delete a record from Binary File ") print("5. To Modify a record in a Binary File ") print("6. To create a file of teachers") print("7. Enter the roll and display the student name along with teacher name and department") x = int(input("Enter your choice = ")) if x == 1: f = open("Myfile.dat","ab") n = int(input("How many records ")) L=[] for i in range(n): roll = int(input("Enter Roll = ")) name = input("Enter name = ") address = input("Enter address = ") Fees = int(input("Enter the fees = ")) Stream = input("Enter the stream Sci/comm/Arts ") T_No = int(input("Enter the teacher no who is teaching him")) L = [roll,name,address,Fees,Stream,T_No] pickle.dump(L,f) f.close() if x == 2: f = open("Myfile.dat","rb") while True: try: L = pickle.load(f) print(" Roll Number ", L[0]) print(" Name ", L[1]) print(" Address ", L[2]) print(" Fees ", L[3]) print(" Stream ", L[4]) print(" Teacher No ", L[5]) except EOFError: print("File end") break f.close() if x == 3: f = open("Myfile.dat","rb") Stream1 = input("Input Stream whose report required ") print("__________________________________________________________________") L = [] S = 0 print(" Roll Name Fees") while True: try: L = pickle.load(f) if Stream1 == L[4]: print(L[0]," ",L[1]," ",L[3]) S = S + L[3] except EOFError: print("________________________________________________________________________") break f.close() print(" Sum of fees of ",Stream1," is = ",S) if x == 4: f = open("Myfile.dat","rb") f1 = open("temp.dat","wb") x = int(input("enter Roll no to be deleted ")) while True: try: L = pickle.load(f) if L[0]!=x: pickle.dump(L,f1) except EOFError: print("File end") break f.close() f1.close() os.remove("Myfile.dat") os.rename("temp.dat","Myfile.dat") if x == 5: f = open("Myfile.dat","rb") f1 = open("temp.dat","wb") x = int(input("Enter Roll no whose fees need to be increase ")) while True: try: L = pickle.load(f) if L[0]==x: L[3] = L[3]+500 pickle.dump(L,f1) else: pickle.dump(L,f1) except EOFError: print("File end") break f.close() f1.close() os.remove("Myfile.dat") os.rename("temp.dat","Myfile.dat") f.close() if x == 6: f = open("teacher.dat","wb") n = int(input("How many records ")) L=[] for i in range(n): Tno = int(input("Enter Teacher No = ")) Tname = input("Enter Teacher name = ") Dept = input("Enter depatrment= ") Salary = int(input("Enter the Salary = ")) L = [Tno,Tname,Dept,Salary] pickle.dump(L,f) f.close() if x == 7: f = open("Myfile.dat","rb") f1 = open("teacher.dat","rb") n = int(input("enter roll no whose record is required ")) L=[] M = [] Temp = [] Flag = 0 Flag1 = 0 while True: try: L = pickle.load(f) if n == L[0]: Temp = L Flag = 1 except EOFError: print("") break if Flag == 0: print("Student does not exists") while True: try: M = pickle.load(f1) if M[0] == Temp[5]: print (" Roll No ",Temp[0]) print (" Student Name ",Temp[1]) print (" Teacher Name ",M[1]) print (" Department ",M[2]) Flag1=1 except EOFError: print() break if Flag1==0: print("Teacher Record does not exists") f.close() f1.close() ch = input("Do You want to continue Y/N ")# Program 11# Program ti create and read csv file and show sql and csv file connectivityimport pymysqlimport csv# Program to work on CSV filech='y'while ch == 'y' or ch=='Y': print("1. Create CSV file with writerow") print("2. Create CSV file with writerows") print("3. To read CSV file") print("4. To copy record from CSV to SQL") print("5. To copy record from SQL to CSV") x = int(input("Enter choice = ")) if x == 1: Heading = ['Admission NO','Name','Fees','Address','Tno'] f = open("Myfile.csv",'w',newline = '') n = int(input("How many Records")) csv_f = csv.writer(f, delimiter = ',',quotechar = "'", quoting=csv.QUOTE_NONNUMERIC) csv_f.writerow(Heading) for i in range(n): Roll = int(input("Enter Admission No")) name = input("Enter Name") Fees = int(input("Enter Fees")) Add = input("Enter Address") Tno = int(input("Enter Teacher No")) row = [Roll,name,Fees,Add,Tno] csv_f.writerow(row) f.close() if x == 2: Heading = ['Admission No' , 'Name','Fees','Addrees','Tno'] f = open("Myfile.csv",'w', newline='') n = int(input("How many Records")) csv_f = csv.writer(f, delimiter = ',') csv_f.writerow(Heading) Records = [] for i in range(n): Roll = int(input("Enter Admission No")) name = input("Enter Name") Fees = int(input("Enter Fees")) Add = input("Enter Address") Tno = int(input("Enter Teacher No")) row = [Roll,name,Fees,Add,Tno] Records.append(row) print(Records) csv_f.writerows(Records) f.close() if x == 3: f = open("Myfile.csv",'r') r = csv.reader(f) for i in r: print(i) print(type(i)) f.close() if x == 4: f = open("Myfile.csv",'r') #establishing the connection conn = pymysql.connect(user='root', password='reeta', host='localhost', database='school') #Creating a cursor object using the cursor() method cursor = conn.cursor() r = csv.reader(f) next(r) for i in r: #Preparing query sql = "INSERT INTO student VALUES (%s, %s, %s,%s, %s)" cursor.execute(sql, i) mit() conn.close() f.close() if x == 5: f = open("Myfile.csv",'a') #establishing the connection conn = pymysql.connect(user='root', password='reeta', host='localhost', database='school') #Creating a cursor object using the cursor() method cursor = conn.cursor() #Preparing query cursor.execute("SELECT * FROM student") # Fetching all the records from cursor result = cursor.fetchall() csv_f = csv.writer(f, delimiter = ',') for row in result: csv_f.writerow(row) conn.close() f.close() ch = input("\nDo you want to continue Y/N = ")# Program 12# Program to do add, modify, delete and search in sql using pythonimport pymysqlch = 'y'while ch == 'y' or ch == 'Y': print("1. To create a database school ") print("2. To create a table student in database school ") print("3. To insert Record in a table") print("4. To Print Records from a table") print("5. TO Search a Roll in a table") print("6. To Delete Record in a table") print("7. To Modify Record in a table") x = int(input("Enter your choice = ")) if x == 1: #establishing the connection conn = pymysql.connect(user='root', password='reeta', host='localhost') #Creating a cursor object using the cursor() method cursor = conn.cursor() #Droping database if already exists. cursor.execute("DROP database IF EXISTS school") #Preparing query to create a database sql = "CREATE database school"; #Creating a database cursor.execute(sql) #Retrieving the list of databases print("List of databases: ") cursor.execute("SHOW DATABASES") print(cursor.fetchall()) #Closing the connection conn.close() if x == 2: #establishing the connection conn = pymysql.connect(user='root', password='reeta', host='localhost', database='school') #Creating a cursor object using the cursor() method cursor = conn.cursor() #Doping database if already exists. cursor.execute("DROP table IF EXISTS student") #Preparing query to create a table sql = "CREATE table student(Admno integer, Name char(20), fees float, Address char(20), Tno integer)"; #Creating a database cursor.execute(sql) #Retrieving the list of databases print("List of tables: ") cursor.execute("SHOW TABLES") print(cursor.fetchall()) #Closing the connection conn.close() if x == 3: #establishing the connection conn = pymysql.connect(user='root', password='reeta', host='localhost', database='school') #Creating a cursor object using the cursor() method cursor = conn.cursor() ch1 = 'y' while ch1 == 'y' or ch1== 'Y': Troll = int(input("Enter Admno No ")) TName = input("Enter Name ") Tfees = int(input("Enter Fees ")) Taddress = input("Enter address ") T_TNo = int(input("Enter Teacher No ")) val = [Troll, TName,Tfees,Taddress,T_TNo] #Preparing query sql = "INSERT INTO student VALUES (%s, %s, %s, %s,%s)" cursor.execute(sql, val) mit() ch1 = input ( " Do you want to add more records Y/N = ") conn.close() if x == 4: #establishing the connection conn = pymysql.connect(user='root', password='reeta', host='localhost', database='school') #Creating a cursor object using the cursor() method cursor = conn.cursor() #Preparing query cursor.execute("SELECT * FROM student") # Fetching all the records from cursor result = cursor.fetchall() print ( " Admno Name Fees Address Teacher No ") for x in range(len(result)): print(result[x][0]," ",result[x][1]," ",result[x][2]," ",result[x][3]," ",result[x][4]) if x == 5: #establishing the connection conn = pymysql.connect(user='root', password='reeta', host='localhost', database='school') #Creating a cursor object using the cursor() method cursor = conn.cursor() Troll = int(input("Enter admno no to be searched")) #Preparing query to create a database cursor.execute("SELECT * FROM student where Admno = %s",Troll) # Fetching all the records from cursor result = cursor.fetchall() if len(result)==0: print("Record does not exists") else: for x in result: print(x) if x == 6: #establishing the connection conn = pymysql.connect(user='root', password='reeta', host='localhost', database='school') #Creating a cursor object using the cursor() method cursor = conn.cursor() Troll = int(input("Enter Admission no you want to delete")) #Preparing query to create a database cursor.execute("DELETE FROM student where Admno = %s",Troll) mit() x=cursor.rowcount if x == 0: print("Record does not exists") else: print("Record Deleted") if x == 7: #establishing the connection conn = pymysql.connect(user='root', password='reeta', host='localhost', database='school') #Creating a cursor object using the cursor() method cursor = conn.cursor() Troll = int(input("Enter Admission no you want to modify")) TFees = int(input("Enter new Fees")) #Preparing query to create a database cursor.execute("UPDATE student SET fees = %s where Admno = %s",(TFees,Troll)) mit() x=cursor.rowcount if x == 0: print("Record does not exists") else: print("Record Modified") ch = input(" \n\n Do You want to continue Y/N = ") # Program 13# Program to perform operation on stackStack = [] # A default empty stack top = -1 # To know the current index position in stack.def Push(Stack, top): Ch = 'Y' while Ch == 'Y' or Ch == 'y'or Ch == 'Yes': val = input("Enter the value to be added in the stack: ") Stack.append(val) # Adding element into list. top += 1 # It checks the total number of addition print ("Do you want to add more...<y/n>: ", end="") Ch = input() if Ch == 'N' or Ch == 'n' or Ch == 'No' or Ch == 'NO': break return top# Removing stack elementsdef Pop(Stack, top): slen = len(Stack) # Finds total elements in the stack. if slen <= 0: # Checks if stack is empty or not. print ("Stack is empty") else: val = Stack.pop() # Removing from top of the stack. top = top - 1 print("Value deleted from stack is", val) return top# Showing stack elementsdef Show_element(Stack, top): slen = len(Stack) # Finds total elements in the stack. if slen <= 0: # Checks if stack is empty or not. print ("Stack is empty") else: print("The stack elements are...") i = top while (i >= 0):# Stack elements processed in reverse order. print(Stack[i]) i = i - 1while (True): print() print (' S T A C K O P E R A T I O N') print ('- - - - - - - - - - - - - - - -') print ('1. Adding elements to a stack') print ('2. Removing elements from a stack') print ('3. Showing elements of a stack') print ('4. Exit from stack operation') Opt = int(input( "Enter your option: ")) print() if (Opt == 1) : # Push operation of stack - Adding element at top of the stack top = Push(Stack, top) elif (Opt == 2) :# Pop operation of stack - Deleting element from top of the stack top = Pop(Stack, top) elif (Opt == 3) : Show_element(Stack, top) elif (Opt == 4) : break # Program 14# Program to perform operation on queueQueue = [] # A default queue using list() function.rear = front = - 1 # Initializing the queue position# Inserting element into a queue.def Insert_Queue(Queue, rear): Ch = 'Y' while Ch == 'Y' or Ch == 'y'or Ch == 'Yes': val = input("Enter the value to be added into the queue: ") rear += 1 Queue.append(val) # Adding element into queue list. print ("Do you want to add more...<y/n>: ", end="") Ch = input() if Ch == 'N' or Ch == 'n' or Ch == 'No' or Ch == 'NO': break return rear# Removing queue elementsdef Remove_Queue(Queue, rear):Qlen = len(Queue) # Finds total elements in the queue. if Qlen <= 0: # Checks if queue is empty or not.print ("Queue is empty")else:rear -= 1val = Queue.pop(0) # Removing from front of the queue.print("Value deleted from the queue is:", val)return rear# Showing queue elementsdef Show_Queue(Queue, rear): front = 0 Qlen = len(Queue) # Finds total elements in the queue. if Qlen <= 0: # Checks if queue is empty or not. print ("Queue is empty") else: print("The queue elements are...") while front <= rear: # Queue elements processed. print(Queue[front], end=" ") front += 1while (True): print() print ('Q U E U E O P E R A T I O N') print ('- - - - - - - - - - - - - - - -') print ('1. Adding elements to a queue') print ('2. Removing elements from a queue') print ('3. Showing elements of a queue') print ('4. Exit from queue operation') Opt = int(input( "Enter your option: ")) print() if (Opt == 1) : # Insert operation of Queue - Adding element at rear of the queue rear = Insert_Queue(Queue, rear) elif (Opt == 2) : # Delete operation of queue - Deleting element at front of the queue rear = Remove_Queue(Queue, rear) elif (Opt == 3) : # Traversing/Showing queue element Show_Queue(Queue, rear) elif (Opt == 4) : break#Program 15# Program to implemnet menu driven program to show and modify details of employeeEmpNo = []Name = []Salary = []DA = []HRA = []Gross = []IT = []Net = []DeptName = []n = int(input("Enter how many employee = "))for i in range(n): x = int(input("Enter emp no = ")) EmpNo.append(x) x = input("Enter name of employee = ") Name.append(x) x = int(input("Enter Salary = ")) Salary.append(x) x = input("Enter departmnet name of employee = ") DeptName.append(x) DA.append(30*Salary[i]/100) HRA.append(35*Salary[i]/100) Gross.append(DA[i]+HRA[i]+Salary[i]) IT.append(Gross[i]*10/100) Net.append(Gross[i]-IT[i])ch = 'Y'while ch=='Y' or ch =='y': print( "1. Print Tabular Report ") print( "2. Print Salary Slip of an Employee") print( "3. Print Tabular Report of given departmnet Name") print(" 4. Print the sum of salary and Highest paid employee of given department") m = int(input("Enter option = ")) if m==1: print("EmpNo\tName\tSalary\tDA\tHRA\tGross\tIT\tNet\t\tDepartment Name") print("___________________________________________________________________") for i in range(n): print(EmpNo[i],end='\t') print(Name[i],end='\t') print(Salary[i],end='\t') print(DA[i],end='\t') print(HRA[i],end='\t') print(Gross[i],end='\t') print(IT[i],end='\t') print(Net[i],end='\t') print(DeptName[i]) if m==2: f=0 x = int(input("Enter Employee No whose Salary slip is required ")) print("\t\tSalary") for i in range(n): if EmpNo[i] == x: f=1 print("Roll No : ",EmpNo[i]) print("Roll No : ",Name[i]) print("___________________________________________________________________") print("Perks\t\t\t\t\tValues") print("Basic\t\t\t\t\t",Salary[i]) print("Dearness Allowance\t\t\t",DA[i]) print("House Rent Allowance\t\t\t",HRA[i]) print("Gross Salary\t\t\t\t",Gross[i]) print("Income Tax\t\t\t\t",IT[i]) print("Net Salary\t\t\t\t",Net[i]) print("___________________________________________________________________") if f==0: print("Emp No does not exists") if m==3: x = input("Enter Department name") print("EmpNo\tName\tNet\t\tDepartment Name") print("___________________________________________________________________") for i in range(n): if DeptName[i] == x: print(EmpNo[i],end='\t') print(Name[i],end='\t') print(Net[i],end='\t\t') print(DeptName[i]) if m ==4: sum=0 max=Net[0] x = input("Enter Department name") for i in range(n): if DeptName[i]==x: sum = sum + Net[i] if Net[i]>max: max = Net[i] Tname=Name[i] print (" Sum of salary of ",x ," is ",sum) print (Tname," is getting the highest salary ",max) ch = input( "Do U want to continue Y/N = ")# Program 16# Program to print the tabular report and report card of a studentroll=[]name=[]m1=[]m2=[]m3=[]m4=[]m5=[]total=[]n=int(input("Enter number of students"))for i in range(n): x=int(input("Enter roll no of student")) roll.append(x) x=input("Enter name of student") name.append(x) x=int(input("Enter marks1 of student")) m1.append(x) x=int(input("Enter marks2 of student")) m2.append(x) x=int(input("Enter marks3 of student")) m3.append(x) x=int(input("Enter marks4 of student")) m4.append(x) x=int(input("Enter marks5 of student")) m5.append(x) total.append(m1[i]+m2[i]+m3[i]+m4[i]+m5[i])ch='Y'while ch=='Y' or ch=='y': print("1.PRINT TABULAR DATA") print("2.PRINT REPORT CARD") m=int(input("Enter which option u choose")) if m==1: print("ROLL NO\t\tNAME\tMARKS1\tMARKS2\tMARKS3\tMARKS4\tMARKS5\tTOTAL") print("________________________________________________________________") for i in range(n): print(roll[i],end='\t\t') print(name[i],end='\t') print(m1[i],end='\t') print(m2[i],end='\t') print(m3[i],end='\t') print(m4[i],end='\t') print(m5[i],end='\t') print(total[i]) if m==2: max1=m1[0] max2=m2[0] max3=m3[0] max4=m4[0] max5=m5[0] for i in range(n): if m1[i]>max1: max1=m1[i] if m2[i]>max2: max2=m2[i] if m3[i]>max3: max3=m3[i] if m4[i]>max4: max4=m4[i] if m5[i]>max5: max5=m5[i] f=0 x=int(input("Enter roll no of student whose Report card u want")) print("\t\t\tREPORT CARD") for i in range(n): if roll[i]== x: f=1 print("ROLL NO: ",roll[i]) print("NAME OF STUDENT: ",name[i]) print("______________________________________________________________________") print("SUBJECTS\tMARKS OBTAINED\tHIGHEST MARKS") print("PHSICS\t\t",m1[i],"\t\t\t",max1) print("MATHEMATICS\t",m2[i],"\t\t\t",max2) print("ENGLISH\t\t",m3[i],"\t\t\t",max3) print("CHEMISTRY\t",m4[i],"\t\t\t",max4) print("COMPUTER SC\t",m5[i],"\t\t\t",max5) print("_______________________________________________________________________") print("TOTAL MARKS",total[i]) if (f==0): print("roll no doesnt exist") ch=input("DO U WANT TO CONTINUE Y/N") # Program 17# Program to print various pyramid using nested loopsch = 'y'while ch=='y': print("1. Pyramid 1") print("2. Pyramid 1") print("3. Pyramid 1") print("4. Pyramid 1") x = int(input("enter choice")) if x==1: n = int(input("Enter N")) for a in range(n,0,-1): for b in range(a,0,-1): print(a,end="") print() if x == 2: n = int(input("Enter N = ")) for a in range(n,0,-1): for b in range(a,0,-1): print(b,end="") print() if x==3: n = int(input("enter end value")) for a in range(1,n+1): for b in range(1,a+1): print(b, end=" ") print() if x ==4: for a in range(1,7): for b in range(7,a,-1): if (b+a)%2==0: print('x', end=" ") else: print('@', end=" ") print() ch = input ("Do U want to contunue ")# Program 18import mathch = 'y'while ch=='y': print("1. Print Fibonacci series") print("2. To check wheather the number of armstrong or not") print("3. To check wheather the number id prime or not") x = int(input("enter choice")) if x==1: n = int(input("Enter N")) i=1 a=1 b=1 print (a) print (b) while i<=n-2: c = a + b print(c) a=b b=c i=i+1 if x == 2: n = int(input("Enter a number")) count = 0 x = n while n!=0: n = n//10 count = count + 1 print("No of digits ",count) n=x s=0 while n!=0: r = n%10 s = s + math.pow(r,count) n = n//10 if x == s: print("No is armstrong") else: print("No is not armstrong") if x==3: n = int (input("Enter a number")) i=2 f= 0 while i<n: if n % i==0: f = 1 i = i + 1 if f==0: print(" Prime") else: print("Not Prime") ch = input ("Do U want to contunue ") ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download