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

Operators PrecendenceOperators 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:

Example

The precedence among logical operators:

Example

Let's clarify this with example:

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.

Tarea

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.

¿Todo estuvo claro?

Sección 1. Capítulo 6
toggle bottom row
course content

Contenido del Curso

Conditional Statements in Python

Operators PrecendenceOperators 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:

Example

The precedence among logical operators:

Example

Let's clarify this with example:

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.

Tarea

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.

¿Todo estuvo claro?

Sección 1. Capítulo 6
toggle bottom row
some-alt