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-2/203.md
+3-3Lines changed: 3 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,7 @@ We highlighted the Copilot logo in the bottom right of figure 2.1 as you should
24
24
25
25
### 2.3.1 Set up your working folder
26
26
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.
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
>
@@ -45,7 +45,7 @@ Let’s check to see if we’ve set up everything properly and that Copilot is w
45
45
46
46
47
47
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”.
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`.
49
49
50
50
Next, in the text editor, type:
51
51
@@ -105,6 +105,6 @@ To run your program, go to the top right corner of the text editor and press the
105
105
Hello Copilot
106
106
```
107
107
108
-
The top line starting with “>” is the command for the computer to run your code and all it says is to run your first\_Copilot\_program.py using Python. The second line is the output from running the command, and it says “Hello Copilot” which is what we’d hoped to see.
108
+
The top line starting with `>` is the command for the computer to run your code and all it says is to run your `first_Copilot_program.py` using Python. The second line is the output from running the command, and it says “Hello Copilot” which is what we’d hoped to see.
109
109
110
110
**Congratulations! You’ve written your first program!** We now know that your programming environment is set up correctly, and we can move onto our first programming task. But before we do, we’d like to go over some tips for how to deal with some common issues we’ve encountered when working with Copilot, so you have these tips available to you when working through the next example.
Copy file name to clipboardExpand all lines: content/chapter-2/205.md
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -23,7 +23,7 @@ Because you’ve just started working with Copilot, we’re wary of showing you
23
23
24
24
Also, Copilot will generate a lot of code in this section and we don’t expect you to understand the code until much later in the book. We provide the code solely so you can see what Copilot gave us, but do NOT feel as though you need to try to understand the code in this chapter.
25
25
26
-
To get started, let’s create a new file. If you aren’t already in VSCode, go ahead and start it. Then create a new Python file and save it as “nfl\_stats”.
26
+
To get started, let’s create a new file. If you aren’t already in VSCode, go ahead and start it. Then create a new Python file and save it as `nfl_stats`.
27
27
28
28
#### Showcasing Copilot’s value in a data processing Task
There are a bunch more columns after these, but these have all we need to perform our first task. We know now that there’s a column for players and a column for passing yards. Aaron Rodgers is a player that gets passing yards in each game that he plays. But how many passing yards does he have in total, over all of the games that he played? This isn’t so easy to answer by directly looking at the file. So what we want the computer to do is to make this easier for us!
58
58
59
-
We want it to sum up all the passing yards (pass\_yds) for rows (games) where Aaron Rodgers is the player. For now, we’re going to just ask for all the yards in the database even though it covers multiple seasons. We could change this later if we’d like. This problem might be a good problem to give to programmers learning to program in their 4th week of a standard college-level introductory programming course, but we have Copilot! So instead of learning how to write this code from scratch, we’re just going to ask Copilot to generate it for us. To make that happen, we are going to be quite specific in our request to make sure Copilot knows what we are asking for.
59
+
We want it to sum up all the passing yards (pass_yds) for rows (games) where Aaron Rodgers is the player. For now, we’re going to just ask for all the yards in the database even though it covers multiple seasons. We could change this later if we’d like. This problem might be a good problem to give to programmers learning to program in their 4th week of a standard college-level introductory programming course, but we have Copilot! So instead of learning how to write this code from scratch, we’re just going to ask Copilot to generate it for us. To make that happen, we are going to be quite specific in our request to make sure Copilot knows what we are asking for.
60
60
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:
In this section, we’re going to write a bunch of functions with Copilot. We’ll be coding them entirely in Copilot to help you see the cycle of function design we just described. Although our goal in this chapter isn’t to help you read code just yet, we will be seeing programming features (sometimes called constructs) in the solutions that are very common in code (e.g., if statements, loops) so we’ll point those out when we see them. Then in Chapter 4, we’ll say more about how to read this code in more detail.
10
10
11
-
Many of the functions we’re about to work on are unrelated to each other. For example, we’ll start with a function about stock share prices and move to functions about strong passwords. You typically wouldn’t store unrelated stuff like this in the same Python file. But as we’re just exploring different examples of good functions, please feel free to store all functions in the same Python file, perhaps named function\_practice.py or ch3.py.
11
+
Many of the functions we’re about to work on are unrelated to each other. For example, we’ll start with a function about stock share prices and move to functions about strong passwords. You typically wouldn’t store unrelated stuff like this in the same Python file. But as we’re just exploring different examples of good functions, please feel free to store all functions in the same Python file, perhaps named `function_practice.py` or `ch3.py`.
12
12
13
13
### 3.6.1 Dan’s stock pick
14
14
15
15
Dan is an investor in a stock called AAAPL. He purchased 10 shares for $15 each. Now, each of those shares is worth $17. Dan would like to know how much money he has made on the stock.
16
16
17
17
Remember that we want to make our function as general as possible. If the only thing our function could do is calculate this exact AAAPL situation, it wouldn’t be that useful in general. Sure, it would help Dan right now, but what about when AAAPL’s share price changes again, or when he is interested in another stock entirely?
18
18
19
-
A useful general function here would take three parameters, all of which are numbers. The first parameter is the number of shares purchased, the second is the share price when the shares were purchased, and the third is the current share price. Let’s call this function money\_made, since it’s going to determine how much money we’ve made or lost on the stock. In general, you want to name your function as an action word or words that describe what your function is doing. With that, we have enough to write the function header:
19
+
A useful general function here would take three parameters, all of which are numbers. The first parameter is the number of shares purchased, the second is the share price when the shares were purchased, and the third is the current share price. Let’s call this function `money_made`, since it’s going to determine how much money we’ve made or lost on the stock. In general, you want to name your function as an action word or words that describe what your function is doing. With that, we have enough to write the function header:
@@ -61,7 +61,7 @@ This way, we can call the function as many times as we like without going throug
61
61
62
62
At the bottom of the resulting window, you will see three greater-than symbols >>>. This is called a _prompt_, and you’re allowed to type Python code here. (This _prompt_ has nothing to do with the kind of prompt that we use when interacting with Copilot.) It will show us the result of the code that we type right away, which is convenient and fast.
63
63
64
-
To call our `money\_made` function, we need to provide three arguments, and they will be assigned left to right to the parameters. Whatever we put first will be assigned to `num\_shares`, whatever we put second will be assigned to `purchase\_share\_price`, and whatever we put third will be assigned to `current\_share\_price`.
64
+
To call our `money_made` function, we need to provide three arguments, and they will be assigned left to right to the parameters. Whatever we put first will be assigned to `num_shares`, whatever we put second will be assigned to `purchase_share_price`, and whatever we put third will be assigned to `current_share_price`.
65
65
66
66
Let’s try this! At the prompt, type the following and press enter (or `<shift>+Enter`). Don’t type the >>> as that’s already there, and we are including it throughout the book to make it clear where we are typing. Please see figure 3.5 for an example of running the function in the terminal at the Python prompt.
67
67
@@ -78,7 +78,7 @@ You’ll see an output of
78
78
79
79
Is `20` correct? Well, we bought 10 shares, and each of them went up $2 (from $15 to $17), so we did make $20. Looks good!
80
80
81
-
> Figure 3.5 Calling the money\_made function from Python prompt in the VSCode Terminal.
81
+
> Figure 3.5 Calling the `money_made` function from Python prompt in the VSCode Terminal.
82
82
83
83

84
84
@@ -104,7 +104,7 @@ What other tests can we do? Well, sometimes a stock price doesn’t change at al
104
104
105
105
Looks good!
106
106
107
-
Testing is a combination of science and art. How many categories of things are there to test? Are these two calls really two different categories? Have we missed any categories? You will improve your testing ability through practice, and we’ll spend all of Chapter 6 talking about testing. For now, it looks like our `money\_made` function is doing its job.
107
+
Testing is a combination of science and art. How many categories of things are there to test? Are these two calls really two different categories? Have we missed any categories? You will improve your testing ability through practice, and we’ll spend all of Chapter 6 talking about testing. For now, it looks like our `money_made` function is doing its job.
108
108
109
109
It’s possible for a function to use variables (rather than just its parameters) in its code, and we want to show you an example of that now so that you’re ready when you see Copilot doing it.
110
110
@@ -129,7 +129,7 @@ Unlike our previous functions in this chapter, we’re not dealing with numbers
129
129
130
130
The Python type for text is called a **string**. There are zillions of possible strings, because we can use a string to store whatever text we want. And the Python type for a yes/no result is called a **Boolean** or **bool**. A bool has only two values: True or False.
131
131
132
-
Alright! We’re ready to prompt Copilot. For functions that return bool (True/False) values, we usually name the function as has\_x, or is\_x, or using some other verb that implies a true/false result.
132
+
Alright! We’re ready to prompt Copilot. For functions that return bool (True/False) values, we usually name the function as `has_x`, or `is_x`, or using some other verb that implies a true/false result.
That `while` keyword creates another kind of loop, this one continuing as long as the entered password is not strong. Copilot is also smart enough to call our earlier `is\_strong\_password` function to determine what counts as a strong password. As we will see in future chapters, using functions as building blocks in this way is precisely how large programs are built. You will often notice Copilot calling your earlier functions to solve later ones, much as we observed here.
352
+
That `while` keyword creates another kind of loop, this one continuing as long as the entered password is not strong. Copilot is also smart enough to call our earlier `is_strong_password` function to determine what counts as a strong password. As we will see in future chapters, using functions as building blocks in this way is precisely how large programs are built. You will often notice Copilot calling your earlier functions to solve later ones, much as we observed here.
353
353
354
354
Let’s test this! Highlight all of our password function code and hit `<Shift>+Enter`. We’ll call the function that we want to test. Then, try typing passwords, hitting enter after each one. You’ll notice that it keeps asking you until you finally provide a strong password:
355
355
@@ -439,7 +439,7 @@ Let’s try a couple more. In each case, calculate by hand what you expect the a
439
439
440
440
There are many ways to write correct code for a function. If you press `<Ctrl>+Enter` and look at the Copilot suggestions, you may see different types of code. It doesn’t necessarily mean that one of these types is right and the others are wrong.
441
441
442
-
For example, here’s another solution for num\_points that we got from the other Copilot suggestions.
442
+
For example, here’s another solution for `num_points` that we got from the other Copilot suggestions.
Copy file name to clipboardExpand all lines: content/chapter-4/401.md
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ The second is being able to determine the overall purpose of a program. As profe
14
14
15
15
At the end of these two chapters, we’ll want you to be able to do both at the level of being able to interpret the code produced by Copilot. We’ll start focusing on that line-by-line understanding but toward the end, you’ll start being able to look at a small chunk of code and determine its purpose.
16
16
17
-
We can illustrate the difference between these two levels of reading code by referring back to our `best\_word` function from Chapter 3, reprinted in the following listing.
17
+
We can illustrate the difference between these two levels of reading code by referring back to our `best_word` function from Chapter 3, reprinted in the following listing.
18
18
19
19
> Listing 4.1 Best Word function for Scrabble, reprinted from Chapter 3
20
20
@@ -35,7 +35,7 @@ Return the word worth the most points.
35
35
return best_word
36
36
```
37
37
38
-
A **tracing description** of what this program does would be a description of each line. For example, we would say that we're defining a function called `best\_word`. We have a variable called `best\_word` that we start off as a string with no characters, otherwise known as the empty string. (It’s unfortunate that the function and this variable are both called `best\_word`, because it makes it trickier to refer to one or the other, but that’s what Copilot gave us.) We also have another variable `best\_points` that we start at 0. Then we have a for loop over each word in the `word\_list`. Inside the for loop, we call our `num\_points` helper function... and so on. (We’ll be explaining how we know what each line of code does over the next two chapters!).
38
+
A **tracing description** of what this program does would be a description of each line. For example, we would say that we're defining a function called `best_word`. We have a variable called `best_word` that we start off as a string with no characters, otherwise known as the empty string. (It’s unfortunate that the function and this variable are both called `best_word`, because it makes it trickier to refer to one or the other, but that’s what Copilot gave us.) We also have another variable `best_points` that we start at 0. Then we have a for loop over each word in the `word_list`. Inside the for loop, we call our `num_points` helper function... and so on. (We’ll be explaining how we know what each line of code does over the next two chapters!).
39
39
40
40
In contrast, a **description of the overall purpose** would be something like our docstring description: “Return the word with the highest Scrabble point value from a list of words.” Rather than refer to each line, this description refers to the code’s purpose as a whole, explaining what it does at a high level.
Copy file name to clipboardExpand all lines: content/chapter-4/402.md
+4-4Lines changed: 4 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ In Chapter 2, when setting up your computer to use GitHub Copilot, you installed
10
10
11
11
We suspect that, soon, the Copilot Labs extension, or parts of it, will be folded into the main Copilot extension. If that happens, the specific steps we give here may vary somewhat, and in that case, we encourage you to consult more general GitHub Copilot documentation.
12
12
13
-
For now, with the Copilot Labs extension installed, you can highlight some code that you want Copilot to describe to you. Let's try this with our `best\_word` function.
13
+
For now, with the Copilot Labs extension installed, you can highlight some code that you want Copilot to describe to you. Let's try this with our `best_word` function.
14
14
15
15
16
16
> Figure 4.1 The Copilot Labs view in VSCode.
@@ -23,15 +23,15 @@ First, click on the Copilot Labs tab in your Activity Bar (on the left-hand side
23
23
24
24
25
25
26
-
Next, highlight all of the code for our best\_word function as is highlighted in figure 4.2. (You may need to have Copilot generate the code for you again if you didn't save it from Chapter 3.)
26
+
Next, highlight all of the code for our `best_word` function as is highlighted in figure 4.2. (You may need to have Copilot generate the code for you again if you didn't save it from Chapter 3.)
27
27
28
-
> Figure 4.2 The code from the `best\_word` function highlighted in the editor.
28
+
> Figure 4.2 The code from the `best_word` function highlighted in the editor.
29
29
30
30

31
31
32
32
After highlighting the code, you should now see the code appear in the left in the EXPLAIN feature as shown in figure 4.3
33
33
34
-
> Figure 4.3 The code from the best\_word function appearing in Copilot Labs.
34
+
> Figure 4.3 The code from the `best_word` function appearing in Copilot Labs.
0 commit comments