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)
# Reversing List using List Comprehension
print("Enter List")
lst = [i for i in input().split()]
rev_lst = [lst[i] for i in range(len(lst)-1, -1, -1)]
print(rev_lst)
Q2) Write a function to verify that tuple is immutable this function takes any tuple as parameter.
Q3) Convert nested dictionary into flattened dictionary for example
given a input test = {"outer1": {"inner1": {"inner2": "data"}}, "outer2": "data", "outer3":
{"inner3": "data"}}
output should be:
{"outer1_inner1_inner2": "data", "outer2": :"data", "outer3:inner3": "data"}
Your program should also work for very large nested dict.
Q6) Using python string formatting print a table on console like this:
Name Age class Roll
Rishav 18 12 56565
Vishal 15 10 131313
Etc
data = [['NAME', 'AGE', 'HANDEDNESS', 'SCORE (%)'],
['Martin', 38, 'L', 54.123],
['Marty', 33, 'L', 32.438],
['Martine', 25, 'R', 71.128],
['Martyn', 59, 'R', 50.472],
['Mart', 23, 'L', 2.438],
['Martyne', 15, 'R', 71.128],
['Marlyn', 101, 'R', 0.472],
['Marti', 2, 'L', 55.438],
['Mardi', 9, 'R', 81.128],
['Martyne', 49, 'R', 24.472],
['Marteen', 91, 'L', 1.128]]
. Write your custom exception class “MyExceptions” and use it with any other python program .
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.
Loops and Iterators:
Q. Read an integer N from user, for all non-negative integers i<N, print i*2, write separate programs
using every kind of loop.
num = int(input("Number: "))
# 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