Course Six - Operators in Python

Operators in Python

By ienex



Operators are symbols that perform operations on variables or values. For example, in the expression 4 + 5 = 9, the numbers 4 and 5 are operands, and + is the operator.

Python provides several types of operators, including:

  1. Arithmetic Operators

  2. Comparison (Relational) Operators

  3. Assignment Operators

  4. Logical Operators

  5. Bitwise Operators

  6. Identity Operators

  7. Membership Operators

  8. Operator Precedence


1. Arithmetic Operators

Arithmetic operators perform basic mathematical operations.

Example:

a = 21
b = 10
c = 0

c = a + b
print("Line 1 - Value of c is", c)

c = a - b
print("Line 2 - Value of c is", c)

c = a * b
print("Line 3 - Value of c is", c)

c = a / b
print("Line 4 - Value of c is", c)

c = a % b
print("Line 5 - Value of c is", c)

a = 2
b = 3
c = a ** b
print("Line 6 - Value of c is", c)

a = 10
b = 5
c = a // b
print("Line 7 - Value of c is", c)

Output:

Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2
Line 5 - Value of c is 1
Line 6 - Value of c is 8
Line 7 - Value of c is 2
Operator Description Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division b / a
% Modulus (remainder) b % a
** Exponentiation a ** b
// Floor Division 9 // 2 = 4

2. Comparison (Relational) Operators

Comparison operators compare two values and return a Boolean result (True or False).

a = 21
b = 10

if a == b:
    print("Line 1 - a is equal to b")
else:
    print("Line 1 - a is not equal to b")

if a != b:
    print("Line 2 - a is not equal to b")
else:
    print("Line 2 - a is equal to b")

if a < b:
    print("Line 3 - a is less than b")
else:
    print("Line 3 - a is not less than b")

if a > b:
    print("Line 4 - a is greater than b")
else:
    print("Line 4 - a is not greater than b")

if a <= b:
    print("Line 5 - a is less than or equal to b")
else:
    print("Line 5 - a is not less than or equal to b")

if b >= a:
    print("Line 6 - b is greater than or equal to a")
else:
    print("Line 6 - b is not greater than or equal to a")

Output:

Line 1 - a is not equal to b
Line 2 - a is not equal to b
Line 3 - a is not less than b
Line 4 - a is greater than b
Line 5 - a is not less than or equal to b
Line 6 - b is greater than or equal to a
Operator Description Example
== Equal to a == b
!= Not equal to a != b
> Greater than a > b
< Less than a < b
>= Greater than or equal to a >= b
<= Less than or equal to a <= b

3. Assignment Operators

Assignment operators store values in variables, optionally performing operations at the same time.

a = 21
b = 10
c = 0

c = a + b
print("Line 1 - Value of c is", c)

c += a
print("Line 2 - Value of c is", c)

c *= a
print("Line 3 - Value of c is", c)

c /= a
print("Line 4 - Value of c is", c)

c = 2
c %= a
print("Line 5 - Value of c is", c)

c **= a
print("Line 6 - Value of c is", c)

c //= a
print("Line 7 - Value of c is", c)
Operator Description Example
= Assigns value c = a + b
+= Add and assign c += ac = c + a
-= Subtract and assign c -= ac = c - a
*= Multiply and assign c *= ac = c * a
/= Divide and assign c /= ac = c / a
%= Modulus and assign c %= ac = c % a
**= Exponent and assign c **= ac = c ** a
//= Floor divide and assign c //= ac = c // a

This covers the basic arithmetic, comparison, and assignment operators in Python. Logical, bitwise, identity, and membership operators follow similar principles.


Comments

Popular posts from this blog

Why You Should Learn JavaScript and Python in 2025

Why you Should Learn JavaScript and Python ?