Difference between revisions of "Python:Filtering Tutorial"

From Progzoo
Jump to: navigation, search
 
Line 72: Line 72:
 
</p>
 
</p>
 
</question>
 
</question>
 +
{{World Factbook Tutorial}}

Latest revision as of 21:12, 11 July 2017

In these examples we select only some countries to print. A process that takes a list and selects only certain items is a "filter". Some get through, some don't.

https://docs.python.org/2.7/library/string.html includes a list of string methods.


Selecting a region.

Print the countries where the region is Europe.

[Font] [Default] [Show] [Resize] [History] [Profile]

Picking the stans.

Print only those countries where the name ends with stan

[Font] [Default] [Show] [Resize] [History] [Profile]

Countries with a z.

Print only those countries where the name contains the letter z

You can use contains for this.

[Font] [Default] [Show] [Resize] [History] [Profile]

Small countries (by area).

Print the countries that have an area of less than 100.

[Font] [Default] [Show] [Resize] [History] [Profile]