Course Content
Introduction to Python(ihor)
Introduction to Python(ihor)
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.
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
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.
# Comparing two characters print('A' > 'B')
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.
# The `ord` returns the number representing the character's unicode code print(ord('A')) print(ord('B'))
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.
Thanks for your feedback!