Python Practice 2
Python Practice 2
class MyExceptions(Exception):
pass
def verify_age(age):
if age < 18:
raise MyExceptions("Pahlebaalig ho jao!!")
else:
print("Badhai ho! Tum baalig ho!")
a = int(input("Umrabatao: "))
try:
verify_age(a)
except MyExceptions as e:
print("Gadbadhaigo!\nError:",e)
finally:
print("Chalo ji! Phirmulakathogi!")
Oops:
Q. You are given two complex numbers , and you have to print the result of their addition, subtraction,
multiplication, division and modulus operations, use classes for this, implement operator overloading.
Q. Write a class A and implement some methods (add, subtract, divide integers), write another class B
which inherits class A and call these 3 methods from class B object.
# for loop
print("For Loop")
for i in range(1, num):
print(i*2, end=" ")
print()
# while loop
print("While Loop")
i=1
while i< num:
print(i*2, end=" ")
i += 1
Q1) Reverse a given list using list comprehensions. Reverse same list also using list slicing.
# Reversing List using Slicing
print("Enter List")
lst = [i for i in input().split()]
rev_lst = lst[ : :-1]
print(rev_lst)
Q2) Write a function to verify that tuple is immutable this function takes any tuple as parameter.
Q5) Create a function which takes a string as an input and modify it by inserting tab escape sequence
between every character of it and return it.
So when returned data is printed it shows tabs between every character on console.
def convert(s):
new_s = []
k=0
length = len(s) + len(s)-1
for i in range(0,length):
if i%2==0:
new_s.append(s[k])
k += 1
else:
new_s.append('\t')
res = str("".join(new_s))
return res
print(convert("nitish"))
Q6) Using python string formatting print a table on console like this:
dash = '-' * 40
for i in range(len(data)):
if i == 0:
print(dash)
print('{:<10s}{:>4s}{:>12s}{:>12s}'.format(data[i][0],data[i][1],data[i][2],data[i][3]))
print(dash)
else:
print('{:<10s}{:>4d}{:^12s}{:>12.1f}'.format(data[i][0],data[i][1],data[i][2],data[i][3]))
Q7)Write a class which reads text file and create another file converting all data in binary and vice versa.
This is simplest form of data encryption/decryption, This class can have following methods:
read_text()
write_binary()
read_binary()
write_text()
data hiding
Object printing
Inheritance
polymorphism
abstraction
Q10) Write your own exception class and inherit properties of "Exception" class.
Write a program to show usage of your custom class.