Variables
Introduction
Let us see how to create {==variables==} in Python. In order to understand variables, I'd like you to meet {++Billy++}, he is a friendly dinosaur and is currently planning for his birthday party. Billy is very excited about the party and is busy thinking about packaging all the return gifts.
Billy has three type of return gifts -
- Balloons
- Party Hats
- Crackers
Declaring Variables
Let's start by creating a file called as 'variables.py' anywhere on your computer and open it in your code editor and type the following -
In order to run the file, open the terminal and navigate to the directory which contains the file.
Suppose I have created that file on my desktop, then the path on my terminal will be -
In order to run the file, type the following in your terminal -
Output will be as follows -
Baloons
Congratulation! You just created a variable and printed its value.
Understanding Variables
Let us again have a look at the code we wrote above
You are declaring a variable called {==box==} and assigning it a value of {==Baloons==}, note the single quote around it, you can also use double quotes as follows and it will give you the same output.
Single and double quotes can be used interchangeably, but be sure to use a consistent style all across your code.
Printing Values
Line 2 prints the value of the variable 'box' to the terminal, which in our case is 'Baloons'.
Baloons
{==print()==} is a builtin python function which we can easily use to print values.
Re-assigning Values
Suppose, now Billy wants to pack 'Party Hats' instead of 'Baloons', then we can re-assign a new value to the same variable 'box'
You can again check the value by running the file
python3 variables.py
Output
Party Hats
Notice that our value has now changed from 'Baloons' to 'Party Hats'.
And now finally Billy wants to pack 'Crackers', so we can once again re-assign 'box' to a new value of 'Crackers'
If you run the file again, this time output will be
Crackers
Full Code
# variables.py
box = 'Baloons'
print(box) # prints -> Baloons
box = 'Party Hats'
print(box) # prints -> Party Hats
box = 'Crackers'
print(box) # prints -> Crackers
Conclusion
As you must have noticed {==box==} is just a placeholder, we can assign different values to it from time to time, thus it is know as a variable, i.e values can change but the placeholder remains the same.