Translate

Monday, October 13, 2025

PP_Operators in Python

 In Python, operators are special symbols that perform operations on variables and values. The operators may perform the operations only on a specific data type. For example, we cannot find the square root of a string. Knowledge of these operators forms the building blocks of all programs. These operators include: 

1. Arithmetic Operators

Arithmetic operators perform mathematical operations. They include:

Operator                 Name                 Example          Result

                              Addition            5 + 4                 9

                               Subtraction       5 - 3                  2

                               Multiplication 5 * 4                  20

                               Division             5 / 2                 2.5 (Will always return a float)

                             Modulus             5 % 3                 2

**                             Exponentiation 5 ** 2                 25

//                             Floor Division 5 // 2 2 (Unlike a the modulus, floor division truncates the decimal part)

2. The Assignment Operators

The assignment operator assigns values to variables.

Operator                                         Example                                         Same as

                                                       x = 5                                               x = 5

+=                                                     x += 4                                             x = x + 4

-=                                                      x -= 2                                              x = x - 2

*=                                                     x *= 5                                              x = x * 5

/=                                                     x /= 2                                              x = x / 2

**=                                                   x **= 5                                            x = x ** 5

//=                                                   x //= 4                                            x = x // 4

%=                                                    x %= 4                                            x = x % 4



3. Comparison Operators

Comparison operators compare two values and return a Boolean result.

Operator                                 Name                             Example                     Result

==                                             Equal to                         10 == 5                         False

!=                                               Not equal to                  5 != 4                            True

                                                Greater than                 5 > 5                             False

                                                Less than                      10 < 3                            False

<=                                              Less than or equal to 4 <= 3                            False

>=                                             Greater than or equal to 2 >= 2                       True

4. Logical Operators

Logical operators combine conditional statements.

Operator      Description                                                             Example

and                Returns True if both statements are true.        x > 3 and x < 10

or                   Returns True if at least one statement is true. x > 3 or x < 1

not      Reverses the result; returns False if the result is true. not(x > 3 and x < 10)

5. Identity Operators

These are used to compare the memory locations.

Operator     Description                     Example

is                   Returns True if both variables point to the same object. x is y

is not       Returns True if both variables do not point to the same object. x is not y

Note:

If

a = ["John", "Joan"]

b = ["Joan", "John"]

c = a

c is a reference to the same object as a

a and b are different objects in memory

a is equal b, because they have the same content

6. Membership Operators

These are test if a sequence contains a specific value.

Operator     Description     Example                                                                       Result

in                    Returns True if a value is found in the sequence. 'a' in 'Joan'     True

not in    Returns True if a value is not found in the sequence. 'z' not in 'John' True

7. Bitwise Operators

Bitwise operators operate on numbers bit by bit. 

Operator                                             Name                                         Example

                                                            AND                                         a & b

                                                                                                            OR

                                                            XOR                                          a ^ b

                                                            NOT                                         ~a

<<                                                          Left Shift                                   a << 2

>>                                                          Right Shift                                 a >> 2


End of Module Quiz

End of module quiz

1. What is the output of the Python snippet? 

`x = 20 

y = 22.5 

z = '20' 

result = x + y + float(z) 

print(result)`

A. TypeError

B. 42.5

C. 62

D. 62.5

Hint: Take note of the float

2. What is the data type of the variable `is_active` after running the following code? 

x = 10 

y = 500 

score = (x > y)

A. True

B. String

C. False

D. Boolean

Hint: Comparison operator

 


No comments:

Post a Comment