Learning how to create variables in Python is one of the first steps toward mastering this programming language. Variables let you store data temporarily so it can be used in different parts of your code. In this article, you will learn in a practical way how to create variables in Python, following best practices and exploring examples.
Variables are like little “boxes” that store information to be used throughout the program. They let you keep temporary values, such as numbers, text, or even the results of calculations, so you can reuse them whenever needed.
For example, in the code below:
name = "Alice"
age = 30
height = 1.65 Python automatically detects the type of data assigned to a variable, eliminating the need for complex declarations like in other languages.
Creating variables in Python is simple. Just assign a value using the assignment operator (=). Here is an example:
city = "São Paulo"
temperature = 28.5
rained_today = False Here, you define three variables:
Python has a few rules for naming variables:
Example:
# Correct
account_balance = 500
# Incorrect
1balance = 500 # Starts with a number In Python, you can update a variable’s value at any time. Here is how it works:
message = "Welcome!"
print(message) # Output: Welcome!
message = "Learning how to create variables in Python."
print(message) # Output: Learning how to create variables in Python. The code above shows how a variable can be reused with a new value.
When you learn how to create variables in Python, it is essential to understand the main data types they can store:
Practical example:
# Variable types in Python
product_name = "Notebook" # String
quantity = 10 # Integer
price = 15.90 # Float
in_stock = True # Boolean To write clean, efficient code, follow these best practices:
Example:
# Calculate the final price of a product including tax
base_price = 50 # Initial price
tax_rate = 0.1 # 10% rate
final_price = base_price + (base_price * tax_rate)
print(final_price) # Output: 55 Now that you know how to create variables and have your development environment set up with Visual Studio Code, look at the result of the code below:
# By convention, Python uses snake_case, not PascalCase or camelCase
player_one = "Joffrey"
# Check the type
print(type(player_one))
# In Python, a variable's type can change
player_one = 200
print(type(player_one))
# Assigning values
print("*" * 20)
player_one += 20 # Adds 20 to the current value
player_one -= 5 # Subtracts 5 from the current value
player_one *= 3 # Multiplies the value by 3
player_one /= 2 # Divides the value by 2
print(player_one) Now you know how to create variables in Python and apply best practices when defining their names and values. This is one of the first steps toward writing efficient, well-organized code.
Continue your journey with the next lesson, where you will learn about Python’s main data types and how variables can change between them as the code runs. Keep following our course and, if you enjoyed this content, share it with your friends!
Watch the previous lesson.
See the final code on my GitHub.
Read more in the official documentation.
Functions are one of the most important concepts in any programming language, and Python is…
Loops in Python are one of the most fundamental concepts in any programming language. They…
When developing software, one of the most common tasks is controlling the program's flow of…
Manipulating strings in Python is one of the most common tasks, and Python offers incredibly…
Understanding data types in Python is one of the first steps to mastering the language.…
The Garbage Collector (GC) is one of the essential pillars of memory management in .NET.…
Este blog utiliza cookies. Se você continuar assumiremos que você está satisfeito com ele.
Leia Mais...