Python Progrmming Lab
Python Progrmming Lab
II Year - I Semester
0 0 3 2
PYTHON PROGRAMMING LAB
Exercise 1 - Basics
Exercise 2 - Operations
a) Write a program to compute distance between two points taking input from the user
(Pythagorean Theorem)
b) Write a program add.py that takes 2 numbers as command line arguments and prints its sum.
a) Write a Program for checking whether the given number is a even number or not.
b) Using a 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 a 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.
b) By considering the terms in the Fibonacci sequence whose values do not exceed four million,
find the sum of the even-valued terms.
Exercise - 5 - DS
a) Write a program to count the numbers of characters in the string and store them in a
dictionary data structure
b) Write a program to use split and join methods in the string and trace a birthday with a
dictionary data structure.
Exercise - 6 DS - Continued
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?
Exercise - 7 Files
Exercise - 8 Functions
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.
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.
Exercise - 12 - Modules
a) Class variables and instance variable and illustration of the self variable
i) Robot
ii) ATM Machine
Exercise - 15 - Testing
a) Write a test-case to check the function even_numbers which return True on passing a list of all
even numbers
b) Write a test-case to check the function reverse_string which returns the reversed string
Exercise - 16 - Advanced