Assignment 1
Corresponds to: Session 1
Quiz
1. What does REPL stand for? Can you name other programming languages/environments that offer a REPL?
Answer
REPL stands for Read, Evaluate, Print Loop. It is a programming environment with its own memory space and can evaluate single or multiple lines of code and produce output. It is an interactive environment where the programmer can try out lines of code one at a time and examine the results before proceeding. One disadvantage of REPL is that it does not save the code in a file for subsequent use, although it can read and execute code that is already written in a file. But that takes away the interactive nature of a REPL and a code editor is much better suited to execute code in a file.
Other programming languages that offer a REPL are Matlab, Julia, Javascript (Node JS) and many more.
2. Having assigned the value 10 to the object a, what are the different ways to display the value of the object a?
Answer
In the REPL, value of an object can be displayed by typing the name of the object and pressing Enter or by using the print() function.
print(a).
3. Given the list a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
3.1. Determine the number of elements in the list
3.2. Use negative indexing operation to obtain the last but one element of the list (in this example, the element with value 9). Negative index -1 is the last element and -2 is the last but one. and so on.
3.3. What are the slice start, stop and step values to produce the list [9, 6, 3]. Remember the slice notation a[start:stop:step].
4. You can create an empty list with a = []. Creating an empty tuple is similar b = (). But how do you create a tuple with exactly one element? Does b = (10) work? What is the correct way to create a tuple with exactly one element. Search the net for the answer.
Answer
>>> b = ()
>>> print(type(b), len(b))
<class 'tuple'> 0
>>> b = (10)
>>> print(type(b))
<class 'int'>
>>> b = (10,)
>>> print(type(b), len(b))
<class 'tuple'> 1
b = (10) creates an int. The comma (,) after the only value ((10,) in this case) is critical and indicates that it is a tuple with a single element.
5. The set data structure is a container, similar to list and tuple, but its elements are not indexed. Further, its elements are unique, there can only be one occurrence of an element in a set. Search for the official Python documentation for set and find the operations that can be performed on it. Can you add a new element? Can you remove an existing element? Can you check if a certain value is present in a set? Can you find the intersection of two sets? Are there other operations that can be performed?
Answer
>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
>>> basket
{'pear', 'banana', 'orange', 'apple'}
>>> len(basket)
4
>>> 'apple' in basket
True
>>> basket[0]
Traceback (most recent call last):
File "<python-input-15>", line 1, in <module>
basket[0]
~~~~~~^^^
TypeError: 'set' object is not subscriptable
>>> c = basket.union(b2)
>>> c
{'pear', 'banana', 'grapes', 'apple', 'orange'}
>>> basket - b2
{'pear', 'banana', 'orange'}
>>> b2 - basket
{'grapes'}
>>> basket + b2
Traceback (most recent call last):
File "<python-input-23>", line 1, in <module>
basket + b2
~~~~~~~^~~~
TypeError: unsupported operand type(s) for +: 'set' and 'set'
>>> d = {"apple", "Apple"}
>>> d
{'Apple', 'apple'}
- Order of the elements may not be in the same order as in the defnition of the set
- Elements of a set cannot be accessed by indexing
- It is possible to check if a value is contained in the set using the
inoperator - Union of two sets can be obtained using the method
set.union(aother_set). Using the+operator to find the union of two sets results in an error - Difference between two sets is obtained using the
-operatorset1 - set2returns the values ofset1that are not present inset2
6. Given the list a = [10, 20, 30, 40, 50], what does a[1:4] return?
A. [30, 40, 50]
B. [10, 20, 30]
C. [20, 30, 40, 50]
D. [20, 30, 40]
Answer
Correct answer: D
Explanation: The slice is defined by start index = 1, stop index = 4 (but 4 not included) with step size = 1. Thus the indices are 1, 2, 3 and the elements are a[1], a[2], a[3], anmely [20, 30, 40]
7. Which of the following operations is NOT allowed on tuples?
A. Item assignment
B. Slicing
C. Indexing
D. Concatenation
Answer
Correct answer: A
Explanation: The operation Item assignment is not allowed because tuple is immutable. However, making a copy of an existing tuple and combining it with another to create a new tuple gives an ppearance that a tuple is mutable.
8. What is the output of len((1, 2, (3, 4)))?
A. 3
B. 4
C. 2
D. Error
Answer
Correct answer: A
Explanation: The items of the tuple ar 1, 2 and (3, 4).
9. Which of the following data type is mutable?
A. int
B. str
C. tuple
D. list
Answer
Correct Answer: D
Explanation: Only list is mutable, the rest are immutable. Here is an exercise that wil clarify how an int is immutable.
>>> x = 10
>>> id(x)
93827280581664
>>> id(10)
93827280581664
>>> y = x
>>> id(y)
93827280581664
>>> x = 20
>>> id(x)
93827280581984
>>> id(20)
93827280581984
>>> id(y)
93827280581664
>>> z = 20
>>> id(z)
93827280581984
- Like the variable (object)
x, an integer constant such as10also has a memory address y = xmakesyan alternate name forxand have the same memory address as the integer 10x = 20creates a new integer20at a new memory adress and that address is assigned tox. But the memory address ofyremains unchanged to the memory address of10.z = 20creates a new variablezand is assigned the memory address that already contains the value20- Integers
10and20are immutable, once created their value stored in a specific memory address does not change
10. What is the result of ('a', 'b') + ('c',)?
A. Error
B. ['a', 'b', 'c']
C. ('a', 'b', 'c')
D. ('a', 'b')
Answer
('c',) is a tuple containing a single element and when added to the tuple with two elements ('a', 'b') returns a new tuple with three elements. If the comma (,) after c is missed, ('')is not atuple` and the operation would result in an error.
11. Create a dict object to represent the name. age and phone number of a person.
11.1. Obtain the keys and values of the object.
Answer
11.2. Obtain the value for the name of the person
11.3. Increase the age of the person by 1 year
Answer True or False
1. The REPL manages its own memory to store the objects created by you, as well as the modules and packages imported by you
Answer
True
REPL is a complete Python environment with the built-in Python interpreter, memory space and memory manager. As you type in each command, REPL will interpret your commands and if valid, carry them out. Carrying out a command may involve creating objects in memory, importing modules from an external source and store in memory for later use, compile a function and store it in memory and make it available for subsequent use.
2. In a REPL, you can display the value of an object without having to use the print() function whereas it is necessary to use the print() function to display the value of an object in a script (code written in a file and executed from the terminal).
Answer
True
REPL has a built-in interpreter and has it logic for interpreting the commands typed atthe prompt. When the user types the name of an object, the interpreter is programmed to print the corresponding value of the object.
But when the name of an object apperas all by itself in script, becuase it is not assigned to any other object or not used in any other way, that line does not result in any output. In a script, one must use the print() function to display values.
3. Data type str is immutable.
Answer
True
4. Immutable means the value assigned to the object cannot be changed once assigned.
Answer
True
5. Immutability only prevents replacing an element with a new value, but does not stop you from assigning an entirely new value to the object. That is, a = (1, 2, 3); a[0] = 10 is not permitted, but a = (1, 2, 3); a = (10, 20, 30) is permitted.
Answer
True
6. Data type list is immutable.
Answer
False
7. You can embed an apostrophe (') inside a string constant delimited by apostrophes, provided it is escaped using the backslash \'. That is, this is a valid string: 'This isn\'t difficult'.
Answer
True
>>> 'This isn\'t difficult'
"This isn't difficult"
>>> print('This isn\'t difficult')
This isn't difficult
Note that the REPL normally delimits the string by single quotes (') when you type the object or the name of an object. But when the string contains single quotes within it, it delimits the string by double quotes (").
When the string is displayed using the print() function, no quotes are shown in the REPL.
8. When you carry out an index or slice operation on a list, tuple or a string, the operation returns a new object with the values copied from the original object.
Answer
True
Using the index of an element returns a single object of the same type as that of the returned object.
Using the slicing operation returns a list with the objects as defined in the slice.
>>> a = [1, 2, 3, 4, 5]
>>> type(a[0]), a[0]
(<class 'int'>, 1)
>>> type(a[::2]), a[::2]
(<class 'list'>, [1, 3, 5])
If you observe, you will notice that typing more than one object in the REPL command promt, separated commas returns a tuple with the values of the objects typed at the command prompt.