Python Operators: Part-2

I am going to explain about the following Python operators in this post-

  1. Logical Operators
  2. Bitwise Operators
  3. Identity Operators
  4. Membership Operators

I have covered some other operators in this post- Python Operators : Part-1

1. Logical Operators

Logical operators are used to combine two or more conditions and evaluate them to produce a Boolean output i.e. True or False.

The logical operators in Python are-

  • and- Prints True if both sides of the operator are correct, and otherwise returns false.
  • or- Returns True at least one side of the operator is correct.
  • not- Prints the opposite of the Boolean output. For example, if the output is True, it will print False and vice-versa.

For example-

x=5
y=10
z=15
print(x<y and y<z)
print(x<y or y>z)
print(not(x<y))

The output-

True
True
False

In the first line, we have used the and operator. It gives the output True because both x<y and y<z are correct.

In the second line, we have used the or operator. While y>z is false, it still gives the output as True as the or operator requires only one side of the operator to be correct and in this case, x<y is correct, and therefore, the output becomes True.

In the third line, x<y is correct, but since we have used the not operator, which gives the opposite result, the output comes as False.

2. Bitwise Operators

Bitwise operators are used to perform bitwise operations on the binary representations of Integers. They are mainly used in low-level programming.

The bitwise operators in Python are-

  • Bitwise AND: &
  • Bitwise OR: |
  • Bitwise XOR: ^
  • Bitwise NOT: ~
  • Left Shift: <<
  • Right Shift: >>

3. Identity Operators

There are two in-built identity operators in Python-

  • is
  • is not

The is operator gives the output as True if the two objects being compared point to the same memory location.

On the other hand, the is not operator prints True if the two objects being compared don't point to the same memory location.

For example-

x=[1,2,3]
y=[1,2,3]
z=x
print(x is y)
print(x is z)

The output:

False
True

In the above code, x and y have the same elements but they are not the same object, hence the output is returned as False.

However, in case of x and z, the same object x has been assigned to z using the assignment operator = and thus, the output is given as True.

4. Membership Operators

Python has two in-built membership operators, in and not in. They are used to check whether a value is a member of a sequence or not. In simple words, it checks if the string/integer/floating-point number is part of a list or not.

For example-

fruits=["apples","bananas","watermelons"]
if "watermelons" not in fruits:
  print("Watermelon is not a fruit.")
else:
  print("Watermelon is a fruit.")

The output;

Watermelon is a fruit.

In the above code, the not in membership operator, checks if watermelons is in the list or not and then gives the specific output.


That is all for this post. Hope you found it informative and accurate.

Check out my stories on Medium!

Regards,

Aarav Iyer

Aarav Iyer

I am a technology and programming enthusiast, currently a high school student. I also love drawing and am fairly interested in aeronautics and astrophysics. My favourite pastimes are reading books, blogging and skywatching with my telescope.

Post a Comment

Previous Post Next Post