Walrus Operator in Python
Level 395 keystrokes
This uses the walrus operator := to assign and use a value in one expression. It assigns the length while checking the condition.
Python code
# A list with five numbers1numbers = [1, 2, 3, 4, 5]# := (walrus operator) assigns len(numbers) to n and also returns it# So the length is computed once, then compared: 5 > 3 is True2if (n := len(numbers)) > 3: # n is still available inside the block: prints "List has 5 items, more than 3"3 print(f"List has {n} items, more than 3")Output
List has 5 items, more than 3
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this Python code and how often they appear.
,× 5(× 3)× 3:× 2"× 2=× 2[× 1]× 1{× 1}× 1