Python Operators

 Operators in Python

An operator in Python is a special symbol or keyword used to perform operations on variables and values.

Operators are used to perform different types of calculations, comparisons and logical operations in a Python program.

An operator in Python is a symbol that tells the interpreter to perform specific mathematical(+,-,*,/,%), logical(>,<,<=,>=,==,!=), or other operations on variables and values(int a,b). Python supports various types of operators that are essential for performing calculations, comparisons, and operations on data structures. Operators work with operands(the values and variables)and can manipulate or compare them based on the type of operator used.

OPERATORS in Python are special symbols or keywords used to perform operations on variables and values.  Python supports a variety of operators that can be used for different types of operations, such as arithmetic, logical decision, bitwise manipulations, and more. Operators form the foundation of expressions in Python, allowing you to manipulate data in a structured and meaningful way.

An operand is a value or variable on which an operator performs an operation.(e.g.x,y)

Basic Types of Operators:

Unary Operators:

These operate on a single operand(e.g. –x, not x)

Examples of Unary Operators:

Operator Name Description
– Unary Minus Changes the sign of a number
+ Unary Plus Indicates a positive number
not Logical NOT Reverses True or False
~ Bitwise NOT Inverts the bits of a number

Example:

a = 10

 

print(+a)

print(-a)

print(not True)

print(~a)

Output:

10

-10

False

-11

Explanation:

  • +a represents positive 10.
  • -a changes 10 into -10.
  • not True gives False.
  • ~a performs bitwise NOT operation.

 

Binary Operators:

These work with two operands(e.g. a+b,a>b)

Examples of Binary Operators:

Operator Name Example Result
+ Addition 10 + 5 15
– Subtraction 10 – 5 5
* Multiplication 10 * 5 50
/ Division 10 / 5 2.0
% Modulus 10 % 3 1
> Greater than 10 > 5 True
== Equal to 10 == 5 False
and Logical AND True and False False

Example:

a = 20

b = 10

 

print(“Addition =”, a + b)

print(“Subtraction =”, a – b)

print(“Multiplication =”, a * b)

print(“Division =”, a / b)

print(“Greater than =”, a > b)

output:

Addition = 30

Subtraction = 10

Multiplication = 200

Division = 2.0

Greater than = True

Explanation:

Here, a and b are two operands, and the operators perform operations on both values.

 

Ternary Operators:

These involve three operands and are usually used for conditional expressions(e.g. a if condition else b).

In Python, the ternary operator is also known as the Conditional Expression.

It is used to make decisions in a single line of code based on a condition.

value_if_true if condition else value_if_false

Example:

a = 20

b = 10

 

result = “A is greater” if a > b else “B is greater”

 

print(result)

Output:

A is greater

Explanation:

  • a > b is the condition.
  • “A is greater” is returned if the condition is True.
  • “B is greater” is returned if the condition is False.

Another Example: Check Even or Odd

num = 7

 

result = “Even” if num % 2 == 0 else “Odd”

 

print(result)

Output:

Odd

 

Difference Between Unary, Binary and Ternary Operators

Basis Unary Binary Ternary
Meaning Works on one operand Works on two operands Uses three operands
Number of operands One Two Three
Example -a a + b a if condition else b
Purpose Performs operation on one value Performs operation on two values Performs conditional selection

 

 OPERATORS IN PYTHON WITH EXAMPLE:

1.Arithmetic Operators in Python

Arithmetic operators(+,-,%,/*) are used to perform mathematical calculations such as addition, subtraction, multiplication and division. Arithmetic operators work on two operands (values or variables) to produce a result. Therefore, most arithmetic operators are examples of Binary Operators.

Operator Name Example Result
+ Addition 10 + 5 15
– Subtraction 10 – 5 5
* Multiplication 10 * 5 50
/ Division 10 / 5 2.0
% Modulus 10 % 3 1
// Floor Division 10 // 3 3
** Exponentiation 2 ** 3 8
Arithmetic Operator in Python
Arithmetic Operator in Python

2.Relational & Comparison Operators in Python

Relational operators are used to compare two values. They return either True or False.

Operator Meaning Example Result
== Equal to 10 == 5 False
!= Not equal to 10 != 5 True
> Greater than 10 > 5 True
< Less than 10 < 5 False
>= Greater than or equal to 10 >= 10 True

 

Relational & Comparison operator in Python
Relational & Comparison Operators inPython

3. Assignment Operators in Python

Assignment operators are used to assign values to variables. They can also perform calculations and assign the result to the same variable.

Operator Example Equivalent to
= x = 10 x = 10
+= x += 5 x = x + 5
-= x -= 5 x = x – 5
*= x *= 5 x = x * 5
/= x /= 5 x = x / 5
%= x %= 5 x = x % 5
//= x //= 5 x = x // 5
**= x **= 2 x = x ** 2

 

Assignment Operator in Python
Assignment Operator in Python

4. Logical Operators in Python

Logical operators are used to combine two or more conditions. They return True or False.

Python has three logical operators:

Operator Description
and Returns True if both conditions are True.
or Returns True if at least one condition is True.
not Reverses the result.

 

Logical Operator in Python
Logical Operator in Python

5. Bitwise Operators in Python

Bitwise operators are used to perform operations on the binary (0 and 1) representation of numbers.

| Operator | Name | Description |
|————|——-|————–|
| & | AND | Sets bit to 1 if both bits are 1. |
| | | OR | Sets bit to 1 if either bit is 1. |
| ^ | XOR | Sets bit to 1 if bits are different. |
| ~ | NOT | Inverts the bits. |
| << | Left Shift | Shifts bits to the left. |
| >> | Right Shift | Shifts bits to the right. |

Key concept:

BITS: every integer is represented as a series of binary digits(0s and 1s). For example, the number 5 is represented as 0101 in binary (4-bit format)

BITWISE OPERATIONS: These operations are performed on each corresponding bit of 2 numbers.

 

 

 

Bitwise Operator in Python
Bitwise Operator in Python

6. Membership Operators in Python

Membership operators are used to check whether a particular value exists in a sequence such as a list, string or tuple.

There are two membership operators:

  • in: Returns True if the value is present.
  • not in: Returns True if the value is not present.
Membership Operator in Python
Membership Operator in Python

7. Identity Operators in Python

Identity operators are used to check whether two variables refer to the same object in memory.

There are two identity operators:

  • is: Returns True if both variables refer to the same object.
  • is not: Returns True if both variables refer to different objects.
Identity Operator in Python
Identity Operator in Python
  • Difference Between Different Operators

Operator Type Purpose
Arithmetic Performs mathematical calculations
Relational Compares two values
Assignment Assigns values to variables
Logical Combines conditions
Bitwise Performs binary operations
Membership Checks whether a value exists in a sequence
Identity Checks whether two variables refer to the same object

 

Python Casting (Type Conversion in Python)