13. Python Assignment Operators & Logical Operators

 



Assignment Operators

Assignment operator means something assign value to the variable.

Ex: x =10 # it’s a simple assignment.

We have another type of assignment operator are there.

Ex: x+=10      # compound assign operator.

 In python, multiple compound assign operators are there. See below.

 

Operator

Operator Name

Description

Example

'='

Assignment

This assignment assigning the values to variables

x=10

 

'+='

Addition Assignment

This assignment adding with assigning the values to variables

x+=20 or

x=x+20

'-='

Subtraction Assignment

This assignment Subtraction with assigning the values to variables

x-=20 or

x=x-20

'*='

Multiplication Assignment

This assignment Multiplication with assigning the values to variables

X*=20 or

x=x*20

'/='

Division Assignment

This assignment Division with assigning the values to variables

X/=20 or

x=x/20

'**='

Exponentiation

Assignment

This assignment Exponentiation with assigning the values to variables

X**=20 or

x=x**20

'//='

Floor Division Assignment

This assignment Floor Division with assigning the values to variables

X//=20 or

x=x//20

'%='

Remainder Assignment

This assignment Floor Remainder with assigning the values to variables

X%=20 or

x=x%20

 

For example,

x = 10

y = 20

x+=y

print (x)

30   # Result

x = 10

y = 20

x-=y

print (x)

-10   # Result

x = 10

y = 20

x*=y

print (x)

200   # Result

x = 10

y = 20

x/=y

print (x)

0.5   # Result

x = 10

y = 20

x**=y

print (x)

100000000000000000000 5   # Result

x = 20

y = 10

x//=y

print (x)

2   # Result

x = 10

y = 20

x%=y

print (x)

10   # Result

 Logical Operators:

The logical operators are used for comparing given values. The values, logical operators operate evaluate to either True or False. The result of the logical operator is always a Boolean value, True or False.

Operator

Operator Name

Description

Example

'and'

Logical AND

The ‘AND’ perform as a

True, when both values are True

True ‘and’ False # Result is False

'or'

Logical OR

The ‘or’ operators result is

True, when any one of both values is True

10!=20 #False

'not'

Logical NOT

The ‘not’ operators result is

True, when any one of both values is True

10>20 #False

 This operator behaves differently, Boolean type and non-Boolean type.

The ‘AND’ perform as a True when both values are True.

Ex:

True and True                      True #Result

True and False                     False #Result

False and True                     False #Result

False and False                    False #Result

 The ‘or’ perform as a True, when anyone, both values is True.

 Ex:

 True or True                         True #Result

True or False                        True #Result

False or True                        True #Result

False or False                       False #Result

 ‘NOT’ operator is a complement operator. When any the result is True, if we apply the not operator then it’s changed to False.

 Ex:

not True                                False #Result

not False                               True #Result

check some examples using with ‘and’ operation

if anyone enters the username and password the same as it is in our database. Then it’s accepted, otherwise, it’s shown to an invalid user.

Basic authentication with and condition.

user_name = input('enter username: ')

user_password = input ('enter password: ')

if user_name==98426 and user_password==98500:

     print("login successfully")

else:

    print("invalid username and password")

 #Result

enter username: dfgrgd

enter password: sdftg

invalid username and password

 

user_name input("enter username: ")
user_password 
input ("enter password: ")
if user_name== '8426' and user_password== '8500':
     
print("login sucessfully")
else:
    
print("invalid username and password")

 #Result

 enter username: 8426

enter password: 8500

login successfully

 

now we are checking non-Boolean types with some examples.

 Logical operators are ‘and’, ‘or’, not.

In python ‘0’ zero means false and non-zero means True. And empty string, empty list, empty set, empty tuple, empty 'dict' is considered as false only.

 x and y in this case, the result has always given x or y only. Not a Boolean type.

 now we are checking non-Boolean types ‘and’ operator with some examples.

‘and’ operator always prefers to True only.

If the 'x' value is False, then the result is also x or False.

If the 'x' value is True, then the result will be y or False.

Ex:  10 and 20

In this case, 10 is equal to some value, which means it’s considered as a one or True. Then the result is y only, or 20 only.

Ex:  0 and 20

In this case, 0 is equal to no value, which means it’s considered as a zero or False. Then the result is x only, or 0 only.

Ex: ‘king’ and ‘queen’

In this case, 'king' is equal to some value, which means it’s considered as a one or True. Then the result is y only or queen only.

Ex: ‘ ’ and ‘queen’

In this case ‘’ is equal to no value, which means it’s considered as a zero or False. Then the result is x only, or ‘’ only.

 now we are checking non-Boolean types, ‘or’ operator with some examples.

 ‘or’ operator always prefers to True only.

If x value is True, then the result is also x or True.

If x value is False, then the result will be y or True.

Ex:  10 and 20

In this case, 10 is equal to some value, which means it’s considered as a one or True. Then the result is x only, or 10 only.

Ex:  0 and 20

In this case, 0 is equal to no value, which means it’s considered as a zero or False. Then the result is y only, or 20 only.

Ex: ‘king’ and ‘queen’

In this case, 'king' is equal to some value, which means it’s considered as a one or True. Then the result is x only or king only.

Ex: ‘ ’ and ‘queen’

In this case ‘’ is equal to no value, which means it’s considered as a zero or False. Then the result is y only or ‘queen’ only.

 now we are checking non-Boolean types, ‘not’ operator with some examples.

 ‘not’ operator always gives the result Boolean types only. (True or False)

 ‘not’ operator always gives the result as reverse type. It means if the result is True then it’s converted to False, if the result is False then it’s converted to True.

 Ex:  not 10

In this case, 10 is equal to some value, which means it’s considered as a one or True. Then the result is False only.

Ex: not 0

In this case, 0 is equal to no value, which means it’s considered as a zero or False. Then the result is True only.

Ex:  not ‘king’

In this case, 'king' is equal to some value, which means it’s considered as a one or True. Then the result is False only.

 Ex:  not ‘’

In this case ‘’ is equal to no value, which means it’s considered as a zero or False. Then the result is True only.

These are all used for certification or job interview purposes mainly.

 

 

 


0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post