Browse Source

Added Walrus Operator

Added Walrus Operator
pull/196/head
Jogesh Ghadai 1 month ago
committed by GitHub
parent
commit
bedc8f6cc1
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
1 changed files with 34 additions and 1 deletions
  1. 35
      README.md

35
README.md

@ -10,7 +10,7 @@ Contents
--------
**   ** **1. Collections:** ** ** **[`List`](#list)**__,__ **[`Dictionary`](#dictionary)**__,__ **[`Set`](#set)**__,__ **[`Tuple`](#tuple)**__,__ **[`Range`](#range)**__,__ **[`Enumerate`](#enumerate)**__,__ **[`Iterator`](#iterator)**__,__ **[`Generator`](#generator)**__.__
**   ** **2. Types:** **          ** **[`Type`](#type)**__,__ **[`String`](#string)**__,__ **[`Regular_Exp`](#regex)**__,__ **[`Format`](#format)**__,__ **[`Numbers`](#numbers-1)**__,__ **[`Combinatorics`](#combinatorics)**__,__ **[`Datetime`](#datetime)**__.__
**   ** **3. Syntax:** **         ** **[`Args`](#arguments)**__,__ **[`Inline`](#inline)**__,__ **[`Import`](#imports)**__,__ **[`Decorator`](#decorator)**__,__ **[`Class`](#class)**__,__ **[`Duck_Types`](#duck-types)**__,__ **[`Enum`](#enum)**__,__ **[`Exception`](#exceptions)**__.__
**   ** **3. Syntax:** **         ** **[`Args`](#arguments)**__,__ **[`Inline`](#inline)**__,__ **[`Import`](#imports)**__,__ **[`Decorator`](#decorator)**__,__ **[`Class`](#class)**__,__ **[`Walrus`](#walrus)**__,__ **[`Duck_Types`](#duck-types)**__,__ **[`Enum`](#enum)**__,__ **[`Exception`](#exceptions)**__.__
**   ** **4. System:** **        ** **[`Exit`](#exit)**__,__ **[`Print`](#print)**__,__ **[`Input`](#input)**__,__ **[`Command_Line_Arguments`](#command-line-arguments)**__,__ **[`Open`](#open)**__,__ **[`Path`](#paths)**__,__ **[`OS_Commands`](#os-commands)**__.__
**   ** **5. Data:** **             ** **[`JSON`](#json)**__,__ **[`Pickle`](#pickle)**__,__ **[`CSV`](#csv)**__,__ **[`SQLite`](#sqlite)**__,__ **[`Bytes`](#bytes)**__,__ **[`Struct`](#struct)**__,__ **[`Array`](#array)**__,__ **[`Memory_View`](#memory-view)**__,__ **[`Deque`](#deque)**__.__
**   ** **6. Advanced:** **   ** **[`Operator`](#operator)**__,__ **[`Match_Stmt`](#match-statement)**__,__ **[`Logging`](#logging)**__,__ **[`Introspection`](#introspection)**__,__ **[`Threading`](#threading)**__,__ **[`Coroutines`](#coroutines)**__.__
@ -753,6 +753,39 @@ def f(x, y, *, z): ... # f(x=1, y=2, z=3) | f(1, y=2, z=3) | f(1, 2, z=
head, *body, tail = <coll.> # Head or tail can be omitted.
```
Walrus
------
**The Walrus Operator (:=) is a new addition to Python 3.8 and higher.
Walrus Operator allows you to assign a value to a variable within an expression.
This can be useful when you need to use a value multiple times in a loop, but don’t want to repeat the calculation.**
### Walrus Operator VS Equivalent assignment operation:
### Example:
**Walrus Operator:**
```python
data = [
{"userId": 1, "name": "Jack", "age": 20},
{"userId": 1, "name": "John", "age": 24}
]
for entry in sample_data:
if name := entry.get("name"):
print(f'Found name: "{name}"')
```
**Equivalent assignment operation without Walrus Operator:**
```python
data = [
{"userId": 1, "name": "Jack", "age": 20},
{"userId": 1, "name": "John", "age": 24}
]
for entry in sample_data:
name = entry.get("name")
if name:
print(f'Found name: "{name}"')
```
Inline
------

Loading…
Cancel
Save