Python Boolean Values and Comparisons
Core idea
Boolean values (bool) represent two logical outcomes: True and False. Comparisons between ordinary numbers or strings produce these values. Use == for equality and != for inequality; = assigns a value to a name. Boolean literals have capitalized first letters.
stock = 12print(stock == 12)print(stock != 12)print(stock > 5)The stock equals 12 and exceeds 5, so the first and third comparisons are true.
Check boundaries
temperature = 20print(temperature < 20)print(temperature <= 20)print(temperature >= 20)The operators < and > exclude equality; <= and >= include it. At exactly 20, both inclusive comparisons are true.
Compare labels
label = "Tea"print(label == "tea")print(label == "Tea")print(3 == 3.0)String equality is case-sensitive. Numeric equality compares values: integer 3 and float 3.0 compare equal.
Common mistake
"False" is a string containing five characters, not the Boolean False. Also, stock = 12 changes the binding; stock == 12 asks a question.
Practice
With stock = 8, predict and print stock >= 8, stock > 8 and stock != 0. Explain which result changes when stock becomes 9.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Boolean values and comparisons
Check your understanding of boolean values.
What is printed?
print(12 == 12)What is printed?
print(4 < 4)Which expression checks equality without assigning a value?
What is printed?
print(3 == 3.0)