Variables

In the previous chapter we wrote a simple program that printed a welcome message to the screen.

Now we'll rewrite this program using variables.

File: hello_world.py

message = "Hello, World!"
print(message)

As before, we can run this program by typing,

$ python hello_world.py

in the shell. As before, the program will print the message

Hello, World!

to the screen.

Note: The conda environment, pyintro, should be activated before running the program.

$ conda activate pyintro
$ python hello_world.py

This will be our way of running Python programs in this book. And we won't remind you about it in the future.

So if the program does the same thing as before, what's the point of writing it in a different way? Before we answer this question, let's take a closer look at the program itself.

The first line of the program declares a variable:

message = "Hello, World!"

In Python, variables are created when you assign a value to them using the = operator. Variable has a name and a value. In our case, the name of the variable is message and the value we assign to it is "Hello, World!".

You can think of variables as containers for storing data. We can later use the variable to access the data stored in it, by its name. In our case, we used the variable message to print the message to the screen:

print(message)

The nice thing about variables is that we can continue to use them in other parts of the program. For example, we can print the message again, but this time printing it with uppercase letters. For this, we can add a new line to the program:

print(message.upper())

The output of the program will be

Hello, World!
HELLO, WORLD!

To answer our question from the beginning of this section, the point of using variables is that they allow us to reuse the data stored in them. In our case, we stored the message in the variable message and then used it twice in the program. If we need to change the message, we only need to change it in one place, where we assign the value to the variable. The rest of the program will use the new value automatically.

Type Hits

One important propery of a variable, which might not be obvious from the example above, is its type. In this case, the type of the variable is str, which is short for string. To make this more explicit, we could have declared the variable in the following way:

message: str = "Hello, World!"

(note the : str part after variable name).

This is called type annotation. We will prefer to call it type hint, because it is not enforced by the Python interpreter. Python is a dynamically typed language, which means that you don't have to specify the type of the variable when you declare it. And, event if you do, Python will ignore it.

However, it is a good practice to do so, because it makes the code easier to read and understand. Also VS Code uses type hints to provide additional help when writing code.

Please note, that in the example above there was no need to specify the type of the variable, because Python can infer it from the value we assign to it. We will prefer using type hints, when appropriate or for illustrative purposes.