Manoel Duran
Voltar para artigosCurso de Python

02. Variables

·2 min de leitura

undefined At the beginning, you can think of a variable as a container that holds a value. Later, we will learn that Python uses references to objects, but this first meaning is enough for now.

Creating Variables

Below, we are creating a variable called name that stores a string value. Unlike many other programming languages, you don't need to declare a variable's type before assigning a value to it.

name = "Manoel"      # String

As a developer, you should choose meaningful variable names. Your code will be read not only by you in the future but also by other developers, so clear names make your code easier to understand and maintain. Here's an example:

name = "Manoel"      # String
x = "Manoel"         # String

The variable name clearly tells us what information it stores. On the other hand, the variable x does not describe its purpose, making the code harder to read and maintain.

name = "Manoel"      # String
age = 31             # Integer
height = 1.73        # Float
is_single = False    # Boolean

Another interesting thing to know is that Python follows a naming convention called snake_case. This convention means that words should be separated by underscores (_) instead of spaces or capital letters.

first_name = "Manoel"      # String
last_name = "Duran"        # String
is_single = False          # Boolean

Congratulations! You're the number one at creating variables. Let's learn an important concept: variables can change their values. If you have a variable called age with the value 31, you can later change it to 32. Run this code snippet below:

age = 31
print(age)
 
age = 32
print(age)

Variables are not limited to basic data types. They can also reference more complex objects, class instances, functions, and data structures, which we will learn about in future articles.