Control Flow: The if
, elif
, else
Statements
Multi-dimensional Lists
Lists can be organized in a multi-dimensional structure. A two dimensional list is a list of lists as in the time table in the following example:
Time table:
time_table = [
["Math101", "Cmpe150", "Cmpe150", "Turkish202", "Turkish202"],
["Cmpe150", "Cmpe150", "Math101", "Hist302", "Hist302"],
["Turkish202", "Turkish202", "Bio201", "Turkish202", "En301"],
["Math101", "Math101", "Bio201", "Bio201", "Hist302"],
["Cmpe150", "Cmpe150", "Phil102", "Phil102", "En301"]]
print(time_table)
print(time_table[1])
print(time_table[1][2])
Exercise: Generate a three dimensional list and try with altering number of elements in each dimension.
Hint:
list = [
[["item at ([0][0][0])", "item at ([0][0][1])"], ["item at ([0][1][0])", "item at ([0][1][0])"]],
[["item at ([1][0][0])", "item at ([1][0][1])"], ["item at ([1][1][0])", "item at ([1][1][0])"]],
[["item at ([2][0][0])", "item at ([2][0][1])"], ["item at ([2][1][0])", "item at ([2][1][0])"]]
]
print (list[0][0][0])
print (list[2][0][1])
Example:
#List of "numbers"
oneDimensionalList = [1,4,7,8]
print (oneDimensionalList)
print (oneDimensionalList[2])
#List of "number lists"
twoDimensionalList = [[5,7], [4,8,6], [3,7,5,3], [3,7]]
print (twoDimensionalList)
print (twoDimensionalList[2])
print (twoDimensionalList[2][1])
#List of "list of number lists"
threeDimensionalList = [ [ [1,5], [3,4,8], [2,4,5,6] ] ,[ [3,4,5], [2], [3,7] ] ]
print (threeDimensionalList)
print (threeDimensionalList[1])
print (threeDimensionalList[1][2])
print (threeDimensionalList[1][2][0])
Time Table Example:
timetable = [ ["Eng", "Arts", "Music", "PE", "PE"],
["Math","Math", "Phys","Free","Chem"],
["Eng","Eng","Bio","Bio","Math"],
["Drama","Drama","Free","Drama"],
["History","Geo","Geo","RE","Math"]
]
print(timetable[0])
print(timetable[1])
print(timetable[2])
print(timetable[3])
print(timetable[4])
days=("Mon","Tue","Wed","Thu","Fri")
for i in range(len(timetable)):
for j in range(len(timetable[i])):
print(days[i], j+1, timetable[i][j])