DEV Community

Sarah Bartley-Dye
Sarah Bartley-Dye

Posted on

Introduction to Python Module Two Part Five: Booleans and Operators

Today's post wraps up Module 2 of SoloLearn's Introduction to Python course. The final topics you need to know are operators and one more data type. This post will concentrate on booleans, comparison operators, and logical operators. You will learn how these concepts work in Python and how to use them in your code.

Booleans

The final data type you need to know are booleans. Booleans might sound like a fancy name, but in tech, it mean true or false. Booleans are used all the time in every programming language and are often used with operators.

Booleans can be stored in variables just like the other data types you have learned about in this module. Capitalization of true and false is important for booleans. When you use booleans, you need to capitalize the "t" in True" and the "f" in False.

while game_over == False:
   score_text.set_text("Score: " + str(score))
   stage.wait(0.05)
Enter fullscreen mode Exit fullscreen mode

This code sample has a while loop checking to see if the game_over variable is still set to False. If the condition is true, the loop will show the score of the game as a string. Then it will wait for a few seconds before repeating.

If the "f" in this while loop was lowercase, the computer would send me an error message in my code when the program runs. So before you run your code, always double-check if you capitalized your booleans.

Comparison Operators

Comparison operators are often paired with booleans because they are good for yes or no kinds of questions. When you use a comparison operator in your code, a boolean is often the result. Developers like to use them in their code to help the computer make decisions in a quick, precise manner.

Comparison operators are used everywhere. SoloLearn says you can find them in video streaming and online banking transactions. If you looked closer at the code used in these tasks, comparison operators are used in simple, tiny calculations.

The comparison operators you will use are <, >, >=, and <=. This code sample prints the results of different comparison operators.

print(5 > 10)  # prints False
print(4 <  6) # prints True
print(3 >= 9) # prints False
print(1 <= 7) # prints True
Enter fullscreen mode Exit fullscreen mode

Booleans aren't the only ones that work well with comparison operators. Developers often will use variables to store the results of comparison operators to use throughout their code.

temperature = 70
print(temperature > 80) # false
Enter fullscreen mode Exit fullscreen mode

This code sample creates a temperature value with an integer assigned to it. When the computer prints the second line, it will use the temperature variable to check if the value inside is greater than 80. The computer will print either true or false.

Comparison operators are examples of binary code.

SoloLearn defines binary code as the two ways the state of a switch might look. A switch inside a computer works the way a light switch might work in a room. A light switch in a room has two options (on and off).

A switch inside the computer works the same way, but there are some key differences. First, a computer has multiple switches inside. These switches use numbers like 0 and 1 instead of words.

However, like real-life light switches, they give the computer the power to do lots of tasks as quickly as possible. When the computer needs to perform a specific action, it turns different switches on and off, changing the information being stored. That means when information changes, the status of those switches changes too.

You can see binary code in real life with digital telescopes. Digital telescopes use binary code to help them store and process information.

Logical Operators

Another type of operator you will use often in your code is the logical operator. Computers need to do complex tasks quickly. Logical operators can evaluate tougher situations better than comparison operators.

Logical operators are best suited for these types of situations because they break down these tasks into small, simple calculations. These kinds of operators take several boolean inputs and result in one Boolean output. Logical operators work best when inserted in a print function to output the results.

The result of logical operators will also be a boolean, just like comparison operators. Another perk of logical operators is that there can be multiple boolean values as an input. If you need to save these results, they can be stored in variables.

You will use two logical operators in your code: "and" and "or". At Coding with Kids, the Level M students use logical operators in their conditional statements when they make their games. Let's look at each operator.

The "and" operator will result in true if both conditions are true simultaneously. If one or both conditions are false, the result will be false.

weather = "snow"
temp = 32

if (weather == "snow" and temp < 40):
   print("Baby it is cold outside. Better bundle up"
else:
   print("It is hot outside. Time to go to the pool!"
Enter fullscreen mode Exit fullscreen mode

This code sample has two variables with values assigned to them. I have an if-else statement. The condition for the if statement uses the logical operator "and" to check if the weather is "snow" and if the temperature is greater than 40 degrees.

If both conditions are true, it will print a statement to the console. If one condition or both conditions are false, it will print a different statement instead. In this example, the condition is true, so it will run the first statement, "Baby it is cold outside. Better bundle up."

weather = "snow"
temp = 32

if (weather == "snow" or temp < 40):
   print("Baby it is cold outside. Better bundle up"
else:
   print("It is hot outside. Time to go to the pool!"
Enter fullscreen mode Exit fullscreen mode

This is the same code as the first example but I replaced the "and" operator with the "or" operator. The result of this code will still be true and print the first statement. The difference it is when this code runs, it just needed one condition out of the two to be true.

Combining comparison and logical operators

Now that you know both types of operators, you can use both of them at the same time in your code. You won't always get projects that just use one or the other. Many times, developers need to combine both together so the app or program they are making will work efficiently.

Variables are your best friend when you are combining operators. When computers read code, any operations that are wrapped in parentheses are done first.

lights_on = True
door_locked = False

print(lights_on or door_locked) # This will be true
print(lights_on and door_locked) # This will be false
Enter fullscreen mode Exit fullscreen mode

This code sample has two variables assigned to two different booleans. When I print each statement, I use the variables between different logical operators in the parentheses. When you run this program, the code will look inside the parentheses first to see anything inside the parentheses first.

Remember computers need very clear, specific instructions to get a program or game to work. That prevents errors and makes code easy to read.

Conclusion

You have finally reached the end of module two of SoloLearn's Introduction to Python course. You now know all 4 data types and 2 types of operators. You also know some tips for debugging your Python code and a few best practices to use as you continue to work in Python.

Up next is module three. Control Flow is the theme for this module. This module introduces new concepts, including loops and conditionals.

Top comments (0)