Week 2 Q&A
Week 2 Q&A
Question 1.
What does the reshape function in NumPy do, and What is the purpose of the ndim attribute in NumPy?
Answer:
The reshape function in NumPy changes the shape of an array without altering its data.
The ndim attribute in NumPy returns the number of dimensions of a NumPy array.
Question 2:
What is the difference between sequential and non-sequential data types and give some examples?
Answer:
Sequential Data Types
These data types store elements in a specific, ordered sequence. Each element has a definite position,
typically based on an index. Access often involves iterating through the sequence or directly referencing an
index.
Question 3:
Explain pop( ) and remove( ) methods used for deleting an element from dictionary.
Ans:
• pop( ) - removes the element at the given index from the dictionary and prints the same.
• remove( ) - removes the first occurrence of the element with the specified value.
Question 4:
Write any two types of sequence data types and mention which data types can beused?
Answer:
Question 5:
What is the difference between append() and extend() methods in Python lists?
Answer:
● append() adds its argument as a single element to the end of the list. For example,
● extend() iterates over its argument, adding each element to the list. For example,
Question 6
What does the insert() function do in Python, and provide an example demonstrating its use in a list?
Answer
The insert() function in Python is used to insert an element at a specified position in a list. It takes two
arguments:
the index at which the element should be inserted and the element itself.
Example:
my_list = [1, 2, 4, 5]
output:
[1, 2, 3, 4, 5]
Question 7:
Answer:
ii) stop: integer before which the sequence of integers is to be returned.The range of integers end at
stop – 1.
iii) step: integer value which determines the increment between each integer in the sequence
Question 8:
What is the difference between lists and tuples in Python and give the syntax?
Answer:
removed, or changed).
ii) Tuples are immutable, meaning their elements cannot be modified after
creation.
Syntax:
Examples of Syntax:
Question 9
Answer
Arrays: Sequence of constrained list of objects (all objects of the same datatype) .
Question 10
Answer
Indexing refers to accessing individual elements of a list using their positions (indices), starting from 0.
Question 11:
Answer:
sliced_list = my_list[1:4]
print(sliced_list)
Question 12:
Why is the axis parameter necessary when using the insert () and delete () functions?
Answer:
delete () function is not available in python only del() is available and there is no
del() - The del keyword is used to delete objects. You can pass the variable name with
Question 13
What is the difference between insert() and append() methods in Python? Explain with an example.
Answer:
append()
Syntax: list.append(element)
insert()
Example:
my_list = [1, 2, 3]
Question 14:
1.) Which function can be used to return the string with only the first letter capitalized?
Answer:
1. stringSample.capitalize()
2. stringSample.replace(‘python’,’C++’)