Python Data Types and Operations Quiz
Python Data Types and Operations Quiz
a. Two integer objects, two references C. One integer object, two references ✓
b. One integer object, one reference D. Two integer objects, one reference
Q4. Which Python built-in function returns the unique number assigned to an object?
Q5. The operator used to check if both the operands reference the same object memory, is the ..........
operator.
a. In b. is ✓ c. id d. ==
Q6. For two objects x and y, the expression x is y will yield True, if and only if
Q13. In Python, a variable may be assigned a value of one type, and then later assigned a value of a
different type. This concept is known as .......... .
Q14. Is it safe to directly use the == operator to determine whether objects of type float are equal ?
a. Yes b. No ✓ c. Yes, if the values are < 100 d. Yes, if the values are > 100
a = 8.6
b=2
print ( a//b )
Q18. What will be the value of y after following code fragment is executed ?
x = 10.0
a. 0.000001 ✓ d. 'None' ✓ g. 0
b. [ ] e. False h. True ✓
c. 1 ✓ f. 33 ✓ i. None
import math
x = 100
a. True b. 1 c. 10 d. 10.0 ✓
a. not b. % c. and ✓ d. + e. **
Q22. What is the value of the expression 10 + 3 ** 3 * 2?
a. 28 b. 739 c. 829 d. 64 ✓
Q23. To increase the value of x five times using an augmented assignment operator, the correct
expression will be
a. x += 5 b. x *= 5 ✓ c. x = x ** 5 d. none of these
a. 0 b. 1 c. 10 ✓ d. 1.0
a. 5 ✓ b. 1 c. 10 d. 0
Question 26
1. 5
2. 1
3. 10 ✓
4. 0
Question 27
1. 5
2. 1
3. 10 ✓
4. 0
Question 28
1. 0
2. 1
3. 10 ✓
4. 1.0
Question 29
What will be the result of the expression 'a' or " (" is an empty string) ?
1. 'a' ✓
2. "
3. 1
4. 0
Question 30
What will be the result of the expression 'a' and " (" is an empty string) ?
1. 'a'
2. " ✓
3. 1
4. 0
Question 31
1. 'a' ✓
2. "
3. 'x'
4. 1
Question 32
1. 'a'
2. ''
3. 'x' ✓
4. 1
Question 33
1. 'a'
2. ''
3. 'None' ✓
4. 1
Question 34
1. 'a' ✓
2. ''
3. 'None'
4. 1
Question 35
1. false
2. False ✓
3. 'false'
4. 'False'
Question 36
1. false
2. False
3. 'false' ✓
4. 'False'
Question 1
Question 2
Two forms of floating-point numbers are: fractional form and exponent notation.
Question 3
Question 4
Question 5
Question 6
Question 7
Question 8
Question 9
The id of an object gives the memory location of the object.
Question 10
To check if two objects reference the same memory address, is operator is used.
Question 11
Question 12
To generate a random floating number in the range 0 to 100, random() or [Link]() function is
used.
Question 13
Question 14
To generate a random number in a sequence of values where two values have a difference a step
value, randrange() or [Link]() function is used.
Question 15
True/False Questions
Question 1
Question 2
Question 3
Question 4
Question 5
Question 6
Question 8
Question 9
Question 10
Question 11
Question 12
Question 13
Question 14
Question 15
Question 16
Question 17
Question 18
Two floating point numbers should not be compared for equality using ==
True
Question 19
In implicit conversion, all operands' data types are converted to the datatype of the largest operand.
True
Question 20
Explicit type conversion involves the use of a function to convert datatype of a value.
True
Question 1
Answer
Data types are used to identify the type of data a memory location can hold and the associated operations
of handling it. The data that we deal with in our programs can be of many types like character, integer, real
number, string, boolean, etc. hence programming languages including Python provide ways and facilities to
handle all these different types of data through data types. The data types define the capabilities to handle
a specific type of data such as memory space it allocates to hold a certain type of data and the range of
values supported for a given data type, etc.
Question 2
Answer
1. Integers (signed)
2. Booleans
Question 3
How are these numbers different from one another (with respect to Python)? 33, 33.0, 33j, 33 + j
Answer
The number 33 is an integer whereas 33.0 is a floating-point number. 33j represent the imaginary part of a
complex number. 33 + j is a complex number.
Question 4
The complex numbers have two parts : real and imaginary. In which data type are real and imaginary parts
represented ?
Answer
In Python, the real and imaginary parts of a complex number are represented as floating-point numbers.
Question 5
How many string types does Python support? How are they different from one another?
Answer
Python supports two types of strings — Single-line strings and Multi-line strings. Single line strings are
enclosed in single or double quotes and terminate in one line. Multi-line strings store multiple lines of text
and are enclosed in triple quotes.
Question 6
str1 = '''Hell
o'''
str2 = '''Hell\
o'''
Answer
True
len(str1) is 6 due to the EOL character. len(str2) is 5 as backslash (\) character is not counted in the length of
string. As len(str1) is greater than len(str2) so the output is True.
Question 7
What are Immutable and Mutable types in Python? List immutable and mutable types of Python.
Answer
Mutable types are those whose values can be changed in place whereas Immutable types are those that
can never change their value in place.
1. Lists
2. Dictionaries
3. Sets
1. Integers
2. Floating-Point numbers
3. Booleans
4. Strings
5. Tuples
Question 8
What are three internal key-attributes of a value-variable in Python ? Explain with example.
Answer
1. Type
2. Value
3. Id
a=4
The type of a is int which can be found with the built-in function type() like this:
type(a).
Value can be found using the built-in function print() like this:
print(a)
Id is the memory location of the object which can be determined using built-in function id() like this:
id(a)
Question 9
Is it true that if two objects return True for is operator, they will also return True for == operator?
Answer
Yes, if is operator returns true, it implicitly means that the equality operator will also return True. is
operator returning true implies that both the variables point to the same object and hence == operator
must return True.
Question 10
1. 20 and 20.0
2. 20 and int(20)
Answer
1. The type of 20 is int whereas the type of 20.0 is float so they are two different objects. Both have
the same value of 20. So, as values are same equality (==) operator return True but as objects are
different is operator returns False.
2. The value and type of both 20 and int(20) are the same and both point to the same object so both
equality (==) and is operator returns True.
3. For str(20) and str(20.0), both equality (==) and is operator returns False as their values are different
and they point to two different objects.
4. For 'a' and "a", both equality (==) and is operator returns True as their values are same and they
point to the same object.
Question 11
Answer
In Python, an atom is something that has a value. Identifiers, literals, strings, lists, tuples, sets, dictionaries,
etc. are all atoms. An expression in Python, is any valid combination of operators and atoms. It is composed
of one or more operations.
Question 12
What is the difference between implicit type conversion and explicit type conversion?
Answer
Example: Example:
a, b = 5, 25.5 a, b = 5, 25.5
c=a+b c = int(a + b)
Question 13
Two objects (say a and b) when compared using == ,return True. But Python gives False when compared
using is operator. Why? (i.e., a == b is True but why is a is b False?)
Answer
As equality (==) operator returns True, it means that a and b have the same value but as is operator returns
False, it means that variables a and b point to different objects in memory. For example, consider the below
Python statements:
>>> a = 'abc'
>>> b = input("Enter a string: ")
>>> a == b
True
>>> a is b
False
Here, both a and b have the same value 'abc' but they point to different objects.
Question 14
(a) str1[0]
(b) str1[1]
(c) str1[-5]
(d) str1[-4]
(e) str1[5]
Answer
(a) H
(b) e
(c) H
(d) e
Explanation
H0−5e1−4l2−3l3−2o4−1−5H0−4e1−3l2−2l3−1o4
Question 15
If you give the following for str1 = "Hello", why does Python report error?
str1[2] = 'p'
Answer
Python reports error because strings are immutable and hence item assignment is not supported.
Question 16
(a) type (6 + 3)
(d) type (6 / 3)
(e) type (6 // 3)
(f) type (6 % 3)
Answer
⇒ int + int
(a) type (6 + 3)
⇒ int
So the result is int.
⇒ int - int
(b) type (6 -3)
⇒ int
So the result is int.
⇒ int * int
(c) type (6 * 3)
⇒ int
So the result is int.
⇒ int / int
(d) type (6 / 3)
⇒ float
So the result is float.
⇒ int // int
(e) type (6 // 3)
⇒ int
So the result is int.
⇒ int % int
(f) type (6 % 3)
⇒ int
So the result is int.
Question 17
Answer
Augmented assignment operators combine the impact of an arithmetic operator with an assignment
operator. For example, to add the value of b to the value of a and assign the result back to a then instead of
writing:
a=a+b
we can write
a += b.
Augmented assignment operators are useful as they provide a shorthand way by combining the arithmetic
and assignment operators.
Question 18
Answer
In the first expression, 555 is of int type whereas in the second expression 555.0 is of float type.
Question 19
Given three Boolean variables a, b, c as : a = False, b = True, c = False. Evaluate the following Boolean
expressions:
(a) b and c
(b) b or c
Answer
⇒ False
⇒ True or False
(b) b or c
⇒ True
Question 20
(a) 1 < x
(b) x >= 4
(c) x == 3
(d) x == 3.0
(h) 4/2 == 2
Answer
(a) True
(b) False
(c) True
(d) True
(e) True
(f) False
(g) True
(h) True
(i) False
Question 21
Question 22
int() converts its argument into an integer. As 'a' is a letter, it cannot be converted into a valid integer hence
int('a') produces an error.
Question 23
int('a') produces error but following expression having int('a') in it, does not return error. Why?
len('a') + 2 or int('a')
Answer
The or operator tests the second operand only if the first operand is false otherwise it ignores it. In the
expression, the first operand of or operator is len('a') + 2. It evaluates to 3 and as it is a non-zero value
hence it is True for or operator. As first operand of or operator is True so it doesn't evaluate its second
argument int('a') and no error is returned.
Question 24
Write expression to convert the values 17, len('ab') to (i) integer (ii) str (iii) boolean values
Answer
Question 25
(i) 22 / 17 = 37 / 47 + 88 /83
(ii) len('375')**2
Answer
(i) It produces an error as LHS value in this case is an expression that evaluates to a literal whereas LHS
value should be a variable.
⇒ 3 ** 2 [∵ len('375') = 3]
(ii) len('375')**2
⇒9 [∵ 3 * 3 = 9]
Question 26
Answer
⇒0
(i) 22.0/7.0 - 22/7
As values of 22.0/7.0 and 22/7 are equal, subtracting them will give the result as 0.0.
⇒ 3.142857142857143 - 3
(ii) 22.0/7.0 - int(22.0/7.0)
⇒ 0.142857142857143
⇒ 0.0
(iii) 22/7 - int (22.0)/7
int (22.0) gives 22 so the expression becomes 22/7 - 22/7 which results in 0.0
Question 27
Answer
(i) This produces an error as false is an invalid literal in Python. It should be False. Had the expression being
False and None, the return value will be False.
(ii) This logical expression evaluates to 0. As first operand of and operator is false so it will return the first
operand itself.
(iii) This logical expression evaluates to None. As first operand of and operator is True so it will return the
second operand.
(iv) This logical expression evaluates to None. As first operand of and operator is false so it will return the
first operand itself.
Question 28
⇒ None
⇒ 1 or None or 'b'
(b) 1 or None and 'a' or 'b'
⇒ 1 or 'b'
⇒1
⇒ False
(c) False and 23
⇒ False
(d) 23 and False
⇒ not True
⇒ False
Question 29
Evaluate the following for each expression that is successfully evaluated, determine its value and type for
unsuccessful expression, state the reason.
(d) 2 * (2 * (len("01")))
Answer
⇒ 5 == 25/5 or 20/10
(a) len("hello") == 25/5 or 20/10
⇒ 5 == 5 or 2
⇒ True or 2
⇒ True
⇒ True or 50/(5 - (3 + 2)) [∵ first operand is True, second operand is not evaluated so no division by 0
(b) 3 < 5 or 50/(5 - (3 + 2))
⇒ True
error happens]
⇒ 50/(5 - 5) or 3 < 5
(c) 50/(5 - (3 + 2)) or 3 < 5
⇒ 50/0 or 3 < 5
⇒ Division by Zero Error
⇒ 2 * (2 * 2)
(d) 2 * (2 * (len("01")))
⇒2*4
⇒8
Question 30
Write an expression that uses exactly 3 arithmetic operators with integer literals and produces result as 99.
Answer
9 * 10 + 21 % 12
Question 31
Add parentheses to the following expression to make the order of evaluation more clear.
Answer
Question 32
A program runs to completion but gives an incorrect result. What type of error would have caused it?
Answer
Logical errors can make a program run till completion but give incorrect result.
Question 33
In Python, strings are immutable while lists are mutable. What is the difference?
Answer
In Python, strings are immutable means that individual letter assignment for strings is not allowed. For
example:
name='hello'
name[0] = 'p'
The above Python code will cause an error as we are trying to assign some value to an individual letter of a
string.
Lists are mutable in Python means that we can assign values to individual elements of a list. For example:
a = [1, 2, 3, 4, 5]
a[0] = 10
The above Python code will work correctly without any errors as Lists are mutable in Python.
Question 34
How does the // operator differ from the / operator? Give an example of where // would be needed.
Answer
The Division operator (/) divides its first operand by second operand and always returns the result as a float
value whereas Floor Division operator (//) divides its first operand by second operand and truncates the
fractional part of the result and returns it as an int value. Floor Division operator is useful in situations
where we only need the integer part of the division operation result. For example, to determine how many
minutes are there in some given number of seconds:
secs = 2565
mins = 2565 // 60
Question 35
MidAir Airlines will only allow carry-on bags that are no more than 22 inches long, 14 inches wide, and 9
inches deep. Assuming that variables named length, width, and depth have already been assigned values,
write an expression combining the three that evaluates to True if bag fits within those limits, and False
otherwise.
Answer
Question 36
What are main error types? Which types are most dangerous and why?
Answer
3. Logical Errors
Logical Errors are the most dangerous as they are hardest to prevent, find and fix.
Question 37
(a) Compile-time errors are usually easier to detect and to correct than run-time errors.
Answer
Question 38
Answer
Syntax errors occurs when the rules of Semantic errors occur when the
the programming language are violated. statement are not meaningful.
Example: Example:
x = false x*y=z
Question 39
Differentiate between a syntax error and a logical error in a python program. When is each type of error
likely to be found?
Answer
Syntax errors are found at compile type whereas Logical errors are found when the program starts
executing.
Question 40
Answer
An Error is a bug in the code that causes irregular output or stops the program from executing whereas an
Exception is an irregular unexpected situation occurring during execution on which programmer has no
control.
Question 1
What is the result produced by (i) bool (0) (ii) bool (str(0))? Justify the outcome.
Answer
The result is True as str(0) converts 0 to string "0". As it becomes a non-empty string so its truth value is
truetval
Question 2
What will be the output, if input for both the statements is 5 + 4/2.
Answer
Output of first statement is False as '5 + 4/2' is entered as a string so it cannot be equal to the number 6.
The second statement gives an error as int() function cannot convert the string '5 + 4/2' to a valid integer.
Question 3
Following Python code has an expression with all integer values. Why is the result in floating point form?
a, b, c = 2, 3, 6
d = a + b * c/b
print(d)
Answer
The output of the above Python code is 8.0. Division operator is present in the expression. The result of
Division operator is of float type. Due to implicit conversion, other operand in this expression are also
converted to float type and hence the final result is in floating point form.
Question 4a
a = va = 3
b = va = 3
print (a, b)
Answer
33
Question 4b
a=3
b = 3.0
print (a == b)
print (a is b)
Answer
True
False
As values of a and b are equal so equality operator returns True. a is of int type and b is of float type so a
and b are different objects so a is b returns False.
Question 5a
What will be output produced by following code? State reason for this output.
a, b, c = 1, 1, 2
d=a+b
e = 1.0
f = 1.0
g = 2.0
h=e+f
print(c == d)
print(c is d)
print(g == h)
print(g is h)
Answer
Output
True
True
True
False
Explanation
Value of d becomes 2 and as values of c and d are equal so print(c == d) prints True.
Question 5b
What will be output produced by following code? State reason for this output.
a=5-4-3
b = 3**2**3
print(a)
print(b)
Answer
Output
-2
6561
Explanation
⇒a=1-3
a=5-4-3
⇒ a = -2
The exponentiation operator (**) has associativity from right to left so:
⇒ b = 3**8
b = 3**2**3
⇒ b = 6561
Question 5c
What will be output produced by following code? State reason for this output.
a, b, c = 1, 1, 1
d = 0.3
e=a+b+c-d
f=a+b+c == d
print(e)
print(f)
Answer
Output
2.7
False
Explanation
⇒ e = 1+1+1-0.3
e = a+b+c-d
⇒ e = 3-0.3
⇒ e = 2.7
As 0.3 is float so implicit conversion converts 3 also to float and result of the expression is of float type.
⇒ f = 1 + 1 + 1 == 0.3
f = a + b + c == d
⇒ f = 3 == 0.3
⇒ f = False
Question 6a
a = 12
b = 7.4
c=1
a -= b
print(a, b)
a *= 2 + c
print(a)
b += a * c
print(b)
Answer
Output
4.6 7.4
13.799999999999999
21.2
Explanation
⇒a=a-b
a -= b
⇒ a = 12 - 7.4
⇒ a = 4.6
⇒ a = 4.6 * (2 + c)
a *= 2 + c
⇒ a = 4.6 * (2 + 1)
⇒ a = 4.6 * 3
⇒ a = 13.799999999999999
⇒ b = b + (a * c)
b += a * c
⇒ b = 7.4 + (13.799999999999999 * 1)
⇒ b = 7.4 + 13.799999999999999
⇒ b = 21.2
Question 6b
x, y = 4, 8
z = x/y*y
print(z)
Answer
Output
4.0
Explanation
⇒ z = 4/8*8
z = x/y*y
⇒ z = 0.5*8
⇒ z = 4.0
Question 7
Make change in the expression for z of previous question so that the output produced is zero. You cannot
change the operators and order of variables. (Hint. Use a function around a sub-expression)
Answer
x, y = 4, 8
z = int(x/y)*y
print(z)
Question 8
Answer
Question 9
a = input()
b = int(input())
c=a+b
print(c)
When the program is run, the user first enters 10 and then 5, it gives an error. Find the error, its reason and
correct it
Answer
It occurs because a is of type string but b is of type int. We are trying to add together a string operand and
an int operand using addition operator. This is not allowed in Python hence this error occurs.
a = int(input())
Question 10
print(a + b)
If the user runs the program and enters 11 for a and 9 for b then what will the above code display?
Answer
Output
119
Explanation
input() function accepts user input as string type. The data type of a and b is string not int so addition
operator concatenates them to print 119 instead of 20.
Question 11
Find out the error and the reason for the error in the following code. Also, give the corrected code.
a, b = "5.0", "10.0"
x = float(a/b)
print(x)
Answer
a and b are defined as strings not float or int. Division operator doesn't support strings as its operand so we
get the error — unsupported operand type(s) for /: "str" and "str".
a, b = 5.0, 10.0
x = float(a/b)
print(x)
Question 12
Consider the following program. It is supposed to compute the hypotenuse of a right triangle after the user
enters the lengths of the other two sides.
h = sqrt(a * a + b * b)
When this program is run, the following output is generated (note that input entered by the user is shown
in bold):
Answer
The error is coming because math module is not imported in the code. To resolve it, we should import the
math module using the import statement import math.
Question 13
Consider the following program. It is supposed to compute the hypotenuse of a right triangle after the user
enters the lengths of the other two sides.
h = sqrt(a * a + b * b)
After adding import math to the code given above, what other change(s) are required in the code to make
it fully work ?
Answer
After adding import math statement, we need to change the line h = sqrt(a * a + b * b) to h = [Link](a *
a + b * b). The corrected working code is below:
import math
h = [Link](a * a + b * b)
Question 14
Which of the following expressions will result in an error message being displayed when a program
containing it is run?
(a) 2.0/4
(c) 4 % 15
(d) int("5")/float("3")
(e) float("6"/"2")
Answer
(a) No Error
(b) No Error
(c) No Error
(d) No Error
(e) This will cause an error of unsupported operand types as using division operator on string types is not
allowed in Python.
Question 15a
Following expression does not report an error even if it has a sub-expression with 'divide by zero' problem:
3 or 10/0
What changes can you make to above expression so that Python reports this error?
Answer
Interchanging the operands of or operator as shown below will make Python report this error:
10/0 or 3
Question 15b
a, b = bool(0), bool(0.0)
c, d = str(0), str(0.0)
Answer
The above code will give an error as the line print (len(a), len(b)) is calling len function with bool arguments
which is not allowed in Python.
Q16.
Given a string s = "12345". Can you write an expression that gives sum of all the digits shown inside the
string s i.e., the program should be able to produce the result as 15 (1+2+3+4+5).
[Hint. Use indexes and convert to integer]
Answer
a = True
b=0<5
print (a == b)
print (a is b)
c = str (a)
d = str (b)
print (c == d)
print (c is d)
print (c == e)
print (c is e)
Answer
Output True
True
True
True
Enter :True
True
False
Explanation
3. print (a is b) gives True as a and b both being True point to the same object in memory.
4. Similarly print (c == d) and print (c is d) give True as c and d both are string and point to the same
object in memory.
5. The user input for e is True so e is of type string having value "True".
7. Even though the values of strings c and e are same, they point to different objects in memory
so print (c is e) gives False.
name = "HariT"
print (name)
name[2] = 'R'
print (name)
Answer The line name[2] = 'R' is trying to assign the letter 'R' to the second index of string name but
strings are immutable in Python and hence such item assignment for strings is not supported
in Python.
a = bool (0)
b = bool (1)
print (a == false)
print (b == true)
Answer false and true are invalid literals in Python. The correct boolean literals are False and True.
Answer In the line print (type (int("Hello"))), string "Hello" is given as an argument to int() but it
cannot be converted into a valid integer so it causes an error.
pi = 3.14
Answer In the line print (type (float("three point fourteen"))), string "three point fourteen" is given
as an argument to float() but it cannot be converted into a valid floating-point number so it
causes an error.
print ("Hello" + 2)
print ("Hello" * 2)
Answer The line print ("Hello" + 2) causes an error as addition operator (+) cannot concatenate a
string and an int.
print ("Hello"/2)
print ("Hello" / 2)
Answer Both the lines of this Python code will give an error as strings cannot be used with division
operator (/).
a = x or (y and z)
b = (x or y) and z
print(a, b)
Explanation
⇒ True
x, y = '5', 2
z=x+y
print(z)
Answer This code produces an error in the line z = x + y as operands of addition operator (+) are
and int, respectively which is not supported by Python.
s = 'Sipo'
s1 = s + '2'
s2 = s * 2
print(s1)
print(s2)
SipoSipo
print(result)
⇒ -6 < -6 or 4 ** 2 < 10
⇒ -6 < -6 or 16 < 10
⇒ False or False
⇒ False
Q20. Program is giving a weird result of "[Link].50.50.50..........". Correct it so that it produces the
correct result which is the probability value (input as 0.5) times 150.
print("Out of 150 tries, the odds are that only", (probability * 150), "will succeed.")
print("Out of 150 tries, the odds are that only", (probability * 150), "will succeed.")
import random
r = [Link](100, 999, 5)
r = [Link](100, 999, 5)
r = [Link](100, 999, 5)
print(r)
Which of the following are the possible outcomes of the above code ? Also, what can be the
maximum and minimum number generated by line 2 ?
(a) 655, 705, 220 (b) 380, 382, 505 (c) 100, 500, 999 (d) 345, 650, 110
import random
r = [Link](10, 100) - 10
r = [Link](10, 100) - 10
print(r)
Which of the following are the possible outcomes of the above code? Also, what can be the
maximum and minimum number generated by line 2?
import random
r = [Link]() * 10
r = random. random() * 10
r = [Link]() * 10
print(r)
Which of the following are the possible outcomes of the above code? Also, what can be the
maximum and minimum number generated by line 2 ?
(a) 0.5 1.6 9.8 (b) 10.0 1.0 0.0 (c) 0.0 5.6 8.7 (d) 0.0 7.9 10.0
import statistics as st
v = [7, 8, 8, 11, 7, 7]
m1 = [Link](v)
m2 = [Link](v)
m3 = [Link](v)
si = p * r * t / 100
Q2. Write a program to obtain temperatures of 7 days (Monday, Tuesday ... Sunday) and then display
average temperature of the week.
Q3. Write a program to obtain x, y, z from user and calculate expression : 4x4 + 3y3 + 9z + 6π
x = int(input("Enter x: "))
y = int(input("Enter y: "))
z = int(input("Enter z: "))
res = 4 * x ** 4 + 3 * y ** 3 + 9 * z + 6 * [Link]
Q4. Write a program that reads a number of seconds and prints it in form : mins and seconds, e.g., 200
seconds are printed as 3 mins and 20 seconds.
[Hint. use // and % to get minutes and seconds]
mins = totalSecs // 60
secs = totalSecs % 60
Q5. Write a program to take year as input and check if it is a leap year or not.
Solution y = int(input("Enter year to check: "))
Q6. Write a program to take two numbers and print if the first number is fully divisible by second
number or not.
Q7. Write a program to take a 2-digit number and then print the reversed number. That is, if the input
given is 25, the program should print 52.
y = x % 10 * 10 + x // 10
print("Reversed Number:", y)
Q8. Try writing program (similar to previous one) for three digit number i.e., if you input 123, the
program should print 321.
d1 = x % 10
x //= 10
d2 = x % 10
x //= 10
d3 = x % 10
y = d1 * 100 + d2 * 10 + d3
print("Reversed Number:", y)
Q9. Write a program to take two inputs for day, month and then calculate which day of the year, the
given date is. For simplicity, take 30 days for all months. For example, if you give input as: Day3,
Month2 then it should print "Day of the year : 33".
n = (m - 1) * 30 + d
Q10. Write a program that asks a user for a number of years, and then prints out the number of days,
hours, minutes, and seconds in that number of years.
d = y * 365
h = d * 24
m = h * 60
s = m * 60
print(d, "days")
print(h, "hours")
print(m, "minutes")
print(s, "seconds")
Q11. Write a program that inputs an age and print age after 10 years as shown below:
Q12. Write a program whose three sample runs are shown below:
Sample Run 1:
Random number between 0 and 5 (A) : 2
Random number between 0 and 5 (B) :5.
A to the power B = 32
Sample Run 2:
Random number between 0 and 5 (A) : 4
Random number between 0 and 5 (B) :3.
A to the power B = 64
Sample Run 3:
Random number between 0 and 5 (A) : 1
Random number between 0 and 5 (B) :1.
A to the power B = 1
a = [Link](0, 5)
b = [Link](0, 5)
c = a ** b
print("Random number between 0 and 5 (A) :", a)
Q13. Write a program that generates six random numbers in a sequence created with (start, stop, step).
Then print the mean, median and mode of the generated numbers.
import statistics
print("Generated Numbers:")
print(a, b, c, d, e, f)
seq = (a, b, c, d, e, f)
mean = [Link](seq)
median = [Link](seq)
mode = [Link](seq)
Q14. Write a program to generate 3 random integers between 100 and 999 which is divisible by 5.
a = [Link](100, 999, 5)
b = [Link](100, 999, 5)
c = [Link](100, 999, 5)
print("Generated Numbers:", a, b, c)
Q15. Write a program to generate 6 digit random secure OTP between 100000 to 999999.
print("OTP:", otp);
Q16. Write a program to generate 6 random numbers and then print their mean, median and mode.
import statistics
a = [Link]()
b = [Link]()
c = [Link]()
d = [Link]()
e = [Link]()
f = [Link]()
print("Generated Numbers:")
print(a, b, c, d, e, f)
seq = (a, b, c, d, e, f)
mean = [Link](seq)
median = [Link](seq)
mode = [Link](seq)
Q17. Write a program to find a side of a right angled triangle whose two sides and an angle is given.
c = [Link](a ** 2 + b ** 2)
print("Hypotenuse =", c)
Q18. Write a program to calculate the radius of a sphere whose area (4πr2) is given.
r = [Link](area / (4 * [Link]))
Q19. Write a program that inputs a string and then prints it equal to number of times its length, e.g.,
len = len(str)
print("Result", opStr)
Radius = 8 cm
Height = 15 cm
r=8
h = 15
v = [Link] * r * r * h
Q21. Write a program to calculate the area of an equilateral triangle. (area = (√3 / 4) * side * side).
Q22. Write a program to input the radius of a sphere and calculate its volume (V = 4/3πr 3)
v = 4 / 3 * [Link] * r ** 3
amt = p + si
amt = p * (1 + r / 100) ** t
Q25. Write a program to compute (a + b)3 using the formula a3 + b3 + 3a2b + 3ab2
b = int(input("Enter b: "))
res = a ** 3 + b ** 3 + 3 * a ** 2 * b + 3 * a * b ** 2