Informatics Practices

What will be the output produced by following code, considering the Series object S given above ?

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

(b) print(S[0:1])

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

(d)

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

(e)

print(S.index)    
print(S.values)  

Python Pandas

1 Like

Answer

(a)

Series([], dtype: int64)

Working

The slice S[1:1] starts at index 1 and ends at index 1, but because the end index is exclusive, it does not include any elements, resulting in an empty Series.

(b)

pencils    20
dtype: int64

Working

The slice S[0: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)

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)

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)

Index(['pencils', 'notebooks', 'scales', 'erasers'], dtype = 'object')
[20 33 52 10]

Working

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

Answered By

2 Likes


Related Questions