Today, we're continuing our 10-day journey to learn Python, kicking off Day 2's lesson. If you haven't checked out Day 1 yet, you can find it here: 🎁Learn Python in 10 Days: Day 1
Day 2: Conditional Statements 🎉
Logical judgment is pretty common in daily life. Similarly, in programming, making logical judgments is one of the most basic functionalities. Below, we’ll break it down so it’s easy to understand.
1. Boolean Types and Comparison Operators 📏
Judgment statements result in two outcomes: yes or no, represented by Boolean types in programs.
Description | Explanation |
---|---|
Boolean (bool) | Booleans represent logical values—True and False. True is essentially the number 1, and False is 0. |
Define a variable to store Boolean data:
variable_name = True/False
You can define Boolean values directly, or use comparison operators to get a Boolean result:
result = 10 > 5
print("Result is %s, type is %s" % (result, type(result)))
Comparison Operators:
Operator | Description | Example |
---|---|---|
== | Checks if values are equal, returns True if they are, False otherwise | e.g., 3 == 3, returns True |
!= | Checks if values are not equal, returns True if they are not, False otherwise | e.g., 1 != 3, returns True |
> | Checks if left-hand side is greater than right-hand side | e.g., 7 > 3, returns True |
< | Checks if left-hand side is less than right-hand side | e.g., 3 < 7, returns True |
>= | Checks if left-hand side is greater than or equal to right-hand side | e.g., 3 >= 3, returns True |
<= | Checks if left-hand side is less than or equal to right-hand side | e.g., 3 <= 3, returns True |
2. Basic if
Statement 🔍
In programs, basic judgments look like this:
if condition:
execute_content
Example:
age = 20
if age >= 18:
print("Adult")
The result of the judgment needs to be a Boolean (True or False). True executes the code block within, False does not.
Example:
print("Welcome to the amusement park, kids get in free!")
age = int(input("Please enter your age:"))
if age >= 18:
print("You are an adult, you need a ticket")
print("Enjoy your visit!")
3. if-else
Statement 🛤️
if-else
lets you execute one block of code if the condition is met, and another if it isn't:
if condition:
execute_content_1
else:
execute_content_2
Example:
print("Welcome to the amusement park, kids get in free!")
age = int(input("Please enter your age:"))
if age >= 18:
print("You are an adult, you need a ticket")
else:
print("You are under 18, you get in free!")
print("Enjoy your visit!")
-
else
doesn't need a condition. - Just like
if
blocks,else
blocks require an indent.
4. if-elif-else
Statement 🌈
if-elif-else
statements handle multiple conditions:
if condition1:
execute_content
elif condition2:
execute_content
...
elif conditionN:
execute_content
else:
execute_content
Example:
print("Welcome to the zoo!")
height = int(input("Please enter your height:"))
vip_level = int(input("Please enter your VIP level (1~5):"))
day = int(input("Please enter today's date (1~30):"))
if height < 120:
print("You are under 120cm, you get in free!")
elif vip_level > 3:
print("Your VIP level is greater than 3, you get in free!")
elif day == 1:
print("Today is the 1st, you get in free!")
else:
print("Sorry, you need a ticket")
print("Enjoy your visit!")
- Multiple
elif
conditions can be used. - Judgments are mutually exclusive and sequential.
5. Nested Judgments 🏗️
In some scenarios, you might have conditions where one conditional block contains another:
if condition1:
execute_content
if condition2:
execute_content
Example:
print("Welcome to the zoo!")
height = int(input("Please enter your height:"))
vip_level = int(input("Please enter your VIP level (1~5):"))
if height > 120:
print("You are over 120cm, need a ticket")
if vip_level > 3:
print("Your VIP level is greater than 3, you get in free!")
else:
print("You need a ticket")
else:
print("You get in free!")
print("Enjoy your visit!")
Practical Exercise 🛠️
Generate a random number between 1 and 10, and you have three chances to guess:
import random
num = random.randint(1, 10)
guess_num = int(input("Guess the number:"))
if guess_num == num:
print("Congratulations! You guessed it right!")
else:
if guess_num > num:
print("Too high!")
elif guess_num < num:
print("Too low!")
guess_num = int(input("Guess again:"))
if guess_num == num:
print("Congratulations! You guessed it right!")
else:
if guess_num > num:
print("Too high!")
elif guess_num < num:
print("Too low!")
guess_num = int(input("Last guess:"))
if guess_num == num:
print("Congratulations! You guessed it right!")
else:
print("Sorry, you've used all your attempts. The number was %d." % num)
Top comments (0)