Skip to content
Question

Q.(a) Given is a Python string declaration : NAME = "Learning Python is Fun" Write the output of : print(NAME[-5:-10:-1])

(b) Write the output of the code given below : dict1={1:["Rohit",20], 2:["Siya",90]} dict2={1:["Rahul",95], 5:["Rajan",80]} dict1.update(dict2) print(dict1.values())
CBSECBSE Class XII Board 2023Subjective· 2mImportance★★★★★
🔒 Locked · start free trial →

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):

CharLearning(sp)Python(sp)is(sp)Fun
Positive index0123456789101112131415161718192021
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 the end index is exclusive.
  • step = -1: This means we are moving backward, one character at a time.

Starting from NAME[-5] ('s'), we move backward:

  1. NAME[-5] is 's'
  2. NAME[-6] is 'i'
  3. NAME[-7] is ' ' (space)
  4. NAME[-8] is 'n'
  5. NAME[-9] is 'o'
  6. We reach NAME[-10] which is 'h'. Since the end index is exclusive, 'h' is not included in the slice.

Therefore, the characters extracted, in order, are 's', 'i', ' ', 'n', 'o'.

Important

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. …

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.