The Art of Evaluation — Python Conditional Statements

Timothy Rudenko
3 min readSep 14, 2022

--

Conditional logic is one of the most important aspects of programming. As a beginner, it’s crucial to master the art of evaluation. Testing certain conditions to change your program’s flow is something you will encounter every day as a software engineer.

How Conditional Statements Work

The purpose of conditional statements is to determine whether a question we ask in Python is True or False. Conditionals give us the benefit of capturing a variable and interrogating it to see if it meets the requirements we need.

We can use conditional statements to have our program take a different approach to solve a problem. If the variable is one value, I want my program to do something. If the variable is a different value, I want my program to do something entirely different. The way that a program runs based on certain conditions is called Control Flow.

How to Write a Conditional Statement

Conditional statements follow a specific structure in programming. The statement starts with the word if with the actual condition following right after it.

How the condition itself is structured is a little more in the weeds. It all depends on the question we want to ask. We use different operators to do this.

Let’s pretend you’re writing a program for a bar that wants to ensure they’re meeting the age requirement for entry. We can solve this problem with a simple conditional statement.

We can tell Python to evaluate if the current year minus the person’s birth year is less than 21. If they are younger, we output “No entry.” If they are older, we output “Entry.” To make this program useful, we would have to add months and days to get the exact age, but that’s outside the scope of this lesson.

The else statement is thought of as “anything else.” In the context of our program, we only have two choices, the customer is either above or below 21 years of age.

Combining Conditionals

In addition to having a conditional statement, we can chain together more complex conditions if we want to be more specific.

With an and keyword in the middle, we are able to check more than one condition on a variable. We have to meet BOTH requirements in order to qualify for the discount. Meeting only one condition will not cut it.

If we want to have either one of the conditions met we can use an or statement.

Let’s pretend we’re programming a ticket booth for a big exposition.

If the person’s age is less than 6 or is greater than 65, They can enter for free. Instead of checking if they’re less than 6 and then checking again if they’re over 65, we can do this with one statement.

Practice

The most common (and most elusive) bugs are often logical errors, It’s easy to miss an improper conditional statement. The best remedy to avoid logic errors is to mess around in an editor and create a few conditional statements to test your input. Play around with chaining statements together. Most importantly, have fun with it. Cheers.

--

--

Timothy Rudenko

Former Red Hat SRE and Python instructor. I write about Programming, Linux, and Workflow. Learn technology in a foundational and reliable way.