What is a variable in Python?

in #python8 years ago (edited)

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.

Sort:  

This is a good article, but since you're using the python tag, you should use Python conventions in your code examples. It would also help to explain the difference between the numeric types.

For example, Circle_Area should be circle_area, as it's more pythonic to use lower case for variable naming. Also, current_Lad should be current_lad. Combining snake and camel case is not good pythonista practice. Snake case is the Python norm.

As for the variable types:

Python supports four different numeric variable types: int, long, float, and complex

An example of each is below:
int - 10
long - 086498263L
float - 3.14159
complex - 2e+24J

Variables are distinguished by the Python run-time by how they look. All the variables above have something that gives them a unique identity, such as the "L" in the long variable, and the decimal in the float.

Strings are declared by just surrounding text in double quotes:
my_first_string = "this is a string"

there are other variable types and much more potential detail to dive into, but I intended this response to just be a gist of constructive tips.

That's about it! Great article! I don't mean any of this to sound negative in any way! I am merely a Python enthusiast who wanted to offer a few points of constructive criticism.

Have a great day and I wish you luck with writing your programming tutorials!

I haven't taken Python for a while so your comments were highly appreciated, it was a great refresher as I haven't taken Python for a while. I may do an intro article and strat c++ as that is what I am currently doing. Thank you for your input: ) ,