initialisation
Giving a variable its first value before it is used, especially before a loop that updates it.
In more detail
Forgetting it gives NameError on the first update; putting it inside the loop resets it every pass, a classic bug. Choose the initial value that makes the first update right, such as 0 for a sum.
Related terms
- accumulation pattern — A loop pattern that builds a result step by step, starting from an initial value and combining each item into it, such as adding up a list.
- successive modification pattern — A loop pattern where a variable is updated a little on every pass, each new value built from the previous one, such as number = number + 1.
- variable — A name that refers to a value, created by assignment, such as width = 10.
Taught in
- Repeat Loops And Successive Modification — Hands-on Python: Complete Video Course & Book
- Accumulation Loops — Hands-on Python: Complete Video Course & Book
- Playing Computer — Hands-on Python: Complete Video Course & Book
- Mad Libs Revisited: Finding The Cues — Hands-on Python: Complete Video Course & Book