Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen 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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 2

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Awesome!

Completion rate improved to 5

bookBasic Arithmetic and Operator Precedence

Swipe um das Menü anzuzeigen

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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 2
some-alt