Arvutiteaduse instituut
  1. Kursused
  2. 2025/26 sügis
  3. Programmeerimise alused (MTAT.03.236)
EN
Logi sisse

Programmeerimise alused 2025/26 sügis

  • Home Page
  • Introduction
  • Expressions
  • Conditional Execution
  • Functions
  • Iterations
  • Strings
  • Files
  • Lists
  • Graphics?
< eelmineTable of Contentsnext >

3.1 BOOLEAN AND COMPARISON OPERATORS

The Boolean data, type represented as bool, is a built-in data type that Python offers for handling logical values. It can take two possible values: True or False. Booleans represent the truth values in the context of comparison operations, such as comparing whether one number is larger than another. They are primarily used in conditional statements and loops to control the flow of a program based on certain conditions.

COMPARISON OPERATORS

Boolean comparison operators are fundamental tools that allow you to compare values and evaluate conditions, leading to Boolean outcomes: True or False. These operators are the building blocks of decision-making in your code. Here’s an overview of all the comparison operators in Python.

Note that we can compare type int with type float as Python will handle the type conversion automatically. However, we cannot compare strings to numeric types. We will cover string comparison in more detail later in the course.

EQUAL TO (==)

Checks if the values on both sides of the operator are equal. If they are, the result is True; otherwise, it's False.


print(5 == 5)  # Output: True
print(3 == 5)  # Output: False
print(4.5 == 5) # Output: False

NOT EQUAL TO (!=)

Checks if the values on both sides of the operator are not equal. If they aren't, the result is True; if they are, it's False


print(10 != 10)  # Output: False
print(7 != 5)  # Output: True
print(10.5 != 10) # Output: True

GREATER THAN (>)

Check if the value on the left side of the operator is greater than the value on the right side. If it is, the result is True; otherwise, it's False.


print(5 > 3)  # Output: True
print(3 > 5)  # Output: False
print(11.25 > 25) # Output: False

LESS THAN (<)

Check if the value on the left side of the operator is less than the value on the right side. If it is, the result is True; otherwise, it's False.


print(5 < 3)  # Output: False
print(3 < 5)  # Output: True
print(15.5 < 30.56) # Output: True

GREATER THAN OR EQUAL TO (>=)

Check if the value on the left is greater than or equal to that on the right. If it is, the result is True; otherwise, it's False.


print(5 >= 5)  # Output: True
print(3 >= 5)  # Output: False
print(30.5 >= 30.1) # Output: False

LESS THAN OR EQUAL TO (<=)

Check if the value on the left is less than or equal to that on the right. If it is, the result is True; otherwise, it's False.


print(5 <= 5)  # Output: True
print(16 <= 10)  # Output: False
print(45.8 <= 50.2) # Output: True

KEY THINGS TO REMEMBER

  • Boolean data type has two values: True and False.
  • Equal to == returns True if both sides are equal and otherwise False
  • Not Equal to != returns True if both sides are not equal and otherwise False
  • Greater than > returns True if the left side is greater than the right side, otherwise False
  • Less than < returns True if the left side is less than the right side, otherwise False
  • Greater than or equal to >= returns True if the left side is greater than or equal to the right side, otherwise False
  • Less than or equal to <= returns True if the left side is less than or equal to the right side, otherwise False

SELF-CONTROL EXERCISES

< eelmineTable of Contentsnext >

  • Arvutiteaduse instituut
  • Loodus- ja täppisteaduste valdkond
  • Tartu Ülikool
Tehniliste probleemide või küsimuste korral kirjuta:

Kursuse sisu ja korralduslike küsimustega pöörduge kursuse korraldajate poole.
Õppematerjalide varalised autoriõigused kuuluvad Tartu Ülikoolile. Õppematerjalide kasutamine on lubatud autoriõiguse seaduses ettenähtud teose vaba kasutamise eesmärkidel ja tingimustel. Õppematerjalide kasutamisel on kasutaja kohustatud viitama õppematerjalide autorile.
Õppematerjalide kasutamine muudel eesmärkidel on lubatud ainult Tartu Ülikooli eelneval kirjalikul nõusolekul.
Courses’i keskkonna kasutustingimused