Dict-1 | Dict-2 | Loop | List | String |
---|---|---|---|---|
100It stores the key-value pair 'id':54321 in the employee dictionary
Suppose a dictionary named employee has been created. What does the following statement do?
employee['id'] = 54321 |
100bbb
What will the following code display?
stuff = {1:'aaa', 2:'bbb', 3:'ccc'} print(stuff[2]) |
100A while loop is used when you do not necessarily know how many iterations the loop will go.
When do you want to use a while loop?
|
100[3, 4, 5] [1, ['a', 'b', 'c'], 3, 4, 5] [5, 6, 7, 8] [1, ['a', 'b', 'c'], 3, 4, 5] [['a', 'b', 'c'], 3, 4] c The following code prints six lines. What do they contain?
lst = [1,['a','b','c'],3,4,5,6,7,8] print(lst[2:5]) print(lst[:5]) print(lst[4:]) print(lst[ :-3]) print(lst[-7:-4]) print(lst[1][2]) |
100Python fun What does the following code produce?
string = "Python is fun" print(string[:6]) print(string[-3:]) |
200dct = {'a':1, 'b':'2', 'c':False, True:'xyz'}
Write code that creates a dictionary containing the following key-value pairs:
'a':1 'b':'2' 'c':False True:'xyz' |
200loop1 A [0, 1] loop1 B [2, 3] loop1 C [4, 5] loop2 A [0, 1] loop2 B [2, 3] loop2 C [4, 5] What will the following code display?
dct = {'A':[0,1], 'B':[2,3], 'C':[4,5]} for k in dct: print('loop1', k, dct[k]) for k,v in dct.items(): print('loop2', k, v) |
200They produce the same results
Compare the results of the following loops (same or different)
for i in range(5): i = 0 print(i) while i < 5: print(i) i += 1 |
200[720, 24, 2] The following code prints one line. What does it contain?
lst = [] somenumber = 1 for number in range(6, 0, -2): for i in range(number, 1, -1): somenumber *= i lst.append(somenumber) somenumber = 1 print(lst) |
200Humpty Dumpty sat on the wall
What is the result of this code
string = 'Humpty;Dumpty;sat;on;the;wall' list_of_words = string.split(';') string = ''.join(list_of_words) print(string) |
3005 6 7 8 What will the following code display?
dct = {'A':[0,1], 'B':[2,3,4], 'C':[5,6,7,8]} for item in dct['C']: print(item) |
300if 'abc' in dct.keys(): print(dct['abc']) else: print('The key is not found') Assume the variable dict references a dictionary
(key-value pairs in dict can be anything)
Write code that determines whether the key 'abc' exists in the dictionary. If so, display the value that is associated with that key. If the key is not in the dictionary, display a message 'The key is not found' |
3004 3 2 8 6 4 What does the following code product?
for i in range(1, 3): for j in range(4, 1, -1): print(i*j) |
300def add_to_list(lst): for i in range(0, len(lst)): lst[i] += 1 return lst Write a function that adds 1 to every element in a list.
For example, the list [4, 2, 7, 3, 5, 6, 6, 1, 0, -2]
would yield [5, 3, 8, 4, 6, 7, 7, 2, 1, -1].
You may not use any built-in functions/methods besides len() and .append().
|
300def number_occurrence(string, sub_string): count = 0 for letter in string: if letter == sub_string: count += 1 return count
Write a function that takes a string and a substring
and return the number of time the substring appears in the string.
You may assume that substring is one character long.
You may not use any built-in function except len().
|
400
Write code to
1. Create a dictionary called fruit_dict that contains 2 items: |
400def check_repeated_words(sentence): result = {} words = sentence.split() for i in range(0, len(words)-1): if words[i] == words[i+1]: if words[i] not in result: result[words[i]] = 1 else: result[words[i]] += 1 return result
Write a function that takes a sentence and
checks if there are repeated words.
For each repeated word, keep track the number of time it repeats.
The function then returns a dictionary with the repeated word as key and
the number of the repetition as value.
If there is no repeated word, return an empty dictionary {}
For example, "I will do more more practice and my bring my my my questions to class" the function will return {'more':1, 'my':2} # 'more' repeats 1 time, 'my' repeats 2 times |
400number = int(input('Enter your number: ')) odd_list = [] for i in range(0, number): if i % 2 != 0: odd_list.append(i) print(odd_list)
Write a program which asks for a number from input.
Given this number, print all odd numbers from 0 to this number.
You can assume all numbers given through input will be positive integers
|
400def sum_list(list1, list2): result_list = list1 for i in range(len(list2)): if i < len(list1): result_list[i] += list2[i] else: result_list.append(list2[i]) return result_list
Write a function that takes two lists, sums them together,
and returns the resulting list.
For example, if the incoming lists are [1, 2, 3] and [4, 5, 6, 7],
the function would return [5, 7, 9, 7].
You may not use any built-in functions/methods besides len() and .append().
|
400def remove_vowel(string): new_string = '' for letter in string: if letter not in 'aeiou': new_string += letter if len(new_string) > 0: return new_string else: return string
Write a function that takes a string
and return a new string with all the vowels (a, e, i, o, u)
removed from the original string,
without using any built-in functions/methods besides len().
Do not use .index()
|
Press F11 for full-screen mode