Infinite Loops
In the next part of this course, we are going to use a "pyBox," which is a built‑in Python environment where you can practice writing Python code.
The "pyBox" will work in the same way your Python programs run in VS Code or another environment; we just have the convenience of using the same page as the lesson. The drawback is that the program is never saved, so it is only used for practice. Continue to use your Python‑lesson files in VS Code for the exercises and programs for this course.
An Infinite Loop is a loop that never ends. In Python, this usually happens when the loop’s condition is always True or when there’s no code inside the loop that changes the condition.
# Example:
while True:
print("This will run forever!") # python requires an indentation, use 4 spaces to indent this line
- The condition True never becomes False, so the loop keeps running.
- In your PyBox panel, this will cause the browser tab to freeze or become unresponsive.
- Recovery: Students can refresh the page or close/reopen the tab. This clears the stuck code and resets the editor.
Exercise: Try an Infinite Loop
Instructions for students:
- In the Python box below, write a loop that never ends:
- Run the code. Notice how your browser becomes unresponsive.
- Recover by refreshing the page (or closing and reopening the tab).
- After recovery, try writing a loop that does end, for example:
# Example:
while True:
print("Looping forever...") # python requires an indentation, use 4 spaces to indent this line
count = 0
while count < 5:
print("Loop number:", count) # python requires an indentation, use 4 spaces to indent this line
count += 1 # python requires an indentation, use 4 spaces to indent this line
pyBox
Try an Infinite Loop
What is Operator Precedence?
You may have learned the term BEDMAS or PEMDAS in math class: Brackets, Exponents, Division, Multiplication, Addition, and Subtraction. This acronym represents the order in which math problems must be solved.
In Python, we use the term Operator Precedence to describe this same concept. While the logic is nearly identical to what you learned in school, the references and symbols we use in code are slightly different:
- Parentheses () replace Brackets.
- Exponents are represented by **.
- Multiplication and Division (*, /) are handled with equal importance from left to right.
- Addition and Subtraction (+, -) are also handled from left to right.
The most important thing to remember is that Python is extremely literal. If you aren't careful with your parentheses, Python might calculate your equation in a different order than you intended!
example: 3 + 2 * 5 / 4 (3 + 2) = ?
If you understand the logic behind BEDMAS, you already understand how Python handles math. While the name sounds technical, Operator Precedence is simply the set of rules that tells Python which part of a math problem to solve first.
The most important thing to realize is that Multiplication and Division are interchangeable, as are Addition and Subtraction. They share the same "rank" or priority level. When you see both in the same expression, Python doesn't pick one based on the acronym; instead, it simply solves the problem by doing whatever comes first from left to right.
By following this left-to-right rule, Python ensures your code is calculated consistently every time. If you ever want to break this natural flow and force an operation to happen first, you can simply wrap it in Parentheses (), which always take the highest priority.
# example: 3 + 2 * 5 / 4 (3 + 2) = ?
print(3 + 2 * 5 / 4 (3 + 2))
print(3 + 2 * 5 / 4 * (3 + 2))
TypeError: 'int' object is not callable
15.5
pyBox
Try: (3 + 2 * 5 / 4 (3 + 2))
The reason we get an error in the previous example is that Python is more literal than a standard BEDMAS equation. In traditional math, placing a number directly next to a bracket—like 4(3+2)—automatically indicates multiplication.
However, in Python, there is no such thing as "invisible" math. You must explicitly use the * operator to tell the computer exactly what you want it to do. If you leave it out, Python won't assume you want to multiply; instead, it will get confused and return a SyntaxError "Syntax Error: A mistake in the structure of your code that prevents Python from running it." .
Always remember: If you want to multiply, you must use the asterisk (*), even when using parentheses!
pyBox
Operator precedence follows the PEMA acronym: Parentheses, Exponents, Multiplication and Division, and Addition and Subtraction.
Multiplication and division share equal priority, as do addition and subtraction.
Write the code for each equation and the use a comment to guess the correct answer before running the program. eg; 2 + 4 # 6
- 12 - 4 + 2
- 24 / 4 * 3
- 10 + 6 * 2
- 15 / (3 + 2) * 4
Test Your Knowledge
Select the correct answer below to see if you've mastered Python Operator Precedence.
Loading quiz...