0% found this document useful (0 votes)
14 views5 pages

Python For Og Lecture 63 - Kwargs Operator

The document discusses the **kwargs operator in Python. **kwargs allows a function to accept an arbitrary number of keyword arguments and makes them available as a dictionary. Several examples are provided to demonstrate how **kwargs works and how it can be used with other parameters and dictionaries.
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)
14 views5 pages

Python For Og Lecture 63 - Kwargs Operator

The document discusses the **kwargs operator in Python. **kwargs allows a function to accept an arbitrary number of keyword arguments and makes them available as a dictionary. Several examples are provided to demonstrate how **kwargs works and how it can be used with other parameters and dictionaries.
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

2/2/2021 Python for O&G Lecture 63: **kwargs operator - 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

# double start operator


# keyword argument

def func(**kwargs):
return kwargs

func(one = 1, two = 2, three = 3)

{'one': 1, 'three': 3, 'two': 2}

type(func(one = 1, two = 2, three = 3))

dict
/
2/2/2021 Python for O&G Lecture 63: **kwargs operator - Colaboratory

# just give positional argument here and see the results

func(1, 2, 3)

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-e0aac76a4982> in <module>()
1 # just give positional argument here and see the results
2
----> 3 func(1, 2, 3)

TypeError: func() takes 0 positional arguments but 3 were given

SEARCH STACK OVERFLOW

# make a dictionary using **kwargs which takes porosity, permeability and depth as keys

def func_2(**kwargs):
return kwargs

Code Text
func_2(por = 0.15, perm = 35, depth = 3500)

{'depth': 3500, 'perm': 35, 'por': 0.15}

# create a function which will loop in kwargs

def func_3(**abc):
for i, j in abc.items():
print(f'{i} is: {j}')

func_3(por = 0.15, perm = 35, depth = 3500)

/
2/2/2021 Python for O&G Lecture 63: **kwargs operator - Colaboratory

por is: 0.15


perm is: 35
depth is: 3500

# **kwargs with normal parameter

def func_4(a, b, **kwargs):


print(a)
print(b)
print(kwargs)

func_4(165, 465, por = 0.15, perm = 35, depth = 3500)

165
465
{'por': 0.15, 'perm': 35, 'depth': 3500}

# this time take normal parameter after the **kwargs

def func_5(**kwargs, a):


print(a)

print(kwargs)

func_4(165, por = 0.15, perm = 35, depth = 3500)

# In coming video, we will use *args, **kwargs, normal arguments all in one function and there we'll see the proper order of their positioning
/
2/2/2021 Python for O&G Lecture 63: **kwargs operator - Colaboratory

File "<ipython-input-14-34100b69241e>", line 5


def func_5(**kwargs, a):
^
SyntaxError: invalid syntax

SEARCH STACK OVERFLOW

Dictionary Unpacking

dict_1 = {
'por' : 0.14,
'perm': '40 md',
'depth': '2500 ft'
}

def func_6(**kwargs):
return kwargs

func_6(**dict_1)

{'depth': '2500 ft', 'perm': '40 md', 'por': 0.14}

/
2/2/2021 Python for O&G Lecture 63: **kwargs operator - Colaboratory

You might also like