python tutorial

Python Boolean

Learn more about Python, one of the world’s most versatile and popular programming languages.

A Python boolean is another data type that can be used to store a value in a variable.


Python Boolean Type

One last data type we will discover as part of this guide is called “boolean”. Booleans are a data type that contains a value of True or False. It is heavily used in programming when storing information based on logical decisions. A boolean value may also be the outcome of a logical operation that results in either true or false. Before we learn about booleans, let’s learn about some special keywords in Python that help in making logical decisions within code. These keywords are notand, & or.

  • and is a reserved keyword in Python that returns True when both the values on either side of and are true. If either of those values are False, the outcome of the expression will be false.
  • or is a reserved keyword in Python that returns True when one or both the values on either side of or is true. If both of those values are False, the outcome of the expression will be false.
  • not not simply converts a True value into False, and a False value into True.

Now, let’s see how we can leverage these concepts. Type the following code line-by-line and then we will explore how it works.

Python
value_a = True
value_b = False
value_c = True
value_d = False
print(value_a and value_b) # True and False will print False
print(value_a and value_c) # True and True will print True
print(value_a or value_d) # True or False will print True
print(value_b or value_d) # False or False will print False
print(not value_d) # not False will print True
Output
False
True
True
False
True

Let’s consider a scenario where a student can only graduate if they meet the following conditions:

  • They have scored above 70% in their grades
  • They have above 90% attendance
  • They have not plagiarized even once during the course.

We can design and use this scenario in code as follows:

Python
grades = 85
attendance = 95
has_plagiarized = False
print(not has_plagiarized)

successfully_graduated = (grades > 70) and (attendance > 90) and (not has_plagiarized)
print(successfully_graduated)
Output
True
True

To close off this section, we’ve learned about some of the most frequently used built-in data types in Python. The language offers much more, along with widely useful data types that developers use in their code.

Learn Python Today

Get hands-on experience writing code with interactive tutorials in our free online learning platform.

  • Free and fun
  • Designed for beginners
  • No downloads or setup required