Q.(a) Given is a Python string declaration : NAME = "Learning Python is Fun" Write the output of : print(NAME[-5:-10:-1])
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →The first part involves string slicing with negative indices and a negative step, while the second part demonstrates how the update() method modifies a dictionary by overwriting existing keys and adding new ones.
Let's break down these Python operations, understanding the core concepts behind each.
Part (a): Understanding String Slicing with Negative Indices
Python strings are sequences, and one of their powerful features is slicing, which allows you to extract a portion of the string. The general syntax for slicing is [start:end:step].
start: This is the index where the slice begins. The character at this index is included.end: This is the index where the slice ends. The character at this index is not included; slicing stops just before this index.step: This determines the increment between indices. If omitted, it defaults to 1.
A crucial aspect of Python indexing is the use of negative indices. While positive indices count from the beginning of the string (0 for the first character, 1 for the second, and so on), negative indices count from the end of the string. So, -1 refers to the last character, -2 to the second-to-last, and so forth.
When the step value is negative, the slicing operation proceeds backward through the string. In such cases, the start index should typically be greater than the end index for any characters to be included in the slice.
Consider the given string: NAME = "Learning Python is Fun"
Let's map out the indices for "Learning Python is Fun" (22 characters, including spaces):
| Char | L | e | a | r | n | i | n | g | (sp) | P | y | t | h | o | n | (sp) | i | s | (sp) | F | u | n |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Positive index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| Negative index | -22 | -21 | -20 | -19 | -18 | -17 | -16 | -15 | -14 | -13 | -12 | -11 | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
The expression is NAME[-5:-10:-1].
start = -5: This corresponds to the character 's' (the last letter of "is").end = -10: This corresponds to the character 'h' (from "Python"). Remember, the character at theendindex is exclusive.step = -1: This means we are moving backward, one character at a time.
Starting from NAME[-5] ('s'), we move backward:
NAME[-5]is 's'NAME[-6]is 'i'NAME[-7]is ' ' (space)NAME[-8]is 'n'NAME[-9]is 'o'- We reach
NAME[-10]which is 'h'. Since theendindex is exclusive, 'h' is not included in the slice.
Therefore, the characters extracted, in order, are 's', 'i', ' ', 'n', 'o'.
When the step is negative, the slice proceeds from start down to end (exclusive). If start is less than end with a negative step, the result will be an empty string.
The output of print(NAME[-5:-10:-1]) will be:
si no
Part (b): Understanding Dictionary update() Method
Dictionaries in Python are collections of key-value pairs, where each key must be unique. The update() method is a powerful tool for merging dictionaries or adding new key-value pairs.
The update() method works as follows:
- It takes another dictionary (or an iterable of key-value pairs) as an argument.
- It iterates through the key-value pairs of the argument dictionary.
- For each key-value pair:
- If the key already exists in the dictionary on which
update()is called, its corresponding value is overwritten with the new value from the argument dictionary. …
- If the key already exists in the dictionary on which
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.