kopia lustrzana https://github.com/animator/learn-python
Removed Formatting errors
rodzic
78e31c3817
commit
378dbddc67
|
@ -6,9 +6,9 @@ Functions in Python are so called first class objects, which means they can be t
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def func1():
|
def func1():
|
||||||
def func2():
|
def func2():
|
||||||
print("Printing from the inner function, func2")
|
print("Printing from the inner function, func2")
|
||||||
return func2
|
return func2
|
||||||
|
|
||||||
```
|
```
|
||||||
Assigning func1 to function_call object
|
Assigning func1 to function_call object
|
||||||
|
@ -17,7 +17,7 @@ function_call=func1()
|
||||||
```
|
```
|
||||||
Calling the function
|
Calling the function
|
||||||
```python
|
```python
|
||||||
>> function_call()
|
>>> function_call()
|
||||||
```
|
```
|
||||||
**Output**
|
**Output**
|
||||||
```
|
```
|
||||||
|
@ -50,8 +50,8 @@ Now that you have understood why \* is used, we can take a look at *args. *args
|
||||||
*args makes python functions flexible to handle dynamic arguments.
|
*args makes python functions flexible to handle dynamic arguments.
|
||||||
```python
|
```python
|
||||||
def test1(*args):
|
def test1(*args):
|
||||||
print(args)
|
print(args)
|
||||||
print(f"The number of elements in args = {len(args)}")
|
print(f"The number of elements in args = {len(args)}")
|
||||||
a=list(range(0,10))
|
a=list(range(0,10))
|
||||||
test1(*a)
|
test1(*a)
|
||||||
```
|
```
|
||||||
|
@ -64,8 +64,8 @@ If in the test1 we do not use \* in the argument
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def test1(*args):
|
def test1(*args):
|
||||||
print(args)
|
print(args)
|
||||||
print(f"The number of elements in args = {len(args)}")
|
print(f"The number of elements in args = {len(args)}")
|
||||||
a=list(range(0,10))
|
a=list(range(0,10))
|
||||||
test1(a)
|
test1(a)
|
||||||
```
|
```
|
||||||
|
@ -78,8 +78,8 @@ The number of elements in args = 1
|
||||||
**kwargs stands for keyword arguments. This is used for key and value pairs and similar to *args, this makes functions flexible enough to handle dynamic key value pairs in arguments.
|
**kwargs stands for keyword arguments. This is used for key and value pairs and similar to *args, this makes functions flexible enough to handle dynamic key value pairs in arguments.
|
||||||
```python
|
```python
|
||||||
def test2(**kwargs):
|
def test2(**kwargs):
|
||||||
print(kwargs)
|
print(kwargs)
|
||||||
print(f"The number of elements in kwargs = {len(kwargs)}")
|
print(f"The number of elements in kwargs = {len(kwargs)}")
|
||||||
test2(a=1,b=2,c=3,d=4,e=5)
|
test2(a=1,b=2,c=3,d=4,e=5)
|
||||||
```
|
```
|
||||||
The above snippet uses some key-value pairs and out test2 function gives the following output:
|
The above snippet uses some key-value pairs and out test2 function gives the following output:
|
||||||
|
@ -96,18 +96,18 @@ Now that we understand what first class object, *args, **kwargs is, we can move
|
||||||
```python
|
```python
|
||||||
import time
|
import time
|
||||||
def multiplication(a,b):
|
def multiplication(a,b):
|
||||||
start=time.time()
|
start=time.time()
|
||||||
c=a*b
|
c=a*b
|
||||||
total=time.time()-start
|
total=time.time()-start
|
||||||
print("Time taken for execution of multiplication",total)
|
print("Time taken for execution of multiplication",total)
|
||||||
return c
|
return c
|
||||||
|
|
||||||
def addition(a,b):
|
def addition(a,b):
|
||||||
start=time.time()
|
start=time.time()
|
||||||
c=a+b
|
c=a+b
|
||||||
total=time.time()-start
|
total=time.time()-start
|
||||||
print("Time taken for execution of addition ",total)
|
print("Time taken for execution of addition ",total)
|
||||||
return c
|
return c
|
||||||
|
|
||||||
multiplication(4,5)
|
multiplication(4,5)
|
||||||
addition(4,5)
|
addition(4,5)
|
||||||
|
|
Ładowanie…
Reference in New Issue