# The Joy of Computing using Python | Week 3

Session: JAN-APR 2024

Course Name: The Joy of Computing using Python

#### Q1. Consider the following code snippet:What does the code do?Takes a list of numbers as input, appends each number to a list, and prints the list of numbersTakes a list of elements as input, appends each element to a list, and prints the list of elementsTakes the count of elements followed by the same number of elements as input, appends each element to a list, and prints the list of elementsTakes the count of elements, appends each element to a list, and prints the count of elements entered.

Answer: c. Takes the count of elements followed by the same number of elements as input, appends each element to a list, and prints the list of elements

Q2. Data Structure is a way by which you can organize/arrange your data. Which of the following statements are true about List Data Structure: [MSQ]
It is a flexible Data Structure
Elements could be added to a list.
Elements could be subtracted from a list.
This_is_not_List = [ ] is an empty list

It is a flexible Data Structure
Elements could be added to a list.
Elements could be subtracted from a list.
This_is_not_List = [ ] is an empty list

These are the Joy of Computing using Python Assignment 3 Answers

Q3. Consider the following code snippet:
What does the above code print?

Amar, Akbar, Anthony
amar is brother of anthony and akbar
Amar is brother of Anthony and Akbar
Anthony is brother of Akbar and Amar

Answer: d. Anthony is brother of Akbar and Amar

Q4. Consider the following code snippet:
What does the code do?

Takes a list of numbers, multiplies each number by 2, and prints the updated list
Takes a list of numbers, appends each number to the list twice, and prints the updated list
Takes a list of numbers, removes even numbers from the list, and prints the updated list
Takes a list of numbers, divides each number by 2, and prints the updated list

Answer: a. Takes a list of numbers, multiplies each number by 2, and prints the updated list

These are the Joy of Computing using Python Assignment 3 Answers

Q5. What will be the output of the following Python code?
“1111111111”
“0000000000”
A string with some 1s and some 0s
The function will raise an error

Answer: c. A string with some 1s and some 0s

Q6. Consider the following code snippet:
What does the code do?

Takes a list of numbers as input, computes the sum of the numbers, and prints the sum along with the average
Takes the count of elements, computes the sum of the elements, and prints the sum along with the average
Takes a list of numbers as input, computes the average of the numbers, and prints the average along with the sum
None of the above

Answer: b. Takes the count of elements, computes the sum of the elements, and prints the sum along with the average

These are the Joy of Computing using Python Assignment 3 Answers

Q7. What will be the output of the following Python code?
“Python”
A random permutation of the letters in “python”
“random”
The function will raise an error

Answer: b. A random permutation of the letters in “python”

Q8. Consider the following code snippet:
What does the code do? [MSQ]

Reverses the numbers list and stores it in new_numbers.
Creates an empty list named new_numbers and appends elements from numbers to it in reverse order.
Produces an error due to an invalid function used for list manipulation.
Generates a new list new_numbers with elements from numbers in the same order as numbers.

These are the Joy of Computing using Python Assignment 3 Answers

Q9. Which of the following are the examples of Social Computing/Crowd Computing: [MSQ]
Wikipedia
Stack Exchange
Quora

Wikipedia
Stack Exchange
Quora

Q10. Consider the following code snippet:
What does the code do?

Prints numbers from 1 to 20, replacing multiples of 3 with “Fizz”, multiples of 5 with “Buzz”, and multiples of both 3 and 5 with “FizzBuzz”.
Prints numbers from 1 to 20, replacing multiples of 3 and 5 with “FizzBuzz”, multiples of 3 with “Fizz”, and multiples of 5 with “Buzz”.
Prints numbers from 1 to 20, replacing multiples of 3 with “Fizz”, multiples of 5 with “Buzz”, and numbers divisible by both 3 and 5 with their product.
Prints numbers from 1 to 20, replacing multiples of 3 with “Buzz”, multiples of 5 with “Fizz”, and numbers divisible by both 3 and 5 with “FizzBuzz”.

Answer: a. Prints numbers from 1 to 20, replacing multiples of 3 with “Fizz”, multiples of 5 with “Buzz”, and multiples of both 3 and 5 with “FizzBuzz”.

These are the Joy of Computing using Python Assignment 3 Answers

### The Joy of Computing using Python Programming Assignment

Question 1

You are given a list marks that has the marks scored by a class of students in a Mathematics test. Find the median marks and store it in a float variable named median. You can assume that marks is a list of float values.

Solution:

``````    for i in range(0, len(marks)):
for j in range(i+1, len(marks)):
if marks[i] >= marks[j]:
marks[i], marks[j] = marks[j],marks[i]

#print(marks)
m=len(marks)//2
if len(marks)%2!=0:
median=marks[m]
else:
median=(marks[m-1]+marks[m])/2``````

These are the Joy of Computing using Python Assignment 3 Answers

Question 2

Accept a string as input, convert it to lower case, sort the string in alphabetical order, and print the sorted string to the console. You can assume that the string will only contain letters.

Solution:

``````get=input()
get=get.lower()
print("".join(sorted(get)), end="")``````

These are the Joy of Computing using Python Assignment 3 Answers

Question 3

You are given the dates of birth of two persons, not necessarily from the same family. Your task is to find the younger of the two. If both of them share the same date of birth, then the younger of the two is assumed to be that person whose name comes first in alphabetical order (names will follow Python’s capitalize case format).
The input will have four lines. The first two lines correspond to the first person, while the last two lines correspond to the second person. For each person, the first line corresponds to the name and the second line corresponds to the date of birth in DD-MM-YYYY format. Your output should be the name of the younger of the two.

Solution:

``````p1=input()
dob1=input()
p2=input()
dob2=input()
y1=int(dob1.split('-')[2])
y2=int(dob2.split('-')[2])
m1=int(dob1.split('-')[1])
m2=int(dob2.split('-')[1])
d1=int(dob1.split('-')[0])
d2=int(dob2.split('-')[0])
#print(y1,m1,d1)
if (y1>y2):
print(p1,end="")
elif (y2>y1):
print(p2,end="")
else:
if(m1>m2):
print(p1,end="")
elif(m2>m1):
print(p2,end="")
else:
if(d1>d2):
print(p1,end="")
elif(d2>d1):
print(p2,end="")
else:
print(min(p1,p2),end="")``````

These are the Joy of Computing using Python Assignment 3 Answers

Session: JULY-DEC 2023

Course Name: The Joy of Computing using Python

These are 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

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()
append()
index()

These are 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.append(item)
list.insert(item)
list.extend(item)

Q5. Which of the following is not a valid data type in Python?
integer
string
float
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 FizzBuzz for multiples of 3 and 5

These are 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

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

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

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

2
5
0
1

These are 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”

These are the Joy of Computing using Python Assignment 3 Answers

Session Jan-Apr 2023

Course Name: The Joy of Computing using Python

These are 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. Pushb. Writec. Insertd. All of the above

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

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 the Joy of Computing using Python Assignment 3 Answers

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

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

Q6. What is the output of this code?

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

These are the Joy of Computing using Python Assignment 3 Answers

Q7. What is the output of the following code?

Q8. What is the output of the following code?

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

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

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

These are the Joy of Computing using Python Assignment 3 Answers

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

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

### The Joy of Computing using Python Programming Assignment

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 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 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 the Joy of Computing using Python Assignment 3 Answers

Session JUL-DEC 2022

#### Course name: The Joy of Computing Using Python

These are 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 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
d. Increase

These are 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()
c. append()
d. index()

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

These are 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

These are the Joy of Computing using Python Assignment 3 Answers

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

These are the Joy of Computing using Python Assignment 3 Answers

Q9. Which of the following statements are true about crowd-sourcing?

These are 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—)

## 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 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 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 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