Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Python - when to use the in operator

Python's in construct, when used with an if statement, lets you loop through all the members of a collection (such as a list or a tuple) and see if there's a member in the list that's equal to the pattern - thus

val = 17
if val in [1,4,5,7,12,14,17,20,34]: print "yes"


will print out "Yes". For looking for an element in a list, this is both quick coding and very efficient at run time, as the check is being done at a 'C level' - in other words, internally to Python without a constant series of reference back to the byte code. However - it IS still a loop.

Consider the following code which checks whether an integer entered by the user in between 0 and 30:

value = int(raw_input("Give me a number between 0 (inclusive) and 30 (exclusive): "))
if value in range(0,30): print "It is in range (1)"
if value < 30 and value >= 0: print "It is in range (2)"
if value in xrange(0,30): print "It is in range (3)"


All three work, and work well. BUT ... if the range we were checking was between 0 and 3 million, it would be a different story. The first test would create a list of 3 million integers internally and would then check our value against each of them - a real memory hog, and likely to break if the number was an order of magnitude higher that 3 million. The third test uses a generator function; not a memory hog, but never the less an internal loop looking at each number from 0 to 2,999,999 in turn and so really slow. The second test remains fast - just two checks to be made, no internal loop, no matter how high the top limit integer is!
(written 2006-08-16)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y103 - Python - Conditionals and Loops
  [4723] Conditional operators in Python - (2016-11-05)
  [4713] Equality (in Python) - (2016-10-30)
  [4545] Method, Class, Module, Package - how to they relate in Python? - (2015-10-17)
  [4541] Setting up and tearing down with the Python with keyword - (2015-10-16)
  [4402] Finding sum, minimum, maximum and average in Python (and Ruby) - (2015-01-19)
  [4323] Learning to program - Loop statements such as while - (2014-11-22)
  [4322] Learning to Program - the conditional statement (if) - (2014-11-21)
  [4210] If elif elif elif - multiway selection in Python - (2013-11-16)
  [4092] Identity in Python - (2013-05-17)
  [3895] Flowchart to program - learning to program with Well House - (2012-10-14)
  [3762] Learning to program - the if statement. Python. - (2012-06-12)
  [3620] Finding the total, average, minimum and maximum in a program - (2012-02-22)
  [3558] Python or Lua - which should I use / learn? - (2011-12-21)
  [3439] Python for loops - applying a temporary second name to the same object - (2011-09-14)
  [3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
  [3200] How a for loop works Java, Perl and other languages - (2011-03-12)
  [3083] Python - fresh examples from recent courses - (2010-12-11)
  [2899] Groupsave tickets - 3 or 4 train tickets for the price of 2 - (2010-08-02)
  [2778] Learning to program in Python 2 ... and / or in Python 3 - (2010-05-24)
  [1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
  [1661] Equality, sameness and identity - Python - (2008-05-31)
  [1477] Decisions - small ones, or big ones? - (2007-12-18)
  [1201] No switch in Python - (2007-05-23)
  [909] Python is like a narrowboat - (2006-10-30)
  [788] New - Conditional expressions in Python 2.5 - (2006-07-01)
  [668] Python - block insets help with documentation - (2006-04-04)
  [657] The ternary operator in Python - (2006-03-25)
  [353] Wimbledon Neck - (2005-06-20)
  [299] What - no switch or case statement? - (2005-05-03)


Back to
Python makes University Challenge
Previous and next
or
Horse's mouth home
Forward to
Build on what you already have with OO
Some other Articles
Reporting on the 10 largest files or 10 top scores
Talking about other training companies.
Tomcat - Shutdown port
Build on what you already have with OO
Python - when to use the in operator
Python makes University Challenge
Old Wardour Castle
Displaying data at 5 items per line on a web page
Comparison of Object Oriented Philosophy - Python, Java, C++, Perl
Butterflies in a Wiltshire garden
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/835_Pyth ... rator.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat