payment by upi: sinhamit@icici or payment by bank account name: amit kumar sinha, account number: 2646728782 IFSC code: KKBK0005660 SWIFT: KKBKINBB

Please support if you like my work by payment through upi: sinhamit@icici or payment by bank

account name: Amit Kumar Sinha,
account number: 2646728782
IFSC code: KKBK0005660
SWIFT: KKBKINBB


Comparison, Logical, Operators   in Category: Python   by amit

🕙 Posted on 2023-06-30 at 18:30:31     Read in Hindi ...


More on Operators

    Conditional (or comparison) operators work with those expressions and operands which when compared with these operators, outputs a boolean value, that is, True or False. Logical operators enhance the functionality of these comparisons. Let's learn about these operators.

Comparison Operators

    In Python, there are only few comparison operators, that is, == (equal to), != (not equal to), < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to). There are no ===, !==, <>, <=>, and other similar operators unlike PHP and JavaScript. In following example, you can see that 5 == '5' outputs False, which is opposite of OUTPUT in PHP and JavaScript.

print( 5 == 5 )   # Outputs True

print( 5 == '5' )   # Outputs False

print( 5 != '5' )   # Outputs True

print( 5 != 6 )   # Outputs True

print( 5 < 6 )   # Outputs True

    Though Python is a loose data-type programming language, it is different from PHP and JavaScript. There is no such thing like loose comparison or strict comparison in Python. Other comparison operators, that is, >, <=, >= work as expected in above examples.

Logical Operators

    There are no &&,   ||   and   !   operators like PHP and JavaScript. In Python, the reserved keywords, and,   or   not   are used as logical operators. When literal or expression of both operand are evaluated as True, then output is True in and logical comparison, otherwise False will be returned.

    When either one of two operands is evaluated as True, then output is True in or logical comparison. When both operands are evaluated as False, then only False will be returned in or logical comparison. When not logical operator is used before an operand, then its value is changed to opposite of it. Let's see some example of logical operators:

print( True and True )   # Outputs True

print( False and True )   # Outputs False

print( False and False )   # Outputs False

print( True or True )   # Outputs True

print( False or True )   # Outputs True

print( False or False )   # Outputs False

print( not True )   # Outputs False

print( not False )   # Outputs True

Bitwise Operators

    Though bitwise operators &, |, and ^ can also output boolean results on comparison, it is not advisable to do so.

Bitwise and returns 1 when both operands have binary value of 1, otherwise 0 is returned

print( False & False )   # Outputs False

print( True & False )   # Outputs False

print( False & True )   # Outputs False

print( True & True )   # Outputs True

Bitwise or returns 1 when either of both operands, has binary value of 1. When both operands have binary value of 0, then 0 is returned.

print( True | True )   # Outputs True

print( False | True )   # Outputs True

print( True | False )   # Outputs True

print( False | False )   # Outputs False

Bitwise xor returns 0 when both operands are same binary value, otherwise 1 is returned.

print( False ^ False )   # Outputs False

print( True ^ True )   # Outputs False

print( False ^ True )   # Outputs True

print( True ^ False )   # Outputs True

Bitwise not flips the binary value of operand.

print( ~ True )   # Outputs -2

print( ~ False )   # Outputs -1

Identity Operators

    is checks whether two variable names are of same object (not data-type), and is not is used to check whether they are different object. id() function is used to know the memory location of an object. id() is a built-in function, which returns the Identity of the object. In following example, you can see that id() of both x and y are same.

x = 5
y = 5
print( id(x) )
print( id(y) )
print( x is y ) # Output is shown below:

2018911322480
2018911322480
True

print( 5 is 5 )   # Outputs True with following ERROR:

C:\xampp\htdocs\python2023>py new.py
C:\xampp\htdocs\python2023\new.py:4: SyntaxWarning: "is" with a literal. Did you mean "=="?
    print( 5 is 5 )
True

    In the below example, you can see that both variable names a and b have same value and same data-type, that is of list, but their id() is different and therefore, output is False when compared with is operator. Also, when both a and b are compared with is not operator, the output is True.

a = [1, 2, 3]
b = [1, 2, 3]
print( id(a) )
print( id(b) )
print( a is b )

    The above example outputs as follows:

2290722944192
2290722878400
False

print( a is not b )   # Outputs True

Membership Operators

    in and not in operators are used to check whether a value of a variable name is in a sequence data-type, for example, string, list, etc. or not.

x = 5   # integer data-type
y = [1, 2, 3, 4, 5]   # list data-type
print( x in y )   # Outputs True
print( id(x) )
print( id( y[4] ) )   # 4 within brackets after 'y' is the index number of 5 (last item)

    The above example outputs as shown below:

True
2920055505264
2920055505264

    In Python, a literal value, for example, 5 whether assigned to a variable name directly or assigned to an item of a sequence, has only one memory location, as shown above. Therefore, when identity of 5 is checked by id() function, both id(x) and id( y[4] ) outputs the same numeric value of memory location.

    When you run/execute any code, that is, any of above example, the memory location (RAM) assigned to a literal is temporary, and will be changed every time. Therefore, you will get different value for memory locations of a literal value. Hope, you have understood the concept, how Python interpreter stores variables in RAM.


Leave a Comment: