NPTEL Data Science Using Python – Week 4: Assignment 4 Answers
1. Which method would you use to check if a string contains only digits?
isnumeric()
isdigit()
isdecimal()
isnumber()
2. Which of the following are true about strings in Python?
- Answer: Strings are immutable. Strings can be created using single, double, or triple quotes. You can access characters in a string using positive or negative indices. Strings support the multiplication operator for repetition.
- Strings are immutable.
- Strings can be created using single, double, or triple quotes.
- You can access characters in a string using positive or negative indices.
- Strings support the multiplication operator for repetition.
3. Which operations can be performed with strings in Python?
- Concatenation
- Multiplication
- Division
- Slicing
4. Python strings are __, meaning that once they are created, they cannot be altered.
- Answer: Immutable
5. To check if a character or substring exists within another string, the __ operator is used.
- Answer:
in
6. Which statements about lists in Python are correct?
- Lists are mutable.
- Lists can contain elements of different types.
- Lists contain elements from the same type only.
- Lists cannot contain duplicate elements.
7. Given the list data = [10, 20, 30, 20, 10, 50, 60, 40, 80, 50, 40]
, which of the following code snippets would correctly remove all duplicate items and keep only unique items in the list?
list(set(data))
data.remove(duplicates)
data.unique()
unique(data)
8. Select all the correct statements about the function text_processor
based on its description and functionality:
- The function returns all words from the input.
- The function returns the total number of words in the input.
- The function returns a list of the longest words found in the input string.
- The function counts and returns the number of unique words in the input.
9. What does the function text_processor
return when called with the string “hello world hello Python”?
(['Python'], 4)
(['hello', 'Python'], 3)
(['hello'], 2)
(['Python'], 3)
10. What does the following tuple slicing operation return: (1, 2, 3, 4, 5)[1:3]
?
(1, 2)
(2, 3)
(3, 4)
(2, 3, 4)
11. Which operation is not possible with tuples in Python?
- Accessing elements by index
- Concatenating with another tuple
- Modifying an element
- Using the
in
operator to check membership
12. What is the output of set([1, 2, 2, 3, 4, 1, 5])
?
- Answer:
{1, 2, 3, 4, 5}
[1, 2, 3, 4, 5]
{1, 2, 3, 4, 5}
(1, 2, 3, 4, 5)
- None of the above
13. Dictionaries in Python are collections of key-value pairs where keys must be __.
- Answer: Immutable
14. What will be the result of inventory['001']['price']
?
- Answer:
29.95
'Python Programming'
'John Doe'
29.95
True
15. Which operations are valid to modify the book inventory based on the provided dictionary data?
- Adding a new book:
inventory['003'] = {'title': 'Data Science Fundamentals', 'author': 'Laura Brown', 'price': 40.00, 'available': True}
- Removing a book from the inventory:
del inventory['002']
- Changing the author of a book:
inventory['001']['author'] = 'Johnathan Doe'
- Checking if a book is available using:
inventory['001']['in_stock']