-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreadme.html
119 lines (119 loc) · 4.17 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
<!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-first-lines-of-code">
Practice Exercises for first lines of 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="repl">REPL</h3>
<p>
REPL stands for Read-Eval-Print-Loop and is the interactive environment
you get when you type <code>python</code> in the terminal / command
prompt. Remember, on macOS and Linux, you start Python 3’s REPL by typing
<code>python3</code>.
</p>
<h3 id="variables-and-values">Variables and values</h3>
<p>
Variables are names that we use to refer to data that could change or is
complex to write directly. Values are the data that is currently assigned
to that variable. We used
<a href="http://pythontutor.com">pythontutor.com</a> to explore this.
</p>
<p>Examples:</p>
<pre><code>x = 7
y = 11
z = x + 2*y
name = 'Sarah'</code></pre>
<h3 id="using-built-in-libraries">Using built-in libraries</h3>
<p>
Python comes with many included libraries (<a
href="https://docs.python.org/3/library/"
>hundreds!</a
>). To use one of these libraries, such as <code>sys</code>, you must tell
Python you want to load it. This is done with the
<code>import</code> keyword.
</p>
<p>Example:</p>
<pre><code>import sys
print(f"The current version of Python is {sys.version_info}")</code></pre>
<h3 id="getting-input-from-users">Getting input from users</h3>
<p>
Getting input from users is done with the <code>input</code> function.
</p>
<p>Example:</p>
<pre><code>name = input("What is your name? ")
print(f"Hello {name}")</code></pre>
<h4 id="converting-data">Converting data</h4>
<p>
Certain operations (like math and string concatenation) require the
correct data types.
</p>
<p>
Data is converted to numerical types using the type name (int, float,
etc). Here are a few examples:
</p>
<pre><code>text = '7.2'
whole_number = int(text) # value = 7
number = float(text) # value = 7.2</code></pre>
<h2 id="exercises">Exercises</h2>
<p>Now it’s your turn. Here are some ideas to practice.</p>
<ol type="1">
<li>Run the Python REPL and verify you have Python 3.6 or higher.</li>
<li>
Create a variable which is a whole number, compute the square and cube
of it (i.e. x^2 and x^3, although that is not the Python code needed).
</li>
<li>
Ask a user for their name and age. Write code to tell them how many
years you are older than them (negative numbers for younger is fine at
this point).
</li>
<li>
Use the built-in library <code>datetime</code> and the function
<code>datetime.datetime.now()</code> to determine the current year and
print that to REPL using an f-string.
</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>