What Are Operators and How Do We Use Them ?

What Are Operators and How Do We Use Them ?


Ever wondered how computers do math and logic ?


It's all thanks to operators!


Operators in Python are special symbols or keywords that tell the computer to perform specific mathematical, logical, or other operations.


Think of them as instructions that guide the computer on what to do with the data.


Let's dive into the world of operators in Python, which are like magic tools that help us perform different operations.


Let's explore the different types of operators and see some cool examples.


1. Python Arithmetic Operators

Arithmetic operators help us perform basic math operations.


Here's a list of arithmetic operators in Python :

1. Addition (+) : Adds two numbers.

2. Subtraction (-) : Subtracts one number from another.

3. Multiplication (*) : Multiplies two numbers.

4. Division (/) : Divides one number by another.

5. Floor Division (//) : Divides and returns the largest whole number.

6. Modulus (%) : Returns the remainder of the division.

7. Exponentiation () : Raises one number to the power of another.


Let's understand each arithmetic operators with examples :


- Addition :

  a = 5

  b = 3

  result = a + b

  print("Addition:", result)  

# Output: Addition : 8        


- Subtraction :

  a = 7

  b = 4

  result = a - b

  print("Subtraction :", result)  

# Output: Subtraction : 3
        


- Multiplication :

  a = 6

  b = 5

  result = a * b

  print("Multiplication :", result)  

# Output: Multiplication : 30        


- Division :

  a = 10

  b = 3

  result = a / b

  print("Division :", result)  

# Output : Division : 3.3333333333333335        


- Floor Division :

  a = 10

  b = 3

  result = a // b

  print("Floor Division :", result)  

# Output: Floor Division : 3        


- Modulus :

  a = 10

  b = 3

  result = a % b

  print("Modulus :", result)  

# Output: Modulus : 1        


- Exponentiation :

  a = 2

  b = 3

  result = a ** b

  print("Exponentiation :", result)  

# Output : Exponentiation : 8        

2. Python Bitwise Operators

Bitwise operators work on the binary representations of integers.


Here's a list of Bitwise Operators in Python :

1. Bitwise AND (&) : Compares corresponding bits and returns 1 if both are 1.

2. Bitwise OR (|) : Compares corresponding bits and returns 1 if at least one is 1.

3. Bitwise XOR (^) : Compares corresponding bits and returns 1 if only one is 1.

4. Bitwise NOT (~) : Inverts all the bits.

5. Left Shift (<<) : Shifts bits to the left, adding zeros at the end.

6. Right Shift (>>) : Shifts bits to the right, discarding bits at the end.


Let's understand each Bitwise Operators with examples :


- Bitwise AND :

  a = 5  # 0101 in binary

  b = 3  # 0011 in binary

  result = a & b

  print("Bitwise AND :", result)  

# Output : 1 (0001 in binary)        


- Bitwise OR :

a = 5  # 0101 in binary

  b = 3  # 0011 in binary

  result = a | b

  print("Bitwise OR :", result)  

# Output : 7 (0111 in binary)        


- Bitwise XOR :

  a = 5  # 0101 in binary

  b = 3  # 0011 in binary

  result = a ^ b

  print("Bitwise XOR :", result)  

# Output : 6 (0110 in binary)        


- Bitwise NOT :

  a = 5  # 0101 in binary

  result = ~a

  print("Bitwise NOT :", result)  

# Output : -6 (1010 in binary for 32-bit integers)        


- Left Shift :

  a = 5  # 0101 in binary

  result = a << 2

  print("Left Shift :", result)  

# Output : 20 (10100 in binary)        


- Right Shift :

  a = 10  # 1010 in binary

  result = a >> 1

  print("Right Shift :", result)  

# Output : 5 (0101 in binary)        

3. Python Comparison Operators

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


Here's the list of Comparison Operators in Python :

1. Equal to (==) : True if both values are equal.

2. Not equal to (!=) : True if values are not equal.

3. Greater than (>) : True if the first value is greater.

4. Less than (<) : True if the first value is less.

5. Greater than or equal to (>=) : True if the first value is greater or equal.

6. Less than or equal to (<=) : True if the first value is less or equal.


Let's understand each Bitwise Operators with examples :


- Equal to :

  a = 5

  b = 5

  result = a == b

  print("Is", a, "equal to", b, "?", result)  

# Output : Is 5 equal to 5 ? True        


- Not equal to :

  a = 5

  b = 6

  result = a != b

  print("Is", a, "not equal to", b, "?", result)  

# Output : Is 5 not equal to 6 ? True        


- Greater than :

  a = 6

  b = 5

  result = a > b

  print("Is", a, "greater than", b, "?", result)  

# Output : Is 6 greater than 5 ? True        


- Less than :

  a = 5

  b = 6

  result = a < b

  print("Is", a, "less than", b, "?", result)  

# Output : Is 5 less than 6 ? True        


- Greater than or equal to :

  a = 6

  b = 6

  result = a >= b

  print("Is", a, "greater than or equal to", b, "?", result)  

# Output : Is 6 greater than or equal to 6 ? True        


- Less than or equal to :

  a = 5

  b = 6

  result = a <= b

  print("Is", a, "less than or equal to", b, "?", result)  

# Output : Is 5 less than or equal to 6 ? True        

4. Python Logical Operators

Logical operators combine multiple conditions.


Here's the list of Logical Operators in Python :

1. Logical AND (and) : True if both conditions are True.

2. Logical OR (or) : True if at least one condition is True.

3. Logical NOT (not) : True if the condition is False.


Let's understand each Logical Operators with examples :


- Logical AND :

  a = True

  b = False

  result = a and b

  print("Logical AND :", result)  

# Output : Logical AND : False        


- Logical OR :

  a = True

  b = False

  result = a or b

  print("Logical OR :", result)  

# Output : Logical OR: True        


- Logical NOT :

  a = True

  result = not a

  print("Logical NOT :", result)  

# Output : Logical NOT : False        

5. Python Ternary Operators

The ternary operator is a shorthand way to perform conditional assignments.


The syntax is :

x = <value_if_true> if <condition> else <value_if_false>        


Let's understand each Ternary Operators with examples :


- Assigning Maximum Value :

  x = 10

  y = 20

  max_value = x if x > y else y

  print("Maximum value:", max_value)  

# Output : Maximum value : 20

        


- Checking Even or Odd :

  num = 15

  result = "Even" if num % 2 == 0 else "Odd"

  print(num, "is", result)  

# Output : 15 is Odd        


- Assigning Absolute Value :

  number = -10

  absolute_value = number if number >= 0 else -number

  print("Absolute value :", absolute_value)  

# Output : Absolute value : 10        


- Checking for Validity :

  username = "john_doe"

  message = "Valid" if len(username) >= 8 else "Invalid"

  print("Username is", message)  

# Output : Username is Valid        


- Handling Division by Zero :

  dividend = 10

  divisor = 0

  result = dividend / divisor if divisor != 0 else "Error : Division by Zero"

  print("Result :", result)  

# Output : Result : Error : Division by Zero        

Understanding operators is a key step in mastering Python programming. They help us perform calculations, make decisions, and manipulate data efficiently.


If you enjoyed this article, please like and comment. For more insights on my Python learning journey, feel free to connect with me.


So, go ahead and experiment with these operators in your own code.

Thank you for reading!


Happy coding! ??

Rahul K

Student at Bangalore North University

8 个月

Informative insight ??

回复

要查看或添加评论,请登录

Subhash Kumar Yadav的更多文章

  • Understanding Python Dictionaries

    Understanding Python Dictionaries

    Have you ever thought about how your computer knows where to find your favorite game or how your phone stores your…

    1 条评论
  • Understanding Python Statements : A Simple Explanation

    Understanding Python Statements : A Simple Explanation

    Hello, Imagine if you could create your own digital world just by writing a few lines of code. What if you could make a…

    3 条评论
  • Python Functions Explained : A Simple Guide for Beginners

    Python Functions Explained : A Simple Guide for Beginners

    Hello , Have you ever wondered how Python functions can make your coding life easier ? Imagine having a magical tool…

    2 条评论
  • Understanding Lists in Python.

    Understanding Lists in Python.

    Python is a popular programming language known for its simplicity and versatility. One of the most powerful features in…

  • Understanding Python Data Types

    Understanding Python Data Types

    Python is a versatile and powerful programming language that is widely used for various applications, from web…

    1 条评论
  • Understanding Variables in Python

    Understanding Variables in Python

    What is a Variable ? In programming, a variable is like a box with a label on it. You can put different pieces of…

    1 条评论
  • Story of Python: A Magic Language That Changes the World.

    Story of Python: A Magic Language That Changes the World.

    Hello, Today, let's travel through the magical world of Python, a special language that has done amazing things and…

  • Introduction to Python Programming

    Introduction to Python Programming

    Python is a versatile and widely-used programming language known for its simplicity, readability and robustness…

    2 条评论
  • The Story of Python: A Magic Language That Changes the World.

    The Story of Python: A Magic Language That Changes the World.

    Hello, Today, let's travel through the magical world of Python, a special language that has done amazing things and…

社区洞察

其他会员也浏览了