Basic Arithmetic and Operator Precedence
You'll use arithmetic operators constantly in Python. This chapter focuses on the most common ones and how precedence determines evaluation order.
Main Operations
Addition (+
)
Addition is used to sum two or more numbers.
1print(5 + 3) # output: 8
Subtraction (-
)
Subtraction finds the difference between two numbers.
1print(10 - 4) # output: 6
Multiplication (*
)
Multiplication calculates the product of two numbers.
1print(6 * 7) # output: 42
Division (/
)
Division returns the quotient of two numbers.
1print(20 / 5) # output: 4.0
Exponentiation (**
)
Exponentiation is used to raise a number to the power of another number. In Python, this is done using the **
operator.
1print(2 ** 3) # output: 8
Operator Precedence
When multiple operators appear, Python evaluates them in this order (highest → lowest among arithmetic):
**
;- Unary
+
and-
(sign); *
,/
;+
,-
.
Parentheses always win and make intent explicit. Exponentiation **
is right-associative.
123456789print(2 + 3 * 4) # 14 (multiplication before addition) print((2 + 3) * 4) # 20 (parentheses change the order) # Exponentiation binds tighter than unary minus print(-3 ** 2) # -9 (equivalent to -(3 ** 2)) print((-3) ** 2) # 9 # Right-associative exponentiation print(2 ** 3 ** 2) # 512 (2 ** (3 ** 2))
- Prefer parentheses in anything nontrivial; readability > cleverness.
- Remember
/
always yields a float (even if divisible).
1. What value will this code output?
2. Which expression evaluates to 64
?
3. What value will this code output?
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
What are some examples of operator precedence in Python?
Can you explain right-associative exponentiation with more examples?
How do parentheses affect the order of operations?
Awesome!
Completion rate improved to 5
Basic Arithmetic and Operator Precedence
Sveip for å vise menyen
You'll use arithmetic operators constantly in Python. This chapter focuses on the most common ones and how precedence determines evaluation order.
Main Operations
Addition (+
)
Addition is used to sum two or more numbers.
1print(5 + 3) # output: 8
Subtraction (-
)
Subtraction finds the difference between two numbers.
1print(10 - 4) # output: 6
Multiplication (*
)
Multiplication calculates the product of two numbers.
1print(6 * 7) # output: 42
Division (/
)
Division returns the quotient of two numbers.
1print(20 / 5) # output: 4.0
Exponentiation (**
)
Exponentiation is used to raise a number to the power of another number. In Python, this is done using the **
operator.
1print(2 ** 3) # output: 8
Operator Precedence
When multiple operators appear, Python evaluates them in this order (highest → lowest among arithmetic):
**
;- Unary
+
and-
(sign); *
,/
;+
,-
.
Parentheses always win and make intent explicit. Exponentiation **
is right-associative.
123456789print(2 + 3 * 4) # 14 (multiplication before addition) print((2 + 3) * 4) # 20 (parentheses change the order) # Exponentiation binds tighter than unary minus print(-3 ** 2) # -9 (equivalent to -(3 ** 2)) print((-3) ** 2) # 9 # Right-associative exponentiation print(2 ** 3 ** 2) # 512 (2 ** (3 ** 2))
- Prefer parentheses in anything nontrivial; readability > cleverness.
- Remember
/
always yields a float (even if divisible).
1. What value will this code output?
2. Which expression evaluates to 64
?
3. What value will this code output?
Takk for tilbakemeldingene dine!