0% found this document useful (0 votes)
1 views

Week 2 Q&A

Uploaded by

parkerupsc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Week 2 Q&A

Uploaded by

parkerupsc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Week 2

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.

Examples: Lists, Tuples ,Strings Range objects

Non-Sequential Data Types


These data types do not maintain a specific order of elements. Instead, elements are organized based on their
value or other properties (e.g., hash values). Data is typically accessed by keys or other identifiers, not by
index. These datatypes are applicable when fast lookups or uniqueness are prioritized over maintaining order.

Examples: Dictionaries, Sets

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:

strings - sequence of charecters

tuples - sequence of compound data

lists - sequence of multi data types

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,

list.append([1, 2]) adds [1, 2] as a single element.

● extend() iterates over its argument, adding each element to the list. For example,

list.extend([1, 2]) adds 1 and 2 as separate elements

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]

my_list.insert(2, 3) # Inserts 3 at index 2


print(my_list)

output:

[1, 2, 3, 4, 5]

Question 7:

How does range() work?

Answer:

Range() takes mainly three arguments:

i) start: integer starting from which the sequence of integers is to be returned

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:

i) Lists are mutable, meaning their elements can be modified (added,

removed, or changed).

ii) Tuples are immutable, meaning their elements cannot be modified after

creation.

Syntax:

i) Lists are defined using square brackets `[ ]`.

ii) Tuples are defined using parentheses `( )'.

Examples of Syntax:

List my_list = [1, 2, 3, 4]


Tuple: my_tuple = (1, 2, 3, 4)

Question 9

Write a note on the different sequence data types.

Answer

Strings: Sequence of characters.

Tuples: Sequence of compound data

Lists: Sequence of multi-data type objects .

Arrays: Sequence of constrained list of objects (all objects of the same datatype) .

Dictionary: Sequence of key-value pairs .

Sets: Sequence of unordered collection of unique data.

Range: Used for looping.

Question 10

Explain the concept of indexing and slicing in Python lists.

Answer

Indexing refers to accessing individual elements of a list using their positions (indices), starting from 0.

Slicing refers to extracting a portion of a list using a range of indices.

Question 11:

What is slicing in Python? Explain it with a code example.

Answer:

A technique used to extract a portion or sub-sequence from a sequence by specifying

a start, stop, and step.


start: The index where the slice begins.

stop: The index where the slice ends.

step: The step size between indices (optional). Defaults to 1 if omitted.

my_list = [10, 20, 30, 40, 50, 60, 70]

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

axis in the insert () and del() function.

The insert() function takes two parameters:

● index - position where an element needs to be inserted

● element - this is the element to be inserted in the list

del() - The del keyword is used to delete objects. You can pass the variable name with

the index value inside the braces.

Question 13

What is the difference between insert() and append() methods in Python? Explain with an example.

Answer:

append()

Adds an element to the end of the list.

Syntax: list.append(element)
insert()

Adds an element at a specified index in the list.

Syntax: list.insert(index, element)

Example:

my_list = [1, 2, 3]

my_list.append(4) # Output: [1, 2, 3, 4]

my_list.insert(1, 4) # Output: [1, 4, 2, 3]

Question 14:

Given the string:

stringSample = 'python for data science'

1.) Which function can be used to return the string with only the first letter capitalized?

2.) How can we replace the word 'python'in stringSamplewith "C++"?

Answer:

1. stringSample.capitalize()

2. stringSample.replace(‘python’,’C++’)

You might also like