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)

bookBoolean 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

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Ask me questions about this topic

Summarize this chapter

Show real-world examples

Awesome!

Completion rate improved to 1.67

bookBoolean Data Type in Python

Swipe to show menu

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
some-alt