Skip to content
CBT Nuggets
DemoBook a Demo

Writing and Running Python Code

Full skill from PCEP. Preview the IT training 23,000+ organizations trust.

1h 5m

Skill 1 of 26 in PCEP

How to Run Python Code

Welcome to this PCEP course, where we'll be learning everything you need to know to pass the PCEP exam. In this first skill, we'll be taking a look at the basics of Python, and the first thing you'll need to know is how to run the Python code that you write. Let's take a look at 3 very popular ways of writing and running code in Python.

Also, as I mention in this video, the process for installing and setting up Python is going to be different depending on what operating system you're using, so here's a link to a very helpful article that shows how to do this on most operating systems.

Knowledge Check

The command for starting a Python REPL in the terminal is __________

Basics Python Syntax Concepts

Now that we've seen the basics of running Python code, the next thing I'd like to talk about are some basic Python syntax concepts. Let's do it.

Knowledge Check

In Python _________ is/are generally used to group blocks of code together.

Declaring Variables

Next, let's talk about variables in a little bit more depth. In this video, we discuss how variables are used, how they're interpreted by Python, and some of the basic data types that variables can contain.

Knowledge Check

When declaring variables, Python requires us to explicitly say what type of data the variable will contain.

Naming Variables

Now that we've talked about variables and how to declare them, let's go through some of the rules governing variable names.

Knowledge Check

Which of the following is not a valid variable name

Challenge & Solution: Writing Simple Programs with Variables

Now it's time for a challenge! In this challenge, you'll be asked to use what you've learned about variables so far to create some simple programs. Watch the video for more information.

And now that you've attempted the challenge, I'll show you how to solve it.

Knowledge Check

Which example did you find the easiest to implement?

This interactive assessment is available in the full learning experience.

Want to answer questions like this yourself?
with no purchase required. Already have an account?

View Transcript

How to Run Python Code

0:00Hi, Sean here and welcome to this course where we're going to be learning all

0:04of the topics that

0:04you'll need to know to pass the PCEP exam that is Python certified entry level

0:11programmer.

0:12Now, there's going to be a lot of topics that'll be covered here, but

0:17essentially what we're going

0:17to be doing is learning all of the basics of Python in depth, right? So we're

0:23going to go into the

0:25syntax of Python, we're going to obviously be seeing how to do things like run

0:29Python in different

0:30contexts. And we'll also see how to build a few simple projects in Python among

0:36many, many other

0:37things that we're going to do in this course. So the first thing that we're

0:41going to take a look at

0:42here is the basics of writing and running a simple Python program. Right, the

0:50fact is that all of

0:51the stuff that we're going to learn here, right, all of the Python syntax, all

0:55of the details that

0:56we'll learn is going to depend on your ability to run Python code. So it's

1:01pretty important that you

1:03just right off the bat get used to running Python in different contexts. So

1:10first of all, if you

1:12don't already have Python installed, what you're going to want to do, I've just

1:16because there's so

1:17many different operating systems out there and the process for getting Python

1:21set up varies so

1:22much between the operating systems. I've included a link to a pretty good

1:26article that shows how to

1:28set up Python on pretty much any computer. So feel free to follow that link and

1:34walk through the

1:35steps in it. And once you've got Python set up, you should be able to follow

1:40along with me here. So

1:42feel free to pause the video while you do that. And well, assuming that you now

1:48have Python set up,

1:49the first thing, the first way that I'm going to show you to run Python is just

1:56in the terminal.

1:57And first of all, the the IDE that you see me using here is called visual

2:02studio code, or

2:04that's often abbreviated VS code. And this is different from just plain old

2:09visual studio. VS

2:11code is basically just a nice straightforward development environment that you

2:17can pretty much

2:18add any plugins you want for whatever programming language you're using. So I

2:22find that it's great

2:24for Python development. And really, it's just easy to use. So I highly

2:28recommend it if you have

2:30another thing that you want to use, whether that's for your job, right, if the

2:33company you're working

2:34at uses something else, feel free to use that instead. That's perfectly fine.

2:38But anyway, what

2:41you're going to want to do first in order to do this is open up a terminal. So

2:45my IDE has a built

2:47in terminal. And if you're using visual studio code, you can either open this

2:50up with control

2:52or command J, or you can just go to terminal and do new terminal here. All

2:58right, that'll open up

2:59that window for you. Cool. So inside this terminal, what you should be able to

3:04do is run Python three,

3:06just that command. And what that'll do is it'll actually open up a Python read

3:13evaluate print loop

3:15for you. All right, so this is there's an abbreviation for this in case you

3:19aren't familiar with this

3:20term, our EPL, basically just refers to a simple program where you can type in

3:28Python statements,

3:30right, or Python expressions, and hit enter. And you can see the results of

3:35those statements. So

3:36the simplest thing that we're going to do here is we're just going to print

3:41something out to the

3:42console. And in Python, the way that you do this is by saying print, and then

3:48inside the parentheses

3:50that you put after print, which is how you call functions in Python, something

3:54that we'll talk about

3:55in much more detail a little bit later, you're going to add a string. And in

4:00Python, you know,

4:03strings just represent some sort of text. In Python, you can use either single

4:07or double quotes.

4:08Again, we'll talk about this in more detail later as well. But we're just going

4:11to say something

4:12like hello, Python. And if you hit enter now, sure enough, you're going to see

4:18that what

4:19happens is the read evaluate print loop, right, the REPL, our EPL, is going to

4:27execute the code

4:28that we entered in. And it's going to print out the results, right? So the

4:32result of calling

4:34print hello Python is that this program simply prints out hello Python to the

4:39console.

4:40Now you can do pretty much anything you want in a read evaluate print loop, a

4:46REPL.

4:48So if we want to create variables, and once again, I'm going to talk about this

4:52in more detail later,

4:53I'm just kind of showing you more the mechanics of running Python code than

4:59actually getting into

5:00the syntax yet. But we can define variables. So we can do that like, we can do

5:04that by saying

5:05things like x equals five, and we can hit enter. And as you can see, this doesn

5:09't have any direct

5:11result, right, creating a variable doesn't have any output as did printing

5:15something out to the

5:16console. So we're going to say x equals five, y equals, I don't know, let's

5:21just pick another

5:22number six. And if we say x plus y now, well, that does have a result, and that

5:29is 11, right,

5:30it adds together the values of these two variables. So again, this is just the,

5:36this is just the

5:37basic read evaluate print loop, this is really great for doing things like

5:41running very simple

5:42Python programs or experimenting around with different Python statements, right

5:48, as you're learning

5:49Python syntax, this can be a great place to come and just kind of, you know,

5:54play around with

5:54different expressions, try changing things, see what happens, right? So you

5:58could say, you might try,

6:00you might see if spacing matters in Python, as far as like having space in

6:04between the variable

6:06name and the value. So you could come back here and try changing x by saying x

6:10equals seven with

6:11no spaces. And you could see that that works, right? So again, this is just a

6:16tool that you can use

6:18to run simple Python expressions and statements. So, you know, feel free again

6:25to as you're learning

6:26different pieces of syntax come, come back here and try them out. It's just, it

6:31's a little bit

6:31easier than having to write the Python code in a file and run it manually. And

6:37speaking of writing

6:38Python code in a file, that's the next thing that we're going to take a look at

6:42here. So now that

6:43we've seen how to use the Python REPL, which again, you can open up just by

6:48just by typing Python

6:49three. And by the way, you can exit this by saying exit with two parentheses

6:55after it, that's going

6:56to close this read evaluate print loop by calling this exit function. All right

7:03, well, if we want

7:05to write our Python code in a file, which very often we will just because this

7:09makes it a lot

7:10easier to reuse that code, come back to it later, make changes, etc. All you

7:16have to do is create a

7:18new directory. So I've created this directory called PCEP examples. And inside

7:24here, what we'll do is

7:25we'll just say something like my first program dot pi, all right, so that dot

7:33py extension is

7:36what Python files will always have. So if you see a file with dot pi, you'll

7:41know that it's a Python

7:42file. And inside here, what we're going to do is we're going to type, we're

7:47just going to write

7:47some simple Python code. So I'm going to take things up a little bit and define

7:52two variables. So we'll

7:54say x equals five, y equals six, just like I did in the in the read evaluate

7:59print loop. And then

8:03I'm going to actually print out the sum of those two by saying print x plus y.

8:11And in Python, or

8:14rather, when we're writing Python in files, as we are here, this print function

8:19is going to be

8:20especially important because unlike in the terminal, right, when we were using

8:25the

8:25REPL to run our Python programs, when we're running Python code in a file, it's

8:32not going to

8:33print out the results of every statement or every expression by default, right?

8:39It's not, you know,

8:40if we were to just say x plus y here, instead of print x plus y, oops, let's

8:45try that again,

8:45there we go. If we were to just say x plus y and run this code, it wouldn't do

8:49anything. So by

8:50adding that print function, that's actually going to print that out to the

8:55console where we can see

8:56it. These are just little details that you'll get used to as you work with

9:00Python more and more.

9:01So don't worry too much about remembering all of them right now. But anyway,

9:05now that we've written

9:06a little bit of Python code in this file, let's run it. And you can run Python

9:12files by saying

9:13Python three, right? So that same command as the way that we ran or the way

9:18that we opened up that

9:20REPL. And then after that, you're going to put the name of the file that you

9:25want to execute. So

9:26I'm going to say my first program dot pi and make sure you write all of that

9:30correctly. If you change

9:31a letter to it won't work. So if we had enter, sure enough, we see that that

9:35will print out 11,

9:37right? x plus y here to the console. And just to demonstrate what I said before

9:42, if we were to

9:42just say x plus y without the print function, and run it again, sure enough,

9:47you'll see that

9:48nothing actually gets printed out. So again, when you're running Python in a

9:51file, the only

9:54things that are going to get printed out that we write from our code are things

10:00that we specify

10:01with that print function. Okay, so let's just add that back and run our file

10:05again to make sure

10:06everything is looking good. So that's the second way to run Python code. And

10:11really, these two ways

10:13are going to be are going to be are going to form the majority of the examples

10:17that we'll do in this

10:19course. There are other ways, though, one way, which you've undoubtedly heard

10:24people talk about

10:25is by using something called a Jupiter notebook. All right, Jupiter with a

10:31clever little pie here

10:32right Python, or py for Python that is, this is sort of like, this is sort of

10:40like what we saw with the

10:42read evaluate print loop in the terminal. But it's much more interactive and

10:47visual. So we're

10:48actually going to be going through just a we'll do a skill about how to work

10:52with Jupiter, just

10:54the very basics of running Python in a Jupiter notebook a little bit later in

10:58the course. So if

10:59you're curious about that, feel free to jump ahead and take a look at that

11:03skill. But anyway,

11:07that's one more way that you can run Python code and a very common way at that,

11:12especially if you're

11:12getting into something like data science. But you can also use there's plenty

11:18of online interpreters.

11:20And by the way, this word interpreter, we'll talk about that in a little bit

11:25more detail later. But

11:27this is Python itself, unlike languages like C++, which are compiled languages,

11:35Python is

11:36actually what's called an interpreted language, right, which basically means

11:40that this code that

11:42we're running doesn't get converted into a bunch of binary before running it.

11:48As would be the case

11:50with C++, right? So with C++, you talk about the compiler with Python, you talk

11:55about the

11:56interpreter. So in other words, when we run Python three with the file name, we

12:01're telling the Python

12:02interpreter to execute the code that that file contains. All right, but anyway,

12:07there's lots of

12:08online interpreters, one that I like, just for basic demonstrations is called

12:14replit.com.

12:15And it's pretty nice, especially if you're having trouble getting Python set up

12:20on your

12:20computer, or if you want to be able to code interactively or collaboratively

12:25with other people,

12:26right, you can actually work on the same Python code at the same time, which

12:30can be great for

12:31things like tutoring or, you know, doing a hackathon, right, if you're, if you

12:36're working on this with

12:37a friend. But anyway, the way that this works is pretty simple. It's, it's

12:41pretty much like an

12:42online version of visual studio code that we've been working with here, right,

12:46just like how visual

12:47studio code has a space for you to write code in files, and a space for you to

12:53work with the

12:53terminal. Over here in replit.com, you have your files over here, and you can

12:59create new files just

13:00by creating them over here on the left hand side. And you can also run that

13:06code by clicking this

13:07run button. So again, if we do x equals, let's do 10 this time, and maybe y

13:11equals 20. And then

13:14you say print x plus y. Well, if you click this run button, what that's going

13:18to do is run your

13:19code, and print out the output right here in the console. All right, so that's,

13:24um, again, this is

13:26just a really nice way to work with Python without having to worry too much

13:30about get, about getting

13:32things set up. So if you want to follow along in this instead of with visual

13:36studio code, feel free

13:38to do that. It'll work at least while we're learning the basic syntax for, you

13:43know, as just a basic

13:44way to follow along. So anyway, those are three basic ways of writing and

13:49running Python code.

13:51We're going to be using pretty much all of these at some point in the, in the

13:56skills of this course.

13:57So what I'd like you to do right now is just take a few minutes and play around

14:01with these

14:02right simple programs experiment around with just the, even just the simple few

14:06things that we've

14:06learned so far, defining variables and printing things out, just, just so that

14:11you get a feel of

14:12doing it and can kind of do it by heart, right? That'll, uh, that'll make

14:15everything that comes

14:16after this a lot easier for you. So anyway, I hope this has been informative

14:21for you, and I'd

14:22like to thank you for viewing.

Basics Python Syntax Concepts

0:00All right, so now that we've seen the basics of writing and running Python

0:04programs in

0:05a few different contexts, let's move on now to talk about the basics of Python

0:11syntax.

0:12Right now, we're going to be learning about Python syntax in quite a bit of

0:15detail.

0:16We're going to go into a lot of depth in different parts of Python's syntax.

0:21But what I'd like to do here before we get into all of those details is take a

0:26look at

0:26just some of the basic concepts of Python syntax.

0:31So the first thing to know is that in Python, instead of using things like

0:38curly braces,

0:40right, so in JavaScript, for example, with curly braces, what you would usually

0:45do is

0:46use them to define blocks of code.

0:48We use these to group pieces of code into things like functions or classes,

0:53things like that.

0:55So in JavaScript, if we were to create a function, which is just a reusable

0:59piece of functionality,

1:01we'll talk about functions in Python in more detail shortly.

1:04But if we were to create a function called something like my function in

1:08JavaScript, we

1:08would actually use curly braces to sort of denote where the body of that

1:14function starts

1:16and ends.

1:17However, in Python, you're not really going to see too many of these, at least

1:23not used

1:23in that context.

1:25In Python, what you'll see is that almost always indentation is used to group

1:32code.

1:33Now what this means is that unlike in many other programming languages with

1:38Python, you

1:39can't just indent things when you want to, right?

1:42So if we were to indent this print x plus y thing, you'll see right away that

1:46in the

1:47IDE, it recognizes that there's something not quite right with that syntax.

1:52And if you try and run this program, I will say Python three, my first program

1:57dot pi

1:57again, you'll see that instead of giving us the instead of giving us x plus y,

2:02which

2:03is 11 in the console, it says indentation error, unexpected indent.

2:07And it even shows us the line here.

2:09And then by the way, this is a, this is an error in Python when something bad

2:14happens,

2:15either, you know, whether that's something that we've written that doesn't work

2:19, or

2:19whether that's something in the code that breaks, you're going to see it

2:23printed out

2:24to the console like this.

2:27And usually it'll come along with a nice little, a nice little description

2:30describing

2:31where the error occurred, and what the problem is, right?

2:35So in this case, we see unexpected indent again, basically just telling us that

2:39we're

2:39not supposed to have that unexpected indent.

2:42So in Python, right, if we were to define a function, and again, we'll talk

2:46about this

2:47in more detail later, the way that we do that is by creating the function name.

2:52All right, so we could say something like define my function.

2:56And then the body of the function is simply going to be underneath that with a

3:01single

3:01indentation before each line, right?

3:05So you know, we could say x equals one, y equals two, z equals three, right?

3:11All of those are going to start a little bit with a little bit of an indent

3:16ation past

3:16the function name itself.

3:19And again, this is how this is how code, right, different lines of code that

3:23are related

3:24to one another are grouped together in Python.

3:28We don't use curly braces or other symbols like that to do the job.

3:33All right, so we'll talk about the indentation rules in much more detail later

3:37as well.

3:38But that's just a main concept that I wanted you to understand in Python is

3:43that indentation

3:44is very important for grouping your code together.

3:48All right, so besides that, in Python, a few other syntax related things that I

3:55wanted

3:56to mention is any line that you write, such as this x equals five, y equals six

4:02, print

4:02x, y thing, is by default, when we run our code, the Python interpreter is

4:07going to try

4:08and execute that.

4:09So if we write something that isn't valid Python syntax, right, if we write

4:14something

4:14like hello, Sean, right, something like that, well, that's not valid Python

4:20syntax.

4:21And if we try and run our code with that in it, it's going to say, yeah, I

4:24really don't

4:25know what that is.

4:26It's going to give us a syntax error in this case saying invalid syntax.

4:30However, in many cases, it might be nice for us to be able to, you know, write

4:35things like

4:36notes next to pieces of our code so that we can remember what they do.

4:41Right.

4:42In this case, with x and y, why are we setting x to five and why are we setting

4:46x to six?

4:47Well, it might be really nice to sort of leave a note for ourselves.

4:51Right.

4:52If we say something like x is the number of people invited to the party,

4:59something like

5:01that.

5:02Right.

5:03Again, because we've written this in the file, it's going to cause a syntax

5:08error if we were

5:09to try and run this, right.

5:10And I'll just show you that again.

5:12Yep.

5:13Sure enough, it doesn't really know what we're trying to do there because again

5:15, that's not

5:16valid Python syntax.

5:18So if we want to write notes next to our code, we can do that by using

5:23something called comments

5:25in Python.

5:27These are a very common thing that are used really no matter what programming

5:31language

5:31you're using.

5:33And comments in Python are indicated by using a number sign, right, or hashtag

5:37as the young

5:38folks these days are calling it.

5:41So by putting a number sign at some point in our Python program, we're saying

5:46that whatever

5:47comes after that number sign on the line, we want Python to completely ignore.

5:53Right.

5:54So Python's not going to try and run this now.

5:56And if you run the code, sure enough, what we'll see is that it, you know, it

6:01doesn't

6:02have a problem with that now.

6:03We could write really anything we want after this little number sign.

6:08And it's really for our eyes only the Python interpreter isn't going to look at

6:12it.

6:12All right.

6:13So we could say X is the number of people invited to the party.

6:16Maybe this is like why is the number of people who showed up uninvited.

6:24Oops, that's uninvited.

6:27There we go.

6:29And then you could say the total number of people at the party, right?

6:34And this, the, you know, the idea behind this is that these comments would make

6:40it much

6:41easier to remember what this code was supposed to do when we look back at it a

6:47day later

6:47or a week later or a month later.

6:49So especially as you're learning Python and getting comfortable with the Python

6:55syntax

6:55and writing your own Python programs, you're going to want to make liberal use

7:00of these

7:01comments because they'll allow you to, again, remember what was going on.

7:06So as we learn how to do things like declare variables, right, which we've done

7:09here with

7:10X and Y, you could say something like, uh, this is how to declare a variable,

7:16right?

7:17And that's all you need to do there to remember.

7:19So in this way, uh, I would highly recommend you do this as you're going

7:23through all of

7:24these videos.

7:25In this way, you can have kind of, uh, all of the example code that you write

7:29will end

7:29up being just, it will end up being sort of like a cheat sheet for you that you

7:34can use

7:34to review everything that you've learned so far.

7:38All right.

7:39Um, so anyway, that's really the, the main pieces of syntax that I wanted to

7:44cover in

7:44Python will cover a lot of other things such as how to declare variables, uh,

7:49simple,

7:49uh, arithmetic operations, declaring functions, things like that, uh, in later

7:55videos and

7:56skills, but that's, those are really the main two things that I wanted to, uh,

8:00show

8:01you here because I'll be using them quite a lot going forward.

8:05Uh, so just as a review here, the first thing is that indentation in Python is

8:10very important.

8:12It's used to group together pieces of code.

8:15Uh, so as we start seeing how to do things like loops or functions, things like

8:20that,

8:21we'll be using indentation and only indentation to group everything together.

8:26So that's really something that you've got to get used to in Python, which is

8:29why I wanted

8:30to mention it right up front here.

8:32And the second thing here is that you can create comments by using the little

8:38hashtag

8:38or number sign.

8:40So that's really all that I wanted to go over here, uh, in terms of general

8:46Python syntax.

8:47So I hope this has been informative for you and I'd like to thank you for

8:50viewing.

Declaring Variables

0:00Okay, so at this point we've seen the basics of running Python programs and we

0:04've also

0:05talked about just a few fundamental concepts in Python syntax.

0:10So let's move on and start taking a look at a few useful tasks.

0:15If you want to call them that, that you're going to be performing in any Python

0:20program

0:21that you write no matter what it is.

0:22All right, so what we're going to take a look at here is the basics of working

0:26with variables

0:27in Python.

0:29Now we've already seen how to declare variables when we said x equals five and

0:34y equals six.

0:35So essentially when you're working with variables in really any programming

0:41language, there

0:42are really two different ways that you might use them.

0:47One is basically you're just giving another name to a value.

0:54So you could say something like pi equals 3.14159.

1:00However far you want to go there, right?

1:03And in this case, the name pi is really just another name for this value here.

1:10So if you were to say, you know, if you were to use that later on, if you were

1:14to say things

1:14like print pi or if you were to multiply something by pi later on in your

1:20Python program, you

1:22could just use that name pi instead of having to type out this value each and

1:26every time.

1:27So that's really the first usage.

1:30And in recent years, this is actually an increasingly common usage of variables

1:38, even in situations

1:39where you might otherwise want to change things.

1:43Don't worry too much if that doesn't make sense.

1:44I'll discuss that in more detail later.

1:47But the second way that you might think about a variable is as sort of a

1:51container that

1:52might hold different values over the course of its lifetime.

1:56So, you know, if you say something like x equals 5 and then later on, you

2:01change the value

2:03of x.

2:04So you say x equals 10 and then maybe you add something to x by saying x equals

2:09x plus

2:10one, which would be incrementing x.

2:12Well, in this case, we can't really say that x is another name for any of these

2:18values,

2:19right?

2:20x doesn't mean 5 because later on in the program, x could mean 10 or 11 or

2:24really anything else.

2:26So that's kind of the other potential way of thinking about variables is as

2:31containers

2:32that can hold different values over their lifetime.

2:35And as you're going to see, you know, as we go through learning Python syntax,

2:39we're

2:40going to actually use both of these ways of thinking in our program.

2:45So these are really just tools that you can use, right?

2:48Ways of thinking that you can use in order to really understand the purpose of

2:53variables.

2:53Now, just as a sort of side note here, this first way, right, since the

3:00variable value

3:01doesn't actually change and therefore it's not truly a variable in the meaning

3:07of something

3:08that varies over time.

3:11This is sometimes referred to as a constant.

3:15So really a constant is just anything whose value doesn't change and therefore

3:19remains

3:20well constant.

3:22But Python, unlike a lot of other programming languages, doesn't actually have

3:27a, it doesn't

3:28actually have a keyword that will prevent you from changing the value of a

3:34variable.

3:35So if you want something to actually be constant, you have to be a little bit

3:38disciplined and

3:39not change it later on, right?

3:41Later on, if we were to say pi equals four, well, that's not really a constant

3:44anymore,

3:45right?

3:46And from that point on, pi would have a different value.

3:49So you might hear me use the word constant sometimes when talking about this

3:52way of doing

3:53things and variable when, you know, talking about this other way of doing

3:58things where

3:58the value actually changes.

4:01But really in Python, they're technically all variables because they can all

4:06change.

4:07So variables, we've seen, we've already seen how to create two variables here,

4:13but variables

4:14can store a pretty wide range of data.

4:17In fact, pretty much any thing that you can, you know, any type of data that

4:21you might

4:21have in a Python program, and we'll talk about these types in much more detail

4:26later

4:26on.

4:27I feel like I keep saying we'll talk about these later on and we will.

4:32But besides simple integer numbers, like five or six, right, counting numbers,

4:37again,

4:37we'll talk about integers and different number types later on.

4:41But besides those, you can also store things like text in variables.

4:46So if you wanted to say something like message equals hello, right, you can do

4:52that in Python.

4:53If you wanted to store something like just a yes or no value, well, that's

4:57something

4:57called a Boolean in Python.

4:59And again, we'll get into more detail on that later.

5:02But that might look something like this.

5:03We could say something like Sean likes coffee equals true.

5:10And in Python, by the way, in case you're already familiar with true and false

5:13values

5:13from another programming language in Python, there's, there's really only two

5:18values here

5:18true with a capital T or false with a capital F, right?

5:24Those those represent really yes and no, respectively.

5:28And besides this variables can also hold more complex types of data.

5:33And we'll cover these later on as well, but they can hold things like lists,

5:37which in

5:38Python are just ordered series of values.

5:42So we could say something like lucky numbers, something like that equals 123.

5:51And in this case, this variable is holding multiple values, right?

5:56So a variable again is really just kind of a label for some other value that we

6:03want

6:03to work with later on in our program.

6:06And we want to work with it by name instead of again, having to specify the

6:09value each

6:10and every time.

6:12So anyway, that's some some of the different data types that variables can

6:16store.

6:17And in case you're eager to know the names of these things, these up here are

6:21called

6:22numbers or more specifically integers in Python.

6:26We'll go into more detail on how those work later, as I've said, these are

6:30called strings.

6:31So really any piece of text in Python represented like this is called a string.

6:37These as I said are called booleans and they're just yes or no values, one or

6:42zero, right?

6:43And these are called lists.

6:45And these are just a few of the data types that you can use in Python.

6:49All right.

6:50But anyway, really, the only point that I want to make at this time is that

6:54variables

6:55can store pretty much any type of data that we want.

6:59And we can change it whenever we want, as I've said, right?

7:02So let's take a look at just one possible, very simple application that you

7:08might want

7:09to use this whole variable thing for.

7:13Let's say that we have a few numbers, which are called x, y and z.

7:16And you want to add all of them together, but you want to do it like one by one

7:20, right?

7:21So you want to add x, you want to add y, and then you want to add z.

7:24There are multiple situations where you might want to do this in Python.

7:28For example, if you want to add all of the numbers in a list, which is

7:31something again

7:32that we'll take a look at later.

7:35But in case you in case you don't know what that means, just follow along with

7:38me here

7:39for a second, we could say something like sum equals zero or total equals zero.

7:47And then we could just go through and add each of these variables one by one.

7:52So we could say total equals total plus x.

7:57And then total equals total plus y, and then total equals total plus z.

8:05And after we do that, we could just print out total and total would contain the

8:11sum

8:11of all of those values.

8:13So if we run our program here, let's just run Python my first program dot pi

8:18again, we'll

8:18see that that gives us 18.

8:21All right.

8:22And basically, in cases like this, all that happens, at least when you're

8:26working with

8:27simple data types, all that happens is Python basically goes through and

8:33replaces any instances

8:35of that name, right, that variable name with the current value of that variable

8:42.

8:42So when it sees total, it's going to look, okay, cool.

8:46What's the current value of total?

8:48Oh, it's zero.

8:49Okay.

8:50Well, that means zero.

8:51And then it comes over here to x.

8:52It says, okay, what's the current value of x?

8:54It looks to see that it's currently five.

8:57So it basically just kind of substitutes that in there.

9:01And that gives us zero plus five.

9:04This whole thing turns into the value five.

9:06And total is now equal to that, right?

9:09That's sort of the process that Python goes through when it's executing our

9:13lines of code.

9:14All right.

9:15So now that total is five here, and let's just delete that now, now that total

9:20is five,

9:21the next line, total again is going to be replaced with five.

9:25And it's going to look and see what y is.

9:28It's going to see that y is six.

9:29So it's going to turn that into five plus six, which is equal to 11.

9:33And total is now 11.

9:36And lastly, it's going to substitute 11 for total.

9:40It's going to substitute seven for z.

9:43And that ultimately gives us 18.

9:45So total is now 18, which means that when we go to print the total, we're

9:49really just

9:50saying print 18.

9:53Okay.

9:54Now, obviously variables are much more useful than what I'm making them out to

9:59be right

10:00here.

10:01Really, they what they allow us to do is work with data in a slightly more

10:06abstract way.

10:07Right?

10:08So it allows us to say things like, you know, a equals two B plus C or

10:14something like that.

10:17And we don't need to actually know ourselves, right?

10:21We don't need to actually know as humans what B is or what C is.

10:26We just know that A is two times B plus C. All right.

10:31That's really all that we need to know.

10:32And that's where, again, variables allow us to work with data in the abstract.

10:37You'll, I think you'll get a sense of this, a much deeper sense of this as we

10:41continue

10:42learning about Python.

10:43But anyway, that's the basics of declaring variables and just a few things that

10:49you can

10:49do with them.

10:51One thing that you'll notice in Python is that there are a few, there are

10:55actually a

10:56lot of shortcuts, right?

10:59That allow us to write less code and achieve the same thing.

11:02So as an example, if you look at these three lines here, you'll see that there

11:06's a little

11:07bit of repetition, right?

11:08We're saying total equals total plus X, total equals total plus Y, total equals

11:13total

11:13plus C, right?

11:14Well, it would be nice if there was a shorthand way that we could just say

11:18something like

11:19add X to total, not total equals total plus X, right?

11:23And as a matter of fact, there is this is something that you'll see very

11:26commonly used

11:27in Python.

11:29In fact, you'll almost never see or you'll very rarely see what I wrote here.

11:34What you'll see instead is total plus equals X, all right?

11:40And total plus equals Y and total plus equals Z.

11:46And again, this basically means add X to whatever the value of total is.

11:52You might also think of this as saying something like increment total by X or

11:55increment total

11:56by Y.

11:57So I hope this has been informative for you and I'd like to thank you for

12:00viewing.

12:01[BLANK_AUDIO]

Naming Variables

0:00All right, so now that we've taken a look at the basics of declaring variables

0:04in Python,

0:05the next thing that I'd like to talk about before I give you a challenge to

0:09kind of sum

0:10up what we've learned so far, the next thing that I want to talk about is the

0:15rules around

0:16naming variables in Python.

0:19So so far, the variable names that we've used have been pretty straightforward,

0:23right?

0:23We have x, y, z and total.

0:26So in many cases, you're actually going to want to have your variables be much

0:30more descriptive

0:32than this.

0:33Frankly, just because it makes it easier to read your code when your variable

0:38names are,

0:40you know, when your variable names are much more descriptive again, picture

0:42that we were

0:43to say something like a equals x plus y plus z and compare that to something

0:51like total

0:52people at party, something like that equals people or invited people, you could

1:01even just

1:02say invited plus uninvited plus my family, something like that, right?

1:11That's a lot easier to tell what's going on in that, you know, in that line of

1:16code than

1:16if we were to just name our variables rather obscure, you know, letters or

1:24things like

1:25that.

1:26So keeping in mind that our variable names are often going to be a little bit

1:30more complex,

1:31sort of like what I just had here.

1:33Let's talk about some of the rules around naming variables.

1:36The first rule around naming variables is that they need to start with a letter

1:43or the

1:44underscore character.

1:46So we'll just write start with and then we'll say letter or the underscore

1:55character, right?

1:57That's what that is there.

1:58You just you hold down shift and type a dash character and that's what you get.

2:03So that's the first rule.

2:04And what this means is that you can't have variables that start with that start

2:08with

2:09numbers, right?

2:10That just doesn't make sense.

2:11And that interferes with Python's syntax, which is why it's not allowed.

2:16The second rule here is that the rest of the characters in the variable name

2:22can be either

2:23letters.

2:24So we'll say rest can be letters.

2:31They can be numbers.

2:32So you can include numbers in your variable names.

2:37So if you wanted to say person one or person two or something like that, you

2:44can.

2:44And the underscore is allowed as well.

2:47All right.

2:48So basically what that means is that variable names can consist of letters,

2:54numbers and

2:55underscores, but they can't start with a number.

2:58I suppose you could kind of combine those two rules into one.

3:00But anyway, the third rule is that variable names are case sensitive.

3:06So if you were to write something like, you know, person one, all in lower case

3:12, that's

3:12not going to be the same thing as if you were to write person one all in upper

3:16case, right?

3:17That's Python just isn't going to consider those to be the same thing.

3:23And a fourth one, just in case that wasn't obvious from the second rule here is

3:28that spaces

3:29are not allowed in Python variable names, right?

3:33So Python would think that those were supposed to be two separate variables or

3:37something

3:38like that.

3:39All right.

3:40And one last rule here is that you can't pick a variable name that is already a

3:46reserved

3:47keyword in Python.

3:49Now we haven't talked about reserved keywords in Python, but we actually have

3:52seen two of

3:53them.

3:54And that is when we, you know, when we were talking about how variables work,

3:58we saw

3:59that with Boolean variables, which are yes or no values, there were two values

4:04true with

4:05a capital T and false with a capital F. So as you might have guessed, you can't

4:11have

4:11a variable name with either of these names.

4:15You can't have a variable with either of these names because Python would get

4:20very confused,

4:21right?

4:22So we'll say no reserved keywords.

4:24All right, and that would again be things like true or false.

4:31So anyway, those are the main rules behind creating some variable names.

4:37All right.

4:38And I suppose let's just, I'll just go through a few examples of what are valid

4:42and what

4:43are not valid variable names according to these rules.

4:46And feel free to take a screenshot of this if you want.

4:49But anyway, as I said, the first example that I'll give you will be a valid

4:55variable name

4:56or you know what I'll start off by giving you an invalid variable name, an

4:59invalid variable

5:00name would look something like this 50% off, right?

5:05If we were, if we were to try and have a variable like that, that expressed

5:09whether something

5:10was on sale for 50% off, we're not allowed to have that as a variable name

5:14because it

5:15starts with a number.

5:17So in this case, you would actually have to say, you know, you'd actually have

5:20to type

5:21out the word 50.

5:23Or if you wanted to, you could fix it by putting a underscore before it, right?

5:29That would be allowed.

5:30Or you could say percent off 50, right?

5:35And you could, you know, you could put the 50 at the end.

5:38It just can't be at the beginning.

5:40So that's the first example I'll give you.

5:43And in fact, let me just show you this, we can actually use comments here to

5:47keep notes

5:48of this so we can keep that there and we can say no.

5:53And then we can create a better version of it where we say something like 50%

6:00off and

6:01that is allowed, right?

6:04So next example that I'll give you is something to do with the subsequent

6:08characters, right?

6:09Something except for the first character in a variable name.

6:14And that might be something like, you know, if you want to say user name, well,

6:18you might

6:18want to use, you know, you might want to separate those two words using a dash.

6:23And that's not going to work either.

6:24Here, let's just give it a value here, Sean.

6:28That's not going to work either because dashes are not allowed in words and in

6:33variable names

6:34rather.

6:35The question is that to Python, this looks like user minus name, right?

6:41So that just isn't going to work in this case.

6:44Dashes really aren't allowed.

6:46In fact, no symbol is allowed in variable names except for that underscore

6:51character.

6:51So we would have to change that to user underscore name if we wanted to do that

6:57.

6:57So let's just, let's just add that dash back in.

7:00I'm going to comment that out and I'm going to add no at the end of it.

7:04Like so.

7:05And then we'll add a fixed version underneath this.

7:07You can use this as notes if you want.

7:09We'll say username equals Sean.

7:11All right.

7:13So another example here, let's take a look at case sensitivity.

7:16Let's say that we were going to say something like print total.

7:19If we wanted to print the total out up here, well, that's not going to work

7:22because we

7:23don't have any variable called total all in caps like that, right?

7:28So that's not going to work.

7:29I'll just comment that out so that that doesn't, that doesn't stop our program.

7:33And by the way, the reason I'm commenting these out is because if we were to

7:36try and

7:37run it without them commented, we wouldn't get past that line, right?

7:41Python would run into this.

7:42It would produce an error and it wouldn't, you know, we wouldn't get to see the

7:47output

7:48of anything that came after that.

7:50So anyway, we could of course print the total.

7:54In fact, we've already done so up above here.

7:56So I'll just, in fact, just move that down there.

7:58All right.

7:59Cool.

8:00So next thing, no spaces.

8:03You know, obviously we can't have something like my favorite color equals, and

8:09then, you

8:10know, let's just say blue, you can't have that, right?

8:14Python has no idea what's going on there.

8:15It thinks that we're having, you know, things that one of these is supposed to

8:18be a keyword

8:19or something.

8:20It just doesn't like that at all.

8:22So we'll put no next to that as well.

8:25And that would have to be something like this, where we actually use

8:28underscores, and this

8:29is a very common usage of underscore characters in Python is to basically serve

8:36as spaces

8:36in the word.

8:38And by the way, you might also see in Python variable names that instead of

8:43using underscores

8:45to separate words, you might see something like this, where someone starts the

8:50first

8:51letter of each new word with a capital letter.

8:57And this is something that's referred to as camel case, right?

9:01The reason being that these look like camel humps apparently.

9:04Someone came along and thought that and well, the name stuck this rather

9:08fittingly considering

9:10the name of the program and language we're learning is called snake case, right

9:14?

9:15Because apparently someone thought that this looks like a snake somehow.

9:18Anyway, those are the names for those two different types of casing snake case

9:23in Python

9:24is usually used more often.

9:26You might see that someone coming from another language such as JavaScript will

9:30use camel

9:30case for, you know, for file names, but snake case is far more common from what

9:37I've seen.

9:38All right, so one last one last example that I'll give you fitting in with the

9:44fifth rule

9:44is if we were to try and say something like true equals, you know, true, maybe

9:49we wanted

9:50a variable called true that held the string true.

9:53Well, we obviously can't do that because Python thinks that this is a reserved

9:57keyword.

9:58And in fact, it is a reserved Python keyword.

10:00So we can't do that either.

10:02Let's just write no there like so.

10:05And you know, if we wanted to do something else like that, we could in theory

10:09use true

10:09or lowercase like that.

10:13But usually with with keywords like that, it's better to just find another, you

10:17know,

10:18it's better to just find another way to express that, right?

10:22You could say something like is true or true string or true text, something

10:28like that.

10:29Any of those would be better than just saying true equals true, right?

10:34One last thing that I wanted to point out here too is that aside from reserved

10:39keywords

10:39such as this true thing here in Python, and there are many other reserved

10:44keywords that

10:45we'll talk about later on in other skills, you might also come across things

10:50that aren't

10:52aren't actually reserved keywords, but are already something else.

10:55So this print function, for example, this already, you know, the word print

11:01already means

11:02something in our program just by default in Python.

11:05So we wouldn't want to say, you know, print equals hello, right?

11:11And the problem with this is we could do that.

11:14And we'll let us do this.

11:16But what that does is it gets rid of the default functionality of print, and it

11:21makes print

11:22just in this case, it makes it just a piece of text, right, a variable with

11:26some text

11:26inside of it, which is generally not what we want.

11:29And another instance of this, which, you know, especially in beginner

11:33programming exercises,

11:35you will see, you know, you'll be very tempted to use this is some equals zero

11:40sum is actually

11:42already a function in Python that you can use to get, well, the sum of a series

11:48of numbers.

11:49We'll talk about this in more detail later, but you don't want to do either of

11:52these things.

11:53So let's just comment those out.

11:54And we'll say this works, but overrides the print function.

12:06And then I'll say something like same here, some is a built in function.

12:11All right, so you don't want to do either of those things.

12:13You'd have to just find another word for it like we did up here, right?

12:17So total, you know, or for print, you could say something like message equals

12:22hello, right?

12:23That would just be a better way to do that.

12:26So we'll say total equals, but I won't use total now.

12:29I'll use something like count equals zero, right?

12:33So anyway, that is the basics of naming conventions and naming rules in Python.

12:39So I hope this has been informative for you,

12:41and I'd like to thank you for viewing.

Challenge & Solution: Writing Simple Programs with Variables

0:00All right, so at this point we've talked about some of the basics of Python

0:03syntax and we've

0:04also learned some details about Python variables. So it's time for you to solid

0:10ify what you've

0:11learned so far by doing a challenge. Now in this challenge, what we're going to

0:17do is I'm going to

0:17give you a few different situations that can be expressed by using variables

0:23and your job is going

0:24to be to write that situation as a Python program. So to give you an example

0:32here, let's say that

0:33I give you the situation of calculating how much of a tip you want to leave at

0:40a restaurant and

0:41using that to calculate the total bill. Well, if we think about what that would

0:47look like here,

0:48right, there are really going to be two main quantities involved. The first is

0:56the bill amount,

0:57right? So let's say that you go to a restaurant, you spend maybe $40 just to

1:03give you an example

1:04here. So let's try that again. There we go. And let's say that you want to

1:09leave a 15% tip,

1:11right? Well, so we'll just write that as tip percentage. And notice here that I

1:16'm not using

1:17variables yet. I'm just kind of drawing this out. And that can be very helpful

1:21sometimes to,

1:22you know, just to kind of work out your thoughts and get them more clear so

1:27that you can actually

1:28express them in code. So let's say that we want a 15% tip. And a few other

1:34things that we might

1:35know about this would be that the tip itself, right, is the way that you

1:41calculate the tip is by

1:43multiplying the bill amount times the tip percentage, whatever that number is,

1:51divided by 100, right?

1:53So we'll say tip percentage divided by 100. And that'll give you the tip amount

2:01, right?

2:02And finally, we would know that the bill amount, right, that the total amount,

2:07the bill plus the tip

2:09would be equal to the bill amount, plus the tip amount. All right. So those are

2:21just a few things

2:22that we know. And if you wanted to express this as a program in Python, here's

2:28what this would look

2:29like really the main purpose here is just to get you used to taking situations

2:34and mapping them into

2:36a fairly simple series of variables. So here's what that would look like here.

2:41What I'm going to do

2:42is create a new file, we'll call this something like variable challenge dot pi.

2:47And let's start off

2:50here by defining those things that we had previously as variables. So we would

2:57say something like

2:59bill amount, that would be a variable. And maybe that's $40. We'd have the tip

3:04percentage, which would

3:06be 15. All right, 15%. And from there, we can pretty much figure out the rest

3:14just by defining

3:15variables as combinations of these. So as I said, we could have the tip amount

3:23equal to the bill

3:25amount, plus, or sorry, times, and then we have the tip percentage divided by

3:34100. All right.

3:36So we now have the tip amount, which means that we can figure out the total

3:41bill. So we'll say total

3:43bill equals bill amount plus tip amount. All right. And what you can see there

3:52is if you print this

3:53out, right, we can say print, and we can print out any of this that we want. So

3:57if we want to see

3:58the tip amount, we can say something like print, well, here, we'll say the tip

4:03is, and then we'll

4:05print out the tip amount underneath that I'll show you how to actually add

4:11values into strings a

4:12little bit later. But then we'll say the total bill is, and then we'll print

4:20out the total bill

4:22underneath that. All right, so I'm going to run this now and what this will

4:25look like. Oops,

4:26let's rephrase. All right, so I'm going to run this now and what this will look

4:29like as we'll say

4:30Python three variable challenge dot pi. And sure enough, we see that the tip is

4:36$6. And the total

4:38bill is $46. So the cool part about this now, and you know, the reason that

4:43expressing

4:44situations in code like this is so helpful is we can now go up and change

4:51either of these two

4:52variables at the top. And everything else will still work, right? So let's say

4:56that we go to a

4:57restaurant now and we spend $100 and we want to tip 18%. Well, that's still the

5:04rest of the

5:04program is still going to work in exactly the same way. In other words, if we

5:08make a line of,

5:09so let's try that again, if we make a line of comment signs there just to kind

5:13of create a

5:14visual separation, we can we can basically change the entire rephrase. We can

5:22use this program in

5:23basically an infinite number of ways just by changing this stuff above the line

5:29. We don't

5:30really need to change anything down below. Unless of course we want to change

5:35the program

5:36significantly, right? So if we wanted to start calculating tax and add that in,

5:40well, we would

5:41have to add a tax percentage or something like that to this, maybe that's 4% or

5:47something,

5:47and we would have to incorporate that into the into this equation, right, this

5:53system of equations

5:55down at the bottom. But I'll just leave that off for now. And let's just run

5:58this again. And

5:59sure enough, we see that this works for those, you know, those modified numbers

6:03as well.

6:03So anyway, that's that's kind of the basic idea. So now that I've given you a

6:09demonstration,

6:10it's time for you to do a few of your own based on situations that I'll

6:14describe for you. All

6:16right, the first one, which should be pretty straightforward because it's

6:19pretty similar to

6:20the code that we just wrote, I'll give you that hint, is calculating the

6:25discount price.

6:27All right, there we go. Based on the original price and the discount percentage

6:35, right, so those

6:38original price and the discount percentage are going to be the two variables

6:41there.

6:41Another situation that you can do here is doing something like calculating the

6:48travel time.

6:49And this one would have probably two variables as well, the distance and the

6:57average speed. Okay,

6:59so again, you can express that as a as a program as well. And finally, I'll

7:05give you one more.

7:06Let's say that you want to calculate the total, your net calories for the day,

7:12right, the number

7:13of calories above or below zero based on diet and exercise. And in this one,

7:20what you could do is

7:21you could have like four variables, let's say you could have one variable for

7:26the calories for

7:27breakfast, one for lunch, one for dinner, and you could have a fourth variable

7:31for the number

7:32of calories you burned while exercising today. All right, so that would be,

7:37that's another program

7:38that you can feel free to work out on your own as well. Again, this is all just

7:43to get the,

7:43just to get the feel for defining variables and using them to express programs.

7:50So anyway,

7:51this is your challenge here. Feel free to give this as much time as you need,

7:55probably about five

7:56to 10 minutes. And once you rephrase. And once you've given this a try, feel

8:03free to move on to the

8:04next video where I'll walk you through the solutions. So I hope this has been

8:08informative for you. And

8:10I'd like to thank you for viewing.

Challenge & Solution: Writing Simple Programs with Variables

0:00All right, well hopefully you've given this challenge a try.

0:03Let me walk you through these solutions.

0:06So the first project or mini program

0:10that I gave you as the challenge,

0:11and I'm just gonna open up the variable challenge here.

0:14And in fact, I'll use the comment marks

0:17to kind of separate the first challenge

0:19or the demonstration that I did from the first challenge.

0:23This was to calculate the discount price of something.

0:29It's something in a store given the price

0:31and the discount percentage.

0:33Well, this was going to be pretty much like the tip calculator,

0:37except a little bit in reverse, right?

0:39We'd be subtracting the discount price

0:42instead of adding the tip.

0:44But here's what this looked like.

0:46This required two variables, the original price.

0:51All right, which you could have set to anything, right?

0:53You could have said maybe 199 as the price.

0:56And then for the discount percentage,

1:01which is the other variable that we need,

1:04let's just say maybe 35%.

1:06All right, so now we just need to kind of figure out

1:09what the basic equation is for getting the discount price.

1:14And here's what that would look like.

1:17We'll say discount price.

1:18And as I said, this is gonna be very similar

1:21to the tip calculator.

1:22We're just gonna say discount price equals original price minus.

1:27And then what we're gonna do is we're going to figure out

1:31how much we want to subtract.

1:34So if we're removing 35% of the price,

1:37what that means is we need to multiply the price,

1:41or multiply the original price by 35,

1:46or we could maybe subtract 35 from 100

1:50if we wanted to do it the other way.

1:51But what this is gonna look like,

1:53'cause I'm just gonna say original price minus discount percentage.

1:58All right, that's gonna be divided by 100

2:01because we need to actually get that into the right form.

2:04And then we're gonna multiply that by the original price.

2:07So this is removing 35% of the price.

2:09There are other ways you could have done this

2:11or slight differences in the equation that you could have done,

2:14and that's fine too.

2:15So what I'm gonna do is I'm just going to say print,

2:19and then we'll print out the discount price is,

2:24and then we'll print out the discount price underneath.

2:27So let's say discount price.

2:29And if we run this code now,

2:31let's try running Python3 variable challenge.py.

2:35Sure enough we see that the discount price is $65

2:38and around 64 cents.

2:40Don't worry about that long string of decimal digits.

2:44We'll see how to format strings and numbers

2:47a little bit later in our Python journey here.

2:50Anyway, that was the first solution.

2:53So the second one was calculating the travel time.

2:57So there were two variables for this one as well.

3:00The first was going to be the distance,

3:03and you can say this in miles or kilometers, whatever you want.

3:07I'm gonna say that it's in miles.

3:09So we'll say that the total distance is equal to maybe 100 miles.

3:15And then we'll say the average speed.

3:18We'll say that that's maybe 60 miles per hour.

3:22So now in order to figure out the travel time,

3:24all we have to do is say travel time equals distance

3:29divided by average speed.

3:33And that would give us the travel time.

3:34So we can print that out now by saying print.

3:37The travel time is, and then we'll say print travel time.

3:44And that should work.

3:46So let's try running this again.

3:47And sure enough, it looks like the travel time

3:49is around one and two thirds of an hour.

3:53And that looks about right to me.

3:54So let's do the third and final challenge,

3:57which is calculating the net calories for the day.

4:02Basically the calories you consumed

4:03minus the calories you burned through exercise.

4:06So let's just assume that we have all of these numbers ready.

4:10The first one would be breakfast calories,

4:13as I said in the challenge video.

4:15So we'll say that maybe you consumed

4:171000 calories for breakfast.

4:19Maybe you had a big donut or something like that.

4:21We'll have lunch calories.

4:22Maybe we were a little bit more well behaved

4:25for lunch and ate 400 calories.

4:28And then for dinner, we'll say dinner calories.

4:32And maybe we went back to our old breakfast habits

4:35and had 1000 calories, all right?

4:37So there we go.

4:38We have our three variables for calories consumed.

4:42So let's do exercise calories burned, we'll call them.

4:47And maybe we, I don't know,

4:50maybe we went for a two mile run in the sun

4:53carrying 100 pounds on your back, something like that.

4:58We'll say that this was maybe we burned 1500 calories.

5:01So now the net calories,

5:04this is a pretty easy equation here.

5:06The net calories would just be breakfast calories

5:09plus lunch calories.

5:11All right, so we'll add those on like so,

5:14plus dinner calories.

5:16And then minus exercise calories burned.

5:21All right, so that should be all we need.

5:23So let's just print out the net calories now.

5:26We'll say net calories and we'll say print net calories.

5:31And if we run our code now, we should see

5:33that sure enough we have 900 net calories.

5:36So we have a little bit more exercising to do, I suppose,

5:39unless well, it depends obviously

5:40on how many calories you burn just sitting around as I do.

5:44But anyway, those are the solutions

5:46for the challenge that I gave you, right?

5:49The three sort of mini challenges.

5:51So I hope this has been informative for you

5:53and I'd like to thank you for viewing.

Team training path

Turn this skill into assignable team training

This free skill is a preview of the courses your team can assign, track, and report on with CBT Nuggets.

What's next?

Ready to keep going?

For your team

Bring this training to your team

See how CBT Nuggets helps IT teams close skills gaps, hit compliance targets, and prove training ROI.

Book a Demo
Just need PCEP?

Learning on your own? Browse individual plans ($49/month, billed annually)

Not ready to buy?
with no purchase required. Already have an account?
Book a Demo