Skip to content

Python Operators

You will use Python operators to calculate, compare values, and combine conditions.

10 min read 10 quiz questions

Arithmetic operators

OperatorMeaningExample
+Addition2 + 3 gives 5
-Subtraction7 - 2 gives 5
*Multiplication4 * 3 gives 12
/Division7 / 2 gives 3.5
//Floor division7 // 2 gives 3
%Remainder7 % 2 gives 1
**Power2 ** 3 gives 8
Arithmetic operators can split a total and calculate powers.
total = 17
people = 4
print(total // people)
print(total % people)
print(2 ** 5)

17 // 4 keeps only the whole-number result, so it prints 4. 17 % 4 prints the leftover 1. Finally, 2 ** 5 means 2 to the fifth power, which is 32.

Assignment and comparison

OperatorMeaningExample
=Assign a valuescore = 10
+=Add, then assignscore += 2
-=Subtract, then assignscore -= 2
*=Multiply, then assignscore *= 2
/=Divide, then assignscore /= 2
OperatorMeaningExample
==Equal to5 == 5
!=Not equal to5 != 3
>Greater than5 > 3
<Less than3 < 5
>=Greater than or equal to5 >= 5
<=Less than or equal to3 <= 5
Assignment updates a variable, and comparison operators produce Boolean values.
score = 8
score += 4
print(score)
print(score >= 10)
print(score == 12)

+= adds 4 to the current score, so the first line is 12. The next two comparisons ask whether 12 meets each condition. Both answers are True.

Logical membership and identity

OperatorMeaningExample
andTrue when both conditions are trueage >= 18 and has_ticket
orTrue when either condition is trueis_weekend or is_holiday
notReverses a Boolean valuenot is_closed
OperatorMeaningExample
inValue appears in a collection'a' in 'cat'
not inValue does not appear in a collection4 not in [1, 2, 3]
OperatorMeaningExample
isChecks whether two names refer to the same objectitem is None
is notChecks whether two names do not refer to the same objectitem is not None
Logical, membership, and identity operators help you test values.
age = 20
has_ticket = True
word = 'python'
value = None
print(age >= 18 and has_ticket)
print('py' in word)
print(value is None)
print(2 + 3 * 4)

The first three tests are true: the age and ticket conditions both pass, 'py' appears in the word, and the value is exactly None. Python follows precedence rules: multiplication happens before addition, so 2 + 3 * 4 is 14. Use parentheses when you want to make the order clear.

Key points

  • Use // for whole-number division and % for a remainder.
  • Compound assignment updates a variable in place.
  • Comparisons produce True or False.
  • Use in for membership and is mainly for None.
  • Parentheses make evaluation order clear.

Worked example

Four friends share a 1250-taka dinner. Floor division gives everyone's share; modulo tells what is left over for the tip bowl.

Example
bill = 1250
friends = 4
share = bill // friends
leftover = bill % friends
print(share, leftover)

How it works, step by step

  1. // divides and throws away the fraction: 1250 // 4 = 312.
  2. % keeps only the remainder: 1250 % 4 = 2 taka.
  3. Together they describe a complete division.

References & Further Reading

Related entries from PyLabs's own reference library.

  • pow() (Raise a number to a power.)
  • abs() (Returns the absolute (non-negative) value of a number or magnitude of a complex number.)
  • round() (Round a number to a chosen number of digits.)

Your notes

Sign in to keep private notes alongside this lesson.

Sign in to take notes

Practice exercise

Not passed yet

Set apples to 23 and boxes to 5. Print how many whole apples go in each box, then print the number left over.

Expected output:

4
3

# Write your code below
Want another challenge on this module? More coding practice

Check yourself

Python Operators quiz

10 questions. You get instant feedback per question, and the timer starts when you press the button.

Lesson incomplete

Not completed

Create an ID or sign in to save lesson completion across devices.

Create ID to save
Module lesson progress0/5 complete
Module cheat sheet