How to Use Conditional Statements in Python

A Python Programming Tutorial

Shu Hasegawa
Geek Culture

--

Photo by Pankaj Patel on Unsplash

When a program runs, it often faces many exceptions that lead to logical errors, compromising your program’s effectiveness. Therefore, programmers use a special type of statement, Conditional Statements, to check for specific conditions during the execution of their programs. This is especially true for cases such as sorting, where you are checking for whether a value is greater or lower than another. Conditional statements are an essential part of coding, and all beginners must learn to use them in order to progress.

Booleans

As its name suggests, conditional statements check for a condition. The condition comes in the form of an expression that returns a boolean value. A boolean is a data type that can only be one of two values, True or False. Here is an example of several expressions that evaluate to boolean values:

# The expression, which states that 12 is less than 15, is True
12 < 15
# The expression, which states that the word "Tandrew" is the same # as "Andrew", is False
"Tandrew" == "Andrew"
# first letter of the word "Teresa", "T"
tree = "Teresa"[0]
drew = "andrew"
# The expression, which states that joining the strings represented # by tree and drew is…

--

--

Shu Hasegawa
Geek Culture

Kyan Chiang - 6X Top Writer. I write about finance and money. Buy me a coffee: https://www.buymeacoffee.com/shuhasegawa