Skip to content

Commit 81f6b70

Browse files
author
Apress
committed
First commit
0 parents  commit 81f6b70

118 files changed

Lines changed: 3563 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

4118.pdf

901 KB
Binary file not shown.

4119.pdf

832 KB
Binary file not shown.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Print out a date, given year, month, and day as numbers
2+
3+
months = [
4+
'January',
5+
'February',
6+
'March',
7+
'April',
8+
'May',
9+
'June',
10+
'July',
11+
'August',
12+
'September',
13+
'October',
14+
'November',
15+
'December'
16+
]
17+
18+
# A list with one ending for each number from 1 to 31
19+
endings = ['st', 'nd', 'rd'] + 17 * ['th'] \
20+
+ ['st', 'nd', 'rd'] + 7 * ['th'] \
21+
+ ['st']
22+
23+
year = raw_input('Year: ')
24+
month = raw_input('Month (1-12): ')
25+
day = raw_input('Day (1-31): ')
26+
27+
month_number = int(month)
28+
day_number = int(day)
29+
30+
# Remember to subtract 1 from month and day to get a correct index
31+
month_name = months[month_number-1]
32+
ordinal = day + endings[day_number-1]
33+
34+
print month_name + ' ' + ordinal + ', ' + year
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Split up a URL of the form http://www.something.com
2+
3+
url = raw_input('Please enter the URL: ')
4+
domain = url[11:-4]
5+
6+
print "Domain name: " + domain
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Prints a sentence in a centered "box" of correct width
2+
3+
# Note that the integer division operator (//) only works in Python
4+
# 2.2 and newer. In earlier versions, simply use plain division (/)
5+
6+
7+
sentence = raw_input("Sentence: ")
8+
9+
screen_width = 80
10+
text_width = len(sentence)
11+
box_width = text_width + 6
12+
left_margin = (screen_width - box_width) // 2
13+
14+
print
15+
print ' ' * left_margin + '+' + '-' * (box_width-2) + '+'
16+
print ' ' * left_margin + '| ' + ' ' * text_width + ' |'
17+
print ' ' * left_margin + '| ' + sentence + ' |'
18+
print ' ' * left_margin + '| ' + ' ' * text_width + ' |'
19+
print ' ' * left_margin + '+' + '-' * (box_width-2) + '+'
20+
print
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Check a user name and PIN code
2+
3+
database = [
4+
['albert', '1234'],
5+
['dilbert', '4242'],
6+
['smith', '7524'],
7+
['jones', '9843']
8+
]
9+
10+
username = raw_input('User name: ')
11+
pin = raw_input('PIN code: ')
12+
13+
if [username, pin] in database: print 'Access granted'
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Print a formatted price list with a given width
2+
3+
width = input('Please enter width: ')
4+
5+
price_width = 10
6+
item_width = width - price_width
7+
8+
header_format = '%-*s%*s'
9+
format = '%-*s%*.2f'
10+
11+
print '=' * width
12+
13+
print header_format % (item_width, 'Item', price_width, 'Price')
14+
15+
print '-' * width
16+
17+
print format % (item_width, 'Apples', price_width, 0.4)
18+
print format % (item_width, 'Pears', price_width, 0.5)
19+
print format % (item_width, 'Cantaloupes', price_width, 1.92)
20+
print format % (item_width, 'Dried Apricots (16 oz.)', price_width, 8)
21+
print format % (item_width, 'Prunes (4 lbs.)', price_width, 12)
22+
23+
print '=' * width
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# A simple database
2+
3+
# A dictionary with person names as keys. Each person is represented as
4+
# another dictionary with the keys 'phone' and 'addr' referring to their phone
5+
# number and address, respectively.
6+
7+
people = {
8+
9+
'Alice': {
10+
'phone': '2341',
11+
'addr': 'Foo drive 23'
12+
},
13+
14+
'Beth': {
15+
'phone': '9102',
16+
'addr': 'Bar street 42'
17+
},
18+
19+
'Cecil': {
20+
'phone': '3158',
21+
'addr': 'Baz avenue 90'
22+
}
23+
24+
}
25+
26+
# Descriptive labels for the phone number and address. These will be used
27+
# when printing the output.
28+
labels = {
29+
'phone': 'phone number',
30+
'addr': 'address'
31+
}
32+
33+
name = raw_input('Name: ')
34+
35+
# Are we looking for a phone number or an address?
36+
request = raw_input('Phone number (p) or address (a)? ')
37+
38+
# Use the correct key:
39+
if request == 'p': key = 'phone'
40+
if request == 'a': key = 'addr'
41+
42+
# Only try to print information if the name is a valid key in
43+
# our dictionary:
44+
if name in people: print "%s's %s is %s." % \
45+
(name, labels[key], people[name][key])
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# A simple database using get()
2+
3+
# Insert database (people) from Listing 4-1 here.
4+
5+
labels = {
6+
'phone': 'phone number',
7+
'addr': 'address'
8+
}
9+
10+
name = raw_input('Name: ')
11+
12+
# Are we looking for a phone number or an address?
13+
request = raw_input('Phone number (p) or address (a)? ')
14+
15+
# Use the correct key:
16+
key = request # In case the request is neither 'p' nor 'a'
17+
if request == 'p': key = 'phone'
18+
if request == 'a': key = 'addr'
19+
20+
# Use get to provide default values:
21+
person = people.get(name, {})
22+
label = labels.get(key, key)
23+
result = person.get(key, 'not available')
24+
25+
print "%s's %s is %s." % (name, label, result)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# hello.py
2+
print "Hello, world!"

0 commit comments

Comments
 (0)