Python Sample Program
Python Sample Program
Programming Exercises
in
Python Programming Unit 1 - Input and Output
We are going to create the program which asks you what your name is and then says hello to
you, using the name you entered.
Start Python, inside the Python Shell go to File and select New File. This stage is very
important. By creating your program in a new file, it lets you save your code so you can edit it
or come back to it later. Write the code shown below.
Your program will be checked to see if you made any mistakes when typing the
program. If it finds a mistake, such as a spelling mistake or missing bracket or speech
mark, the program will not run and instead you will see an error message displayed
indicating what it thinks is wrong and on which line – but beware, sometimes the error
can be on the line before! If you get an error message you must fix it and then save
the program again (Ctrl + s) before pressing F5 to run it
1
Python Programming Unit 1 - Variables
We are going to create the program which asks you what your name and favourite colour is
and then says hello to you, using the name you entered, and tells you what colour you entered.
Inside the Python Shell go to File and select New File. Write the code shown below.
#Using Variables
#Your name
#Today’s date
Name = “”
Colour = “”
print('What is your name? ')
name = str(input())
print('What is your favourite colour? ')
colour = str(input())
print('hello ', name, ' your favourite colour is ', colour)
Run the program and enter your name and favourite colour.
2
Python Programming Unit 1 – Arithmetic Operations
In the example shown here you are being asked how many hours per week you work and how
much you get paid per hour.
The program then works out how much you earn in one week from the information given.
Inside the Python Shell go to File and select New File. Write the code shown below.
#Arithmetic Operations
#Your name
#Today’s date
hours = 0
rateOfPay = 0.0
weeklyPay = 0.0
print('How many hours do you work? ')
hours = int(input())
print('What is your rate of pay? ')
rateOfPay = int(input())
weeklyPay = hours * rateOfPay
Run the program and test it is working, enter 30 hours and £8 rate of pay.
Program 1
Write a program that asks the following questions:
What is the price of a train ticket from Glasgow to London? How many tickets do you require?
3
The program then reports back how much this is going to cost in total.
Program 2
Write a program that asks the following questions:
How many classrooms are in the school? What is the maximum number of pupils who can fit
into one classroom?
The program then reports back the maximum number of pupils the school can accommodate.
Program 3
Write a program that asks the following questions:
How many pupils are in the lunch hall? How many boys are in the lunch hall?
The program should then report back how many girls are in the lunch hall.
Program 4
Write a program that asks the price of an air flight to Australia.
The program should then report back how much this will cost if the person travelling is allowed
20% discount.
4
Python Programming Unit 2 – Keeping a running total
In this example we are going to start using a variable to keep a total. The idea here is that
every time a new value is entered the total increases by that value.
A simple example is a game of darts, where a player is given three darts and the total is the
sum of the three scores. Enter
Inside the Python Shell go to File and select New File. Write the code shown below.
#Initialise variables
total = 0
number1 = 0
number2= 0
number3 = 0
print('What is your first score? ')
number1 = int(input())
total = (total + number1)
print('What is your second score? ')
number2 = int(input())
total = (total + number2)
print ('What is your third score? ')
number3 = int(input())
total = (total + number3)
Run the program and test it is working, enter the following dart score: 80, 19, 85
5
Select two of the following programs to complete. Save the programs in
your Python folder as Task 7 and Task 8.
Program 1
Write a program that asks two people what their ages are.
The program then reports back what their total age is.
Program 2
Write a program that asks four people what their scores in an archery tournament were.
The program then reports back what their total score was.
Program 3
Write a program that asks the price of apples, bananas, cherries, grapes and pears.
The program then reports back what the total of these five items is.
Program 4
Write a program that asks three people their height in centimetres.
The program then reports back what their total height is.
6
Python Programming Unit 3 – Selection with IF
In this example we are going to start writing programs that make decisions.
A sample program is where you are asked a question and the computer replies to your answer.
For example, the program on this page asks you to enter a name and an age. If the age
is less than 18 the program sends the message ‘You are not old enough to vote’.
Inside the Python Shell go to File and select New File. Write the code shown below.
#Selection with IF
#Your name
#Today’s date
Name = “”
Age = 0
print('What is your name? ')
name = str(input())
print('What is your age? '))
age = int(input())
if age < 18:
print(name, 'you are not old enough to vote')
Test the program by entering your name and age, the program should display the message.
Now test the program by entering ‘Jane’ and 21, the program does not display a message.
7
Select two of the following programs to complete. Save the programs in
your Python folder as Task 10 and Task 11.
Program 1
Write a program which asks you what your score in a Computing test out of 30 is (The pass
mark is 15).
Program 2
Write a program which asks you to enter your age.
The program then reports back whether you are a senior pupil (age >=16 and age <=18)
Program 3
Write a program which asks you to enter 3 marks between 1 and 30, calculates the total then
works out the average of the marks. average = total/3
The program then reports back if your marks are above average (45).
Program 4
Write a program which asks you to enter 4 amounts of money between £1 and £50. The
program adds up these numbers.
The program then reports back that you get 20% discount if the total is greater than £35.
8
Python Programming Unit 4 – Selection with If - Else
A sample program is where you are asked a question and the computer replies to your answer.
For example, the program on this page asks you what the capital of Spain is. If you
guess correctly the computer says ‘Well Done’, if not the computer tells you ‘That is not
Correct’.
Inside the Python Shell go to File and select New File. Write the code shown below.
Test the program by entering madrid, the program should display ‘Well done!’
Run the program again this time enter a wrong answer, the program should display ‘That is
not correct’.