If you look carefully in the code below, you will notice a difference between the string object "Program started!" and the two list objects in lines 5 and 6. The difference (apart from one being a string and the other two being lists) is that the string has not been associated with a variable, but the two lists have.
The first list has been associated with the variable named dates and the second with temperatures_celsius. So, what is a variable?
👉A variable is a name that the programmer chooses to give to an object.
Why should the programmer use a variable?
Using a variable is not necessary most of the time but it makes programming easier. For example, you can see that in line 14 we are using the two list objects that we created in lines 5 and 6. We could also enter the actual list objects in line 14 and the code would look as below:
pyplot.plot(["1-2-2089", "1-3-2089", "1-4-2089", "1-5-2089", "1-6-2089"], [32, 29, 28.5, 30, 31])
but this version below cleaner:
pyplot.plot(dates, temperatures_celsius)
I think the variable in the case of the "Program started!" string is not necessary. In the case of the lists the use of variables makes the code cleaner because the lists are two long to fit in line 15. So, better create them first in separate lines and associate them with variables there, and then refer to their variables later in the code.
If you can't decide whether to use a variable or not, then the rule of thumb is to use one.
Next Lecture
Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.