0% found this document useful (0 votes)
21 views177 pages

In Pytho1

Uploaded by

ajay.replit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views177 pages

In Pytho1

Uploaded by

ajay.replit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 177

In Python, "ops" typically refers to operators, which are symbols or

keywords used to perform operations on variables or values. Python provides


a wide range of operators to manipulate data. These operators can be
classified into several types based on the operation they perform:

1. Arithmetic Operators

These operators are used to perform mathematical operations like addition,


subtraction, multiplication, etc.

Operat Exampl
Description
or e

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division (float) x/y

// Floor Division x // y

Modulus
% x%y
(remainder)

Exponentiation
** x ** y
(power)

Examples:

python

Copy code

x = 10

y=3

print(x + y) # 13

print(x - y) # 7

print(x * y) # 30

print(x / y) # 3.3333333333333335

print(x // y) # 3 (floor division)


print(x % y) # 1 (remainder)

print(x ** y) # 1000 (10 raised to the power of 3)

2. Assignment Operators

Assignment operators are used to assign values to variables.

Operat Exampl
Description
or e

Assigns value to a
= x = 10
variable

+= Adds and assigns x += 3

-= Subtracts and assigns x -= 3

*= Multiplies and assigns x *= 3

/= Divides and assigns x /= 3

Floor divides and


//= x //= 3
assigns

%= Modulus and assigns x %= 3

Exponentiates and
**= x **= 3
assigns

Examples:

python

Copy code

x=5

x += 2 # x = x + 2 -> x becomes 7

x *= 3 # x = x * 3 -> x becomes 21

print(x) # Output: 21

3. Comparison Operators
These operators are used to compare two values. The result of a comparison
is always a boolean (True or False).

Operat Exampl
Description
or e

== Equal to x == y

!= Not equal to x != y

> Greater than x>y

< Less than x<y

Greater than or
>= x >= y
equal to

Less than or equal


<= x <= y
to

Examples:

python

Copy code

x = 10

y=5

print(x == y) # False

print(x != y) # True

print(x > y) # True

print(x <= y) # False

4. Logical Operators

Logical operators are used to perform logical operations on boolean values.

Operat Exampl
Description
or e

and True if both are True x and y


Operat Exampl
Description
or e

True if at least one is


or x or y
True

not Reverses the result not x

Examples:

python

Copy code

x = True

y = False

print(x and y) # False

print(x or y) # True

print(not x) # False

5. Bitwise Operators

Bitwise operators perform operations on the binary representations of


integers.

Operat
Description Example
or

& Bitwise AND x&y

Bitwise
` `
OR

Bitwise XOR (exclusive


^ x^y
OR)

Bitwise NOT
~ ~x
(inversion)

Left shift (shift bits


<< x << y
left)
Operat
Description Example
or

Right shift (shift bits


>> x >> y
right)

Examples:

python

Copy code

x = 5 # 101 in binary

y = 3 # 011 in binary

print(x & y) # 1 (binary 001)

print(x | y) # 7 (binary 111)

print(x ^ y) # 6 (binary 110)

print(~x) # -6 (inversion: all bits flipped)

print(x << 1) # 10 (binary 1010, shifted left by 1 bit)

print(x >> 1) # 2 (binary 10, shifted right by 1 bit)

6. Membership Operators

Membership operators are used to check whether a value exists in a


sequence like a list, tuple, string, etc.

Operat Exampl
Description
or e

in Returns True if a value exists in the sequence x in y

Returns True if a value does not exist in the x not in


not in
sequence y

Examples:

python

Copy code
lst = [1, 2, 3, 4]

print(2 in lst) # True

print(5 not in lst) # True

7. Identity Operators

Identity operators are used to compare the memory locations of two objects.

Operat Exampl
Description
or e

Returns True if both variables point to the


is x is y
same object

Returns True if both variables point to x is not


is not
different objects y

Examples:

python

Copy code

x = [1, 2, 3]

y = [1, 2, 3]

z=x

print(x is z) # True (z points to the same object as x)

print(x is y) # False (x and y are different objects, even though their


contents are the same)

print(x is not y) # True

8. Ternary Operator (Conditional Expression)

The ternary operator is a compact form of conditional statements. It’s used


to assign a value to a variable based on a condition.
Syntax:

python

Copy code

value_if_true if condition else value_if_false

Example:

python

Copy code

x = 10

y = 20

max_value = x if x > y else y

print(max_value) # Output: 20

9. Operator Precedence

In Python, operators have a predefined precedence (order of evaluation).


This defines which operators are evaluated first in an expression.

Precedence Table (from highest to lowest):

1. ** (Exponentiation)

2. ~, +, - (Unary operators)

3. *, /, //, % (Multiplication, Division, Floor Division, Modulus)

4. +, - (Addition, Subtraction)

5. <<, >> (Bitwise shift operators)

6. & (Bitwise AND)

7. ^ (Bitwise XOR)

8. | (Bitwise OR)

9. ==, !=, >, >=, <, <= (Comparison operators)

10. not (Logical NOT)


11. and (Logical AND)

12. or (Logical OR)

13. = and compound assignment operators (+=, -=, etc.)

Operator Overloading

Python allows you to change the behavior of its operators based on user-
defined data types by implementing special methods (also known as dunder
methods or magic methods).

Example of Operator Overloading:

python

Copy code

class Point:

def __init__(self, x, y):

self.x = x

self.y = y

# Overloading the '+' operator

def __add__(self, other):

return Point(self.x + other.x, self.y + other.y)

def __str__(self):

return f"({self.x}, {self.y})"

p1 = Point(1, 2)

p2 = Point(3, 4)

print(p1 + p2) # Output: (4, 6)


In Python, "ops" typically refers to operators, which are symbols or
keywords used to perform operations on variables or values. Python provides
a wide range of operators to manipulate data. These operators can be
classified into several types based on the operation they perform:

1. Arithmetic Operators

These operators are used to perform mathematical operations like addition,


subtraction, multiplication, etc.

Operat Exampl
Description
or e

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division (float) x/y

// Floor Division x // y

Modulus
% x%y
(remainder)

Exponentiation
** x ** y
(power)

Examples:

python

Copy code

x = 10

y=3

print(x + y) # 13

print(x - y) # 7

print(x * y) # 30

print(x / y) # 3.3333333333333335

print(x // y) # 3 (floor division)


print(x % y) # 1 (remainder)

print(x ** y) # 1000 (10 raised to the power of 3)

2. Assignment Operators

Assignment operators are used to assign values to variables.

Operat Exampl
Description
or e

Assigns value to a
= x = 10
variable

+= Adds and assigns x += 3

-= Subtracts and assigns x -= 3

*= Multiplies and assigns x *= 3

/= Divides and assigns x /= 3

Floor divides and


//= x //= 3
assigns

%= Modulus and assigns x %= 3

Exponentiates and
**= x **= 3
assigns

Examples:

python

Copy code

x=5

x += 2 # x = x + 2 -> x becomes 7

x *= 3 # x = x * 3 -> x becomes 21

print(x) # Output: 21

3. Comparison Operators
These operators are used to compare two values. The result of a comparison
is always a boolean (True or False).

Operat Exampl
Description
or e

== Equal to x == y

!= Not equal to x != y

> Greater than x>y

< Less than x<y

Greater than or
>= x >= y
equal to

Less than or equal


<= x <= y
to

Examples:

python

Copy code

x = 10

y=5

print(x == y) # False

print(x != y) # True

print(x > y) # True

print(x <= y) # False

4. Logical Operators

Logical operators are used to perform logical operations on boolean values.

Operat Exampl
Description
or e

and True if both are True x and y


Operat Exampl
Description
or e

True if at least one is


or x or y
True

not Reverses the result not x

Examples:

python

Copy code

x = True

y = False

print(x and y) # False

print(x or y) # True

print(not x) # False

5. Bitwise Operators

Bitwise operators perform operations on the binary representations of


integers.

Operat
Description Example
or

& Bitwise AND x&y

Bitwise
` `
OR

Bitwise XOR (exclusive


^ x^y
OR)

Bitwise NOT
~ ~x
(inversion)

Left shift (shift bits


<< x << y
left)
Operat
Description Example
or

Right shift (shift bits


>> x >> y
right)

Examples:

python

Copy code

x = 5 # 101 in binary

y = 3 # 011 in binary

print(x & y) # 1 (binary 001)

print(x | y) # 7 (binary 111)

print(x ^ y) # 6 (binary 110)

print(~x) # -6 (inversion: all bits flipped)

print(x << 1) # 10 (binary 1010, shifted left by 1 bit)

print(x >> 1) # 2 (binary 10, shifted right by 1 bit)

6. Membership Operators

Membership operators are used to check whether a value exists in a


sequence like a list, tuple, string, etc.

Operat Exampl
Description
or e

in Returns True if a value exists in the sequence x in y

Returns True if a value does not exist in the x not in


not in
sequence y

Examples:

python

Copy code
lst = [1, 2, 3, 4]

print(2 in lst) # True

print(5 not in lst) # True

7. Identity Operators

Identity operators are used to compare the memory locations of two objects.

Operat Exampl
Description
or e

Returns True if both variables point to the


is x is y
same object

Returns True if both variables point to x is not


is not
different objects y

Examples:

python

Copy code

x = [1, 2, 3]

y = [1, 2, 3]

z=x

print(x is z) # True (z points to the same object as x)

print(x is y) # False (x and y are different objects, even though their


contents are the same)

print(x is not y) # True

8. Ternary Operator (Conditional Expression)

The ternary operator is a compact form of conditional statements. It’s used


to assign a value to a variable based on a condition.
Syntax:

python

Copy code

value_if_true if condition else value_if_false

Example:

python

Copy code

x = 10

y = 20

max_value = x if x > y else y

print(max_value) # Output: 20

9. Operator Precedence

In Python, operators have a predefined precedence (order of evaluation).


This defines which operators are evaluated first in an expression.

Precedence Table (from highest to lowest):

1. ** (Exponentiation)

2. ~, +, - (Unary operators)

3. *, /, //, % (Multiplication, Division, Floor Division, Modulus)

4. +, - (Addition, Subtraction)

5. <<, >> (Bitwise shift operators)

6. & (Bitwise AND)

7. ^ (Bitwise XOR)

8. | (Bitwise OR)

9. ==, !=, >, >=, <, <= (Comparison operators)

10. not (Logical NOT)


11. and (Logical AND)

12. or (Logical OR)

13. = and compound assignment operators (+=, -=, etc.)

Operator Overloading

Python allows you to change the behavior of its operators based on user-
defined data types by implementing special methods (also known as dunder
methods or magic methods).

Example of Operator Overloading:

python

Copy code

class Point:

def __init__(self, x, y):

self.x = x

self.y = y

# Overloading the '+' operator

def __add__(self, other):

return Point(self.x + other.x, self.y + other.y)

def __str__(self):

return f"({self.x}, {self.y})"

p1 = Point(1, 2)

p2 = Point(3, 4)

print(p1 + p2) # Output: (4, 6)


In Python, "ops" typically refers to operators, which are symbols or
keywords used to perform operations on variables or values. Python provides
a wide range of operators to manipulate data. These operators can be
classified into several types based on the operation they perform:

1. Arithmetic Operators

These operators are used to perform mathematical operations like addition,


subtraction, multiplication, etc.

Operat Exampl
Description
or e

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division (float) x/y

// Floor Division x // y

Modulus
% x%y
(remainder)

Exponentiation
** x ** y
(power)

Examples:

python

Copy code

x = 10

y=3

print(x + y) # 13

print(x - y) # 7

print(x * y) # 30

print(x / y) # 3.3333333333333335

print(x // y) # 3 (floor division)


print(x % y) # 1 (remainder)

print(x ** y) # 1000 (10 raised to the power of 3)

2. Assignment Operators

Assignment operators are used to assign values to variables.

Operat Exampl
Description
or e

Assigns value to a
= x = 10
variable

+= Adds and assigns x += 3

-= Subtracts and assigns x -= 3

*= Multiplies and assigns x *= 3

/= Divides and assigns x /= 3

Floor divides and


//= x //= 3
assigns

%= Modulus and assigns x %= 3

Exponentiates and
**= x **= 3
assigns

Examples:

python

Copy code

x=5

x += 2 # x = x + 2 -> x becomes 7

x *= 3 # x = x * 3 -> x becomes 21

print(x) # Output: 21

3. Comparison Operators
These operators are used to compare two values. The result of a comparison
is always a boolean (True or False).

Operat Exampl
Description
or e

== Equal to x == y

!= Not equal to x != y

> Greater than x>y

< Less than x<y

Greater than or
>= x >= y
equal to

Less than or equal


<= x <= y
to

Examples:

python

Copy code

x = 10

y=5

print(x == y) # False

print(x != y) # True

print(x > y) # True

print(x <= y) # False

4. Logical Operators

Logical operators are used to perform logical operations on boolean values.

Operat Exampl
Description
or e

and True if both are True x and y


Operat Exampl
Description
or e

True if at least one is


or x or y
True

not Reverses the result not x

Examples:

python

Copy code

x = True

y = False

print(x and y) # False

print(x or y) # True

print(not x) # False

5. Bitwise Operators

Bitwise operators perform operations on the binary representations of


integers.

Operat
Description Example
or

& Bitwise AND x&y

Bitwise
` `
OR

Bitwise XOR (exclusive


^ x^y
OR)

Bitwise NOT
~ ~x
(inversion)

Left shift (shift bits


<< x << y
left)
Operat
Description Example
or

Right shift (shift bits


>> x >> y
right)

Examples:

python

Copy code

x = 5 # 101 in binary

y = 3 # 011 in binary

print(x & y) # 1 (binary 001)

print(x | y) # 7 (binary 111)

print(x ^ y) # 6 (binary 110)

print(~x) # -6 (inversion: all bits flipped)

print(x << 1) # 10 (binary 1010, shifted left by 1 bit)

print(x >> 1) # 2 (binary 10, shifted right by 1 bit)

6. Membership Operators

Membership operators are used to check whether a value exists in a


sequence like a list, tuple, string, etc.

Operat Exampl
Description
or e

in Returns True if a value exists in the sequence x in y

Returns True if a value does not exist in the x not in


not in
sequence y

Examples:

python

Copy code
lst = [1, 2, 3, 4]

print(2 in lst) # True

print(5 not in lst) # True

7. Identity Operators

Identity operators are used to compare the memory locations of two objects.

Operat Exampl
Description
or e

Returns True if both variables point to the


is x is y
same object

Returns True if both variables point to x is not


is not
different objects y

Examples:

python

Copy code

x = [1, 2, 3]

y = [1, 2, 3]

z=x

print(x is z) # True (z points to the same object as x)

print(x is y) # False (x and y are different objects, even though their


contents are the same)

print(x is not y) # True

8. Ternary Operator (Conditional Expression)

The ternary operator is a compact form of conditional statements. It’s used


to assign a value to a variable based on a condition.
Syntax:

python

Copy code

value_if_true if condition else value_if_false

Example:

python

Copy code

x = 10

y = 20

max_value = x if x > y else y

print(max_value) # Output: 20

9. Operator Precedence

In Python, operators have a predefined precedence (order of evaluation).


This defines which operators are evaluated first in an expression.

Precedence Table (from highest to lowest):

1. ** (Exponentiation)

2. ~, +, - (Unary operators)

3. *, /, //, % (Multiplication, Division, Floor Division, Modulus)

4. +, - (Addition, Subtraction)

5. <<, >> (Bitwise shift operators)

6. & (Bitwise AND)

7. ^ (Bitwise XOR)

8. | (Bitwise OR)

9. ==, !=, >, >=, <, <= (Comparison operators)

10. not (Logical NOT)


11. and (Logical AND)

12. or (Logical OR)

13. = and compound assignment operators (+=, -=, etc.)

Operator Overloading

Python allows you to change the behavior of its operators based on user-
defined data types by implementing special methods (also known as dunder
methods or magic methods).

Example of Operator Overloading:

python

Copy code

class Point:

def __init__(self, x, y):

self.x = x

self.y = y

# Overloading the '+' operator

def __add__(self, other):

return Point(self.x + other.x, self.y + other.y)

def __str__(self):

return f"({self.x}, {self.y})"

p1 = Point(1, 2)

p2 = Point(3, 4)

print(p1 + p2) # Output: (4, 6)


In Python, "ops" typically refers to operators, which are symbols or
keywords used to perform operations on variables or values. Python provides
a wide range of operators to manipulate data. These operators can be
classified into several types based on the operation they perform:

1. Arithmetic Operators

These operators are used to perform mathematical operations like addition,


subtraction, multiplication, etc.

Operat Exampl
Description
or e

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division (float) x/y

// Floor Division x // y

Modulus
% x%y
(remainder)

Exponentiation
** x ** y
(power)

Examples:

python

Copy code

x = 10

y=3

print(x + y) # 13

print(x - y) # 7

print(x * y) # 30

print(x / y) # 3.3333333333333335

print(x // y) # 3 (floor division)


print(x % y) # 1 (remainder)

print(x ** y) # 1000 (10 raised to the power of 3)

2. Assignment Operators

Assignment operators are used to assign values to variables.

Operat Exampl
Description
or e

Assigns value to a
= x = 10
variable

+= Adds and assigns x += 3

-= Subtracts and assigns x -= 3

*= Multiplies and assigns x *= 3

/= Divides and assigns x /= 3

Floor divides and


//= x //= 3
assigns

%= Modulus and assigns x %= 3

Exponentiates and
**= x **= 3
assigns

Examples:

python

Copy code

x=5

x += 2 # x = x + 2 -> x becomes 7

x *= 3 # x = x * 3 -> x becomes 21

print(x) # Output: 21

3. Comparison Operators
These operators are used to compare two values. The result of a comparison
is always a boolean (True or False).

Operat Exampl
Description
or e

== Equal to x == y

!= Not equal to x != y

> Greater than x>y

< Less than x<y

Greater than or
>= x >= y
equal to

Less than or equal


<= x <= y
to

Examples:

python

Copy code

x = 10

y=5

print(x == y) # False

print(x != y) # True

print(x > y) # True

print(x <= y) # False

4. Logical Operators

Logical operators are used to perform logical operations on boolean values.

Operat Exampl
Description
or e

and True if both are True x and y


Operat Exampl
Description
or e

True if at least one is


or x or y
True

not Reverses the result not x

Examples:

python

Copy code

x = True

y = False

print(x and y) # False

print(x or y) # True

print(not x) # False

5. Bitwise Operators

Bitwise operators perform operations on the binary representations of


integers.

Operat
Description Example
or

& Bitwise AND x&y

Bitwise
` `
OR

Bitwise XOR (exclusive


^ x^y
OR)

Bitwise NOT
~ ~x
(inversion)

Left shift (shift bits


<< x << y
left)
Operat
Description Example
or

Right shift (shift bits


>> x >> y
right)

Examples:

python

Copy code

x = 5 # 101 in binary

y = 3 # 011 in binary

print(x & y) # 1 (binary 001)

print(x | y) # 7 (binary 111)

print(x ^ y) # 6 (binary 110)

print(~x) # -6 (inversion: all bits flipped)

print(x << 1) # 10 (binary 1010, shifted left by 1 bit)

print(x >> 1) # 2 (binary 10, shifted right by 1 bit)

6. Membership Operators

Membership operators are used to check whether a value exists in a


sequence like a list, tuple, string, etc.

Operat Exampl
Description
or e

in Returns True if a value exists in the sequence x in y

Returns True if a value does not exist in the x not in


not in
sequence y

Examples:

python

Copy code
lst = [1, 2, 3, 4]

print(2 in lst) # True

print(5 not in lst) # True

7. Identity Operators

Identity operators are used to compare the memory locations of two objects.

Operat Exampl
Description
or e

Returns True if both variables point to the


is x is y
same object

Returns True if both variables point to x is not


is not
different objects y

Examples:

python

Copy code

x = [1, 2, 3]

y = [1, 2, 3]

z=x

print(x is z) # True (z points to the same object as x)

print(x is y) # False (x and y are different objects, even though their


contents are the same)

print(x is not y) # True

8. Ternary Operator (Conditional Expression)

The ternary operator is a compact form of conditional statements. It’s used


to assign a value to a variable based on a condition.
Syntax:

python

Copy code

value_if_true if condition else value_if_false

Example:

python

Copy code

x = 10

y = 20

max_value = x if x > y else y

print(max_value) # Output: 20

9. Operator Precedence

In Python, operators have a predefined precedence (order of evaluation).


This defines which operators are evaluated first in an expression.

Precedence Table (from highest to lowest):

1. ** (Exponentiation)

2. ~, +, - (Unary operators)

3. *, /, //, % (Multiplication, Division, Floor Division, Modulus)

4. +, - (Addition, Subtraction)

5. <<, >> (Bitwise shift operators)

6. & (Bitwise AND)

7. ^ (Bitwise XOR)

8. | (Bitwise OR)

9. ==, !=, >, >=, <, <= (Comparison operators)

10. not (Logical NOT)


11. and (Logical AND)

12. or (Logical OR)

13. = and compound assignment operators (+=, -=, etc.)

Operator Overloading

Python allows you to change the behavior of its operators based on user-
defined data types by implementing special methods (also known as dunder
methods or magic methods).

Example of Operator Overloading:

python

Copy code

class Point:

def __init__(self, x, y):

self.x = x

self.y = y

# Overloading the '+' operator

def __add__(self, other):

return Point(self.x + other.x, self.y + other.y)

def __str__(self):

return f"({self.x}, {self.y})"

p1 = Point(1, 2)

p2 = Point(3, 4)

print(p1 + p2) # Output: (4, 6)


In Python, "ops" typically refers to operators, which are symbols or
keywords used to perform operations on variables or values. Python provides
a wide range of operators to manipulate data. These operators can be
classified into several types based on the operation they perform:

1. Arithmetic Operators

These operators are used to perform mathematical operations like addition,


subtraction, multiplication, etc.

Operat Exampl
Description
or e

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division (float) x/y

// Floor Division x // y

Modulus
% x%y
(remainder)

Exponentiation
** x ** y
(power)

Examples:

python

Copy code

x = 10

y=3

print(x + y) # 13

print(x - y) # 7

print(x * y) # 30

print(x / y) # 3.3333333333333335

print(x // y) # 3 (floor division)


print(x % y) # 1 (remainder)

print(x ** y) # 1000 (10 raised to the power of 3)

2. Assignment Operators

Assignment operators are used to assign values to variables.

Operat Exampl
Description
or e

Assigns value to a
= x = 10
variable

+= Adds and assigns x += 3

-= Subtracts and assigns x -= 3

*= Multiplies and assigns x *= 3

/= Divides and assigns x /= 3

Floor divides and


//= x //= 3
assigns

%= Modulus and assigns x %= 3

Exponentiates and
**= x **= 3
assigns

Examples:

python

Copy code

x=5

x += 2 # x = x + 2 -> x becomes 7

x *= 3 # x = x * 3 -> x becomes 21

print(x) # Output: 21

3. Comparison Operators
These operators are used to compare two values. The result of a comparison
is always a boolean (True or False).

Operat Exampl
Description
or e

== Equal to x == y

!= Not equal to x != y

> Greater than x>y

< Less than x<y

Greater than or
>= x >= y
equal to

Less than or equal


<= x <= y
to

Examples:

python

Copy code

x = 10

y=5

print(x == y) # False

print(x != y) # True

print(x > y) # True

print(x <= y) # False

4. Logical Operators

Logical operators are used to perform logical operations on boolean values.

Operat Exampl
Description
or e

and True if both are True x and y


Operat Exampl
Description
or e

True if at least one is


or x or y
True

not Reverses the result not x

Examples:

python

Copy code

x = True

y = False

print(x and y) # False

print(x or y) # True

print(not x) # False

5. Bitwise Operators

Bitwise operators perform operations on the binary representations of


integers.

Operat
Description Example
or

& Bitwise AND x&y

Bitwise
` `
OR

Bitwise XOR (exclusive


^ x^y
OR)

Bitwise NOT
~ ~x
(inversion)

Left shift (shift bits


<< x << y
left)
Operat
Description Example
or

Right shift (shift bits


>> x >> y
right)

Examples:

python

Copy code

x = 5 # 101 in binary

y = 3 # 011 in binary

print(x & y) # 1 (binary 001)

print(x | y) # 7 (binary 111)

print(x ^ y) # 6 (binary 110)

print(~x) # -6 (inversion: all bits flipped)

print(x << 1) # 10 (binary 1010, shifted left by 1 bit)

print(x >> 1) # 2 (binary 10, shifted right by 1 bit)

6. Membership Operators

Membership operators are used to check whether a value exists in a


sequence like a list, tuple, string, etc.

Operat Exampl
Description
or e

in Returns True if a value exists in the sequence x in y

Returns True if a value does not exist in the x not in


not in
sequence y

Examples:

python

Copy code
lst = [1, 2, 3, 4]

print(2 in lst) # True

print(5 not in lst) # True

7. Identity Operators

Identity operators are used to compare the memory locations of two objects.

Operat Exampl
Description
or e

Returns True if both variables point to the


is x is y
same object

Returns True if both variables point to x is not


is not
different objects y

Examples:

python

Copy code

x = [1, 2, 3]

y = [1, 2, 3]

z=x

print(x is z) # True (z points to the same object as x)

print(x is y) # False (x and y are different objects, even though their


contents are the same)

print(x is not y) # True

8. Ternary Operator (Conditional Expression)

The ternary operator is a compact form of conditional statements. It’s used


to assign a value to a variable based on a condition.
Syntax:

python

Copy code

value_if_true if condition else value_if_false

Example:

python

Copy code

x = 10

y = 20

max_value = x if x > y else y

print(max_value) # Output: 20

9. Operator Precedence

In Python, operators have a predefined precedence (order of evaluation).


This defines which operators are evaluated first in an expression.

Precedence Table (from highest to lowest):

1. ** (Exponentiation)

2. ~, +, - (Unary operators)

3. *, /, //, % (Multiplication, Division, Floor Division, Modulus)

4. +, - (Addition, Subtraction)

5. <<, >> (Bitwise shift operators)

6. & (Bitwise AND)

7. ^ (Bitwise XOR)

8. | (Bitwise OR)

9. ==, !=, >, >=, <, <= (Comparison operators)

10. not (Logical NOT)


11. and (Logical AND)

12. or (Logical OR)

13. = and compound assignment operators (+=, -=, etc.)

Operator Overloading

Python allows you to change the behavior of its operators based on user-
defined data types by implementing special methods (also known as dunder
methods or magic methods).

Example of Operator Overloading:

python

Copy code

class Point:

def __init__(self, x, y):

self.x = x

self.y = y

# Overloading the '+' operator

def __add__(self, other):

return Point(self.x + other.x, self.y + other.y)

def __str__(self):

return f"({self.x}, {self.y})"

p1 = Point(1, 2)

p2 = Point(3, 4)

print(p1 + p2) # Output: (4, 6)


In Python, "ops" typically refers to operators, which are symbols or
keywords used to perform operations on variables or values. Python provides
a wide range of operators to manipulate data. These operators can be
classified into several types based on the operation they perform:

1. Arithmetic Operators

These operators are used to perform mathematical operations like addition,


subtraction, multiplication, etc.

Operat Exampl
Description
or e

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division (float) x/y

// Floor Division x // y

Modulus
% x%y
(remainder)

Exponentiation
** x ** y
(power)

Examples:

python

Copy code

x = 10

y=3

print(x + y) # 13

print(x - y) # 7

print(x * y) # 30

print(x / y) # 3.3333333333333335

print(x // y) # 3 (floor division)


print(x % y) # 1 (remainder)

print(x ** y) # 1000 (10 raised to the power of 3)

2. Assignment Operators

Assignment operators are used to assign values to variables.

Operat Exampl
Description
or e

Assigns value to a
= x = 10
variable

+= Adds and assigns x += 3

-= Subtracts and assigns x -= 3

*= Multiplies and assigns x *= 3

/= Divides and assigns x /= 3

Floor divides and


//= x //= 3
assigns

%= Modulus and assigns x %= 3

Exponentiates and
**= x **= 3
assigns

Examples:

python

Copy code

x=5

x += 2 # x = x + 2 -> x becomes 7

x *= 3 # x = x * 3 -> x becomes 21

print(x) # Output: 21

3. Comparison Operators
These operators are used to compare two values. The result of a comparison
is always a boolean (True or False).

Operat Exampl
Description
or e

== Equal to x == y

!= Not equal to x != y

> Greater than x>y

< Less than x<y

Greater than or
>= x >= y
equal to

Less than or equal


<= x <= y
to

Examples:

python

Copy code

x = 10

y=5

print(x == y) # False

print(x != y) # True

print(x > y) # True

print(x <= y) # False

4. Logical Operators

Logical operators are used to perform logical operations on boolean values.

Operat Exampl
Description
or e

and True if both are True x and y


Operat Exampl
Description
or e

True if at least one is


or x or y
True

not Reverses the result not x

Examples:

python

Copy code

x = True

y = False

print(x and y) # False

print(x or y) # True

print(not x) # False

5. Bitwise Operators

Bitwise operators perform operations on the binary representations of


integers.

Operat
Description Example
or

& Bitwise AND x&y

Bitwise
` `
OR

Bitwise XOR (exclusive


^ x^y
OR)

Bitwise NOT
~ ~x
(inversion)

Left shift (shift bits


<< x << y
left)
Operat
Description Example
or

Right shift (shift bits


>> x >> y
right)

Examples:

python

Copy code

x = 5 # 101 in binary

y = 3 # 011 in binary

print(x & y) # 1 (binary 001)

print(x | y) # 7 (binary 111)

print(x ^ y) # 6 (binary 110)

print(~x) # -6 (inversion: all bits flipped)

print(x << 1) # 10 (binary 1010, shifted left by 1 bit)

print(x >> 1) # 2 (binary 10, shifted right by 1 bit)

6. Membership Operators

Membership operators are used to check whether a value exists in a


sequence like a list, tuple, string, etc.

Operat Exampl
Description
or e

in Returns True if a value exists in the sequence x in y

Returns True if a value does not exist in the x not in


not in
sequence y

Examples:

python

Copy code
lst = [1, 2, 3, 4]

print(2 in lst) # True

print(5 not in lst) # True

7. Identity Operators

Identity operators are used to compare the memory locations of two objects.

Operat Exampl
Description
or e

Returns True if both variables point to the


is x is y
same object

Returns True if both variables point to x is not


is not
different objects y

Examples:

python

Copy code

x = [1, 2, 3]

y = [1, 2, 3]

z=x

print(x is z) # True (z points to the same object as x)

print(x is y) # False (x and y are different objects, even though their


contents are the same)

print(x is not y) # True

8. Ternary Operator (Conditional Expression)

The ternary operator is a compact form of conditional statements. It’s used


to assign a value to a variable based on a condition.
Syntax:

python

Copy code

value_if_true if condition else value_if_false

Example:

python

Copy code

x = 10

y = 20

max_value = x if x > y else y

print(max_value) # Output: 20

9. Operator Precedence

In Python, operators have a predefined precedence (order of evaluation).


This defines which operators are evaluated first in an expression.

Precedence Table (from highest to lowest):

1. ** (Exponentiation)

2. ~, +, - (Unary operators)

3. *, /, //, % (Multiplication, Division, Floor Division, Modulus)

4. +, - (Addition, Subtraction)

5. <<, >> (Bitwise shift operators)

6. & (Bitwise AND)

7. ^ (Bitwise XOR)

8. | (Bitwise OR)

9. ==, !=, >, >=, <, <= (Comparison operators)

10. not (Logical NOT)


11. and (Logical AND)

12. or (Logical OR)

13. = and compound assignment operators (+=, -=, etc.)

Operator Overloading

Python allows you to change the behavior of its operators based on user-
defined data types by implementing special methods (also known as dunder
methods or magic methods).

Example of Operator Overloading:

python

Copy code

class Point:

def __init__(self, x, y):

self.x = x

self.y = y

# Overloading the '+' operator

def __add__(self, other):

return Point(self.x + other.x, self.y + other.y)

def __str__(self):

return f"({self.x}, {self.y})"

p1 = Point(1, 2)

p2 = Point(3, 4)

print(p1 + p2) # Output: (4, 6)


In Python, "ops" typically refers to operators, which are symbols or
keywords used to perform operations on variables or values. Python provides
a wide range of operators to manipulate data. These operators can be
classified into several types based on the operation they perform:

1. Arithmetic Operators

These operators are used to perform mathematical operations like addition,


subtraction, multiplication, etc.

Operat Exampl
Description
or e

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division (float) x/y

// Floor Division x // y

Modulus
% x%y
(remainder)

Exponentiation
** x ** y
(power)

Examples:

python

Copy code

x = 10

y=3

print(x + y) # 13

print(x - y) # 7

print(x * y) # 30

print(x / y) # 3.3333333333333335

print(x // y) # 3 (floor division)


print(x % y) # 1 (remainder)

print(x ** y) # 1000 (10 raised to the power of 3)

2. Assignment Operators

Assignment operators are used to assign values to variables.

Operat Exampl
Description
or e

Assigns value to a
= x = 10
variable

+= Adds and assigns x += 3

-= Subtracts and assigns x -= 3

*= Multiplies and assigns x *= 3

/= Divides and assigns x /= 3

Floor divides and


//= x //= 3
assigns

%= Modulus and assigns x %= 3

Exponentiates and
**= x **= 3
assigns

Examples:

python

Copy code

x=5

x += 2 # x = x + 2 -> x becomes 7

x *= 3 # x = x * 3 -> x becomes 21

print(x) # Output: 21

3. Comparison Operators
These operators are used to compare two values. The result of a comparison
is always a boolean (True or False).

Operat Exampl
Description
or e

== Equal to x == y

!= Not equal to x != y

> Greater than x>y

< Less than x<y

Greater than or
>= x >= y
equal to

Less than or equal


<= x <= y
to

Examples:

python

Copy code

x = 10

y=5

print(x == y) # False

print(x != y) # True

print(x > y) # True

print(x <= y) # False

4. Logical Operators

Logical operators are used to perform logical operations on boolean values.

Operat Exampl
Description
or e

and True if both are True x and y


Operat Exampl
Description
or e

True if at least one is


or x or y
True

not Reverses the result not x

Examples:

python

Copy code

x = True

y = False

print(x and y) # False

print(x or y) # True

print(not x) # False

5. Bitwise Operators

Bitwise operators perform operations on the binary representations of


integers.

Operat
Description Example
or

& Bitwise AND x&y

Bitwise
` `
OR

Bitwise XOR (exclusive


^ x^y
OR)

Bitwise NOT
~ ~x
(inversion)

Left shift (shift bits


<< x << y
left)
Operat
Description Example
or

Right shift (shift bits


>> x >> y
right)

Examples:

python

Copy code

x = 5 # 101 in binary

y = 3 # 011 in binary

print(x & y) # 1 (binary 001)

print(x | y) # 7 (binary 111)

print(x ^ y) # 6 (binary 110)

print(~x) # -6 (inversion: all bits flipped)

print(x << 1) # 10 (binary 1010, shifted left by 1 bit)

print(x >> 1) # 2 (binary 10, shifted right by 1 bit)

6. Membership Operators

Membership operators are used to check whether a value exists in a


sequence like a list, tuple, string, etc.

Operat Exampl
Description
or e

in Returns True if a value exists in the sequence x in y

Returns True if a value does not exist in the x not in


not in
sequence y

Examples:

python

Copy code
lst = [1, 2, 3, 4]

print(2 in lst) # True

print(5 not in lst) # True

7. Identity Operators

Identity operators are used to compare the memory locations of two objects.

Operat Exampl
Description
or e

Returns True if both variables point to the


is x is y
same object

Returns True if both variables point to x is not


is not
different objects y

Examples:

python

Copy code

x = [1, 2, 3]

y = [1, 2, 3]

z=x

print(x is z) # True (z points to the same object as x)

print(x is y) # False (x and y are different objects, even though their


contents are the same)

print(x is not y) # True

8. Ternary Operator (Conditional Expression)

The ternary operator is a compact form of conditional statements. It’s used


to assign a value to a variable based on a condition.
Syntax:

python

Copy code

value_if_true if condition else value_if_false

Example:

python

Copy code

x = 10

y = 20

max_value = x if x > y else y

print(max_value) # Output: 20

9. Operator Precedence

In Python, operators have a predefined precedence (order of evaluation).


This defines which operators are evaluated first in an expression.

Precedence Table (from highest to lowest):

1. ** (Exponentiation)

2. ~, +, - (Unary operators)

3. *, /, //, % (Multiplication, Division, Floor Division, Modulus)

4. +, - (Addition, Subtraction)

5. <<, >> (Bitwise shift operators)

6. & (Bitwise AND)

7. ^ (Bitwise XOR)

8. | (Bitwise OR)

9. ==, !=, >, >=, <, <= (Comparison operators)

10. not (Logical NOT)


11. and (Logical AND)

12. or (Logical OR)

13. = and compound assignment operators (+=, -=, etc.)

Operator Overloading

Python allows you to change the behavior of its operators based on user-
defined data types by implementing special methods (also known as dunder
methods or magic methods).

Example of Operator Overloading:

python

Copy code

class Point:

def __init__(self, x, y):

self.x = x

self.y = y

# Overloading the '+' operator

def __add__(self, other):

return Point(self.x + other.x, self.y + other.y)

def __str__(self):

return f"({self.x}, {self.y})"

p1 = Point(1, 2)

p2 = Point(3, 4)

print(p1 + p2) # Output: (4, 6)


In Python, "ops" typically refers to operators, which are symbols or
keywords used to perform operations on variables or values. Python provides
a wide range of operators to manipulate data. These operators can be
classified into several types based on the operation they perform:

1. Arithmetic Operators

These operators are used to perform mathematical operations like addition,


subtraction, multiplication, etc.

Operat Exampl
Description
or e

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division (float) x/y

// Floor Division x // y

Modulus
% x%y
(remainder)

Exponentiation
** x ** y
(power)

Examples:

python

Copy code

x = 10

y=3

print(x + y) # 13

print(x - y) # 7

print(x * y) # 30

print(x / y) # 3.3333333333333335

print(x // y) # 3 (floor division)


print(x % y) # 1 (remainder)

print(x ** y) # 1000 (10 raised to the power of 3)

2. Assignment Operators

Assignment operators are used to assign values to variables.

Operat Exampl
Description
or e

Assigns value to a
= x = 10
variable

+= Adds and assigns x += 3

-= Subtracts and assigns x -= 3

*= Multiplies and assigns x *= 3

/= Divides and assigns x /= 3

Floor divides and


//= x //= 3
assigns

%= Modulus and assigns x %= 3

Exponentiates and
**= x **= 3
assigns

Examples:

python

Copy code

x=5

x += 2 # x = x + 2 -> x becomes 7

x *= 3 # x = x * 3 -> x becomes 21

print(x) # Output: 21

3. Comparison Operators
These operators are used to compare two values. The result of a comparison
is always a boolean (True or False).

Operat Exampl
Description
or e

== Equal to x == y

!= Not equal to x != y

> Greater than x>y

< Less than x<y

Greater than or
>= x >= y
equal to

Less than or equal


<= x <= y
to

Examples:

python

Copy code

x = 10

y=5

print(x == y) # False

print(x != y) # True

print(x > y) # True

print(x <= y) # False

4. Logical Operators

Logical operators are used to perform logical operations on boolean values.

Operat Exampl
Description
or e

and True if both are True x and y


Operat Exampl
Description
or e

True if at least one is


or x or y
True

not Reverses the result not x

Examples:

python

Copy code

x = True

y = False

print(x and y) # False

print(x or y) # True

print(not x) # False

5. Bitwise Operators

Bitwise operators perform operations on the binary representations of


integers.

Operat
Description Example
or

& Bitwise AND x&y

Bitwise
` `
OR

Bitwise XOR (exclusive


^ x^y
OR)

Bitwise NOT
~ ~x
(inversion)

Left shift (shift bits


<< x << y
left)
Operat
Description Example
or

Right shift (shift bits


>> x >> y
right)

Examples:

python

Copy code

x = 5 # 101 in binary

y = 3 # 011 in binary

print(x & y) # 1 (binary 001)

print(x | y) # 7 (binary 111)

print(x ^ y) # 6 (binary 110)

print(~x) # -6 (inversion: all bits flipped)

print(x << 1) # 10 (binary 1010, shifted left by 1 bit)

print(x >> 1) # 2 (binary 10, shifted right by 1 bit)

6. Membership Operators

Membership operators are used to check whether a value exists in a


sequence like a list, tuple, string, etc.

Operat Exampl
Description
or e

in Returns True if a value exists in the sequence x in y

Returns True if a value does not exist in the x not in


not in
sequence y

Examples:

python

Copy code
lst = [1, 2, 3, 4]

print(2 in lst) # True

print(5 not in lst) # True

7. Identity Operators

Identity operators are used to compare the memory locations of two objects.

Operat Exampl
Description
or e

Returns True if both variables point to the


is x is y
same object

Returns True if both variables point to x is not


is not
different objects y

Examples:

python

Copy code

x = [1, 2, 3]

y = [1, 2, 3]

z=x

print(x is z) # True (z points to the same object as x)

print(x is y) # False (x and y are different objects, even though their


contents are the same)

print(x is not y) # True

8. Ternary Operator (Conditional Expression)

The ternary operator is a compact form of conditional statements. It’s used


to assign a value to a variable based on a condition.
Syntax:

python

Copy code

value_if_true if condition else value_if_false

Example:

python

Copy code

x = 10

y = 20

max_value = x if x > y else y

print(max_value) # Output: 20

9. Operator Precedence

In Python, operators have a predefined precedence (order of evaluation).


This defines which operators are evaluated first in an expression.

Precedence Table (from highest to lowest):

1. ** (Exponentiation)

2. ~, +, - (Unary operators)

3. *, /, //, % (Multiplication, Division, Floor Division, Modulus)

4. +, - (Addition, Subtraction)

5. <<, >> (Bitwise shift operators)

6. & (Bitwise AND)

7. ^ (Bitwise XOR)

8. | (Bitwise OR)

9. ==, !=, >, >=, <, <= (Comparison operators)

10. not (Logical NOT)


11. and (Logical AND)

12. or (Logical OR)

13. = and compound assignment operators (+=, -=, etc.)

Operator Overloading

Python allows you to change the behavior of its operators based on user-
defined data types by implementing special methods (also known as dunder
methods or magic methods).

Example of Operator Overloading:

python

Copy code

class Point:

def __init__(self, x, y):

self.x = x

self.y = y

# Overloading the '+' operator

def __add__(self, other):

return Point(self.x + other.x, self.y + other.y)

def __str__(self):

return f"({self.x}, {self.y})"

p1 = Point(1, 2)

p2 = Point(3, 4)

print(p1 + p2) # Output: (4, 6)


In Python, "ops" typically refers to operators, which are symbols or
keywords used to perform operations on variables or values. Python provides
a wide range of operators to manipulate data. These operators can be
classified into several types based on the operation they perform:

1. Arithmetic Operators

These operators are used to perform mathematical operations like addition,


subtraction, multiplication, etc.

Operat Exampl
Description
or e

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division (float) x/y

// Floor Division x // y

Modulus
% x%y
(remainder)

Exponentiation
** x ** y
(power)

Examples:

python

Copy code

x = 10

y=3

print(x + y) # 13

print(x - y) # 7

print(x * y) # 30

print(x / y) # 3.3333333333333335

print(x // y) # 3 (floor division)


print(x % y) # 1 (remainder)

print(x ** y) # 1000 (10 raised to the power of 3)

2. Assignment Operators

Assignment operators are used to assign values to variables.

Operat Exampl
Description
or e

Assigns value to a
= x = 10
variable

+= Adds and assigns x += 3

-= Subtracts and assigns x -= 3

*= Multiplies and assigns x *= 3

/= Divides and assigns x /= 3

Floor divides and


//= x //= 3
assigns

%= Modulus and assigns x %= 3

Exponentiates and
**= x **= 3
assigns

Examples:

python

Copy code

x=5

x += 2 # x = x + 2 -> x becomes 7

x *= 3 # x = x * 3 -> x becomes 21

print(x) # Output: 21

3. Comparison Operators
These operators are used to compare two values. The result of a comparison
is always a boolean (True or False).

Operat Exampl
Description
or e

== Equal to x == y

!= Not equal to x != y

> Greater than x>y

< Less than x<y

Greater than or
>= x >= y
equal to

Less than or equal


<= x <= y
to

Examples:

python

Copy code

x = 10

y=5

print(x == y) # False

print(x != y) # True

print(x > y) # True

print(x <= y) # False

4. Logical Operators

Logical operators are used to perform logical operations on boolean values.

Operat Exampl
Description
or e

and True if both are True x and y


Operat Exampl
Description
or e

True if at least one is


or x or y
True

not Reverses the result not x

Examples:

python

Copy code

x = True

y = False

print(x and y) # False

print(x or y) # True

print(not x) # False

5. Bitwise Operators

Bitwise operators perform operations on the binary representations of


integers.

Operat
Description Example
or

& Bitwise AND x&y

Bitwise
` `
OR

Bitwise XOR (exclusive


^ x^y
OR)

Bitwise NOT
~ ~x
(inversion)

Left shift (shift bits


<< x << y
left)
Operat
Description Example
or

Right shift (shift bits


>> x >> y
right)

Examples:

python

Copy code

x = 5 # 101 in binary

y = 3 # 011 in binary

print(x & y) # 1 (binary 001)

print(x | y) # 7 (binary 111)

print(x ^ y) # 6 (binary 110)

print(~x) # -6 (inversion: all bits flipped)

print(x << 1) # 10 (binary 1010, shifted left by 1 bit)

print(x >> 1) # 2 (binary 10, shifted right by 1 bit)

6. Membership Operators

Membership operators are used to check whether a value exists in a


sequence like a list, tuple, string, etc.

Operat Exampl
Description
or e

in Returns True if a value exists in the sequence x in y

Returns True if a value does not exist in the x not in


not in
sequence y

Examples:

python

Copy code
lst = [1, 2, 3, 4]

print(2 in lst) # True

print(5 not in lst) # True

7. Identity Operators

Identity operators are used to compare the memory locations of two objects.

Operat Exampl
Description
or e

Returns True if both variables point to the


is x is y
same object

Returns True if both variables point to x is not


is not
different objects y

Examples:

python

Copy code

x = [1, 2, 3]

y = [1, 2, 3]

z=x

print(x is z) # True (z points to the same object as x)

print(x is y) # False (x and y are different objects, even though their


contents are the same)

print(x is not y) # True

8. Ternary Operator (Conditional Expression)

The ternary operator is a compact form of conditional statements. It’s used


to assign a value to a variable based on a condition.
Syntax:

python

Copy code

value_if_true if condition else value_if_false

Example:

python

Copy code

x = 10

y = 20

max_value = x if x > y else y

print(max_value) # Output: 20

9. Operator Precedence

In Python, operators have a predefined precedence (order of evaluation).


This defines which operators are evaluated first in an expression.

Precedence Table (from highest to lowest):

1. ** (Exponentiation)

2. ~, +, - (Unary operators)

3. *, /, //, % (Multiplication, Division, Floor Division, Modulus)

4. +, - (Addition, Subtraction)

5. <<, >> (Bitwise shift operators)

6. & (Bitwise AND)

7. ^ (Bitwise XOR)

8. | (Bitwise OR)

9. ==, !=, >, >=, <, <= (Comparison operators)

10. not (Logical NOT)


11. and (Logical AND)

12. or (Logical OR)

13. = and compound assignment operators (+=, -=, etc.)

Operator Overloading

Python allows you to change the behavior of its operators based on user-
defined data types by implementing special methods (also known as dunder
methods or magic methods).

Example of Operator Overloading:

python

Copy code

class Point:

def __init__(self, x, y):

self.x = x

self.y = y

# Overloading the '+' operator

def __add__(self, other):

return Point(self.x + other.x, self.y + other.y)

def __str__(self):

return f"({self.x}, {self.y})"

p1 = Point(1, 2)

p2 = Point(3, 4)

print(p1 + p2) # Output: (4, 6)


In Python, "ops" typically refers to operators, which are symbols or
keywords used to perform operations on variables or values. Python provides
a wide range of operators to manipulate data. These operators can be
classified into several types based on the operation they perform:

1. Arithmetic Operators

These operators are used to perform mathematical operations like addition,


subtraction, multiplication, etc.

Operat Exampl
Description
or e

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division (float) x/y

// Floor Division x // y

Modulus
% x%y
(remainder)

Exponentiation
** x ** y
(power)

Examples:

python

Copy code

x = 10

y=3

print(x + y) # 13

print(x - y) # 7

print(x * y) # 30

print(x / y) # 3.3333333333333335

print(x // y) # 3 (floor division)


print(x % y) # 1 (remainder)

print(x ** y) # 1000 (10 raised to the power of 3)

2. Assignment Operators

Assignment operators are used to assign values to variables.

Operat Exampl
Description
or e

Assigns value to a
= x = 10
variable

+= Adds and assigns x += 3

-= Subtracts and assigns x -= 3

*= Multiplies and assigns x *= 3

/= Divides and assigns x /= 3

Floor divides and


//= x //= 3
assigns

%= Modulus and assigns x %= 3

Exponentiates and
**= x **= 3
assigns

Examples:

python

Copy code

x=5

x += 2 # x = x + 2 -> x becomes 7

x *= 3 # x = x * 3 -> x becomes 21

print(x) # Output: 21

3. Comparison Operators
These operators are used to compare two values. The result of a comparison
is always a boolean (True or False).

Operat Exampl
Description
or e

== Equal to x == y

!= Not equal to x != y

> Greater than x>y

< Less than x<y

Greater than or
>= x >= y
equal to

Less than or equal


<= x <= y
to

Examples:

python

Copy code

x = 10

y=5

print(x == y) # False

print(x != y) # True

print(x > y) # True

print(x <= y) # False

4. Logical Operators

Logical operators are used to perform logical operations on boolean values.

Operat Exampl
Description
or e

and True if both are True x and y


Operat Exampl
Description
or e

True if at least one is


or x or y
True

not Reverses the result not x

Examples:

python

Copy code

x = True

y = False

print(x and y) # False

print(x or y) # True

print(not x) # False

5. Bitwise Operators

Bitwise operators perform operations on the binary representations of


integers.

Operat
Description Example
or

& Bitwise AND x&y

Bitwise
` `
OR

Bitwise XOR (exclusive


^ x^y
OR)

Bitwise NOT
~ ~x
(inversion)

Left shift (shift bits


<< x << y
left)
Operat
Description Example
or

Right shift (shift bits


>> x >> y
right)

Examples:

python

Copy code

x = 5 # 101 in binary

y = 3 # 011 in binary

print(x & y) # 1 (binary 001)

print(x | y) # 7 (binary 111)

print(x ^ y) # 6 (binary 110)

print(~x) # -6 (inversion: all bits flipped)

print(x << 1) # 10 (binary 1010, shifted left by 1 bit)

print(x >> 1) # 2 (binary 10, shifted right by 1 bit)

6. Membership Operators

Membership operators are used to check whether a value exists in a


sequence like a list, tuple, string, etc.

Operat Exampl
Description
or e

in Returns True if a value exists in the sequence x in y

Returns True if a value does not exist in the x not in


not in
sequence y

Examples:

python

Copy code
lst = [1, 2, 3, 4]

print(2 in lst) # True

print(5 not in lst) # True

7. Identity Operators

Identity operators are used to compare the memory locations of two objects.

Operat Exampl
Description
or e

Returns True if both variables point to the


is x is y
same object

Returns True if both variables point to x is not


is not
different objects y

Examples:

python

Copy code

x = [1, 2, 3]

y = [1, 2, 3]

z=x

print(x is z) # True (z points to the same object as x)

print(x is y) # False (x and y are different objects, even though their


contents are the same)

print(x is not y) # True

8. Ternary Operator (Conditional Expression)

The ternary operator is a compact form of conditional statements. It’s used


to assign a value to a variable based on a condition.
Syntax:

python

Copy code

value_if_true if condition else value_if_false

Example:

python

Copy code

x = 10

y = 20

max_value = x if x > y else y

print(max_value) # Output: 20

9. Operator Precedence

In Python, operators have a predefined precedence (order of evaluation).


This defines which operators are evaluated first in an expression.

Precedence Table (from highest to lowest):

1. ** (Exponentiation)

2. ~, +, - (Unary operators)

3. *, /, //, % (Multiplication, Division, Floor Division, Modulus)

4. +, - (Addition, Subtraction)

5. <<, >> (Bitwise shift operators)

6. & (Bitwise AND)

7. ^ (Bitwise XOR)

8. | (Bitwise OR)

9. ==, !=, >, >=, <, <= (Comparison operators)

10. not (Logical NOT)


11. and (Logical AND)

12. or (Logical OR)

13. = and compound assignment operators (+=, -=, etc.)

Operator Overloading

Python allows you to change the behavior of its operators based on user-
defined data types by implementing special methods (also known as dunder
methods or magic methods).

Example of Operator Overloading:

python

Copy code

class Point:

def __init__(self, x, y):

self.x = x

self.y = y

# Overloading the '+' operator

def __add__(self, other):

return Point(self.x + other.x, self.y + other.y)

def __str__(self):

return f"({self.x}, {self.y})"

p1 = Point(1, 2)

p2 = Point(3, 4)

print(p1 + p2) # Output: (4, 6)


In Python, "ops" typically refers to operators, which are symbols or
keywords used to perform operations on variables or values. Python provides
a wide range of operators to manipulate data. These operators can be
classified into several types based on the operation they perform:

1. Arithmetic Operators

These operators are used to perform mathematical operations like addition,


subtraction, multiplication, etc.

Operat Exampl
Description
or e

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division (float) x/y

// Floor Division x // y

Modulus
% x%y
(remainder)

Exponentiation
** x ** y
(power)

Examples:

python

Copy code

x = 10

y=3

print(x + y) # 13

print(x - y) # 7

print(x * y) # 30

print(x / y) # 3.3333333333333335

print(x // y) # 3 (floor division)


print(x % y) # 1 (remainder)

print(x ** y) # 1000 (10 raised to the power of 3)

2. Assignment Operators

Assignment operators are used to assign values to variables.

Operat Exampl
Description
or e

Assigns value to a
= x = 10
variable

+= Adds and assigns x += 3

-= Subtracts and assigns x -= 3

*= Multiplies and assigns x *= 3

/= Divides and assigns x /= 3

Floor divides and


//= x //= 3
assigns

%= Modulus and assigns x %= 3

Exponentiates and
**= x **= 3
assigns

Examples:

python

Copy code

x=5

x += 2 # x = x + 2 -> x becomes 7

x *= 3 # x = x * 3 -> x becomes 21

print(x) # Output: 21

3. Comparison Operators
These operators are used to compare two values. The result of a comparison
is always a boolean (True or False).

Operat Exampl
Description
or e

== Equal to x == y

!= Not equal to x != y

> Greater than x>y

< Less than x<y

Greater than or
>= x >= y
equal to

Less than or equal


<= x <= y
to

Examples:

python

Copy code

x = 10

y=5

print(x == y) # False

print(x != y) # True

print(x > y) # True

print(x <= y) # False

4. Logical Operators

Logical operators are used to perform logical operations on boolean values.

Operat Exampl
Description
or e

and True if both are True x and y


Operat Exampl
Description
or e

True if at least one is


or x or y
True

not Reverses the result not x

Examples:

python

Copy code

x = True

y = False

print(x and y) # False

print(x or y) # True

print(not x) # False

5. Bitwise Operators

Bitwise operators perform operations on the binary representations of


integers.

Operat
Description Example
or

& Bitwise AND x&y

Bitwise
` `
OR

Bitwise XOR (exclusive


^ x^y
OR)

Bitwise NOT
~ ~x
(inversion)

Left shift (shift bits


<< x << y
left)
Operat
Description Example
or

Right shift (shift bits


>> x >> y
right)

Examples:

python

Copy code

x = 5 # 101 in binary

y = 3 # 011 in binary

print(x & y) # 1 (binary 001)

print(x | y) # 7 (binary 111)

print(x ^ y) # 6 (binary 110)

print(~x) # -6 (inversion: all bits flipped)

print(x << 1) # 10 (binary 1010, shifted left by 1 bit)

print(x >> 1) # 2 (binary 10, shifted right by 1 bit)

6. Membership Operators

Membership operators are used to check whether a value exists in a


sequence like a list, tuple, string, etc.

Operat Exampl
Description
or e

in Returns True if a value exists in the sequence x in y

Returns True if a value does not exist in the x not in


not in
sequence y

Examples:

python

Copy code
lst = [1, 2, 3, 4]

print(2 in lst) # True

print(5 not in lst) # True

7. Identity Operators

Identity operators are used to compare the memory locations of two objects.

Operat Exampl
Description
or e

Returns True if both variables point to the


is x is y
same object

Returns True if both variables point to x is not


is not
different objects y

Examples:

python

Copy code

x = [1, 2, 3]

y = [1, 2, 3]

z=x

print(x is z) # True (z points to the same object as x)

print(x is y) # False (x and y are different objects, even though their


contents are the same)

print(x is not y) # True

8. Ternary Operator (Conditional Expression)

The ternary operator is a compact form of conditional statements. It’s used


to assign a value to a variable based on a condition.
Syntax:

python

Copy code

value_if_true if condition else value_if_false

Example:

python

Copy code

x = 10

y = 20

max_value = x if x > y else y

print(max_value) # Output: 20

9. Operator Precedence

In Python, operators have a predefined precedence (order of evaluation).


This defines which operators are evaluated first in an expression.

Precedence Table (from highest to lowest):

1. ** (Exponentiation)

2. ~, +, - (Unary operators)

3. *, /, //, % (Multiplication, Division, Floor Division, Modulus)

4. +, - (Addition, Subtraction)

5. <<, >> (Bitwise shift operators)

6. & (Bitwise AND)

7. ^ (Bitwise XOR)

8. | (Bitwise OR)

9. ==, !=, >, >=, <, <= (Comparison operators)

10. not (Logical NOT)


11. and (Logical AND)

12. or (Logical OR)

13. = and compound assignment operators (+=, -=, etc.)

Operator Overloading

Python allows you to change the behavior of its operators based on user-
defined data types by implementing special methods (also known as dunder
methods or magic methods).

Example of Operator Overloading:

python

Copy code

class Point:

def __init__(self, x, y):

self.x = x

self.y = y

# Overloading the '+' operator

def __add__(self, other):

return Point(self.x + other.x, self.y + other.y)

def __str__(self):

return f"({self.x}, {self.y})"

p1 = Point(1, 2)

p2 = Point(3, 4)

print(p1 + p2) # Output: (4, 6)


In Python, "ops" typically refers to operators, which are symbols or
keywords used to perform operations on variables or values. Python provides
a wide range of operators to manipulate data. These operators can be
classified into several types based on the operation they perform:

1. Arithmetic Operators

These operators are used to perform mathematical operations like addition,


subtraction, multiplication, etc.

Operat Exampl
Description
or e

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division (float) x/y

// Floor Division x // y

Modulus
% x%y
(remainder)

Exponentiation
** x ** y
(power)

Examples:

python

Copy code

x = 10

y=3

print(x + y) # 13

print(x - y) # 7

print(x * y) # 30

print(x / y) # 3.3333333333333335

print(x // y) # 3 (floor division)


print(x % y) # 1 (remainder)

print(x ** y) # 1000 (10 raised to the power of 3)

2. Assignment Operators

Assignment operators are used to assign values to variables.

Operat Exampl
Description
or e

Assigns value to a
= x = 10
variable

+= Adds and assigns x += 3

-= Subtracts and assigns x -= 3

*= Multiplies and assigns x *= 3

/= Divides and assigns x /= 3

Floor divides and


//= x //= 3
assigns

%= Modulus and assigns x %= 3

Exponentiates and
**= x **= 3
assigns

Examples:

python

Copy code

x=5

x += 2 # x = x + 2 -> x becomes 7

x *= 3 # x = x * 3 -> x becomes 21

print(x) # Output: 21

3. Comparison Operators
These operators are used to compare two values. The result of a comparison
is always a boolean (True or False).

Operat Exampl
Description
or e

== Equal to x == y

!= Not equal to x != y

> Greater than x>y

< Less than x<y

Greater than or
>= x >= y
equal to

Less than or equal


<= x <= y
to

Examples:

python

Copy code

x = 10

y=5

print(x == y) # False

print(x != y) # True

print(x > y) # True

print(x <= y) # False

4. Logical Operators

Logical operators are used to perform logical operations on boolean values.

Operat Exampl
Description
or e

and True if both are True x and y


Operat Exampl
Description
or e

True if at least one is


or x or y
True

not Reverses the result not x

Examples:

python

Copy code

x = True

y = False

print(x and y) # False

print(x or y) # True

print(not x) # False

5. Bitwise Operators

Bitwise operators perform operations on the binary representations of


integers.

Operat
Description Example
or

& Bitwise AND x&y

Bitwise
` `
OR

Bitwise XOR (exclusive


^ x^y
OR)

Bitwise NOT
~ ~x
(inversion)

Left shift (shift bits


<< x << y
left)
Operat
Description Example
or

Right shift (shift bits


>> x >> y
right)

Examples:

python

Copy code

x = 5 # 101 in binary

y = 3 # 011 in binary

print(x & y) # 1 (binary 001)

print(x | y) # 7 (binary 111)

print(x ^ y) # 6 (binary 110)

print(~x) # -6 (inversion: all bits flipped)

print(x << 1) # 10 (binary 1010, shifted left by 1 bit)

print(x >> 1) # 2 (binary 10, shifted right by 1 bit)

6. Membership Operators

Membership operators are used to check whether a value exists in a


sequence like a list, tuple, string, etc.

Operat Exampl
Description
or e

in Returns True if a value exists in the sequence x in y

Returns True if a value does not exist in the x not in


not in
sequence y

Examples:

python

Copy code
lst = [1, 2, 3, 4]

print(2 in lst) # True

print(5 not in lst) # True

7. Identity Operators

Identity operators are used to compare the memory locations of two objects.

Operat Exampl
Description
or e

Returns True if both variables point to the


is x is y
same object

Returns True if both variables point to x is not


is not
different objects y

Examples:

python

Copy code

x = [1, 2, 3]

y = [1, 2, 3]

z=x

print(x is z) # True (z points to the same object as x)

print(x is y) # False (x and y are different objects, even though their


contents are the same)

print(x is not y) # True

8. Ternary Operator (Conditional Expression)

The ternary operator is a compact form of conditional statements. It’s used


to assign a value to a variable based on a condition.
Syntax:

python

Copy code

value_if_true if condition else value_if_false

Example:

python

Copy code

x = 10

y = 20

max_value = x if x > y else y

print(max_value) # Output: 20

9. Operator Precedence

In Python, operators have a predefined precedence (order of evaluation).


This defines which operators are evaluated first in an expression.

Precedence Table (from highest to lowest):

1. ** (Exponentiation)

2. ~, +, - (Unary operators)

3. *, /, //, % (Multiplication, Division, Floor Division, Modulus)

4. +, - (Addition, Subtraction)

5. <<, >> (Bitwise shift operators)

6. & (Bitwise AND)

7. ^ (Bitwise XOR)

8. | (Bitwise OR)

9. ==, !=, >, >=, <, <= (Comparison operators)

10. not (Logical NOT)


11. and (Logical AND)

12. or (Logical OR)

13. = and compound assignment operators (+=, -=, etc.)

Operator Overloading

Python allows you to change the behavior of its operators based on user-
defined data types by implementing special methods (also known as dunder
methods or magic methods).

Example of Operator Overloading:

python

Copy code

class Point:

def __init__(self, x, y):

self.x = x

self.y = y

# Overloading the '+' operator

def __add__(self, other):

return Point(self.x + other.x, self.y + other.y)

def __str__(self):

return f"({self.x}, {self.y})"

p1 = Point(1, 2)

p2 = Point(3, 4)

print(p1 + p2) # Output: (4, 6)


In Python, "ops" typically refers to operators, which are symbols or
keywords used to perform operations on variables or values. Python provides
a wide range of operators to manipulate data. These operators can be
classified into several types based on the operation they perform:

1. Arithmetic Operators

These operators are used to perform mathematical operations like addition,


subtraction, multiplication, etc.

Operat Exampl
Description
or e

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division (float) x/y

// Floor Division x // y

Modulus
% x%y
(remainder)

Exponentiation
** x ** y
(power)

Examples:

python

Copy code

x = 10

y=3

print(x + y) # 13

print(x - y) # 7

print(x * y) # 30

print(x / y) # 3.3333333333333335

print(x // y) # 3 (floor division)


print(x % y) # 1 (remainder)

print(x ** y) # 1000 (10 raised to the power of 3)

2. Assignment Operators

Assignment operators are used to assign values to variables.

Operat Exampl
Description
or e

Assigns value to a
= x = 10
variable

+= Adds and assigns x += 3

-= Subtracts and assigns x -= 3

*= Multiplies and assigns x *= 3

/= Divides and assigns x /= 3

Floor divides and


//= x //= 3
assigns

%= Modulus and assigns x %= 3

Exponentiates and
**= x **= 3
assigns

Examples:

python

Copy code

x=5

x += 2 # x = x + 2 -> x becomes 7

x *= 3 # x = x * 3 -> x becomes 21

print(x) # Output: 21

3. Comparison Operators
These operators are used to compare two values. The result of a comparison
is always a boolean (True or False).

Operat Exampl
Description
or e

== Equal to x == y

!= Not equal to x != y

> Greater than x>y

< Less than x<y

Greater than or
>= x >= y
equal to

Less than or equal


<= x <= y
to

Examples:

python

Copy code

x = 10

y=5

print(x == y) # False

print(x != y) # True

print(x > y) # True

print(x <= y) # False

4. Logical Operators

Logical operators are used to perform logical operations on boolean values.

Operat Exampl
Description
or e

and True if both are True x and y


Operat Exampl
Description
or e

True if at least one is


or x or y
True

not Reverses the result not x

Examples:

python

Copy code

x = True

y = False

print(x and y) # False

print(x or y) # True

print(not x) # False

5. Bitwise Operators

Bitwise operators perform operations on the binary representations of


integers.

Operat
Description Example
or

& Bitwise AND x&y

Bitwise
` `
OR

Bitwise XOR (exclusive


^ x^y
OR)

Bitwise NOT
~ ~x
(inversion)

Left shift (shift bits


<< x << y
left)
Operat
Description Example
or

Right shift (shift bits


>> x >> y
right)

Examples:

python

Copy code

x = 5 # 101 in binary

y = 3 # 011 in binary

print(x & y) # 1 (binary 001)

print(x | y) # 7 (binary 111)

print(x ^ y) # 6 (binary 110)

print(~x) # -6 (inversion: all bits flipped)

print(x << 1) # 10 (binary 1010, shifted left by 1 bit)

print(x >> 1) # 2 (binary 10, shifted right by 1 bit)

6. Membership Operators

Membership operators are used to check whether a value exists in a


sequence like a list, tuple, string, etc.

Operat Exampl
Description
or e

in Returns True if a value exists in the sequence x in y

Returns True if a value does not exist in the x not in


not in
sequence y

Examples:

python

Copy code
lst = [1, 2, 3, 4]

print(2 in lst) # True

print(5 not in lst) # True

7. Identity Operators

Identity operators are used to compare the memory locations of two objects.

Operat Exampl
Description
or e

Returns True if both variables point to the


is x is y
same object

Returns True if both variables point to x is not


is not
different objects y

Examples:

python

Copy code

x = [1, 2, 3]

y = [1, 2, 3]

z=x

print(x is z) # True (z points to the same object as x)

print(x is y) # False (x and y are different objects, even though their


contents are the same)

print(x is not y) # True

8. Ternary Operator (Conditional Expression)

The ternary operator is a compact form of conditional statements. It’s used


to assign a value to a variable based on a condition.
Syntax:

python

Copy code

value_if_true if condition else value_if_false

Example:

python

Copy code

x = 10

y = 20

max_value = x if x > y else y

print(max_value) # Output: 20

9. Operator Precedence

In Python, operators have a predefined precedence (order of evaluation).


This defines which operators are evaluated first in an expression.

Precedence Table (from highest to lowest):

1. ** (Exponentiation)

2. ~, +, - (Unary operators)

3. *, /, //, % (Multiplication, Division, Floor Division, Modulus)

4. +, - (Addition, Subtraction)

5. <<, >> (Bitwise shift operators)

6. & (Bitwise AND)

7. ^ (Bitwise XOR)

8. | (Bitwise OR)

9. ==, !=, >, >=, <, <= (Comparison operators)

10. not (Logical NOT)


11. and (Logical AND)

12. or (Logical OR)

13. = and compound assignment operators (+=, -=, etc.)

Operator Overloading

Python allows you to change the behavior of its operators based on user-
defined data types by implementing special methods (also known as dunder
methods or magic methods).

Example of Operator Overloading:

python

Copy code

class Point:

def __init__(self, x, y):

self.x = x

self.y = y

# Overloading the '+' operator

def __add__(self, other):

return Point(self.x + other.x, self.y + other.y)

def __str__(self):

return f"({self.x}, {self.y})"

p1 = Point(1, 2)

p2 = Point(3, 4)

print(p1 + p2) # Output: (4, 6)


In Python, "ops" typically refers to operators, which are symbols or
keywords used to perform operations on variables or values. Python provides
a wide range of operators to manipulate data. These operators can be
classified into several types based on the operation they perform:

1. Arithmetic Operators

These operators are used to perform mathematical operations like addition,


subtraction, multiplication, etc.

Operat Exampl
Description
or e

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division (float) x/y

// Floor Division x // y

Modulus
% x%y
(remainder)

Exponentiation
** x ** y
(power)

Examples:

python

Copy code

x = 10

y=3

print(x + y) # 13

print(x - y) # 7

print(x * y) # 30

print(x / y) # 3.3333333333333335

print(x // y) # 3 (floor division)


print(x % y) # 1 (remainder)

print(x ** y) # 1000 (10 raised to the power of 3)

2. Assignment Operators

Assignment operators are used to assign values to variables.

Operat Exampl
Description
or e

Assigns value to a
= x = 10
variable

+= Adds and assigns x += 3

-= Subtracts and assigns x -= 3

*= Multiplies and assigns x *= 3

/= Divides and assigns x /= 3

Floor divides and


//= x //= 3
assigns

%= Modulus and assigns x %= 3

Exponentiates and
**= x **= 3
assigns

Examples:

python

Copy code

x=5

x += 2 # x = x + 2 -> x becomes 7

x *= 3 # x = x * 3 -> x becomes 21

print(x) # Output: 21

3. Comparison Operators
These operators are used to compare two values. The result of a comparison
is always a boolean (True or False).

Operat Exampl
Description
or e

== Equal to x == y

!= Not equal to x != y

> Greater than x>y

< Less than x<y

Greater than or
>= x >= y
equal to

Less than or equal


<= x <= y
to

Examples:

python

Copy code

x = 10

y=5

print(x == y) # False

print(x != y) # True

print(x > y) # True

print(x <= y) # False

4. Logical Operators

Logical operators are used to perform logical operations on boolean values.

Operat Exampl
Description
or e

and True if both are True x and y


Operat Exampl
Description
or e

True if at least one is


or x or y
True

not Reverses the result not x

Examples:

python

Copy code

x = True

y = False

print(x and y) # False

print(x or y) # True

print(not x) # False

5. Bitwise Operators

Bitwise operators perform operations on the binary representations of


integers.

Operat
Description Example
or

& Bitwise AND x&y

Bitwise
` `
OR

Bitwise XOR (exclusive


^ x^y
OR)

Bitwise NOT
~ ~x
(inversion)

Left shift (shift bits


<< x << y
left)
Operat
Description Example
or

Right shift (shift bits


>> x >> y
right)

Examples:

python

Copy code

x = 5 # 101 in binary

y = 3 # 011 in binary

print(x & y) # 1 (binary 001)

print(x | y) # 7 (binary 111)

print(x ^ y) # 6 (binary 110)

print(~x) # -6 (inversion: all bits flipped)

print(x << 1) # 10 (binary 1010, shifted left by 1 bit)

print(x >> 1) # 2 (binary 10, shifted right by 1 bit)

6. Membership Operators

Membership operators are used to check whether a value exists in a


sequence like a list, tuple, string, etc.

Operat Exampl
Description
or e

in Returns True if a value exists in the sequence x in y

Returns True if a value does not exist in the x not in


not in
sequence y

Examples:

python

Copy code
lst = [1, 2, 3, 4]

print(2 in lst) # True

print(5 not in lst) # True

7. Identity Operators

Identity operators are used to compare the memory locations of two objects.

Operat Exampl
Description
or e

Returns True if both variables point to the


is x is y
same object

Returns True if both variables point to x is not


is not
different objects y

Examples:

python

Copy code

x = [1, 2, 3]

y = [1, 2, 3]

z=x

print(x is z) # True (z points to the same object as x)

print(x is y) # False (x and y are different objects, even though their


contents are the same)

print(x is not y) # True

8. Ternary Operator (Conditional Expression)

The ternary operator is a compact form of conditional statements. It’s used


to assign a value to a variable based on a condition.
Syntax:

python

Copy code

value_if_true if condition else value_if_false

Example:

python

Copy code

x = 10

y = 20

max_value = x if x > y else y

print(max_value) # Output: 20

9. Operator Precedence

In Python, operators have a predefined precedence (order of evaluation).


This defines which operators are evaluated first in an expression.

Precedence Table (from highest to lowest):

1. ** (Exponentiation)

2. ~, +, - (Unary operators)

3. *, /, //, % (Multiplication, Division, Floor Division, Modulus)

4. +, - (Addition, Subtraction)

5. <<, >> (Bitwise shift operators)

6. & (Bitwise AND)

7. ^ (Bitwise XOR)

8. | (Bitwise OR)

9. ==, !=, >, >=, <, <= (Comparison operators)

10. not (Logical NOT)


11. and (Logical AND)

12. or (Logical OR)

13. = and compound assignment operators (+=, -=, etc.)

Operator Overloading

Python allows you to change the behavior of its operators based on user-
defined data types by implementing special methods (also known as dunder
methods or magic methods).

Example of Operator Overloading:

python

Copy code

class Point:

def __init__(self, x, y):

self.x = x

self.y = y

# Overloading the '+' operator

def __add__(self, other):

return Point(self.x + other.x, self.y + other.y)

def __str__(self):

return f"({self.x}, {self.y})"

p1 = Point(1, 2)

p2 = Point(3, 4)

print(p1 + p2) # Output: (4, 6)


In Python, "ops" typically refers to operators, which are symbols or
keywords used to perform operations on variables or values. Python provides
a wide range of operators to manipulate data. These operators can be
classified into several types based on the operation they perform:

1. Arithmetic Operators

These operators are used to perform mathematical operations like addition,


subtraction, multiplication, etc.

Operat Exampl
Description
or e

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division (float) x/y

// Floor Division x // y

Modulus
% x%y
(remainder)

Exponentiation
** x ** y
(power)

Examples:

python

Copy code

x = 10

y=3

print(x + y) # 13

print(x - y) # 7

print(x * y) # 30

print(x / y) # 3.3333333333333335

print(x // y) # 3 (floor division)


print(x % y) # 1 (remainder)

print(x ** y) # 1000 (10 raised to the power of 3)

2. Assignment Operators

Assignment operators are used to assign values to variables.

Operat Exampl
Description
or e

Assigns value to a
= x = 10
variable

+= Adds and assigns x += 3

-= Subtracts and assigns x -= 3

*= Multiplies and assigns x *= 3

/= Divides and assigns x /= 3

Floor divides and


//= x //= 3
assigns

%= Modulus and assigns x %= 3

Exponentiates and
**= x **= 3
assigns

Examples:

python

Copy code

x=5

x += 2 # x = x + 2 -> x becomes 7

x *= 3 # x = x * 3 -> x becomes 21

print(x) # Output: 21

3. Comparison Operators
These operators are used to compare two values. The result of a comparison
is always a boolean (True or False).

Operat Exampl
Description
or e

== Equal to x == y

!= Not equal to x != y

> Greater than x>y

< Less than x<y

Greater than or
>= x >= y
equal to

Less than or equal


<= x <= y
to

Examples:

python

Copy code

x = 10

y=5

print(x == y) # False

print(x != y) # True

print(x > y) # True

print(x <= y) # False

4. Logical Operators

Logical operators are used to perform logical operations on boolean values.

Operat Exampl
Description
or e

and True if both are True x and y


Operat Exampl
Description
or e

True if at least one is


or x or y
True

not Reverses the result not x

Examples:

python

Copy code

x = True

y = False

print(x and y) # False

print(x or y) # True

print(not x) # False

5. Bitwise Operators

Bitwise operators perform operations on the binary representations of


integers.

Operat
Description Example
or

& Bitwise AND x&y

Bitwise
` `
OR

Bitwise XOR (exclusive


^ x^y
OR)

Bitwise NOT
~ ~x
(inversion)

Left shift (shift bits


<< x << y
left)
Operat
Description Example
or

Right shift (shift bits


>> x >> y
right)

Examples:

python

Copy code

x = 5 # 101 in binary

y = 3 # 011 in binary

print(x & y) # 1 (binary 001)

print(x | y) # 7 (binary 111)

print(x ^ y) # 6 (binary 110)

print(~x) # -6 (inversion: all bits flipped)

print(x << 1) # 10 (binary 1010, shifted left by 1 bit)

print(x >> 1) # 2 (binary 10, shifted right by 1 bit)

6. Membership Operators

Membership operators are used to check whether a value exists in a


sequence like a list, tuple, string, etc.

Operat Exampl
Description
or e

in Returns True if a value exists in the sequence x in y

Returns True if a value does not exist in the x not in


not in
sequence y

Examples:

python

Copy code
lst = [1, 2, 3, 4]

print(2 in lst) # True

print(5 not in lst) # True

7. Identity Operators

Identity operators are used to compare the memory locations of two objects.

Operat Exampl
Description
or e

Returns True if both variables point to the


is x is y
same object

Returns True if both variables point to x is not


is not
different objects y

Examples:

python

Copy code

x = [1, 2, 3]

y = [1, 2, 3]

z=x

print(x is z) # True (z points to the same object as x)

print(x is y) # False (x and y are different objects, even though their


contents are the same)

print(x is not y) # True

8. Ternary Operator (Conditional Expression)

The ternary operator is a compact form of conditional statements. It’s used


to assign a value to a variable based on a condition.
Syntax:

python

Copy code

value_if_true if condition else value_if_false

Example:

python

Copy code

x = 10

y = 20

max_value = x if x > y else y

print(max_value) # Output: 20

9. Operator Precedence

In Python, operators have a predefined precedence (order of evaluation).


This defines which operators are evaluated first in an expression.

Precedence Table (from highest to lowest):

1. ** (Exponentiation)

2. ~, +, - (Unary operators)

3. *, /, //, % (Multiplication, Division, Floor Division, Modulus)

4. +, - (Addition, Subtraction)

5. <<, >> (Bitwise shift operators)

6. & (Bitwise AND)

7. ^ (Bitwise XOR)

8. | (Bitwise OR)

9. ==, !=, >, >=, <, <= (Comparison operators)

10. not (Logical NOT)


11. and (Logical AND)

12. or (Logical OR)

13. = and compound assignment operators (+=, -=, etc.)

Operator Overloading

Python allows you to change the behavior of its operators based on user-
defined data types by implementing special methods (also known as dunder
methods or magic methods).

Example of Operator Overloading:

python

Copy code

class Point:

def __init__(self, x, y):

self.x = x

self.y = y

# Overloading the '+' operator

def __add__(self, other):

return Point(self.x + other.x, self.y + other.y)

def __str__(self):

return f"({self.x}, {self.y})"

p1 = Point(1, 2)

p2 = Point(3, 4)

print(p1 + p2) # Output: (4, 6)


In Python, "ops" typically refers to operators, which are symbols or
keywords used to perform operations on variables or values. Python provides
a wide range of operators to manipulate data. These operators can be
classified into several types based on the operation they perform:

1. Arithmetic Operators

These operators are used to perform mathematical operations like addition,


subtraction, multiplication, etc.

Operat Exampl
Description
or e

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division (float) x/y

// Floor Division x // y

Modulus
% x%y
(remainder)

Exponentiation
** x ** y
(power)

Examples:

python

Copy code

x = 10

y=3

print(x + y) # 13

print(x - y) # 7

print(x * y) # 30

print(x / y) # 3.3333333333333335

print(x // y) # 3 (floor division)


print(x % y) # 1 (remainder)

print(x ** y) # 1000 (10 raised to the power of 3)

2. Assignment Operators

Assignment operators are used to assign values to variables.

Operat Exampl
Description
or e

Assigns value to a
= x = 10
variable

+= Adds and assigns x += 3

-= Subtracts and assigns x -= 3

*= Multiplies and assigns x *= 3

/= Divides and assigns x /= 3

Floor divides and


//= x //= 3
assigns

%= Modulus and assigns x %= 3

Exponentiates and
**= x **= 3
assigns

Examples:

python

Copy code

x=5

x += 2 # x = x + 2 -> x becomes 7

x *= 3 # x = x * 3 -> x becomes 21

print(x) # Output: 21

3. Comparison Operators
These operators are used to compare two values. The result of a comparison
is always a boolean (True or False).

Operat Exampl
Description
or e

== Equal to x == y

!= Not equal to x != y

> Greater than x>y

< Less than x<y

Greater than or
>= x >= y
equal to

Less than or equal


<= x <= y
to

Examples:

python

Copy code

x = 10

y=5

print(x == y) # False

print(x != y) # True

print(x > y) # True

print(x <= y) # False

4. Logical Operators

Logical operators are used to perform logical operations on boolean values.

Operat Exampl
Description
or e

and True if both are True x and y


Operat Exampl
Description
or e

True if at least one is


or x or y
True

not Reverses the result not x

Examples:

python

Copy code

x = True

y = False

print(x and y) # False

print(x or y) # True

print(not x) # False

5. Bitwise Operators

Bitwise operators perform operations on the binary representations of


integers.

Operat
Description Example
or

& Bitwise AND x&y

Bitwise
` `
OR

Bitwise XOR (exclusive


^ x^y
OR)

Bitwise NOT
~ ~x
(inversion)

Left shift (shift bits


<< x << y
left)
Operat
Description Example
or

Right shift (shift bits


>> x >> y
right)

Examples:

python

Copy code

x = 5 # 101 in binary

y = 3 # 011 in binary

print(x & y) # 1 (binary 001)

print(x | y) # 7 (binary 111)

print(x ^ y) # 6 (binary 110)

print(~x) # -6 (inversion: all bits flipped)

print(x << 1) # 10 (binary 1010, shifted left by 1 bit)

print(x >> 1) # 2 (binary 10, shifted right by 1 bit)

6. Membership Operators

Membership operators are used to check whether a value exists in a


sequence like a list, tuple, string, etc.

Operat Exampl
Description
or e

in Returns True if a value exists in the sequence x in y

Returns True if a value does not exist in the x not in


not in
sequence y

Examples:

python

Copy code
lst = [1, 2, 3, 4]

print(2 in lst) # True

print(5 not in lst) # True

7. Identity Operators

Identity operators are used to compare the memory locations of two objects.

Operat Exampl
Description
or e

Returns True if both variables point to the


is x is y
same object

Returns True if both variables point to x is not


is not
different objects y

Examples:

python

Copy code

x = [1, 2, 3]

y = [1, 2, 3]

z=x

print(x is z) # True (z points to the same object as x)

print(x is y) # False (x and y are different objects, even though their


contents are the same)

print(x is not y) # True

8. Ternary Operator (Conditional Expression)

The ternary operator is a compact form of conditional statements. It’s used


to assign a value to a variable based on a condition.
Syntax:

python

Copy code

value_if_true if condition else value_if_false

Example:

python

Copy code

x = 10

y = 20

max_value = x if x > y else y

print(max_value) # Output: 20

9. Operator Precedence

In Python, operators have a predefined precedence (order of evaluation).


This defines which operators are evaluated first in an expression.

Precedence Table (from highest to lowest):

1. ** (Exponentiation)

2. ~, +, - (Unary operators)

3. *, /, //, % (Multiplication, Division, Floor Division, Modulus)

4. +, - (Addition, Subtraction)

5. <<, >> (Bitwise shift operators)

6. & (Bitwise AND)

7. ^ (Bitwise XOR)

8. | (Bitwise OR)

9. ==, !=, >, >=, <, <= (Comparison operators)

10. not (Logical NOT)


11. and (Logical AND)

12. or (Logical OR)

13. = and compound assignment operators (+=, -=, etc.)

Operator Overloading

Python allows you to change the behavior of its operators based on user-
defined data types by implementing special methods (also known as dunder
methods or magic methods).

Example of Operator Overloading:

python

Copy code

class Point:

def __init__(self, x, y):

self.x = x

self.y = y

# Overloading the '+' operator

def __add__(self, other):

return Point(self.x + other.x, self.y + other.y)

def __str__(self):

return f"({self.x}, {self.y})"

p1 = Point(1, 2)

p2 = Point(3, 4)

print(p1 + p2) # Output: (4, 6)


In Python, "ops" typically refers to operators, which are symbols or
keywords used to perform operations on variables or values. Python provides
a wide range of operators to manipulate data. These operators can be
classified into several types based on the operation they perform:

1. Arithmetic Operators

These operators are used to perform mathematical operations like addition,


subtraction, multiplication, etc.

Operat Exampl
Description
or e

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division (float) x/y

// Floor Division x // y

Modulus
% x%y
(remainder)

Exponentiation
** x ** y
(power)

Examples:

python

Copy code

x = 10

y=3

print(x + y) # 13

print(x - y) # 7

print(x * y) # 30

print(x / y) # 3.3333333333333335

print(x // y) # 3 (floor division)


print(x % y) # 1 (remainder)

print(x ** y) # 1000 (10 raised to the power of 3)

2. Assignment Operators

Assignment operators are used to assign values to variables.

Operat Exampl
Description
or e

Assigns value to a
= x = 10
variable

+= Adds and assigns x += 3

-= Subtracts and assigns x -= 3

*= Multiplies and assigns x *= 3

/= Divides and assigns x /= 3

Floor divides and


//= x //= 3
assigns

%= Modulus and assigns x %= 3

Exponentiates and
**= x **= 3
assigns

Examples:

python

Copy code

x=5

x += 2 # x = x + 2 -> x becomes 7

x *= 3 # x = x * 3 -> x becomes 21

print(x) # Output: 21

3. Comparison Operators
These operators are used to compare two values. The result of a comparison
is always a boolean (True or False).

Operat Exampl
Description
or e

== Equal to x == y

!= Not equal to x != y

> Greater than x>y

< Less than x<y

Greater than or
>= x >= y
equal to

Less than or equal


<= x <= y
to

Examples:

python

Copy code

x = 10

y=5

print(x == y) # False

print(x != y) # True

print(x > y) # True

print(x <= y) # False

4. Logical Operators

Logical operators are used to perform logical operations on boolean values.

Operat Exampl
Description
or e

and True if both are True x and y


Operat Exampl
Description
or e

True if at least one is


or x or y
True

not Reverses the result not x

Examples:

python

Copy code

x = True

y = False

print(x and y) # False

print(x or y) # True

print(not x) # False

5. Bitwise Operators

Bitwise operators perform operations on the binary representations of


integers.

Operat
Description Example
or

& Bitwise AND x&y

Bitwise
` `
OR

Bitwise XOR (exclusive


^ x^y
OR)

Bitwise NOT
~ ~x
(inversion)

Left shift (shift bits


<< x << y
left)
Operat
Description Example
or

Right shift (shift bits


>> x >> y
right)

Examples:

python

Copy code

x = 5 # 101 in binary

y = 3 # 011 in binary

print(x & y) # 1 (binary 001)

print(x | y) # 7 (binary 111)

print(x ^ y) # 6 (binary 110)

print(~x) # -6 (inversion: all bits flipped)

print(x << 1) # 10 (binary 1010, shifted left by 1 bit)

print(x >> 1) # 2 (binary 10, shifted right by 1 bit)

6. Membership Operators

Membership operators are used to check whether a value exists in a


sequence like a list, tuple, string, etc.

Operat Exampl
Description
or e

in Returns True if a value exists in the sequence x in y

Returns True if a value does not exist in the x not in


not in
sequence y

Examples:

python

Copy code
lst = [1, 2, 3, 4]

print(2 in lst) # True

print(5 not in lst) # True

7. Identity Operators

Identity operators are used to compare the memory locations of two objects.

Operat Exampl
Description
or e

Returns True if both variables point to the


is x is y
same object

Returns True if both variables point to x is not


is not
different objects y

Examples:

python

Copy code

x = [1, 2, 3]

y = [1, 2, 3]

z=x

print(x is z) # True (z points to the same object as x)

print(x is y) # False (x and y are different objects, even though their


contents are the same)

print(x is not y) # True

8. Ternary Operator (Conditional Expression)

The ternary operator is a compact form of conditional statements. It’s used


to assign a value to a variable based on a condition.
Syntax:

python

Copy code

value_if_true if condition else value_if_false

Example:

python

Copy code

x = 10

y = 20

max_value = x if x > y else y

print(max_value) # Output: 20

9. Operator Precedence

In Python, operators have a predefined precedence (order of evaluation).


This defines which operators are evaluated first in an expression.

Precedence Table (from highest to lowest):

1. ** (Exponentiation)

2. ~, +, - (Unary operators)

3. *, /, //, % (Multiplication, Division, Floor Division, Modulus)

4. +, - (Addition, Subtraction)

5. <<, >> (Bitwise shift operators)

6. & (Bitwise AND)

7. ^ (Bitwise XOR)

8. | (Bitwise OR)

9. ==, !=, >, >=, <, <= (Comparison operators)

10. not (Logical NOT)


11. and (Logical AND)

12. or (Logical OR)

13. = and compound assignment operators (+=, -=, etc.)

Operator Overloading

Python allows you to change the behavior of its operators based on user-
defined data types by implementing special methods (also known as dunder
methods or magic methods).

Example of Operator Overloading:

python

Copy code

class Point:

def __init__(self, x, y):

self.x = x

self.y = y

# Overloading the '+' operator

def __add__(self, other):

return Point(self.x + other.x, self.y + other.y)

def __str__(self):

return f"({self.x}, {self.y})"

p1 = Point(1, 2)

p2 = Point(3, 4)

print(p1 + p2) # Output: (4, 6)


In Python, "ops" typically refers to operators, which are symbols or
keywords used to perform operations on variables or values. Python provides
a wide range of operators to manipulate data. These operators can be
classified into several types based on the operation they perform:

1. Arithmetic Operators

These operators are used to perform mathematical operations like addition,


subtraction, multiplication, etc.

Operat Exampl
Description
or e

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division (float) x/y

// Floor Division x // y

Modulus
% x%y
(remainder)

Exponentiation
** x ** y
(power)

Examples:

python

Copy code

x = 10

y=3

print(x + y) # 13

print(x - y) # 7

print(x * y) # 30

print(x / y) # 3.3333333333333335

print(x // y) # 3 (floor division)


print(x % y) # 1 (remainder)

print(x ** y) # 1000 (10 raised to the power of 3)

2. Assignment Operators

Assignment operators are used to assign values to variables.

Operat Exampl
Description
or e

Assigns value to a
= x = 10
variable

+= Adds and assigns x += 3

-= Subtracts and assigns x -= 3

*= Multiplies and assigns x *= 3

/= Divides and assigns x /= 3

Floor divides and


//= x //= 3
assigns

%= Modulus and assigns x %= 3

Exponentiates and
**= x **= 3
assigns

Examples:

python

Copy code

x=5

x += 2 # x = x + 2 -> x becomes 7

x *= 3 # x = x * 3 -> x becomes 21

print(x) # Output: 21

3. Comparison Operators
These operators are used to compare two values. The result of a comparison
is always a boolean (True or False).

Operat Exampl
Description
or e

== Equal to x == y

!= Not equal to x != y

> Greater than x>y

< Less than x<y

Greater than or
>= x >= y
equal to

Less than or equal


<= x <= y
to

Examples:

python

Copy code

x = 10

y=5

print(x == y) # False

print(x != y) # True

print(x > y) # True

print(x <= y) # False

4. Logical Operators

Logical operators are used to perform logical operations on boolean values.

Operat Exampl
Description
or e

and True if both are True x and y


Operat Exampl
Description
or e

True if at least one is


or x or y
True

not Reverses the result not x

Examples:

python

Copy code

x = True

y = False

print(x and y) # False

print(x or y) # True

print(not x) # False

5. Bitwise Operators

Bitwise operators perform operations on the binary representations of


integers.

Operat
Description Example
or

& Bitwise AND x&y

Bitwise
` `
OR

Bitwise XOR (exclusive


^ x^y
OR)

Bitwise NOT
~ ~x
(inversion)

Left shift (shift bits


<< x << y
left)
Operat
Description Example
or

Right shift (shift bits


>> x >> y
right)

Examples:

python

Copy code

x = 5 # 101 in binary

y = 3 # 011 in binary

print(x & y) # 1 (binary 001)

print(x | y) # 7 (binary 111)

print(x ^ y) # 6 (binary 110)

print(~x) # -6 (inversion: all bits flipped)

print(x << 1) # 10 (binary 1010, shifted left by 1 bit)

print(x >> 1) # 2 (binary 10, shifted right by 1 bit)

6. Membership Operators

Membership operators are used to check whether a value exists in a


sequence like a list, tuple, string, etc.

Operat Exampl
Description
or e

in Returns True if a value exists in the sequence x in y

Returns True if a value does not exist in the x not in


not in
sequence y

Examples:

python

Copy code
lst = [1, 2, 3, 4]

print(2 in lst) # True

print(5 not in lst) # True

7. Identity Operators

Identity operators are used to compare the memory locations of two objects.

Operat Exampl
Description
or e

Returns True if both variables point to the


is x is y
same object

Returns True if both variables point to x is not


is not
different objects y

Examples:

python

Copy code

x = [1, 2, 3]

y = [1, 2, 3]

z=x

print(x is z) # True (z points to the same object as x)

print(x is y) # False (x and y are different objects, even though their


contents are the same)

print(x is not y) # True

8. Ternary Operator (Conditional Expression)

The ternary operator is a compact form of conditional statements. It’s used


to assign a value to a variable based on a condition.
Syntax:

python

Copy code

value_if_true if condition else value_if_false

Example:

python

Copy code

x = 10

y = 20

max_value = x if x > y else y

print(max_value) # Output: 20

9. Operator Precedence

In Python, operators have a predefined precedence (order of evaluation).


This defines which operators are evaluated first in an expression.

Precedence Table (from highest to lowest):

1. ** (Exponentiation)

2. ~, +, - (Unary operators)

3. *, /, //, % (Multiplication, Division, Floor Division, Modulus)

4. +, - (Addition, Subtraction)

5. <<, >> (Bitwise shift operators)

6. & (Bitwise AND)

7. ^ (Bitwise XOR)

8. | (Bitwise OR)

9. ==, !=, >, >=, <, <= (Comparison operators)

10. not (Logical NOT)


11. and (Logical AND)

12. or (Logical OR)

13. = and compound assignment operators (+=, -=, etc.)

Operator Overloading

Python allows you to change the behavior of its operators based on user-
defined data types by implementing special methods (also known as dunder
methods or magic methods).

Example of Operator Overloading:

python

Copy code

class Point:

def __init__(self, x, y):

self.x = x

self.y = y

# Overloading the '+' operator

def __add__(self, other):

return Point(self.x + other.x, self.y + other.y)

def __str__(self):

return f"({self.x}, {self.y})"

p1 = Point(1, 2)

p2 = Point(3, 4)

print(p1 + p2) # Output: (4, 6)


In Python, "ops" typically refers to operators, which are symbols or
keywords used to perform operations on variables or values. Python provides
a wide range of operators to manipulate data. These operators can be
classified into several types based on the operation they perform:

1. Arithmetic Operators

These operators are used to perform mathematical operations like addition,


subtraction, multiplication, etc.

Operat Exampl
Description
or e

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division (float) x/y

// Floor Division x // y

Modulus
% x%y
(remainder)

Exponentiation
** x ** y
(power)

Examples:

python

Copy code

x = 10

y=3

print(x + y) # 13

print(x - y) # 7

print(x * y) # 30

print(x / y) # 3.3333333333333335

print(x // y) # 3 (floor division)


print(x % y) # 1 (remainder)

print(x ** y) # 1000 (10 raised to the power of 3)

2. Assignment Operators

Assignment operators are used to assign values to variables.

Operat Exampl
Description
or e

Assigns value to a
= x = 10
variable

+= Adds and assigns x += 3

-= Subtracts and assigns x -= 3

*= Multiplies and assigns x *= 3

/= Divides and assigns x /= 3

Floor divides and


//= x //= 3
assigns

%= Modulus and assigns x %= 3

Exponentiates and
**= x **= 3
assigns

Examples:

python

Copy code

x=5

x += 2 # x = x + 2 -> x becomes 7

x *= 3 # x = x * 3 -> x becomes 21

print(x) # Output: 21

3. Comparison Operators
These operators are used to compare two values. The result of a comparison
is always a boolean (True or False).

Operat Exampl
Description
or e

== Equal to x == y

!= Not equal to x != y

> Greater than x>y

< Less than x<y

Greater than or
>= x >= y
equal to

Less than or equal


<= x <= y
to

Examples:

python

Copy code

x = 10

y=5

print(x == y) # False

print(x != y) # True

print(x > y) # True

print(x <= y) # False

4. Logical Operators

Logical operators are used to perform logical operations on boolean values.

Operat Exampl
Description
or e

and True if both are True x and y


Operat Exampl
Description
or e

True if at least one is


or x or y
True

not Reverses the result not x

Examples:

python

Copy code

x = True

y = False

print(x and y) # False

print(x or y) # True

print(not x) # False

5. Bitwise Operators

Bitwise operators perform operations on the binary representations of


integers.

Operat
Description Example
or

& Bitwise AND x&y

Bitwise
` `
OR

Bitwise XOR (exclusive


^ x^y
OR)

Bitwise NOT
~ ~x
(inversion)

Left shift (shift bits


<< x << y
left)
Operat
Description Example
or

Right shift (shift bits


>> x >> y
right)

Examples:

python

Copy code

x = 5 # 101 in binary

y = 3 # 011 in binary

print(x & y) # 1 (binary 001)

print(x | y) # 7 (binary 111)

print(x ^ y) # 6 (binary 110)

print(~x) # -6 (inversion: all bits flipped)

print(x << 1) # 10 (binary 1010, shifted left by 1 bit)

print(x >> 1) # 2 (binary 10, shifted right by 1 bit)

6. Membership Operators

Membership operators are used to check whether a value exists in a


sequence like a list, tuple, string, etc.

Operat Exampl
Description
or e

in Returns True if a value exists in the sequence x in y

Returns True if a value does not exist in the x not in


not in
sequence y

Examples:

python

Copy code
lst = [1, 2, 3, 4]

print(2 in lst) # True

print(5 not in lst) # True

7. Identity Operators

Identity operators are used to compare the memory locations of two objects.

Operat Exampl
Description
or e

Returns True if both variables point to the


is x is y
same object

Returns True if both variables point to x is not


is not
different objects y

Examples:

python

Copy code

x = [1, 2, 3]

y = [1, 2, 3]

z=x

print(x is z) # True (z points to the same object as x)

print(x is y) # False (x and y are different objects, even though their


contents are the same)

print(x is not y) # True

8. Ternary Operator (Conditional Expression)

The ternary operator is a compact form of conditional statements. It’s used


to assign a value to a variable based on a condition.
Syntax:

python

Copy code

value_if_true if condition else value_if_false

Example:

python

Copy code

x = 10

y = 20

max_value = x if x > y else y

print(max_value) # Output: 20

9. Operator Precedence

In Python, operators have a predefined precedence (order of evaluation).


This defines which operators are evaluated first in an expression.

Precedence Table (from highest to lowest):

1. ** (Exponentiation)

2. ~, +, - (Unary operators)

3. *, /, //, % (Multiplication, Division, Floor Division, Modulus)

4. +, - (Addition, Subtraction)

5. <<, >> (Bitwise shift operators)

6. & (Bitwise AND)

7. ^ (Bitwise XOR)

8. | (Bitwise OR)

9. ==, !=, >, >=, <, <= (Comparison operators)

10. not (Logical NOT)


11. and (Logical AND)

12. or (Logical OR)

13. = and compound assignment operators (+=, -=, etc.)

Operator Overloading

Python allows you to change the behavior of its operators based on user-
defined data types by implementing special methods (also known as dunder
methods or magic methods).

Example of Operator Overloading:

python

Copy code

class Point:

def __init__(self, x, y):

self.x = x

self.y = y

# Overloading the '+' operator

def __add__(self, other):

return Point(self.x + other.x, self.y + other.y)

def __str__(self):

return f"({self.x}, {self.y})"

p1 = Point(1, 2)

p2 = Point(3, 4)

print(p1 + p2) # Output: (4, 6)


In Python, "ops" typically refers to operators, which are symbols or
keywords used to perform operations on variables or values. Python provides
a wide range of operators to manipulate data. These operators can be
classified into several types based on the operation they perform:

1. Arithmetic Operators

These operators are used to perform mathematical operations like addition,


subtraction, multiplication, etc.

Operat Exampl
Description
or e

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division (float) x/y

// Floor Division x // y

Modulus
% x%y
(remainder)

Exponentiation
** x ** y
(power)

Examples:

python

Copy code

x = 10

y=3

print(x + y) # 13

print(x - y) # 7

print(x * y) # 30

print(x / y) # 3.3333333333333335

print(x // y) # 3 (floor division)


print(x % y) # 1 (remainder)

print(x ** y) # 1000 (10 raised to the power of 3)

2. Assignment Operators

Assignment operators are used to assign values to variables.

Operat Exampl
Description
or e

Assigns value to a
= x = 10
variable

+= Adds and assigns x += 3

-= Subtracts and assigns x -= 3

*= Multiplies and assigns x *= 3

/= Divides and assigns x /= 3

Floor divides and


//= x //= 3
assigns

%= Modulus and assigns x %= 3

Exponentiates and
**= x **= 3
assigns

Examples:

python

Copy code

x=5

x += 2 # x = x + 2 -> x becomes 7

x *= 3 # x = x * 3 -> x becomes 21

print(x) # Output: 21

3. Comparison Operators
These operators are used to compare two values. The result of a comparison
is always a boolean (True or False).

Operat Exampl
Description
or e

== Equal to x == y

!= Not equal to x != y

> Greater than x>y

< Less than x<y

Greater than or
>= x >= y
equal to

Less than or equal


<= x <= y
to

Examples:

python

Copy code

x = 10

y=5

print(x == y) # False

print(x != y) # True

print(x > y) # True

print(x <= y) # False

4. Logical Operators

Logical operators are used to perform logical operations on boolean values.

Operat Exampl
Description
or e

and True if both are True x and y


Operat Exampl
Description
or e

True if at least one is


or x or y
True

not Reverses the result not x

Examples:

python

Copy code

x = True

y = False

print(x and y) # False

print(x or y) # True

print(not x) # False

5. Bitwise Operators

Bitwise operators perform operations on the binary representations of


integers.

Operat
Description Example
or

& Bitwise AND x&y

Bitwise
` `
OR

Bitwise XOR (exclusive


^ x^y
OR)

Bitwise NOT
~ ~x
(inversion)

Left shift (shift bits


<< x << y
left)
Operat
Description Example
or

Right shift (shift bits


>> x >> y
right)

Examples:

python

Copy code

x = 5 # 101 in binary

y = 3 # 011 in binary

print(x & y) # 1 (binary 001)

print(x | y) # 7 (binary 111)

print(x ^ y) # 6 (binary 110)

print(~x) # -6 (inversion: all bits flipped)

print(x << 1) # 10 (binary 1010, shifted left by 1 bit)

print(x >> 1) # 2 (binary 10, shifted right by 1 bit)

6. Membership Operators

Membership operators are used to check whether a value exists in a


sequence like a list, tuple, string, etc.

Operat Exampl
Description
or e

in Returns True if a value exists in the sequence x in y

Returns True if a value does not exist in the x not in


not in
sequence y

Examples:

python

Copy code
lst = [1, 2, 3, 4]

print(2 in lst) # True

print(5 not in lst) # True

7. Identity Operators

Identity operators are used to compare the memory locations of two objects.

Operat Exampl
Description
or e

Returns True if both variables point to the


is x is y
same object

Returns True if both variables point to x is not


is not
different objects y

Examples:

python

Copy code

x = [1, 2, 3]

y = [1, 2, 3]

z=x

print(x is z) # True (z points to the same object as x)

print(x is y) # False (x and y are different objects, even though their


contents are the same)

print(x is not y) # True

8. Ternary Operator (Conditional Expression)

The ternary operator is a compact form of conditional statements. It’s used


to assign a value to a variable based on a condition.
Syntax:

python

Copy code

value_if_true if condition else value_if_false

Example:

python

Copy code

x = 10

y = 20

max_value = x if x > y else y

print(max_value) # Output: 20

9. Operator Precedence

In Python, operators have a predefined precedence (order of evaluation).


This defines which operators are evaluated first in an expression.

Precedence Table (from highest to lowest):

1. ** (Exponentiation)

2. ~, +, - (Unary operators)

3. *, /, //, % (Multiplication, Division, Floor Division, Modulus)

4. +, - (Addition, Subtraction)

5. <<, >> (Bitwise shift operators)

6. & (Bitwise AND)

7. ^ (Bitwise XOR)

8. | (Bitwise OR)

9. ==, !=, >, >=, <, <= (Comparison operators)

10. not (Logical NOT)


11. and (Logical AND)

12. or (Logical OR)

13. = and compound assignment operators (+=, -=, etc.)

Operator Overloading

Python allows you to change the behavior of its operators based on user-
defined data types by implementing special methods (also known as dunder
methods or magic methods).

Example of Operator Overloading:

python

Copy code

class Point:

def __init__(self, x, y):

self.x = x

self.y = y

# Overloading the '+' operator

def __add__(self, other):

return Point(self.x + other.x, self.y + other.y)

def __str__(self):

return f"({self.x}, {self.y})"

p1 = Point(1, 2)

p2 = Point(3, 4)

print(p1 + p2) # Output: (4, 6)


In Python, "ops" typically refers to operators, which are symbols or
keywords used to perform operations on variables or values. Python provides
a wide range of operators to manipulate data. These operators can be
classified into several types based on the operation they perform:

1. Arithmetic Operators

These operators are used to perform mathematical operations like addition,


subtraction, multiplication, etc.

Operat Exampl
Description
or e

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division (float) x/y

// Floor Division x // y

Modulus
% x%y
(remainder)

Exponentiation
** x ** y
(power)

Examples:

python

Copy code

x = 10

y=3

print(x + y) # 13

print(x - y) # 7

print(x * y) # 30

print(x / y) # 3.3333333333333335

print(x // y) # 3 (floor division)


print(x % y) # 1 (remainder)

print(x ** y) # 1000 (10 raised to the power of 3)

2. Assignment Operators

Assignment operators are used to assign values to variables.

Operat Exampl
Description
or e

Assigns value to a
= x = 10
variable

+= Adds and assigns x += 3

-= Subtracts and assigns x -= 3

*= Multiplies and assigns x *= 3

/= Divides and assigns x /= 3

Floor divides and


//= x //= 3
assigns

%= Modulus and assigns x %= 3

Exponentiates and
**= x **= 3
assigns

Examples:

python

Copy code

x=5

x += 2 # x = x + 2 -> x becomes 7

x *= 3 # x = x * 3 -> x becomes 21

print(x) # Output: 21

3. Comparison Operators
These operators are used to compare two values. The result of a comparison
is always a boolean (True or False).

Operat Exampl
Description
or e

== Equal to x == y

!= Not equal to x != y

> Greater than x>y

< Less than x<y

Greater than or
>= x >= y
equal to

Less than or equal


<= x <= y
to

Examples:

python

Copy code

x = 10

y=5

print(x == y) # False

print(x != y) # True

print(x > y) # True

print(x <= y) # False

4. Logical Operators

Logical operators are used to perform logical operations on boolean values.

Operat Exampl
Description
or e

and True if both are True x and y


Operat Exampl
Description
or e

True if at least one is


or x or y
True

not Reverses the result not x

Examples:

python

Copy code

x = True

y = False

print(x and y) # False

print(x or y) # True

print(not x) # False

5. Bitwise Operators

Bitwise operators perform operations on the binary representations of


integers.

Operat
Description Example
or

& Bitwise AND x&y

Bitwise
` `
OR

Bitwise XOR (exclusive


^ x^y
OR)

Bitwise NOT
~ ~x
(inversion)

Left shift (shift bits


<< x << y
left)
Operat
Description Example
or

Right shift (shift bits


>> x >> y
right)

Examples:

python

Copy code

x = 5 # 101 in binary

y = 3 # 011 in binary

print(x & y) # 1 (binary 001)

print(x | y) # 7 (binary 111)

print(x ^ y) # 6 (binary 110)

print(~x) # -6 (inversion: all bits flipped)

print(x << 1) # 10 (binary 1010, shifted left by 1 bit)

print(x >> 1) # 2 (binary 10, shifted right by 1 bit)

6. Membership Operators

Membership operators are used to check whether a value exists in a


sequence like a list, tuple, string, etc.

Operat Exampl
Description
or e

in Returns True if a value exists in the sequence x in y

Returns True if a value does not exist in the x not in


not in
sequence y

Examples:

python

Copy code
lst = [1, 2, 3, 4]

print(2 in lst) # True

print(5 not in lst) # True

7. Identity Operators

Identity operators are used to compare the memory locations of two objects.

Operat Exampl
Description
or e

Returns True if both variables point to the


is x is y
same object

Returns True if both variables point to x is not


is not
different objects y

Examples:

python

Copy code

x = [1, 2, 3]

y = [1, 2, 3]

z=x

print(x is z) # True (z points to the same object as x)

print(x is y) # False (x and y are different objects, even though their


contents are the same)

print(x is not y) # True

8. Ternary Operator (Conditional Expression)

The ternary operator is a compact form of conditional statements. It’s used


to assign a value to a variable based on a condition.
Syntax:

python

Copy code

value_if_true if condition else value_if_false

Example:

python

Copy code

x = 10

y = 20

max_value = x if x > y else y

print(max_value) # Output: 20

9. Operator Precedence

In Python, operators have a predefined precedence (order of evaluation).


This defines which operators are evaluated first in an expression.

Precedence Table (from highest to lowest):

1. ** (Exponentiation)

2. ~, +, - (Unary operators)

3. *, /, //, % (Multiplication, Division, Floor Division, Modulus)

4. +, - (Addition, Subtraction)

5. <<, >> (Bitwise shift operators)

6. & (Bitwise AND)

7. ^ (Bitwise XOR)

8. | (Bitwise OR)

9. ==, !=, >, >=, <, <= (Comparison operators)

10. not (Logical NOT)


11. and (Logical AND)

12. or (Logical OR)

13. = and compound assignment operators (+=, -=, etc.)

Operator Overloading

Python allows you to change the behavior of its operators based on user-
defined data types by implementing special methods (also known as dunder
methods or magic methods).

Example of Operator Overloading:

python

Copy code

class Point:

def __init__(self, x, y):

self.x = x

self.y = y

# Overloading the '+' operator

def __add__(self, other):

return Point(self.x + other.x, self.y + other.y)

def __str__(self):

return f"({self.x}, {self.y})"

p1 = Point(1, 2)

p2 = Point(3, 4)

print(p1 + p2) # Output: (4, 6)


In Python, "ops" typically refers to operators, which are symbols or
keywords used to perform operations on variables or values. Python provides
a wide range of operators to manipulate data. These operators can be
classified into several types based on the operation they perform:

1. Arithmetic Operators

These operators are used to perform mathematical operations like addition,


subtraction, multiplication, etc.

Operat Exampl
Description
or e

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division (float) x/y

// Floor Division x // y

Modulus
% x%y
(remainder)

Exponentiation
** x ** y
(power)

Examples:

python

Copy code

x = 10

y=3

print(x + y) # 13

print(x - y) # 7

print(x * y) # 30

print(x / y) # 3.3333333333333335

print(x // y) # 3 (floor division)


print(x % y) # 1 (remainder)

print(x ** y) # 1000 (10 raised to the power of 3)

2. Assignment Operators

Assignment operators are used to assign values to variables.

Operat Exampl
Description
or e

Assigns value to a
= x = 10
variable

+= Adds and assigns x += 3

-= Subtracts and assigns x -= 3

*= Multiplies and assigns x *= 3

/= Divides and assigns x /= 3

Floor divides and


//= x //= 3
assigns

%= Modulus and assigns x %= 3

Exponentiates and
**= x **= 3
assigns

Examples:

python

Copy code

x=5

x += 2 # x = x + 2 -> x becomes 7

x *= 3 # x = x * 3 -> x becomes 21

print(x) # Output: 21

3. Comparison Operators
These operators are used to compare two values. The result of a comparison
is always a boolean (True or False).

Operat Exampl
Description
or e

== Equal to x == y

!= Not equal to x != y

> Greater than x>y

< Less than x<y

Greater than or
>= x >= y
equal to

Less than or equal


<= x <= y
to

Examples:

python

Copy code

x = 10

y=5

print(x == y) # False

print(x != y) # True

print(x > y) # True

print(x <= y) # False

4. Logical Operators

Logical operators are used to perform logical operations on boolean values.

Operat Exampl
Description
or e

and True if both are True x and y


Operat Exampl
Description
or e

True if at least one is


or x or y
True

not Reverses the result not x

Examples:

python

Copy code

x = True

y = False

print(x and y) # False

print(x or y) # True

print(not x) # False

5. Bitwise Operators

Bitwise operators perform operations on the binary representations of


integers.

Operat
Description Example
or

& Bitwise AND x&y

Bitwise
` `
OR

Bitwise XOR (exclusive


^ x^y
OR)

Bitwise NOT
~ ~x
(inversion)

Left shift (shift bits


<< x << y
left)
Operat
Description Example
or

Right shift (shift bits


>> x >> y
right)

Examples:

python

Copy code

x = 5 # 101 in binary

y = 3 # 011 in binary

print(x & y) # 1 (binary 001)

print(x | y) # 7 (binary 111)

print(x ^ y) # 6 (binary 110)

print(~x) # -6 (inversion: all bits flipped)

print(x << 1) # 10 (binary 1010, shifted left by 1 bit)

print(x >> 1) # 2 (binary 10, shifted right by 1 bit)

6. Membership Operators

Membership operators are used to check whether a value exists in a


sequence like a list, tuple, string, etc.

Operat Exampl
Description
or e

in Returns True if a value exists in the sequence x in y

Returns True if a value does not exist in the x not in


not in
sequence y

Examples:

python

Copy code
lst = [1, 2, 3, 4]

print(2 in lst) # True

print(5 not in lst) # True

7. Identity Operators

Identity operators are used to compare the memory locations of two objects.

Operat Exampl
Description
or e

Returns True if both variables point to the


is x is y
same object

Returns True if both variables point to x is not


is not
different objects y

Examples:

python

Copy code

x = [1, 2, 3]

y = [1, 2, 3]

z=x

print(x is z) # True (z points to the same object as x)

print(x is y) # False (x and y are different objects, even though their


contents are the same)

print(x is not y) # True

8. Ternary Operator (Conditional Expression)

The ternary operator is a compact form of conditional statements. It’s used


to assign a value to a variable based on a condition.
Syntax:

python

Copy code

value_if_true if condition else value_if_false

Example:

python

Copy code

x = 10

y = 20

max_value = x if x > y else y

print(max_value) # Output: 20

9. Operator Precedence

In Python, operators have a predefined precedence (order of evaluation).


This defines which operators are evaluated first in an expression.

Precedence Table (from highest to lowest):

1. ** (Exponentiation)

2. ~, +, - (Unary operators)

3. *, /, //, % (Multiplication, Division, Floor Division, Modulus)

4. +, - (Addition, Subtraction)

5. <<, >> (Bitwise shift operators)

6. & (Bitwise AND)

7. ^ (Bitwise XOR)

8. | (Bitwise OR)

9. ==, !=, >, >=, <, <= (Comparison operators)

10. not (Logical NOT)


11. and (Logical AND)

12. or (Logical OR)

13. = and compound assignment operators (+=, -=, etc.)

Operator Overloading

Python allows you to change the behavior of its operators based on user-
defined data types by implementing special methods (also known as dunder
methods or magic methods).

Example of Operator Overloading:

python

Copy code

class Point:

def __init__(self, x, y):

self.x = x

self.y = y

# Overloading the '+' operator

def __add__(self, other):

return Point(self.x + other.x, self.y + other.y)

def __str__(self):

return f"({self.x}, {self.y})"

p1 = Point(1, 2)

p2 = Point(3, 4)

print(p1 + p2) # Output: (4, 6)

You might also like