2.1 Implement basic operators in Python.

In Python, operators are special symbols or keywords that carry out operations on values and python variables. They serve as a basis for expressions, which are used to modify data and executecomputations. Python contains several operators, each with its unique purpose.

Types of Python Operators: Python language supports various types of operators, which are:

  1. Arithmetic Operators
  2. Logical Operators
  3. Comparison (Relational) Operators
  4. Conditional operators
  5. Bitwise Operators
  1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc. For example,

OperatorOperationExample
+Addition2 + 2 = 4
-Subtraction2 - 2 = 0
*Multiplication2 * 2 = 4
/Division4 / 2 = 2
//Floor Division13 // 3 = 4
%Modulo7 % 2 = 1
**Power2 ** 4 = 16
Arithmatic Operators

Example 1: Arithmetic Operators in Python

a = 2
b = 2

# addition
print ('Sum: ', a + b)  
# subtraction
print ('Subtraction: ', a - b)   

# multiplication
print ('Multiplication: ', a * b)  

a=4
b=2
# division
print ('Division: ', a / b) 
a=13
b=3
# floor division
print ('Floor Division: ', a // b)
a=7
b=2
# modulo
print ('Modulo: ', a % b)  

a=2
b=4
# a to the power b
print ('Power: ', a ** b)

OUTPUT

Sum:  4
Subtraction:  0
Multiplication:  4
Division:  2.0
Floor Division:  4
Modulo:  1
Power:  16
=== Code Execution Successful ===

2. Python Logical Operators

OperatorExampleMeaning
anda and bLogical AND:
True only if both the operands are True
ora or bLogical OR:
True if at least one of the operands is True
notnot aLogical NOT:
True if the operand is False and vice-versa.

Example 2: Logical Operators

# logical AND
print(True and True)     # True
print(True and False)    # False

# logical OR
print(True or False)     # True

# logical NOT
print(not True)          # False
True
False
True
False

=== Code Execution Successful ===

3. Relational Operators

OperatorMeaningExample
==Is Equal To2 == 4 gives us False
!=Not Equal To2 != 4 gives us True
>Greater Than2 > 4 gives us False
<Less Than2 < 4 gives us True
>=Greater Than or Equal To2 >= 4 give us False
<=Less Than or Equal To2 <= 4 gives us True

Example 3: Relational Operators

a = 2
b = 4

# equal to operator
print('a == b =', a == b)

# not equal to operator
print('a != b =', a != b)

# greater than operator
print('a > b =', a > b)

# less than operator
print('a < b =', a < b)

# greater than or equal to operator
print('a >= b =', a >= b)

# less than or equal to operator
print('a <= b =', a <= b)
OUTPUT

a == b = False
a != b = True
a > b = False
a < b = True
a >= b = False
a <= b = True

=== Code Execution Successful ===

4. Conditional Operators (Ternary Operators)

In Python, Ternary Operator determines if a condition is true or false and then returns the appropriate value as the result.

Syntax: true_value if condition else false_value
# Program to demonstrate ternary operator
a = 5
b = -20

# python ternary operator
large = "a is largest" if a > b else "b is largest"

print(large)
OUTPUT:
a is largest

5. Bitwise Operators

Python bitwise operators are used to perform bitwise calculations on integers. The integers are first converted into binary and then operations are performed on each bit or corresponding pair of bits, hence the name bitwise operators. The result is then returned in decimal format.

OperatorMeaningExample
& Bitwise AND Sets each bit to 1 if both bits are 1a = 12 # 1100 in binary
b = 5 # 0101 in binary
result = a & b
print(result)
# Output: 4 (0100 in binary)
| Bitwise ORSets each bit to 1 if one of two bits is 1a = 12 # 1100 in binary
b = 5 # 0101 in binary
result = a | b
print(result)
# Output: 13 (1101 in binary)
 ^Bitwise XORSets each bit to 1 if only one of two bits is 1a = 12 # 1100 in binary b = 5 # 0101 in binary result = a ^ b print(result) # Output: 9 (1001 in binary)
~ Bitwise NOTInverts all the bitsa = 12 # 1100 in binary
result = ~a
print(result) # Output: -13 (in binary: -1101, due to two’s complement representation)
<< Bitwise Left ShiftShift left by pushing zeros in from the right and let the leftmost bits fall offa = 5 # 0101 in binary
result = a << 2
print(result) # Output: 20 (10100 in binary)
>> Bitwise Right ShiftShift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall offa = 20 # 10100 in binary
result = a >> 2
print(result) # Output: 5 (0101 in binary)
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top