Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

Learning objectives

By the end of this section you should be able to

  • Explain a Boolean value.
  • Use bool variables to store Boolean values.
  • Demonstrate converting integers, floats, and strings to Booleans.
  • Demonstrate converting Booleans to integers, floats, and strings.
  • Use comparison operators to compare integers, floats, and strings.

bool data type

People often ask binary questions such as yes/no or true/false questions. Ex: Do you like pineapple on pizza? Ex: True or false: I like pineapple on pizza. The response is a Boolean value, meaning the value is either true or false. The bool data type, standing for Boolean, represents a binary value of either true or false. true and false are keywords, and capitalization is required.

Checkpoint

Example: Crosswalk sign

Concepts in Practice

Using Boolean variables

Consider the following code:

    is_fruit = "True"
    is_vegetable = 0
    is_dessert = False
    
1.
What is the data type of is_fruit?
  1. Boolean
  2. integer
  3. string
2.
What is the data type of is_vegetable?
  1. Boolean
  2. integer
  3. string
3.
What is the data type of is_dessert?
  1. Boolean
  2. integer
  3. string
4.
How many values can a Boolean variable represent?
  1. 2
  2. 4
  3. 8
5.
Which is a valid value for a Boolean variable?
  1. true
  2. True
  3. 1
6.
Suppose the following is added to the code above:
is_dessert = 0
print(type(is_dessert))
What is the output?
  1. <class 'bool'>
  2. <class 'int'>
  3. Error

Type conversion with bool()

Deciding whether a value is true or false is helpful when writing programs/statements based on decisions. Converting data types to Booleans can seem unintuitive at first. Ex: Is "ice cream" True? But the conversion is actually simple.

bool() converts a value to a Boolean value, True or False.

  • True: any non-zero number, any non-empty string
  • False: 0, empty string

Checkpoint

Converting integers, floats, and strings using bool()

Concepts in Practice

Converting numeric types and strings to Booleans

7.
bool(0.000)
  1. True
  2. False
8.
bool(-1)
  1. True
  2. False
9.
bool("")
  1. True
  2. False
10.
bool("0")
  1. True
  2. False
11.
Given input False, what is bool(input())?
  1. True
  2. False

Concepts in Practice

Converting Booleans to numeric types and strings

Given is_on = True, what is the value of each expression?

12.
float(is_on)
  1. 0.0
  2. 1.0
13.
str(is_on)
  1. "is_on"
  2. "True"
14.
int(is_on)
  1. 0
  2. 1

Comparison operators

Programmers often have to answer questions like "Is the current user the admin?" A programmer may want to compare a string variable, user, to the string, "admin". Comparison operators are used to compare values, and the result is either true or false. Ex: is_admin = (user == "admin"). user is compared with "admin" using the == operator, which tests for equality. The Boolean variable, is_admin, is assigned with the Boolean result.

The 6 comparison operators:

  • equal to: ==
  • not equal to: !=
  • greater than: >
  • less than: <
  • greater than or equal to: >=
  • less than or equal to: <=

Checkpoint

Example: Rolling a d20 in a tabletop game

Concepts in Practice

Comparing values

For each new variable, what is the value of compare_result?

15.
x = 14
compare_result = (x <= 13)
  1. True
  2. False
16.
w = 0
compare_result = (w != 0.4)
  1. True
  2. False
17.
v = 4
compare_result = (v < 4.0)
  1. True
  2. False
18.
y = 2
compare_result = (y > "ab")
  1. True
  2. False
  3. Error
19.
z = "cilantro"
compare_result = (z == "coriander")
  1. True
  2. False
20.
a = "dog"
compare_result = (a < "cat")
  1. True
  2. False

= vs ==

A common mistake is using = for comparison instead of ==. Ex: is_zero = num=0 will always assign is_zero and num with 0, regardless of num's original value. The = operator performs assignment and will modify the variable. The == operator performs comparison, does not modify the variable, and produces True or False.

Exploring further

Try It

Friday Boolean

"It's Friday, I'm in love" —from "Friday I'm in Love," a song released by the Cure in 1992.

Write a program that reads in the day of the week. Assign the Boolean variable, in_love, with the result of whether the day is Friday or not.

Try It

Even numbers

Write a program that reads in an integer and prints whether the integer is even or not. Remember, a number is even if the number is divisible by 2. To test this use number % 2 == 0. Ex: If the input is 6, the output is "6 is even: True".

Citation/Attribution

This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a print format, then you must include on every physical page the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
  • If you are redistributing all or part of this book in a digital format, then you must include on every digital page view the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
Citation information

© Mar 15, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.