This content originally appeared on flaviocopes.com and was authored by flaviocopes.com
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
Example:
age = 8
age += 1
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
Those are rather 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.
This content originally appeared on flaviocopes.com and was authored by flaviocopes.com
flaviocopes.com | Sciencx (2020-12-02T05:00:00+00:00) Python Operators. Retrieved from https://www.scien.cx/2020/12/02/python-operators/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.