< Prev  |  Home  |  Next >

Python Numbers

The first type of data that we will use in our programs are whole numbers. The posh, programming word for a whole number is an integer. Here’s an example of an integer: 5. Would you like another example? 22. We can use integers in Python like this:

Remember that you can use the shell to execute these commands, or save them in a file and run it.

Notice how the print statement just prints whatever expression is in the brackets to the screen, ignoring any spaces you typed. Also, just like in maths, notice the difference between the expression 3+2*2 and (3+2)*2.

We can do the something similar with decimal numbers too. In programming, we call decimal numbers floating point numbers, for reasons that I won’t go into right now. Here’s another program to try out. If you’re saving these commands in a file, you can delete what you’ve already written, or just add it below the other 3 lines.

Congratulations. Your £499 computer has been transformed into a £3.99 calculator.


Use integers and floating point numbers in a sum, any sum, that makes 14.5. And typing…

… doesn’t count! You’re better than that.


The following print statements:

should all print:
< Prev  |  Home  |  Next >