Python Basic Operators
In
this lecture, we will master all about basic operators and their expressions in
python. Besides these will learn about the different types of operators in
python and how to use them.
Python
Operators are unique symbols that perform some sort of computation. The objects
or values on which operators act are known as operands in
python. Any statement that contains operators and operands is called an expression.
X = 50
Y = 20
Z = X + Y -10
print(Z)
Output:
60
In
the above example, Z = X + Y -10 is
an expression with + and - as its operators, and Z, X, Y&10 are
the operands.
What
are the different types of operators in python?
The
different types of operators in python are listed below.
· Arithmetic Operators
· Comparison Operators
· Logical Operators
· Assignment Operators
· Identity Operators
· Membership Operators
Arithmetic
Operators
Arithmetic
Operators in python are used for mathematical calculations like Addition,
Subtraction, Multiplication, Division, Modulus, etc.
Operator |
Meaning |
Description |
Example |
+ |
Addition |
Adds
two operands or unary plus |
10+2=12 |
- |
Subtraction |
Subtracts
right operand from left operand or unary minus |
10-2=8 |
* |
Multiplication |
Multiplies
two operands |
10*2=20 |
/ |
Division |
Divides
left operand by right operand |
10/2=5 |
% |
Modulus |
Remainder
after division |
10%2=0 |
** |
Exponentiation |
Performs
the exponential calculation |
10**2=100 |
// |
Floor
Division |
Only
considers the integer part of the quotient while excluding the values after
the decimal point. |
10//2=5 10//-4=-3 |
Note: In-floor , when the result is negative then it floors
to the next smallest integer value.
Some
of the arithmetic operators and their calculations are given in the below
example.
X = 10
Y = 2
print('X+Y=',X+Y)
print('X-Y=',X-Y)
print('X*Y=',X*Y)
print('X/Y=',X/Y)
print('X%Y=',X%Y)
print('X**Y=',X**Y)
print('X//Y=',X//Y)
Output:
X+Y=
12
X-Y=
8
X*Y=
20
X/Y=
5.0
X%Y=
0
X**Y=
100
X//Y=
5
Note: The result (quotient) of division is always
represented in the float.
Comparison
Operators
As
the name indicates, Comparison operators also known as Relational
operators compare the two operands and return either True (if the
condition met )or False as result in python.
Operator |
Meaning |
Description |
Examples |
== |
Equal to |
Returns True if two operands are
equal |
a==b |
!= |
Not Equal to |
Returns True if two operands are
not equal |
a!=b |
> |
Greater than |
Returns True if left operands is
greater than the right |
a>b |
< |
Less than |
Returns True if the left operand
is less than the right |
a |
>= |
Greater than or equal to |
Returns True if left operands is
greater than or equal to the right |
a>=b |
<= |
Less than or equal to |
Returns True if the left operand
is less than or equal to the right |
a<=b |
Example: Comparison Operators
a = 10
b = 2
print('a==b is',a==b)
print('a!=b is',a!=b)
print('a>b is',a>b)
print('a=b is',a>=b)
print('a<=b is',a<=b)
Output:
a==b
is False
a!=b
is True
a>b
is True
a=b
is True
a<=b
is False
Note: Comparison operators are usually used to control the
flow of the program.
Logical
Operators
Logical
operators are used to evaluating two expressions or statements. Three basic
logic operators are and, or and not. They are typically used to
control the flow of the program in a boolean context similar to comparison
operators. Boolean values are either True(T)
or False(F).
Operator |
Meaning |
Description |
Example |
and |
Logical AND/ Conjunction |
Returns True if and only if both
statements are true |
X and Y |
or |
Logical OR / Disjunction |
Returns True if any of the
statement is true |
X or Y |
not |
Logical NOT/ Negation |
Returns true if the operand is a
negation |
not X |
Here
is the example:
x=12
print('x>5 and x<10 is',x>5
and x<10)
print('x>5 or x<10 is',x>5
or x<10)
print('not x<10 is', not x<10)
Output:
x>5
and x<10 is False
x>5
or x<10 is True
not
x<10 is True
Bitwise
Operators
A
bitwise operator is used to manipulate bit-level data. Common logical operators
like arithmetic operators, comparison operators, etc work on bytes while
bitwise operators work on bits in a byte. Common bitwise operators are listed
in the below table. The binary representation of 3 is 0000 0011 and that of 4
is 0000 0100.
Operators |
Meaning |
Description |
Example |
& |
Binary AND |
The result is 1 if both operands
are true otherwise 0 |
3&4 =0 |
| |
Binary OR |
The result is 1if anyone operand
is true otherwise 0 |
3|4=7 |
^ |
Binary XOR |
The result is 1 if and only if
both operands are true else 0 |
3^4=7 |
~ |
Binary One's Complement |
The result is the negation of the
operand |
~3=-4 |
<< |
Binary Left Shift |
Aligns the bits to the left |
12 |
>> |
Binary Right Shift |
Aligns the bits to the right |
0 |
Example
of bitwise operators:
a=3
b=4
print('a&b is',a&b)
print('a|b is',a|b)
print('a^b is',a^b)
print('~a is',~a)
print('a<<2 is',a<<2)
print('a>>2 is',a>>2)
Output:
a&b
is 0
a|b
is 7
a^b
is 7
~a
is -4
a<<2
is 12
a>>2
is 0
Where
do we use bitwise operators?
Bitwise
operations are not common in the real world as they are typically used for
low-level operations. Low-level operations use the binary format of 0 and 1 to
manipulate data. Listed below are some of the areas in which bitwise operators
are used.
· Primarily used in embedded system
· Data Compression - WinRAR,zip
· Encryption
· Networking
· Hardware manipulation
· Graphics
Assignment
Operators
Assignment
operators as studied in previous tutorials assigns a value to the variables.
For instance, a = 10 assigns value 10 to the variable a. Some compound assignment operators
are available in python. They take the expression a+=10 which is equivalent to a= a+10. So let’s learn about some
assignment operators which are tabulated below.
Operators |
Example |
Meaning |
= |
a = 10 |
|
+= |
a+=10 |
a=a+10 |
-= |
a-=10 |
a=a-10 |
*= |
a*=10 |
a=a*10 |
/= |
a/=10 |
a=a/10 |
%= |
a%=10 |
a=a |
//= |
a//=10 |
a=a//10 |
**= |
a**=10 |
a=a**10 |
&= |
a&=10 |
a=a&10 |
|= |
a|=10 |
a=a|10 |
^= |
a^=10 |
a=a^10 |
>>= |
a>>=10 |
a=a>>10 |
<<= |
a<<=10 |
a=a<<10 |
Membership
Operators
From
the previous tutorial, we are familiar with Membership operators. Membership
operators in python are used for validating the existence of a member in a
sequence like string, list, tuple, set, and Dictionary.
The
two membership operators in python are :
· in: Returns True if the member exists in the sequence
· Not in Returns True if the member does not
exist in the sequence.
Example of Membership operator
A = {1,3,5,7,8}
print(3 in A)
print(8 not in A)
Output:
True
False
Identity
Operators
Identity
operators in python are used to compare the memory location of two variables.
The result will be true if the memory locations are the same for two variables.
The
two identity operators in python are:
· is: True if operands point to the same objects or same
location
· is not: False if operands point to the same objects or same
location
Example of Identity Operator:
x1=10
x2=10
y1=y2='OP'
x1=[1,2,3]
print('x1 is x2 evaluates to',x1 is
x2)
print('y1 is not x2 evaluates to',x1
is not x2)
print('x1 is x1 evaluates to',x1 is
x1)
Output:
x1
is x2 evaluates to False
y1
is not x2 evaluates to True
x1
is x1 evaluates to True
Operators
Precedence in Python
Operator
precedence refers to the order of operators in which they evaluate an expression.
The list below provides the operator precedence in python.
Order |
Arithmetic Operators |
Logical Operators |
Relational Operators |
1 |
Exponentiation (**) |
not |
Equality(==) |
2 |
Complement () unary plus, unary
minus |
and |
Inequality(<>) |
3 |
Multiplication(*) ,Division(/) |
or |
Less than(<) |
4 |
Floor Division(\) |
|
Greater than(>) |
5 |
Modulus(%) |
|
Less than or equal to(<=) |
6 |
Addition(+), Subtraction(-) |
|
Greater than or equal to (>=) |