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:

#### Example 2:

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!

Task

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.

Task

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.

Everything was clear?

# 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:

#### Example 2:

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!

Task

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.

Task

- 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.

Everything was clear?

# 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:

#### Example 2:

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!

Task

- 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.

Task

- 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.

Everything was clear?

#### Example 1:

#### Example 2:

Let's practice!

Task

- 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.