Booleans

Boolean's are useful for storing data and also for creating flags in a program. Flags indicate a state, for example a Boolean variable called item_found = False, will remain in this state until the program has found the item it has been looking for. Using concepts like this carefully allows you to create code that is easy to follow and maintain.

Boolean Logic Operators

Boolean's can be compared with the "and", "or" and "not" statements. "and" compares variables and will return true if all components are true, otherwise false. The "or" statement returns true is one of the items is true, otherwise it returns false. The "not" statement inverts a Boolean value. These are important when controlling the flow of a program.
Var1 = True
Var2 = False

print (Var1 or Var2)
print (Var1 or Var1)
print (Var2 or Var2)

print (Var1 and Var2)
print (Var1 and Var1)
print (Var2 and Var2)

print (not (Var1 or Var2))
Returns:
True
True
False
False
True
False
False



Related Sections

Python - Learn the python from the basics up. This fast track example code course will get you creating powerful python programs in no time.
Tkinter - Learn the key features of the tkinter to allow you to create user interfaces for your python programs.


Related Pages

Pickle - The pickle library allows a program to save data in a binary file.
Python - List Comprehension - Create powerful list comprehension expressions.
Python - Generators - Learn about generator statements.
Python - Data types - The learn about the key datatypes in the python language.
Tkinter - Combo Box - This is a short example showing how to use combo boxes in a GUI.
Tkinter - Radio Buttons - A short example of how to create a GUI with radio buttons.
Tkinter - Canvas - Canvas allows for a range of shapes to be plotted, such as circles, squares, lines