What is a variable in Python?

A variable is a way of accessing data when writing code. If I create a variable called "var" and set it equal to the number 24, everytime I want to access that number again, all I have to do is use "var" instead of 24. Variables cannot start with numbers or have any special characters in them, that is with the exception of the "_". One may use the underscore often to represent 2 or more words, for example, "Circle_Area". A variable is fundamental to coding because, in order to manipulate data, one must be able to properly store their data. One can store different types of data, one can store integers, floats, booleans, lists, strings etc. If you have a logic problem where you want to solve you get from point A to point B as if you were coding part of a GPS system, you may want to compare 2 longitudes and latitudes. If both are the same, then the driver has arrived, else refresh and check again. In this scenario, one cannot use literal floats efficiently as one would constantly have to change the float value to match the driver's location. If one were to use a variable one could simply update the variable every time. There are also things called constant variables, which can allow one to create a variable that cannot be changed. For python, one uses all uppercase letters in order to indicate that a variable's value must not be changed. In our scenario, we could use constants for our destination longitude and latitude. whilst tuples would be much simpler, this is a good way to explain the idea of variables. The pseudocode may look something like this:
DESTINATION_LAD = 34.506
DESTINATION_LONG = 76.485
current_Lad = 43.675
current_Long = 86.455
if current_Lad == DESTINATION_LAD and current_Long == DESTINATION_LONG :
print("Arrived at destination")
else:
//update current_Lad and current_Long here
This is a huge concept in programming, and next time I will cover data structures.

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now