Course Content
Conditional Statements in Python
Conditional Statements in Python
Conditional Expression
At times, we may need to assign different values to a variable based on specific conditions. Achieving this efficiently can be done in just one line of code. Let's explore a few approaches to grasp this concept more effectively.
Example 1:
age = 23 status = '' if age >= 18: status = 'Adult' else: status = 'Not adult' print(status)
Example 2:
age = 15 status = 'Adult' if age >= 18 else 'Not Adult' print(status)
Have you observed the distinction? The second method is not only more elegant but also more convenient, condensing it into just one line instead of four!
Conditional expressions, also known as Python's ternary operator, act as decision-making tools and adhere to the following syntax:
Let's practice!
Swipe to show code editor
Your friend wants to choose the car to buy, and you can help. He has a certain amount of money for this purchase. We know how much both cars cost.
- Assign the brand name your friend will buy to the variable
car_to_buy
; - The decision is based on the car's cost and your friend's amount of money;
- Print the car's brand that suits the condition;
- Your friend has an amount that is enough at least for one car.
Note
We will compare the amount of money with the cost of the more expensive car.
Fill in the blanks, please.
Thanks for your feedback!
Conditional Expression
At times, we may need to assign different values to a variable based on specific conditions. Achieving this efficiently can be done in just one line of code. Let's explore a few approaches to grasp this concept more effectively.
Example 1:
age = 23 status = '' if age >= 18: status = 'Adult' else: status = 'Not adult' print(status)
Example 2:
age = 15 status = 'Adult' if age >= 18 else 'Not Adult' print(status)
Have you observed the distinction? The second method is not only more elegant but also more convenient, condensing it into just one line instead of four!
Conditional expressions, also known as Python's ternary operator, act as decision-making tools and adhere to the following syntax:
Let's practice!
Swipe to show code editor
Your friend wants to choose the car to buy, and you can help. He has a certain amount of money for this purchase. We know how much both cars cost.
- Assign the brand name your friend will buy to the variable
car_to_buy
; - The decision is based on the car's cost and your friend's amount of money;
- Print the car's brand that suits the condition;
- Your friend has an amount that is enough at least for one car.
Note
We will compare the amount of money with the cost of the more expensive car.
Fill in the blanks, please.
Thanks for your feedback!
Conditional Expression
At times, we may need to assign different values to a variable based on specific conditions. Achieving this efficiently can be done in just one line of code. Let's explore a few approaches to grasp this concept more effectively.
Example 1:
age = 23 status = '' if age >= 18: status = 'Adult' else: status = 'Not adult' print(status)
Example 2:
age = 15 status = 'Adult' if age >= 18 else 'Not Adult' print(status)
Have you observed the distinction? The second method is not only more elegant but also more convenient, condensing it into just one line instead of four!
Conditional expressions, also known as Python's ternary operator, act as decision-making tools and adhere to the following syntax:
Let's practice!
Swipe to show code editor
Your friend wants to choose the car to buy, and you can help. He has a certain amount of money for this purchase. We know how much both cars cost.
- Assign the brand name your friend will buy to the variable
car_to_buy
; - The decision is based on the car's cost and your friend's amount of money;
- Print the car's brand that suits the condition;
- Your friend has an amount that is enough at least for one car.
Note
We will compare the amount of money with the cost of the more expensive car.
Fill in the blanks, please.
Thanks for your feedback!
At times, we may need to assign different values to a variable based on specific conditions. Achieving this efficiently can be done in just one line of code. Let's explore a few approaches to grasp this concept more effectively.
Example 1:
age = 23 status = '' if age >= 18: status = 'Adult' else: status = 'Not adult' print(status)
Example 2:
age = 15 status = 'Adult' if age >= 18 else 'Not Adult' print(status)
Have you observed the distinction? The second method is not only more elegant but also more convenient, condensing it into just one line instead of four!
Conditional expressions, also known as Python's ternary operator, act as decision-making tools and adhere to the following syntax:
Let's practice!
Swipe to show code editor
Your friend wants to choose the car to buy, and you can help. He has a certain amount of money for this purchase. We know how much both cars cost.
- Assign the brand name your friend will buy to the variable
car_to_buy
; - The decision is based on the car's cost and your friend's amount of money;
- Print the car's brand that suits the condition;
- Your friend has an amount that is enough at least for one car.
Note
We will compare the amount of money with the cost of the more expensive car.
Fill in the blanks, please.