NOTE: In the following problems, if an error prevents an example from running, make
certain to mention that as part of the results. Also, be precise. If program prints [1],
doesn't say it prints 1.
- List the four types of data we've covered, and give an example of each:
- What does this code print out? For this and the following problems, make
sure you understand WHY it prints what it does. You don't have to explain it,
but if you don't understand why, make sure to ask.
Otherwise you are wasting your time doing these.
my_list = [5, 2, 6, 8, 101]
print(my_list[1])
print(my_list[4])
print(my_list[5])
- What does this code print out?
my_list=[5, 2, 6, 8, 101]
for my_item in my_list:
print(my_item)
- What does this code print out?
my_list1 = [5, 2, 6, 8, 101]
my_list2 = (5, 2, 6, 8, 101)
my_list1[3] = 10
print(my_list1)
my_list2[2] = 10
print(my_list2)
- What does this code print out?
my_list = [3 * 5]
print(my_list)
my_list = [3] * 5
print(my_list)
- What does this code print out?
my_list = [5]
for i in range(5):
my_list.append(i)
print(my_list)
- What does this code print out?
print(len("Hi"))
print(len("Hi there."))
print(len("Hi") + len("there."))
print(len("2"))
print(len(2))
- What does this code print out?
print("Simpson" + "College")
print("Simpson" + "College"[1])
print( ("Simpson" + "College")[1] )
- What does this code print out?
word = "Simpson"
for letter in word:
print(letter)
- What does this code print out?
word = "Simpson"
for i in range(3):
word += "College"
print(word)
- What does this code print out?
word = "Hi" * 3
print(word)
- What does this code print out?
my_text = "The quick brown fox jumped over the lazy dogs."
print("The 3rd spot is: " + my_text[3])
print("The -1 spot is: " + my_text[-1])
- What does this code print out?
s = "0123456789"
print(s[1])
print(s[:3])
print(s[3:])
- Write a loop that will take in a list of five numbers from the user, adding
each to an array. Then print the array. Try doing this without looking at the
book.
- Write a program that take an array like the following, and print the average.
Use the len function, don't just use 15, because that won't work if
the list size changes.
(There is a sum function I haven't told you about. Don't use that.
Sum the numbers individually as shown in the chapter.)
(Also, a common mistake is to calculate the average each time through the
loop to add the numbers. Finish adding the numbers before you divide.)
my_list = [3,12,3,5,3,4,6,8,5,3,5,6,3,2,4]