28.10.2021 Views

Python Tutorial ( PDFDrive )

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

else:

print "Line 3 - a and b are true"

print "Line 3 - Either a is not true or b is not true"

if ( a or b ):

print "Line 4 - Either a is true or b is true or both are true"

else:

print "Line 4 - Neither a is true nor b is true"

if not( a and b ):

else:

print "Line 5 - Either a is not true or b is not true"

print "Line 5 - a and b are true"

When you execute the above program, it produces the following result:

Line 1 - a and b are true

Line 2 - Either a is true or b is true or both are true

Line 3 - Either a is not true or b is not true

Line 4 - Either a is true or b is true or both are true

Line 5 - Either a is not true or b is not true

Python Membership Operators:

In addition to the operators discussed previously, Python has membership operators, which test for membership

in a sequence, such as strings, lists or tuples. There are two membership operators explained below:

Operator Description

Example

In

not in

Evaluates to true if it finds a variable in the specified sequence and false

otherwise.

Evaluates to true if it does not finds a variable in the specified sequence and

false otherwise.

x in y, here in results

in a 1 if x is a member

of sequence y.

x not in y, here not

in results in a 1 if x is

not a member of

sequence y.

Example:

Try following example to understand all the membership operators available in Python programming language:

#!/usr/bin/python

a = 10

b = 20

list = [1, 2, 3, 4, 5 ];

TUTORIALS POINT

Simply Easy Learning

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!