From bedc8f6cc1cb94768fcea8b757b84253aa337895 Mon Sep 17 00:00:00 2001 From: Jogesh Ghadai Date: Thu, 13 Mar 2025 20:30:41 +0530 Subject: [PATCH] Added Walrus Operator Added Walrus Operator --- README.md | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dc2e84d..8e54782 100644 --- a/README.md +++ b/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 = # 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 ------