Skip to content

Commit 86105e2

Browse files
committed
Added Lambda Functions and Ternary Conditionals Operators
1 parent a56048f commit 86105e2

3 files changed

Lines changed: 85 additions & 3 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Basic cheatsheet for Python.
44

55
## Credits
66

7-
Based on the book writted by Al Sweigart, [Automate the Boring Stuff with Python](https://automatetheboringstuff.com/) under the [Creative Commons license](https://creativecommons.org/licenses/by-nc-sa/3.0/).
7+
Mostly based on the book writted by Al Sweigart, [Automate the Boring Stuff with Python](https://automatetheboringstuff.com/) under the [Creative Commons license](https://creativecommons.org/licenses/by-nc-sa/3.0/) and many other sources.
88

99
## PDF Version
1010

@@ -129,4 +129,6 @@ Based on the book writted by Al Sweigart, [Automate the Boring Stuff with Python
129129
- Disabling Logging
130130
- Logging to a File
131131
- Virtual Environment
132-
- Windows
132+
- Windows
133+
- Lambda Functions
134+
- One line if statement in Python (ternary conditional operator)

python_cheat_sheet.md

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@
121121
- [Logging to a File](#logging-to-a-file)
122122
- [Virtual Environment](#virtual-environment)
123123
- [Windows](#windows)
124+
- [Lambda Functions](#lambda-functions)
125+
- [One line if statement in Python (ternary conditional operator)](#one-line-if-statement-in-python-ternary-conditional-operator)
124126

125127
## Python Basics
126128

@@ -2475,4 +2477,82 @@ Usage:
24752477

24762478
Open up the command prompt and type ‘workon HelloWold’ to activate the environment and move into your root project folder:
24772479

2478-
workon HelloWold
2480+
workon HelloWold
2481+
2482+
## Lambda Functions
2483+
2484+
This function:
2485+
2486+
```python
2487+
>>> def add(x, y):
2488+
return x + y
2489+
2490+
>>> add(5, 3)
2491+
8
2492+
```
2493+
2494+
Is equivalente to the *lambda* function:
2495+
2496+
```python
2497+
>>> add = lambda x, y: x + y
2498+
>>> add(5, 3)
2499+
8
2500+
```
2501+
2502+
It's not even need to bind it to a name like add before:
2503+
2504+
```python
2505+
>>> (lambda x, y: x + y)(5, 3)
2506+
8
2507+
```
2508+
2509+
Like regular nested functions, lambdas also work as lexical closures:
2510+
2511+
```python
2512+
>>> def make_adder(n):
2513+
return lambda x: x + n
2514+
2515+
>>> plus_3 = make_adder(3)
2516+
>>> plus_5 = make_adder(5)
2517+
2518+
>>> plus_3(4)
2519+
7
2520+
>>> plus_5(4)
2521+
9
2522+
```
2523+
2524+
## One line if statement in Python (ternary conditional operator)
2525+
2526+
Many programming languages have a ternary operator, which define a conditional expression. The most common usage is to make a terse simple conditional assignment statement. In other words, it offers one-line code to evaluate the first expression if the condition is true, otherwise it evaluates the second expression.
2527+
2528+
<expression1> if <condition> else <expression2>
2529+
2530+
Example:
2531+
2532+
```python
2533+
>>> age = 15
2534+
2535+
>>> print('kid' if age < 18 else 'adult')
2536+
kid
2537+
```
2538+
2539+
Ternary operators can be changed:
2540+
2541+
```python
2542+
>>> age = 15
2543+
2544+
>>> print('kid' if age < 13 else 'teenager' if age < 18 else 'adult')
2545+
teenager
2546+
```
2547+
2548+
The code above is equivalent to:
2549+
2550+
```python
2551+
if age < 18:
2552+
if age < 12:
2553+
print('kid')
2554+
else:
2555+
print('teenager')
2556+
else:
2557+
print('adult')
2558+
```

python_cheat_sheet.pdf

15 KB
Binary file not shown.

0 commit comments

Comments
 (0)