In this tutorial, we’re going to start to work with the basic types of Python: strings (for text) and integers & floats (for numeric values).
Notice that in this tutorial you are working in REPL (IDLE). You can find more information
on REPL and how to start Python in your
terminal in the Installing Python tutorial .
In keeping with tradition, we’re going to start by printing “Hello, World!” to
the console. In Python, the function to achieve this is aptly named
Type the following next to the
The REPL will simply print the text right back at you.
Now, print your name, and experiment a little!
Something else interesting about the
print() function is that you can pass it
multiple arguments to print:
>>> print("Hello", "Goodbye")
In fact, you can pass as many things into print as you like:
>>> print("one", "two", "three", "four", "five", "six", "seven", "eight", "nine")
Of course, you could also get the same output with this:
>>> print("one two three four five six seven eight nine")
It’s up to you to decide what’s appropriate and when.
Everybody’s favourite pastime.
Python can do simple arithmetic. To start, let’s try addition:
>>> 5 + 7
You should now see the result of that calculation in your REPL.
Subtraction, multiplication and division work the same way.
>>> 6 - 2 >>> 8 * 4 >>> 9 / 3
Now try a few more to see what results you get. Try your hand at all of the
basic mathematical operators:
** (exponents) and
Being able to only perform one operation at a time is pretty limiting, so Python allows us to combine mathematical operations. Try this one:
>>> 9 * 4 - 6
Now try a few more. You can combine as many operations as you like.
Did you notice any unexpected results when you started combining operations? If you didn’t, try this:
>>> 10 - 2 * 4
Python follows the traditional mathematical rules of precedence, which state that multiplication and division are done before addition and subtraction. (You may remember BODMAS.) This means in our example above, 2 and 4 are multiplied first, and then the result is subtracted from 10.
We can change the order of operations by using parentheses. Anything inside parentheses is executed first.
Now try it like this:
>>> (10 - 2) * 4
You should have a different answer.
Because of precedence rules, complex operations such as our first example can be quite confusing to read. If you find yourself writing more complex expressions, there is no harm in adding parentheses for clarity.
One of the things that tends to confuse newcomers to programming in general is the concept of floating point numbers. Basically, numbers with decimal points tend to behave a little strangely when you’re performing mathematical operations on them. The reasons for this are complex and rooted in the nature of computing itself, so for now, let’s just go with the understanding that weird things happen with decimal numbers.
To see an example of this, try dividing
>>> 10 / 3
The answer should go on forever, but it doesn’t. Now try something a little more precision-sensitive:
>>> 1.000000000000001 * 8
Probably not what you’d expect right? For now, you’ll just have to accept this as a limitation, and later on you’ll learn how other programmers work around it.
Now let’s combine what we have learnt today. We can tell
print() to print
multiple things at once, separated by a comma:
>>> print('The result of 2 + 2 is', 2 + 2)
Saving Your Work
In this tutorial you coded in the REPL (IDLE), but a lot of times you want to save your code instead. In such cases you can save your code to a file using a text editor. We give some information on text editors in our Getting started guide .
Open your text editor and write the code from the first exercise:
Save the file as
ex1.py. You can name your files as you like,
but they should end with
.py, so python can read them easily.
Reading your file into Python you will use your
terminal shell again.
You can read your file with the following command (type without the $ sign):
$ python ex1.py
If you have
>>>in front of your code, you are still in REPL(IDLE) and need to exit
Then you should be able to load the file.
This concludes today’s tutorial. In the next tutorial, we’ll find out how to combine the results of multiple separate expressions using variables, get input from the user, and make decisions based on that information.
We recommend watching Gaurav Pandey’s How to Get Started with Python video from codebar Festival 2022 over on our YouTube.
There is a very good introductory article in Google Developers Guide.