Python for Data
Science
Live Interactive Problem-Solving Session
U. Sethu Vinayagam
PhD Scholar
Department of Civil Engineering, IIT
Madras
Jupyter Notebook
Overview • Setup and Usage
Sequence Data Types
• Mutable and Immutable Types
• String, List, Array, Tuple, Dictionary, Set, and Range
Indexing
• The index method
• Negative index
• Out-of-range error
• Indexing Using keys in Dictionaries
More Operations on Sequences
• Slicing, Concatenation and Repetition
Built-in Methods
• Methods for String, List, Array, Set, and Dictionary
Numpy
• Array creation, Reshaping, and Element-wise operations
Python for Data Science (noc24-ce68) Week 2 2
What is []?
Question 1 a) set
b) list
c) tuple
d) dict
Python for Data Science (noc24-ce68) Week 2 3
How would you replace the value ‘hello’ as the third
Question 2 value in a list stored in a variable named spam?
(Assume spam contains [2, 4, 6, 8, 10].)
a) spam = “hello”
b) spam[3] = ‘hello’
c) spam[2] = ‘hello’
d) spam[:3] = ‘hello’
Python for Data Science (noc24-ce68) Week 2 4
spam = [‘a’, ‘b’, ‘c’, ‘d’]
Question 3 What does spam[int(int(‘3’ * 2) // 11)] evaluate to?
a) Out-of-range error
b) ‘a’
c) ‘b’
d) ‘c’
e) ‘d’
Python for Data Science (noc24-ce68) Week 2 5
spam = [‘a’, ‘b’, ‘c’, ‘d’]
Question 4 What does spam[-1] evaluate to?
a) ‘a’
b) ‘b’
c) Error
d) [‘a’, ‘b’, ‘c’]
e) ‘d’
Python for Data Science (noc24-ce68) Week 2 6
spam = [‘a’, ‘b’, ‘c’, ‘d’]
Question 5 What does spam[:2] evaluate to?
a) ‘c’
b) ‘b’
c) [‘a’, ‘b’, ‘c’]
d) [‘a’, ‘b’]
e) (‘c’, ‘d’)
Python for Data Science (noc24-ce68) Week 2 7
bacon = [3.14, ‘cat’, 11, ‘cat’, True]
Question 6 What does bacon.index(‘cat’) evaluate to?
a) 3
b) 1
c) -2
d) -4
Python for Data Science (noc24-ce68) Week 2 8
bacon = [3.14, ‘cat’, 11, ‘cat’, True]
Question 7 What does bacon.append(99) make the list value in
bacon look like?
a) [3.14, ‘cat’, 11, ‘cat’, 99]
b) (99, 3.14, ‘cat’, 11, ‘cat’, True)
c) {3.14, ‘cat’, 11, ‘cat’, True, 99}
d) [3.14, ‘cat’, 11, ‘cat’, True, 99]
Python for Data Science (noc24-ce68) Week 2 9
bacon = [3.14, ‘cat’, 11, ‘cat’, True]
Question 8 What does bacon.remove('cat') make the list value
in bacon look like?
a) [3.14, ‘cat’, 11, True]
b) [3.14, 11, ‘cat’, True]
c) [3.14, 11, True]
d) Error
Python for Data Science (noc24-ce68) Week 2 10
What is the difference between the append() and
Question 9 insert() list methods?
Python for Data Science (noc24-ce68) Week 2 11
How do you create a tuple variable a that has just
Question 10 the integer value 42 in it?
a) a = 42
b) a = (42,)
c) a = [42]
d) a = (42)
Python for Data Science (noc24-ce68) Week 2 12
What is the output of the following code?
Question 11 text = "learning"
print(text.index("n"), text.index("ng"),
text.index("ning"))
a) -2, -2, -4
b) 4, 6, 4
c) 5, 5, 5
d) 4, 4, 4
Python for Data Science (noc24-ce68) Week 2 13
What is the output of the following code?
Question 12 numbers = [1, 2, 5, 1]
print(numbers[-1:1:-1])
a) [5, 1]
b) [1, 5]
c) Error
d) [1]
e) [1, 5, 2, 1]
Python for Data Science (noc24-ce68) Week 2 14
A = ("a", "b")
Question 13 B = ("c", "d")
print(A+B)
The above code produces the following output.
("a", "b", "c", "d")
If tuples are immutable sequences, then how is the
above output possible?
Python for Data Science (noc24-ce68) Week 2 15
Given a list of values and the index of an element in
Question 14 the list, how can the negative index corresponding
to the element be calculated?
Hint: Make use of the len() function.
Python for Data Science (noc24-ce68) Week 2 16
Which of the following statements about tuples is
Question 15 incorrect?
a) Tuples are immutable.
b) Tuples can be used as keys in dictionaries.
c) Tuples can be modified after creation.
d) Tuples can contain elements of different data
types.
Python for Data Science (noc24-ce68) Week 2 17
Given the string s = "hello world", what will be the
Question 16 output of s[-5:]?
a) ‘world’
b) ‘o worl’
c) ‘world ‘
d) An error will occur
Python for Data Science (noc24-ce68) Week 2 18
Consider the following code:
Question 17 my_str = "python"
new_str = my_str[::-1]
What will be the value of new_str?
a) ‘python’
b) ‘nohtyp’
c) ‘typhon’
d) ‘n’
Python for Data Science (noc24-ce68) Week 2 19
Given the list my_list = [1, 2, 3, 4, 5], what will be the
Question 18 output of my_list[2:4] = [6, 7]?
a) [1, 2, 6, 7, 5]
b) [1, 2, 6, 7, 4, 5]
c) [6, 7]
d) [1, 6, 7, 4, 5]
Python for Data Science (noc24-ce68) Week 2 20
What is the purpose of the in operator when used
Question 19 with a list?
a) To check if an element is present in the list
b) To iterate over the elements of the list
c) To modify the elements of the list
d) To create a new list
Python for Data Science (noc24-ce68) Week 2 21
Consider the following dictionary:
Question 20 my_dict = {‘a’: 1, ‘b’: 2, ‘c’: 3}
What is the output of my_dict.get(‘d’, 0)?
a) 0
b) None
c) KeyError
d) 3
Python for Data Science (noc24-ce68) Week 2 22
Given the dictionary my_dict = {‘a’: 1, ‘b’: 2, ‘c’: 3},
Question 21 how would you remove the key-value pair ‘b’: 2?
a) del my_dict[‘b’]
b) my_dict.pop(‘b’)
c) my_dict.delete(‘b’, 2)
d) my_dict.discard(‘b’)
Python for Data Science (noc24-ce68) Week 2 23
Given the sets set1 = {1, 2, 3} and set2 = {2, 3, 4},
Question 22 what is the result of
set1.symmetric_difference(set2)?
a) {1, 4}
b) {1, 2, 3, 4}
c) {2, 3}
d) {}
Python for Data Science (noc24-ce68) Week 2 24
What is the primary use case for sets in Python?
Question 23 a) Storing ordered collections of elements
b) Storing unique elements
c) Performing mathematical operations on
numbers
d) Creating complex data structures
Python for Data Science (noc24-ce68) Week 2 25
What is the output of the following code?
Question 24 text = "Python programming"
result = text.replace(" ", "")
print(result)
a) Python programming
b) Pythonprogramming
c) pythonprogramming
d) None of the above
Python for Data Science (noc24-ce68) Week 2 26
What is the output of the following code?
Question 25 print(“Strings are immutable”.lower().title())
a) strings are immutable
b) Strings are Immutable
c) Strings Are Immutable
d) STRINGS ARE IMMUTABLE
Python for Data Science (noc24-ce68) Week 2 27