Booleans – der logische Datentyp

Datentypen in Python

Jason Myers

Instructor

Booleans als Datentyp

  • True
  • False

Achte auf die Großschreibung – das kann beim Wechsel zwischen Python und anderen Sprachen verwirren.

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

Truthy und Falsey

  • Truthy-Werte ergeben true
  • Falsey-Werte ergeben false
apples=2
if apples:
     print("We have apples.")
"We have apples."
apples=0
if apple:
     print('We have apples.')
Datentypen in Python

Truthy und Falsey

Truthy

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

Falsey

  • 0
  • ""
  • []
  • {}
  • None
Datentypen in Python

Operatoren – boolescher Auswertungskontext

cookie_qty == 3
  • == gleich
  • != ungleich
  • < kleiner als
  • <= kleiner/gleich
  • > größer als
  • >= größer/gleich
Datentypen in Python

Floats sind nur näherungsweise genau

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

Vorsicht bei Gleichheitsvergleichen von Floats!

Datentypen in Python

Lass uns üben!

Datentypen in Python

Preparing Video For Download...