DEPARTMENT OF CSE PYTHON PROGRAMMING LAB MREC (A) Page 1 DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING PYTHON PROGRAMMING LAB MANUAL B.Tech I II Year II Semester ACADAMIC YEAR : 2021 - 22 MALLA REDDY ENGINEERING COLLEGE (AUTONOMOUS) (An Autonomous institution, Autonomy granted by UGC and affiliated to JNTUH, Accredited by NAAC with ‘A’ Grade, Accredited by NBA (2008 - 11) & Recipient of World Bank Assistance under TEQIP phase – II S.C.1.1for the period (2011 - 14)) Maisammaguda, Dhulapally (Post. Via. Kompally), Secunderabad 500100. Website: www.mrec.ac.in DEPARTMENT OF CSE PYTHON PROGRAMMING LAB MREC (A) Page 2 DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING PYTHON PROGRAMMING LAB OBJECTIVES: After completion of this course, the students would be able to 1. Understand how to d e sign and program python application 2. Learn how to use list, tuples and dictionaries 3. Understand how to write functions and pass arguments LIST OF EXPERIMENTS 1 a) Write a program to purposefully raise Indentation Error and correct it. b) Write a program to compute distance between two points taking input from the user (Pythagorean Theorem). c) Write a program add.py that takes 2 numbers as command line arguments and prints its sum. 2 a) Write a Program for checking whether the given number is a even number or not. b) Using for loop, write a program that prints out the decimal equivalents of 1/2, 1/3, 1/4, . . . , 1/10. c) Write a program using for loop that loops over a sequence. What is sequen ce? d) Write a program using a while loop that asks the user for a number, and prints a countdown from that number to zero. 3 a) Find the sum of all the primes below two million. b) Each new term in the Fibonacci sequence is generated by adding the previous two te rms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even - valued terms. c) Write a program to count the numbers of characters in the given string and store them in a dictionary data structure DEPARTMENT OF CSE PYTHON PROGRAMMING LAB MREC (A) Page 3 d) Write a program to use split and join methods in the given string and trace a birthday with a dictionary data structure. 4 a) Write a program to combine two lists into a dictionary. b) Write a program to count frequency of characters in a given file. Can you use chara cter frequency to tell whether the given file is a Python program file, C program file or a text file? 5 a) Write a program to print each line of a file in reverse order. b) Write a program to compute the number of characters, words and lines in a file. 6 a) Write a function ball _collide that takes two balls as parameters and computes if they are colliding. Your function should return a Boolean representing whether or not the balls are colliding. Hint: Represent a ball on a plane as a tuple of (x, y, r), r be ing the radius. If (distance between two balls centers) <= (sum of their radii) then (they are colliding) b) Find mean, median, mode for the given set of numbers in a list. 7 a) Write a function nearly_ equal to test whether two strings are nearly equal. Two strings a and b are nearly equal when a can be generated by a single mutation on b. b) Write a function dups to find all duplicates in the list. c) Write a function unique to find all the unique elements of a list. 8 a) Write a function cumulative_product to c ompute cumulative product of a list of numbers. b) Write a function reverse to reverse a list. Without using the reverse function. 9 Create a Regular Expression and implement the following a) Recognize the following strings: “bat,” “bit,” “but,” “hat,” “hit,” or “hut.” b) Match any pair of words separated by a single space, i.e., first and last names. c) Match any word and single letter separated by a comma and single space, as in last name, first initial. 10 Write a python program to implement multithreading scenarios. 11 Write a python program to simulate the banking operations using Class. 12 Write a python program to demonstrate the Queue / Stack operations using Class. DEPARTMENT OF CSE PYTHON PROGRAMMING LAB MREC (A) Page 4 INDEX Program Name Page No 1 a) Write a program to purposefully raise Indentation Error and correct it. b) Write a program to compute distance between two points taking input from the user (Pythagorean Theorem). c) Write a program add.py that takes 2 numbers as command line arguments and prints its sum. 6 7 8 2 a) Write a Program for checking whether the given number is a even number or not. b) Using for loop, write a program that prints out the decimal equiva lents of 1/2, 1/3, 1/4, .. 1/10. c)Write a program using for loop that loops over a sequence. What is sequence? d) Write a program using a while loop that asks the user for a number, and prints a countdown from that number to zero. 9 9 10 11 3 a) Find the sum of all the primes below two million. b) Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even - valued terms. c) Write a program to count the numbers of characters in the given string and store them in a dictionary data structure d) Write a program to use split and join methods in the given string and trace a birthday with a dictionary data structure. 12 1 3 1 4 1 5 4 a) Write a program to combine two lists into a dictionary. b) Write a program to count frequency of characters in a given file. Can you use character frequency to tell whether the given file is a Python program file, C program file or a text file? 1 6 1 7 5 a) Write a program to print each line of a file in reverse order. b) Write a program to compute the number of characters, words and lines in a file. 1 9 20 DEPARTMENT OF CSE PYTHON PROGRAMMING LAB MREC (A) Page 5 6 a) Write a function ball _collide that takes two balls as parameters and computes if they are colliding. Your function should return a Boolean representing whether or not the balls are colliding. Hint: Represent a ball on a plane as a tuple of (x, y, r), r being the radius. If (distance between two balls centers) <= (sum of their radii) then (they are colliding) b) Find mean, median, mode for the given set of numbers in a list 2 1 2 3 7 a) Write a function nearly_ equal to test whether two strings are nearly equal. Two strings a and b are nearly equal when a can be generated by a single mutation on b. b) Write a function dups to find all duplicates in the list. c) Write a function unique to find all the unique elements of a list. 2 5 2 6 2 7 8 a) Write a function cumulative_product to compute cumulative product of a list of numbers. b) Write a function reverse to reverse a list. Without using the reverse function. 2 8 2 9 9 Create a Regular Expression and implement the following a) Recognize the following strings: “bat,” “bit,” “but,” “hat,” “hit,” or “hut.” b) Match any pair of words separated by a single space, i.e., first and last names. c) Match any word and single letter separated by a comma and single space, as in last name, first initial. 30 31 32 10 Write a python program to implement multithreading scenarios. 3 4 11 Write a python program to simulate the banking operations using Class. 3 6 12 Write a python program to demonstrate the Queue / Stack operations using Class. 3 8 DEPARTMENT OF CSE PYTHON PROGRAMMING LAB MREC (A) Page 6 1. a) Write a program to purposefully raise Indentation Error and correct it. Description : Indentation refers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important. Python uses indentation to indicate a block of code. The number of spaces is up to you as a programmer, but it has to be at least one. You have to use the same number of spaces in the same block of code, otherwise Py thon will give you an error Ex ample 1 - Program : if 5 > 2 : print(" Five is greater than two !") Output : DEPARTMENT OF CSE PYTHON PROGRAMMING LAB MREC (A) Page 7 Example 2 - Program : if 5 > 2: print("Five is greater than two!") print("Five is greater than two!") Output : DEPARTMENT OF CSE PYTHON PROGRAMMING LAB MREC (A) Page 8 1 b)Write a program to compute distance between two points taking input from the user (Pythagorean Theorem). Program : x1=int( input ("enter x1 : ")) x2=int( input ("enter x2 : ")) y1=int( input ("enter y1 : ")) y2=int( input ("enter y2 : ")) result= ((((x2 - x1 )** 2 ) + ((y2 - y1)** 2 ) )**0.5) print(result) Output : 1 c) Write a program add.py that takes 2 numbers as command line arguments and prints its sum. #addition of two numbers using commandline. Let the filename be add.py Program : import sys x=int(sys.argv[1]) y=int(sys.argv[2]) sum=x+y print("The result is :",sum) Output : p ython add.py 5 7 The result is: 12 DEPARTMENT OF CSE PYTHON PROGRAMMING LAB MREC (A) Page 9 2.a )Write a Program for checking whether the given number is a even number or not. Program : n=int(input("Number")) if n%2==0: print("Number is even") else: print("Number is odd") Output : 2.b) Using for loop, write a program that prints out the decimal equivalents of 1/2, 1/3, 1/4, ..1/10. Program : fori in range(2,11): print(1/i) Output : DEPARTMENT OF CSE PYTHON PROGRAMMING LAB MREC (A) Page 10 2.c) Write a program using for loop that loops over a sequence. What is sequence? Description : Sequences are one of the principal built - in data types besides numerics, mappings, files, instances and exceptions. Python provides for six sequence (or sequential) data types: S trings byte S equences byte arrays lists tuples range objects Program : thislist = ["apple", "banana", "cherry"] for x in thislist: print(x) Output : DEPARTMENT OF CSE PYTHON PROGRAMMING LAB MREC (A) Page 11 2.d) Write a program using a while loop that asks the user for a number, and prints a countdown from that number to zero. Program : n=int(input("number")) while n>=0: print(n) n=n - 1 Output : DEPARTMENT OF CSE PYTHON PROGRAMMING LAB MREC (A) Page 12 3 a) Find the sum of all the primes below two million. Program : s =0 fori in range(2,2000000): f=0 for j in range(2,int(i**0.5)+1): ifi%j==0: f=f+1 break if f==0: s=s+i print("sum is",s) Output: Sum is 142,913,828,922 DEPARTMENT OF CSE PYTHON PROGRAMMING LAB MREC (A) Page 13 3 .b ) Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even - valued terms. Program : n=int(input("enter the limit:")) a=1 b=2 s=0 while(i <n - 2 ): c=a+b if(c%2==0): s=s+c a=b b=c print("sum of even numbers",s) Output : enter the limit : 5 sum of even numbers : 10 DEPARTMENT OF CSE PYTHON PROGRAMMING LAB MREC (A) Page 14 3 .c ) Write a program to count the numbers of characters in the given string and store them in a dictionary data structure Program : x=input("enter string") r={} fori in x: ifi in r: r[i]+=1 else: r[i]=1 print(r) Output : DEPARTMENT OF CSE PYTHON PROGRAMMING LAB MREC (A) Page 15 3.d) Write a program to use split and join methods in the given string and trace a birthday with a dictionary data structure. Program : str1=input("Enter date of birth:") sp=str1.split(".") bday='/'.join(sp) d1={"birthday":bday } if "birthday" in d1: print(d1["birthday"]) Output : DEPARTMENT OF CSE PYTHON PROGRAMMING LAB MREC (A) Page 16 4.a )Write a program to combine two lists into a dictionary. Program : t1=["name","clg","age"] t2=["abc","mrec",34,] res = {} for key in t1: for value in t2: res[key] = value t2.remove(value) break print(res) Output : DEPARTMENT OF CSE PYTHON PROGRAMMING LAB MREC (A) Page 17 4.b )Write a program to count frequency of characters in a given file. Can you use character frequency to tell whether the given file is a Python program file, C program file or a text file? Program : import collections importpprint file_input = input('File Name: ') with open(file_input, 'r') as info: count = collections.Counter(info.read().upper()) value = pprint.pformat(count) print(value ) Output : File Name: abc.txt Counter({' ': 93, 'E': 64, 'N': 45, 'A': 42, 'T': 40, 'I': 36, 'O': 31, 'R': 29, 'H': 25, 'D': 19, 'M': 17, 'Y': 17, 'L': 15, 'F': 15, 'U': 14, 'C': 13, DEPARTMENT OF CSE PYTHON PROGRAMMING LAB MREC (A) Page 18 'G': 13, 'S': 12, ',': 7, 'B': 6, 'W': 5, '9': 5, '.': 4, 'P': 4, '1': 3, ' \ n': 2, '0': 2, '3': 2, ':': 1, ' - ': 1, 'K': 1, '(': 1, ')': 1, 'V': 1}) DEPARTMENT OF CSE PYTHON PROGRAMMING LAB MREC (A) Page 19 5.a ) Write a program to print each line of a file in reverse order. Description: reversed() function produces a reverse iterator. Program : textfile =open("a_file.txt") lines = textfile readlines () for line in reversed( lines ): print ( line ) Output : ghi def abc DEPARTMENT OF CSE PYTHON PROGRAMMING LAB MREC (A) Page 20 5.b ) Write a program to compute the number of characters, words and lines in a file. Program : file=open("sample.txt","r") number_of_lines =0 number_of_words =0 number_of_characters =0 for line in file: line = line strip (" \ n")# won'tcount \ n as character words = line split () number_of_lines +=1 number_of_words +=len( words ) number_of_characters +=len( line ) file. close () print ("lines:", number_of_lines, "words:", number_of_words, "characters:", number_of_characters) OUTPUT : lines: 3 words: 5 characters: 29