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

  1. Tick the values that are floats in Python: 5, 3.0, -2.75, "4", 7/2.
  2. True / False: Reassigning x = 5; x = x + 1 changes the value bound to x.
  3. True / False: Parentheses can change the order in which Python evaluates arithmetic expressions.

Section B – Variable Naming Rules

  1. Mark each name as Valid or Invalid. If invalid, write a valid alternative.
    • total_score
    • 3dPoint
    • _temp
    • speed-mps
    • AverageValue
    • Speed (when speed already exists)
  2. 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).

  1. distance = 120 / 4

  2. mass = 7.2 + 0.8

  3. greeting = "hi" + " there"

  4. result = 4 ** 2 - 3 * 2

  5. check = 12 % 5 == 2

  6. flip = (7 > 2) and (3 > 5)

  7. mix = (5 + 2.0) * 3

  8. check_char = "a" in "data"

  9. not_member = "z" not in "science"

  10. steps = 17 // 5

  11. left_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

  1. Compute the average of 5, 7, and 10 and store it in avg. Print a sentence showing the average as a float.
  2. Store your height in meters as a float (example: 1.72) and print My height is ___ meters.
  3. Evaluate (3 / 2) * 4 and note the resulting type. Verify at the >>> prompt.

Section F – Strings

  1. Create course = "EE1021" and print EE1021 EE1021 EE1021 using repetition.
  2. With tag = "engineer", print: first character, last character, slice "gine", reversed string.
  3. Starting from phrase = "HelloWorld", produce "Hello Python World" using slicing and concatenation (no loops). Test at the >>> prompt.

Section G – Mini Practice

  1. Write 3–4 lines that assign base and height (floats), compute triangle area, and print a descriptive message.
  2. With temp = 18, humidity = 40, is_raining = False, write three boolean expressions: one with and, one with or, one with not. Print each; verify in VS Code.
  3. Read a number from the user with input("Side length: "), convert it with float(), and print the area of the square. Then predict what print(3 * side) displays if you forget the float() conversion and the user types 4. Run both versions and explain the difference.

Section H – Reflection and Exit Check

  1. Which result in this worksheet surprised you most? Explain the misunderstanding in one sentence.
  2. Without running Python, predict the value and type of 7 / 2.
  3. If word = "sensor", predict word[0], word[-1], and word[1:4].
  4. Correct the invalid variable name 3d-point, then bind the corrected name to 2.5.