Final 6 Flashcards

1
Q

Does python’s “a variable is a pointer to a value” idea make it easier for variables to change size?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

According to lectures, when we change the value
of a variable, does the interpreter accomplish this by changing the value at the place pointed to by the variable, or does the interpreter accomplish this by moving the pointer over to point to another place in memory that has the new desired value.

a) first way b) second way

A

b) second way

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Can we change a variable’s type at runtime?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

According to lectures, are constants and variables declared the same way in Python?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Would you expect = or == to be used in the expression of an if statement?

A

==

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How many ways are there to declare a variable in Python as expressed in class lectures?

A

One

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Is it normal to execute pseudo code?

A

No, pseudo code is not executable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What happens when a variable (all of it, not a part of it) is assigned a value?

A

New value is put into some place of memory, and then the pointer value of the variable is changed to now point to this new place.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is one advantage of Pythons indention policy over C’s use of brackets
to show what code belongs within an if, etc.

A

Prevents disconnect between what the human reading the code understands it to mean and what the interpreter understands it to mean because it means the same thing to both. Less ambiguous.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what it the output of

print(“aaaaa”,end=””);print(“bbb”,end=””);print(“c”);print(“d”)

A

aaaaabbbc

d

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what is the output of

print(1,2,3,sep=””)

A

123
(default sep is a space, sep=”” removes it)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When doing expressions like (5 + 5.0)

the Python interpreter first turns the integer
5 into a float

a) True B) False

A

a) True, the integer 5 will be temporarily converted to a float and the output of the calculation will also be a float.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Given the code

AA = []

write code with the append function

to make AA become the list with 2 elements, both equal to 2

A

AA = []
AA.append(2)
AA.append(2)
print(AA)
[2, 2]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

According to lectures, is it possible for roundoff
errors to cause problems with equality with real numbers

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Starting with aaa = [], add elements 1-500 using a while loop and the append function. This is to say that index 0 of aaa should be 1, index 1 index should be 2, etc., up to the 499 index which should be 500 .

Then, after this first while loop, write a second while loop that prints what is in aaa, one element per line, so that you get the following output:
aaa 1
aaa 2
aaa 3
.
.
.
aaa 500

A

aaa = []
i = 0

while i < 500:
i += 1
aaa.append(i)

i = 0
while i < 500:
print(“aaa “ + str(aaa[i]))
i += 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Given the starting code

Names =[“Dan”,”Sally”]
Quiz1Grades=[70,56]
Quiz2Grades=[60,62]
AveGrades=[]

write two lines of code which use a for loop
and the append function to set AveGrades to a list
where for each indes, i, AveGrades[i] is
(Quiz1Grades[i] + Quiz2Grades[i])/2.0

again, use the append function to do this

add a second for loop that prints out
Names[i],Quiz1Grades[i],Quiz2Grades[i],AveGrades[i]

all on one line, for the 2 indexes

In other words, the total code should result in

Dan 70 60 65.0
Sally 56 62 59.0

getting printed out

A

for i in range(len(Names)):
AveGrades.append((Quiz1Grades[i] + Quiz2Grades[i]) / 2.0)

for i in range(len(Names)):
print(Names[i], Quiz1Grades[i], Quiz2Grades[i], AveGrades[i])

17
Q

Given code

astring = “ James , Lamm : 12 , 19, 2021 “

write some lines of code using two “split” commands together
with the “strip”, “lower” commands together with “int” casts which
assign variables
FN
LN
Month
Day
Year
MonthPlusDay

to
james
lamm
12
19
2021
31

respectively. Note that you need to convert the Month and
Day to ints in order to add them up and put the
result in variable MonthPlusDay. Also note that lower
should be used to take out the caps.

A

astring = “ James , Lamm : 12 , 19, 2021 “

parts = astring.split(“:”)
names = parts[0].split(“,”)
dates = parts[1].split(“,”)

FN = names[0].strip().lower()
LN = names[1].strip().lower()
Month = int(dates[0].strip())
Day = int(dates[1].strip())
Year = int(dates[2].strip())

MonthPlusDay = Month + Day

print(“FN: “ + FN)
print(“LN: “ + LN)
print(“Month: “ + str(Month))
print(“Day: “ + str(Day))
print(“Year: “ + str(Year))
print(“MonthPlusDay: “ + str(MonthPlusDay))