EE1021 Lecture 1 Worksheet: Variables, Operators and Strings
Topics: variable naming rules, assignment, operators, floats, strings, indexing & slicing, print output
Workflow: predict on paper first, run the code, then explain any difference between your prediction and the result. There is no lab this week; bring the completed worksheet to Lab 1 on Thursday 24 September. Any completion check will be announced in CATS.
How to run single lines: open the VS Code terminal (Terminal > New Terminal), type python (macOS: python3) and press Enter. The prompt changes to >>>; the terminal is now Python, not PowerShell or the shell. Type one line, press Enter, and Python shows the value. Use type(...) to see the type. Leave the prompt with exit(). For the multi-line blocks in Section D, save the code in a .py file and press Run.
Section A – Quick Checks
- Tick the values that are floats in Python:
5,3.0,-2.75,"4",7/2. - True / False: Reassigning
x = 5; x = x + 1changes the value bound tox. - True / False: Parentheses can change the order in which Python evaluates arithmetic expressions.
Section B – Variable Naming Rules
- Mark each name as Valid or Invalid. If invalid, write a valid alternative.
total_score3dPoint_tempspeed-mpsAverageValueSpeed(whenspeedalready exists)
- Briefly explain why Python rejects any name you marked invalid (one short phrase each, e.g., “starts with digit”).
Section C – Evaluate the Expression
Write the final value and type for each, then type them at the >>> prompt (see How to run single lines above), enter the variable name to see its value, and confirm the type with type(...), for example type(distance).
distance = 120 / 4mass = 7.2 + 0.8greeting = "hi" + " there"result = 4 ** 2 - 3 * 2check = 12 % 5 == 2flip = (7 > 2) and (3 > 5)mix = (5 + 2.0) * 3check_char = "a" in "data"not_member = "z" not in "science"steps = 17 // 5left_over = 17 % 5
Section D – Predict the Printed Output
Predict the output (line by line) before running each block as a .py file with Run.
x = 4
print("x is", x)
x += 3
print("now x is", x)
x *= 2
print("finally x is", x)word = "python"
print(word[0])
print(word[-1])
print(word[1:4])first = "data"
second = "science"
message = first + " " + second
print(message)
print(len(message))Section E – Float Practice
- Compute the average of
5,7, and10and store it inavg. Print a sentence showing the average as a float. - Store your height in meters as a float (example:
1.72) and printMy height is ___ meters. - Evaluate
(3 / 2) * 4and note the resulting type. Verify at the>>>prompt.
Section F – Strings
- Create
course = "EE1021"and printEE1021 EE1021 EE1021using repetition. - With
tag = "engineer", print: first character, last character, slice"gine", reversed string. - Starting from
phrase = "HelloWorld", produce"Hello Python World"using slicing and concatenation (no loops). Test at the>>>prompt.
Section G – Mini Practice
- Write 3–4 lines that assign
baseandheight(floats), compute triangle area, and print a descriptive message. - With
temp = 18,humidity = 40,is_raining = False, write three boolean expressions: one withand, one withor, one withnot. Print each; verify in VS Code. - Read a number from the user with
input("Side length: "), convert it withfloat(), and print the area of the square. Then predict whatprint(3 * side)displays if you forget thefloat()conversion and the user types4. Run both versions and explain the difference.
Section H – Reflection and Exit Check
- Which result in this worksheet surprised you most? Explain the misunderstanding in one sentence.
- Without running Python, predict the value and type of
7 / 2. - If
word = "sensor", predictword[0],word[-1], andword[1:4]. - Correct the invalid variable name
3d-point, then bind the corrected name to2.5.