0% found this document useful (0 votes)
24 views7 pages

Phiếu thực hành lap trinh Python căn bản - Lenh IF -

This tutorial teaches how to use the Python if statement, if-else statement, and if-elif-else statement to execute code blocks based on different conditions. It also introduces the Python ternary operator as a concise way to replace simple if-else statements.

Uploaded by

khoa huỳnh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views7 pages

Phiếu thực hành lap trinh Python căn bản - Lenh IF -

This tutorial teaches how to use the Python if statement, if-else statement, and if-elif-else statement to execute code blocks based on different conditions. It also introduces the Python ternary operator as a concise way to replace simple if-else statements.

Uploaded by

khoa huỳnh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Phiếu thực hành Python căn bản

Lệnh IF
Python if Statement
Summary: in this tutorial, you’ll learn how to use the Python if statement to
execute a block of code based on a condition.
The simple Python if statement
You use the ifstatement to execute a block of code based on a specified
condition.
The syntax of the ifstatement is as follows:
if condition:
if-block

Code language: Python (python)


The if statement checks the condition first.
If the condition evaluates to True, it executes the statements in the if-block.
Otherwise, it ignores the statements.
Note that the colon (:) that follows the condition is very important. If you forget
it, you’ll get a syntax error.
The following flowchart illustrates the if staetement:
For example:

age = input('Enter your age:')


if int(age) >= 18:
print("You're eligible to vote.")
Code language: Python (python)
This example prompts you to input your age. If you enter a number that is
greater than or equal to 18, it’ll show a message "You're eligible to vote" on
the screen. Otherwise, it does nothing.
The condition int(age) >= 18 converts the input string to an integer and
compares it with 18.
Enter your age:18
You're eligible to vote.
Code language: Python (python)
See the following example:

age = input('Enter your age:')


if int(age) >= 18:
print("You're eligible to vote.")
print("Let's go and vote.")
Code language: Python (python)
In this example, if you enter a number that is greater than or equal to 18, you’ll
see two messages.
In this example, indentation is very important. Any statement that follows
the if statement needs to have has four spaces.
If you don’t use the indentation correctly, the program will work differently. For
example:

age = input('Enter your age:')


if int(age) >= 18:
print("You're eligible to vote.")
print("Let's go and vote.")

Code language: Python (python)


In this example, the final statement always executes regardless of
the condition in the if statement. The reason is that it doesn’t belong to
the if block:
Enter your age:11
Let's go and vote.

Code language: Python (python)


Python if…else statement
Typically, you want to perform an action when a condition is True and another
action when the condition is False.
To do so, you use the if...else statement.
The following shows the syntax of the if...else statement:
if condition:
if-block;
else:
else-block;

Code language: Python (python)


In this syntax, the if...else will execute the if-block if the condition evaluates
to True. Otherwise, it’ll execute the else-block.
The following flowchart illustrates the if..else statement:

The following example illustrates you how to use the if...else statement:
age = input('Enter your age:')
if int(age) >= 18:
print("You're eligible to vote.")
else:
print("You're not eligible to vote.")

Code language: Python (python)


In this example, if you enter your age with a number less than 18, you’ll see the
message "You're not eligible to vote." like this:
Enter your age:11
You're not eligible to vote.

Code language: Python (python)


Python if…elif…else statement
If you want to check multiple conditions and perform an action accordingly, you
can use the if...elif...else statement. The elif stands for else if.
Here is the syntax if the if...elif...else statement:
if if-condition:
if-block
elif elif-condition1:
elif-block1
elif elif-condition2:
elif-block2
...
else:
else-block

Code language: Python (python)


The if...elif...else statement checks each condition (if-condition, elif-
condition1, elif-condition2, …) in the order that they appear in the statement
until it finds the one that evaluates to True.
When the if...elif...else statement finds one, it executes the statement that
follows the condition and skips testing the remaining conditions.
If no condition evaluates to True, the if...elif...else statement executes the
statement in the else branch.

Note that the else block is optional. If you omit it and no condition is True, the
statement does nothing.
The following flowchart illustrates the if...elif...else statement:

The following example uses the if...elif..else statement to determine the


ticket price based on the age:
age = input('Enter your age:')

# convert the string to int


your_age = int(age)
# determine the ticket price
if your_age < 5:
ticket_price = 5
elif your_age < 16:
ticket_price = 10
else:
ticket_price = 18
# show the ticket price
print(f"You'll pay ${ticket_price} for the ticket")
Code language: Python (python)
In this example:

 If the input age is less than 5, the ticket price will be $5.
 If the input age is greater than or equal to 5 and less than 16, the ticket price is
$10.
 Otherwise, the ticket price is $18.
Summary
 Use the if statement when you want to run a code block based on a condition.
 Use the if...else statement when you want to run another code block if the
condition is not True.
 Use the if...elif...else statement when you want to check multiple
conditions and run the corresponding code block that follows the condition that
evaluates to True.
Python Ternary Operator
Summary: in this tutorial, you’ll learn about the Python ternary operator and how to
use it to make your code more concise.

Introduction to Python Ternary Operator

The following program prompts you for your age and determines the ticket price
based on it:

age = input('Enter your age:')

if int(age) >= 18:


ticket_price = 20
else:
ticket_price = 5

print(f"The ticket price is {ticket_price}")


Code language: Python (python)

Here is the the output when you enter 18:

Enter your age:18


The ticket price is $20
Code language: Python (python)

In this example, the following if...else statement assigns 20 to the ticket_price if


the age is greater than or equal to 18. Otherwise, it assigns the ticket_price 5:

if int(age) >= 18:


ticket_price = 20
else:
ticket_price = 5
Code language: Python (python)

To make it more concise, you can use an alternative syntax like this:

ticket_price = 20 if int(age) >= 18 else 5


Code language: Python (python)

In this statement, the left side of the assignment operator ( =) is the variable ticket_price.

The expression on the right side returns 20 if the age is greater than or equal
to 18 or 5 otherwise.

The following syntax is called a ternary operator in Python:


value_if_true if condition else value_if_false
Code language: Python (python)

The ternary operator evaluates the condition. If the result is True, it returns
the value_if_true. Otherwise, it returns the value_if_false.

The ternary operator is equivalent to the following if...else statement:

if condition:
value_if_true
else:
value_if_true
Code language: Python (python)

Note that you have been programming languages such as C# or Java, you’re familiar
with the following ternary operator syntax:

condition ? value_if_true : value_if_false


Code language: Python (python)

However, Python doesn’t support this ternary operator syntax.

The following program uses the ternary operator instead of the if statement:

age = input('Enter your age:')

ticket_price = 20 if int(age) >= 18 else 5

print(f"The ticket price is {ticket_price}")


Code language: Python (python)
Summary

 The Python ternary operator is value_if_true if condition else value_if_false.


 Use the ternary operator to make your code more concise.

You might also like