In [32]:
number_list = [1, 2, 4, 8, 16, 32]
the_pythons = ["Graham", "Terry", "Michael", "Eric", "Terry", "John"]
mixed = [1, "Terry", 4]
print (mixed)
In [34]:
my_list = [10, 20, 30, 40]
my_list[2]
Out[34]:
In [35]:
# Using a while loop
the_pythons = ["Graham", "Terry", "Michael", "Eric", "Terry", "John"]
index = 0
while index < len(the_pythons):
print(the_pythons[index])
index += 1
In [37]:
# Using a for loop
the_pythons = [54, "Graham", "Terry", "Michael", "Eric", "Terry", "John"]
for item in the_pythons:
print(item)
In [39]:
my_list = [10, 20, 30, 40]
print(my_list)
my_list[3] = "Graham"
print(my_list)
In [41]:
range(5, 10)
Out[41]:
In [18]:
# Plus sign concatenates two lists
list1 = list(range(5))
list2 = list(range(5,10))
list3 = list1 + list2
list3
Out[18]:
In [43]:
# Asterisk/multiplication repeats
# Example list * n
days = ["Ni!"] * 1024
print(days)
In [44]:
the_pythons = ["Graham", "Terry", "Terry"]
# Same as the_pythons = the_pythons + ["Michael", "Eric", "John"]
the_pythons += ["Michael", "Eric", "John"]
the_pythons
Out[44]:
In [22]:
days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
mid_days = days[2:5]
mid_days
Out[22]:
In [45]:
# This is a global constant (read only)
# This is OK
# Uppercase naming is a convention, not enforced by compiler
THE_PYTHONS = ["Graham", "Terry", "Michael", "Eric", "Terry", "John"]
# global variables are to be used with care
# use local variables instead
# use a top-level function, e.g. main
def was_a_python(name):
LOCAL_CONSTANT = 0.13
if name in THE_PYTHONS:
print(name + " was in Monty Python")
else:
print(name + " was not in Monty Python")
def main():
name = raw_input("Was in the Pythons? ")
was_a_python(name)
main()
Other useful list methods are:
For a full list, see the Python documentation.
In [54]:
# Using indexing
the_pythons = ["Graham", "Terry", "Michael", "Eric", "Terry", "John"]
try:
the_pythons[-7] = "Ringo"
except IndexError:
# raise IndexError
print("Oops")
else:
print(the_pythons)
In [55]:
# Using append
the_pythons = ["Graham", "Terry", "Michael", "Eric", "Terry", "John"]
the_pythons.append("Ringo")
the_pythons
Out[55]:
In [56]:
# Using insert
the_pythons = ["Graham", "Terry", "Michael", "Eric", "Terry", "John"]
# not the same as the_pythons[2] = "Ringo" !
the_pythons.insert(2, "Ringo")
the_pythons
Out[56]:
In [60]:
# Removes first occurence of a value
the_pythons = ["Graham", "Terry", "Michael", "Eric", "Terry", "John"]
the_pythons.remove("Terry")
the_pythons.remove("Terry")
if "Terry" in the_pythons:
the_pythons.remove("Terry")
the_pythons
Out[60]:
In [61]:
# Deletes item at index
my_list = list(range(5))
del my_list[2]
my_list
Out[61]:
In [62]:
# Shallow copying
the_pythons = ["Graham", "Terry", "Michael", "Eric", "Terry", "John"]
more_pythons = the_pythons
the_pythons.insert(2, "Ringo")
print(the_pythons)
print(more_pythons)
In [63]:
# Deep copying 1
the_pythons = ["Graham", "Terry", "Michael", "Eric", "Terry", "John"]
# Using append
more_pythons = []
for comedian in the_pythons:
more_pythons.append(comedian)
more_pythons
Out[63]:
In [65]:
# Deep copying 2
the_pythons = ["Graham", "Terry", "Michael", "Eric", "Terry", "John"]
# Using arithmetic
more_pythons = [] + the_pythons
more_pythons
Out[65]:
In [ ]:
# Initializing variables
# Creates a variable AND tells the interpreter the type
variable1 = ""
variable2 = 0 # a bit hacky
variable3 = 0.0 # a bit hacky
variable4 = []
In [75]:
partners = [["Alyha", "Jeanne-Marie"],
["Billal", "Pranjal"],
["Dusan", "Piaoyao"],
["Isabelle", "Tyler"]]
# long way
pair = partners[0]
pair
pair[0]
# short way
partners[3][1]
Out[75]:
In [74]:
crazy = [0, [1, [2, 3]]]
crazy[1][1][1]
Out[74]:
In [6]:
# Iterating over a two-dimensional list
partners = [["Alyha", "Jeanne-Marie"],
["Billal", "Pranjal"],
["Dusan", "Piaoyao"],
["Isabelle", "Tyler"]]
for team in partners:
for name in team:
print(name)
In [83]:
# Iterating over a two-dimensional list
partners = [["Alyha", "Jeanne-Marie"],
["Billal", "Pranjal"],
["Dusan", "Piaoyao"],
["Isabelle", "Tyler"]]
team_number = 1
for team in partners:
print("Team " + str(team_number))
name_number = 1
for name in team:
print("\tPartner " + str(name_number) +": " + name)
name_number += 1
team_number += 1
In [4]:
# Iterating over a two-dimensional list
partners = [["Alyha", "Jeanne-Marie"],
["Billal", "Pranjal"],
["Dusan", "Piaoyao"],
["Isabelle", "Tyler"]]
partner_index = 0
while partner_index < len(partners):
print("Team " + str(partner_index + 1))
person_index = 0
while person_index < len(partners[partner_index]):
print("\tPartner: " + str(person_index + 1) + " " + partners[partner_index][person_index])
person_index += 1
partner_index += 1
Team 1 Partner 1: Alyha Partner 2: Jeanne-Marie
In [5]:
help("print")
In [ ]:
__author__ = "Susan Sim"
__email__ = "ses@drsusansim.org"
__copyright__ = "2015 Susan Sim"
__license__ = "MIT License"
Other attributes
__credits__
__maintainer__
__status__
__deprecated__
__version__
In [ ]: