Python 3 Function and OOPs Handson
Python 3 Function and OOPs Handson
Python 3 Function and OOPs Handson
for i in para:
if i in special1:
newpara += i
else:
word1 += i
rword2= word1[:70]
rword2= rword2[::-1]
print (rword2)
flag = 0
for each in list1:
if each in para:
continue
else:
flag = 1
break
if flag:
print("Every string in {0} were not
present".format(list1))
else:
print("Every string in {0} were present".
format(list1))
splitted =word1.split()
print (splitted[:20])
ans=[]
for each in splitted:
if splitted.count(each)<3:
if each not in ans:
ans.append(each)
print(ans[-20:])
print(word1.rindex(strfind))
Magic Constant Generator:
def generator_Magic(n1):
lst=[]
for i in range(3,n1+1):
M=i*(i*i+1)/2
lst.append(M)
for i in range(len(lst)):
yield lst[i]
if(val):
for i in range (len(lst1)):
yield lst1[i]
else:
for i in range (len (lst2)):
yield lst2[i]
Classes and Objects 1:
Task1:
class Movie:
def __init__(self, name, n, cost):
self.name = name
self.n = n
self.cost=cost
def __str__(self):
return"Movie : {self.name}\nNumber of Tickets : {self.
n}\nTotal Cost : {self.cost}".format(self=self)
Task2:
class comp:
def __init__(self, a,b):
self.a = a
self.b = b
def son_display(self):
Percentage_for_son = (self.t*self.sp)/100
print("Share of Son is {} Million.".format(round(Perce
ntage_for_son, 2)))
print("Total Asset Worth is {} Million.". format(round
(self.t, 2)))
Task 2:
class rectangle:
def display (self):
print("This is a Rectangle")
class square:
def display (self):
print("This is a Square")
def area(self, a):
self.a = a
print("Area of square is {}".format(self.a * self.a))
Handling Exceptions 1:
def Handle_Exc1():
# Write your code here
a = int(input())
b = int(input())
if(a>150 or b<100):
print("Input integers value out of range.");
elif(a + b) > 400:
print("Their sum is out of range")
else:
print("All in range")
Handling Exceptions 2:
def FORLoop():
# Write your code here
n = int(input())
l1=[]
for i in range(n):
l1.append(int(input()))
print (l1)
iter1= iter(l1)
return iter1
Handling Exceptions 3:
def Bank_ATM(balance,choice,amount):
# Write your code here
flag = 0
if balance < 500:
print("As per the Minimum Balance Policy, Balance must
be at least 500")
else:
if (choice == 1):
if amount < 2000:
print("The Minimum amount of Deposit should be
2000.")
else:
flag = 1
balance += amount
if (choice == 2):
if(balance - amount)<500:
print("You cannot withdraw this amount due to
Minimum Balance Policy")
else:
flag=1
balance-=amount
if(flag):
print("Updated Balance Amount: {}".format(balance))
Handling Exceptions 4:
def Library(memberfee,installment,book):
# Write your code here
if(installment > 3):
print("Maximum Permitted Number of Installments is 3")
else:
if (installment == 0):
print("Number of Installments cannot be Zero." )
else:
print("Amount per Installment is {}".format (memb
erfee/installment))
elif val == 2:
c1= datetime.date.fromtimestamp (tup[0])
l.append(c1)
elif val == 3:
c1 = datetime.time (tup [0], tup [1], tup [2])
l.append(c1)
l.append(c1.strftime ("%I"))
elif val == 4:
c1= datetime.date(tup[0], tup [1], tup[2])
l.append(c1.strftime ("%A"))
l.append(c1.strftime("%B"))
l.append(c1.strftime("%j"))
elif val == 5:
c1= datetime.datetime (tup[0], tup[1], tup [2], tup[3]
, tup [4], tup[5])
l.append(c1)
return l
Modules 2: Itertools
import itertools
import operator
def performIterator(tuplevalues):
# Write your code here
l1 = []
ans = []
count = 0
for i in temp:
l1.append(i)
count+=1
if count == 4:
break
ans.append(tuple(l1))
ans.append(tuple (list(itertools.repeat (tuplevalues [1][0
], len(tuplevalues[1])))))
return mainList
Modules 4: Calendar
import calendar
import datetime
lst = []
MONTH= month
YEAR = year
for i in range(7):
d= datetime.date (year, month, day)
lst.append(d)
if (leap == 1) and (month == 2):
if(day== 29):
day = 0
if(month== 12):
month = 1
year= year + 1
else:
month= month + 1
else:
if((month == 1) or (month== 3) or (month==5)or(mon
th ==7) or (month==8) or(month ==10)or(month == 12)) and (day
== 31):
day= 0
if(month== 12):
month = 1
year = year + 1
else:
month= month + 1
day= day + 1
print(lst)
a = (datetime.datetime(YEAR, MONTH,1))
starting_day = a.strftime ("%A")
print(days [pos])
Modules 5: Collections
import collections
def collectionfunc(text1, dictionary1, key1, val1, deduct, list1):
# Write your code here
d = {}
for each in text1.split():
if each in d:
d[each] += 1
else:
d[each]= 1
print(dict(cou))
del orde[key1[1]]
print(dict (orde))
if d4 ["even"]== []:
del d4["even"]
if d4 ["odd"] == []:
del d4["odd"]
print (d4)