0% found this document useful (0 votes)
4 views

Week 2

Uploaded by

riyaranirr2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Week 2

Uploaded by

riyaranirr2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

The Joy of computing Using Python

Name: Rupesh Kumar


PMRF ID: 1403220

1. At any given point during a program's execution, what value is held within a
variable?
a. The value it was initially assigned when it was created (Only if the value of the
variable has not been modified within the program).
b. The sum of all values that have ever been assigned to it (Incorrect).
c. The most recent value that was assigned to it (Yes, it will hold for any
modifications to the variable a during the program execution).
d. The average of all values that have ever been assigned to it.
Answer: Option c
Variables are used to store some values in programming languages. The values can be a
number or a string.
a = 10
...
a = a+1
print(a)
The variable a will hold the value 10, at any stage in the program, until and unless it’s value
has been modified.

2. What will be the output of the following Python code snippet?

Which of the following represents the correct output if the user inputs "25"?
a. The value is: 25
b. 25
c. The value is:
d. None of the above
Answer: Option a
The value is: 25

+ acts as concatenation operator for strings in python.


a = “Hello”
b = “World”
c=a+b
This means, c is the result of concatenation of the strings stored in variables a and b.
The value of c beomes: “HelloWorld”

3. What are the possible loop values that can be specified in the range argument of a
for loop in Python, and why are these values used?
a. Integers, representing the starting and ending points of the loop, allowing
iteration through a specified range of numbers.
b. Floating-point numbers, enabling iteration through decimal ranges with
precise steps.
c. Strings, facilitating iteration through characters in a specified string
d. Tuples, allowing iteration through multiple sequences simultaneously.
Answer option a
Loops are statements in programming languages used to repeat a set of instructions
multiple times.
Write a program to print “Hello World” 10 times.
for i in range(10):
print(“Hello World”)
Print “Hello World” for 1.5 times: it does not make any sense.

4. What values can be used in the argument of a while loop in Python, and why are
these values used?
a. Integers, representing the starting and ending points of the loop, allowing
iteration through a specified range of numbers
b. Boolean expressions or conditions, enabling the loop to execute until the
condition becomes False.
c. Floating-point numbers, allowing iteration through decimal ranges with
precise steps.
d. Strings, facilitating iteration through characters in a specified string.
Answer: Option b. The argument inside a while loop expects a boolean expression or
condition that restricts the loop to execute until the condition becomes false.
while(some condition or boolean):
do something
Update the iterator so that the conditon becomes false after some finite number of
iterations.

5. What types of conditional entries can be used in a Python if statement, and why are
these entries used?
a. Integers, representing specific numeric values to check against a variable,
ensuring a match for equality
b. Boolean expressions or conditions, evaluating to True or False and
determining the path of execution in the code.
c. Strings, enabling comparison of text values for exact matches in the
condition
d. Floating-point numbers, allow range-based conditions to check if a variable
falls within a specific numerical range.

Answer: Option b

if(some condition is true):


do something
6. Consider the following code snippet:

What does the code do?


a. Takes an input n, computes the multiplication table of n from 1 to 10, and
prints each multiplication.
b. Takes an input n, computes the multiplication table of n from 1 to 11, and
prints the final product.
c. Takes an input n, computes the multiplication table of n from 1 to 10, and
prints the final product.
d. Takes an input n, initializes i to 1, and prints the multiplication of n and i
without any loop.

Answer: Option a
n x i = n*i

n x 1 = n*1

n x 2 = n*2

7. Consider the following code snippet:

What does the code do?


a. Takes an input number, computes the multiplication table of numbers from
1 to 10, and prints each multiplication
b. Takes an input number, computes the multiplication table of numbers from
1 to 11, and prints the final product.
c. Takes an input number, computes the multiplication table of numbers from
1 to 10, and prints the final product.
d. Takes an input number, initializes i to 1, and prints the multiplication of
number and i without any loop.

Answer: Option a

8. Consider the following code snippet:

What does the code do?


a. Takes an input num, computes the factorial of num, and prints the factorial
value.
b. Takes an input num, computes the product of numbers from 1 to num, and
prints the final product.
c. Takes an input num, computes the sum of numbers from 1 to num, and
prints the final sum.
d. Takes an input num, initializes the result to 1, and prints the value of the
result without any factorial computation.

Answer: Option a, b

while counter <= num

num = 5 counter = 1 result = 1

result = 1 * 1 counter = 2

num = 5 counter = 2 result = 1

result = 1 * 2 counter 2 + 1

num = 5 counter = 3 result = 2

result = 2 * 3 counter 3 + 1

num = 5 counter = 4 result = 6

result = 6 * 4 counter = 4 + 1

num = 5 counter = 5 result = 24

result 24 * 5 counter 5 + 1

num = 5 counter = 6 result = 120

Shorthand operators

a=2

a += 1 # a = a + 1

a -= 1 # a = a - 1

a *= 1 # a = a * 1

a /= 1 # a = a / 1
9. An Integrated Development Environment (IDE) is a software application that
provides comprehensive facilities to computer programmers for software
development. Which of the following are IDEs?
a. PyCharm
b. Spyder
c. Visual Studio Code (VS Code)
d. C# (pronounced "C sharp")

Answer: Option a, b, c

10. A Python distribution is a software bundle, which contains a Python interpreter and
the Python standard library. What is Anaconda being discussed in lectures?
a. A species of snake
b. A type of programming language
c. An integrated development environment (IDE)
d. A Python distribution for scientific computing and data science

Answer: Option d

Programming Problems

Write a program that takes two numbers as input and tells whether they are equal or not.

num1 = int(input(“Kindly enter the first number”))

num2 = int(input(“Kindly enter the second number”))

if (num1 == num2):

print(“Yes, the numbers are equal”)

if (num1 != num2):

print (“No, the numbers are not equal”)

Write a program that takes user age as input and tells whether the user is eligible to vote or
not.

Voting eligibility criteria: The user’s age should be greater than or equal to 18.

Write a program in Python to check if the given year as input is a leap year or not.
Write a program to accept XY coordinates of a point and tell the quadrant in which the point
lies.

Write a program to determine eligibility for admission to a professional course based


on the following criteria:
Eligibility Criteria : Marks in Maths >=65 and Marks in Physics >=55 and Marks in
Chemistry>=50 and Total in all three subject >=190 or Total in Maths and Physics >=140.

Write a program to print all the even numbers between 1 to 100.

You might also like