Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Basic Arithmetic and Operator Precedence | Numbers
Data Types in Python

bookBasic 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.

1
print(5 + 3) # output: 8
copy

Subtraction (-)

Subtraction finds the difference between two numbers.

1
print(10 - 4) # output: 6
copy

Multiplication (*)

Multiplication calculates the product of two numbers.

1
print(6 * 7) # output: 42
copy

Division (/)

Division returns the quotient of two numbers.

1
print(20 / 5) # output: 4.0
copy

Exponentiation (**)

Exponentiation is used to raise a number to the power of another number. In Python, this is done using the ** operator.

1
print(2 ** 3) # output: 8
copy

Operator Precedence

When multiple operators appear, Python evaluates them in this order (highest → lowest among arithmetic):

  1. **;
  2. Unary + and - (sign);
  3. *, /;
  4. +, -.

Parentheses always win and make intent explicit. Exponentiation ** is right-associative.

123456789
print(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))
copy
Note
Note
  • 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?

question mark

What value will this code output?

Select the correct answer

question mark

Which expression evaluates to 64?

Select the correct answer

question mark

What value will this code output?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 2

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Suggested prompts:

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

bookBasic Arithmetic and Operator Precedence

Pyyhkäise näyttääksesi valikon

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.

1
print(5 + 3) # output: 8
copy

Subtraction (-)

Subtraction finds the difference between two numbers.

1
print(10 - 4) # output: 6
copy

Multiplication (*)

Multiplication calculates the product of two numbers.

1
print(6 * 7) # output: 42
copy

Division (/)

Division returns the quotient of two numbers.

1
print(20 / 5) # output: 4.0
copy

Exponentiation (**)

Exponentiation is used to raise a number to the power of another number. In Python, this is done using the ** operator.

1
print(2 ** 3) # output: 8
copy

Operator Precedence

When multiple operators appear, Python evaluates them in this order (highest → lowest among arithmetic):

  1. **;
  2. Unary + and - (sign);
  3. *, /;
  4. +, -.

Parentheses always win and make intent explicit. Exponentiation ** is right-associative.

123456789
print(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))
copy
Note
Note
  • 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?

question mark

What value will this code output?

Select the correct answer

question mark

Which expression evaluates to 64?

Select the correct answer

question mark

What value will this code output?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 2
some-alt