CSC 309

Boolean Function

Returns true or false.

Value return statement must include:

def foo(x) /t return x:

What happens when the end of the function block is reached after a function is executed

When the end of the function is reached, the computer returns back to the
part of the program that called the function, and the program resumes
execution at that point

Names_list contains a list of (string)names. Write a for loop that walks over the list and prints the names

for I in Names_list print(I)

Dictionary

Uses a key and a value. The key is a name and value is the ID of the key. {Brandon: 913032177} Brandon is the key and 913... is the value for Brandon

Assume namesDict contains a dictionary of names as keys and ids. Write a for-loop that walks over the list and prints out all the keys and values

for K in namesDict: /t print(k, namesDict[K])

What will the following code display?
values = [2, 4, 6, 8, 10]
print(values[1:3])

[4, 6]

What does the following code display?
numbers = [1, 2, 3, 4, 5, 6, 7]
print(numbers[5:])

[6, 7]

What does the following code display?
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
print(numbers[-4:])

[5, 6, 7, 8]

Write a statement that creates a list with the following strings: 'Einstein', 'Newton',
'Copernicus', and 'Kepler'.

...

Assume names references a list. Write a for loop that displays each element of the
list.

names = ['Brandon', 'Kera', 'Kevin']
for x in names:
print(x)

Assume the lists numbers1 has 100 elements and numbers2 is an empty list. Write
code that copies the values in numbers1 to numbers2.

list1 = [2]*100
list2 = list1[:]
print(list2)

Write a function that accepts a list as an argument (assume the list contains integers)
and returns the total of the values in the list.

def list_x(x):
return x
list1 = [1, 2, 66, "Happy"]
list_x(list1)

Assume namesList contains a list of names (as strings). Write a for-loop that walks over the list
and prints out all the names. (5 pts)

namesList = ["damn", "son"]
for x in namesList:
print(x)

Assume namesDict contains a dictionary of names, as keys, and IDs, as values. Write a for-loop
that walks over the list and prints out all the key and values (names and IDs).

namesDict = {"Brandon": 913032199}
for x in namesDict:
print(x, namesDict[x])

What is printed on the screen when the following code is executed? (5 pts)
aList = ['a', 'b', 'c', 'd', 'e', 'f', 'f', 'g']
print(aList[::2])
aList.remove('e')
print(aList[::2])

['a', 'c', 'e', 'f']
['a', 'c', 'f', 'g']

Write a function named times_ten() that accepts a number as an argument. When the function is
called, it should return the the value of its argument multipled by 10. (5 pts)

def times_ten(number):
return number * 10

A program contains the following function definition, for cube(). Write a statement that passes the
value 4 to this function and then assigns the return value to the variable result.

def cube(num):
return num
num
num
result = cube(4)

Write a function, computeStatistics(), which takes a list of ints as input, and returns a tuple of
the min, max, and mean values of the list. Do not use built-in functions. You may assume the
maximum possible number is 100 and minimum possible number is 0

def computeStatistics(a):
length = len(a)
min, max, sum = 0, 100, 0
for i in a:
if i < max:
min = i
if i > min:
max = i
sum = sum + i
return min, max, sum/length

Write a function, computeStatistics2(), which takes a list of ints as input, and returns a tuple
of the min, max, and mean values of the list. Use the Python built-ins this time. You may assume the
maximum possible number is 100 and minimum possible numbe

def computeStatistics(a):
return min(a), max(a), sum(a)/len(a)

Write a function called applyRule() that takes a list of floats, theList[], as input, computes a
new list, and returns the new list as output. The function computes the new list based on this rule: if
theList[i] is non-negative and both of its neighbors a

def applyrule(s):
a = [0] * len(s)
for x in range(1, len(s) - 1):
if s[x - 1] < 0 and s[x] > 0:
a[x] = 1
else: a[x] = 0
return a

Last element in a list

len(list) - 1

append

adds an element to the end of a list

What will the following code display?
stuff = {1 : 'aaa', 2 : 'bbb', 3 : 'ccc'}
for k in stuff:
print(k)

1
2
3

name = 'Juliet'
for ch in name:
print(ch)

J
u
l
i
e
t

Void Function

def foo(x):
print(x)

Assume the names variable references a list of strings. Write code that determines
whether 'Ruby' is in the names list. If it is, display the message 'Hello Ruby'.
Otherwise, display the message 'No Ruby'.

if 'Ruby" not in names:
print('No Ruby')
else: print("Hi Ruby")

How can you use insert to change an item in a list?

somelist.insert(index, value to replace)