Course Content
Conditional Statements in Python
Conditional Statements in Python
Operators Precendence
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:
# 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)
Swipe to show code editor
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.
Fitness goals should be tracked efficiently, and so should your code! The current implementation checks if:
- The user hasn't met their step goal.
- The user hasn't burned enough calories.
- The user didn't exercise in the morning.
These conditions are evaluated using nested if
statements, but they can be combined into one clear and concise condition. Rewrite the code to improve its readability while keeping the same logic.
Solution
Thanks for your feedback!
Operators Precendence
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:
# 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)
Swipe to show code editor
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.
Fitness goals should be tracked efficiently, and so should your code! The current implementation checks if:
- The user hasn't met their step goal.
- The user hasn't burned enough calories.
- The user didn't exercise in the morning.
These conditions are evaluated using nested if
statements, but they can be combined into one clear and concise condition. Rewrite the code to improve its readability while keeping the same logic.
Solution
Thanks for your feedback!