Worksheet 1 - Solutions

✅ Solutions



---


🔹 Part 1 — Using the % Operator


Q1.


name = "Ali"

age = 21


print("My name is %s and I am %d years old." % (name, age))



---


Q2.


price = 499.99


print("The price is %.2f dollars." %(price))



---


Q3.


x = 5

y = 10


print("x = %d, y = %d" % (x, y))



---


Small Task:


temperature = 37.5


print("Temperature: %.1f°C" %(temperature))



---


🔹 Part 2 — Using .format()


Q4.


city = "Lahore"


print("I live in {}.".format(city))



---


Q5.


subject = "Math"

marks = 95


print("I got {} marks in {}.".format(marks, subject))



---


Q6.


print("{1} is {0} years old.".format(20, "Ahmed"))


✅ Output:


Ahmed is 20 years old.



---


Small Task:


print("Book: {} | Pages: {}".format("Python Basics", 350))



---


🔹 Part 3 — Small Challenges


Q7.


print("PI is approximately %.2f." %(3.14159))



---


Q8.


print("Name: {}, Age: {}".format("Sara", 18))



---


ChatGPT is trash

No comments:

Post a Comment

Formatting in Python means combining strings and numbers using formatters. The formatters act as placeholders for parameters. Python include...