Professional Documents
Culture Documents
LAB – 2
# Q1. Check whether a number is even or odd
is_even = int(input("Enter a number: "))
if is_even % 2 == 0:
print("number is even")
else:
print("number is odd")
def triangle(n):
k = 2*n - 2
for i in range(0, n):
for j in range(0, k):
print(end=" ")
k=k-1
for j in range(0, i+1):
print("* ", end="")
print("\r")
n=4
triangle(n)
#Q7. Write a program to print this series: 1 1 2 3 5 8 13
a=0
b=1
print(a, b, end=' ')
for i in range(a, 6):
s=a+b
print(s, end=' ')
a=b
b=s
if(choice == 1):
print("Sum of {} and {} is: {}".format(val1, val2, add(val1, val2)))
elif(choice == 2):
print("Difference of {} and {} is: {}".format(val1, val2, sub(val1, val2)))
elif(choice == 3):
print("Product of {} and {} is: {}".format(val1, val2, mul(val1, val2)))
elif(choice == 4):
print("Quotient of {} and {} is: {}".format(val1, val2, div(val1, val2)))
elif(choice == 5):
print("Remainder of {} and {} is: {}".format(val1, val2, mod(val1, val2)))
LAB – 3
# Q1. Write a Python program to demonstrate while loop with else statement.
counter = 0
while counter < 3:
print("Inside while loop")
counter += 1
else:
print("Inside else")
# Q2. Write a Python program to print 1st 5 even numbers (use break statement).
num = 1
flag = 0
while num:
if num % 2 == 0:
print(num, end=' ')
flag+=1
elif flag == 5:
break
num += 1
# Q3. Write a Python program to print 1st 4 even numbers (use continue statement).
for i in range(1, 10):
if (i%2 == 0):
if(i < 10):
print(i, end=' ')
else:
continue
# Q6. Write a Python program to count the number of characters (character frequency) in a string
test_str = "thought Though tough"
freq = {}
for i in test_str:
if i in freq:
freq[i] += 1
elif i == " ":
continue
else:
freq[i] = 1
print(freq)
# Q7. Write a Python program to get a string made of the first 2 and the last 2 chars from a given a string. If the string
length is less than 2, return instead of the empty string.
test_str1 = "this is test string"
if len(test_str1) < 3:
print(" ")
else:
new = test_str1[:2] + test_str1[-2:]
print(new)
# Q8. Write a Python program to get a string from a given string where all occurrences of its first char have been
changed to '$', except the first char itself
old_string = "cute child cuts cake cleverly"
x = old_string[0]
new_string = old_string.replace(x, "$")
print(new_string.replace("$", x, 1))
# Q9. Write a Python program to get a single string from two given strings, separated by a space and swap the first
two characters of each string.
str1 = input()
str2 = input()
a = str1[0]
b = str2[0]
new_str1 = b + str1[1:]
new_str2 = a + str2[1:]
print("Original string 1: " + str1)
print("Original string 2: " + str2)
print("New string: " + new_str1 + " " + new_str2)
# Q10. Write a Python program to add 'ing' at the end of a given string (length should be at least 3). If the given string
already ends with 'ing' then add 'ly' instead. If the string length of the given string is less than 3, leave it unchanged
def string_update(string):
if len(string) < 3:
return string
elif string.endswith("ing"):
return string.replace("ing", "ly")
string = input()
print("Old string: {} \nNew string: {}".format(string, string_update(string)))
LAB – 4
1. Write a Python program to sum all the items in a list.
sum = 0
list1 = [1, 2, 3, 4, 5]
print("List: {}".format(list1))
multiply = 1
print("List: {}".format(list2))
large = list3[0]
large = element
print("List: {}".format(list3))
small = list4[0]
small = element
print("List: {}".format(list4))
5. Write a Python program to count the number of strings where the string length is 2 or more and the first and last
character are same from a given list of strings
Sample List : ['abc', 'xyz', 'aba', '1221']
Expected Result : 2
def match_words(words):
ctr = 0
ctr += 1
return ctr
print(match_words(list5))
6. Write a Python program to get a list, sorted in increasing order by the last element in each tuple from a given list of
non-empty tuples.
Sample List : [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)] Expected Result : [(2, 1), (1, 2), (2, 3), (4, 4), (2, 5)]
def last(n):
return n[1]
def list_sort(lists):
list6 = [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]
print(list_sort(list6))
7. Write a Python program to remove duplicates from a list.
res = []
for x in list7:
if x not in res:
res.append(x)
list8 = [1, 2, 3]
if not list8:
print("List is empty")
else:
def cloning(old_list):
new_list = []
new_list = old_list
return new_list
l1 = [11,22,33,44,55]
l2 = cloning(l1)
10. Write a Python program to find the list of words that are longer than n from a given list of words.
n=4
new_list = []
for x in list_10:
if len(x) > n:
new_list.append(x)
print(new_list)
11. Write a Python function that takes two lists and returns True if they have at least one common member.
a_set = set(l1)
b_set = set(l2)
return True
else:
return False
l1 = [2, 4, 6, 8]
l2 = [1, 4, 6, 7]
print(common_member(l1, l2))
12. Write a Python program to print a specified list after removing the 0th, 4th and 5th elements.
Sample List : ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']
Expected Output : ['Green', 'White', 'Black']
color_list = ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']
new_list = []
new_list.append(color)
LAB – 5
1. Write a Python program to create a tuple.
3. Write a Python program to create a tuple with numbers and print one item.
4. Write a Python program to unpack a tuple in several variables.
7. Write a Python program to get the 4th element and 4th element from last of a tuple
LAB – 6
Dictionary
1. Create an empty dictionary
2. Create the following dictionary
a. Key value
b. A 10
c. B 20
Sample Dictionary :
dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50,6:60}
Expected Result : {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
16. Write a Python script to print a dictionary where the keys are numbers between 1 and 15 (both included) and the
values are square of keys.
Sample Dictionary
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225}
17. Insert factorial of keys in values. And print dictionary
Sets
1. Write a program to create a set
9. Write a program that demonstrates the (built in function) Date & Time functions.
LAB – 8
1. Write a Python program that create a class tringle and define two methods, create_triangle() and print_sides().
2. Write a Python program to create a class with two methods get_String() and print_String().
3. Write a Python program to create a class Rectangle that takes the parameter length and width. The class should also
contain a method for computing its perimeter.
4. Write a Python program to create a class Circle that takes the parameter radius. The class should also contain two
methods for computing its area & perimeter respectively. Use constructor to implement initialization of parameters
5.Create a Circle class and initialize it with radius. Make two methods getArea and getCircumference inside this
class.
LAB – 9
1. Write a Python program that has a class Animal with a method legs(). Create two subclasses Tiger and Dog, access
the method leg explicitly with class Dog and implicitly with the class Tiger.
2. Write a Python program to create a class Employee. Define two subclasses: Engineer and Manager. Every class
should have method named printDesignation() that prints Engineer for Engineer class and Manager for Manager
Class.
3. Write a Python program to demonstrate classes and their attributes.