FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!

Python Sequences: Testing membership




A simple way to test whether a given item is a member of a sequence the in command can be used:
Code:

>>> list = [1,2,3,4]
>>> 1 in list
True
>>> 5 in list
False

Of course this also works for strings, as I said: strings are a special form or sequences.
Code:

>>> s = "1234"
>>> "1" in s
True
>>> "5" in s
False

in is also used in a for loop to loop thru a squence, like:
Code:

>>> for i in s:
   print i

   
1
2
3
4

or:
Code:

>>> for i in list:
   print i

   
1
2
3
4




0 blog comments below




FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.