Python Tutorials

Overview

Booleans represent one of two values: True or False. In Python you can evaluate any expression to determine whether it is True or False.

print(3 > 1) #returns True
print(3 == 1) #returns False
print(3 < 1) #returns False
a = 3
b = 1

if b > a:
    print("b is greater than a.")
else:
    print("b is not greater than a.")

Output

b is not greater than a.

Most Values will be True

Almost any value is evaluated to True if it has some sort of content.

  • Any string is True, except empty strings
  • Any number is True, except 0
  • Any list, tuple, set, and dictionary are True, except empty ones

All of the following will return True.

bool("xyz")
bool(123)
bool(["Ferrari", "Maserati", "Alfa Romeo"])

Some Value will Always be False

There are not many values that evaluate to False, except empty values, such as (), [], {}, “”, the number 0, and the value None or False.

All of the following will return False.

bool(False)
bool(None)
bool(0)
bool("")
bool(())
bool([])
bool({})

Python Notes:

  • The most recent major version of Python is Python 3; however, Python 2 is still in use and quite popular, although not being updated with anything other than security updates
  • Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses
  • Python relies on indentation, using whitespace to define scope, such as the scope of loops, functions, and classes; other programming languages often use curly-brackets for this purpose
  • Python string methods return new values, and DO NOT change the original string

We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.