Section B Programs: 1. Demonstrate usage of basic regular expression. import re pattern = '^a...s$' test_string = 'abyss' result = re.match(pattern, test_string) if result: print("Search successful.") else: print("Search unsuccessful.") str="The rain in spain" x=re.findall("ai",str) print(x) str="The rain in spain" x=re.search("\s" ,str) print(" The first white-space character is located in position : ",x.start()) str="The rain in spain" x=re.split("\s",str,1) print(x) str="The rain in spain" x=re.sub("\s","9",str) print(x Demonstrate use of advanced regular expressions for - data validation. # importing re library import re p=input("Enter your password:") x=True while x: if(len(p)<6 or len(p)>20): break elif not re.search("[a-z]",p): break elif not re.search("[A-Z]",p): break elif not re.search("[0-9]",p): break elif not re.search("[$#@]",p): break elif re.search("\s",p): break else: print("Valid Password") x=False break if x: print("Invalid Password") 3) Demonstrate use of List. print("PROGRAM FOR BUILT-IN METHODS OF LIST\n ") animal = ['Cat','Dog','Rabbit'] animal.append('Cow') print("Updated animal lists : ",animal) language = [ 'Python', 'Java', 'C++', 'French', 'c'] return_value = language.pop(3) print("Return value ; ", return_value) print("Updated List : " , language) vowels = [ 'e', 'a', 'u', 'o', 'i'] vowels.sort() print("Sorted list in Ascending : ", vowels) vowels.sort(reverse = True) print("sorted list in Descending : ",vowels) vowel = ['a', 'e', 'i', 'u'] vowel.insert(3,'o') print("Updated list : " , vowel) animal = ['cat', 'dog', 'rabbit', 'pig'] animal.remove('rabbit') print('Updated animal lists : ', animal) os= ['Window', 'MacOS', 'Linux'] print("Original list : ", os) os.reverse() print("Updated list : ",os) 3Demonstrate use of Dictionaries. thisdict={ "NAME":"Vishal" ,"PLACE":"Sankeshwar" ,"USN":1711745 } print("Dictionary Content= ",thisdict) x=thisdict["PLACE"] print("PLACE is ",x) for x in thisdict: print(x) print("Values in this Dictionary are: ") for x in thisdict: print(thisdict[x]) print("\nBoth keys and values of the Dictionary are: ") for x,y in thisdict.items(): print(x,y) print("Total items in the dictionary : ",len(thisdict)) thisdict["District"]="Belagavi" print("\nAdded new item is = ",thisdict) thisdict.pop("PLACE") print("\nAfter removing the item from dictionary= ",thisdict) del thisdict["USN"] print("\nAfter deleting the item from dictionary= ",thisdict) thisdict.clear() print("\n Empty Dictionary= ",thisdict) thisdict={ "NAME":"Vishal" ,"PLACE":"Sankeshwar" ,"USN":1711745 } print("\nPresent directory content= ",thisdict) mydict=thisdict.copy() print("\nCopy of dictionary is = ",mydict) 5. Create SQLite Database and Perform Operations on Tables. import sqlite3 # create a connection to a Sqlite database conn=sqlite3.connect('test.db') # create a cursor object cursor=conn.cursor() # create a table cursor.execute("create table emp1(id integer, name text)") #insert a record into the tables cursor.execute("insert into emp1(id,name) values(101,'Sachin T')") cursor.execute("insert into emp1(id,name) values(102,'Rahul S')") cursor.execute("insert into emp1(id,name) values(103,'Virat K')") print("\nDisplaying the emp1 table") cursor.execute("select *from emp1") rows=cursor.fetchall() for row in rows: print(row) print("\nafter update and delete the records in the table table") # update records in the table cursor.execute("update emp1 set name ='Raju' where id=101") #delete a record fro the table cursor.execute("delete from emp1 where id=103") print("Displaying the emp1 table") cursor.execute("select *from emp1") rows=cursor.fetchall() for row in rows: print(row) print("\nTable is droped...") #drop the table cursor.execute("drop table emp1") #commit the transaction conn.commit() #close the cursor and the connections cursor.close() conn.close() 6) Create a GUI using Tkinter module. import tkinter as tk # Create a new window window = tk.Tk() # Set the window title window.title("My GUI") # Add a label to the window label = tk.Label(window, text="This is example for label") label.pack() # Add a button to the window button = tk.Button(window, text="Click me!") button.pack() # Add a Checkbutton to the window chk1 = tk.Checkbutton(window, text="Python") chk2 = tk.Checkbutton(window, text="Java") chk1.pack() chk2.pack() # Add a entrybox to the window entry= tk.Entry(window, text="") entry.pack() #add a radiobutton R1=tk.Radiobutton(window,text="Python") R1.pack() R2=tk.Radiobutton(window,text="Java") R2.pack() #listbox # Start the event loop window.mainloop() 6) Demonstrate Exceptions in Python. try: print("Try block") a = int(input("Enter a:")) b = int(input("Enter b:")) c = a/b; print("a/b = %d"%c) except ZeroDivisionError: print("Except zerodivision error block") print("Division by zero") else: print("Else block") print("The division : ",c) print("No exceptions") finally: print("Finaly block") print("out of try ,except,else & finally block") 7) Drawing Line chart and Bar chart using Matplotlib. Note:install matplot library import matplotlib.pyplot as plt #Data x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] # Plot the line chart plt.subplot(121) plt.plot(x,y,color='tab:red') # Add labels and title plt.title('Line Chart') plt.xlabel('X axis label') plt.ylabel('Y axis label') # Show the plot plt.subplot(122) # Add labels and title plt.xlabel('X axis label') plt.ylabel('Y axis label') plt.title('Bar Chart') # Show the plot plt.bar(x,y) plt.show() 8) Drawing Histogram and Pie chart using Matplotlib. Note:install matplot library,install numpy library. import matplotlib.pyplot as plt import numpy as np # Generate some random data data = np.random.randint(0,10,100) # Create a histogram plt.subplot(121) plt.hist(data, bins=20) plt.title("Histogram of Random Data") plt.xlabel("Value") plt.ylabel("Frequency") plt.subplot(122) sizes=[30,25,20,15,10] labels=['A','B','C','D','E'] #Create pie chart plt.pie(sizes,labels=labels) #Add title plt.title("Pie chart example") #show the plot plt.show() 9) Create Array using NumPy and Perform Operations on Array. Note:install numpy library. import numpy as np # Create an array array = np.array([4,3,2,1]) # Print the array print("Array elements are : ",array) # Find the minimum value in the array min_value = np.min(array) # Find the maximum value in the array max_value = np.max(array) # Calculate the mean of the array mean = np.mean(array) # Print the minimum, maximum,mean and sort values print("The minimum value is:", min_value) print("The maximum value is:", max_value) print("The mean value is:", mean) #Broadcasting array = array + 10 print("Add 10 to each element in the array : ",array) array = array * 2 print("Multiply each element in the array by 2 : ",array) #Sorting the elements of the array sort=np.sort(array) print("Sorted of array elements : ",sort)