0% found this document useful (0 votes)
6 views69 pages

Software Training and Function Basics

Uploaded by

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

Software Training and Function Basics

Uploaded by

punamdaware0
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Transforming career

Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

There are 2 types of functions or methods


1. In-built functions
2. User Defined Functions (UDF)
lst = [1,2,3,4,5,6,2,2,2,2,4,4,4,4,5,5,5,5,6,6,6,]

len(lst)

21

#count() This method will count the number of occurence


# of an element in the list
[Link](4)

list1 = [1,2,3,4,5]
list2 = [6,7,8,9]

[Link](list1)

list2

[6, 7, 8, 9, 1, 2, 3, 4, 5]

There are 2 things in a function:


1. Function definition
2. Function Calling
def test1():
print("Welcome to my first function !!")

test1()

Welcome to my first function !!

def test2(name):
print("Welcome ",name)

test2(1)

Welcome 1

def add(a,b,c,d,e,f):
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

print("The sum is ",a+b+c+d+e+f)

add(32874,32784,1,2,3,5)

The sum is 65669

def sub(a,b):
return a-b
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

difference = sub(100,59)

difference

41

addition = add(1,2,3,4,5,6)

The sum is 21

addition

def test5():
a = int(input("Please enter 1st number"))
b = int(input("Please enter 2nd number"))
sum = a+b
return sum

x = test5()

Please enter 1st number128


Please enter 2nd number329

457

def test6():
if 10>50:
print("Hello")
else:
print("Bye")

test6()

Bye

#Scope and Nested in functions


x = 25
def printer():
x = 50
x = 100
return x

x = x+100
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

print(x)

125

printer()

100
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

print(x)

25

def greet():
print("I am in greet function !!!")
def hello1():
print("I am in hello1 function !!")
hello1()

greet()

I am in greet function !!!

hello1()

NameError Traceback (most recent call


last)
<ipython-input-66-e07fcb05c47e> in <module>()
----> 1 hello1()

NameError: name 'hello1' is not defined

#10/03/2022
#map function
def square(num):
return num**2

my_nums = [1,2,3,4,5]

list(map(square,my_nums))

[1, 4, 9, 16, 25]

def add(a,b):
return a+b

list(map(add,[1],[2]))

[3]

#Filter
def check_even(num):
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

return num%2==0

nums = [7,10,0.3,0,10,0,2]

list(filter(check_even,nums))

[10, 0, 10, 0, 2]
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

list(map(check_even,nums))

[False, True, False, True, True, True, True]

list(filter(square,nums))

[7, 10, 0.3, 10, 2]

#lambda

def squares(num):
result = num**2
return result

squares(2)

def addition(a,b): return a+b

addition(5,6)

11

lambda num: num**2

<function main .<lambda>>

x = lambda num: num**2

x(2)

y = lambda a,b: a+b

y(2,3)

my_nums = [1,2,3,4,5]

list(filter(lambda n: n%2==0,my_nums))

[2, 4]
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

There are 2 types of functions or methods


1. In-built functions
2. User Defined Functions (UDF)
lst = [1,2,3,4,5,6,2,2,2,2,4,4,4,4,5,5,5,5,6,6,6,]

len(lst)

21

#count() This method will count the number of occurence


# of an element in the list
[Link](4)

list1 = [1,2,3,4,5]
list2 = [6,7,8,9]

[Link](list1)

list2

[6, 7, 8, 9, 1, 2, 3, 4, 5]

There are 2 things in a function:


1. Function definition
2. Function Calling
def test1():
print("Welcome to my first function !!")

test1()

Welcome to my first function !!

def test2(name):
print("Welcome ",name)

test2(1)

Welcome 1

def add(a,b,c,d,e,f):
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

print("The sum is ",a+b+c+d+e+f)

add(32874,32784,1,2,3,5)

The sum is 65669

def sub(a,b):
return a-b
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

difference = sub(100,59)

difference

41

addition = add(1,2,3,4,5,6)

The sum is 21

addition

def test5():
a = int(input("Please enter 1st number"))
b = int(input("Please enter 2nd number"))
sum = a+b
return sum

x = test5()

Please enter 1st number128


Please enter 2nd number329

457

def test6():
if 10>50:
print("Hello")
else:
print("Bye")

test6()

Bye

#Scope and Nested in functions


x = 25
def printer():
x = 50
x = 100
return x

x = x+100
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

print(x)

125

printer()

100
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

print(x)

25

def greet():
print("I am in greet function !!!")
def hello1():
print("I am in hello1 function !!")
hello1()

greet()

I am in greet function !!!

hello1()

NameError Traceback (most recent call


last)
<ipython-input-66-e07fcb05c47e> in <module>()
----> 1 hello1()

NameError: name 'hello1' is not defined


Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

lst = [1,2,3,2,2]
a = 10

print(type(lst))

<class 'list'>

print(type(a))

<class 'int'>

[Link](2)

print(type(1))
print(type([]))

<class 'int'>
<class 'list'>

#Create a new object type called Sample


class Sample:
pass

#Instance of a class
x = Sample()
print(type(x))

<class ' main .Sample'>

class Dog:
def init (self,in_breed):
self.out_breed=in_breed

sam = Dog('Lab123')

sam.out_breed

{"type":"string"}

test = Dog('test123')
test.out_breed

{"type":"string"}
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

sam.out_breed

{"type":"string"}

sam = Dog('test123456')

sam.out_breed

{"type":"string"}
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

print(type(test))

<class ' main .Dog'>

class Dog1:
def init (self,a,b):
self.z = a
self.y = b

x = Dog1(2,3)

print(x.y+x.z)

class Dog:
species = 'mammal'
def init (self,breed,name):
[Link]=breed
[Link]=name

sam = Dog('Lab','Sam')

[Link]

{"type":"string"}

[Link]

{"type":"string"}

[Link]='test'

sam1 = Dog('Lab1','Sam1')

[Link]

{"type":"string"}

[Link]

{"type":"string"}

class Circle1:
pi = 3.14
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

def init (self,radius=1):


[Link]=radius
[Link] = radius*radius*[Link]
print([Link])

def setRadius(self,new_radius):
[Link] = new_radius
[Link] = new_radius*new_radius*[Link]
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

print([Link])

def getCircumference(self):
return [Link]*[Link]*2

c1 = Circle1()

3.14

[Link](4)

50.24

[Link]()

25.12
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

#There is no error no exception


a = 10
b = 20
print(a+b)

30

#There is an error
a = 10
b = '20'
print(a+b)

TypeError Traceback (most recent call


last)
<ipython-input-2-dc4b1a56c2cb> in <module>()
2 a = 10
3 b = '20'
----> 4 print(a+b)

TypeError: unsupported operand type(s) for +: 'int' and 'str'

#Exception handled by the developer


try:
a = 10
b = '20'
print(a+b)
except:
print("There is some error !!!")

There is some error !!!

try:
a = 'India
except:
print('Hi')
File "<ipython-input-6-2d26fed926fc>", line 2
a = 'India
^
SyntaxError: EOL while scanning string literal
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

# if try fails, it goes to except and if except fails


# then it goes to python interpreter
try:
a = 10
b = '20'
print(a+b)
except:
print("There is some error !!!)
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

File "<ipython-input-11-6a1cd00e525c>", line 6


print("There is some error !!!)
^
SyntaxError: EOL while scanning string literal

try:
number = int(input("Enter a number: "))
print("You entered a valid number")
except:
print("Invalid Attempt..!!")

Enter a number: 876


You entered a valid number

# It is not recommended to write logic in except block.


try:
number = int(input("Enter a number: "))
print("You entered a valid number")
except:
number=int(input("Enter a number"))
print("Valid Attempt")

Enter a number: ankur


Enter a numberankur

ValueError Traceback (most recent call


last)
<ipython-input-14-d90156e2d79b> in <module>()
1 try:
----> 2 number = int(input("Enter a number: "))
3 print("You entered a valid number")

ValueError: invalid literal for int() with base 10: 'ankur'

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call


last)
<ipython-input-14-d90156e2d79b> in <module>()
3 print("You entered a valid number")
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

4 except:
----> 5 number=int(input("Enter a number"))
6 print("Valid Attempt")

ValueError: invalid literal for int() with base 10: 'ankur'

a = True
while a:
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

try:
number = int(input("Please enter a number "))
print("You entered a valid number ")
break
except:
print("Invalid input, please try again ")
Please enter a number Ankur
Invalid input, please try again
Please enter a number Datascience
Invalid input, please try again
Please enter a number Technogeeks
Invalid input, please try again
Please enter a number 123
You entered a valid number
a = True
while a:
number = int(input("Please enter a number "))
print("You entered a valid number ")
break
Please enter a number Ankur

ValueError Traceback (most recent call


last)
<ipython-input-2-0ffb5d341da9> in <module>()
1 a = True
2 while a:
----> 3 number = int(input("Please enter a number "))
4 print("You entered a valid number ")
5 break

ValueError: invalid literal for int() with base 10: 'Ankur'

tascience

a = 0
password = 123
while a<3:
try:
number = int(input("Enter the otp: "))
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

if number == password:
print("Transaction successful")
break
else:
a = a+1
continue
except:
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

a = a+1
print("Invalid Input entered, please enter a numeric value")
if a ==3:
print("You tried 3 times with wrong password, account blocked")

Enter the otp: 987


Enter the otp: 98
Enter the otp: 67
You tried 3 times with wrong password, account blocked
try:
name = input("Enter your user name : ")
age = int(input("Enter your age: "))
print("Logged in successfully !!")
print(type(age))
print("My name is ........... ")
except:
print("Wrong Input !!")

Enter your user name : ankur


Enter your age: 12
Logged in successfully !!
<class 'int'>
My name is ....

try:
name=input("Enter your username ")
age = int(input("Enter your age "))
print("Logged in successfully !!")
except:
print("Wrong input !!")
finally:
print("Successfully Logged out !!")

Enter your username ankur


Enter your age ankur
Wrong input !!
Successfully Logged out !!
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

Python has the datetime module to help you deal with the timestamps in your code.
from datetime import datetime

t = [Link]()
print(t)
2022-03-21 02:43:33.165279

from datetime import time


print("Earliest time is :",[Link])

Earliest time is : 00:00:00

print("Latest time is :",[Link])

Latest time is : 23:59:59.999999

from datetime import date


today = [Link]()
print(today)

2022-03-21

print("Earliest date : ",[Link])


print("Last date : ",[Link])

Earliest date : 0001-01-01


Last date : 9999-12-31

d1 = date(2015,12,21)
print('date1',d1)

date1 2015-12-21

d2 = [Link](year=1990)
print(d2)

1990-12-21

d2

[Link](1990, 12, 21)

d1-d2
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

[Link](days=9131)

from time import localtime


obj = localtime()
obj.tm_zone

{"type":"string"}
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

Task: How to change the time zone in Python


d1 = date(2015,12,21)

print([Link])

12

[Link]

2015

[Link]

21

x = str([Link])+'-'+str([Link])+'-'+str([Link])

{"type":"string"}

import datetime
x1 = [Link](2020,1,20)
print(x1)

2020-01-20 00:00:00

print([Link]("%Y %d"))

2020 20

type(x1)

[Link]
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

#Hexadecimal: Using the function hex(), you can convert


# numbers into hexadecimal format

print(hex(246))
print(hex(512))

0xf6
0x200

#Binary:
bin(1234)

{"type":"string"}

bin(128)

{"type":"string"}

bin(512)

{"type":"string"}

2**3

pow(3,4)

81

3**4

81

round(3.537,1)

3.5

round(336,-2)

300

round(320,-2)

300

round(376,-1)
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

380

round(500,-3)

0
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

s = 'hello world'
[Link]()

{"type":"string"}

s = [Link]()

{"type":"string"}

[Link]()

{"type":"string"}

{"type":"string"}

[Link]('O') #Returns the number of occurences

[Link]("O") #Returns the starting index


#position of the first occurence

len(s)

11
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

class Animal:
def init (self,name,legs):
[Link] = name
[Link] = legs

class Bear(Animal):
def init (self,name,legs=4,hibernate='yes'):
[Link]=name
[Link]=legs
[Link]=hibernate

b = Bear('sample',5,'no')

print([Link])
print([Link])
print([Link])

sample
no
5
class Animal:
def init (self,name,legs):
[Link] = name
[Link] = legs

class Bear(Animal):
def init (self,name='xyz',legs=4,hibernate=5):
Animal. init (self,name,legs)
[Link]=hibernate

obj = Bear('test',2,20)
print([Link])
print([Link])
print([Link])

test
2
20
#Multiple Inheritance
class Car:
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

def init (self,wheels=4):


[Link] = wheels

class Gasoline(Car):
def init (self,engine='Gasoline',tank_cap=20):
Car. init (self)
[Link]=engine
self.tank_cap=tank_cap
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

[Link]=0

def refuel(self):
[Link]=self.tank_cap

class Electric(Car):
def init (self,engine='Electric',KWh_cap=60):
Car. init (self)
[Link]=engine
self.KWh_cap=KWh_cap
[Link]=0

def recharge(self):
[Link] = self.KWh_cap

class Hybrid(Gasoline,Electric):
def init (self,engine='Hybrid',tank_cap=11,KWh_cap=0):

Gasoline. init (self,engine)


Electric. init (self,engine)

prius = Hybrid()
print([Link])
print(prius.tank_cap)

Hybrid
20

[Link]()

print([Link])

60

class MyBaseClass1:
def init (self,y,x=100):
self.x = x
self.y = y

class MyDerivedClass1(MyBaseClass1):
def init (self,x,y,z):
super(). init (x,y)
self.z=z
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

tst = MyDerivedClass(10,20,30)

test.z

30

test.x
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

20

test.y

10

tst.x

20

x = MyDerivedClass1(10,20,30)

x.x

20

class A:
def truth(self):
return "All numbers are even"

class B(A):
pass

class C(A):
def truth(self):
return "Some numbers are even"

class D(B,C):
def truth(self,num):
if num%2 == 0:
return [Link](self)

else:
return super().truth()

d = D()

[Link](7)

{"type":"string"}

[Link](50)

{"type":"string"}
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

from collections import Counter

lst = [1,2,2,2,2,2,2,3,3,3,3,3,1,12,3,2,32,1,21,1,2,3,4,5]

Counter(lst)

Counter({1: 4, 2: 8, 3: 7, 4: 1, 5: 1, 12: 1, 21: 1, 32: 1})

s = 'aaannnnnddddjjjiiieoejkllllljsaf'
Counter(s)

Counter({'a': 4,
'd': 4,
'e': 2,
'f': 1,
'i': 3,
'j': 5,
'k': 1,
'l': 5,
'n': 5,
'o': 1,
's': 1})

s = "My name is Ankur , I teach Datascience Ankur name"

words = [Link]()
words

['My',
'name',
'is',
'Ankur',
',',
'I',
'teach',
'Datascience',
'Ankur',
'name']

Counter(words)

Counter({',': 1,
'Ankur': 2,
'Datascience': 1,
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

'I': 1,
'My': 1,
'is': 1,
'name': 2,
'teach': 1})

# Write a python program to count the occurences of words.

c = Counter(words)
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

c.most_common(4)

[('name', 2), ('Ankur', 2), ('My', 1), ('is', 1)]

sum([Link]())

10

[Link]() #reset all counts

Counter()

list(c)

['My', 'name', 'is', 'Ankur', ',', 'I', 'teach', 'Datascience']

set(c)

{',', 'Ankur', 'Datascience', 'I', 'My', 'is', 'name', 'teach'}

dict(c)

{',': 1,
'Ankur': 2,
'Datascience': 1,
'I': 1,
'My': 1,
'is': 1,
'name': 2,
'teach': 1}

Default Dict: defaultdict is a dictionary like object which provides all methods
provided bydictionary byt takes a first argument as a default data type for the
dictionary.
from collections import defaultdict

d = {}
type(d)
dict
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

d = defaultdict(object)

d['one']

defaultdict(object, {'one': 'Ankur'})

d = defaultdict(lambda: 0)
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

d['one']

defaultdict(<function main .<lambda>>, {'one': 0})

d['two'] = 100

defaultdict(<function main .<lambda>>, {'one': 0, 'two': 100})

[Link]()

dict_keys(['one'])

print("Normal dictionary")

d = {}
d['a'] = 'A'
d['b'] = 'B'
d['c'] = 'C'
d['d'] = 'D'
d['e'] = 'E'

for k,v in [Link]():


print(k,v)

Normal dictionary
a A
b B
c C
d D
e E

from collections import OrderedDict

print("OrderedDictionary")

d = OrderedDict()
d['a'] = 'A'
d['b'] = 'B'
d['c'] = 'C'
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

d['d'] = 'D'
d['e'] = 'E'

for k,v in [Link]():


print(k,v)

OrderedDictionary
a A
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

b B
c C
d D
e E

print("Are the dictionaries equal ?")

d1 = {}
d1['a'] = 'A'
d1['b'] = 'B'

d2 = {}
d2['b'] = 'B'
d2['a'] = 'A'

print(d1==d2)

Are the dictionaries equal ?


True

print("Are the dictionaries equal ?")

d3 = OrderedDict()
d3['a'] = 'A'
d3['b'] = 'B'

d4=OrderedDict()
d4['b'] = 'B'
d4['a'] = 'A'

print(d3==d4)

Are the dictionaries equal ?


False

#Named tuple

t = (12,11,12)
t[0]

12

from collections import namedtuple


Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

Dog = namedtuple('Dog1','age breed name')

sam = Dog(breed = 'Lab',name='sample',age=2)

frank = Dog(age=3,breed='Shephard',name = 'Frankie')

type(sam)
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

main .Dog1

frank

Dog1(age=3, breed='Shephard', name='Frankie')


Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

Regular Expressions are text matching patterns described with a format syntax. In
normaltechnical jargan, "regex" is the short form of regular expressions. Regular
expressions includes a set of rules, which needs to be followed.
import re

text = "This is a string with term1 , but it does not have term"
pattern = ['term1','term2']

[Link](pattern,text)

<[Link] object; span=(22, 27), match='term1'>

if [Link](pattern,text):
print("Match was found !!")
else:
print("No match found !!")

TypeError Traceback (most recent call


last)
<ipython-input-10-f9aa2ec179ff> in <module>()
----> 1 if [Link](pattern,text):
2 print("Match was found !!")
3 else:
4 print("No match found !!")

/usr/lib/python3.7/[Link] in search(pattern, string, flags)


183 """Scan through string looking for a match to the pattern,
returning
184 a Match object, or None if no match was found."""
--> 185 return _compile(pattern, flags).search(string)
186
187 def sub(pattern, repl, string, count=0, flags=0):

/usr/lib/python3.7/[Link] in _compile(pattern, flags)


276 flags = [Link]
277 try:
--> 278 return _cache[type(pattern), pattern, flags]
279 except KeyError:
280 pass
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

TypeError: unhashable type: 'list'

for x in pattern:
print('Searching for "%s" in \n "%s"'%(x,text))

if [Link](x,text):
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

print("Match was found !!")


else:
print("No match found !!")

Searching for "term1" in


"This is a string with term1 , but it does not have term"
Match was found !!
Searching for "term2" in
"This is a string with term1 , but it does not have term"
No match found !!

pat = 'term1'
match = [Link](pat,text)

[Link]()

22

[Link]()

27

split_term = '@'
phrase = "What is the domain of this email : hello@[Link]"

[Link](split_term,phrase)

['What is the domain of this email : hello', '[Link]']

#findall : to find all the instances of a pattern

test = [Link]('match','test phrase match is in middle match')

test

['match', 'match']

def multi_re_find(patterns,phrase):
for x in patterns:
print('Searching for phrase using re : %s '%(x))
print([Link](x,phrase))
print('\n')

patterns = 'term1'
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

multi_re_find(patterns,text)

Searching for phrase using re : t


['t', 't', 't', 't', 't', 't', 't']

Searching for phrase using re : e


Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

['e', 'e', 'e', 'e']

Searching for phrase using re : r


['r', 'r', 'r']

Searching for phrase using re : m


['m', 'm']

Searching for phrase using re : 1


['1']

test_phrase = 'sdsd..sssddd...sdddsddd...dsds...dsssss'

pattern = 'sd+' #one s followed by 1 or more d


# 'sdddd',

[Link](pattern,test_phrase)

['sd', 'sd', 'sddddddddd', 'sddd', 'sddd', 'sd']

test_phrase = 'sdsd..sssddd...sdddsddd...dsds...dsssss'
pattern = 'sd*' #s followed by 0 or more d
[Link](pattern,test_phrase)

['sd',
'sd',
's',
's',
'sddd',
'sddd',
'sddd',
'sd',
's',
's',
's',
's',
's',
's']
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

Decorators can be thought of as functions which modify the functionality of another


functions.
def func():
return 1+1

def add(x,y):
return x+y

locals()

{'In': ['',
'def func():\n return 1+1',
'locals()',
'def func():\n return 1+1\n\ndef add(x,y):\n return x+y',
'locals()'],
'Out': {2: {...}},
'_': {...},
'_2': {...},
' ': '',
' ': '',
' builtin ': <module 'builtins' (built-in)>,
' builtins ': <module 'builtins' (built-in)>,
' doc ': 'Automatically created module for IPython interactive
environment',
' loader ': None,
' name ': ' main ',
' package ': None,
' spec ': None,
'_dh': ['/content'],
'_i': 'def func():\n return 1+1\n\ndef add(x,y):\n return x+y',
'_i1': 'def func():\n return 1+1',
'_i2': 'locals()',
'_i3': 'def func():\n return 1+1\n\ndef add(x,y):\n return x+y',
'_i4': 'locals()',
'_ih': ['',
'def func():\n return 1+1',
'locals()',
'def func():\n return 1+1\n\ndef add(x,y):\n return x+y',
'locals()'],
'_ii': 'locals()',
'_iii': 'def func():\n return 1+1',
'_oh': {2: {...}},
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

'_sh': <module '[Link]' from


'/usr/local/lib/python3.7/dist-packages/IPython/core/[Link]'>,
'add': <function main .add>,
'exit': <[Link] at 0x7fa3d76233d0>,
'func': <function main .func>,
'get_ipython': <bound method InteractiveShell.get_ipython of
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

<[Link]._shell.Shell object at 0x7fa3d75f8fd0>>,


'quit': <[Link] at 0x7fa3d76233d0>}

func()

s = 'Global Variable'
def check_for_locals():
a = 10
b = 20.5
c = 'Pune'
print(locals())
print(type(locals()))

check_for_locals()

{'a': 10, 'b': 20.5, 'c': 'Pune'}


<class 'dict'>

globals()['s']

{"type":"string"}

def hello(name='Jose'):
return 'Hello '+name

hello()

{"type":"string"}

# assign any function to a variable, then that variable


# will become another function
greet = hello

type(greet)

function

greet()

{"type":"string"}
del hello

greet()
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

{"type":"string"}

#Functions within Functions

def hello(name='Jose'):
print("The hello() has been executed")
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

def greet():
return '\t This is inside the gree() function'

def welcome():
return '\t This is inside the welcome() function'

print(greet())
print(welcome())
print("Now we are back in hello() function")

hello()

The hello() has been executed


This is inside the gree() function
This is inside the welcome() function
Now we are back in hello() function

welcome()

NameError Traceback (most recent call


last)
<ipython-input-25-a401d7101853> in <module>()
----> 1 welcome()

NameError: name 'welcome' is not defined

greet()

{"type":"string"}

def hello(name = 'Jose'):


def greet():
return '\t This is inside the gree() function'

def welcome():
return '\t This is inside the welcome() function'

if name == 'Jose':
return greet
else:
return welcome
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

x = hello

print(hello()())

This is inside the gree() function

hello()
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

This is inside the gree() function

#Functions as Arguments

def hello():
return 'Hi Jose !'

def other(x):
print("Other code ......... !!!")
print(x())

other(hello)

Other code ........ !!!


Hi Jose !
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

Decorators can be thought of as functions which modify the functionality of another


functions.
def func():
return 1+1

def add(x,y):
return x+y

locals()

{'In': ['',
'def func():\n return 1+1',
'locals()',
'def func():\n return 1+1\n\ndef add(x,y):\n return x+y',
'locals()'],
'Out': {2: {...}},
'_': {...},
'_2': {...},
' ': '',
' ': '',
' builtin ': <module 'builtins' (built-in)>,
' builtins ': <module 'builtins' (built-in)>,
' doc ': 'Automatically created module for IPython interactive
environment',
' loader ': None,
' name ': ' main ',
' package ': None,
' spec ': None,
'_dh': ['/content'],
'_i': 'def func():\n return 1+1\n\ndef add(x,y):\n return x+y',
'_i1': 'def func():\n return 1+1',
'_i2': 'locals()',
'_i3': 'def func():\n return 1+1\n\ndef add(x,y):\n return x+y',
'_i4': 'locals()',
'_ih': ['',
'def func():\n return 1+1',
'locals()',
'def func():\n return 1+1\n\ndef add(x,y):\n return x+y',
'locals()'],
'_ii': 'locals()',
'_iii': 'def func():\n return 1+1',
'_oh': {2: {...}},
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

'_sh': <module '[Link]' from


'/usr/local/lib/python3.7/dist-packages/IPython/core/[Link]'>,
'add': <function main .add>,
'exit': <[Link] at 0x7fa3d76233d0>,
'func': <function main .func>,
'get_ipython': <bound method InteractiveShell.get_ipython of
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

<[Link]._shell.Shell object at 0x7fa3d75f8fd0>>,


'quit': <[Link] at 0x7fa3d76233d0>}

func()

s = 'Global Variable'
def check_for_locals():
a = 10
b = 20.5
c = 'Pune'
print(locals())
print(type(locals()))

check_for_locals()

{'a': 10, 'b': 20.5, 'c': 'Pune'}


<class 'dict'>

globals()['s']

{"type":"string"}

def hello(name='Jose'):
return 'Hello '+name

hello()

{"type":"string"}

# assign any function to a variable, then that variable


# will become another function
greet = hello

type(greet)

function

greet()

{"type":"string"}
del hello

greet()
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

{"type":"string"}

#Functions within Functions

def hello(name='Jose'):
print("The hello() has been executed")
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

def greet():
return '\t This is inside the gree() function'

def welcome():
return '\t This is inside the welcome() function'

print(greet())
print(welcome())
print("Now we are back in hello() function")

hello()

The hello() has been executed


This is inside the gree() function
This is inside the welcome() function
Now we are back in hello() function

welcome()

NameError Traceback (most recent call


last)
<ipython-input-25-a401d7101853> in <module>()
----> 1 welcome()

NameError: name 'welcome' is not defined

greet()

{"type":"string"}

def hello(name = 'Jose'):


def greet():
return '\t This is inside the gree() function'

def welcome():
return '\t This is inside the welcome() function'

if name == 'Jose':
return greet
else:
return welcome
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

x = hello

print(hello()())

This is inside the gree() function

hello()
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

This is inside the gree() function

#Functions as Arguments

def hello():
return 'Hi Jose !'

def other(x):
print("Other code ......... !!!")
print(x())

other(hello)

Other code ........ !!!


Hi Jose !

# 04/04/2022
#Decorators
def first(msg):
print(msg)

first("Hello")
second = first
second('Hello')

Hello
Hello

When you are trying to run the code, both functions first and second give the same
[Link] the names first and second refer to the same function object.
def inc(x):
return x+1

def dec(x):
return x-1

def operate(func,x):
result = func(x)
return result

operate(inc,3)

4
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

operate(dec,3)

def is_called():
def is_returned():
print("Hello")
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

return is_returned

new = is_called()
#new = is_returned

new()

Hello

def make_pretty(func):
def inner():
print("I got decorated !!")
func()
return inner

def ordinary():
print("I am ordinary !!")

ordinary()

I am ordinary !!

pretty = make_pretty(ordinary)

pretty()

I got decorated !!
I am ordinary !!

@make_pretty
def abc():
print("I am in abc function")

xyz = make_pretty(abc)

xyz()

I got decorated !!
I got decorated !!
I am in abc function
def smart_divide(func):
def inner(a,b):
print("I am diving a and b")
if b==0:
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

print("Can not divide by 0")


return
return func(a,b)
return inner

@smart_divide
def divide(a,b):
print(a/b)
Transforming career
Yess InfoTech
Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

divide(100,10)

I am diving a and b
10.0

def star(func):
def inner(*args,**kwargs):
print("*"*30)
func(*args,**kwargs)
print("*"*30)
return inner

def percent(func):
def inner(*args,**kwargs):
print("%" * 30)
func(*args,**kwargs)
print("%"*30)
return inner

@star
@percent
def printer(msg):
print(msg)

printer("Hello")

******************************
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Hello
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
******************************

def print1(msg):
print(msg)

printer1 = star(percent(print1))
printer1('Hello')

******************************
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Hello
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
******************************
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

def new_decorator(func):
def wrap_func():
print("Code will be here before executing the func")
func()
print("Code here will be executed after func execution")

return wrap_func
Yess InfoTech
Transforming career Software Training & Placement

Head Office: Yess InfoTech, Office Number 101, Floor No 1,


Manisha Blitz, Near Shankar Math Pune- Solapur Highway
Near Magarpatta City, Hadapsar, Pune, Maharashtra 411013
Contact: 7798623005 Email:yessinfotech@[Link] Website:
[Link]

def func_needs_decorator():
print("This function is in need of a decorator")

func_needs_decorator()

This function is in need of a decorator

func_needs_decorator1 = new_decorator(func_needs_decorator)

func_needs_decorator1()

Code will be here before executing the func


Code will be here before executing the func
This function is in need of a decorator
Code here will be executed after func execution
Code here will be executed after func execution

@new_decorator
def func_needs_decorator_new():
print("This function is in need of new decorator !!")

func_needs_decorator_new()

Code will be here before executing the func


This function is in need of new decorator !!
Code here will be executed after func execution

You might also like