PYTHON PROGRAMMING
ASSIGNMENT ‐ 02
1. Why are comments an essential part of writing code? Provide two different reasons.
2. In your own words, define what a string is in Python. How can you create a string value using
single quotes ('), double quotes ("), and triple quotes (''' or """)? What is a key advantage of
using triple quotes?
3. Imagine you have a string user_input = " Hello World! " entered by a user.
a) Which string method would you use to remove the extra spaces at the beginning and
end?
b) Which method would you use to convert the entire string to lowercase?
c) Write the code to perform both operations and store the result.
4. Explain the primary purpose of the .format() method for strings. Then, write a line of code
using .format() to insert the variables name = "Alice" and age = 25 into the following
sentence:
"Hello, my name is Alice and I am 25 years old."
5. What is the result of evaluating each of the following expressions?
a) "Python" + " " + "Rules"
b) "echo" * 3
6. What is the key difference between an integer (int) and a floating-point (float) number in
Python? Provide an example of a scenario where you would use each type.
7. Why are functions like int(), str(), and float() necessary? Given the code below, why will it
cause a TypeError and how would you fix it using a conversion function?
age = 20
message = "I am " + age + " years old."
print(message)
8. What does the input() function do? What is always the data type of the value returned by
the input() function, regardless of what the user types? How would you convert this input into
an integer?
9. The % operator is an older method for string formatting. If we have a variable price = 4.5, how
would you use the % method to create the string: "The item costs $4.50"?
10. The print() function has parameters like sep and end. What will the output of the following
code be, and why?
print("Hello", "World", sep="-", end="! ")
print("How are you?")