‘and’ Operator

Outputs False if either of the operands is False. That means, The ‘and’ operator produces True if the values on the left and right are both True.

Suppose, If we are comparing two statement using ‘and’ operator. If any of the two statement is False, then the output will be False.

In the below programme we will check criteria for a person entering movie hall.

The person should have ticket and shoud not be carrying any food items.

Try changing variable values to explore how 'and' operator works

age = 17
isCarryingFood = False
if (age > 18 and isCarryingFood == False) :
print("Is allowed to enter")
else :
print("Is not allowed to enter")

#### Since nonzero numbers are equal to True, the ‘and’ operator can also be used with numbers.

When two values are combined with the and operator, the right value is produced. This is why 1 and True produces True, while True and 1 produces 1.

An expression of true values combined with and produces the last value

print("Value of 1 and True is ", 1 and True)
print("Value of 100 and True is ", 100 and True)
print("Value of 0 and True is ", 0 and True)
print("Value of \"one\" and \"two\" and \"three\" is " ,"one" and "two" and "three")
print("Value of \"three\" and \"two\" and \"one\" is ", "three" and "two" and "one")
print("Value of 10 and 20 and 0 is ", 10 and 20 and 0)

x = 10
y = 20
z = 30
print(x>5 and y>5)
print(x>15 and y>15)
print(x>40 and y>80)
print(x>20 and y>30 and z<50)

Write a programme to check condition of voting. If a person has valid voter id and not already voted only then he is allowed to vote.

Solution

hasValidVoterID = True

hasNotVoted = False

if ( hasValidVoterID and hasNotVoted ) :

print("Is allowed to vote")

else :

print("Ineligible to vote")