-
Notifications
You must be signed in to change notification settings - Fork 3
/
readme.html
133 lines (128 loc) · 4.52 KB
/
readme.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, user-scalable=yes"
/>
<title>readme</title>
<style type="text/css">
code {
white-space: pre-wrap;
}
span.smallcaps {
font-variant: small-caps;
}
span.underline {
text-decoration: underline;
}
div.column {
display: inline-block;
vertical-align: top;
width: 50%;
}
</style>
</head>
<body>
<h1 id="practice-exercises-for-interactive-code">
Practice exercises for interactive code
</h1>
<h2 id="overview">Overview</h2>
<p>
In this section, you’ll have a chance to practice the concepts you’ve
learned in the videos. First, review the core concepts covered that you’ll
need to keep in mind. Then go through the exercises below.
</p>
<p>
Remember, these are for your own benefit. Feel free to skip them if you
don’t find a particular exercise valuable or you get stuck for too long.
</p>
<h2 id="core-concepts">Core concepts</h2>
<h3 id="running-python-code">Running Python code</h3>
<p>
If you have a Python <strong>file</strong>, it can be executed inside
PyCharm by simply right-clicking and choosing <strong>Run</strong>.
Outside of PyCharm, you execute it like this:
</p>
<pre><code>$ python3 program.py
# or
C:\> python program.py</code></pre>
<p>
Remember to use either <code>python</code> or <code>python3</code> based
on your system setup.
</p>
<h3 id="if-else-statements">if / else statements</h3>
<p>
The essential lesson from this chapter is how code makes decisions to do
one thing or another. The first building block are simple
<strong>this or that</strong> type of processes. These are done with
<code>if</code> statements.
</p>
<pre><code>num = 7
if num < 100:
print("Number is smallish")</code></pre>
<p>
We can also have code that has two or more branches with
<code>elif</code> and <code>else</code>:
</p>
<pre><code>num = 7
if num < 100:
print("Number is smallish")
elif num < 1000:
print("Middle sized number.")
else:
print("That's one big number!")</code></pre>
<h3 id="while-loops">while loops</h3>
<p>
When you need to repeat an operation as long as some condition is met, the
<code>while</code> loop is the thing you want.
</p>
<pre><code>attempts = 0
while attempts < 5:
attempts += 1
# do whatever you are attempting here...</code></pre>
<h3 id="is-a-number-even-or-odd">Is a number even or odd?</h3>
<p>
This seems like a simple question to answer but requires a new operation.
The modulo operator. This is basically the remainder of a division. For
example, 19 / 5 is 3 as a <strong>whole number</strong> (int). But we know
that there is a remainder of 4 for whole number math. In Python we express
this as:
</p>
<pre><code>div = int(19 / 5) # <-- 3
rem = 19 % 5 # <-- 4</code></pre>
<p>
Then we can test whether a number is even if it is evenly divisible by 2
or has a remainder of 0:
</p>
<pre><code>remainder = num % 2 # Is this 0 or 1?</code></pre>
<h2 id="exercises">Exercises</h2>
<p>Now it’s your turn. Here are some ideas to practice.</p>
<ol type="1">
<li>
Create a <strong>hello_world.py</strong> file and execute it with
Python. This can be in PyCharm or in another editor and using the
technique above. Seems trivial but will help you verify everything is
working right there. Just have the program output “Hello world”
</li>
<li>
Write a program that requests a number from the user. Have the program
print “Even” or “Odd” depending on whether they entered an even or odd
number.
</li>
<li>
Extend the program above to repeatedly ask that question as long as the
user enters a nonzero number. But if they enter 0, it should then stop
asking and say goodbye.
</li>
<li>
Take one of these sets of code and visualize them with
<a href="http://pythontutor.com/visualize.html#mode=edit"
>pythontutor.com</a
>
</li>
</ol>
</body>
</html>