Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Boolean Data Type in Python | Conditional Statements in Python
Introduction to Python(ihor)
course content

Course Content

Introduction to Python(ihor)

Introduction to Python(ihor)

1. First Acquaintance with Python
2. Variables and Types in Python
3. Conditional Statements in Python
4. Other Data Types in Python
5. Loops in Python
6. Functions in Python

book
Boolean Data Type in Python

Python has the boolean (or logical) data type, which can have only two values True or False. It is primarily used for evaluating logical conditions. Below are the logical operators for comparison, which return a boolean value True if the condition is met and False if not.

123456789
a = 10 b = 20 print("a == b:", a == b) # Equal to print("a != b:", a != b) # Not equal to print("a > b:", a > b) # Greater than print("a < b:", a < b) # Less than print("a >= b:", a >= b) # Greater than or equal to print("a <= b:", a <= b) # Less than or equal to
copy

String comparison is more complex than it seems. The expression below compares two characters, and the first thought might be that it will result in True because alphabetically, 'A' comes first.

12
# Comparing two characters print('A' > 'B')
copy

When comparing characters like 'A' and 'B', you might wonder why 'A' > 'B' evaluates to False. This is because characters in Python are compared based on their Unicode values. Unicode is a standardized character encoding that assigns a unique number to each character, regardless of platform, program, or language.

To check the Unicode value of any character, you can use the ord() function. This function returns the Unicode code point of a given character.

123
# The `ord` returns the number representing the character's unicode code print(ord('A')) print(ord('B'))
copy

Since 65 is less than 66, the expression evaluates to False. Python compares strings character by character from left to right and stops as soon as it finds a difference.

question mark

What will be the output of the following code snippet?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 1
We're sorry to hear that something went wrong. What happened?
some-alt