Python 2 hour intro: Difference between revisions

no edit summary
imported>Jesstess
No edit summary
imported>Jesstess
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 1:
This material is intended for a 1.5 - 2 hour interactive lecture.
 
[[Python 2 hour intro/Handout| Lecture handout]]
 
==Math==
Line 556 ⟶ 558:
<b>Remember that '=' is for assignment and '==' is for comparison.</b>
 
==End of Part 2Lists==
 
* Use lists to store data where order matters.
Congratulations! You've learned about and practiced executing Python scripts, booleans, conditionals, and if/else blocks, and you've written your own Python functions. This is a huge, huge accomplishment!
* Lists are indexed starting with 0.
 
====List initialization====
[[File:Champagne.png|100px]][[File:Party.png|125px]]
 
<pre>
>>> my_list = []
>>> my_list
[]
>>> your_list = ["a", "b", "c", 1, 2, 3]
>>> your_list
['a', 'b', 'c', 1, 2, 3]
</pre>
 
====Access and adding elements to a list====
Take a break, stretch, meet some neighbors, and ask the staff if you have any questions about this material.
 
<pre>
>>> len(my_list)
0
>>> my_list[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> my_list.append("Alice")
>>> my_list
['Alice']
>>> len(my_list)
1
>>> my_list[0]
'Alice'
>>> my_list.insert(0, "Amy")
>>> my_list
['Amy', 'Alice']
</pre>
 
<pre>
[[Boston Python Workshop 6/Friday|&laquo; Back to the Friday Workshop page]]
>>> my_list = ['Amy', 'Alice']
>>> 'Amy' in my_list
True
>>> 'Bob' in my_list
False
</pre>
 
====Changing elements in a list====
 
<pre>
>>> your_list = []
>>> your_list.append("apples")
>>> your_list[0]
'apples'
>>> your_list[0] = "bananas"
>>> your_list
['bananas']
</pre>
 
====Slicing lists====
 
<pre>
>>> her_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
>>> her_list[0]
'a'
>>> her_list[0:3]
['a', 'b', 'c']
>>> her_list[:3]
['a', 'b', 'c']
>>> her_list[-1]
'h'
>>> her_list[5:]
['f', 'g', 'h']
>>> her_list[:]
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
</pre>
 
====Strings are a lot like lists====
 
<pre>
>>> my_string = "Hello World"
>>> my_string[0]
'H'
>>> my_string[:5]
'Hello'
>>> my_string[6:]
'World'
>>> my_string = my_string[:6] + "Jessica"
>>> my_string
'Hello Jessica'
</pre>
 
* One big way in which strings are different from lists is that lists are mutable (you can change them), and strings are immutable (you can't change them). To "change" a string you have to make a copy:
 
<pre>
>>> h = "Hello"
>>> h[0] = "J"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> h = "J" + h[1:]
>>> h
'Jello'
</pre>
 
==Loops==
 
Use a <code>for</code> loop to do something to every element in a list.
 
<pre>
>>> names = ["Jessica", "Adam", "Liz"]
>>> for name in names:
... print name
...
Jessica
Adam
Liz</pre>
 
<pre>
>>> names = ["Jessica", "Adam", "Liz"]
>>> for name in names:
... print "Hello " + name
...
Hello Jessica
Hello Adam
Hello Liz</pre>
 
==End of Part 2==
 
Congratulations! You've learned about and practiced writing and executing Python scripts, making choices with booleans and conditionals, and using lists and iteration. This is a huge, huge accomplishment!
 
[[File:Champagne.png|100px]][[File:Party.png|125px]]
 
Take a break, stretch, meet some neighbors, and ask the staff if you have any questions about this material.
Anonymous user