Sunday, June 9, 2019

Variables in Python

In order to do anything with your output it is useful, and it can be argued absolutely necessary to store information somehow in order to manipulate that information. Python has a very straight forward and easy syntax ( set of rules ) in order to do this called variables.

They are called variables because the value can be changed.

To declare a variable in python is simple:
1
2
a = 1
b = 100

In the above example I have declared two variables named a and b. I store the value 1 in a and the value 100 in b. To avoid confusion I like to say that a becomes 1 and b becomes 100. This is to avoid confusion between equality and assignment. To say that a is equal 1 and/or b = 100 could be true for this portion of the code but the variable could later be manipulated to become another value.

Now that these values are stored in variables I can output them using the print command we learned earlier:

1
2
3
4
5
a = 1
b = 100

print(a)
print(b)

If you type this in to a python shell or in a python program you will get the output:

 1
 100


Continued On -> More About Variables in Python


https://www.w3schools.com/python/python_variables.asp - More about Variables

Python Programming: A Practical Introduction To Python Programming For Total Beginners

Note: I use the term declare to describe how to create a variable in python. If I am honest I have told a white lie. To declare a variable usually means that it has to be predefined before it is used. In python its first use automatically declares it. For someone learning python I think this is splitting hairs and it is inevitable that programmers are going to ask "How do I declare a variable in python" and the python zealots will respond with "One does not declare variables in python".

No comments:

Post a Comment

nonlocal keyword

The nonlocal keyword is used to allow an inner function to access variables defined in an outer function. Without using the nonlocal keywo...