Day # 2 🐍 Unlocking the Power of Python: Operators in Python

Day # 2 🐍 Unlocking the Power of Python: Operators in Python

On Day#1, we learn about types of Variables in Python. Now let,s move ahead with next important concept i.e. Operators.

 

Operators are specific symbols which are used to perform action between two operands. Like any other programming language Python has some operators to perform several tasks. Let us go through each operator along with simple examples.

 

1. Arithmetic Operators

 Arithmetic operators are used to perform mathematical operations on numeric values.

1.1)Addition (+):

    a = 5

  b = 3

  result = a + b

  print(result)  # Output: 8

 

1.2)Subtraction (-):

    a = 7

  b = 4

  result = a - b

  print(result)  # Output: 3

 

1.3)Multiplication (*):

 a = 2

  b = 6

  result = a * b

  print(result)  # Output: 12

 

 

1.4)Division (/):

  a = 10

  b = 2

  result = a / b

  print(result)  # Output: 5.0

 

1.5)Modulus (%):   Returns the remaining numbers by dividing the first number from the second. It is also known as the Python modulo.

 

For example, we have two numbers, 24 and 5. And we can get the remainder by using the modulus or modulo operator between the numbers 24 % 5. Here 24 is divided by 5 that returns 4 as the remainder, and 4 as the quotient. When the first number is completely divisible by another number without leaving any remainder, the result will be 0. For example, we have two numbers, 20 and 5. And we can get the remainder by using the modulus or modulo operator between the numbers 20 % 5. Here 20 is divided by 5 that returns 0 as the remainder, and 4 as the quotient.

 

  a = 10

  b = 3

  result = a % b

  print(result)  # Output: 1

 

1.6)Exponentiation (**):

 

  a = 2

  b = 3

  result = a ** b

  print(result)  # Output: 8

 

1.7)Floor Division (//): It returns integer part only after performing division.

  a = 15

  b = 4

  result = a // b

  print(result)  # Output: 3

 

2. Logical Operators:

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

 

2.1)AND (and): The condition will also be true if the expression is true. If the two expressions x and y are the same, then x and y must both be true.

    x = True

  y = False

  result = x and y

  print(result)  # Output: False

 

2.2) OR (or): The condition will be true if one of the phrases is true.

 

  x = True

  y = False

  result = x or y

  print(result)  # Output: True

 

2.3)NOT (not): If an expression x is true, then not (x) will be false and vice versa.

  x = True

  result = not x

  print(result)  # Output: False

 

 

3. Comparison Operators :

 Comparison operators are used to compare values and return Boolean results.

 

3.1)Equal to (==):

  a = 5

  b = 5

  result = a == b

  print(result)  # Output: True

 

 

3.2)Not equal to (!=):

  a = 7

  b = 3

  result = a != b

  print(result)  # Output: True

 

 

3.3)Greater than (>):

  a = 8

  b = 5

  result = a > b

  print(result)  # Output: True

 

3.4) Less than (<):

    a = 3

  b = 6

  result = a < b

  print(result)  # Output: True

 

 

3.5) Greater than or equal to (>=):

  a = 5

  b = 5

  result = a >= b

  print(result)  # Output: True

 

 

3.6)Less than or equal to (<=):

 a = 4

  b = 7

  result = a <= b

  print(result)  # Output: True

 

 

4. Assignment Operators :

Assignment operators are used to assign values to variables.

 

4.1)Assignment (=): to assign values to operands

    x = 10 (this will assign value 10 to x)

 

 

4.2)Add and assign (+=):

   x = 5

  x += 3  # Equivalent to x = x + 3

 

 

4.3) Subtract and assign (-=):

  y = 8

  y -= 2  # Equivalent to y = y - 2

 

4.4) Multiply and assign (*=):

  z = 4

  z *= 2  # Equivalent to z = z * 2

 

4.5) Divide and assign (/=):

  w = 16

  w /= 4  # Equivalent to w = w / 4

 

5. Bitwise Operators :

 Bitwise operators perform operations on binary representations of integers.

 

5.1) Bitwise AND (&):

  a = 5   # Binary: 0101

  b = 3   # Binary: 0011

  result = a & b

  print(result)  # Output: 1 (Binary: 0001)

 

5.2) Bitwise OR (|):

 

  a = 5   # Binary: 0101

  b = 3   # Binary: 0011

  result = a | b

  print(result)  # Output: 7 (Binary: 0111)

 

5.3) Bitwise XOR (^):

  a = 5   # Binary: 0101

  b = 3   # Binary: 0011

  result = a ^ b

  print(result)  # Output: 6 (Binary: 0110)

 

5.4) Bitwise NOT (~):

  a = 5   # Binary: 0101

  result = ~a

  print(result)  # Output: -6

 

 

5.5) Left Shift (<<):

    a = 5   # Binary: 0101

  result = a << 1

  print(result)  # Output: 10 (Binary: 1010)

 

5.6) Right Shift (>>):

  a = 5   # Binary: 0101

  result = a >> 1

  print(result)  # Output: 2 (Binary: 0010)

 

6. Membership Operators:

 Membership operators are used to test if a value is a member of a sequence.

 

6.1) In:

  fruits = ['apple', 'banana', 'orange']

  result = 'banana' in fruits

  print(result)  # Output: True

 

6.2) Not In:

  fruits = ['apple', 'banana', 'orange']

  result = 'grape' not in fruits

  print(result)  # Output: True

 

 

7. Identity Operators : 

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

 

7.1) Is: If the references on both sides point to the same object, it is determined to be true.

x = [1, 2, 3]

y = [1, 2, 3]

result = x is y

print(result)  # Output: False

 

7.2) Is Not: If the references on both sides do not point at the same object, it is determined to be true.

  x = [1, 2, 3]

  y = [1, 2, 3]

  result = x is not y

  print(result)  # Output: True

 

In conclusion, Python operators play a crucial role in performing various operations on data. Understanding these operators is essential for writing effective and efficient Python code. By mastering these concepts, you'll have the foundation needed to tackle more complex programming challenges.

To view or add a comment, sign in

More articles by Abhijeet Mohite

Insights from the community

Others also viewed

Explore topics