remainder and lists
What is the value….
Assume x = 42 (hint: an integer)
Assume primes = [2, 3, 5, 7]
x / 5
x % 5
len(primes)
primes + [11]
primes[1] == 2
(BTW, If you had an excused absence, this pop quiz won't count in your pop quiz average. I know that a bunch of athletes had excused absences, Feb 22.)
Ans 1: 42/5 is 8
- Not 8.4, because both values are integers, so this is integer division.
- Not 8 remainder 2, because Python gives one result.
Ans 2: 42 % 5 is 2
- This is the remainder left over.
- Not 8 remainder 2, because Python gives one result.
Ans 3: len(primes) is 4
- Because that is the number of elements in or length of the list.
- Some of you gave lists as the answer. len is the length function, so it returns a number.
Ans 4: primes + [11] is [2,3,5,7,11]
- I did not insist on the commas or square brackets in grading this, but strictly speaking they should be there.
- Some of you added 11 to all the items or did other weird things. There is no arithmetic here. Adding lists, like adding strings, just concatenates one list onto the end of another: You are "adding" one list to another.
Ans 5: primes[1] == 2 is 0
- I also accepted "false" which is what 0 means here, but the correct Pythonic value is 0.
- Note that the first item of the list is primes[0] not primes[1]. If you thought the answer was 1, or true, be careful in future about off-by-one errors.
- Many of you gave the answer 2, but we already know that the number is two: we want to know whether the element at position 1 is that number.
- Some of you gave lists as the answer. Any expression containing equality or comparison operators must have a logical value, so you should have worked out that the answer had to be either 1 or 0. It's always a good idea to work out what type of value an expression must have as a sanity check before you try to work out its actual value.
Link to this Page