Tour The Python Data Types!

Try these in your own Python interactive shell:

$ python

Numbers

Addition

>>> 2 + 2
4

Subtraction

>>> 0 - 2
-2

Multiplication

>>> 2 * 3
6

Division

>>> 4 / 2
2
>>> 1 / 2
0

Integer divison produces an integer. You need a number that knows about the decimal point to get a decimal out of division:

>>> 1.0 / 2
0.5
>>> float(1) / 2
0.5

Remainder (Modulo)

>>> 4 % 2
0
>>> 4 % 3
1

Types

>>> type(1)
<type 'int'>
>>> type(1.0)
<type 'float'>

Strings

>>> "Hello"
'Hello'

Printing strings

>>> print "Hello"
Hello

String concatenation

>>> print "Hello" + "World"
HelloWorld

>>> print "Hello", "World"
Hello World

Printing different data types together

>>> print "Hello", "World", 1
Hello World 1

String formatting

>>> print "Hello %d" % (1,)
Hello 1
>>> print "Hello %s" % ("World",)
Hello World

>>> type("Hello")
<type 'str'>

Lists

List initialization

>>> my_list = list()
>>> my_list
[]
>>> your_list = []
>>> your_list
[]
>>> her_list = ["a", "b", "c", "d", "e", "f", "g", "h"]
>>> her_list
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

Access and adding elements to a list

>>> 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']

Changing elements in a list

>>> your_list.append("apples")
>>> your_list[0]
'apples'
>>> your_list[0] = "bananas"
>>> your_list
['bananas']

Slicing lists

>>> 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']

Sharing versus copying: Sharing

>>> my_list
['Alice']
>>> your_list = my_list
>>> your_list
['Alice']
>>> my_list[0] = "Bob"
>>> my_list
['Bob']
>>> your_list
['Bob']

Copying

>>> my_list
['Alice']
>>> your_list = my_list[:]
>>> my_list[0] = "Bob"
>>> my_list
['Bob']
>>> your_list
['Alice']

Strings can be manipulated like lists

>>> my_string = "Hello World"
>>> my_string[0]
'H'
>>> my_string[:5]
'Hello'
>>> my_string[6:]
'World'
>>> my_string[6:] = "Jessica"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> my_string = my_string[:6] + "Jessica"
>>> my_string
'Hello Jessica'

>>> type(my_string)
<type 'str'>

Dictionaries

Dictionary initialization

>>> my_dict = dict()
>>> your_dict = {}
>>> her_dict = {"Alice" : "chocolate", "Bob" : "strawberry", "Cara" : "mint chip"}
>>> her_dict
{'Bob': 'strawberry', 'Cara': 'mint chip', 'Alice': 'chocolate'}

Adding elements

>>> her_dict["Dora"] = "vanilla"
>>> her_dict
{'Bob': 'strawberry', 'Cara': 'mint chip', 'Dora': 'vanilla', 'Alice': 'chocolate'}

Accessing elements

>>> her_dict["Alice"]
'chocolate'
>>> her_dict.get("Alice")
'chocolate'

>>> her_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
>>> her_dict.get("Eve")
>>> her_dict.get("Alice")
'chocolate'

Changing elements

>>> her_dict["Alice"] = "coconut"
>>> her_dict
{'Bob': 'strawberry', 'Cara': 'mint chip', 'Dora': 'vanilla', 'Alice': 'coconut'}

>>> type(my_dict)
<type 'dict'>

>>> her_dict.get("Alice")
'coconut'

Tuples

Initialization

>>> tuple()
()
>>> ("apple", "oranges", "bananas")
('apple', 'oranges', 'bananas')
>>> my_tuple = ("apple", "oranges", "bananas")

Accessing elements of a tuple: just like lists

>>> my_tuple = ("apple", "oranges", "bananas")
>>> my_tuple[0]
'apple'
>>> len(my_tuple)
3
>>> my_tuple[-1]
'bananas'
>>> my_tuple[:2]
('apple', 'oranges')

Adding or changing elements of a tuple: can’t do it! Not a mutable data type.

>>> my_tuple[:2]
('apple', 'oranges')
>>> my_tuple[0] = "figs"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

>>> type(my_tuple)
<type 'tuple'>