Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

Boolean values

To conclude with Python's literals, there are two additional ones. 

They're not as obvious as any of the previous ones, as they're used to represent a very abstract value - truthfulness.

Each time you ask Python if one number is greater than another, the question results in the creation of some specific data - a Boolean value. 

The name comes from George Boole (1815-1864), the author of the fundamental work, The Laws of Thought, which contains the definition of Boolean algebra - a part of algebra which makes use of only two distinct values: True and False, denoted as 1 and 0. 

A programmer writes a program, and the program asks questions. Python executes the program, and provides the answers. The program must be able to react according to the received answers. 

Fortunately, computers know only two kinds of answers: 

  • Yes, this is true
  • No, this is false

You'll never get a response like: I don't know or Probably yes, ut I don't know for sure.

Python, then, is a binary reptile.

These two Boolean values have strict denotiations in Python: 

True
False

You cannot change anything - you have to take these symbols as they are, including case-sensitivity. 

Challenge: What will the output of the following snippet of code? 

print(True > False)

print(True < False)

Run the code in the Sandbo to check. Can you explain the result? 

 

LAB

Estimated time

5-10 minutes

Level of difficulty

Easy

Objectives

  • becoming familiar with the print() function and its formatting capabilities
  • practicing coding strings
  • experimenting with Python code

Scenario

Write a one-line piece of code, using the print() function, as well as the newline and escape characters, to match the expected result outputted on three lines. 

swiftCopy code

print("I'm\n\"learning\"\n\"\"\"Python\"\"\"")

Output:

rustCopy code

I'm "learning" """Python"""

Explanation:

  • The newline character \n is used to break the output into three lines.
  • Double quotes " are used to enclose the strings "learning" and "Python". To include double quotes within a string, we need to escape them with a backslash \ to avoid interpreting them as the end of the string.
  • The single quote in "I'm" is not escaped because we used double quotes to enclose the entire string.