voidex/20-29 Areas/20 Programming/20.01 Python/operatorPrecedence.md

32 lines
741 B
Markdown

---
title: operatorPrecedence
tags:
- python
- syntax
- operators
- math
aliases:
- math order
- expression order
- operator rules
---
> [!important] Operator Precedence
> Python follows standard math precedence:
> 1. `**` (exponentiation)
> 2. `*`, `/`, `//`, `%` (multiplication/division)
> 3. `+`, `-` (addition/subtraction)
> Operations are evaluated **left to right** at each level.
> Use parentheses `()` to override precedence and clarify expressions.
>
> > [!example] Precedence in Practice
> > ```python
> > 2 + 3 * 4 # 14
> > (2 + 3) * 4 # 20
> > ```
>
> > [!info] Related Notes
> > [[arithmeticOperators]]
> > [[expressions]]
> > [[mixedTypeArithmetic]]
> > [[pythonSyntaxOverview]]