You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/chapter-1/101.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -101,7 +101,7 @@ for i in range(10):
101
101
```
102
102
103
103
Unlike the example we showed you before, this piece of Python code actually works!
104
-
104
+
105
105
不同于我们在上面看到的那个示例,这段 Python 代码确实能够正常运行!
106
106
107
107
AI coding assistants can be used to help people write code. In this book, we will learn how to use Copilot to write code for us. We will ask for what we want in English, and we will get the code back in Python.
Copy file name to clipboardExpand all lines: content/chapter-2/203.md
+4-4Lines changed: 4 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -20,14 +20,14 @@ We highlighted the Copilot logo in the bottom right of figure 2.1 as you should
20
20
21
21
> Figure 2.1 The VSCode Interface [3].
22
22
23
-

23
+

24
24
25
25
### 2.3.1 Set up your working folder
26
26
27
27
In the top of the Activity Bar on the left in VSCode you will find the “Explorer” as the top icon. After you click on the “Explorer”, it should say “No Folder Open.” Click on the button to “Open Folder” and select a folder in your computer (or make a new one—we like the folder name `fun_with_Copilot`). Once you’ve opened this folder, your workspace will be the folder you opened which means you should have your code and any data files, like the one we’ll use later this chapter, in that folder.
28
28
29
29
> **File not found or file missing errors**
30
-
>
30
+
>
31
31
> If you ever receive an error that says you are missing a file, these are the kind of errors that can be really annoying when writing software. It could be that you just didn’t put the file in your working folder, this happens. That’s an easy fix by copying or moving the file into the correct folder. However, sometimes, you’ll look in the folder and the file will be there, but when you run your code in VSCode, Python can’t seem to find it. If this happens to you (it happened to us when writing the book!), be sure to have the folder with the code and the desired file open using Explorer in VSCode (as shown in the Side Bar in figure 2.1).
32
32
33
33
### 2.3.2 Check to see if your setup is working properly
@@ -36,14 +36,14 @@ Let’s check to see if we’ve set up everything properly and that Copilot is w
36
36
37
37
> Figure 2.2 How to create a new file in VSCode.
38
38
39
-

39
+

40
40
41
41
42
42
> Figure 2.3 Select to create the New File as a Python File.
43
43
44
44

45
45
46
-
46
+
47
47
48
48
After creating it, we like to make sure that we’ve saved the file. Go to “File -> Save As” and let’s just name this file `first_Copilot_program`.
<td> <p>If you give Copilot a prompt using the comment symbol (#), when you start a newline, it wants to just give you more comments rather than code. For example:</p> <p><strong><kbd># output "Hello Copilot" to the screen</kbd></strong></p> <p><kbd># print "Hello world" to the screen</kbd></p> <p>We’ve seen Copilot generate line after line of comments, sometimes repeating itself! When this happens, suggestion 3 (use docstrings) is sometimes the most effective.</p> </td>
27
-
<td> <p>1. Add a newline (Enter) between your comment and Copilot’s suggestion to help it switch from comments to code.</p> <p>2. If a newline doesn’t work, you can type a letter or two of code (no comment symbol). A couple letters from a keyword in your prompt usually works. For example:</p> <p><strong><kbd># output "Hello Copilot" to the screen</kbd></strong></p> <p><kbd>pr</kbd></p> <p>A couple letters from a keyword typically causes Copilot to give a code suggestion.</p> <p>3. Switch from using # comments to <a id="idIndexMarker037" href="/book/learn-ai-assisted-python-programming/chapter-2/"></a>docstring comments like this:</p> <p><i><kbd>"""</kbd></i></p> <p><i><kbd>output "Hello Copilot" to the screen</kbd></i></p> <p><i><kbd>"""</kbd></i></p> <p>4. Use Ctrl–Enter to see if Copilot will give you suggestions that are code rather than comments</p> </td>
28
-
</tr>
29
-
<tr>
30
-
<td> <p>Wrong code</p> </td>
31
-
<td> <p>Sometimes Copilot just gives you obviously wrong code from the start. (You’ll learn throughout this book how to identify incorrect code!)</p> <p>In addition, sometimes Copilot seems to get stuck down wrong paths. For example, it might seem to be trying to solve a different problem than what you’ve asked it to solve. (Suggestion 3, in particular, can help with getting Copilot to go down a new path.)</p> </td>
32
-
<td> <p>Much of this book is about how to address this problem, but here are some quick tips to get Copilot to help:</p> <p>1. Change your prompt to see if you can better describe what you need.</p> <p>2. Try using Ctrl–Enter to find a suggestion from Copilot that is the correct code.</p> <p>3. Close the VS Code program, wait a little bit, and restart it. This can help clear the Copilot cache to get new suggestions.</p> <p>4. Try breaking down the problem into smaller steps (see chapter 7 for more details).</p> <p>5. Debug the code (see chapter 8).</p> <p>6. Try asking ChatGPT for the code and feed its suggestions into VS Code. A different large language model (LLM) can sometimes give suggestions that help the other LLM to get unstuck.</p> </td>
33
-
</tr>
34
-
<tr>
35
-
<td> <p>Copilot gives you</p> <p><kbd># YOUR CODE HERE</kbd></p> </td>
36
-
<td> <p>We’ve had Copilot seem to tell us to write our own code by generating this (or similar text) after a prompt:</p> <p><kbd># YOUR CODE HERE</kbd></p> </td>
37
-
<td> <p>We believe this is happening when we ask Copilot to solve a problem that has been given by an instructor to students to solve in the past. Why? Well, when we write our assignments for our students, we (as instructors) often write some code and then tell our students to write the rest by writing</p> <p><kbd># YOUR CODE HERE</kbd></p> <p>where we want students to write their code. Students tend to leave that comment in their solution code, which means Copilot was trained to think this comment is an important part of the solution (it’s not). Often, we’re able to solve this problem by finding reasonable solutions in the Copilot suggestions with Ctrl–Enter, but please see the solutions for Wrong Code if that doesn’t work.</p> </td>
38
-
</tr>
39
-
<tr>
40
-
<td> <p>Missing modules</p> </td>
41
-
<td> <p>Copilot gives you code, but it won’t work because there are modules missing. (Modules are additional libraries that can be added to Python to give prebuilt functionality.)</p> </td>
42
-
<td> <p>See section 2.5, under “Modules” for how to install new modules on your machine.</p> </td>
<td> <p>If you give Copilot a prompt using the comment symbol (#), when you start a newline, it wants to just give you more comments rather than code. For example:</p> <p><strong><kbd># output "Hello Copilot" to the screen</kbd></strong></p> <p><kbd># print "Hello world" to the screen</kbd></p> <p>We’ve seen Copilot generate line after line of comments, sometimes repeating itself! When this happens, suggestion 3 (use docstrings) is sometimes the most effective.</p> </td>
27
+
<td> <p>1. Add a newline (Enter) between your comment and Copilot’s suggestion to help it switch from comments to code.</p> <p>2. If a newline doesn’t work, you can type a letter or two of code (no comment symbol). A couple letters from a keyword in your prompt usually works. For example:</p> <p><strong><kbd># output "Hello Copilot" to the screen</kbd></strong></p> <p><kbd>pr</kbd></p> <p>A couple letters from a keyword typically causes Copilot to give a code suggestion.</p> <p>3. Switch from using # comments to <a id="idIndexMarker037" href="/book/learn-ai-assisted-python-programming/chapter-2/"></a>docstring comments like this:</p> <p><i><kbd>"""</kbd></i></p> <p><i><kbd>output "Hello Copilot" to the screen</kbd></i></p> <p><i><kbd>"""</kbd></i></p> <p>4. Use Ctrl–Enter to see if Copilot will give you suggestions that are code rather than comments</p> </td>
28
+
</tr>
29
+
<tr>
30
+
<td> <p>Wrong code</p> </td>
31
+
<td> <p>Sometimes Copilot just gives you obviously wrong code from the start. (You’ll learn throughout this book how to identify incorrect code!)</p> <p>In addition, sometimes Copilot seems to get stuck down wrong paths. For example, it might seem to be trying to solve a different problem than what you’ve asked it to solve. (Suggestion 3, in particular, can help with getting Copilot to go down a new path.)</p> </td>
32
+
<td> <p>Much of this book is about how to address this problem, but here are some quick tips to get Copilot to help:</p> <p>1. Change your prompt to see if you can better describe what you need.</p> <p>2. Try using Ctrl–Enter to find a suggestion from Copilot that is the correct code.</p> <p>3. Close the VS Code program, wait a little bit, and restart it. This can help clear the Copilot cache to get new suggestions.</p> <p>4. Try breaking down the problem into smaller steps (see chapter 7 for more details).</p> <p>5. Debug the code (see chapter 8).</p> <p>6. Try asking ChatGPT for the code and feed its suggestions into VS Code. A different large language model (LLM) can sometimes give suggestions that help the other LLM to get unstuck.</p> </td>
33
+
</tr>
34
+
<tr>
35
+
<td> <p>Copilot gives you</p> <p><kbd># YOUR CODE HERE</kbd></p> </td>
36
+
<td> <p>We’ve had Copilot seem to tell us to write our own code by generating this (or similar text) after a prompt:</p> <p><kbd># YOUR CODE HERE</kbd></p> </td>
37
+
<td> <p>We believe this is happening when we ask Copilot to solve a problem that has been given by an instructor to students to solve in the past. Why? Well, when we write our assignments for our students, we (as instructors) often write some code and then tell our students to write the rest by writing</p> <p><kbd># YOUR CODE HERE</kbd></p> <p>where we want students to write their code. Students tend to leave that comment in their solution code, which means Copilot was trained to think this comment is an important part of the solution (it’s not). Often, we’re able to solve this problem by finding reasonable solutions in the Copilot suggestions with Ctrl–Enter, but please see the solutions for Wrong Code if that doesn’t work.</p> </td>
38
+
</tr>
39
+
<tr>
40
+
<td> <p>Missing modules</p> </td>
41
+
<td> <p>Copilot gives you code, but it won’t work because there are modules missing. (Modules are additional libraries that can be added to Python to give prebuilt functionality.)</p> </td>
42
+
<td> <p>See section 2.5, under “Modules” for how to install new modules on your machine.</p> </td>
Copy file name to clipboardExpand all lines: content/chapter-2/205.md
+9-9Lines changed: 9 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -37,7 +37,7 @@ To download the dataset, you will have to sign up for a Kaggle account. If you d
37
37
38
38
> Figure 2.4 The first few columns and rows of the `nfl_offensive_stats.csv` dataset.
39
39
40
-

40
+

41
41
42
42
43
43
The `nfl_offensive_stats.csv` file is something known as a comma separated value text file (see figure 2.4 for a portion of the file). This is a pretty standard format for storing data. It has a header row at the top that explains what’s in every column. The way that we (or a computer) know the boundaries between columns is to use the commas between cells. Also notice that each row is placed on its own line.
@@ -61,7 +61,7 @@ We want it to sum up all the passing yards (pass_yds) for rows (games) where Aar
61
61
Specifically, we’re only going to ask it to perform small amounts of work and then re-prompt it to perform the next step. Later we’ll discuss how to write good prompts, but for now, just go ahead and use what we’ve written below by placing this text at the top of your new file:
62
62
63
63
```python
64
-
"""
64
+
"""
65
65
open the csv file called "nfl_offensive_stats.csv" and read in
66
66
the csv data from the file
67
67
"""
@@ -85,9 +85,9 @@ Now that we have the data from the file, we’re going to give it a new prompt t
85
85
Because the computer doesn’t know what football is or specifics like that Aaron Rodgers is a quarterback, our prompt is going to be quite specific. We’ll teach you how to write prompts like this over the course of the book. Here is the new prompt:
86
86
87
87
```python
88
-
"""
88
+
"""
89
89
In the data we just read in, the fourth column is the player
90
-
and the 8th column is the passing yards. Get the sum of
90
+
and the 8th column is the passing yards. Get the sum of
91
91
yards from column 8 where the 4th column value is
92
92
"Aaron Rodgers"
93
93
"""
@@ -188,7 +188,7 @@ To make this change, we’re just going to modify the prompt at the bottom. Go t
188
188
```python
189
189
"""
190
190
print the sum of the passing yards sorted by sum
191
-
of passing yards in descending order
191
+
of passing yards in descending order
192
192
"""
193
193
for player insorted(passing_yards, key=passing_yards.get, reverse=True):
194
194
print(player, passing_yards[player])
@@ -199,7 +199,7 @@ Delete the code, just leaving the comment, and add another line to the docstring
199
199
```python
200
200
"""
201
201
print the sum of the passing yards sorted by sum
202
-
of passing yards in descending order
202
+
of passing yards in descending order
203
203
Do not include Tom Brady because he wins too much
204
204
"""
205
205
```
@@ -232,7 +232,7 @@ At the end of the code we just wrote, add the following comment:
232
232
233
233
```python
234
234
"""
235
-
plot the players by their number of passing yards only for
235
+
plot the players by their number of passing yards only for
236
236
players with more than 4000 passing yards
237
237
"""
238
238
```
@@ -290,15 +290,15 @@ pip install matplotlib
290
290
```
291
291
292
292
> **Note**
293
-
>
293
+
>
294
294
> For some operating systems you may need to use `pip3` rather than `pip`. On Windows machines, we recommend using `pip` if you followed our installation instructions. On Mac or Linux machines, we recommend using `pip3`.
295
295
296
296
When you run this command, you’ll see that a bunch of modules are installed, including numpy (the next module this code wants to use). (matplotlib requires Python modules of its own, so it installs all the modules you need to use matplotlib in addition to matplotlib itself.) When you try to run the code again, you’ll get a plot like this.
297
297
298
298
> Figure 2.5 The plot produced by the code in listing 2.2.
299
299
300
300
301
-

301
+

302
302
303
303
In this bar graph, we see the y-axis is the number of passing yards and the x-axis is the player’s name. The players are sorted from the fewest yards (with a minimum of 4000) to most yards. Admittedly, it’s not perfect as it is missing a y-axis label and the names on the x-axis are cut off at the bottom, but this is pretty impressive given all we gave Copilot was a pretty short prompt. We could keep adding prompts to see if we can format the graph better, but we’ve already achieved the primary goals for this section which was to show you how powerful Copilot is at helping us write code and to get a feel for how to interact with Copilot.
Copy file name to clipboardExpand all lines: content/chapter-3/301.md
+4-4Lines changed: 4 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ Suppose that you’ve found a word search puzzle in the newspaper that you’d l
13
13
> Figure 3.1 Example Wordsearch puzzle
14
14
15
15
16
-

16
+

17
17
18
18
At a high level, your task is “find all of the words in the word search”. Unfortunately, that description of the task isn’t helpful on its own. It doesn’t tell us what steps we need to carry out to solve the problem.
19
19
@@ -59,18 +59,18 @@ Although we had Copilot generate this function using # comments, this approach i
59
59
There’s an alternate way to prompt Copilot to write the code for a function that may help it generate code more accurately and may help us better understand exactly what we want our function to do. It involves writing a docstring, and we’ll use docstrings to write functions for the majority of the book.
60
60
61
61
> **Docstrings explain function behavior**
62
-
>
62
+
>
63
63
> Docstrings are how Python functions are described by programmers. They follow the function header and begin and end with three quotation marks.
64
64
65
65
By writing the header and docstring, you’ll make it easier for Copilot to generate the right code. In the header, you will be the one deciding on the name of the function and will provide the names of each parameter that you want the function to use. After the function header, you’ll provide a docstring which tells Copilot what the function does. Then, just as before, Copilot will generate the code for the function. Because we gave Copilot the function header, it will be able to learn from the header and is less likely to make mistakes.
66
66
67
67
Here’s what the alternate approach would look like when writing that same `larger` function:
Copy file name to clipboardExpand all lines: content/chapter-3/303.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -75,7 +75,7 @@ So, we’re back in `funct1`, on the line `print("")`. Printing an empty piece o
75
75
> Figure 3.2 Flow of function execution in our example from listing 3.1.
76
76
77
77
78
-

78
+

79
79
80
80
That was a long example, but we provided it to help give you an idea of how functions execute and how programs consist of defining and calling functions. In any software you use, think about the specific tasks that it performs: the programmers probably wrote one or more functions for each one. The button in a text editor that changes the text to bold probably calls a function to change the text to bold. That function might change the editor’s internal idea of the text (the editor likely stores your text in a different format than how you view it) and then it might call another function that updates the user’s (your) view of the text.
0 commit comments