- Conditional Test
- >>> cars = 'bmw'
- Can be interpreted as: 'Set the value of the cars 'bmw' '
- >>> cars == 'bmw'
- True
- >>> cars == 'audi'
- False
- Can be interpreted as: 'Is the value of the cars 'bmw'?' True. 'Is the value of the cars 'audi'?' False.
- Case sensitivity is important. For exmaple:
>>> car = 'Audi'
>>> car == 'audi'
False
However,
>>> car = 'Audi'
>>> car.lower() == 'audi'
True
- Checking for inequality using 'if' statement
- Use != inequality operator if the values are not equal. For example:
requested_topping = 'mushrooms'
if requested_topping != 'anchovies':
print("Hold the anchovies!")
Notice in the 'if' loop, there's a colon and print function is indentated
Hold the anchovies!
- Checking for certain value if it's in the list
- Use 'in', For example:
>>> requested_toppings = ['mushrooms', 'onions', 'pineapple']
>>> 'mushrooms' in requested_toppings
True
>>> 'pepperoni' in requested_toppings
False
- Checking for certain value if it's NOT in the list
- Use 'not in', For example:
banned_users = ['andrew', 'carolina', 'david']
user = 'marie'
if user not in banned_users:
print(f"{user.title()}, you can post a response if you wish.")
Marie, you can post a response if you wish.
- if-else Statements
age = 18
if age >= 18:
print("You are old enough to vote!")
print("Have you registered to vote yet?")
else:
print("Sorry, you are too young to vote.")
print("Please register to vote as soon as you turn 18!")
You are old enough to vote!
Have you registered to vote yet?
If the value satisfies the if statement, the following indentated codelines are printed
If the value does not satisfy the if statement, codelines in the else statement are printed
*Good to execute one or two possible actions
- if-elif-else Statements
age = 12
if age < 4:
price = 0
elif age < 18:
p rice = 25
elif age < 65:
price = 40
else:
price = 20
print(f"Your admission cost is ${price}.")
There can be multiple elif statements, meaning if-elif-else statements are good to use when executing multiple actions
Most of the time, it’s clearer to use an additional elif statement that catches the specific condition of interest. Instead of else statement
So it could be re-written like this:
age = 12
if age < 4:
price = 0
elif age < 18:
price = 25
elif age < 65:
price = 40
elif age >= 65:
price = 20
print(f"Your admission cost is ${price}.")
- Testing multiple conditions
The if-elif-else chain is powerful, but it’s only appropriate
to use when you just need one test to pass. As soon as
Python finds one test that passes, it skips the rest of the
tests. This behavior is beneficial, because it’s efficient and
allows you to test for one specific condition.
However, sometimes it’s important to check all conditions
of interest. In this case, you should use a series of simple if
statements with no elif or else blocks.
requested_toppings = ['mushrooms', 'extra cheese']
if 'mushrooms' in requested_toppings:
print("Adding mushrooms.")
if 'pepperoni' in requested_toppings:
print("Adding pepperoni.")
if 'extra cheese' in requested_toppings:
print("Adding extra cheese.")
print("\nFinished making your pizza!")
In summary, if you want only one block of code to run, use
an if-elif-else chain. If more than one block of code needs
to run, use a series of independent if statements
'Python > Python Crash Course' 카테고리의 다른 글
Python Crash Course: 챕터 7 (0) | 2024.06.29 |
---|---|
Python Crash Course: 챕터 6 (0) | 2024.06.28 |
Python Crash Course: 챕터 4 (0) | 2024.06.26 |
Python Crash Course: 챕터 3 (0) | 2024.06.25 |
Python Crash Course: 챕터 2 (0) | 2024.06.23 |