Home > Backend Development > Python Tutorial > How Does Logical Operator Precedence Work in Python?

How Does Logical Operator Precedence Work in Python?

Mary-Kate Olsen
Release: 2024-11-17 18:45:02
Original
758 people have browsed it

How Does Logical Operator Precedence Work in Python?

Logical Operator Precedence in Python

When dealing with logical operators in programming languages, understanding their precedence and order of operations is crucial. In Python, the operators NOT, AND, and OR follow a specific hierarchy that governs the order in which they evaluate expressions.

Precedence Order:

Contrary to the precedence sequence in C and C (NOT > AND > OR), Python has its own unique order:

1. NOT
2. AND
3. OR

From highest to lowest precedence, NOT evaluates first, followed by AND, and then OR. This means that NOT operations take precedence over both AND and OR, and AND operations take precedence over OR.

Example:

Consider the following Python expression:

result = not a and b or c
Copy after login

According to the precedence order, the expression will be evaluated as follows:

  1. a is checked for truthiness.
  2. If a is False, not a is True. Otherwise, it's False.
  3. If the result of not a is True, and b evaluates to b. Otherwise, it's False.
  4. If the result of and b is True, the entire expression evaluates to True. Otherwise, the result of or c is evaluated.
  5. c is checked for truthiness.
  6. If c is True, the entire expression evaluates to True. Otherwise, it's False.

Complete Precedence Table:

For a comprehensive list of operator precedence in Python, refer to the following table:

Precedence Operators
0 Assignment (=)
1 Lambda functions (lambda)
2 Conditional expression (if-else)
Precedence Operators
0 Assignment (=)
1 Lambda functions (lambda)
2 Conditional expression (if-else)
3 Logical OR (or)
4 Logical AND (and)
5 Logical NOT (not)
6 Comparison operators (<, <=, >, >=, !=, ==)
7 Bitwise OR ( )
8 Bitwise XOR (^), Bitwise AND (&)
9 Shift operators (<<, >>)
10 Addition ( ), Subtraction (-)
11 Multiplication (*), Division (/), Floor division (//), Modulo (%)
12 Unary plus ( ), Unary minus (-), Bitwise NOT (~)
13 Exponentiation (**)
14 Coroutine creation (async with)
15 Indexing and slicing ([...], [...]), Function calls, Attribute access
16 Parentheses, Square brackets, Braces, Set literals
3
Logical OR (or)
4 Logical AND (and)
5 Logical NOT (not)
6 Comparison operators (<, <=, >, >=, !=, ==)
7 Bitwise OR ( )
8 Bitwise XOR (^), Bitwise AND (&)
9 Shift operators (<<, >>)
10 Addition ( ), Subtraction (-)
11 Multiplication (*), Division (/), Floor division (//), Modulo (%)
12 Unary plus ( ), Unary minus (-), Bitwise NOT (~)
13 Exponentiation (**)
14 Coroutine creation (async with)
15 Indexing and slicing ([...], [...]), Function calls, Attribute access
16 Parentheses, Square brackets, Braces, Set literals

The above is the detailed content of How Does Logical Operator Precedence Work in Python?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template