Locked lesson.
About this lesson
Learn when and how to use comments in your code.
Exercise files
Download this lesson’s related exercise files.
Comments and C9 Transition - Solution.docx57.4 KB Comments and C9 Transition.docx
57.2 KB
Quick reference
Comments and C9 Transition
Comments are important to all coding.
When to use
You should use comments at the beginning of every major block of code, or whenever you write particularly complicated code.
Instructions
Comments are created with #
# This is a comment.
Please note that since this course was originally recorded, the C9 coding environment John uses in some of the videos in this course was shut down. John encourages everyone to use Sublime Text instead and explains the similarities between the two coding environments in the lesson.
Hints & tips
- Comments are very important
- # this is a comment
- comments use the hashtag #
- 00:04 Okay, so we've built our first Python program, very simple program.
- 00:08 Like I said, this is the first program that all coders always do the Hello World
- 00:12 program, so you're in good company.
- 00:14 So in this video I want to expand upon this a little bit, talk about comments,
- 00:18 talk about a couple of other little things.
- 00:20 And it should be fun.
- 00:21 First things first, let's add a little bit more functionality to our program.
- 00:25 Up here at the top, let's type in import os, and
- 00:29 then os.system, and then parenthesis, and then clear.
- 00:36 Now, go ahead and
- 00:37 save this file, Ctrl+S on your keyboard, Cmd+S if you're on a Mac.
- 00:41 Come over here and click File > Save.
- 00:43 You see it right here says Ctrl+S as well.
- 00:45 And let's head back over to our terminal.
- 00:47 Now if you press the up arrow on your keyboard,
- 00:50 it will go to the last command you typed in.
- 00:52 So we don't have to keep typing in python, hello.py every time.
- 00:55 We can just type up on the keyboard and then hit Enter.
- 00:59 When you do see the whole thing cleared.
- 01:01 And right up here at the top says Hello world!
- 01:03 Now if we come down here and fill up the screen with a bunch more stuff, and
- 01:08 then run this file again.
- 01:10 Again, boom, it clears everything and then runs the program.
- 01:12 So that's what these two lines of code do.
- 01:16 We're important a module.
- 01:18 We don't really care what that means.
- 01:19 We'll talk about that much later.
- 01:21 And inside that module,
- 01:22 we can run this little command that just clears the screen.
- 01:26 So we're going to keep this at the top of all of our Python programs, so
- 01:29 that every time we run a program it just clears the screens.
- 01:32 It's a handy little thing to do.
- 01:33 For the rest of this video I want to talk a little bit more about what this
- 01:37 line of code is.
- 01:38 And I also want to talk about comments.
- 01:39 So first things first this is a print function and
- 01:42 we're going to get into functions much later.
- 01:46 But basically all this is doing is you could probably guess,
- 01:48 it's just printing something to the screen.
- 01:50 And what is it printing?
- 01:51 Whatever is inside these quotation marks, right?
- 01:54 Again, we're going to talk about functions later on and learn how to create our own
- 01:58 functions and learn all kinds of cool things about already made functions.
- 02:02 But for now, that's really all we need to know about this.
- 02:05 For the rest of this video, I want to talk about comments very quickly.
- 02:07 So a comment is created with a hashtag, a number sign.
- 02:12 And we can just type in whatever our comment is.
- 02:15 So we might type in print hello world to the screen.
- 02:20 And you'll notice everything is kind of grayed out.
- 02:24 Now if we save this and come back over to our terminal and run this again, and
- 02:28 notice that comment doesn't appear here.
- 02:31 And that's what comments do.
- 02:32 They're ignored by the program.
- 02:35 They're just for us as coders to sort of jog our memory of what the code is.
- 02:40 This seems kind of silly to write print Hello World to the screen,
- 02:44 the comment is longer than the actual code.
- 02:46 And normally, you wouldn't put a comment right here.
- 02:49 What you want to comment are big changes in your code,
- 02:52 big blocks of code that do new things.
- 02:54 And it's important because over time,
- 02:56 you're kind of forget what your code is supposed to do.
- 02:59 It just happens.
- 03:00 And you may have to come back six months from now and fix something,
- 03:04 update something, tweak something in your code, and you'll look at it and you go,
- 03:08 what in the world was I trying to do there.
- 03:10 And you won't remember because coding isn't always
- 03:12 obvious if you just glance at it.
- 03:14 Comments as so important and
- 03:16 you just want to be brief, just a few worlds that's really all.
- 03:19 It's also good if your working with teams and
- 03:21 you have other people looking at your code.
- 03:23 They need to know what your code is supposed to do so comments help with that.
- 03:27 There's lots of ways you can do comments.
- 03:28 You can do more than one line, as many as you like.
- 03:33 Some people like to do things like kind of wrap a comment in a comment, right?
- 03:41 That's kind of overboard, but this might be a good idea if you
- 03:46 have a major block of code and you really want to write three or
- 03:52 four different lines of code, you might do something like this.
- 03:55 It used to be popular to, at the beginning of your code, do something like this.
- 04:00 You could, let's go ahead and delete this.
- 04:05 And at the top and here you might explain what this program is supposed to do and
- 04:12 then you might go you Copyright (c) John Elder 2019.
- 04:17 That's kind of a popular thing that the people used to do.
- 04:19 You don't see that as much these days.
- 04:21 But you can do that.
- 04:22 So those are comments super important.
- 04:24 And try and just get in the habit of creating a comment for major changes in
- 04:29 your code, major new blocks of code and we'll get into this more as we go on.
- 04:33 And it will become apparent to you as a coder or when you should comment,
- 04:36 when you should not, it's just best practices.
- 04:38 So I should note, you can comment on lines of code too.
- 04:42 This is a comment.
- 04:44 And as soon as the Python interpreter sees the hashtag from there till the end of
- 04:48 a line it sees that as a comment.
- 04:50 So it'll still run this, it will just ignore this, right?
- 04:54 That's kind of cool, so those are comments.
- 04:57 But before we move forward very quickly for the rest of the course I'm going to be
- 05:01 using a different development environment than what we've set up here.
- 05:05 And I can pull this right up here.
- 05:06 This is what I'm going to be using.
- 05:07 This is called cloud nine, it's a cloud development tool.
- 05:11 It has everything in one screen.
- 05:13 So it has the files here on the left, it has the code here in the middle.
- 05:17 As the terminal down here at the bottom.
- 05:19 So whenever I type something in here, you follow along and
- 05:24 type it here in your Sublime Text, obviously right?
- 05:28 Anytime I make a command down here,
- 05:31 you'll do the exact same command in your terminal.
- 05:34 It's just I use this because it's all one screen and it's easier for teaching.
- 05:38 This actually doesn't exist anymore.
- 05:40 Amazon bought this company and they recently closed it down.
- 05:43 So I used to tell my students to use this tool as well, but
- 05:46 you can't use it anymore.
- 05:48 So you'll just have to use the sublime text editor that we downloaded and
- 05:51 installed in the Git Bash terminal.
- 05:53 So this is what I'm going to be using throughout the rest of the course.
- 05:56 So you should be able to follow along with no problem.
- 05:58 Like I said, the commands I typed down here will be the exact commands
- 06:02 that you type in your terminal.
- 06:03 The code that I write up here will be the exact code that you write in Sublime Text.
- 06:08 You notice here on the left, you have this directory of files.
- 06:12 It's the same directory that I'll have right here.
- 06:15 So anytime I add a new file over here, you'll do the same thing over here,
- 06:21 create a new file there then it will pop up.
- 06:25 You save it just like we saved the first one.
- 06:28 Save as file2.py whatever.
- 06:31 You see what pops right up.
- 06:33 You can toggle between them and should be pretty simple.
- 06:35 So that's all for this video.
- 06:36 In the next video we're going to jump in and talk about variables.
Lesson notes are only available for subscribers.