Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Operator Precedence in Python | Mastering Python if Statements
Conditional Statements in Python
course content

Course Content

Conditional Statements in Python

Conditional Statements in Python

1. Mastering Python if Statements
2. Mastering Python if-else Statements
3. Mastering Python if-elif-else Statements

book
Operator Precedence in Python

When developing a Python application, it's important to understand how Python handles operator precedence, which determines the order in which operations are performed in expressions. This ensures that conditions are evaluated correctly. Using parentheses can also help clarify and control the order of operations.

Logical Operator Precedence

Among logical operators, Python follows this precedence:

Example: Fitness Tracker Goals

Let's clarify with examples in the context of a Fitness Tracker:

12345678910111213141516171819
# Example 1: AND has higher precedence than OR steps_taken = 8000 step_goal = 10000 calories_burned = 450 calorie_goal = 500 first_result = steps_taken >= step_goal or calories_burned >= calorie_goal and False # Same as: steps_taken >= step_goal or (calories_burned >= calorie_goal and False) # Example 2: Parentheses change the precedence second_result = (steps_taken >= step_goal or calories_burned >= calorie_goal) and False # Example 3: NOT has the highest precedence third_result = not (steps_taken >= step_goal) or calories_burned >= calorie_goal # Same as: (not (steps_taken >= step_goal)) or (calories_burned >= calorie_goal) print('The first expression is:', first_result) print('The second expression is:', second_result) print('The third expression is:', third_result)
copy
Task

Swipe to start coding

In the current Fitness Tracker code, multiple nested if statements make the logic harder to read and maintain. Your task is to rewrite the code using a single, concise if statement with logical operators. Use the and logical operator to combine multiple conditions.

Fitness goals should be tracked efficiently, and so should your code! The current implementation checks if:

  1. The user hasn't met their step goal.
  2. The user hasn't burned enough calories.
  3. The user didn't exercise in the morning.

Rewrite the code to improve its readability while keeping the same logic.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 5
toggle bottom row

book
Operator Precedence in Python

When developing a Python application, it's important to understand how Python handles operator precedence, which determines the order in which operations are performed in expressions. This ensures that conditions are evaluated correctly. Using parentheses can also help clarify and control the order of operations.

Logical Operator Precedence

Among logical operators, Python follows this precedence:

Example: Fitness Tracker Goals

Let's clarify with examples in the context of a Fitness Tracker:

12345678910111213141516171819
# Example 1: AND has higher precedence than OR steps_taken = 8000 step_goal = 10000 calories_burned = 450 calorie_goal = 500 first_result = steps_taken >= step_goal or calories_burned >= calorie_goal and False # Same as: steps_taken >= step_goal or (calories_burned >= calorie_goal and False) # Example 2: Parentheses change the precedence second_result = (steps_taken >= step_goal or calories_burned >= calorie_goal) and False # Example 3: NOT has the highest precedence third_result = not (steps_taken >= step_goal) or calories_burned >= calorie_goal # Same as: (not (steps_taken >= step_goal)) or (calories_burned >= calorie_goal) print('The first expression is:', first_result) print('The second expression is:', second_result) print('The third expression is:', third_result)
copy
Task

Swipe to start coding

In the current Fitness Tracker code, multiple nested if statements make the logic harder to read and maintain. Your task is to rewrite the code using a single, concise if statement with logical operators. Use the and logical operator to combine multiple conditions.

Fitness goals should be tracked efficiently, and so should your code! The current implementation checks if:

  1. The user hasn't met their step goal.
  2. The user hasn't burned enough calories.
  3. The user didn't exercise in the morning.

Rewrite the code to improve its readability while keeping the same logic.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 5
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt