The Joy of Computing using Python | Week 3

Session: JULY-DEC 2023

Course Name:  The Joy of Computing using Python

Course Link: Click Here

These are answers of The Joy of Computing using Python Assignment 3 Answers


Q1. What will be the output of the following code?
L = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’]
print(L[2:5])

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

Answer: c, d, e


Q2. Which of the following is a valid way to declare a dictionary in Python?
{1: “one”, 2: “two”, 3: “three”}
[1: “one”, 2: “two”, 3: “three”]
(1: “one”, 2: “two”, 3: “three”)
<1: “one”, 2: “two”, 3: “three”>

Answer: {1: “one”, 2: “two”, 3: “three”}


Q3. Which of the following method is correct to add an element at a specific position?
insert()
add()
append()
index()

Answer: insert()


These are answers of The Joy of Computing using Python Assignment 3 Answers


Q4. What is the correct syntax to add an item to the end of a list in Python?
list.add(item)
list.append(item)
list.insert(item)
list.extend(item)

Answer: list.append(item)


Q5. Which of the following is not a valid data type in Python?
integer
string
float
character

Answer: character


Q6. What is the output of the following code?
Prints numbers from 1 to 20
Prints Fizz for multiples of 3 and Buzz for multiples of 5
Prints FizzBuzz for multiples of 3 and 5
None of the above

Answer: Prints Fizz for multiples of 3 and Buzz for multiples of 5


These are answers of The Joy of Computing using Python Assignment 3 Answers


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

2
2.5
3
2.0

Answer: 2


Q8. What is the output of the following code?
s = “hello”
print(s[::-1])

“hello”
“olleh”
“hlo”
“leh”

Answer: “olleh”


Q9. What is the output of the following code?
a = 10
b = 5
c = a % b
print(c)

2
5
0
1

Answer: 0


These are answers of The Joy of Computing using Python Assignment 3 Answers


Q10. What is the output of the following code?
s = “python”
print(s[1:4])

“pyt”
“yth”
“tho”
“hon”

Answer: “yth”


These are answers of The Joy of Computing using Python Assignment 3 Answers

More Weeks of The Joy of Computing Using Python: Click here

More Nptel Courses: Click here


Session Jan-Apr 2023

Course Name:  The Joy of Computing using Python

Course Link: Click Here

These are answers of The Joy of Computing using Python Assignment 3 Answers


Q1. _____ is the method to insert an item into a specified position in a list.
a. Push
b. Write
c. Insert
d. All of the above

Answer: c. Insert


Q2. Which method returns the number of occurrences of an element in a list.
a. Number of
b. Total
c. Count
d. Length

Answer: c. Count


Q3. The function random.randint(1,100) in python generates.
a. A random integer between 1 to 100 with 1 and 100 both inclusive
b. A random integer between 1 to 100 with 1 and 100 both exclusive
c. A random integer between 1 to 100 with only 100 inclusive
d. None of the above

Answer: a. A random integer between 1 to 100 with 1 and 100 both inclusive


These are answers of The Joy of Computing using Python Assignment 3 Answers


Q4. The method open(“file1.txt”, r+) opens the file file1.txt in ______.
a. Read only mode
b. Write only mode
c. Read Write mode
d. None of the above

Answer: c. Read Write mode


Q5. Consider the list L= [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]. What will be the output of the statement L [3:6]?
a. [2, 3, 5]
b. [0, 1, 1]
c. [1, 2, 3]
d. none

Answer: a. [2, 3, 5]


Q6. What is the output of this code?

image 34

a. 0
b. 1
c. 2
d. This code will raise a runtime error

Answer: a. 0


These are answers of The Joy of Computing using Python Assignment 3 Answers


Q7. What is the output of the following code?

image 35

Answer: 1


Q8. What is the output of the following code?

image 36

a. False True
b. True False
c. True True
d. False False

Answer: a. False True


Q9. Explain what the output will be when the code given below is executed.

image 37
image 38

a. The program throws an error
b. 5
c. 5.5
d. 4.5

Answer: d. 4.5


These are answers of The Joy of Computing using Python Assignment 3 Answers


Q10. Which among the following statements is True with respect to the code given below?

image 39

a. count=50
b. The following code throws up an error.
c. count=550
d. count=450

Answer: d. count=450


The Joy of Computing using Python Programming Assignmnet

Question 1

Take an input N as an integer and write a program to display a right angle triangle with N rows and N columns.
Input
5

Solution:

num = int(input())
k = 1
for i in range(0, num):
    for j in range(0, k):
        print("* ", end="")
    k = k + 1
    print()

These are answers of The Joy of Computing using Python Assignment 3 Answers


Question 2

Write a program to take an input N and print Fibonacci sequence till N terms.
The Fibonacci sequence is a set of integers (the Fibonacci numbers) that starts with a zero, followed by a one, then by another one, and then by a series of steadily increasing numbers. The sequence follows the rule that each number is equal to the sum of the preceding two numbers.
Input
7
Output
0
1
1
2
3
5
8
Explanation: since the input is 7 there are seven terms. The first two terms are always 0 and 1. The third term is sum of first two terms. The fourth term is sum of second and third terms and so on.

Solution:

# Python program to display the Fibonacci sequence
n = int(input())
def recur_fibo(n):
  if n <= 1:
    return n
  else:
    return(recur_fibo(n-1) + recur_fibo(n-2))


# check if the number of terms is valid
if n <= 0:
   print("Plese enter a positive integer")
else:
   for i in range(n):
       print(recur_fibo(i))

These are answers of The Joy of Computing using Python Assignment 3 Answers


Question 3

Take base B and height H as an input and write a program to print a parallelogram.
Input
8
4
Output
********
********
********
********

Solution:

B = int(input())
H = int(input())

for i in range(H):
    print("*" * B)

These are answers of The Joy of Computing using Python Assignment 3 Answers

More Weeks of The Joy of Computing using Python: Click Here

More Nptel courses: https://progiez.com/nptel/


Session JUL-DEC 2022

Course name: The Joy of Computing Using Python

Link to Enroll: Click here

These are answers of The Joy of Computing using Python Assignment 3 Answers


Q1. Which of the following statements describes the challenge ‘Fizz Buzz’?

a. Multiples of 3 should print buzz, multiples of 5 should print fizz, and multiples of 3 and 5 should print fizz buzz.
b. Multiples of 3 should print fizz, multiples of 5 should print buzz, and multiples of 3 or 5 should print fizz buzz.
c. Multiples of 3 should print fizz, multiples of 5 should print buzz, and multiples of 3 and 5 should print fizz buzz.
d. Multiples of 3 should print buzz, multiples of 5 should print fizz, and multiples of 3 and 5 should print fizz buzz.

Answer: c. Multiples of 3 should print fizz, multiples of 5 should print buzz, and multiples of 3 and 5 should print fizz buzz.


These are answers of The Joy of Computing using Python Assignment 3 Answers


Q2. random.randint(1,100) will generate a number _________.(assume random is imported)
a. Between 1,100 both inclusive.
b. Between 1,100 both exclusive.
c. Between 1,100 only 100 inclusive.
d. Between 1,100 only 1 inclusive.

Answer: a. Between 1,100 both inclusive.


Q3. Consider a string of 20 digits initialized with all zeros as a DNA sequence, in the context of lectures, updating a random ‘zero’ as ‘one’ implies ___.
a. Updating a random number
b. Evolution
c. Degradation
d. Increase

Answer: b. Evolution


These are answers of The Joy of Computing using Python Assignment 3 Answers


Q4. Which of the following method is correct to add an element at a specific position?
a. insert()
b. add()
c. append()
d. index()

Answer: a. insert()


Q5. What will be the output of the following program?
a. Python, C++, Java, Kotlin
b. 0, 1, 2, 3
c. 0, 1, 2, 3, 4
d. Python, C++, Java

Answer: b. 0, 1, 2, 3


Q6. Which of the following methods is correct to count the number of instances on an element in a list?
a. total()
b. sum()
c. count()
d. numberof()

Answer: c. count()


These are answers of The Joy of Computing using Python Assignment 3 Answers


Q7. In the Fizz Buzz game, What will be the output if the number is 285?
a. Fizz
b. Buzz
c. Fizz Buzz
d. No output

Answer: c. Fizz Buzz


Q8. Which of the following keywords is used to define a function in python?
a. func
b. function
c. define function
d. def

Answer: d. def


Q9. Which of the following statements are true about crowd-sourcing?
a. Answers received via crowdsourcing are never correct.
b, Answers received via crowdsourcing can be as good as the answer by an expert.
c. Answers received via crowdsourcing can be better than the answer by an expert.
d. Answers received via crowdsourcing are always correct.

Answer: b, c


These are answers of The Joy of Computing using Python Assignment 3 Answers


Q10. Which of the following commands is not correct in order to generate a graph?

a. import matplotlib.pyplot as plt plt.plot([1,2,3,4],[5,6,7,8],to)

b. import matplotlib.pyplot as plt plt.plot([1,2,3,4],[5,6,7,8],r–)

c. import matplotlib.pyplot as plt plt.plot([1,2,3,4],[5,6,7,8],bs)

d. import matplotlib.pyplot as plt plt.plot([1,2,3,4],[5,6,7,8],r—)

Answer: d. def


Python Assignment 3 Programming Solutions

Question 1

There is list L containing some numbers. Write a program to create a new list which contains the numbers which are either divisible by 5 or 7 or both. Print that new list in ascending order.

Input is already managed for you.

Input : A list L

Output : A new list P

Example Input : I [7, 8, 9, 10, 11]

Output : [7, 10]

Solution:

L = [int(i) for i in input().split()]

ans = [z for z in L if z%5==0 or z%7==0]
print(sorted(ans), end = "")

These are answers of The Joy of Computing using Python Assignment 3 Answers


Question 2

Write a function rev which takes a list L and integer n and print the first n largest numbers of the list.

Input is managed for you, please write the required function only.

Input : A list L and an integer n.

Output : First n largest numbers

Solution:

def rev(L,n):
  print(sorted(L)[::-1][:n], end = "")

These are answers of The Joy of Computing using Python Assignment 3 Answers


Question 3

Write a program to count and print the number of odd numbers in a list L.

Input is managed for you.

Input : A list L

Output : Total number of odd numbers.

Solution:

L = [int(i) for i in input().split()]
even_count, odd_count = 0, 0

# iterating each number in list
for num in L :

       # checking condition
       if num % 2 != 0 :
               odd_count += 1

print (odd_count, end = "")

These are answers of The Joy of Computing using Python Assignment 3 Answers

More Weeks solutions of this course: https://progies.in/answers/nptel/the-joy-of-computing-using-python

More NPTEL Solution: https://progies.in/answers/nptel


The Joy of Computing using Python Assignment 3 Answers

This content is uploaded for study, general information, and reference purpose only.