Learn Python, Microsoft 365 and Google Workspace
:=
) in PythonThe walrus operator (:=
) is an assignment expression introduced in Python 3.8. It allows you to assign a value to a variable as part of an expression, making your code more concise and readable.
variable := expression
Here, variable
is assigned the value of expression
, and the whole expression evaluates to the value.
if
ConditionInstead of:
value = len(my_list)
if value > 5:
print(f"List is long: {value}")
We can use:
my_list = [1, 2, 3, 4, 5, 6, 7]
if (value := len(my_list)) > 5:
print(f"List is long: {value}")
Why? It avoids writing len(my_list)
twice, making it more efficient.
while
LoopsInstead of:
data = input("Enter something: ")
while len(data) > 0:
print(f"You entered: {data}")
data = input("Enter something: ")
We can use:
while (data := input("Enter something: ")):
print(f"You entered: {data}")
Why? This avoids calling input()
twice, making the loop cleaner.
Instead of:
numbers = [10, 15, 20, 25, 30]
filtered = []
for num in numbers:
square = num ** 2
if square > 400:
filtered.append(square)
We can use:
numbers = [10, 15, 20, 25, 30]
filtered = [square for num in numbers if (square := num ** 2) > 400]
print(filtered) # Output: [625, 900]
Why? The walrus operator lets us compute the square once instead of repeating it.
match
Statements (Python 3.10+)def categorize(number):
match number:
case n if (n := number) > 50:
print(f"Large number: {n}")
case _:
print(f"Small number: {n}")
categorize(60) # Output: Large number: 60
Why? We assign n
inside case n if ...
, making the code cleaner.
import logging
if (msg := "Error occurred!") and is_critical:
logging.error(msg)
Why? The message is assigned and logged in one step.
if (response := fetch_data()).status_code == 200:
print(response.json())
Why? It avoids making multiple API calls.
with open("data.txt") as file:
while (line := file.readline().strip()):
print(line)
Why? It prevents extra file reads.
Write a Python program that takes user input and counts how many times a user enters something before typing “exit”.
Solution:
count = 0
while (user_input := input("Enter something (or 'exit' to stop): ")) != "exit":
count += 1
print(f"You entered {count} times before exiting.")
Given a list of words, find and print the first word that has more than 7 letters.
words = ["apple", "banana", "strawberry", "kiwi", "pineapple"]
if (long_word := next((word for word in words if len(word) > 7), None)):
print(f"First long word: {long_word}")
Why? This finds the first long word efficiently.
Write a program that keeps taking numbers from the user and sums them until the user enters a negative number.
total = 0
while (num := int(input("Enter a number: "))) >= 0:
total += num
print(f"Total sum: {total}")
Why? This makes the code more compact.
The walrus operator (:=
) improves readability and efficiency by combining assignment and expressions in a single step. However, use it wisely—overusing it can make code harder to read.