Python Program: Operators (Arithmetic, Comparison, Logical)

 

### Python Program: Operators (Arithmetic, Comparison, Logical)

Python program demonstrating arithmetic, comparison, and logical operators with an explanation for each operator:

 

```python

# Arithmetic Operators

a = 10

b = 3        

 

# Addition

addition = a + b

print("Addition (a + b):", addition)  # Output: 13        

 

# Subtraction

subtraction = a - b

print("Subtraction (a - b):", subtraction)  # Output: 7        

 

# Multiplication

multiplication = a * b

print("Multiplication (a * b):", multiplication)  # Output: 30        

 

# Division

 
division = a / b

print("Division (a / b):", division)  # Output: 3.3333333333333335        

# Floor Division

floor_division = a // b

print("Floor Division (a // b):", floor_division)  # Output: 3        

 

# Modulus

modulus = a % b

print("Modulus (a % b):", modulus)  # Output: 1        

 

# Exponentiation

 
exponentiation = a ** b

print("Exponentiation (a ** b):", exponentiation)  # Output: 1000        

### Explanation - Arithmetic Operators:

   - +: Adds two operands (e.g., a + b gives 13).

   - -: Subtracts second operand from the first (e.g., a - b gives 7).

   - *: Multiplies two operands (e.g., a * b gives 30).

   - /: Divides first operand by the second (e.g., a / b gives 3.33...).

   - //: Performs floor division, returning the largest integer less than or equal to the division (e.g., a // b gives 3).

   - %: Returns the remainder of division (e.g., a % b gives 1).

   - **: Performs exponentiation (e.g., a ** b gives 1000).

 

# Comparison Operators

 
x = 5

y = 8        

# Equal to

print("Equal to (x == y):", x == y)  # Output: False        

 

# Not equal to

print("Not equal to (x != y):", x != y)  # Output: True        

 

# Greater than

print("Greater than (x > y):", x > y)  # Output: False        

 

# Less than

print("Less than (x < y):", x < y)  # Output: True        

 

# Greater than or equal to

print("Greater than or equal to (x >= y):", x >= y)  # Output: False        

 

# Less than or equal to

print("Less than or equal to (x <= y):", x <= y)  # Output: True        

 

### Explanation - Comparison Operators:

   - ==: Checks if two operands are equal (e.g., x == y gives False).

   - !=: Checks if two operands are not equal (e.g., x != y gives True).

   - >: Checks if the left operand is greater than the right (e.g., x > y gives False).

   - <: Checks if the left operand is less than the right (e.g., x < y gives True).

   - >=: Checks if the left operand is greater than or equal to the right (e.g., x >= y gives False).

   - <=: Checks if the left operand is less than or equal to the right (e.g., x <= y gives True).

 

# Logical Operators

p = True

q = False        

 

# Logical AND

print("Logical AND (p and q):", p and q)  # Output: False        

 

# Logical OR

print("Logical OR (p or q):", p or q)  # Output: True        

 

# Logical NOT

print("Logical NOT (not p):", not p)  # Output: False        

```

 

### Explanation - Logical Operators:

   - and: Returns True if both operands are True (e.g., p and q gives False).

   - or: Returns True if at least one operand is True (e.g., p or q gives True).

   - not: Returns True if the operand is False (e.g., not p gives False).

 

This program introduces basic operators used for performing mathematical calculations, making comparisons between values, and combining logical conditions in Python.

  • No alternative text description for this image
Like
Reply

To view or add a comment, sign in

More articles by Rama Krishna Vankam

Others also viewed

Explore content categories