Skip to content

Commit 585db41

Browse files
committed
First upload of source-code files for our book "Python for Programmers"
Source-code and related files for our book's examples.
1 parent 58739c0 commit 585db41

File tree

649 files changed

+88591
-0
lines changed

Some content is hidden

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

649 files changed

+88591
-0
lines changed

examples/ch01/RollDieDynamic.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# RollDieDynamic.py
2+
"""Dynamically graphing frequencies of die rolls."""
3+
from matplotlib import animation
4+
import matplotlib.pyplot as plt
5+
import random
6+
import seaborn as sns
7+
import sys
8+
9+
def update(frame_number, rolls, faces, frequencies):
10+
"""Configures bar plot contents for each animation frame."""
11+
# roll die and update frequencies
12+
for i in range(rolls):
13+
frequencies[random.randrange(1, 7) - 1] += 1
14+
15+
# reconfigure plot for updated die frequencies
16+
plt.cla() # clear old contents contents of current Figure
17+
axes = sns.barplot(faces, frequencies, palette='bright') # new bars
18+
axes.set_title(f'Die Frequencies for {sum(frequencies):,} Rolls')
19+
axes.set(xlabel='Die Value', ylabel='Frequency')
20+
axes.set_ylim(top=max(frequencies) * 1.10) # scale y-axis by 10%
21+
22+
# display frequency & percentage above each patch (bar)
23+
for bar, frequency in zip(axes.patches, frequencies):
24+
text_x = bar.get_x() + bar.get_width() / 2.0
25+
text_y = bar.get_height()
26+
text = f'{frequency:,}\n{frequency / sum(frequencies):.3%}'
27+
axes.text(text_x, text_y, text, ha='center', va='bottom')
28+
29+
# read command-line arguments for number of frames and rolls per frame
30+
number_of_frames = int(sys.argv[1])
31+
rolls_per_frame = int(sys.argv[2])
32+
33+
sns.set_style('whitegrid') # white backround with gray grid lines
34+
figure = plt.figure('Rolling a Six-Sided Die') # Figure for animation
35+
values = list(range(1, 7)) # die faces for display on x-axis
36+
frequencies = [0] * 6 # six-element list of die frequencies
37+
38+
# configure and start animation that calls function update
39+
die_animation = animation.FuncAnimation(
40+
figure, update, repeat=False, frames=number_of_frames, interval=33,
41+
fargs=(rolls_per_frame, values, frequencies))
42+
43+
plt.show() # display window
44+
45+
46+
#**************************************************************************
47+
#* (C) Copyright 1992-2018 by Deitel & Associates, Inc. and *
48+
#* Pearson Education, Inc. All Rights Reserved. *
49+
#* *
50+
#* DISCLAIMER: The authors and publisher of this book have used their *
51+
#* best efforts in preparing the book. These efforts include the *
52+
#* development, research, and testing of the theories and programs *
53+
#* to determine their effectiveness. The authors and publisher make *
54+
#* no warranty of any kind, expressed or implied, with regard to these *
55+
#* programs or to the documentation contained in these books. The authors *
56+
#* and publisher shall not be liable in any event for incidental or *
57+
#* consequential damages in connection with, or arising out of, the *
58+
#* furnishing, performance, or use of these programs. *
59+
#**************************************************************************

examples/ch02/fig02_01.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# fig02_01.py
2+
"""Compare integers using if statements and comparison operators."""
3+
4+
print('Enter two integers and I will tell you',
5+
'the relationships they satisfy.')
6+
7+
# read first integer
8+
number1 = int(input('Enter first integer: '))
9+
10+
# read second integer
11+
number2 = int(input('Enter second integer: '))
12+
13+
if number1 == number2:
14+
print(number1, 'is equal to', number2)
15+
16+
if number1 != number2:
17+
print(number1, 'is not equal to', number2)
18+
19+
if number1 < number2:
20+
print(number1, 'is less than', number2)
21+
22+
if number1 > number2:
23+
print(number1, 'is greater than', number2)
24+
25+
if number1 <= number2:
26+
print(number1, 'is less than or equal to', number2)
27+
28+
if number1 >= number2:
29+
print(number1, 'is greater than or equal to', number2)
30+
31+
##########################################################################
32+
# (C) Copyright 2019 by Deitel & Associates, Inc. and #
33+
# Pearson Education, Inc. All Rights Reserved. #
34+
# #
35+
# DISCLAIMER: The authors and publisher of this book have used their #
36+
# best efforts in preparing the book. These efforts include the #
37+
# development, research, and testing of the theories and programs #
38+
# to determine their effectiveness. The authors and publisher make #
39+
# no warranty of any kind, expressed or implied, with regard to these #
40+
# programs or to the documentation contained in these books. The authors #
41+
# and publisher shall not be liable in any event for incidental or #
42+
# consequential damages in connection with, or arising out of, the #
43+
# furnishing, performance, or use of these programs. #
44+
##########################################################################

examples/ch02/fig02_02.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Fig. 2.2: fig02_02.py
2+
"""Find the minimum of three values."""
3+
4+
number1 = int(input('Enter first integer: '))
5+
number2 = int(input('Enter second integer: '))
6+
number3 = int(input('Enter third integer: '))
7+
8+
minimum = number1
9+
10+
if number2 < minimum:
11+
minimum = number2
12+
13+
if number3 < minimum:
14+
minimum = number3
15+
16+
print('Minimum value is', minimum)
17+
18+
##########################################################################
19+
# (C) Copyright 2019 by Deitel & Associates, Inc. and #
20+
# Pearson Education, Inc. All Rights Reserved. #
21+
# #
22+
# DISCLAIMER: The authors and publisher of this book have used their #
23+
# best efforts in preparing the book. These efforts include the #
24+
# development, research, and testing of the theories and programs #
25+
# to determine their effectiveness. The authors and publisher make #
26+
# no warranty of any kind, expressed or implied, with regard to these #
27+
# programs or to the documentation contained in these books. The authors #
28+
# and publisher shall not be liable in any event for incidental or #
29+
# consequential damages in connection with, or arising out of, the #
30+
# furnishing, performance, or use of these programs. #
31+
##########################################################################
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# 2.2 Variables and Assignment Statements"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": null,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"45 + 72"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": null,
22+
"metadata": {},
23+
"outputs": [],
24+
"source": [
25+
"x = 7"
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": null,
31+
"metadata": {},
32+
"outputs": [],
33+
"source": [
34+
"y = 3"
35+
]
36+
},
37+
{
38+
"cell_type": "markdown",
39+
"metadata": {},
40+
"source": [
41+
"### Adding Variable Values and Viewing the Result"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": null,
47+
"metadata": {},
48+
"outputs": [],
49+
"source": [
50+
"x + y"
51+
]
52+
},
53+
{
54+
"cell_type": "markdown",
55+
"metadata": {},
56+
"source": [
57+
"### Calculations in Assignment Statements"
58+
]
59+
},
60+
{
61+
"cell_type": "code",
62+
"execution_count": null,
63+
"metadata": {},
64+
"outputs": [],
65+
"source": [
66+
"total = x + y"
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": null,
72+
"metadata": {},
73+
"outputs": [],
74+
"source": [
75+
"total"
76+
]
77+
},
78+
{
79+
"cell_type": "markdown",
80+
"metadata": {},
81+
"source": [
82+
"### Types"
83+
]
84+
},
85+
{
86+
"cell_type": "code",
87+
"execution_count": null,
88+
"metadata": {},
89+
"outputs": [],
90+
"source": [
91+
"type(x)"
92+
]
93+
},
94+
{
95+
"cell_type": "code",
96+
"execution_count": null,
97+
"metadata": {},
98+
"outputs": [],
99+
"source": [
100+
"type(10.5)"
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": null,
106+
"metadata": {},
107+
"outputs": [],
108+
"source": [
109+
"##########################################################################\n",
110+
"# (C) Copyright 2019 by Deitel & Associates, Inc. and #\n",
111+
"# Pearson Education, Inc. All Rights Reserved. #\n",
112+
"# #\n",
113+
"# DISCLAIMER: The authors and publisher of this book have used their #\n",
114+
"# best efforts in preparing the book. These efforts include the #\n",
115+
"# development, research, and testing of the theories and programs #\n",
116+
"# to determine their effectiveness. The authors and publisher make #\n",
117+
"# no warranty of any kind, expressed or implied, with regard to these #\n",
118+
"# programs or to the documentation contained in these books. The authors #\n",
119+
"# and publisher shall not be liable in any event for incidental or #\n",
120+
"# consequential damages in connection with, or arising out of, the #\n",
121+
"# furnishing, performance, or use of these programs. #\n",
122+
"##########################################################################\n"
123+
]
124+
}
125+
],
126+
"metadata": {
127+
"kernelspec": {
128+
"display_name": "Python 3",
129+
"language": "python",
130+
"name": "python3"
131+
},
132+
"language_info": {
133+
"codemirror_mode": {
134+
"name": "ipython",
135+
"version": 3
136+
},
137+
"file_extension": ".py",
138+
"mimetype": "text/x-python",
139+
"name": "python",
140+
"nbconvert_exporter": "python",
141+
"pygments_lexer": "ipython3",
142+
"version": "3.6.6"
143+
}
144+
},
145+
"nbformat": 4,
146+
"nbformat_minor": 2
147+
}

0 commit comments

Comments
 (0)