Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
74 views

Python Basics Assignment

The document contains examples of Python code and questions about Python concepts like data types, exceptions, functions and data structures. It provides solutions and explanations for coding problems involving strings, lists, tuples, dictionaries and built-in functions.

Uploaded by

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

Python Basics Assignment

The document contains examples of Python code and questions about Python concepts like data types, exceptions, functions and data structures. It provides solutions and explanations for coding problems involving strings, lists, tuples, dictionaries and built-in functions.

Uploaded by

sayantani 11
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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.
Solution
n = 10
r=5
p = 100

A = p * (1 + r / 100)**n

# Print the result


print(A)

Ans : B ) 162.89

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
Solution
A = 10
B = 20
string = "There are {} students in the class, with {} who play at least one sport."
print(string.format(A, B))

Ans : c. print(string.format(b, a))

3. In a given sample string, How do you print a double quoted string in between a regular
string using the escape character?
Solution
print("It goes without saying, \"Time is Money\", and none can deny it.")

Ans : 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)

Ans : B) 3

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")

Ans : c). Greater

6. What will be the output of the following code

Ans : A. [2 7 3 5 4 6]

7. Create 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] d. string[slice(8,13,1)]

Ans : This option is appropriate and will give you the substring "Learn" because it starts at
index 8 and goes up to, but not including, index 14.
c. string[8:14]

8. Create 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
Ans :
sequence = list(range(10, 26, 4))

index_of_18 = sequence.index(18)
print(index_of_18)

Ans : B ) 2

9. Which of the following is true with respect to the below codes?

A. num1 = num2

10. A Python NameError exception is raised when: - a. Trying to access a variable which has
not been defined b. Trying to access a key in a dictionary that does not exist c. Accessing a
column with misspelled column name d. Accessing the function from a module that has not
been imported

Ans : A . Trying to access a variable which has not been defined

11.What type of exception will be raised for the code given below?

Ans. ValueError

12.A FileNotFoundError exception is raised by operating system errors when: -

Ans :
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:

Ans : B )Character

14.Which of the following variable(s) are character data type?

Ans : D ) All of the above

15.Choose the symbol/s that does not have the ability to convert any values to string
Ans :
a. ( )

16.Create 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:

Ans :

Country["France"] = "Paris"
Country.update({"France": "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
Ans :
D. tuple_1[3] = 45
The code tuple_1[3] = 45 does not work on a tuple because tuples are immutable in Python,
which means you cannot change their elements once they are assigned
18. How many elements in the following data structure?
Ans : 6

19.Write a function which finds all pythagorean triplets of triangles whose sides are no
greater than a natural number N
Ans :

def find_pythagorean_triplets(N):
triplets = []
for a in range(1, N + 1):
for b in range(a, N + 1):
c_square = a**2 + b**2
c = int(c_square**0.5)
if c_square == c**2 and c <= N:
triplets.append((a, b, c))
return triplets
N = 10
triplets = find_pythagorean_triplets(N)
print(triplets)

You might also like