CSC 309

Ace your homework & exams now with Quizwiz!

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

J u l i e t

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, 8] print(numbers[-4:])

[5, 6, 7, 8]

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

[6, 7]

Boolean Function

Returns true or false.

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

1 2 3

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

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

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

append

adds an element to the end of a list

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 are negative, then set theList[i] to 1, otherwise set theList[i] to 0. You may assume the first and last elements are always 0. The function header is provided. Complete the code for the function. You do not need to document or provide a main() function. (20 pts)

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

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. You do not need to document and you do not need wrote a main() function. (15 pts)

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 number is 0. You do not need to document and you do not need to write a main() function. (10 pts)

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

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)

Value return statement must include:

def foo(x) /t return x:

Void Function

def foo(x): print(x)

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)

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

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)

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

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")

Last element in a list

len(list) - 1

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)

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

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)

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

somelist.insert(index, value to replace)


Related study sets

Nutrition MindTap Study Guide (Chp 8)

View Set

Chapter 4- Neuronal function in the Nervous System

View Set

Wk 5 - Practice: Fiscal Policy [due Day 5]

View Set

Discrimination II - Equal Pay and the Sex Equality Clause

View Set

Mastering Biology Chp 4 and 5 quiz

View Set

Basics of Material Science and Engineering #1

View Set

Business Law Chapters 10-12,19, & 43

View Set