# The Joy of Computing using Python Week 2 Assignment 1 | NPTEL | [Jan 2023]

## What are the applications of Python?

a. Image processing and graphic design applications
b. Enterprise and business applications development
c. Operating systems
d. All of the above
e. None of the above

d. All of the above
Enterprise/ Business Applications: Python’s features like scalability, extensibility, and readability make it the best choice for business applications.
Operating Systems Development: Many operating systems are using Python as a backbone. Generally, it is used along with C. Some of them are as follows: - Linux based Ubuntu’s Ubiquity Installer - Anaconda Installer of Red Hat Enterprises Image Processing and Graphic Designing: One can Use Python libraries like Opencv and Scikit Image for image analysis in the research field.

## Which of the following is not the correct variable name

a. Abc
b. Abd23
c. 32asd
d. Ab_cd_23

c. 32asd
- variable names cannot start with a number. Therefore, "32asd" is not a correct variable name and the correct answer is c. 32asd.

## Write the output of the following code. L = [1,2,3,4,5,6,7,8,9] print(L[::-1])

a. [1,2,3,4,5,6,7,8,9]
b. [1,2,3,4,5,9,8,7,6]
c. [9,8,7,6,5,4,3,2,1]
d. Error

c. [9,8,7,6,5,4,3,2,1]
In the code, L is a list containing the integers from 1 to 9. The slice L[::-1] is used to reverse the list. The slice L[start:stop:step] will start from the start index (defaults to 0), go up to the stop index (defaults to the end of the list) and step step indices at a time (defaults to 1). The step of -1 means to step backwards through the list, so the list is reversed.

## Predict the output of the following code: L = [[1,2,3],[0,4,5],[0,0,6]]

```for i in range (3):
for j in range(2,i-1,-1)
print(L(iJ(j], end="")```
a. 3,2,1,5,4,6
b. 3 2 1 5 4 6
c. 0,0,0,0,0,0
d. 0 0 0 0 0 0

b. 3 2 1 5 4 6
The outer for loop for i in range (3) iterates over the rows of the matrix, L, and the inner for loop for j in range(2,i-1,-1) iterates over the columns of the matrix in reverse order.

## Find all the error(s)in the following code:

```n=10
l = []
for i in range(10,n+10):
l.append(i**i)

for i in range (10,0,-1):
print(l[i])
```
a. Index out of range
b. Syntax error
c. Variable not defined
d. 'int' object does not support item assignment

b. Syntax error [IndentationError is a kind of SyntaxError]

IF we reomve indentationError then this code will give a. Index out of range
- This code will result in an IndexError because the list index is out of range. When i is 10 in the second loop, l[i] will be trying to access the 11th element of the list l, which does not exist. To avoid this, you can change the index in the print statement to l[i-1] to access elements that exist in the list.

## What is the output of the following code:

```def add_items(x,y):
x+=[1,2]
y+=(3,4)
l = 
t = (5,)
print(l,end="")
print(t)
```

a. [0,1,2] (5,3,4)
b. [0,1,2] (5,)
c.  (5,3,4)
d.  (5,)

b. [0,1,2] (5,)
This code defines a function add_items(x, y) which takes two arguments x and y. Inside the function, x is modified by adding the list [1,2] to it using the + operator, which results in x being concatenated. On the other hand, y is a tuple and tuples are immutable.

## What is the correct syntax for defining a function in Python?

a. def function name():
b. function function_name():
c. function_name():
d. def function_name:

The syntax for defining a function in Python is as follows: def function_name(arguments)
- a. def function name(): is incorrect as function name() contains space.
b. function function_name(): is incorrect
c. function_name(): is incorrect def keyword is required to define function.
d. def function_name: is incorrect as parentheses'()' is required.

## What is the purpose of the continue statement in a for loop?

a. To skip the rest of the current iteration and move on to the next one
b. To terminate the loop and exit the loop block
c. To return to the top of the loop and start a new iteration
d. To skip the current iteration and move on to the next one, but only if a certain condition is met

d. To skip the current iteration and move on to the next one, but only if a certain condition is met
The purpose of the continue statement in a for loop in Python is to skip the rest of the current iteration and move on to the next one. In other words, if the continue statement is encountered within the body of a loop, the program will immediately stop executing the rest of the statements in the current iteration and proceed to the next iteration. This allows for more control over the flow of the loop and can be useful for skipping certain iterations based on certain conditions. However, it should be used with caution, as it can sometimes make code more complex and harder to understand.

## How do you check if a number is even in Python?

a. if number % 2 == 0
b. if number.is_even()
c. if number % 2 is 0
d. if number.even()

a. if number % 2 == 0
If a number is divisible by two, it is even. When we divide a number by 2, we use the remainder operator% to calculate the remainder. The number is odd if the remainder is not zero.

## What should be the value of _ to print all numbers from 0-10?

```for i in range( _ ):
print(i)
```
a. 11
b. 9
c. 10
d. None of the above

a. 11

for i in range(11):
print(i)
Output
1 2 3 4 5 6 7 8 9 10

# The Joy of Computing using Python Week 2 : Programming Assignment | NPTEL | [Jan 2023]

## Week 2: Programming Assignment 1:Given an integer as an input, write a program to print that integer.

```print(num,end="")
```

## Week 2: Programming Assignment 2:Given two integers as an input, write a program to print the sum of those two integers

```print(num1+num2,end="")
```

## Week 2: Programming Assignment 3:You are given a number as an input. You have to display all the numbers from 0 till x.

```for a in range(num+1):
if a==num:
print(a,end="")
else:
print(a)
```

Disclaimer:
"This page contains multiple choice questions (MCQs) related to The Joy of Computing using Python . The answers to these questions are provided for educational and informational purposes only.These answers are provided only for the purpose to help students to take references. This website does not claim any surety of 100% correct answers. So, this website urges you to complete your assignment yourself."