PYTHON BASICS
ASSIGNMENT
NAME HARINI S V
EMAIL harini102405@[Link]
1. Using Python script as a calculator
Create the variables n, r, p and assign them values 10, 5, and 100 respectively.
Then evaluate the following expression in the Python console:
A=p(1+r/100)nA = p (1 + r/100)^nA=p(1+r/100)n
Answer: b. 162.89 (≈ 162.8894627)
2. In 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."
Answer: a. print([Link](a,b))
3. In a given sample string, how do you print a double-quoted string inside a regular string
using the escape character?
Sample output:
It goes without saying, “Time is Money”, and none can deny it.
Answer: a. print("It goes without saying, \"Time is Money\", and none can deny it.")
4. What will be the output of the following code?
x = lambda a,b: a/b
x(10,3)
Answer: a. 3.3333333333
5. What 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")
Answer: c. Greater
6. What will be the output of the following code?
import os
import numpy as np
my_list1 = [2,7,3,5,4,6]
print(my_list1)
arr_1 = [Link](my_list1, dtype = int)
print(arr_1)
Answer: c. NameError: name 'numpy' is not defined
7. Create a string called ‘string’ with the value as "Machine Learning".
Which code(s) is/are appropriate to slice the substring “Learn”?
Answer: d. string[slice(8,13,1)]
8. Create a sequence of numbers from 10 to 25 and increment by 4.
What is the index of the value 18?
Answer: b. 2
9. Which of the following is true with respect to the below codes?
num1 = 5**4
num2 = pow(5,4)
print(num1, num2)
Answer: a. num1 = num2
10. A Python NameError exception is raised when:
Answer: a. Trying to access a variable which has not been defined
11. What type of exception will be raised for the code given below?
x = "string"
int(x)
Answer: c. ValueError
12. A FileNotFoundError exception is raised by operating system errors when:
Answer: b. A file or directory is requested but does not exist in the working directory
13. Consider a variable Z. The value of Z is "ID-5632". Data type of Z is:
Answer: b. Character
14. Which of the following variable(s) are character data type?
Answer: d. All of the above
15. Choose the symbol/s that does not have the ability to convert any values to string?
Answer: d. #
16. Create a dictionary ‘Country’ that maps the following countries to their capitals
respectively:
Count Ind Chin Japa Qat
France
ry ia a n ar
Del Beiji Tok Doh Marseill
State
hi ng yo a es
Find 2 commands to replace “Marseilles” with “Paris”.
17. Create 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.
Answer: d. tuple_1[3] = 45
18. How many elements in the following data structure?
S = {1,2,3,4,4,4,5,6}
Answer: 6
19. Write a function which finds all pythagorean triplets of triangles whose sides are no
greater than a natural number N.