Variables

In this chapter we will talk about variables. Variables allows us to store data that can be reused later. Imagine you’re doing some complex calculation, with data retrieved from a separate source, a database, for example. Now, imagine you will have to reuse it later in your program. It makes no sense to retrieve the data and do the calculation all over again. For this, we use variables.

Defining variables

To define a variable in Python, all we have to do is to type a name for it, the equal sign (=), and then the value that the value will store.

A variable in Python can store basically any kind of data; numbers, strings, lists, dictionaries.

Variable names in Python have to start with a alphabetical character or an underscore (_). Numbers can be used in the variable name but they can’t be in the beginning.

Variables in Python also do not have a pre-definite type, meaning that it’s not a statically typed language, but a dynamically typed language. In this sense, one variable tha initially stores a string, can later store an integer, or a list. Let’s see then, an example of variables

a = 3
b = 7
print(a+b)

a = "Now, a string"
print(a)

> 10
> Now, a string

You can also set the value of a variable as the same as another variable. Continuing with the previous example:

b = a
print(b)

> Now, a string

It’s also possible to set more than one variable at a time. If the value is the same for everyone, you can just use = between the variable names. If the values are different, you can use commas between the variable names and between the values. See the examples for this below:

# Defining multiple variables with equal values
x = y = z = 10
print(x)
print(y)
print(z)

# Defining multiple variables with different values
x, y, z = 10, 20, 30
print(x)
print(y)
print(z)

> 10
> 10
> 10
> 10
> 20
> 30

Remember that we saw on the previous chapter that == is the comparison signal. It’s important to not mistake the comparison sign == and the attribution signal =. To define a variable, use =, and to make comparisons, use ==.

The input() function

Now, we will talk a little bit of a use case of variables that might help in seeing the use of it. Your program needs data to be entered by the user, to then work upon it. For this, we use the input() function. This function basically prompts data to be inputted by the standard input by the user. It accepts a parameter, a message to be shown before the data is entered.

Whatever is entered by the user has to be saved in a variable, so we can then use it later in our program. In the example below, we save the user name in a variable called name and then, we print it, along with a message. In this example, right after the messsage, the program will wait for the user’s input. After typing your name, hit Enter, so that the code proceeds:

name = input("Hi, what is your name?\n")
print("Hi, %s" % name)

> Hi, what is your name?
Felipe
> Hi, Felipe

Conclusion

In this chapter, we saw how to use variables. Variables are essential in programming, allowing you to store retrieved values or values inputted by the user to be reused later.

Basically, a variable is defined by its name, an equal sign, which is the attribution sign (=) and then the value the variable will have.