Python: Operators


Operators in python are the symbols used to perform an operation. Some of the most common operators used in Python are

CategoryOperators
Arithmetic Operators+, -, *, /,%,//
Relational Operators==,!=,<,>,>=,<=
Assignment Operators=,+=,-=,*=,/=,%=
Logical Operatorsand, or , not

You will see example and more detail of all these Python Operators in further sections.

Arithmetic Operators

Here is more detail about the Python supported Arithmetic Operators:

OperatorExplanationExample
+Used for addition operation“+” is used as addition operator where 11+2 is evaluated as 13
Used for the subtraction operation“-” is used as subtraction operator where 11-2 is evaluated as 9, 2-11 is evaluated as -9
*Used for a multiplication operation“*” is used as multiplication operator where 11*2 is evaluated as 22
/Used for division operation“/” is used as division operator where 11/2 is evaluated as 5.5
//Used for integer division operation“//” is used for integer division where 11//2 is evaluated as 5
%Used for the modulo operation, consider the expression num 1%num2 which finds the remainder after dividing num1 by num2“%” is used as modulo operator where 11%2 is evaluated as 1, 9%11 is evaluated as 9

Relational Operators

Here is more detail about the Python supported Relational Operators:

OperatorExplanationExample
==Used for checking the equality of two values/variable10==10 is evaluated as True
100==10 is evaluated as False
!=Used for checking the in-equality of two values/variable10 != 10 is evaluated as False
10 != 100 is evaluated as True
>Used for checking the of num1 is greater than num2 in num1 > num210 > 10 is evaluated as False
100 > 10 is evaluated as True
<Used for checking the of num1 is lesser than num2 in num1 < num210 < 10 is evaluated as False
10 < 100 is evaluated as True
>=Used for checking the of num1 is greater than or equal to num2 in num1 >= num210 >= 10 is evaluated as True
10 >= 100 is evaluated as True
<=Used for checking the of num1 is lesser than or equal to num2 in num1 <= num210 <= 10 is evaluated as True
100 <= 10 is evaluated as False

Assignment Operators

Here is more detail about the Python supported Assignment Operators:

OperatorExplanationExample
=Used for assigning value to a variablenum=5
Here num is assigned with the value 5
+=Used as short hand assignment operator additionnum=num+1 can be represented using short hand assignment operator as num+=1
-=Used as short hand assignment operator subtractionnum=num-1 can be represented using shorthand assignment operator as num-=1
*=Used as short hand assignment operator multiplicationnum=num1 can be represented using shorthand assignment operator as num=1
/=Used as short hand assignment operator divisionnum=num/1 can be represented using shorthand assignment operator as num/=1
%=Used as short hand assignment operator modulo operationnum=num%1 can be represented using short hand assignment operator as num%=1

Logical Operator

Here is more detail about the Python supported Logical Operators:

OperatorsDescription
ANDThe result will be true if both the expressions are true. If any one or both the expressions are false, the result will be false.
ORThe result will be true, even if one of the expressions is true. If both the expressions are false, the result will be false.
NOTIf the expression is true, the result will be false and vice versa.

If A and B are two relational expressions, say A = (Num1>2000), B= (Num2>100), the result of combining A and B using logical operator is based on the result of A and B as shown below:

You can also try all these operators in problem mentioned in Python Exercises.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s