Every problem in the chapter beside the book’s answer, a chapter at a time as you scroll.
Chapter 3: Objects
31 problems · 0 figuresConcepts in Practice 3.1.1 §3.1 source ↗
- a.1
- b.2
- c.-2
Answer
Concepts in Practice 3.1.2 §3.1 source ↗
s = "Python!", what is the value of s[1] + s[-1]?- a.
"P!" - b.
"y!" - c.
"yn"
Answer
"y!"y, and the character at index -1 is !.Concepts in Practice 3.1.3 §3.1 source ↗
s = "Python!", what type of object is s[0]?- a.character
- b.integer
- c.string
Answer
s[0] is "P" , which is a string of length 1.Concepts in Practice 3.1.4 §3.1 source ↗
- a.1
- b.65
- c.97
Answer
Concepts in Practice 3.1.5 §3.1 source ↗
ord("0") return?- a.
0 - b.
48 - c.
Error
Answer
48Concepts in Practice 3.1.6 §3.1 source ↗
chr(126) return?- a.
~ - b.
"~" - c.
Error
Answer
"~"chr() function returns a string of length one.Concepts in Practice 3.1.7 §3.1 source ↗
- a.
t - b.
/t - c.
\t
Answer
\tConcepts in Practice 3.1.8 §3.1 source ↗
- a.
print(\\) - b.
print(\"\") - c.
print("\\")
Answer
print("\\")Concepts in Practice 3.1.9 §3.1 source ↗
- a.
print("Enter here") - b.
print("Enter" + \n + "here") - c.
print("Enter" + "\n" + "here")
Answer
print("Enter" + "\n" + "here")\n produces a new line. print("Enter\nhere") produces the same result.Concepts in Practice 3.2.1 §3.2 source ↗
animal = "dog"
says = "bark"
print(f"My {animal} says {says} {says} {says}")- a.
My dog bark bark bark bark - b.
My dog says bark bark bark - c.
Error
Answer
My dog says bark bark barksays, is used in three replacement fields.Concepts in Practice 3.2.2 §3.2 source ↗
temp = "hot"
food = "potato"
print("{temp} {food}")- a.
{temp} {food} - b.
hot potato - c.
Error
Answer
{temp} {food}Concepts in Practice 3.2.3 §3.2 source ↗
print(x, "+", y, "=", x + y)
- a.
print(f"x + y = {x+y}") - b.
print(f"{x + y} = {x + y}") - c.
print(f"{x} + {y} = {x+y}")
Answer
print(f"{x} + {y} = {x+y}")Concepts in Practice 3.2.4 §3.2 source ↗
MM/DD/YYYY?- a.
f"{month:02d}/{day:02d}/{year}" - b.
f"{month:2d}/{day:2d}/{year}" - c.
f"{month}/{day}/{year}"
Answer
f"{month:02d}/{day:02d}/{year}"02d appends a 0 to the front of the integer when the integer is only one digit.Concepts in Practice 3.2.5 §3.2 source ↗
money rounded to two decimal places?- a.
print(f"{money:2d}") - b.
print(f"{money:2f}") - c.
print(f"{money:.2f}")
Answer
print(f"{money:.2f}") ".2f" (with a period) means fixed-point, rounded to two decimal places.Concepts in Practice 3.2.6 §3.2 source ↗
- a.
.2,f - b.
,.2f - c.
,d.2f
Answer
,.2fConcepts in Practice 3.3.1 §3.3 source ↗
- a.2
- b.3
- c.4
Answer
Concepts in Practice 3.3.2 §3.3 source ↗
- a.two float objects
- b.two int objects, one float object
- c.one int object, one float object
Answer
10 and the float object 9.7 , as shown in the memory diagram.Concepts in Practice 3.3.3 §3.3 source ↗
- a.an arrow
- b.a small black box
- c.a rounded box
Answer
Concepts in Practice 3.3.4 §3.3 source ↗
id(rating)?- a.
9793344 - b.
<class 'float'> - c.
10.0
Answer
9793344Concepts in Practice 3.3.5 §3.3 source ↗
type(rating)?- a.
10.0 - b.
"float" - c.
<class 'float'>
Answer
<class 'float'>Concepts in Practice 3.3.6 §3.3 source ↗
- a.
value(rating) - b.
rating - c.
"rating"
Answer
ratingConcepts in Practice 3.4.1 §3.4 source ↗
- a.
new_list == [3, 4, 5] - b.
new_list = [3, 4, 5] - c.
new_list = (3, 4, 5)
Answer
new_list = [3, 4, 5]Concepts in Practice 3.4.2 §3.4 source ↗
- a.
my_list = [2, 2 3, 23] - b.
my_list = ["Jimmy", 2, "times"] - c.
my_list = ["C", "C++", "Python", "Java", "Rust", "Scala"]
Answer
my_list = [2, 2 3, 23]2 3.Concepts in Practice 3.4.3 §3.4 source ↗
cardList?- a.
card_list[17] - b.
16)card_list(16) - c.
card_list[16]
Answer
card_list[16]Concepts in Practice 3.4.4 §3.4 source ↗
"Tom" to "Tim" in the following list? name_list = ["Carla", "Monique", "Westin", "Tom"]
- a.
name_list[-1] = "Tim" - b.
name_list[4] = "Tim" - c.
name_list[end] = "Tim"
Answer
name_list[-1] = "Tim"len(name_list)-1, which evaluates to 3 and refers to the last element in a list of 4 elements, can also be used in place of -1 here.Concepts in Practice 3.5.1 §3.5 source ↗
tuple_1's first element?- a.
tuple_1[0] - b.
tuple_1[1] - c.
tuple_1[2]
Answer
tuple_1[0] 2 , is at index 0.Concepts in Practice 3.5.2 §3.5 source ↗
- a.
tuple_2 = (42, 26, 13) - b.
tuple_3 = (42, 26, 13.5) - c.both
Answer
Concepts in Practice 3.5.3 §3.5 source ↗
- a.
my_tuple = 'a', 'b', 'c' - b.
my_tuple = ['a', 'b', 'c'] - c.
my_tuple = ('a', 'b', 'c')
Answer
my_tuple = 'a', 'b', 'c' "a" , "b" , and "c" .Concepts in Practice 3.5.4 §3.5 source ↗
my_tuple?- a.
(0.693, 0.414, 3.142) - b.
(0.693, 1.414, 3.142) - c.
Error
Answer
(0.693, 1.414, 3.142)my_tuple is initially assigned with the tuple (0.693, 0.414, 3.142). Then my_tuple is assigned with the new tuple, (0.693, 1.414, 3.142), to correct a mistake with the second element.Concepts in Practice 3.5.5 §3.5 source ↗
tuple_1 = ('alpha', 'bravo', 'charlie')
tuple_1.append('charlie')- a.Append isn't allowed.
- b.Duplicates aren't allowed.
- c.Strings aren't allowed
Answer
Concepts in Practice 3.5.6 §3.5 source ↗
- a.list
- b.tuple
Answer
Loading chapter 4…
Problems and answers are adapted from Introduction to Python Programming by Udayan Das, Aubrey Lawson, Chris Mayfield and Narges Norouzi, OpenStax, and are used under the CC BY-NC-SA 4.0 licence. Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction. Illustrations: Copyright Rice University, OpenStax, under CC BY-NC-SA 4.0 license. This adaptation is released under the same licence, is non-commercial, and is neither affiliated with nor endorsed by OpenStax or Rice University.