# Program 1: Demonstrate usage of basic regular expression import re # Search String str="The rain in spain" x=re.findall("ai",str) print("Search of string 'ai': ",x) x=re.search("\s" ,str) print("The first white-space character is located in position : ",x.start()) x=re.split("\s",str,1) print("Split the string in the occurrence of first white-space:",x) x=re.sub("\s","9",str) print("Each white-space is replaced with 9 digit: ",x) print("----------------------------------------------") # Metacharacters x=re.findall("[a-m]",str) print("The letters in between a to m in the string:",x) x=re.findall("spain$",str) if(x): print("\n",str, ": Yes, this string ends with 'spain' word ") else: print("\n No match ") x=re.findall("^The",str) if(x): print("\n",str,": Yes, this string starts with 'The' word") else: print("\n No match") str1="The rain in spain falls mainly in the plain" x=re.findall("aix*",str1) print("\n All ai matching characters:",x) if(x): print("Yes, there is atleast one match") else: print("No match") x=re.findall("al{2}",str1) print("\n The string which contains 'all':",x) if(x): print("Yes, there is atleast one match") else: print("No match") str2="That will be 59 dollars" x=re.findall("\d",str2) print("\n Digits in the string:",x) x=re.findall("do...rs",str2) print("\n Dot metacharacter matches any character:",x) any character: ['dollars'] # Program 2: Demonstrate use of advanced regular expressions for data validation (Password Validation Program) # 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") # Program 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("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) Output PROGRAM FOR BUILT-IN METHODS OF LIST Updated animal lists : ['Cat', 'Dog', 'Rabbit', 'Cow'] Updated List : ['Python', 'Java', 'C++', 'c'] Sorted list in Ascending : ['a', 'e', 'i', 'o', 'u'] sorted list in Descending : ['u', 'o', 'i', 'e', 'a'] Updated list : ['a', 'e', 'i', 'o', 'u'] Updated animal lists : ['cat', 'dog', 'pig'] Original list : ['Window', 'MacOS', 'Linux'] Updated list : ['Linux', 'MacOS', 'Window'] # Program 4: Demonstrate use of Dictionaries thisdict={ "NAME":"Vishal" ,"PLACE":"Sankeshwar" ,"USN":1711745 } print("Directory Content= ",thisdict) x=thisdict["PLACE"] print("\n PLACE is ",x) print("\n Keys in the dictionary are:") for x in thisdict: print(x) print("\n Values in this dictionary are: ") for x in thisdict: print(thisdict[x]) print("\n Both keys and values of the dictionary are: ") for x,y in thisdict.items(): print(x,y) print("\n Total items in the dictionary : ",len(thisdict)) thisdict["District"]="Belagavi" print("\n Added new item is = ",thisdict) thisdict.pop("PLACE") print("\n After removing the item from dictionary= ",thisdict) del thisdict["USN"] print("\n After deleting the item from dictionary= ",thisdict) thisdict.clear() print("\n Empty Dictionary= ",thisdict) thisdict={ "NAME":"Vishal" ,"PLACE":"Sankeshwar" ,"USN":1711745 } print("\n Present dictionary content= ",thisdict) mydict=thisdict.copy() print("\n Copy of dictionary is = ",mydict) Total items in the dictionary : 3 Added new item is = {'NAME': 'Vishal', 'PLACE': 'Sankeshwar', 'USN': 1711745, 'District': 'Belagavi'} After removing the item from dictionary= {'NAME': 'Vishal', 'USN': 1711745, 'District': 'Belagavi'} After deleting the item from dictionary= {'NAME': 'Vishal', 'District': 'Belagavi'} Empty Dictionary= {} Present dictionary content= {'NAME': 'Vishal', 'PLACE': 'Sankeshwar', 'USN': 1711745} Copy of dictionary is = {'NAME': 'Vishal', 'PLACE': 'Sankeshwar', 'USN': 1711745} # Program 7: Demonstrate Exceptions in Python try: print("Try block:") a = int(input("Enter a:")) b = int(input("Enter b:")) c = a/b; except ZeroDivisionError: print("Except block:") print("We can not divide by zero") else: print("Else block:") print("The division of a and b is: ",c) print("No exceptions") finally: print("Finally block:") print("out of try, except, else & finally block") # Progrm 8: Drawing Linechart and Barchart using matplotlib import matplotlib.pyplot as plt # Data x = [2, 3, 4, 6, 8] y = [2, 3, 4, 6, 8] # 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) plt.title('Bar Chart') plt.xlabel('X axis label') plt.ylabel('Y axis label') plt.bar(x, y) plt.show() # Program 10:Create Array using NumPy and Perform Operations on Array. 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) import pandas as pd #Read data form multiple sheets in an Excel file data = pd.read_excel('my.xlsx', sheet_name='persion') print("\n**Displaying Data from DataFrames**\n") print(data) print("\n**OPERATIONS ON DATAFRAMES**\n") print("\n1.View the first few rows of the DataFrame :") print(data.head()) print("\n2.Number of rows and columns in the DataFrame :",end="") print(data.shape) print("\n3.Filtered data(Age column<20) :") filtered_data = data[data['Age'] < 20] print(filtered_data) print("\n4.Sort DataFrame based on 'Age' col in ascending order :") sorted_data = data.sort_values(by='Age', ascending=True) print(sorted_data) print("\nProgram Closed...")