Python training exercise 0

From BITS wiki
Jump to: navigation, search

Introduction

A program needs information (input) to run, and then needs to export its results so that you know what happened (output). The easiest way to do this is to send a 'text message' to the screen; this is possible with the print command which we will introduce here.

In this section we also discuss some basics of Python syntax, and the errors that occur if you don't get it right.

Exercises

Writing a message

The print command allows you to write data to the console screen. Try the following example:

print("Hello world!")

This line writes the message 'Hello world!' to your shell/console window. The quotation marks around the text indicate that this is a string - it is not part of the Python language but just a piece of text.

Writing numbers

You can also print out numbers as text messages to the screen. You do not need quotation marks in this case; just the number is enough!

If you number does not have a decimal point (.) in it, it is called an integer, if it does have a decimal point, it is a float.

print(5)
print(3.1415)

Note

In Python programs you will often see a first line like this:

#!/usr/bin/python

This line tells the operating system where it can find the Python language interpreter so it can run the program without you having to specify where Python is. We do not need it when using IDLE, so you can leave it out.

Back to main page