0% found this document useful (0 votes)
102 views5 pages

Python Assignment Solutions and Tips

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views5 pages

Python Assignment Solutions and Tips

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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([Link](A, B))

Ans : c. print([Link](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 = [Link](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

[Link] 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

[Link] of the following variable(s) are character data type?

Ans : D ) All of the above

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

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

Ans :

Country["France"] = "Paris"
[Link]({"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

[Link] 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:
[Link]((a, b, c))
return triplets
N = 10
triplets = find_pythagorean_triplets(N)
print(triplets)

Common questions

Powered by AI

In a sequence created with range(10, 26, 4), the index of the value 18 is 2 .

The result of the conditional expression print("Smaller") if A == B else print("Greater") if A < B else print("True") when A=10 and B=12 is "Greater" because A < B is true .

The output of the lambda function x=lambda a,b:a//b when called with arguments 10 and 3 is 3, since it performs integer division of 10 by 3 .

The assignment tuple_1[3] = 45 does not work on a tuple in Python because tuples are immutable, meaning their elements cannot be changed once assigned .

Accessing an undefined variable in Python raises a NameError exception .

The result of evaluating the expression A = p * (1 + r / 100)**n with initial values n=10, r=5, and p=100 in Python is 162.89 .

To replace 'Marseilles' with 'Paris' in a dictionary, you can use either of the following commands: Country['France'] = 'Paris' or Country.update({'France': 'Paris'}).

To slice the substring 'Learn' from the string 'Machine Learning', use the code string[8:14], which captures the substring starting at index 8 and up to, but not including, index 14 .

To implement a function to find all Pythagorean triplets with sides not greater than N, iterate through pairs of possible side lengths, compute the hypotenuse using the Pythagorean theorem, and check if it forms a perfect square. Append valid triplets to a list. This can be achieved with the function find_pythagorean_triplets as described: 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 .

To print a formatted string in Python using variables A and B, you can use the format method: print("There are {} students in the class, with {} who play at least one sport.".format(A, B)).

You might also like