Python Exercise13

#regular expressions import re txt=”You can call me on 9988888888. My ATM pin is 1234 . You are good” #find all occurances of “ATM” words=re.findall(“ATM”,txt) print(“Occurances of ATM : “,words) #find if the string begins with “You” words=re.findall(“^You”,txt) print(words) #find if the string ends with “You” words=re.findall(“You$”,txt) print(words) #find all digits in the string” words=re.findall(“d”,txt) print(words) #find one or more digits” words=re.findall(“d+”,txt) print(words) #find ATM pin in the string” words=re.findall(“dddd”,txt) print(“ATM Pin”,words) #find ATM pins in the string” words=re.findall(“sdddds”,txt) print(words) #find ATM pins in the string” words=re.findall(“sd{4}s”,txt) print(words) #find ATM pins in the string” words=re.findall(r”bd{4}b”,txt) print(words) #find all ATM pin … Continue reading Python Exercise13

Python Exercise12

Write a program to take input of integers from the user and divide the numbers handle TypeError and ZeroDivisionError in your program try: a=input(“Please enter the dividend : “) b=input(“Please enter the divisor : “) print (a/b) except TypeError: print( “Both values must be integer”) except ZeroDivisionError: print (“Divisor must not be zero”) Write a program to enter the amount to withdraw from ATM handle your errors counter=1 while True: amount=input(“Enter the amount you wish to withdraw : “) try: print(int(amount)) print(“Dispensing your cash”) break except ValueError: print(“Pl. check the amount you entered”) if counter < 4: counter+=1 else: print(“I … Continue reading Python Exercise12

Python Exercise11

Write a program to take input from command line and print all the values from sys import argv script,first, second =argv print (“The script is called:”,script) print( “The first variable is :”,first) print (“Your Second variable is :”, second) Write a program to get input from command line and add those values import sys print(len(sys.argv)) if (len(sys.argv)>3): sum=0 for i in sys.argv[1:]: sum=sum+int(i) print(sum) else: print (“Please give atleast two parameters only”) print( “My name=”,sys.argv[0]) print (“First arg”,sys.argv[1]) print( “Second arg”,sys.argv[2]) Write a program to get input from command line and print number of arguments received import sys print (“My … Continue reading Python Exercise11

Python Exercise10

Write a program to find the random numbers from 1 to 100 and call the function for 500 times and print how many times each number has been repeated import random def myrandom():                return random.randint(1,100) #2. Call the function 500 times and save the results into a list. r=[] for x in range(500):                r.append(myrandom()) #3. Print how many times each number has repeated for x in set(r):                print( x,r.count(x)) Write a program to print the size of the file and the time file was created import os,time size=os.stat(“C:/Users/exercise.py”).st_size ctime=os.stat(“C:/Users/exercise.py”).st_ctime print (“Size of the file :”,size) print (“Created … Continue reading Python Exercise10

Python Exercise9

Write a program to stop the search as soon as we find 50 in the list numbers = [100, 25, 125, 50, 150, 75, 175] for x in numbers: print( x ) if x == 50: print( “Found It!” ) break Write a program to skip all triple digit numbers numbers = [100, 25, 125, 50, 150, 75, 175] for x in numbers: if x >= 100: continue print( x ) < Write a program to unpack the values to variables colors=”red,blue,green,yellow,pink” #print(colors.split(‘,’))               # list of colors r,b,g,y,p=colors.split(‘,’)            # unpacked in to variables … Continue reading Python Exercise9

Python Exercise8

1. Write a program to write all multiple of 7 from 10 to 100 in to a file f=open(‘file1.txt’,’w’) for i in range(14,100,7):  f.write(str(i)+ ‘n’) f.close() 2.  Write all elements in the list to a file a=[‘hellon’,’hown’,’aren’,’youn’] f=open(‘file1.txt’,’w’) a=[‘hellon’,’hown’,’aren’,’youn’] f.writelines(a) #write all the elements in list into file f.close() print(f.closed)        #Prints whether the file is closed or not 3. Write a program to count the number of words in a text file num_words = 0 f=open(‘input.txt’,’r’) for line in f: words = line.split() num_words += len(words) print(“Number of words:”,num_words) f.close()   4. Write a program to count the occurrences of … Continue reading Python Exercise8

Python Exercise7

Write a program to read file contents at a time f = open(‘input.txt’,’r’) print(f.read()) f.close()   Write a program to read a file char by char f = open(‘input.txt’,’r’) print(f.read(1)) print(f.read(1)) f.close()   Write a program to read a file line by line f = open(‘input.txt’,’r’) print(f.readline()) print(f.readline()) f.close()   Write a program to read all lines in to a list f = open(‘input.txt’,’r’) print(f.read()) f.close()   Write a program to write in to a file f = open(‘input.txt1′,’w’) f.write(“this is line1”) f.close()   Write a program to append to an existing file f = open(‘input.txt’,’a’) f.write(“nthis is newline”) f.close() … Continue reading Python Exercise7

Python Exercise6

Write a function to read each char from the word if the char matches with the word find the index of that char,store it in indices return indices  def get_indices(word,char):     indicies=[]     for x in range(len(word)):         if word[x]==char:             indicies.append(x)     print(indicies) get_indices(“apple”,”p”)   Write a function to calculate the factorial of a given number def factorial(n):     fact=1     for i in range(2,n+1):         fact=fact*i     return fact print(factorial(5))   Write a function to find fibonacci series def fib(x):     a, b = 0, 1     while a < x:         print(a)         a, b = b, a+b … Continue reading Python Exercise6

Python Exercise5

Print values from 0 to 10. for i in range(11):     print(i) Print values from 10 to 0  for x in range(10,-1,-1):     print(x) Print values from 100 to 170  for x in range(100,171):     print(x) Print all the even numbers from 0 to 100 for x in range(0,100,2):     print(x)  Create a list with different string values, and print all the values  cars=[“maruti”,”hyundai”,”bmw”,”benz”] for car in cars:     print(car)   Initialize a variable with a string and print each character  city=”Bangalore” for char in city:     print(char)   convert the below c for loop into python for (i=65;i<=345;i=i+5) { … Continue reading Python Exercise5