FREE ONLINE INTERACTIVE COURSE

Python 3

FOR BEGINNERS

LESSON 2
PAGE 2 / 4
Arithmetic operators
Home >>> Free online lessons

The division operator ( / )

The operator "/" has the meaning of division. The operands can be integer or real values, but the result will always be real.

Examples:
3 / 2 results 1.5
4 / 2 results 2.0

Careful. In the case of version 2, Python automatically rounds the whole part! Thus, some programs may get different results depending on the version of the interpreter.

Floor division ( // )

The operator // (known in other languages ​​as div) has the meaning of integer division (because the result of the division is rounded down to an integer) - thus the quotient is obtained.

Examples:
7 // 2, results 3
14 // 2, results 7
25 // 12, results 2

Careful. The result of the integer division in Python 3 is a real number only if one of the operands is also a real number.

Example: 10 // 3.0, results 3.0

The modulo operator ( % )

The % operator (known as modulo) returns the remainder of dividing the left hand operand by right hand operand.

Examples:
7 % 2, results 1
14 % 2, results 0
25 % 12, results 1

The exponentiation operator ( ** )

The exponentiation operator ( ** ) returns the result of raising the first operand to the power of the second operand.

Example:
2**3, results 8

About parentheses

Unlike mathematics, in Python we can only use round brackets. So instead of writing [2+3*(2+1)]*2, we will write (2+3*(2+1))*2.
See some examples on the next page.
 home   list  LESSONS   arrow_upward