Python Operators
By Flavio
Published · Updated
Tagged: Python
Learn how Python operators work: assignment and arithmetic operators, comparison and boolean operators, bitwise operators, and the is and in keywords.
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 assigns a value as part of another operation. You use it when you need the value and also want to test it:
items = ['Roger', 'Syd', 'Luna']
if (n := len(items)) > 2:
print(n) #3
Arithmetic operators
Python has a number of arithmetic operators: +, -, *, / (division), % (remainder), ** (exponentiation) and // (floor division). There’s also @ for matrix multiplication, used by NumPy. I cover the number types in Python numbers.
1 + 1 #2
2 - 1 #1
2 * 2 #4
4 / 2 #2.0
4 % 3 #1
4 ** 2 #16
4 // 2 #2
/ always returns a float in Python 3. // is floor division. With int operands it returns an int.
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
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. See Python Booleans for more on that:
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:
notandor
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 the identity operator. It is True when both names point at the same object. == compares values:
a = [1, 2]
b = [1, 2]
a == b #True
a is b #False
a and b have the same items, but they are two lists. The usual check for None is is:
name = None
name is None #True
in is the membership operator. It tells you if a value sits in a list, a string, or another sequence:
'dog' in ['cat', 'dog'] #True
'og' in 'dog' #TrueWant me to talk about your product? You can sponsor this site.
Related posts about python: