Variables are very important because they let you store and organize information. Without variables, you would have to remember every piece of information manually, which would be very difficult. By using variables, you can:
- Save data (like numbers or words) to use later.
- Change the data if needed (like updating your age as you grow).
- Make your code easier to read and understand.
- Create programs that calculate, store, and display results dynamically (Actually an important usecase)
Table of Contents
What is Variable?
Variable is just like a container that is used to store values. Imagine you are organizing your room. You label boxes to store toys, books, or clothes. A variable in Python works similar to that. It is a name that holds a value.
For example:
name = "Inkita"
age = 25
hobby = "art and crafts"
PythonNow the syntax for create a variable is <variable> = <value>
.
write the “variable name” on the left side of =
(assignment operator) and the value on the right side. Now whenever you use <variable>
it will replace with the value it has been assigned with.
Although variables work differently in Python compared to some other programming languages. In other programming languages, you must first initialize a variable, meaning you have to tell the compiler what value the variable will hold. But in Python, you don’t have to do that. You can directly assign values to a variable and even change them later. This is called dynamic typing.
How Do Variables Hold Data in Memory?
Variables in Python are stored in memory through a simple and efficient process.
Variables don’t actually hold the value itself; it simply point or refer to that value in a memory. When you assign a value to a variable, Python reserves a specific space in the computer’s memory to store that value. This is why variables don’t have a specific type; instead, it takes type on the type of the value it refers or point to.
age = 12
PythonPython creates a memory location to hold the value 12
.
The variable name age
acts as a label or reference that points to the memory location of value 12
. This allows Python to find the value whenever you use the variable.
If no variable references a memory location, Python automatically frees up that memory so it can be reused. This process is called garbage collection.
Rules to Name a Variable
In the token, you learned that Python has identifiers used to identify or refer to something. A variable is nothing but an identifier, and you need to follow the rules for identifiers when creating a variable which are:
- Variable can contain characters, digits, and underscores only.
- Must Start with a letter or an underscore (_), and not with a number.
- Avoid whitespace
@
,$
,%
, etc - Variables are case-sensitive. Hence,
Name
andname
are different. - keywords or any reserved words cannot be used as a variable name like
if
,for
. - Use meaningful names to describe what the variable holds.
Summary
We have learned about the concept of variables in Python. So they are used to store, label, and manage data effortlessly, which makes your programs dynamic and interactive. From simple values to complex data structures, they are the backbone of every Python program. To learn more about memory management, you can refer to this blog.