Python Variables: Numbers

The programs you write work by storing things in memory. These storage areas are called variables. You then manipulate these storage areas, variables, and do something with them. Let's get right to it. We'll tackle Python number variables first before doing string variables in a later lesson.

Keep open the project you already have from the previous section. If you have closed PyCharm down, it should start up with the last project you were working on displayed. If it doesn't, you may see this screen:

Opening PyCharm screen

The area on the left shows you the project you have already created. Click the name of a project to open it up.

If you haven't yet created a Python project, then click on Create New Project. You'll then see this:

Naming your Python project

The untitled on the end of the location box should be changed to something more appropriate. It will be used as the name of a folder where the project files will be stored. Click the Create button to create the folder and project.

Another way to create a new project is from PyCharm itself, when you already have a project open in the IDE. From the menu bar at the top, click File > New Project:

The new project menu in PyCharm

When you click New Project, you'll see the same dialogue box as above.

So, variables.

In most other languages, variables are set up with the kind of information they are going to store. For example, in Java, if you want to store a whole number, you'd need an int variable (short for integer):

int firstNumber;

In the code above, the name of the storage area is firstNumber. You'd then use this name to get at whatever number you later store under this variable name. If you wanted to store some text, you'd set up a string variable:

string someText;

The name of the variable this time is someText. It can only hold a string of text, and not numbers. Likewise, the int variable could only hold numbers and not text.
In Python, however, you don't need to worry about the type of information that's going to be stored in your variable. All you need is the variable name, followed by whatever you want to store under that name:

firstNumber = 10
someText = "Kenny"

Variable names are something you come up with, and you can call them just about anything you like. There are few rules, though:

  • Variable names can't start with a number
  • Variable names can't contain spaces
  • Variable names must start with a letter or an underscore character ( _ )
  • You can't have a reserved word for your variable names

These four variable names are fine:

firstNumber
_firstNumber
first_Number
first_number

They all start with either a letter or an underscore character.

This variable name is not OK:

first Number

The variable name above contains a space, so you'll get an error.

This name is also not OK:

for

There is a type of loop called a for loop, so Pythons needs this word for itself. It won't let you use it as a variable name. Unless it's forFirstNumber. This would be OK, because it's not the word for by itself.

Let's set up a variable and print out whatever is stored inside of it.

In your main coding window, delete the "Hello World" print statement, if you have one. Instead, type the following:

firstNumber = 10

You should have this:

Python number variable in code window

The equals sign is used to store values in your variable names. It's known as the assignment operator. To the right of the equals sign, you type whatever value you want in your variable. To the left of an equals sign, you have the name of your variable. So, the variable that we have called firstNumber is going to hold a value of ten. We can print this out.

Press the enter key on your keyboard to get a new line. Now type a letter p. PyCharm will present you with a popup menu of choices:

Code completetion in PyCharm

We want to print something to the screen, so select that choice by either double clicking print, or pressing the tab key on your keyboard. PyCharm will complete the code for you. At least, it will add the word print followed by a pair of round brackets. In the previous section, we added some direct text between the round brackets surrounded by either single quotes or double quotes. But you can also add a variable name, here, as well as the text.

Inside of the round brackets type "My first number is ". Then type a comma. After a space, type your variable name, firstNumber. So this:

print("My first number is", firstNumber)

And it should look like this in your coding window:

Python code to print out a variable

We can run this code now.

Either right-click in the coding area and select Run 'HelloWorld' from the menu that appears. Or click on Run from the menu at the top of PyCharm and select Run.

You should see the output window appear at the bottom of PyCharm:

A Python output window

The black text is your code. You can ignore the purple text, unless the last line is showing you an error. If it is, make sure you have the round brackets, the quote marks, and the comma in the right place.

Incidentally, if you want to change the colours for your output window, you can do so. After all, not everybody like purple and black!

To change the colours, click on File at the top of PyCharm. From the File menu, select Settings. When the Settings screen appears, expand the Editor item on the left. Under Editor, expand Color Scheme, then click on Console Colors:

Setting a color for the output window

Expand the Console item in the main Settings area, just under ANSI Colors. Under Console, select System output. This is where you can change the purple colour. Click the colour rectangle, the one to the right of Foreground. Select any colour you like.

Now click on Standard output. This is the black text from the output window. Again, change this to anything you like. You can even check the Bold and Italic boxes, if you like.

Before clicking OK at the bottom, notice that there is a Font item under the Editor item on the left. Select this to see the following screen:

Setting a font for the output window

From here, you can change the size of the font that you see in the main coding area. If yours is too small for you, increase the size. You can also choose a different font and increase the line spacing.

If you look at our output window, we have chosen a light grey for the system output, and black bold for the standard output:

Python Output window with font and color changed

OK, try these exercises.

Python Exercise

Print out the number 25 instead of the number 10

Python Exercise

Use Python code to print out the following:

One half of 100 is 50.

Use a variable to print out the number 50

In the next lesson, we'll continue with Python number variables, and have a look at comments in Python.

Python Comments, More on Variables >