Introduction to Python

Welcome to the basic programming course. In this section you learn the basics of programming in Python 3:

  • How to write your first program
  • Variables and data types
  • Selection (if statements)
  • Iteration (loops)

We will work on two tasks: a maze game and a quiz.

Starting the Python shell

Assuming that you have Python installed you will want to get started! The video below demonstrates how to start to writing simple statements in Python. We are using Python 3 and using the Idle editor. In this first video we will show you how to send output to the screen using the keyword print and how to input data from the user using the keyword input. We also introduce the concept of a variable.

Saving your programs

In the last section, we used the Python shell to write statements and get an instant response.

However, we could not save any of the statements we wrote.

To write a simple program we need to open the text editor in Idle by selecting File then New Window. As an example, here is a simple first program which asks a user for their name and then greets them.

First Python program

The video below shows step-by-step how to create and run this program.

You can also format the output in a more efficient way that will be useful as you make more complex programs. This uses the format function and is shown in the next short video clip.


Exercises

Using the skills demonstrated above:

  • Write a program to input two messages and output them to a user.
  • Write a program to input two whole numbers, add them together and print the result to the screen.

Tip: When inputting numbers, use int(input()) instead of input() - more on this later.