Python summary: variables


A variable is a name that refers to an object (a data) in memory.

>>> var1 = 'Hello'
>>> var1
'Hello'
>>> var2 = 5 + 5
>>> var2
10

The single-equal character (=) assigns the value on the right to a variable on the left.

And the reference can be changed:

>>> var = 1
>>> var
1
>>> var = 2
>>> var
2

Even referer to a data which is refered by another variable:

>>> var1 = 'Hello'
>>> var2 = var1
>>> var2
'Hello'

But in any case, the ones must be created at the moment it is assigned to a data:

>>> wellvar = 'Hello'
>>> badvar
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'new_var2' is not defined

And the possible characters for a variable name are:

  • It can just start with alphabetic character.
  • It can contain underscore character (_) and any alphabetic character.

 


More information:

Liked it? Share it! ;)

Be First to Comment

Leave a Reply