Python Operators

⭐️ 👀 2023 WEB DEVELOPMENT BOOTCAMP starting in days! Join the waiting list to reserve your spot in my 10-weeks cohort course and learn the fundamentals, HTML, CSS, JS, Tailwind, React, Next.js and much much more! 👀 ⭐️

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:

  • assignment operator
  • arithmetic operators
  • comparison operators
  • logical operators
  • bitwise operators

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:

  • +=
  • -=
  • *=
  • /=
  • %=
  • ..and so on


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:

  • not
  • and
  • or

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:

  • & performs binary AND
  • | performs binary OR
  • ^ performs a binary XOR operation
  • ~ performs a binary NOT operation
  • << shift left operation
  • >> shift right operation

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.

One more thing! ⚠️ ✋

At the end of January I will organize the Web Development Bootcamp.

It's a 10-weeks long cohort online course where I will guide you to becoming a Web Developer.

It's not "just a course". It's a big event I organize once a year.

We'll start from zero, learn the fundamentals of Web Development, HTML CSS, JavaScript, Tailwind, Git, using the command line, VS Code, GitHub, Node.js, we'll then learn React, JSX, how to use PostgreSQL, Astro, Next.js, Prisma, deploying on Netlify/DigitalOcean/Fly/Vercel and much more! 

At the end of the first 10 weeks you'll know how to create web sites and web applications and I'll unlock you the 2nd phase of the Bootcamp: you will get access to a large number of projects exclusive to the Bootcamp graduates, so you can follow my instructions to build things like private areas with authentication, clones of popular sites like Twitter YouTube Reddit, create e-commerce sites, and much much more.

Because once you got the fundamentals, you only learn by working on real, exciting projects.

To find out more, visit