Programming PPT CS
Programming PPT CS
Overview
>>> [1, 2, 3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> “Hello” * 3
‘HelloHelloHello’
Mutability:
Tuples vs. Lists
Lists are mutable
Potentially confusing:
• extend takes a list as an argument.
• append takes a singleton as an argument.
>>> li.append([10, 11, 12])
>>> li
[1, 2, ‘i’, 3, 4, 5, ‘a’, 9, 8, 7, [10,
11, 12]]
Operations on Lists Only
Lists have many methods, including index, count,
remove, reverse, sort
>>> li = [‘a’, ‘b’, ‘c’, ‘b’]
>>> li.index(‘b’) # index of 1st occurrence
1
>>> li.count(‘b’) # number of occurrences
2
>>> li.remove(‘b’) # remove 1st occurrence
>>> li
[‘a’, ‘c’, ‘b’]
Operations on Lists Only
>>> li = [5, 2, 6, 8]
>>> li.sort(some_function)
# sort in place using user-defined comparison
Tuple details
The comma is the tuple creation operator, not parens
>>> 1,
(1,)
_______________________________________________________
break() funtion
To terminate a loop, you can use the break()
function.
The break() statement breaks out of the innermost
enclosing for or while loop.
While loops
The while loop tells the computer to do something as long
as a specific condition is met.
It essentially says:
student_scores = [["Alice", 85], ["Bob", 92], ["Charlie", 78], ["David", 92], ["Eve", 88]]
highest_scorer = find_highest_score(student_scores)
print("The student with the highest score is:", highest_scorer)
Note: Your function should be able to handle lists of different lengths and the provided list of student scores is just an
example. Your code should work for any similar list of student scores.