# 🌟 New Python Features

newlatestwalrusoperatorfeatures
The Walrus Operator

:= assigns values to variables as part of a larger expression

# Handle a matched regex
if (match := pattern.search(data)) is not None:
    # Do something with match

# A loop that can't be trivially rewritten using 2-arg iter()
while chunk := file.read(8192):
   process(chunk)

Positional and keyword Parameters

def f(a, b, /, c, d, *, e, f):

def f(pos1, pos2, /, pos_or_kwd, *, kwd1, kwd2):
-----------    ----------     ----------
    |             |                  |
    |        Positional or keyword   |
    |                                - Keyword only
    -- Positional only
Parameter | Argument

Parameter is variable in the declaration of function.

Argument is the actual value of this variable that gets passed to function.
public void MyMethod(string myParam) { }

string myArg1 = "this is my argument";
myClass.MyMethod(myArg1);
Dictionary Merging operator

dict3 = dict1 | dict2 or inplace using dict1 |= dict2

  • remove prefix and suffix from strings