Answers Document (LTPI Python)
Answers Document (LTPI Python)
Answers Document (LTPI Python)
Copyright
© 2017 PG Online Limited
The contents of this pack are protected by copyright. No part of the materials distributed with
this unit may be reproduced, stored in a retrieval system, or transmitted, in any form or by
any means, electronic or otherwise, without the prior written permission of PG Online
Limited.
01227 342567
3. (a) >>> 40 // 11
3
(b) >>> 40 % 11
7
(c) >>> 2**10
1024
(d) >>> "three" > "two"
False
(e) >>> "abc" < "ABC"
False
(f) >>> 1<=4 and 7<=7
True
(g) >>> "Fred" != "fred"
True
Exercises
1. See Python program Ch 2 Exercise 1 string methods.py
2. See Python program Ch 2 Exercise 2 radius and circumference.py
3. See Python program Ch 2 Exercise 3 rounding floating point numbers.py
Exercises
1. See Python program Ch 3 Exercise 1 area of square or rectangle.py
2. See Python program Ch 3 Exercise 2 menu of subject choices.py
3. See Python program Ch 3 Exercise 3 throw two dice.py
4. See Python program Ch 3 Exercise 4 discount on goods purchased.py
5. See Python program Ch 3 Exercise 5 car parking charges.py
Chapter 4 – Iteration
In-text questions
Q2: The maximum result would be correct so long as all the results are 100 or less. The
minimum result would be given as -1.
See Python program Ch 4 Question 2 max and min error.py
Q4: See Python program Ch 4 Question 4 max and min no results entered.py
Exercises
1. See Python program Ch 4 Exercise 1 10 random numbers.py
Q1: The program will crash with an “index out of range” error
Q5: monthlyAvg[10][2]
Q6: print(monthlyAvg[11])
Q7: (a) 12
(b) 3
Exercises
1. (a) alist =[]
(b) alist.append(“apple”)
(c) blist = [0] * 20
(d) clist = alist + blist
(e) lastC = clist.pop()
See Python program Ch 5 Exercise 1 list operations.py
Exercises
1. See Python program Ch 6 Exercise 1 theatre tickets.py
Q1: print(studentMarks[“Robina”])
Q2: studentMarks[2][0]
Exercises
1. See Python program Ch 7 Exercise 1 anagrams.py
Exercises
1. See Python program Ch 8 Exercise 1 cube function.py
Q1: (a) See Python program Ch 9 Question 1 read and print temperature file.py
(b) London,7,1200,
Accra,30,1200,
Baghdad,20,1500,
Winnipeg,-12,0600,
New York,14,0700,
Nairobi,27,1500,
Sydney,22,2300,
Q3: a = 3
b = 4
c = a + b
d = a * b
print("%d + %d = %d" %(a,b,c))
print("The product of %d and %d is %d" %(a,b,d))
Exercises
1. See Python program Ch 9 Exercise 1 create or append to student file.py
3. UPDATE tblScores
SET score1 = 87, score2 = 79, score3 = 63
WHERE playerID = “MF123”
4. SELECT score1
FROM tblScores
where playerID = “MF123”
5. SELECT *
FROM tblScores
where playerID = “MF123”
Exercises
1. As it stands, the program adds 1 to the variable passwordChecks each time a valid
lowercase or uppercase letter is detected. It also adds 1 to passwordChecks if the
password length is within the acceptable range. However, it will accept any password
with at least 3 valid upper- or lowercase letters.
There needs to be separate flags for each condition, i.e. between 8 and 15 letters, an
uppercase letter, a lowercase letter, and a number.
2. The program works correctly if the correct password is entered on the first attempt.
If the user enters an incorrect password, the statement
input("Password incorrect - please re-enter: ")
is executed. However even when the correct password is entered, it is not accepted.
Artwork