Python 101Free
FOUNDATIONS

Operators and Expressions

Arithmetic, comparison, and logical operators, plus how they combine.

SECTION 01

The operator table

Operators in Python fall into three families. Arithmetic operators (+, -, *, /, //, %, **) work on numbers. Comparison operators (==, !=, <, >, <=, >=) compare two values and return a bool. Logical operators (and, or, not) combine booleans.

Python has the standard precedence rules. Multiplication binds tighter than addition, comparison binds tighter than logical. The actual rules are not the kind of thing you should memorize. When precedence matters, add parentheses, the reader will thank you.

A few operators do double duty. + adds numbers and concatenates strings. * multiplies numbers and repeats sequences. in checks containment for any iterable. The shared symbols are part of why Python code reads naturally for different kinds of values.

1 more section

Create a free account to access all sections and animations.

Create free accountAlready have an account? Sign in