KnowledgeBoat Logo
|

Informatics Practices

What will be the output produced by the following codes, considering the Series object S given in Q.13?

(a) print(S[1:4])

(b) print(S[:1])

(c) print(S[0:2])

(d) S[0:2] = 12
     print(S)

(e) print(S.index)

(f) print(S.values)

The Series object 'S' is as follows:

pencils      20
notebooks    33
scales       52
erasers      10
dtype: int64

Python Data Handling

2 Likes

Answer

(a) print(S[1:4])

notebooks    33
scales       52
erasers      10
dtype: int64

Working

The slice S[1:4] starts at index 1 and ends at index 3, hence, it includes three elements i.e., elements from index 1 and 3.

(b) print(S[:1])

pencils    20
dtype: int64

Working

The slice S[:1] starts at index 0 and ends at index 1, but because the end index is exclusive, it includes only one element i.e., the element at index 0.

(c) print(S[0:2])

pencils      20
notebooks    33
dtype: int64

Working

The slice S[0:2] starts at index 0 and ends at index 1, hence, it includes two elements i.e., elements from index 0 and 1.

(d) S[0:2] = 12
     print(S)

pencils      12
notebooks    12
scales       52
erasers      10
dtype: int64

Working

The slice S[0:2] = 12 assigns the value 12 to indices 0 and 1 in Series S, directly modifying those elements. The updated Series is then printed.

(e) print(S.index)

Index(['pencils', 'notebooks', 'scales', 'erasers'], dtype='object')

Working

The code print(S.index) displays the indices of Series S.

(f) print(S.values)

[12 12 52 10]

Working

The code print(S.values) displays the values of Series S.

Answered By

1 Like


Related Questions