Python Workbook 2
Python Workbook 2
Name
Created by D.Aldred
P a g e |1
KEYWORDS to complete
Time.sleep
\
Created by D.Aldred
P a g e |2
Save as simple
Create a program, add the time function to delay the closing of the window.
Save as simple2
Time Functions.
Python can do more with the inbuilt time functions. The time function needs to be imported first and then coded to print
or work with the data. The import function is as simple as import time; (note the semi colon).
ENTER THE FOLLOWING CODE:
import time;
x = time.time()
print x
What do you notice about the result, is it correct?
To get the current time you need to use the code, localtime
TRY THE CODE BELOW:
import time;
x = time.localtime(time.time())
print x
Yet we still do not have the time in the standard format. You can format any time as per your requirement, but simple
method to get time in readable format is asctime():
TRY THE CODE BELOW:
import time;
localtime = time.asctime( time.localtime(time.time()) )
print "Local current time :", localtime
A mouthful to remember but it shows you the programming required to display the time in a suitable format.
Calendar Functions.
Python also has a calendar function, it operates in a similar way to the time function. First import the calendar function
using import calendar. A lot simpler to remember than the time function.
Created by D.Aldred
P a g e |3
Can you work the code for the current month? Write your code below.
What happens if you add another number after the month, for example cal = calendar.month(2008, 1, 6)
Rounding up Numbers.
Ever wanted to round up a number? Probably not!
Use round if you want to round a number up for example,
Created by D.Aldred
round(3020.2323)
P a g e |4
Pass Blocks.
In simple terms a pass block passes or misses out an instruction if a condition is met. Basically it does nothing.
TRY THE CODE BELOW:
month = 3
if month == 1:
pass
elif month < 6:
print "early"
else:
print "late"
Try changing the month value to 10, what happens.
Can you create a program for all the numbers 1 - 1000 that looks to see if they are divisible by 6, and then
prints them if not pass. (Use the range function from book one)
Create a list called num with the numbers 1 to 10, then append with the numbers 11 to 20. Write your code
below.
Created by D.Aldred
Save as MyList
P a g e |5
Inserting
You may need to insert the data or the elements in a string for example a,b,c,d needs to be inserted with a,b,c,d,
example. To do this there are several methods,
TRY THE CODE BELOW:
li = ['a', 'b', 'c', 'd']
li.insert(4, "example",)
print li
You can also use the simple code of a[position of the data] =[data to be entered]
TRY THE CODE BELOW:
a = [1,2,3,4,]
a[1:1] = ['eggs', ham']
print a
(remember text needs to be entered using the whilst the numbers can just be added)
This inserts the word or data after the position ion the list. It places the word example after the fourth item in the list
Can you create a program that will add two more items to the list,
SaveasInsert
Create a program that will add two more items to the list and onto each line
SaveasLine
Extending a list
A list can be very easily extended. This means data is added to the list. In the example below the list consist of 1 and 2.
The extend adds 3, 4 and 5 to the list.
L = [1, 2]
L.extend([3,4,5])
print L
Created by D.Aldred
P a g e |6
Can you create a program that first prints the list of four items and then allows the user to add two items of
their choice to the list.
SaveasLister
Created by D.Aldred
P a g e |7
import time
import random
sub = random.randrange(101)
t = 10
sea = range(101)
print"
WELCOME TO THE ATLANTIC OCEAN"
time.sleep(1)
print " ~~_~ _~_ __~__~~_~ _~_ __~_~~~~_~_ _ ~~~"
time.sleep(1)
print "
~~_~ _~_ __~__~~_~ _~_ __~_~~~~_~_ _ ~~~"
print "
~~_~ _~_ __~__~~_~ _~_ __~_~~~~_~_ _ ~~~"
time.sleep(1)
print "
~~_~ _~_ __~__~~_~ _~_ __~_~~~~_~_ _ ~~~"
time.sleep(1)
print "You have 5 sonar depth charges to find the sub."
print "\nThe sea is divided into, 100, blocks, the sub will be hidden in one of these"
for x in sea:
print "~_~",
time.sleep(1)
while t > 0:
d = input("Please select the location of your depth charge")
print "Depthcharge number", t, "released"
time.sleep(1)
print "
oooo>>>"
if sub > d:
print "Captain, we need to try further to the right"
time.sleep(1)
print " We should move further over"
elif sub < d:
print "We should move further to the left Captain"
elif d == sub:
print "You sunk my battleship"
time.sleep(1)
print "
WELL DONE, YOU WIN"
sea[d]=['SUB LOCATED AND DESTORYED']
print sea
break
t=t-1
if t == 0:
print "
GAME OVER"
Created by D.Aldred
P a g e |8
Created by D.Aldred
P a g e |9
BONUS
Internet Access:
A Python program can be used to access the internet and search a website for data or a sentence. In the
example below you can search for the word school in your school website.
The first part of the program is to import the URL library 2 module with the command import urllib2. The next
step is to search for the data,
for line in urllib2.urlopen("http://fullwebaddress of site"):
if "word you are searching for in line:
print line
What does the program do?
Created by D.Aldred
P a g e | 10