Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

2/2/2021 Python for O&G Lecture 65: Lambda Expression (Anonymous Function) - Colaboratory

Python for Oil and Gas

Website - https://petroleumfromscratchin.wordpress.com/

LinkedIn - https://www.linkedin.com/company/petroleum-from-scratch

YouTube - https://www.youtube.com/channel/UC_lT10npISN5V32HDLAklsw

# let's create a function which calculates pressure

def pressure(rho, h):


return f'The pressure is {.052*rho*h} psi'

pressure(10.1, 3500)

'The pressure is 1838.2 psi'

# we can create same results in one line using lambda expression

# no need to define function name when you use lambda expressions /


2/2/2021 Python for O&G Lecture 65: Lambda Expression (Anonymous Function) - Colaboratory
# no need to define function name when you use lambda expressions

# lambda a, b: 0.052*a*b # --->>> SYNTAX --->>> lambda (arguments): what they will return

press = lambda rho, h: f'The pressure is {0.052*rho*h} psi'

press(10.1, 3500)

'The pressure is 1838.2 psi'

# print the function name and varible name which contains the lambda expression

print(pressure)

print('----')

print(press)

<function pressure at 0x7f6ae3f66400>


----
<function <lambda> at 0x7f6ae3e85a60>

# example 2

# we want to create functions where when one porosity value is given, that function will convert that fraction value into percent value

def fract(a):
return a*100

fract(0.24)

24.0

fract_new = lambda a: a*100


fract_new(0.24)

24.0
/
2/2/2021 Python for O&G Lecture 65: Lambda Expression (Anonymous Function) - Colaboratory

# define a function which calculates us the flow rate when all required data is given (Darcy law example, we have seen already)

def flow_rate(a, b, c, d, e):


return (a*b*d)/(c*e)

# Given data:

# permeability value: 0.333 darcys a


# area: 3 cm2 b
# viscosity of fluid: 1 cp c
# Pessure differential: 20 atm d
# length covered by fluid: 4 cm e

# get same results using lambda expression

if else with lambda expression

# let us create a very simple function which takes porosity in fraction


# if it is less than 0.5 print Realistic value of porosity otherwise print unrealistic

def por(a):
if a<0.5:
print('Realistic porosity value')
else:
print('UNrealistic porosity value')

por(0.52)

UNrealistic porosity value

/
por new lambda a: 'Realistic porosity value' if a<0 5 else 'Unrealistic porosity value'
2/2/2021 Python for O&G Lecture 65: Lambda Expression (Anonymous Function) - Colaboratory
por_new = lambda a: Realistic porosity value if a<0.5 else Unrealistic porosity value

por_new(0.7)

'Unrealistic porosity value'

Practie question for you

# PROBLEM:
# If i give a list to my lambda expression, ings were it returns me the list which contains reversed strings if length those strless than 5

def func_3(a):
rev = []
for i in a:
if len(i) < 5:
rev.append(i[::-1])
else:
rev.append(i)
return rev

list_1 = ['ok', 'petroleum', 'scratch', 'hey', '442', '456987']

func_3(list_1)

['ko', 'petroleum', 'scratch', 'yeh', '244', '456987']

abc = lambda a: [i[::-1] for i in a if len(i) < 5]

abc(list_1)

['ko', 'yeh', '244']

xyz = lambda a: [i[::-1] if len(i) < 5 else i for i in a]


/
(li t )
2/2/2021 Python for O&G Lecture 65: Lambda Expression (Anonymous Function) - Colaboratory
xyz(list_1)

['ko', 'petroleum', 'scratch', 'yeh', '244', '456987']

You might also like