# The Joy Of Computing Using Python Week 3 : Assignment 1 | NPTEL | Answers July 2023

## L = [ 'a','b','c','d','e','f','g','h'] print(L[2:5])

a. a, b, c
b. a, b, c, d
c. c, d, e
d. c, d, e, f

c. c, d, e

The given code snippet prints a sublist of the list L using slicing. The syntax for slicing is L[start:end], where start is the index of the first element to include in the sublist, and end is the index of the first element to exclude from the sublist.
Let's break down the example step by step:

The list L contains the elements: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'].
The slice L[2:5] starts from index 2 (inclusive) and goes up to index 5 (exclusive).
So, the sublist extracted using the slice will include elements at indices 2, 3, and 4. These elements are 'c', 'd', and 'e', respectively.

Therefore, the output of the code will be:
['c', 'd', 'e']

## Which of the following is a valid way to declare a dictionary in Python?

a. {1: "one", 2: "two", 3: "three"}
b. [1: "one", 2: "two", 3: "three"]
c. (1: "one", 2: "two", 3: "three")
d. <1: "one", 2: "two", 3: "three">

a. {1: "one", 2: "two", 3: "three"}

The correct way to declare a dictionary in Python is using curly braces {}. Among the options you provided, the first one is the correct one:
{1: "one", 2: "two", 3: "three"}

Dictionaries in Python are composed of key-value pairs enclosed in curly braces, where each key is separated from its corresponding value by a colon (:), and pairs are separated by commas.

## Which of the following method is correct to add an element at a specific position?

a. insert()
c. append()
d. index()

d. index()

The correct method to add an element at a specific position in a list in Python is the insert() method. The insert() method takes two arguments: the index where you want to insert the element, and the element itself.

example:
my_list = [1, 2, 3, 4]
my_list.insert(2, 10) # Inserts the element 10 at index 2
print(my_list) # Output: [1, 2, 10, 3, 4]
The other methods you mentioned are not used for adding an element at a specific position in a list:

add() is not a standard method for adding elements to a list. It's not commonly used for this purpose.
append() adds an element to the end of the list, not at a specific position.
index() is used to find the index of a particular element in the list, not to add elements.

## What is the correct syntax to add an item to the end of a list in Python?

b. list.append(item)
c. list.insert(item)
d. list.extend(item)

b. list.append(item)

examele:
my_list = [1, 2, 3] my_list.append(4) # Adds the element 4 to the end of the list print(my_list) # Output: [1, 2, 3, 4]

## Which of the following is not a valid data type in Python?

a. integer
b. string
c. float
d. character

d. character

In Python, individual characters are represented using strings, so the concept of a separate "character" data type is not present. Instead, characters are just single-character strings.
The other options ("integer," "string," and "float") are valid data types in Python:

"integer" represents whole numbers, positive or negative.
"string" represents sequences of characters.
"float" represents floating-point numbers (decimal numbers).

## What is the output of the following code?

```for i in range(1, 21):
if i % 3 == 0 and i % 5 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)```
a. Prints numbers from 1 to 20
b. Prints Fizz for multiples of 3 and Buzz for multiples of 5
c. Prints FizzBuzz for multiples of 3 and 5
d. None of the above

d. None of the above

The given code is a Python implementation of the FizzBuzz problem. The FizzBuzz problem is a common programming challenge where the program needs to print the numbers from 1 to a given number, replacing multiples of 3 with "Fizz", multiples of 5 with "Buzz", and multiples of both 3 and 5 with "FizzBuzz".

## What is the output of the following code? a = 5 b = 2 print(a // b)

a. 2
b. 2.5
c. 3
d. 2.0

a. 2

The code uses the floor division operator // to divide a by b. The floor division operator returns the largest integer that is less than or equal to the result of the division. In this case, a is 5 and b is 2, so a // b returns 2.

## What is the output of the following code?

```s = "hello"
print(s[::-1])
```
a. "hello"
b. "olleh"
c. "hlo"
d. "leh"

b. "olleh"

The given code is printing the reverse of the string s using slicing. The syntax s[::-1] is used to create a slice that starts from the end of the string and goes in reverse order.
Let's evaluate the code step by step:
The string s is assigned the value "hello".
The slice s[::-1] is created, which starts from the end of the string and goes in reverse order. So, it produces "olleh".

## What is the output of the following code?

```a = 10
b = 5
c = a % b
print(c)```
a. 2
b. 5
c. 0
d. 1

c. 0

The given code calculates the remainder of the division of a by b using the modulo operator % and then prints the result.
Let's evaluate the code step by step:

a is assigned the value 10.
b is assigned the value 5.
The modulo operation a % b is calculated, which is 10 % 5. The remainder of the division is 0.

## What is the output of the following code?

```s = "python"
print(s[1:4])```
a. "pyt"
b. "yth"
c. "tho"
d. "hon"