Previous | Next


A variable is basically a name that represents or refers to some value. Assigning a variable in Python is simple. For example, you might want the name w to represent 8. To make it so, simply execute the following:

Arithmetic Operators:

Assuming a = 10 and b = 2

Opertor Description Example
+ Additional
>>> a + b
12
Substraction
>>> a - b
8
x Multiplication
>>> a * b
20
/ Division
>>> a / b
5
** Exponent
>>> a ** b
100

 

Assignment Operators:

Assuming a = 10 and b = 2

Opertor Description Example
= Simple Assignment. Assigne value of a + b into c
>>> c = a + b
+= Add AND assignment operator. Equivalent to c = c + a
>>> c += a
-= Subtract AND assignment operator. Equivalent to c = c – a
>>> c -= a
*= Multiply AND assignment operator. Equivalent to c = c * a
>>> c *= a
/= Division AND assignment operator. Equivalent to c = c / a
>>> c /= a
%= Modulus AND assignment operator. Equivalent to c = c % a
>>> c %= a
**= Exponent AND assignment operator. Equivalent to c = c ** a
>>> c **= a

 

Comparison Operators:

Assuming a = 10 and b = 2

Opertor Description Example
== Checks if a and b are equal or not, if yes then condition becomes true.
>>> a == b
False
!= Checks if a and b are not equal, if yes then condition becomes true.
>>> a != b
True
<> Checks if a and b are equal or not, if values are not equal then condition becomes true.
>>> a <> b
True
< Checks if a is less than b, if yes then condition becomes true.
>>> a < b
False
> Checks if a is greater than b, if yes then condition becomes true.
>>> a > b
True
<= Checks if a is less than or equal to b, if yes then condition becomes true.
>>> a <= b
False
>= Checks if a is greater than or equal to b, if yes then condition becomes true.
>>> a >= b
True

 

Bitwise Operators:

Assuming a = 10 and b = 2 in decimal. The binary value for both value would be:

a = 0000 1010 and b = 0000 0010 in decimal.

Opertor Description Example
& Bitwise AND operator. Copy the bit when corresponding bits in the variables x and y are both equal to 1.
>>> a & b
2
| Bitwise OR operator. Copy the bit when corresponding bits in the variables x or y is equal to 1.
>>> a & b
10
~ Bitwise NOT operator,. Flips the bits of its operand, so 1 becomes 0, and 0 becomes 1. Presented in 2’s complement format.
>>> ~a
-11
^ Bitwise Exclusive OR operator. Produces a 1 if both bits are different, and 0 if they’re the same.
>>> a ^ b
8
>> Bitwise shift right operator. Shift right based on b positions.
>>> a >> b
2
<< Bitwise shift left operator. Shift left based on b positions.
>>> a << b
40

 


Previous | Next