E ExamMaster

Engineering Python · Language Basics

Variables and Names

In Engineering Python because labs store intermediate values under names — assignment and rebinding are how every later calculation starts.

A variable is a name bound to a value. Assignment stores; printing a name shows the current binding. Rebinding changes what the name refers to next — it does not rewrite earlier printed text.

  • Engineering Python
  • Easy level
  • 4 concepts

1Assignment stores a value

`name = value` binds the name on the left to the value on the right.

Reading the name later yields that value until you rebind it.

Figure. name = value binds the name on the left to the value on the right. Reading the name later yields that value.

Assign and print

voltage = 5
print(voltage)
After `x = 3`, what does `print(x)` show?
  1. 3
  2. x
  3. print(x)

The name `x` refers to the integer 3.

2Choosing names

Names should say what the value is for: `temp_c`, `row_count`, not `a` and `b`, unless the idea really is abstract.

Python names start with a letter or underscore; they are case-sensitive. `Temp` and `temp` are different names.

Figure. Names should say what the value is for: temp_c, row_count, not a and b, unless the idea really is abstract.

Takeaway

  1. IdeaNames should say what the value is for: `temp_c`, `row_count`, not `a` and `b`, unless the idea really is abstract.
Which of the following represents a syntactically valid Python variable identifier according to language rules?
  1. 2_user_score
  2. user-score
  3. user_score_2
  4. class

Variable names cannot start with numbers, cannot contain hyphens, and cannot be reserved keywords like class.

3Rebinding updates the name

Assigning again replaces the binding. Earlier prints already happened with the old value; they are not rewritten.

This is the trap: people expect old output to change when they rebind. It does not — only future uses of the name see the new value.

Figure. Assigning again replaces the binding. Earlier prints already happened with the old value; they are not rewritten.

Takeaway

  1. IdeaAssigning again replaces the binding. Earlier prints already happened with the old value; they are not rewritten.

Rebind then print

x = 1
print(x)
x = 2
print(x)
In Python, after executing x = 10 followed by x = "alpha", what has occurred in memory?
  1. The memory location holding 10 is mutated into ASCII character bytes
  2. A TypeError exception is raised due to static type conflict
  3. The name x has been rebound to refer to a new string object, leaving the integer eligible for garbage collection
  4. Both values 10 and alpha are combined into a two-element tuple

Python variables are references (names bound to objects). Rebinding assigns the name to a completely new object in memory.

4Lab: print a variable

Assign a sensor reading to a name, then print it. Change the number and re-run to see the new binding.

Figure. Assign a sensor reading to a name, then print it. Printing the name shows the current binding.

Takeaway

  1. IdeaAssign a sensor reading to a name, then print it. Change the number and re-run to see the new binding.

Coding lab. Print a named value runs in the app, with checks on your output.

Notes

  • In Engineering Python because labs store intermediate values under names — assignment and rebinding are how every later calculation starts.
  • `name = value` binds the name on the left to the value on the right.
  • Names should say what the value is for: `temp_c`, `row_count`, not `a` and `b`, unless the idea really is abstract.

Exam traps & shortcuts

  • Run one cell at a time and read stdout before changing more lines.
  • Names are labels for values; rebinding a name does not rewrite old prints.

Recap

Assignment binds a name; rebinding changes future reads, not past prints.

Assignment stores a value
`name = value` binds the name on the left to the value on the right.
Choosing names
Names should say what the value is for: `temp_c`, `row_count`, not `a` and `b`, unless the idea really is abstract.
Rebinding updates the name
Assigning again replaces the binding. Earlier prints already happened with the old value; they are not rewritten.
Lab: print a variable
Assign a sensor reading to a name, then print it. Change the number and re-run to see the new binding.

Practise Variables and Names

Reading is free and needs no account. Practice, mocks and progress live in the app.

  • A 2-question practice set that ends the chapter
  • 3 quick checks with worked explanations
  • Timed mocks scored with the real marking scheme
  • Readiness tracked per topic, kept on your device
Continue with Google — freeNo card, no trial. Works offline once installed.