Python Operators A Comprehensive Guide to Operators in Python


Python Operators  A Comprehensive Guide to Operators in Python

Python Operators  A Comprehensive Guide to Operators in Python
Python Operators  A Comprehensive Guide to Operators in Python


  • Python Operators  A Comprehensive Guide to Operators in Python
  • The term "Operator" refers to symbols or special keywords that perform operations on one or more operands. Operators are used to manipulate data and variables in various ways. 

  • There are several types of operators in Python, including arithmetic operators, comparison operators, logical operators, assignment operators, and more.

Types of Operators:-

NAME OF OPERATOR

SYMBOL

Description

Example

 

 

 

 

 

 

Arithmetic Operators

+

Addition:  Adds two operands

a = 5

b = 3              result=a+b                             output: 8

-

Subtraction:  Subtracts the right operand from the left operand

 a = 7

 b = 4

result=a-b                               Output : 3

*

Multiplication: Multiplies two operands

a = 2

b = 6

result = a * b  

Output: 12

/

Division: Divides the left operand by the right operand Note:  Returns a float

a = 10

b = 2

result = a / b                           Output : 5.0

//

Floor Division: Divides the left operand by the right operand and returns the floor value

a = 10

b = 3

result=a//b                               Output : 3

%

Modulus: Returns the remainder of the division of the left operand by the right operand.

a = 10

b = 3

result = a %b                          Output : 1

**

Exponentiation: Raises the left operand to the power of the right operand.

a = 2

b = 3

result=a**b                                Output:  8


NAME OF OPERATOR

SYMBOL

Description

Example

 

 

Comparison Operators

(Or)

Relational Operators

 

==

Equal: Returns `True` if the operands are equal

   a = 5 

   b = 5

result=a==b                             output: True

!=

Not Equal: Returns `True` if the operands are not equal

 a = 7

 b = 4

 result=a!=b                               Output : True

< 

Less Than: Returns `True` if the left operand is less than the right operand.

a = 3

b = 6

result = a < b  

Output: True

<=

Less Than or Equal To:- Returns `True` if the left operand is less than or equal to the right operand.

a = 4

b = 4

result = a<=b                             Output: True

> 

Greater Than: Returns `True` if the left operand is greater than the right operand.

a = 8

b = 5

result = a>b                               Output: True

>=

Greater Than or Equal To: Returns `True` if the left operand is greater than or equal to the right operand.

 

a = 5

b = 5

result = a >= b                          Output: True


NAME OF OPERATOR

SYMBOL

Description

Example

 

 

Logical Operators

 

and

Logical AND: Returns `True` if both operands are true.

   a = True

   b = False

result= a and b   Output: False                 

or

Logical OR:  Returns `True` if at least one operand is true

 a = True

 b = False

 result = a or b                               Output : True

not

Logical NOT: Returns `True` if the operand is false, and `False` if the operand is true

a=True

result= not a

Output : False




NAME OF OPERATOR

SYMBOL

Description

Example

 

 

Assignment Operators

=

Assignment: Assigns the value on the right to the variable on the left

x = 10

+=

Add and Assign: Adds the right operand to the left operand and assigns the result to the left operand

x = 5

x += 3  # equivalent to      x=x+3,                                   result is x is 8

-=

Subtract and Assign: Subtracts the right operand from the left operand and assigns the result to the left operand

x = 10

x -= 4  # equivalent to x = x - 4,                                Result is x is 6

*=

Multiply and Assign: Multiplies the left operand by the right operand and assigns the result to the left operand

x = 2

x *= 5  # equivalentto x=x*5,                           Result is x is 10

/=

Divide and Assign: Divides the left operand by the right operand and assigns the result to the left operand

x = 8

x /= 2  # equivalent to x = x / 2,                             Result is x is 4.0

//=

Floor Divide and Assign: Floor divides the left operand by the right operand and assigns the result to the left operand

x = 10

x //= 3  # equivalentto x = x // 3,                                    Result is x is 3

%=

Modulus and Assign: Computes the modulus of the left operand by the right operand and assigns the result to the left operand

x = 7

x %= 4  # equivalent to x = x%4,                                       Result is x is 3

**=

Exponentiate and Assign: Raises the left operand to the power of the right operand and assigns the result to the left operand

x = 2

x **= 3  # equivalent to x=x**3,                        result is x is 8



NAME OF OPERATOR

SYMBOL

Description

Example

 

Membership Operators

in

in`: Returns `True` if a value is found in the sequence like list, tuple, string, etc.

my_list = [1, 2, 3, 4, 5]

result =3 in my_list           Result is : True

not in

not in`: Returns `True` if a value is not found in the sequence

my_list = [1, 2, 3, 4, 5]

result=6 not in my_list   

result is: True



NAME OF OPERATOR

SYMBOL

Description

Example

 

Bitwise Operators

&

Bitwise AND: Performs bitwise AND operation on corresponding bits of operands

a = 5  # 101 in binary

b = 3  # 011 in binary

result=a&b                          # result is 1 (001 in binary)

|

Bitwise OR: Performs bitwise OR operation on corresponding bits of operands

a = 5  # 101 in binary

b = 3  # 011 in binary

result = a | b                               # result is 7 (111 in binary)

^

Bitwise XOR: Performs bitwise XOR (exclusive OR) operation on corresponding bits of operands

a = 5  # 101 in binary

b = 3  # 011 in binary

result = a ^ b                           # result is 6 (110 in binary)

~

Bitwise NOT: Inverts the bits of its operand.

a = 5  # 101 in binary

result=~a                                  # result is -6 (-(101 + 1) in binary)

<< 

Left Shift: Shifts the bits of the left operand to the left by a specified number of positions

a = 5  # 101 in binary

result= a << 1                          # result is 10 (1010 in binary)

>> 

Right Shift: Shifts the bits of the left operand to the right by a specified number of positions

a = 5    # 101 in binary

result=a>>1                               # result is 2 (10 in binary)



NAME OF OPERATOR

SYMBOL

Description

Example

 

Ternary Operator

 

`x if condition else y`: Returns `x` if the condition is true; otherwise, it returns `y`.

age = 25

status = "Adult" if age >= 18 else "Minor"



NAME OF OPERATOR

SYMBOL

Description

Example

 

Slice Operator

[:]

[start:stop:step]: Extracts a portion of a sequence (string, list, etc.) specified by the start, stop, and step

my_list = [1, 2, 3, 4, 5]

subset=my_list[1:4]              # subset is [2, 3, 4]



NAME OF OPERATOR

SYMBOL

Description

Example

 

 

Type Conversion Operators

int(x)

 `int(x)`: Converts `x` to an integer

x = "5"

result = int(x)                 # result is 5

float(x)

float(x)`: Converts `x` to a floating-point number

x = "3.14"

result=float(x)                   # result is 3.14

str(x)

str(x)`: Converts `x` to a string

x = 42

result = str(x)               

  # result is "42"

list(x)

list(x)`: Converts `x` to a list

x = (1, 2, 3)

result = list(x)                # result is [1, 2, 3]

tuple(x)

tuple(x)`: Converts `x` to a tuple

x = [4, 5, 6]

result = tuple(x)  # result is (4, 5, 6)


FAQ:

What are arithmetic operators in Python?

Arithmetic operators perform mathematical operations on numeric values. The common ones include `+` (addition), `-` (subtraction), `*` (multiplication), `/` (division), `%` (modulo), and `//` (floor division).

How does the modulo operator `%` work?

The modulo operator returns the remainder of the division of the left operand by the right operand. For example, `7 % 3` equals 1 because 7 divided by 3 leaves a remainder of 1.

Explain the floor division operator `//`

The floor division operator `//` performs division and rounds down the result to the nearest whole number. For example, `7 // 3` equals 2, as it discards the decimal part.

What are comparison operators in Python?

Comparison operators are used to compare values. Common ones include `==` (equal), `!=` (not equal), `<` (less than), `>` (greater than), `<=` (less than or equal to), and `>=` (greater than or equal to).

How does the equality operator `==` work?

The equality operator `==` checks if two values are equal. For example, `5 == 5` evaluates to `True`, while `5 == 7` evaluates to `False`.

Explain the difference between `=` and `==`

The `=` operator is used for assignment, while `==` is the equality operator used for comparison. For example, `x = 5` assigns the value 5 to variable `x`, and `x == 5` checks if `x` is equal to 5.

What are logical operators in Python?

Logical operators perform logical operations on boolean values. Common ones include `and`, `or`, and `not`.

How does the `and` operator work?

The `and` operator returns `True` if both operands are `True`, otherwise, it returns `False`. For example, `True and False` evaluates to `False`.

Explain the `not` operator?

The `not` operator negates the truth value of its operand. If the operand is `True`, `not` makes it `False`, and vice versa.

What are assignment operators in Python?

Assignment operators are used to assign values to variables. The basic one is `=` (assignment), but there are also compound assignment operators like `+=`, `-=`, `*=`, etc.

How does the `+=` operator work?

The `+=` operator adds the right operand to the left operand and assigns the result to the left operand. For example, `x += 3` is equivalent to `x = x + 3`.

Explain the `//=` operator?

The `//=` operator performs floor division and assigns the result to the left operand. For example, `x //= 2` is equivalent to `x = x // 2`.

What are bitwise operators in Python?

Bitwise operators perform operations on binary representations of integers. Common ones include `&` (bitwise AND), `|` (bitwise OR), `^` (bitwise XOR), `~` (bitwise NOT), << (left shift), and >> (right shift).

How does the & operator work?

The `&` operator performs a bitwise AND operation on corresponding bits of two integers. For example, `5 & 3` results in `1` because in binary, `0101 & 0011` equals `0001`.

Explain the << operator?

The << operator shifts the bits of the left operand to the left by the number of positions specified by the right operand. For example, 8 << 2 results in 32 because 8 in binary is `1000`, and shifting it left by 2 positions gives `100000`, which is `32` in decimal.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.