Python basics
  • Tutorial
  • Exercices
  • About
  1. Fundamentals
  2. 6-Booleans
  • Introduction
    • Python
  • Fundamentals
    • 0-Basic Syntax
    • 1-Variables
    • 2-Data types
    • 3-Numbers
    • 4-Strings
    • 5-Lists
    • 6-Booleans
    • 7-Loops
    • 8-Functions
    • 9-Dictionary
  • Advanced
    • 10-Numpy
    • 11-Matplotlib
    • 12-DataFrame
  • CLI
    • Exercices
    • Solutions
  • Git
    • Git introduction
    • Git branch
    • Git exercices
  • SQL
    • SQL Exercices
    • SQL Solutions

On this page

  • Comparison
  • Evaluate values
  1. Fundamentals
  2. 6-Booleans

6-Booleans

Notebook

Link to the Google Colaboratory notebook: 6-Booleans

A boolean is either True or False.

Comparison

When you compare two values, it returns a boolean:

x,y = -1, 10
print(x == y)
print(x > y)
print(x < y)
print(x != y)
False
False
True
True

It is used a the basic for conditions.

x,y = -1, 10
if x<y:
    print("x less than y")
else: 
    print("x greater than y")
x less than y

Evaluate values

The bool variable allows you to evaluate any variable.

Most of the times, the output will be True.

x = 77
name = "Evry"
list_test = [0, 10, 4]
print(bool(x), bool(name), bool(list_test))
True True True
Note
  • Any str is True, except if it is empty.
  • Any number is True, except from 0.
  • Any list is True, except an empty list.
x = 0
name = ""
list_test = []
print(bool(x), bool(name), bool(list_test))
False False False
5-Lists
7-Loops
 

Copyright 2023, Clément Bernard