5.9 Variables
In Python, simple rules apply to the handling of variables:
-
Assignment prior to useEach variable must be assigned an initial value before the variable can be parsed in an expression. For example, you cannot execute x=x+1 if you have not done a first-time assignment like x=0, x=1, x=n beforehand.
-
No type declarationPython variables can store objects of any type. Python remembers the type and thus knows what kind of data a variable refers to.Unlike many other programming languages, however, the type of variable cannot be specified or restricted. You can easily store data of different types in the same variable, for example, first a number (x=1), later a string (x='abc'), and finally a list (x=[3,2,1]).
-
NamesVariable names ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access