Introduction to Python

Operators

Devesh Kayal , Head Content Creater - Python, DIG-IT.WORLD

Operators in Python are symbols or special characters that perform specific operations on one or more operands (values or variables). Python provides a wide range of operators that enable various computations and manipulations. Here are some commonly used operators in Python:

  1. Arithmetic Operators: addition (+), subtraction (-), multiplication (*), division (/), modulo (%), and exponentiation (**).

  2. Assignment Operators: equals sign (=), but Python also provides compound assignment operators like +=, -=, *=, /=, etc., which combine an arithmetic operation with assignment.

  3. Comparison Operators: They include == (equal to), != (not equal to), < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to).

  4. Logical Operators: Logical operators are used to combine or negate logical conditions. The three logical operators in Python are and, or, and not. They evaluate expressions and return Boolean values based on the logical operations performed.

  5. Bitwise Operators

  6. Membership Operators

The symbols that show a special behaviour or action when applied to operands are called operators. For ex- + , - , > , < etc. • Python supports following operators 

I. Arithmetic Operator 

II. Relation Operator 

III. Logical Operators 

IV. Identity Operators 

V. Bitwise Operators 

VI. Membership Operators

Let us talk about the above highlighted operators. 


 

+ Addition Adds values on either side of the operator. a + b = 30

 

- Subtraction Subtracts right hand operand from left hand operand. a – b = -10

 

* Multiplication Multiplies values on either side of the operator a * b = 200

 

/ Division Divides left hand operand by right hand operand b / a = 2

 

% Modulus Divides left hand operand by right hand operand and returns remainder b % a = 0

 

** Exponent Performs exponential (power) calculation on operators a**b =10 to the power 20

 

// Integer Division - The division of whole numbers where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity) −

9//2 = 4.0 and 9.0//2.0 = 4.0, -11//3 = -4, -11.0//3 = -4.0


 

 




























 

Relation Operators

Relational operators in python are used to compare the operand values on either side. Refer to the list below for the entire list of relational operators in python.

Operator

Name

Description

Syntax

==

Equal To

operand1 == operand2

!=

Not Equal To

operand1!= operand2

>

Greater Than

operand1 > operand2

<

Less Than

operand1 < operand2

>=

Greater Than or Equal To

operand1 >= operand2

<=

Lesser Than or Equal To

operand1 <= operand2




 

Logical Operators

and

Returns True if both statements are true

x < 5 and x < 10

or

Returns True if one of the statements is true

x < 5 or x < 4

not

Reverse the result, returns False if the result is true

not (x < 5 and x < 10)


 

Both Relation and Logical operators are mostly used with If statements .