What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
In Python 3.8 and later, use the assignment expression := in a while condition when the value you calculate or fetch is also needed inside the loop. For example, while chunk := file.read(9000): reads a chunk, assigns it to chunk, and continues while that result is truthy.
How assignment works in a while condition
The general form is while name := expression:. Python evaluates the expression, assigns its result to name, and uses that same result as the condition. The loop body runs when the result is true; it stops when the result is false. Python calls := an assignment expression, also known as a named expression or informally the walrus operator. Python 3.14.7 language reference: expressions
Unlike an ordinary assignment statement, an assignment expression produces a value. This lets the condition both save a result and test it without repeating the calculation or operation.
Read a file until it reaches the end
Python’s language reference uses this pattern to process a file in chunks:
Recommended Free Tools
#1 Best Overall
while chunk := file.read(9000):
process(chunk)
Before each iteration, file.read(9000) is called and its result is assigned to chunk. A non-empty chunk is truthy, so the body processes it. When the read returns an empty result at the end of the file, the condition is false and the loop ends. The number 9000 is the read size in this example, not a special requirement of assignment expressions. Python 3.14.7 language reference: expressions
Equivalent expanded form
You can write the same basic control flow without an assignment expression:
Rank #2
while True:
chunk = file.read(9000)
if not chunk:
break
process(chunk)
The compact version keeps the read and its termination check together. The expanded version can be easier to understand when the stopping logic has several branches, or when the loop needs additional cleanup or error handling. These are readability trade-offs, not different Python semantics.
Choose a condition that matches the data
The assignment-expression form uses the assigned value itself as the condition. Empty strings, empty containers, zero, and None are false in Python, so a bare condition is useful when one of those values means “stop.” If a falsey value is valid data, it will stop the loop too early; test the actual end condition instead.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Scan for outdated or missing drivers - takes under a minute3Clear out junk files and repair common Windows errorsFor example, if zero is valid and only None signals that there is no next value, make that test explicit:
while (value := get_next_value()) is not None:
process(value)
Parentheses make the assignment expression’s scope clear in this more complex condition. They are optional around a named expression in an if or while statement, but may help readability. Python 3.14.7 language reference: expressions
Common syntax and control-flow mistakes
- Using
=instead of:=. A regular assignment statement such aswhile chunk = file.read(9000):is not valid syntax. Use:=to assign and return a value in the condition. - Confusing assignment with comparison.
:=assigns a value;==compares two values. - Forgetting what makes the loop stop. A
whilecondition is checked before each iteration, and the body repeats while it is true. Ensure the operation can produce a false result or add an explicit stopping test. Python 3.11.17 language reference: compound statements - Misreading
while ... else. A loop’s optionalelsesuite runs when its condition becomes false; it does not run if the loop exits withbreak. Python 3.11.17 language reference: compound statements
Python version requirement
Assignment expressions were introduced in Python 3.8, so code using := requires Python 3.8 or later. The feature is described in PEP 572 and documented in the current language reference.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




