And Keyword In Python

Overview:

- Python supports logical operator and through the and keyword.

 

- In mathematics, the and operation returns true only if both the

  operands evaluate to True. Otherwise the and operation returns False.

 

- In Python the and operator returns the first operand if the first operand evaluates to False. Otherwise it returns the second operand.

 

- The and operator in Python is a short circuit operator. That is the second operator will be evaluated by the and operator only if the first operand evaluated to True.

 

Truth Table for the 'and' operator:

Operator1

Operator2

Result

True

True

True

True

False

False

False

True

False

False

False

False

 

Ven Diagram for the operator 'and':

  • The Ven diagram of the and operator is given below.

 

  • This Ven diagram is about a search criteria stating the search results should include News that is only about Europe.

 

Example 1:

signal = "Green"

path   = "Pedestrian walking"

 

if signal is "Green" and path is "clear":

    print("All clear and Going")

else:

    print("Can not go")

    print("Reason:%s"%(path))

 

Output 1:

Can not go

Reason:Pedestrian walking

 

Example 2:

filter1 = True

filter2 = True

 

if filter1 is True and filter2 is True:

    print("Printing Search Results")

else:

    print("Search could not find any results")

 

Output 2:

Printing Search Results

 


Copyright 2018 © pythontic.com