Python Comments, Variable Practice


We'll get to Python comments in a moment. Let's have some more variable practice, though.

In the previous lesson, you set up a variable that held one value. But you can have more than one value to the right of your equals sign. In fact, you can have as many as you like. Let's add two numbers together and print out the answer.

Hit the enter key on your keyboard to start on a new line in your code from the last lesson. Now set up a new variable and store the number 7 in it:

myTotal = 7

To add numbers, you use the plus symbol ( + ). So, type a plus then the number 5:

myTotal = 7 + 5

Your coding window should look like this:

Python code with two variables

Now add a print line:

print("My addition =", myTotal)

(NOTE: Don't copy and paste from here because you'll get the wrong type of quotes marks for Python. Plus, you learn far more from typing the code out for yourself than you do from just copying and pasting.)

The code in PyCharm looks like this:

Python code printing out two variables

Now run your code. The output should be this (don't worry about the blank lines at the end of file message):

Python Output window

Python comments

You could delete the first two lines from your code, the ones producing the output "My first number is 10". Or you can add a Python comment. This will turn the lines grey in PyCharm, and they will be ignored. To add a comment to a line, simply type a # symbol before them, then a space:

# This is a comment.

NOTE: The space is not strictly necessary, and other IDEs, like Visual Studio, don't care if you add one or not.)

NOTE: Comments are used extensively for documentation purposes, for example explaining what a particular function or class does.

Here's our code again, this time with the first lines commented out:

Comments added to Python code

When you run your code with the comments added, only the final print statement will be displayed in the output window.

You can add other numbers after the 5. As many as you need. Try this:

myTotal = 7 + 5 + 8 + 9

Run your code again to get an answer of 29.

You can use the other math symbols as well, of course. The math symbols are these:

+ (Add)

- (Subtract)

* (Multiply)

/ (Divide)

Try them out. Change the myTotal line to this, first:

myTotal = 7 - 5

Change the print line to print("The answer is:", myTotal)

Then to this:

myTotal = 7 * 5

Then to this:

myTotal = 7 / 5

Notice for the final one, you should have this in your output window:

A floating point value in Python

Now we have a floating-point value, rather than a whole number (integer).

In Python, you don't have to do anything special to set up floating-point value. You can have this inside a variable:

myNumber= 7.5

Or this:

myNumber= 7.598456

If you don't want the point something at the end, you can round down by surrounding your number with the round method:

myNumber= round(7.598456, 2)

The two at the end, after a comma, means you want to round to a maximum of two decimal places. If you miss out the comma and the number then Python will round up or down for you:

myNumber= round(7.598456)

This will produce an answer of 8, because Python will round up.

If you're dividing, you can use two forward slashes to do floor division:

myTotal = 7 // 5

This will get you the answer of 1 rather than the 1.4 we had before.

Round brackets

Sometimes, you have to give Python a helping hand when it comes to maths. For example, change your myTotal line to this:

myTotal = 8 + 2 * 10

Starting on the right of the equals sign, this says add 8 and 2 then multiply by 10. Once Python has worked out the answer to that, it will store that answer in your myTotal variable.

Try it out. Run your code and you should find that the output is this:

The answer is: 28

Clearly, something went wrong. If you add 8 and 2, you get 10. 10 times 10 is 100. Which is the answer we wanted. So why did we get 28?

The reason is down to something called Operator Precedence. The operators are the plus, minus, divide and multiply symbols. Python, and most other programming languages, will see division and multiplication as taking priority (precedence) over addition and subtraction. It will do multiplication before it does addition. In our sum, the 2 gets multiplied by the 10 first, then 8 gets added to that answer.

You can tell Python what you mean with round brackets, parentheses. Add two round brackets to your code:

myTotal = (8 + 2) * 10

Run your code again and the output will be this:

The answer is: 100

Likewise, if we had this:

myTotal = 8 + 10 / 2

The answer would be 13. It's 13 because the 10 is divided by the 2 first to get 5. The 5 is then added to the 8 to make 13. But if we add round brackets:

myTotal = (8 + 10) / 2

The answer is now 9. The brackets force Python to do the sums in the parentheses and then divide by 2.

So just be aware that if you're getting the wrong answer in your sums, you may need to use parentheses. Try these exercises to get the hang of using round brackets.

Python Exercise

Type these numbers after the equals sign for your myTotal variable:

myTotal = 4 + 2 * 8 - 6

Can you see why the answer you get when you run your code is 14?

Python Exercise

Use round brackets with the numbers above to get an answer of 8

Using Python Variables >