Python 3. Daily exercise
What will the following nested for-loop print? (Note, if you are having trouble with this question, review CodeLens 3). for i in range(3): for j in range(2): print(i, j)
0 0 0 1 1 0 1 1 2 0 2 1
How many times is the word HELLO printed by the following statements? s = "python rocks" for ch in s: print("HELLO")
12
What is printed by the following statements? s = "python rocks" print(len(s))
12
What is the value of the following expression: 16 - 2 * 5 // 3 + 1
14
What is printed when the following statements execute? x = 12 x = x - 3 x = x + 5 x = x + 1 print(x)
15
What will be stored in the variable ty below? qu = "wow, welcome week! Were you wanting to go?" ty = qu.count("we")
2 😉
How many times is the letter p printed by the following statements? s = "python" for idx in range(len(s)): print(s[idx % 2])
3
What is printed by the following statements? L = [0.34, '6', 'SI106', 'Python', -2] print(len(L[1:-1]))
3
What is printed by the following statements? alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False] print(alist[5])
3.14
What value is printed when the following statement executes? print(18/4)
4.5
How many times is the word HELLO printed by the following statements? s = "python rocks" for ch in s[3:8]: print("HELLO")
5
What is printed by the following statements? alist = [3, 67, "cat", 3.14, False] print(len(alist))
5
What will be stored in the variable ty below? qu = "wow, welcome week!" ty = qu.index("we")
5
What value is printed when the following statement executes? print(int(53.785))
53
How many times will the for loop iterate in the following statements? p = [3, 4, "Me", 3, [], "Why", 0, "Tell", 9.3] for ch in p: print(ch)
9
What is printed when the following statements execute? n = input("Please enter your age: ") # user types in 18 print(type(n))
<class 'str'>
If you have a pixel whose RGB value is (50, 0, 0), what color will this pixel appear to be?
Dark Red Because all three values are close to 0, the color will be dark. But because the red value is higher than the other two, the color will appear red.
Consider the following code: for aColor in ["yellow", "red", "green", "blue"]: alex.forward(50) alex.left(90) What does each iteration through the loop do?
Draw one side of a square.
What is the type of your iterable? y = 18 for z in y: print(z)
Error, unable to iterate over the object/ integer.
"A_good_grade_is_A+" is a legal variable name in python 3.
False
Consider the following code: nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] for w in nums: accum = 0 accum = accum + w print(accum)
It will print out 10 instead of 55
TypeError , ValueError are an example of...
Run Time Error
What is the data type of 'this is what kind of data'?
String
What are correct ways to tell a turtle named Tex to move forward 20 pixels? Select as many as apply.
Tex.forward(20) Tex.forward(10+10)
Who or what typically finds semantic errors?
The Programmer You must fully understand the problem so the you can tell if your program properly solves it.
Who or what typically finds syntax errors?
The compiler / interpreter.
Who or what typically finds runtime errors?
The interpreter If an instruction is illegal to perform at that point in the execution, the interpreter will stop with a message describing the exception.
How does python know what statements are contained in the loop body?
They are indented to the same degree from the loop header.
What is printed by the following statements? alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False] print(alist[4:])
[ [ ], 3.14, False]
What is printed by the following statements? alist = [1,3,5] blist = [2,4,6] print(alist + blist)
[1,3,5,2,4,6]
The code below initializes two variables, z and y. We want to assign the total number of characters in z and in y to the variable a. Which of the following solutions, if any, would be considered hard coding? z = "hello world" y = "welcome!"
a = len("hello worldwelcome!") a = 11 + 8 a = len("hello world") + len("welcome!")
What is printed by the following statements? s = "python rocks" print(s[3])
h
What is printed by the following statements? s = "python rocks" print(s[3:8])
hon r
Why does counting start at 0 going from left to right, but at -1 going from right to left?
indexing starting at 0 has a long history in computer science having to do with some low-level implementation details that we won't go into. For indexing from right to left, it might seem natural to do the analgous thing and start at -0. Unfortunately, -0 is the same as 0, so s[-0] can't be the last item. Remember we said that programming languages are formal languages where details matter and everything is taken literally
What's the type of your iterator variable in the first iteration? t = [9, "setter", 3, "wing spiker", 10, "middle blocker"] for z in t: print(z)
integer
What is the type of your iterable? n = ["word", "phrase", 8, ("beam")] for item in n: print(item)
list
Assign the value of the 34th element of lst to the variable output. lst = ["hi", "morning", "dog", "506", "caterpillar", "balloons", 106, "yo-yo", "python", "moon", "water", "sleepy", "daffy", 45, "donald", "whiteboard", "glasses", "markers", "couches", "butterfly", "100", "magazine", "door", "picture", "window", ["Olympics", "handle"], "chair", "pages", "readings", "burger", "juggle", "craft", ["store", "poster", "board"], "laptop", "computer", "plates", "hotdog", "salad", "backpack", "zipper", "ring", "watch", "finger", "bags", "boxes", "pods", "peas", "apples", "horse", "guinea pig", "bowl", "EECS"]
output = lst[33] print(output)
The correct code to generate a random number between 1 and 100 (inclusive) is:
prob = random.randrange(1, 101)
What will the output be for the following code? let = "z" let_two = "p" c = let_two + let m = c*5 print(m)
pzpzpzpzpz
What is the type of your iterable? t = "couch" for z in t: print(z)
string
What's the type of your iterator variable in the second iteration? t = [9, "setter", 3, "wing spiker", 10, "middle blocker"] for z in t: print(z)
string
What's the type of your iterator variable? t = ["couch", "chair", "washer", "dryer", "table"] for z in t: print(z)
string
Which of the following correctly uses indexing? Assume that a is a list or string. Select as many as apply.
t = a[0]
What will be stored in the variable ht below? rooms = ['bathroom', 'kitchen', 'living room', 'bedroom', 'closet', "foyer"] ht = rooms.index("garden")
there is no 'garden' in the list; so we will get back an error message.
What is printed by the following statements? s = "python rocks" print(s[2] + s[-4])
to
What is the type of your iterable? t = ("couch", "chair", "washer", "dryer", "table") for z in t: print(z)
tuple
Which is the correct way to make a new instance of the Turtle class?
turtle.Turtle()
Which of the following explains why wait_time_int = int(wait_time_int) is an error?
wait_time_int does not have a value so it cannot be used on the right hand side. Variables must already have values in order to be used on the right hand side.
After the following statements, what are the values of x and y? x = 15 y = x x = 22
x is 22, y is 15