Skip to content

Commit c37e0d2

Browse files
committed
Added yield.md and Lambda.md
1 parent 502a6b2 commit c37e0d2

14 files changed

+127
-5
lines changed

Diff for: Lambda.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## Lambda
2+
```python
3+
Lambda functions are called as anonymous functions i.e a function that is defined without a name.
4+
The general syntax of a lambda function is quite simple:
5+
lambda argument_list: expression
6+
Lambda is a tool for building functions, or more precisely, for building function objects.
7+
That means that Python has two tools for building functions:
8+
def and lambda.
9+
Def:
10+
```
11+
![Code1](/images/L1.png)
12+
```python
13+
Lambda:
14+
```
15+
![Code2](/images/L2.png)
16+
```python
17+
Generally, functions are created for to reduce code duplication, or to modularize code.
18+
But suppose you need to create a function that is going to be used only once.
19+
Well, first of all, you don’t need to give the function a name.
20+
It can be “anonymous”. And you can just define it right in the place where you want to use it.
21+
That’s where lambda is useful.
22+
Lambda functions are mainly used in combination with the functions filter(), map() and reduce().
23+
```
24+
### With Map
25+
```python
26+
r = map(func, seq)
27+
The first argument func is the name of a function and the second a sequence (e.g. a list) seq. map() applies the function func to all the elements of the sequence seq.
28+
It returns a new list with the elements changed by func.
29+
Here is an example where using lambda with map reduces the size of code which was earlier written by normal functions.
30+
```
31+
![Code3](/images/L3.png)
32+
![Code4](/images/L4.png)
33+
### With FILTER()
34+
```pyhton
35+
The function filter(function, list) offers an elegant way to filter out all the elements of a list, for which the function function returns True.
36+
```
37+
![Code5](/images/L5.png)

Diff for: README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
5. [\__repr__]()
3131
8. [Miscellaneous Features]()
3232
1. [with as](./With_As.md)
33-
2. [yield]()
34-
3. [lambda]()
35-
4. [.format function]()
36-
5. [ logging in python]()
37-
6. [args and kwargs]()
33+
2. [yield](./Yield.md)
34+
3. [lambda](./Lambda.md)
35+
4. [.format function](./Format_Function.md)
36+
5. [ logging in python](./Logging.md)
37+
6. [args and kwargs](./Args.md)

Diff for: README.md~

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Advance Python Notes
2+
3+
### Content
4+
5+
1. [In-build and Custom Generators](./generator.md)
6+
2. [Modules in Python]()
7+
1. [Argeparse Module](./argparse.md)
8+
2. [Request Module](./Request_Module.md)
9+
3. [JSON module](./JSON.md)
10+
4. [Regular expression (re) module](./RE.md)
11+
5. [BeautifulSoup](./Beautiful_Soup.md)
12+
3. [Decorators in Python]()
13+
5. [Usefull functions]()
14+
1. [Enumerate]()
15+
2. [zip]()
16+
3. [map]()
17+
4. [reduce]()
18+
6. [Muliprocessing or Multi threading]()
19+
7. [Object Oriented Programming](./oopm.md)
20+
1. [ \__init__ (Constructor) ]()
21+
2. [Inheritance]()
22+
1. [Introduction]()
23+
2. [super method]()
24+
3. [Operator Overloading]()
25+
4. [Special methods]()
26+
1. [dir method]()
27+
2. [\__next__]()
28+
3. [\__iter__]()
29+
4. [\__str__]()
30+
5. [\__repr__]()
31+
8. [Miscellaneous Features]()
32+
1. [with as](./With_As.md)
33+
2. [yield](./Yield.md)
34+
3. [lambda](./Lamda.md)
35+
4. [.format function](./Format_Function.md)
36+
5. [ logging in python](./Logging.md)
37+
6. [args and kwargs](./Args.md)

Diff for: Yield.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## Yield
2+
```python
3+
When we call a function in Python, the function will normally start working until it encounters a return, exception,
4+
or reaches its end – after which it returns control back to the caller.
5+
Whenever you call that function again, the process will start from scratch!
6+
Whereas in case of the yield statement, it suspends function’s execution and sends a value back to calle,
7+
but retains enough state to enable function to resume where it is left off.
8+
When resumed, the function continues execution immediately after the last yield run.
9+
This allows its code to produce a series of values over time, rather them computing them at once and sending them back like a list.
10+
```
11+
![Code1](/images/Y1.png)
12+
```python
13+
Here we can’t return 2 values at a time.Using yield, it is allowed.
14+
```
15+
![Code2](/images/Y2.png)
16+
```python
17+
Yield are used in Python generators. A generator function is defined like a normal function, but whenever it needs to generate a value, it does so with the yield keyword rather than return. If the body of a def contains yield, the function automatically becomes a generator function.
18+
```
19+
![Code3](/images/Y3.png)
20+
```python
21+
Or it could be like this:
22+
```
23+
![Code4](/images/Y4.png)
24+

Diff for: Yield.md~

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## Yield
2+
```python
3+
When we call a function in Python, the function will normally start working until it encounters a return, exception,
4+
or reaches its end – after which it returns control back to the caller.
5+
Whenever you call that function again, the process will start from scratch!
6+
Whereas in case of the yield statement, it suspends function’s execution and sends a value back to calle,
7+
but retains enough state to enable function to resume where it is left off.
8+
When resumed, the function continues execution immediately after the last yield run.
9+
This allows its code to produce a series of values over time, rather them computing them at once and sending them back like a list.
10+
```
11+
![Code1](/images/Y1.png)
12+
```python
13+
Here we can’t return 2 values at a time.Using yield, it is allowed.
14+
```
15+
![Code2](/images/Y2.png)
16+
```python
17+
Yield are used in Python generators. A generator function is defined like a normal function, but whenever it needs to generate a value, it does so with the yield keyword rather than return. If the body of a def contains yield, the function automatically becomes a generator function.
18+
```
19+
![Code3](/images/Y3.png)
20+
```python
21+
Or it could be like this:
22+
```
23+
![Code4](/images/Y4.png)
24+

Diff for: images/L1.png

1.89 KB
Loading

Diff for: images/L2.png

1.97 KB
Loading

Diff for: images/L3.png

3.85 KB
Loading

Diff for: images/L4.png

3.07 KB
Loading

Diff for: images/L5.png

2.61 KB
Loading

Diff for: images/Y1.png

2.3 KB
Loading

Diff for: images/Y2.png

3.6 KB
Loading

Diff for: images/Y3.png

8.23 KB
Loading

Diff for: images/Y4.png

13.9 KB
Loading

0 commit comments

Comments
 (0)