Python 2 hour intro/Handout: Difference between revisions

no edit summary
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:
Alice starts with a vowel.
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 ==
Line 272 ⟶ 261:
==== sorting lists ====
 
Use <code>.sortsorted()</code> to sort a list:
 
<pre>
>>> names = ["Eliza", "Joe", "Henry", "Harriet", "Wanda", "Pat"]
>>> names.sortsorted(names)
>>> names
['Eliza', 'Harriet', 'Henry', 'Joe', 'Pat', 'Wanda']
</pre>
Anonymous user