Variables
10 min. | Beginner
Assignment
Variables in Python act as containers that store information the program can access and modify. They can hold:
- Data types.
- The results of expressions.
- Data structures.
- Functions.
- Objects.
- Modules.
This flexibility allows variables to store almost any kind of information, making them essential for all kinds of programming tasks.
Naming
In Python, there are some conventions when it comes to naming variables:
- Must start with a letter or underscore.
- Cannot start with a number.
- Can only contain alphanumeric characters and underscores.
- Are case-sensitive.
Reassignment
Reassignment means updating a variable’s value after it has been created. This allows the variable to hold new information that the program can use and modify.
Multiple Assignment
Multiple assignment allows you to assign values to several variables in a single line, making your code more concise and readable.
Dynamic Assignment
In Python, variables are dynamically typed, which means a variable can be reassigned to a value of a different type at runtime. This allows the same variable to hold, for example, a number one moment and text the next.