How to Write Conditional Statements in Python
Introduction
Conditional statements are part of every programming language.
Thanks to these statements, we can make the code sometimes work and sometimes not, depending on the current state of the program.
When we run each line of a program in top-down order, we are not actually asking the program to check for certain conditions.
But by using conditional statements we can have the program check if certain conditions are met and determine what to do accordingly.
Let's look at a few examples of where we can use conditional statements:
- If a student gets over 65% on their exam, report it as pass; otherwise failed.
- If you have money in an account, calculate interest; otherwise apply penalty fee.
- If they buy 10 or more oranges, apply 5% discount; If they buy less, don't give a discount.
By evaluating conditions and executing code based on those conditions, we are actually writing conditional code.
In this lesson, we will learn how to write conditional statements in the Python programming language.
if Statement
Let's start with the expression if.
if checks whether a condition is true or false.
If the condition is true, the relevant code runs; if it is false, the code is skipped.
Open a new file in a simple text editor and write the following code:
not_ortalama = 70
if not_ortalama >= 65:
print("Başarılı, geçti 🎉")
In this code, we assigned the value 70 to the variable not_ortalama.
Then we checked whether this value is greater than or equal to 65 with the expression if (>=).
If the condition is met, the program will print the message "Başarılı, geçti 🎉".
Save the program as gecme-notu.py and run it from the terminal with the following command:
python gecme-notu.py
Output
Başarılı, geçti 🎉
Now let's change the result of the program.
For this, let's set the value of variable not_ortalama to 60:
not_ortalama = 60
if not_ortalama >= 65:
print("Başarılı, geçti 🎉")
When we save and run this code, we will get no output because the condition is not met and we did not tell the program what to do otherwise.
Let's give another example: Let's check if a bank account balance is below 0.
To do this, let's create a file named hesap.py and write the following program:
bakiye = -50
if bakiye < 0:
print("Hesap bakiyesi ekside..")
When we run the program with the following command:
python3 hesap.py
We will see the following output on the screen:
Hesap bakiyesi ekside..
In the program, we initialized the variable bakiye with the value -50.
Since this value is less than 0, the condition if bakiye < 0: is met and the message "Hesap bakiyesi ekside." is printed on the screen.
If we change bakiye to 0 or a positive number, the program will not give any output because the condition is not met.
###else Statement
In most cases, we want the program to do something even when the if condition is False.
For example, in our grade example, we definitely want to see an output based on whether the student passed or not.
To do this, we add an else statement to the if condition. Its structure is as follows:
not_ortalama = 60
if not_ortalama >= 65:
print("Başarılı, geçti 🎉")
else:
print("Başarısız, kaldı ❌")
In the above example, since the variable not_ortalama is 60, the condition if returns False.
Therefore the program does not output "Başarılı, geçti 🎉".
But the else block we added still makes the program do something.
When we save and run the program, we get the following output:
Output
Başarısız, kaldı ❌
If we set the value not_ortalama to 65 or higher in the program, this time we will get the output "Başarılı, geçti 🎉".
Let's apply the same logic to the bank account example.
With the expression else added, it looks like this:
bakiye = 50
if bakiye < 0:
print("Hesap bakiyesi ekside..")
else:
print("Hesap bakiyesi artıda..")
Output
Hesap bakiyesi artıda..
Here we set the value of the variable bakiye to a positive number, so the block else executed, printing the message "Hesap bakiyesi artıda..".
If we make the value negative, this time the first if block will run and the "Hesap bakiyesi ekside.." message will appear.
When we combine the expression if with an expression else, we are essentially creating a two-part conditional expression.
This structure ensures that the program does something in any case, regardless of whether the condition is met or not.
elif (Else If) Statement
So far we have only considered two possibilities (True or False) in conditional statements.
But in most cases we want the program to check more than two possibilities.
For this we use the structure else if. In Python this is written as elif.
elif works just like if but checks an additional condition.
In other words, it allows us to evaluate different situations in turn.
We may want to get three separate printouts for three different situations in the bank account program:
- Balance below 0
- Balance equals 0
- Balance above 0
In this case, we can place elif between if and else:
bakiye = 0
if bakiye < 0:
print("Hesap bakiyesi ekside..")
elif bakiye == 0:
print("Hesap bakiyesi sıfır.")
else:
print("Hesap bakiyesi artıda..")
Now, when we run the program, three different possibilities may occur:
- If
bakiyeis equal to 0,elifblock runs → we get the output"Hesap bakiyesi sıfır..". - If
bakiyeis a positive number, blockelseexecutes → we get the output"Hesap bakiyesi artıda..". - If
bakiyeis negative, theifblock runs → we get the output"Hesap bakiyesi ekside..".
But what if we want there to be more than three possibilities?
To do this, we can write more than one elif expression.
For example, let's rewrite the gecme-notu.py program and convert numerical grades to letter grades:
- 90 and above → A
- 80-89 → B
- 70-79 → C
- 65-69 → D
- 64 and below → F
To do this, we will use 1 expression if, 3 expressions elif and 1 expression else:
In the previous example, we were printing letter grades directly.
Now let's update the code and print each letter grade as an annotated string.
Our expression else can remain the same:
not_ortalama = 72
if not_ortalama >= 90:
print("Tebrikler 🎉 Harf Notun: A (Mükemmel başarı!)")
elif not_ortalama >= 80:
print("Harf Notun: B (Gayet iyi, devam et )")
elif not_ortalama >= 70:
print("Harf Notun: C (Ortalama, biraz daha gayret )")
elif not_ortalama >= 65:
print("Harf Notun: D (Geçtin ama daha iyisini yapabilirsin )")
else:
print("Harf Notun: F (Başarısız , tekrar denemelisin)")
Since the expressions elif are evaluated sequentially, our code can remain quite simple.
The program follows these steps:
- If the grade is greater than 90, the program prints the grade
"A". If it is less than 90, it moves to the next condition. - If the grade is 80 or greater, prints the grade
"B". If it is 79 or lower, it moves on to the next condition. - If the grade is 70 or greater, prints the grade
"C". If it is 69 or lower, it moves on to the next condition. - If the grade is 65 or greater, prints the grade
"D". If it is 64 or lower, it goes to the last step. - If none of the above conditions are met, the program prints
"F"(Fail).
Nested If Statements
If you are comfortable using if, elif and else expressions, you can now move on to nested conditional statements.
Nested if structures are useful when we want to check an additional condition when the first condition is True.
So within a block if-else there can be another block if-else.
if ifade1: # dış if ifadesi
print("doğru")
if ic_ifade: # iç (nested) if ifadesi
print("evet")
else: # iç (nested) else ifadesi
print("hayır")
else: # dış else ifadesi
print("yanlış")
Several different outputs can be obtained from this code:
- If the statement1 condition is true (
doğru), the program evaluates the nested_statement condition this time. - If both conditions are true, the output is:
Output
doğru
evet
If ifade1 is true but ic_ifade (nested_statement) returns false, the output is:
doğru
hayır
If ifade1 is False, the inner if-else block will not work at all.
In this case only the outer else works and the output is:
yanlış
We can also use multiple nested (nested) if statements in our codes.
In other words, a condition may contain other conditions, and within them additional conditions.
if ifade1: # dış if
print("merhaba dünya")
if ic_ifade1: # ilk iç if
print("evet")
elif ic_ifade2: # ilk iç elif
print("belki")
else: # ilk iç else
print("hayır")
elif ifade2: # dış elif
print("merhaba galaksi")
if ic_ifade3: # ikinci iç if
print("evet")
elif ic_ifade4: # ikinci iç elif
print("belki")
else: # ikinci iç else
print("hayır")
else: # dış else
print("merhaba evren")
In the example above, each if block also has an internal if statement.
This allows us to control more options in every situation.
Now let's do an example using our gecme-notu.py program:
First we will check if the student's grade passes (>= 65).
If it is passed, we will then determine which letter grade it falls into.
If it didn't pass, there's no need to check letter grades; The program will directly output fail.
not_ortalama = 78
if not_ortalama >= 65: # geçme durumu
print("Geçti ✅")
if not_ortalama >= 90:
print("Harf Notu: A")
elif not_ortalama >= 80:
print("Harf Notu: B")
elif not_ortalama >= 70:
print("Harf Notu: C")
else:
print("Harf Notu: D")
else:
print("Başarısız ❌ - Harf Notu: F")
If we run the program with not_ortalama = 92:
- The first condition (
>= 65) is satisfied →"Geçti ✅"is printed. - Then
>= 90is checked → since this is provided,"Harf Notu: A"is printed.
If we run the program with not_ortalama = 60:
- The first condition (
>= 65) is not met → the innerifblocks are skipped. - The program directly executes block
elseand outputs"Başarısız ❌ - Harf Notu: F".
If we want to elaborate further, we can add a second layer nested if**.
For example, to evaluate the grades A+, A and A- separately, we can first check whether the grade is passed, then whether it is 90 or above, and then whether it is above 96:
not_ortalama = 97
if not_ortalama >= 65:
print("Geçti ✅")
if not_ortalama >= 90:
if not_ortalama > 96:
print("Harf Notu: A+ 🌟")
elif not_ortalama > 93:
print("Harf Notu: A")
else:
print("Harf Notu: A-")
elif not_ortalama >= 80:
print("Harf Notu: B")
elif not_ortalama >= 70:
print("Harf Notu: C")
else:
print("Harf Notu: D")
else:
print("Başarısız ❌ - Harf Notu: F")
When not_ortalama = 96 in the above code, the program follows these steps:
not_ortalama >= 65is checked → True ✅
→"Geçti ✅"is printed.not_ortalama >= 90is checked → True ✅not_ortalama > 96is checked → False ❌not_ortalama > 93and<= 96are checked → True ✅
→"Harf Notu: A"is printed.- Since the nested conditions are completed, the program continues with the remaining codes.
Output
Geçti ✅
Harf Notu: A
Nested (nested) if statements provide the opportunity to add multiple and more customized conditions to your code.
This way, you can make more detailed checks for different situations and make the program's output more flexible and meaningful.
Result
By using conditional statements like if, you can better control what code your program runs.
Conditional statements check whether a certain condition is met:
- If the condition is met, the relevant code block runs.
- If the condition is not met, the program continues to run by switching to other codes.
To better reinforce conditional statements:
- Try different comparison operators.
- Write conditions by combining logical operators such as
andandor. - Use conditional statements with loops (
for,while).

