Python Basics Assignment
100 respectively. Then evaluate the following expression in the Python console 𝐴 = 𝑝 (1 + 𝑟/ 100)n
Q1. Using Python script as a calculator Create the variables n, r, p and assign them values 10, 5, and
a. 100
b. 162.89
c. 189
d. None of the above
Solution :
n,r,p =10,5,100
A = A = (p*(1+r/100)**n)
A= 162.8894626777442
[Link] a given string format operation, how will you print the given string.
A = 10 B = 20 Str = "There are {} students in the class, with {} who play at least one sport."
a. print([Link](a,b))
b. print(string+a+b)
c. print([Link](b,a))
d. None of the above
Solution : C
[Link] a given sample string, How do you print a double quoted string in between a regular string
using the escape character? Sample output = It goes without saying, "Time is Money", and none can
deny it.
[Link]("It goes without saying,\"Time is Money\", and none can deny it.")
[Link]("It goes without saying,\Time is Money\, and none can deny it.")
[Link]("It goes without saying" + "Time is Money" + "and none can deny it.")
d. None of the above
Solution : A
[Link] will be the output of the following code?
x = lambda a,b: a//b x(10,3)
a. 3.3333333333
b. 3
c. 30
d. 1000
Solution : B
[Link] will be the output of the following code?
A = 10 B = 12 print("Smaller") if A == B else print("Greater") if A < B else print("True")
a. True
b. Smaller
c. Greater
d. None of the above
Solution : C
[Link] will be the output of the following code?
a. [2 7 3 5 4 6]
b. TypeError
[Link]: name 'numpy' is not defined
d. None of the above
Solution : C
[Link] a string called'string' with the value as "Machine Learning". Which code(s) is/are
appropriate to slice the substring "Learn"?
a. string[slice(13,8,1)]
b. string[slice(1,8,1)]
c. string[8:14]
[Link][slice(8,13,1)]
Solution : D
[Link] a sequence of numbers from 10 to 25 and increment by 4. What is the index of the value
18?
a. 3
b.2
c. 0
d. 1
Solution : B
[Link] of the following is true with respect to the below codes?
a. num1 = num2
b.num1 ≠ num2
c.num1 < num2
d.num1 > num2
Solution : A
Q10.A Python NameError exception is raised when: -
a. Trying to access a variable which has not been defined
[Link] to access a key in a dictionary that does not exist
[Link] a column with misspelled column name
d. Accessing the function from a module that has not been imported
Solution : A
[Link] type of exception will be raised for the code given below?
a. NameError
b. KeyError
c. ValueError
d. AttributeError
Solution : C
[Link] is raised by operating system errors when: -
a. Trying to create a file or directory which already exists
b. A file or directory is requested but does not exist in the working directory
c. Trying to run an operation without the adequate access rights
d. A directory operation, [Link]() is requested on something which is not a directory
Solution : B
[Link] a variable Z. The value of Z is "ID-5632". Data type of Z is: -
a. Complex
[Link] (string Character)
[Link]
[Link]
Solution : B
[Link] of the following variable(s) are character data type?
a.K= "4"
b.J= "Welcome"
c.L= "?"
[Link] of the above
Solution : D
[Link] the symbol/s that does not have the ability to convert any values to string?
a. ( )
b." "
c. {}
d. #
Solution : D
[Link] a dictionary 'Country' that maps the following countries to their capitals respectively:
Country India China Japan Qatar France State Delhi Beijing Tokyo Doha Marseilles
Find 2 commands to replace "Marseilles" with"Paris"is:
Solution:
1. [Link]({'France':'Paris'})
2. Country["France"]="Paris"
[Link] the tuples given below tuple_1 = (1,5,6,7,8) tuple_2 = (8,9,4) Identify which of the
following code does not work on a tuple.
a. sum(tuple_1)
b. len(tuple_2)
c. tuple_2 + tuple_1
d.tuple_1[3] = 45
Solution : D
Q18. How many elements in the following data structure?
S={1,2,3,4,4,4,5,6}
Solution : Elements- 6
[Link] a function which finds all pythagorean triplets of triangles whose sides are no greater than
a natural number N.
Solution :
Def Pythagorean_triplets(n):
Triplets = []
For i in range (1, n+1):
For j in range(i, n+1):
For k in range(j, n+1):
If i**2 + j**2:
[Link]((I, j, k))
Return triplets
Pythagorean_triplets(10)