02-01: Exercises — Basic Syntax¶
Notes reference: 02-01: Basic Syntax
Q1: Print a greeting¶
Print your name and current city on two separate lines using a single print() call.
Solution
Q2: Print with separator¶
Print the three values "Dhaka", 110, and True on one line, separated by " | ".
Solution
Q3: Print without newline¶
Print "Loading" followed by "..." on the same line without a newline at the end, then print " Done" after.
Solution
Q4: Comments¶
Write a line that calculates the area of a rectangle (width=8, height=5) and add a comment explaining each variable.
Solution
width = 8 # width of the rectangle in cm
height = 5 # height of the rectangle in cm
area = width * height # area = width × height
print(area) # 40
Q5: Fix the indentation error¶
The following code has an indentation error. Identify and fix it.
Solution
Q6: Line continuation¶
Write a long arithmetic expression (sum of 6 numbers) split across two physical lines using \.
Solution
Alternative — use parentheses (preferred):
Q7: String with quotes inside¶
Print the sentence: She said, "Python is great!"
Solution
print('She said, "Python is great!"')
# Alternative — escape the inner quotes
print("She said, \"Python is great!\"")
Q8: Case sensitivity¶
Predict what happens when this code runs and explain why.
Solution
Name and name are two different variables. Python is case-sensitive — the variable was defined as Name (capital N) but referenced as name (lowercase).