Skip to content

Commit 844b8ac

Browse files
committed
Posted examples for Lessons 1-13
1 parent 23cf1fd commit 844b8ac

File tree

1,021 files changed

+122455
-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.

1,021 files changed

+122455
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"![Self Check Exercises check mark image](files/art/check.png)\n",
8+
"# 2.2 Self Check"
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"metadata": {},
14+
"source": [
15+
"**1. _(True/False)_** The following are valid variable names: `3g`, `87` and `score_4`.\n",
16+
"\n",
17+
"**Answer:** False. Because they begin with a digit, 3g and 87 are invalid names.\n",
18+
"\n",
19+
"**2. _(True/False)_** Python treats `y` and `Y` as the same identifier.\n",
20+
"\n",
21+
"**Answer:** False. Python is case sensitive, so `y` and `Y` are different identifiers.\n",
22+
"\n",
23+
"**3. _(IPython Session)_** Calculate the sum of `10.8`, `12.2` and `0.2`, store it in the variable `total`, then display `total`’s value. \n",
24+
"\n",
25+
"**Answer:** "
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": null,
31+
"metadata": {},
32+
"outputs": [],
33+
"source": [
34+
"total = 10.7 + 12.2 + 0.2"
35+
]
36+
},
37+
{
38+
"cell_type": "code",
39+
"execution_count": null,
40+
"metadata": {},
41+
"outputs": [],
42+
"source": [
43+
"total"
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": null,
49+
"metadata": {},
50+
"outputs": [],
51+
"source": [
52+
"##########################################################################\n",
53+
"# (C) Copyright 2019 by Deitel & Associates, Inc. and #\n",
54+
"# Pearson Education, Inc. All Rights Reserved. #\n",
55+
"# #\n",
56+
"# DISCLAIMER: The authors and publisher of this book have used their #\n",
57+
"# best efforts in preparing the book. These efforts include the #\n",
58+
"# development, research, and testing of the theories and programs #\n",
59+
"# to determine their effectiveness. The authors and publisher make #\n",
60+
"# no warranty of any kind, expressed or implied, with regard to these #\n",
61+
"# programs or to the documentation contained in these books. The authors #\n",
62+
"# and publisher shall not be liable in any event for incidental or #\n",
63+
"# consequential damages in connection with, or arising out of, the #\n",
64+
"# furnishing, performance, or use of these programs. #\n",
65+
"##########################################################################\n"
66+
]
67+
}
68+
],
69+
"metadata": {
70+
"kernelspec": {
71+
"display_name": "Python 3",
72+
"language": "python",
73+
"name": "python3"
74+
},
75+
"language_info": {
76+
"codemirror_mode": {
77+
"name": "ipython",
78+
"version": 3
79+
},
80+
"file_extension": ".py",
81+
"mimetype": "text/x-python",
82+
"name": "python",
83+
"nbconvert_exporter": "python",
84+
"pygments_lexer": "ipython3",
85+
"version": "3.6.6"
86+
}
87+
},
88+
"nbformat": 4,
89+
"nbformat_minor": 2
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"45 + 72"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": null,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"5 * (12.7 - 4) / 2"
19+
]
20+
}
21+
],
22+
"metadata": {
23+
"kernelspec": {
24+
"display_name": "Python 3",
25+
"language": "python",
26+
"name": "python3"
27+
},
28+
"language_info": {
29+
"codemirror_mode": {
30+
"name": "ipython",
31+
"version": 3
32+
},
33+
"file_extension": ".py",
34+
"mimetype": "text/x-python",
35+
"name": "python",
36+
"nbconvert_exporter": "python",
37+
"pygments_lexer": "ipython3",
38+
"version": "3.7.1"
39+
}
40+
},
41+
"nbformat": 4,
42+
"nbformat_minor": 2
43+
}

examples/ch01/RollDieDynamic.py

+59
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/ch01/TestDrive.ipynb

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"data": {
10+
"text/plain": [
11+
"117"
12+
]
13+
},
14+
"execution_count": 1,
15+
"metadata": {},
16+
"output_type": "execute_result"
17+
}
18+
],
19+
"source": [
20+
"45 + 72"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": 2,
26+
"metadata": {},
27+
"outputs": [
28+
{
29+
"data": {
30+
"text/plain": [
31+
"21.75"
32+
]
33+
},
34+
"execution_count": 2,
35+
"metadata": {},
36+
"output_type": "execute_result"
37+
}
38+
],
39+
"source": [
40+
"5 * (12.7 - 4) / 2"
41+
]
42+
},
43+
{
44+
"cell_type": "markdown",
45+
"metadata": {},
46+
"source": [
47+
"# this is a text note \n",
48+
"\n",
49+
"## this is an H2\n",
50+
"\n",
51+
"### this is an H3\n"
52+
]
53+
}
54+
],
55+
"metadata": {
56+
"kernelspec": {
57+
"display_name": "Python 3",
58+
"language": "python",
59+
"name": "python3"
60+
},
61+
"language_info": {
62+
"codemirror_mode": {
63+
"name": "ipython",
64+
"version": 3
65+
},
66+
"file_extension": ".py",
67+
"mimetype": "text/x-python",
68+
"name": "python",
69+
"nbconvert_exporter": "python",
70+
"pygments_lexer": "ipython3",
71+
"version": "3.7.1"
72+
}
73+
},
74+
"nbformat": 4,
75+
"nbformat_minor": 2
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"![Self Check Exercises check mark image](files/art/check.png)\n",
8+
"# Self Check"
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"metadata": {},
14+
"source": [
15+
"**3. _(IPython Session)_** Evaluate the expression `5 * (3 + 4)` both with and without the parentheses. Do you get the same result? Why or why not?\n",
16+
"\n",
17+
"**Answer:** "
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"execution_count": null,
23+
"metadata": {},
24+
"outputs": [],
25+
"source": [
26+
"5 * (3 + 4)"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": null,
32+
"metadata": {},
33+
"outputs": [],
34+
"source": [
35+
"5 * 3 + 4"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": null,
41+
"metadata": {},
42+
"outputs": [],
43+
"source": [
44+
"##########################################################################\n",
45+
"# (C) Copyright 2019 by Deitel & Associates, Inc. and #\n",
46+
"# Pearson Education, Inc. All Rights Reserved. #\n",
47+
"# #\n",
48+
"# DISCLAIMER: The authors and publisher of this book have used their #\n",
49+
"# best efforts in preparing the book. These efforts include the #\n",
50+
"# development, research, and testing of the theories and programs #\n",
51+
"# to determine their effectiveness. The authors and publisher make #\n",
52+
"# no warranty of any kind, expressed or implied, with regard to these #\n",
53+
"# programs or to the documentation contained in these books. The authors #\n",
54+
"# and publisher shall not be liable in any event for incidental or #\n",
55+
"# consequential damages in connection with, or arising out of, the #\n",
56+
"# furnishing, performance, or use of these programs. #\n",
57+
"##########################################################################\n"
58+
]
59+
}
60+
],
61+
"metadata": {
62+
"kernelspec": {
63+
"display_name": "Python 3",
64+
"language": "python",
65+
"name": "python3"
66+
},
67+
"language_info": {
68+
"codemirror_mode": {
69+
"name": "ipython",
70+
"version": 3
71+
},
72+
"file_extension": ".py",
73+
"mimetype": "text/x-python",
74+
"name": "python",
75+
"nbconvert_exporter": "python",
76+
"pygments_lexer": "ipython3",
77+
"version": "3.7.1"
78+
}
79+
},
80+
"nbformat": 4,
81+
"nbformat_minor": 2
82+
}

0 commit comments

Comments
 (0)