Welcome to the second part of our introduction to Python. If you haven’t done so already, you take a look at part 1 here.
In part 2, we are going to look at variables and some basic operators.
A simple description of a variable is something that can hold a value and that value may/can change over time. At any point, you can either give it a new value to store or you can retrieve its value and use it for some purpose. Typically, the type of value variables store would include numbers and strings (pieces of text).
Naming the variable
The first part of creating a variable is to give it a name. There are some basic rules about what you can call a variable but naming it with a meaningful name really helps. For instance, naming a variable X, doesn’t mean much when you read your code but naming a variable, age, clearly tells you the intent of the value it’s storing.
The general rules for naming a variable are:
- Must begin with a letter (a – z, A – B) or underscore (_)
- Other characters can be letters, numbers or _
- All names are case sensitive
- Can be any (reasonable) length
Assigning a value
The next step is to assign a value to the variable. To do this we use, =, this is the assignment operator.
- For example, if you add this line to a new Python file:
age = 21
Here we have assigned the value 21 to a variable called age. The value you want to assign is always on the right of the = sign and the variable itself on the left.
Now, anytime we refer to age, we actually get the value 21.
For instance, if we use the print command, I could print the value of a variable out.
- Add this line to print the value of age out
Different value types
Typically, there are a few different types of values we can store with a variable. These tend to be either numerical values, including integers and floating point numbers and strings.
- This example would store an integer
score = 1099 print score
- This example would store a floating point number
pi = 3.1415927 print pi
- This example would store a string
name = "Han Solo" print name
We also have a type called a Boolean. A Boolean can only store one of two values. Those values being True and False. The capitalisation is important.
- Add this example to create a boolean
gameOver = True isItRaining = False
Basic arithmetic operators
Within Python, we have some basic operators to calculate values. This includes addition, subtraction, multiplication and division.
- To perform an addition we use the + operator
- To perform a subtraction we use the – operator
- To perform a multiplication we use the * operator
- To perform a division we use the / operator
We can mix and match these to create more complex calculations.
- To add two numbers together input this:
result = 5 + 10 print result
- This example uses multiplication to calculate a VAT
totalPrice = 100 * 1.2 print totalPrice
You can even use other variables in the calculation
- For instance, this example adds two variables together and assigns the result to a new variable
score1 = 90 score2 = 95 totalScore = score1 + score2 print totalScore
You can even use the addition operator to join two strings together
- This example takes two strings, joins them and assigns the result to a new variable. Notice we add an extra space between them.
firstName = "Luke" surName = "SkyWalker" fullName = firstName + " " + surName print fullName
Just as you learn in school, multiplication and division has priority over addition and subtraction.
- For instance, if I wanted to find the average of 3 numbers, I would want to add them up and then divide by 3. If I do it this way:
score1 = 90 score2 = 95 score3 = 100 average = score1 + score2 + score3 / 3 print average
I would get the wrong result, as this would perform the division on score3 first and then perform the two other additions afterwards.
By using brackets, I can prioritise what I want to be done first.
- This would now work correctly
score1 = 90 score2 = 95 score3 = 100 average = ( score1 + score2 + score3 ) / 3 print average
This is the end of part 2. Hope you enjoyed it. Read part 3 here.