0% found this document useful (0 votes)
21 views

Abcdd

The document contains code snippets related to Python classes, date and time handling, iterators, cryptography, calendar, collections and exception handling.

Uploaded by

Aishwarya Nayak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Abcdd

The document contains code snippets related to Python classes, date and time handling, iterators, cryptography, calendar, collections and exception handling.

Uploaded by

Aishwarya Nayak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

class son(parent):

def __init__(self, total_asset,pps):


super().__init__(total_asset)
self.pps = pps
def son_display(self):
print("Share of Son is "+str(round(self.total_asset*self.pps/100,2))+"
Million.")

class daughter(parent):
def __init__(self, total_asset,pps):
super().__init__(total_asset)
self.pps = pps
def daughter_display(self):
print("Share of Daughter is "+str(round(self.total_asset*self.pps/100,2))+"
Million.")

///////////////////////////////////
class rectangle:
def display(self):
display("This is a Rectangle")
def area(self, Length, Breadth):
area = 0.5 * Length * Breadth
print("Area of Rectanngle is {}".format(area))
class sqaure():
def display(self):
display("This is a Square")
def area(self, side):
area = side * side
print("Area of Square is {}".format(side))

//////////////Hands-on – Python Date Time//////////////////


import calendar

import datetime

def dateandtime(val,tup):
l1=[]
s=””
if(val==1):
for x in tup:
s+=str(x)

d1 = datetime.datetime.strptime(s,’%Y%m%d’).date()
l1.append(d1)
l1.append(datetime.date.strftime(d1,’%d/%m/%Y’))
elif(val==2):
l1.append(datetime.date.fromtimestamp(tup[0]))
elif(val==3):
s = “”
for x in tup:
s+=str(x)
s1=int(s)
t = datetime.time(tup[0],tup[1],tup[2])
l1.append(t)
h=datetime.time.strftime(t,’%I’)

l1.append(h)
elif(val==4):
for x in tup:
s+=str(x)
s1=int(s)
d1 = datetime.date(tup[0],tup[1],tup[2])
l1.append(calendar.day_name[d1.weekday()])
l1.append(calendar.month_name[d1.month])
l1.append(str(d1.strftime(‘%j’)))
elif(val==5):
s=””
for x in tup:
s+=str(x)
s1=int(s)
l1.append(datetime.datetime.strptime(s,’%Y%m%d%H%M%S’))
return l1

/////////////////////Hands-on – Python – Itertools///////////////////////


import itertools
import operator
def performIterator(tuplevalues):

l1=[]
l2=[]
for x in range(4):
l2.append(tuplevalues[0][x])

t2=tuple(l2)
l1.append(t2)

ll=[tuplevalues[1][0] for x in range(len(tuplevalues[1]))]

t3=tuple(ll)
l1.append(t3)
t4=tuple(itertools.accumulate(tuplevalues[2]))
l1.append(t4)

f=[]
for x in tuplevalues:
for y in x:
f.append(y)
l1.append(tuple(f))

ff = itertools.filterfalse(lambda x: x%2==0,f)
l1.append(tuple(ff))

tf = tuple(l1)
return tf

/////////////////////////Hands-on – Python – Cryptography Python –


Cryptography//////////////////////////////////////
from cryptography.fernet import Fernet
def encrdecr(keyval, textencr, textdecr):

l1=[]
f=Fernet(keyval)
l1.append(f.encrypt(textencr))
l1.append(f.decrypt(textdecr).decode())
return l1
////////////////////////Hands-on Python – Calendar Python –
Calendar///////////////////////////////
import calendar
import datetime
def usingcalendar(datetuple):

y=datetuple[0]
m=datetuple[1]
if(calendar.isleap(datetuple[0])== True):
m=2
print(calendar.month(datetuple[0],m))
ob = calendar.Calendar()

g=ob.itermonthdates(y,m)
ld=[x for x in g]
ldt = ld[-7:]
print(ldt)
try:
print(datetime.date(y, m,29).strftime('%A'))
except ValueError:
print("Monday")

/////////////////////////Python – Collections////////////////////////////////
from collections import OrderedDict
def collectionfunc(text1, dictionary1, key1, val1, deduct, list1):

d1=text1.split(' ')
d = {}
for x in d1:
if(x in d):
d[x]=d[x]+1
else:
d[x]=1
d2={}
for a in (sorted(d)):
d2[a]=d[a]
print(d2)
c=dictionary1

for x in c.keys():
if(x in deduct.keys()):
c[x]=c[x]-deduct[x]

for x in deduct.keys():
if(x not in c.keys()):
c[x]=0-deduct[x]

a=dict(c)
print(a)
od=OrderedDict()
for x in range(len(key1)):
od[key1[x]]=val1[x]
od.pop(key1[1])
od[key1[1]]=val1[1]
d=dict(od)
print(d)

df = {}
df['odd']=[]
df['even']=[]
for i in list1:
if(i%2==0):
df['even'].append(i)
else:
df['odd'].append(i)
print({k:v for k,v in df.items() if len(v)>0})

///////////////////Handling Exceptions-2 FOR LOOP/////////////////////////////


def FORLoop():

n = int(input())
l1=[]
for x in range(n):
a = int(input())
l1.append(a)
print(l1)
iter1 = iter(l1)
for x in range(n):
print(next(iter1))

return iter1
/////////////////////////Handling Exceptions – 3 Bank
ATM//////////////////////////////////
class MinimumDepositError(Exception):
pass
class MinimumBalanceError(Exception):
pass
def Bank_ATM(balance,choice,amount):

if(balance<500):
raise ValueError("As per the Minimum Balance Policy, Balance must be at
least 500")
if(choice == 1 and amount<2000):
raise MinimumDepositError("The Minimum amount of Deposit should be 2000.")
elif(choice == 1):
balance+=amount
print("Updated Balance Amount: "+str(balance))

if(choice == 2 and balance-amount<500):


raise MinimumBalanceError("You cannot withdraw this amount due to Minimum
Balance Policy")
elif(choice ==2):
balance-=amount
print("Updated Balance Amount: "+str(balance))

///////////////////////Handling Exception -4//////////////////////////////


def Library(memberfee,installment,book):
# Write your code here
b = ["Philosophers stone","Order of Phoenix","Chamber of Secrets","Prisoner of
Azkaban","Goblet of Fire","Half Blood Prince","Deathly Hallows 1","Deathly Hallows
2"]
c = []
for i in b:
e=i.upper()
c.append(e)
book=book.upper()

if installment > 3:
raise ValueError("Maximum Permitted Number of Installments is 3")
elif installment == 0:
raise ZeroDivisionError("Number of Installments cannot be Zero.")
elif installment <= 3:
a=memberfee/installment
print("Amount per Installment is {}".format(memberfee/installment))
if(not book in c):
raise NameError("No such book exists in this section")
else:
print("It is available in this section")

You might also like