Python Programming for Beginners (2015)
Chapter Three: Storing and Processing Data
In this chapter, you will learn:
· What variables are and how to use them
· What operators are and how they work
One of the most important parts of any computer program is the way that it stores data and processes data. This is all of the important work in the background which makes your programs work properly.
To store and process data in your programs you will use variables and operators. If you are a programmer then you will know what these are, but if you're not then you are just about to find out.
What Are Variables?
You will store any data you need to use in your programs by using variables. For example, if you want to store the name of the user in your program you would use a variable to do so. Before you use a variable you need to tell the computer to set aside some space to store the data in. This is called defining a variable. You also need to set a value to your new variable. You do that with this statement:
user_name = 'Richard'
You may be wondering what the equals sign (=) is doing in the above statement. In maths = is used to define where the answer should go and you would probably expect to see it at the end of a question. In programming the equals sign is used to assign a value to the variable listed before it. If you write the following details into your IDLE editor:
#Defines the variable user_name
user_name = “Richard
#Displays the variable user_name on the screen
print (“Name = ”, user_name)
Now save and run the program and you will see exactly how this works.
You can also define more than one variable in one go. So rather than defining the variables on two separate lines like this:
user_name = “Richard”
user_age = 25
You could use the following statement:
user_name, user_age = “Richard”, 25
Which saves space when you are writing your new program.
When you name your variables you must use letters, numbers or underscores and the first character must be a letter. The variables are case sensitive, so you could have both user_age and User_Age... but I wouldn't recommend it! Imagine how complicated it would get.
You are also not allowed to use any variables which already have a function in Python. So print, if and for are all out of the question.
Most programmers will use different naming conventions when it comes to creating variables to make the code easier to read – especially if you are coding with others. The naming conventions I use are underscores, so all of my variables are created with an underscore in between the words. For example, user_name.
There are other naming conventions but this is the one we will use throughout the book. If you are an experienced programmer than you can feel free to stick to the naming conventions that you have used in the past.
What are yhe Different Basic Data Types
In the last section of of this chapter we used two data types, these were integer and string.
An integer is a whole number with no decimal parts. In the last section this was user_age. If you define a number without a decimal point and without quotation marks it will be an integer. For example, user_age = 25.
A string is simply text. In the last section this was user_name. If you put a number in quotes then it will be defined as a string and will not work as a number. For example, user_age = “25”. We will go through some string operators in the next section.
Finally, there is a third data type called float, which is a number which has decimal parts. We have a float in the last section, but this would be numbers like 2.14 or 0.01. For example, user_age = 25.5.
Going back to assigning variables, it is also possible to assign a variable to the value of another variable. So if you write the following program:
x = 0
y = 5
print (“x = ”, x)
x = y
print (“x = ”, x)
print (“y = ”, y)
Then save and run the program you will find that the x variable is 0 the first time it is displayed, then has turned into 5 by the time it is displayed the second time. When the y variable is printed it retains the same value even though its value has also been taken up by the x variable.
If you switch the x = y to y = x you will find that the y is then changed to 0 at the end of the program.
Many of the main operators are mathematical equations which you can carry out using your variables. The basic operators you can use are +, -, *, /, //, % and **. These symbols are used for addition, subtraction, multiplication, division, floor division, modulus and exponent.
Most of these are self explanatory, but a few will require some extra explanation:
Floor division is where you divide the numbers then round the answer down to the nearest whole number.
Modulus is where you give the remainder when the two numbers are divided.
Finally, exponent is the first number to the power of the second number. If you were performing 10 ** 3 your answer would be 10 to the power of 3.
I'll give a few examples of these operations below (in these examples x = 10 and y = 3):
x + y = 13
x - y = 7
x * y = 30
x / y = 3.3
x // y = 3
x % y = 1
x ** y = 1000
You can use these different operators to assign values to different variables. If we take addition as an example you would use the following piece of code:
#assigns the answer of x + y to x
x += y
So x += y works the same way as x = x + y. This works for all of the operators listed above.