Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

If, If. .. Else, If. .. Elif. .. Else Commands in Python

Explore If, If. .. Else, If. .. Elif. .. Else Commands in Python with clear explanations, practical examples, and useful tips.

Table of Contents

This guide covers if, if. .. else, if. .. elif. .. else commands in python with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.

In the previous section we learned about some Python Data types and how to use it, and also know a bit about While in Python commands. In this section, you will learn more about the most common command in Python if.

If you have learned other programming languages, you already know the use of this command, but in Python programming language it has some interesting features. Let's find out.

Decision making is necessary when we want to execute a code only when it satisfies certain conditions. The if. Elif. Else command is used in Python to serve this purpose. Here we will learn about If statements in Python , each item has an example and a specific explanation for you to understand.

if  ?i?u ki?n

 kh?i l?nh

Here, the program evaluates The condition And will execute the Command When The condition Is True. If The condition False, the Command Will not be executed.

In Python, the command block of the If statement Is indented inside. If's block starts with an indent interval and the first indented line will be interpreted as an if Statement end.

If command diagram in Python

Example 1:

# N?u là s? d??ng ta s? in m?t thông ?i?p thích h?p
num = 3
if num > 0 :
print ( num , "là s? d??ng." )
print ( "Thông ?i?p này luôn ???c in." )

num = - 1
if num > 0 :
print ( num , "là s? d??ng." )
print ( "Thông ?i?p này c?ng luôn ???c in." )

Output of the program above:

3 l à s ? d ?? ng .
Th ô ng ? i ? pn à y lu ô n ??? c in .
Th ô ng ? i ? pn à yc ? ng lu ô n ??? c in .

In the above example, num> 0 is a condition, if's block is executed when the condition is satisfied. When num equals 3, check the condition, true, if the block of if is executed. When num equals -1, does not satisfy the condition, if's block is ignored and the last print () is executed.

A bit more careful, you will see that the print () statement is not indented, which indicates that print () is outside the if block, so it will be executed, regardless of the condition.

If. Else command structure

if ?i?u ki?n:
 Kh?i l?nh c?a if
else:
 Kh?i l?nh c?a else

If. Else command checks the condition and executes if if the condition is true. If the condition is false, the else block of the statement will be executed. Indentation is used to separate command blocks.

If. Else command diagram

Example 2:

# Kiem tra xem so am hay duong
# Va hien thi thong bao phu hop

num = 3

if num >= 0:
 print("So duong hoac bang 0")
else:
 print("So am")

num1=-1

 if num1 >= 0:
 print("So duong hoac bang 0")
else:
 print("So am")

In Listing 2, we examine two variables, num and num1. When num equals 3, satisfies condition num> = 0, if's block is executed. Num1 = -1 does not satisfy condition num1> = 0 so else's block is executed and ignores if's block.

The result of the above command will print the two line screen: the above line is the result of checking the variable num and the lower line is the variable num1.

So duong hoac bang 0
So am

Structure of if. Elif. Else command

if ?i?u ki?n:
 Kh?i l?nh c?a if
elif test expression:
 Kh?i l?nh c?a elif
else:
 Kh?i l?nh c?a else

Elif is else if abbreviation, it allows us to check many conditions.

If the condition is false, it will check the condition of the next elif block and so on.

If all conditions are wrong, it will execute else's block.

Only one block in if. Elif. Else is executed according to the condition.

There may be no or more elif, the else part is optional.

Diagram of if. Elif. Else command

Example 3:

x = int(input("Nhap mot so: ")) 
 if x < 0: 
 x = 0 
 print('So am') 
 elif x == 0: 
 print('So 0') 
 elif x == 1: 
 print('So 1') 
 else: 
 print('So duong')

Output result:

If x is a negative number, print the screen: "So am".

If x = 0, it will print: "So 0".

If x = 1, it will print: "So 1".

If all three conditions are wrong, print: "So duong".

The if. Elif. Elif. Command replaces the switch or case statement in other programming languages.

You can write the if. Elif. Else statement in an if. Elif. Else block, and make the if statement nested. There is no limit to the number of commands that are inserted into another command. Indentation is the only way to identify cage levels, so it can be confusing and confusing. You should limit use if possible.

Example 4:

# Trong code này, nh?p vào m?t s?
# Ki?m tra xem s? âm hay d??ng
# hay b?ng không và hi?n th?
# thông báo thích h?p
# S? d?ng hàm if l?ng nhau

num = float(input("Nh?p m?t s?: "))
if num >= 0:
 if num == 0:
 print("S? Không")
 else:
 print("S? d??ng")
else:
 print("S? âm")

Result 1:

Nh?p m?t s?: 10

S? d??ng

Result 2:

Nh?p m?t s?: -5

S? âm

Result 3:

Nh?p m?t s?: 0

S? Không

Now that you know the basic elements when using the if statement in Python. The next section we will learn about for for loop. Have you remembered to it.

Next article: For in Python loop

Previous lesson: Introducing about strings, numbers, and lists in Python

FAQ

What should I check before following these steps?

Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.

Why might the process not work?

Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.

Can I undo the changes if necessary?

That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.