Loading...

Python tutorial

Python Operators

 

Operators are used to perform operations on variables and values.

Python divides the operators in the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Identity operators
  • Membership operators
  • Bitwise operators

Python Arithmetic Operators

Arithmetic operators are used with numeric values to perform common mathematical operations:

operator   name Example values for x and y output
+ Addition x + y print(5+3) 8
- Subtraction x - y print(5-3) 2
* Multiplication x * y print(5*3) 15
/ Division x / y print(12/3) 4
% Modulus x%y print(5%2) 1
** Exponentiation x**y print(2**5) 32
// Floor Division x//y print(12//2) 7

Python Assignment Operators

Assignment operators are used to assign values to variables:

Oprator Example Same as output
= x=5 x=5 5
+= x+=3 x=x+3 8
-= x-=3 x=x-3 2
*= x*=3 x=x*3 15
/= x/=3 x=x/3 1.666
%= x%=3 x=x%3 2
//= x//=3 x=x//3 1
**= x**=3 x=x**3 125
&= x&=3 x=x&3 1
|= x|=3 x=x|3 7
^= x^=3 x=x^3 6
>>= x>>3 x=x>>3 0
<<= x<<3 x=x<<3 40

Python Comparison Operators

Comparison operators are used to compare two values:

Operator Name example values output
== Equal x==y print(5==3) False
!= Not equal x!=y print(5!=3) True
> Greater than x>y print(5>3) True
< Less than x<y print(5<3) False
>= Greater than or equal to x>=y print(5>=3) True
<= Less than or equal to x<=y print(5<=3) False

Python Logical Operators

Logical operators are used to combine conditional statements:

Operator Description eaxample output
and Returns True if both statements are true print(5 > 3 and 5 < 10) True
or Returns True if one of the statements is true print(5 > 3 or 5 < 4) True
not Reverse the result, returns False if the result is true print(not(5 > 3 and 5 < 10)) False

Python Identity Operators

Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location:

Operator Description eaxample output
is Returns true if both variables are the same object

x = ["apple", "banana"]
y = ["apple", "banana"]
z = x

print(x is z)

print(x is y)

print(x == y)

 

 

 


True

False

True

is not Returns true if both variables are not the same object

x = ["apple", "banana"]
y = ["apple", "banana"]
z = x

print(x is not z)

print(x is not y)

print(x != y)

 

 

 


False

True

False


Python Membership Operators

Membership operators are used to test if a sequence is presented in an object:

Operator Description eaxample output
in Returns True if a sequence with the specified value is present in the object

x = ["apple", "banana"]

print("banana" in x)

True
not in Returns True if a sequence with the specified value is not present in the object

x = ["apple", "banana"]

print("pineapple" not in x)

True

Python Bitwise Operators

Bitwise operators are used to compare (binary) numbers:

Operator Name Description
AND Sets each bit to 1 if both bits are 1
| OR Sets each bit to 1 if one of two bits is 1
 ^ XOR Sets each bit to 1 if only one of two bits is 1
NOT Inverts all the bits
<< Zero fill left shift Shift left by pushing zeros in from the right and let the leftmost bits fall off
>> Signed right shift Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off