Skip to content

Python Operators

New Course Coming Soon:

Get Really Good at Git

Python operators are symbols that we use to run operations upon values and variables.

We can divide operators based on the kind of operation they perform:

plus some interesting ones like is and in.

Assignment operator

The assignment operator is used to assign a value to a variable:

age = 8

Or to assign a variable value to another variable:

age = 8
anotherVariable = age

Since Python 3.8, the := walrus operator is used to assign a value to a variable as part of another operation. For example inside an if or in the conditional part of a loop. More on that later.

Arithmetic operators

Python has a number of arithmetic operators: +, -, *, / (division), % (remainder), ** (exponentiation) and // (floor division):

1 + 1 #2
2 - 1 #1
2 * 2 #4
4 / 2 #2
4 % 3 #1
4 ** 2 #16
4 // 2 #2

Note that you don’t need a space between the operands, but it’s good for readability.

- also works as a unary minus operator:

print(-4) #-4

+ is also used to concatenate String values:

"Roger" + " is a good dog"
#Roger is a good dog

We can combine the assignment operator with arithmetic operators:

Example:

age = 8
age += 1
# age is now 9

Comparison operators

Python defines a few comparison operators:

You can use those operators to get a boolean value (True or False) depending on the result:

a = 1
b = 2

a == b #False
a != b #True
a > b #False
a <= b #True

Boolean operators

Python gives us the following boolean operators:

When working with True or False attributes, those work like logical AND, OR and NOT, and are often used in the if conditional expression evaluation:

condition1 = True
condition2 = False

not condition1 #False
condition1 and condition2 #False
condition1 or condition2 #True

Otherwise, pay attention to a possible source of confusion.

or used in an expression returns the value of the first operand that is not a falsy value (False, 0, '', []..). Otherwise it returns the last operand.

print(0 or 1) ## 1
print(False or 'hey') ## 'hey'
print('hi' or 'hey') ## 'hi'
print([] or False) ## 'False'
print(False or []) ## '[]'

The Python docs describe it as if x is false, then y, else x.

and only evaluates the second argument if the first one is true. So if the first argument is falsy (False, 0, '', []..), it returns that argument. Otherwise it evaluates the second argument:

print(0 and 1) ## 0
print(1 and 0) ## 0
print(False and 'hey') ## False
print('hi' and 'hey') ## 'hey'
print([] and False ) ## []
print(False and [] ) ## False

The Python docs describe it as if x is false, then x, else y.

Bitwise operators

Some operators are used to work on bits and binary numbers:

Bitwise operators are rarely used, only in very specific situations, but they are worth mentioning.

is and in

is is called the identity operator. It is used to compare two objects and returns true if both are the same object. More on objects later.

in is called the membership operator. Is used to tell if a value is contained in a list, or another sequence. More on lists and other sequences later.

Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching Summer 2024. Join the waiting list!
→ Get my Python Handbook
→ Get my Python Handbook

Here is how can I help you: