01. Booleans and logical testing

Python comes in a variety of data types. Before we go over the more interesting data structures (like collections) let's take a brief tour of the primitive data types - booleans, numbers and strings. We'll start with the most basic type - Booleans.

Booleans

Booleans contain just two values - True and False. These values are case-sensitive, so don't forget to uppercase the first letters!

Booleans can be the result of expressions.

>>> 2 < 3
True
>>> 2 > 3
False
>>> 2 < 2 < 1
False

True and False are interchangable with 1 and 0. Additionally, the empty string "" is treated as False, while all other values are equivalent to True. It's best practice to use actual boolean types.

Boolean Operators

Booleans have operators not, and and or that can be used in conjunction with boolean data types.

>>> not True
False
>>> True and False
False
>>> False or True
True

Later on you'll see that booleans are used in conditional statements for flow control. For example, in an if statement, the block of code that becomes execute depends on the boolean value of the expression proceeds the if statement.

Learn to be a Pythonista!

Python Playground

Learn to be a Pythonista! Try Python

Python Playground is a collection of fun programming projects that will inspire you to new heights. You'll manipulate images, build simulations, and interact with hardware using Arduino & Raspberry Pi. With each project, you'll get familiarized with leveraging external libraries for specialized tasks, breaking problems into smaller, solvable pieces, and translating algorithms into code.

$ Check price
29.9529.95Amazon 4 logo(14+ reviews)

More Python resources

Aching back from coding all day?

Inversion Therapy Table

Aching back from coding all day? Try Back Problems

Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.

$$ Check price
119.98119.98Amazon 4.5 logo(1,700+ reviews)

More Back Problems resources

Ad