Boston Python Workshop 3/Data types: Difference between revisions

imported>Jesstess
imported>Jesstess
 
(6 intermediate revisions by the same user not shown)
Line 40:
==Strings==
 
* Strings are bits of text, and contain characters like numbers, letters, whitespace, and punctuation.
* String are surrounded by quotes.
* Use triple-quotes (""") to create whitespace-preserving multi-line strings.
Line 46 ⟶ 47:
>>> "Hello"
'Hello'
</pre>
 
====String concatenation====
 
<pre>
>>> "Hello" + "World"
HelloWorld
>>> "Hello" + "World" + 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> "Hello" + "World" + str(1)
'HelloWorld1'
</pre>
 
====Printing strings====
 
<pre>
>>> print "Hello" + "World"
HelloWorld
>>> print "Hello", "World"
Hello World
>>> print "Hello", "World", 1
Hello World 1
</pre>
 
Line 56 ⟶ 81:
was US $1.4 million.
</pre>
 
====Types====
 
<pre>
Line 61 ⟶ 88:
<type 'str'>
</pre>
 
<b>String concatenation with '+'</b>: "Hello" + "World"<br />
<b>Printing strings with '+'</b>: print "Hello" + "World"<br />
<b>Printing strings with ','</b>: print "Hello", "World", 1<br />
 
==Booleans==
Line 70 ⟶ 93:
* There are two booleans, <code>True</code> and <code>False</code>.
* Use booleans to make decisions.
 
<pre>
>>> type(True)
<type 'bool'>
>>> type(False)
<type 'bool'>
</pre>
 
====Containment with 'in' and 'not in'====
Line 118 ⟶ 134:
else:
print "Too extreme for me."
</pre>
 
====Types====
 
<pre>
>>> type(True)
<type 'bool'>
>>> type(False)
<type 'bool'>
</pre>
 
Line 123 ⟶ 148:
 
* Use lists to store data where order matters.
* Lists are indexed starting with 0.
 
====List initialization====
Line 238 ⟶ 263:
>>> 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>
 
Line 245 ⟶ 283:
>>> type(my_list)
<type 'list'>
</pre>
 
==Dictionaries==
 
* Use dictionaries to store key/value pairs.
* Dictionaries do not guarantee ordering.
* A given key can only have one value, but multiple keys can have the same value.
 
====Initialization====
 
<pre>
>>> my_dict = {}
>>> my_dict
{}
>>> your_dict = {"Alice" : "chocolate", "Bob" : "strawberry", "Cara" : "mint chip"}
>>> your_dict
{'Bob': 'strawberry', 'Cara': 'mint chip', 'Alice': 'chocolate'}
</pre>
 
====Adding elements to a dictionary====
 
<pre>
>>> your_dict["Dora"] = "vanilla"
>>> your_dict
{'Bob': 'strawberry', 'Cara': 'mint chip', 'Dora': 'vanilla', 'Alice': 'chocolate'}
</pre>
 
====Accessing elements of a dictionary====
 
<pre>
>>> your_dict["Alice"]
'chocolate'
>>> your_dict.get("Alice")
'chocolate'
</pre>
 
<pre>
>>> your_dict["Eve"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'Eve'
>>> "Eve" in her_dict
False
>>> "Alice" in her_dict
True
>>> your_dict.get("Eve")
>>> person = your_dict.get("Eve")
>>> print person
None
>>> print type(person)
<type 'NoneType'>
>>> your_dict.get("Alice")
'coconut'
</pre>
 
====Changing elements of a dictionary====
 
<pre>
>>> your_dict["Alice"] = "coconut"
>>> your_dict
{'Bob': 'strawberry', 'Cara': 'mint chip', 'Dora': 'vanilla', 'Alice': 'coconut'}
</pre>
 
====Types====
 
<pre>
>>> type(my_dict)
<type 'dict'>
</pre>
Anonymous user