Variables
In Python
In Python
By the end of the lesson I will be able to:
We use variables in mathematics to identify unknown values. Values that can change.So an equation of the form "y = 2x + 1" tells us information about all the possible values that x and y can have.
Programming variables are different. They are variables in one sense because they can change value, but we can always know what is value stored there. In programming a variable refers to an identifier or label. This label tells a computer program where to look to find information.
So, a variable is a label that points to some storage in memory. The blue bucket in the image represents our variable.
A variable declaration is, in general, composed of three parts:
Every variable has a type. The type determines the types of actions you can take using the data. There are four most common types of data in computing:
The label is the name we give to a variable. Variables should always be named carefully and appropriate to the data that is being stored inside it. If we are storing someone's name into a variable, an appropriate label would be "name." Examples of good variable names:
In most programming languages, including Python, the label used for a variable can only have alphanumeric (a to z, A to Z and 0 to 9) characters and underscores (_) in them. In addition, the label MUST start with a letter.
The process of choosing a location and giving it a label is called declaring a variable. We can set values into those locations immediately. This is helpful if you know something has an initial value. For example, if you are writing a program that counts to 100, you know that you want to start at a particular value, either 0 or 1. So, when we declare the variable, we can assign it a value immediately, then use it or change it later.
IMPORTANT: A variable must be given a value BEFORE it is used. If I told you to get the contents of bucket 4 in the example above before putting anything in there, what would happen? The same is true for variables in computers. If you attempt to use a variable that is not declared or does not have a value stored in it, you will see an error message.
In Python, variables are declared at the same time they are first given a value. We will explore that next.
The value of a variable is the data that is stored in the container.
Assignment is the process of putting data into a variable.
Lets say we have 5 buckets. Each bucket starts out empty and unused.
Now lets say we want to store some information. First we need to pick a location, so we choose a bucket.
Then we assign a label to that bucket, say "A".
Now we put something into our bucket; our data. Let's fill this bucket with water.
Whenever we want water, we simply remember that "A" has water in it, then go to "A" and take some. We could go through the same process again and pick another bucket, assign it a name, then fill it with something else. Lets fill bucket 3 with leaves and call it "B"
So now we have 2 buckets, each storing some data that we may need later.
In programming we do something similar: Choose a location (bucket), give it a label, then fill it with information. To fill it, we write our label first, use a single equal sign (=), followed by the value we wish to store.
Here are some examples of storing a value into a variable in Python:
i = 0
s = "Hello World!"
f = 0.1
b = True
i = 1
s = "I am changing my value!"
f = i
i = s
In our first four lines, we are declaring four variables:
We are also storing values into those variables immediately, thus i contains 0, s contains "Hello World!", f contains 0.1 and b contains True. Notice that we did not tell Python about the type of each variable. Python determines the type automatically based on the value assigned.
Note that "Hello World!" is in quotations. This is because a string must be surrounded by quotes to be valid. In fact, 0 and "0" are two different values; 0 is the integer representation of the number 0 (00000000base 2), whereas "0" is a string represented by the 0 character (00110000base 2).
The next two lines are variable assignments of variables that have been previously declared. Think of this like changing what we are storing in the bucket: The old value is removed in order to store the new value.
The last two lines show us that we can store values from one variable to another. On the line "f = i" f is no longer a floating point number! Now it is an integer!
The last line has a similar effect: the value stored in i is now a string!
Most programming languages try to use a common way of creating labels so that we can easily identify a variable. Consistency of variable names is a key component in making your code readable.
In Python, convention dictates that the underscores be used between words and we write everything in lower case. For example:
An Informal Introduction to Python - Basic Python mathematical and string operations. Read through 3, 3.1, 3.1.1 and 3.1.2 and try it out in your development environment.
Also read this article on string slicing.
1. Create a Python file which does the following:
2. Create a Python file which does the following:
3. Identify one reason you may want to use Modulus division.