02. Variables
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" # StringAs 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" # StringThe 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 # BooleanAnother 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 # BooleanCongratulations! 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.