Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Operators Precendence | Python if Statement
Conditional Statements in Python
course content

Course Content

Conditional Statements in Python

Conditional Statements in Python

1. Python if Statement
2. Python if-else Statement
3. Python if-elif-else Statement
4. Python Ternary Operator

bookOperators Precendence

Python has priorities in which order the operations are performed. You must use parentheses to change the order of calculations.

However, if you do not use parentheses, the priorities in the operators are as follows:

The precedence among logical operators:

Let's clarify this with example:

1234567891011
# AND is the first operation, OR is the second first_result = True or False and False # same as True or (False and False) # OR is the first operation, AND is the second second_result = (True or False) and False third_result = not True or False # same as (not True) or False print('The first expression is:', first_result) print('The second expression is:', second_result) print('The third expression is:', third_result)
copy

In the code above, you can see that the expressions only differ because of the parentheses, leading to completely different outcomes. Let's break it down:

  • first_result is showing that and has higher precedence than or, resulting in True;
  • second_result uses parentheses to force True or False to evaluate first, then applies and False, resulting in False;
  • third_result shows that not has the highest precedence, making the expression equivalent to (not True) or False, resulting in False.
Task
test

Swipe to show code editor

Create a program that checks whether a given year is a leap year. A leap year is divisible by 4 but not divisible by 100, except if it's also divisible by 400. The program should output either 'Leap year' or 'Not a leap year' accordingly.

  1. The main condition of the leap year is that year must be divided by 4. Use year % 4 == 0;
  2. The second condition is that the year must not be divided by 100. Use year % 100 != 0;
  3. But if the year is divisible by 400 is the leap year. Use year % 400 == 0.

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 6
toggle bottom row

bookOperators Precendence

Python has priorities in which order the operations are performed. You must use parentheses to change the order of calculations.

However, if you do not use parentheses, the priorities in the operators are as follows:

The precedence among logical operators:

Let's clarify this with example:

1234567891011
# AND is the first operation, OR is the second first_result = True or False and False # same as True or (False and False) # OR is the first operation, AND is the second second_result = (True or False) and False third_result = not True or False # same as (not True) or False print('The first expression is:', first_result) print('The second expression is:', second_result) print('The third expression is:', third_result)
copy

In the code above, you can see that the expressions only differ because of the parentheses, leading to completely different outcomes. Let's break it down:

  • first_result is showing that and has higher precedence than or, resulting in True;
  • second_result uses parentheses to force True or False to evaluate first, then applies and False, resulting in False;
  • third_result shows that not has the highest precedence, making the expression equivalent to (not True) or False, resulting in False.
Task
test

Swipe to show code editor

Create a program that checks whether a given year is a leap year. A leap year is divisible by 4 but not divisible by 100, except if it's also divisible by 400. The program should output either 'Leap year' or 'Not a leap year' accordingly.

  1. The main condition of the leap year is that year must be divided by 4. Use year % 4 == 0;
  2. The second condition is that the year must not be divided by 100. Use year % 100 != 0;
  3. But if the year is divisible by 400 is the leap year. Use year % 400 == 0.

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 6
toggle bottom row

bookOperators Precendence

Python has priorities in which order the operations are performed. You must use parentheses to change the order of calculations.

However, if you do not use parentheses, the priorities in the operators are as follows:

The precedence among logical operators:

Let's clarify this with example:

1234567891011
# AND is the first operation, OR is the second first_result = True or False and False # same as True or (False and False) # OR is the first operation, AND is the second second_result = (True or False) and False third_result = not True or False # same as (not True) or False print('The first expression is:', first_result) print('The second expression is:', second_result) print('The third expression is:', third_result)
copy

In the code above, you can see that the expressions only differ because of the parentheses, leading to completely different outcomes. Let's break it down:

  • first_result is showing that and has higher precedence than or, resulting in True;
  • second_result uses parentheses to force True or False to evaluate first, then applies and False, resulting in False;
  • third_result shows that not has the highest precedence, making the expression equivalent to (not True) or False, resulting in False.
Task
test

Swipe to show code editor

Create a program that checks whether a given year is a leap year. A leap year is divisible by 4 but not divisible by 100, except if it's also divisible by 400. The program should output either 'Leap year' or 'Not a leap year' accordingly.

  1. The main condition of the leap year is that year must be divided by 4. Use year % 4 == 0;
  2. The second condition is that the year must not be divided by 100. Use year % 100 != 0;
  3. But if the year is divisible by 400 is the leap year. Use year % 400 == 0.

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!

Python has priorities in which order the operations are performed. You must use parentheses to change the order of calculations.

However, if you do not use parentheses, the priorities in the operators are as follows:

The precedence among logical operators:

Let's clarify this with example:

1234567891011
# AND is the first operation, OR is the second first_result = True or False and False # same as True or (False and False) # OR is the first operation, AND is the second second_result = (True or False) and False third_result = not True or False # same as (not True) or False print('The first expression is:', first_result) print('The second expression is:', second_result) print('The third expression is:', third_result)
copy

In the code above, you can see that the expressions only differ because of the parentheses, leading to completely different outcomes. Let's break it down:

  • first_result is showing that and has higher precedence than or, resulting in True;
  • second_result uses parentheses to force True or False to evaluate first, then applies and False, resulting in False;
  • third_result shows that not has the highest precedence, making the expression equivalent to (not True) or False, resulting in False.
Task
test

Swipe to show code editor

Create a program that checks whether a given year is a leap year. A leap year is divisible by 4 but not divisible by 100, except if it's also divisible by 400. The program should output either 'Leap year' or 'Not a leap year' accordingly.

  1. The main condition of the leap year is that year must be divided by 4. Use year % 4 == 0;
  2. The second condition is that the year must not be divided by 100. Use year % 100 != 0;
  3. But if the year is divisible by 400 is the leap year. Use year % 400 == 0.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 1. Chapter 6
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