Python 2 hour intro/Handout: Difference between revisions
Content added Content deleted
imported>Jesstess (Created page with "==Numbers: integers and floats== * Integers don't have a decimal place. * Floats have a decimal place. * Math mostly works the way it does on a calculator, and you can use pa...") |
imported>Jesstess No edit summary |
||
Line 256: | Line 256: | ||
Alice starts with a vowel. |
Alice starts with a vowel. |
||
Ellen starts with a vowel.</pre> |
Ellen starts with a vowel.</pre> |
||
Sometimes you want to start with a new empty list, and only add to that list if some condition is true: |
|||
<pre> |
|||
>>> vowel_names = [] |
|||
>>> for name in ["Alice", "Bob", "Cassie", "Deb", "Ellen"]: |
|||
... if name[0] in "AEIOU": |
|||
... vowel_names.append(name) |
|||
... |
|||
>>> print vowel_names |
|||
['Alice', 'Ellen']</pre> |
|||
== Useful functions related to lists and for loops == |
== Useful functions related to lists and for loops == |
||
Line 272: | Line 261: | ||
==== sorting lists ==== |
==== sorting lists ==== |
||
Use <code> |
Use <code>sorted()</code> to sort a list: |
||
<pre> |
<pre> |
||
>>> names = ["Eliza", "Joe", "Henry", "Harriet", "Wanda", "Pat"] |
>>> names = ["Eliza", "Joe", "Henry", "Harriet", "Wanda", "Pat"] |
||
>>> |
>>> sorted(names) |
||
>>> names |
|||
['Eliza', 'Harriet', 'Henry', 'Joe', 'Pat', 'Wanda'] |
['Eliza', 'Harriet', 'Henry', 'Joe', 'Pat', 'Wanda'] |
||
</pre> |
</pre> |