Booleans - the logical data type

Data Types in Python

Jason Myers

Instructor

Booleans as a data type

  • True
  • False

Notice the capitalization as this can trip you up when switching between Python and other languages.

out_of_cookies = True
if out_of_cookies:
    print("Run to the store NOW!")
Run to the store NOW!
Data Types in Python

Truthy and Falsey

  • Truthy values are ones that will return true
  • Falsey values will evaluate to false
apples=2
if apples:
     print("We have apples.")
"We have apples."
apples=0
if apple:
     print('We have apples.')
Data Types in Python

Truthy and Falsey

Truthy

  • 1
  • "Cookies"
  • ["Cake", "Pie"]
  • {"key": "value"}

Falsey

  • 0
  • ""
  • []
  • {}
  • None
Data Types in Python

Operators - a boolean evaluation context

cookie_qty == 3
  • == equal to
  • != not equal to
  • < less than
  • <= less than or equal to
  • > greater than
  • >= greater than or equal to
Data Types in Python

Floats are approximately an issue

x = 0.1 + 1.1
x == 1.2
False
print(x)
1.2000000000000002

Be careful with equality comparisons of floats!

Data Types in Python

Let's practice!

Data Types in Python

Preparing Video For Download...