Relational, Logical and Assignment Operators

40 min read Operators and Expressions

This is a free course. Log in to run the code exercises and track your progress.

Relational, Logical and Assignment Operators

Relational operators compare two values and give True or False: == (equal to), != (not equal to), >, <, >= and <=. Do not confuse == (comparison) with = (assignment).

Logical operators combine conditions:

  • and is True only when both sides are True.
  • or is True when at least one side is True.
  • not reverses a value: not True is False.
marks = 72
print(marks >= 33)                  # True
print(marks >= 33 and marks < 75)  # True: passed, but no distinction
print(not marks == 72)              # False

Assignment operators store a value. The shortcut forms update a variable: count += 1 means count = count + 1; -=, *= and /= work the same way.

Python exercise

Read a student's marks (out of 100). Print True on the first line if the student passed (33 or more), and on the second line True if the marks are a distinction (75 or more), else False.

marks = int(input())
print(marks > 100)  # replace with the pass condition
print(marks > 100)  # replace with the distinction condition

Log in to edit and run this code.