Python Lessons – 1.9 Variables

Pyhton Lessons - 1.9 Variables

Variables

In previous lessons we have already used the variables, although perhaps we are not yet well aware of them. In effect, the variables are practically unavoidable in programming and are used to store values in order to be used later.

>>> a = 3

In fact, we immediately saw the need to use them in the context of the Python console. For example by entering a value with input () it will be necessary to store it somewhere, and not to use it immediately and then lose it or re-insert it a second time.

>>> name = input("Enter your name")

As you can see in this case the string that you insert will remain in memory, and every time we need it we will refer to it as name. You can use any name you want. The important thing is to use names that are easily identifiable with their function or with what they contain.

Often programs using hundreds of variables and you can well understand the importance of being able to distinguish one from the other through a good nomination.

In the meantime, we can use the console to perform other commands, such as calculations.

>>>1 + 3
4

And then reuse the variable when you need it.

>>> print (name)
Fabio

Or you can vary the value, assigning a new one (reassignment)

>>> name = "Mario"
>>> print(name)
Mario

In Python, unlike other programming languages, they do not have a variable type, that is, the variable we use can have different types of data during its existence (until the Python console is closed).

>>> name = 3

Variables name

We have seen the importance of variables and how they should be called. However, there are some restrictions that we must take into account.

The characters with which we can assign a name to a variable are the numbers, the letters and ‘_’ (underscore). Furthermore, the name of a variable can never begin with a number.

Furthermore, Python is a case sensitive programming language. That is, it is able to distinguish between uppercase and lowercase characters. So variables like Name and name are different.

More about variables

Referring to a variable that has not been defined (ie to which no value has been assigned) generates an error.

>>> foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'foo' is not defined

We said that a variable exists as long as the Python console (session) is open. But this is not always true. We can delete (or better delete) a variable using the del command.

>>>del name

From now on, if we refer to this variable, without reassigning some value, we will get an error message

>> >name
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'name' is not defined

NOTE: Often you will find many examples that show variables with the name of foo and bar. These variables are called metasyntactic and serve as fictitious names in tutorials or sample codes for demonstrations.

⇐ Go to Python Lesson 1.8 – Type Conversions

Go to Python Lesson 1.10 – In-place operators ⇒

Leave a Reply