Skip to content
CBT Nuggets
DemoBook a Demo

Writing and Running Python Code

Full skill from PCEP. Preview the training IT teams trust.

Updated

1h 4m

Skill 1 of 26 in PCEP

What you'll learn

  • Run a simple Python program from the terminal and in other contexts used throughout the course
  • Explain how Python uses indentation instead of curly braces to define blocks of code such as functions and classes
  • Declare variables in Python to give a reusable name to a value such as pi and use it in print statements and calculations
  • Apply Python variable naming rules, including case sensitivity, and choose descriptive names that make code easier to read
  • Identify why naming a variable print or sum overrides a built-in function and choose a safer alternative name
  • Write simple programs with variables, such as a tip calculator, discount price calculator, and travel time calculator

Free 64-minute video lesson with a full transcript and knowledge checks. No account needed to watch.

How to Run Python Code

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 of the topics that you'll need to know to pass the PCEP exam that is Python certified entry level programmer. Now, there's going to be a lot of topics that'll be covered here, but essentially what we're going to be doing is learning all of the basics of Python in depth, right? So we're going to go into the syntax of Python, we're going to obviously be seeing how to do things like run Python in different contexts. And we'll also see how to build a few simple projects in Python among many, many other things that we're going to do in this course. So the first thing that we're going to take a look at here is the basics of writing and running a simple Python program. Right, the fact is that all of the stuff that we're going to learn here, right, all of the Python syntax, all of the details that we'll learn is going to depend on your ability to run Python code. So it's pretty important that you just right off the bat get used to running Python in different contexts. So first of all, if you don't already have Python installed, what you're going to want to do, I've just

1:16because there's so many different operating systems out there and the process for getting Python set up varies so much between the operating systems. I've included a link to a pretty good article that shows how to set up Python on pretty much any computer. So feel free to follow that link and walk through the steps in it. And once you've got Python set up, you should be able to follow along with me here. So feel free to pause the video while you do that. And well, assuming that you now have Python set up, the first thing, the first way that I'm going to show you to run Python is just in the terminal.

1:57And first of all, the the IDE that you see me using here is called visual studio code, or that's often abbreviated VS code. And this is different from just plain old visual studio. VS code is basically just a nice straightforward development environment that you can pretty much add any plugins you want for whatever programming language you're using. So I find that it's great for Python development. And really, it's just easy to use. So I highly recommend it if you have another thing that you want to use, whether that's for your job, right, if the company you're working at uses something else, feel free to use that instead. That's perfectly fine.

2:38But anyway, what you're going to want to do first in order to do this is open up a terminal. So my IDE has a built in terminal. And if you're using visual studio code, you can either open this up with control or command J, or you can just go to terminal and do new terminal here. All right, that'll open up that window for you. Cool. So inside this terminal, what you should be able to do is run Python three, just that command. And what that'll do is it'll actually open up a Python read evaluate print loop for you. All right, so this is there's an abbreviation for this in case you aren't familiar with this term, our EPL, basically just refers to a simple program where you can type in Python statements, right, or Python expressions, and hit enter. And you can see the results of those statements. So the simplest thing that we're going to do here is we're just going to print something out to the console. And in Python, the way that you do this is by saying print, and then inside the parentheses that you put after print, which is how you call functions in Python, something

3:54that we'll talk about in much more detail a little bit later, you're going to add a string. And in Python, you know, strings just represent some sort of text. In Python, you can use either single or double quotes. Again, we'll talk about this in more detail later as well. But we're just going to say something like hello, Python. And if you hit enter now, sure enough, you're going to see that what happens is the read evaluate print loop, right, the REPL, our EPL, is going to execute the code that we entered in. And it's going to print out the results, right? So the result of calling print hello Python is that this program simply prints out hello Python to the console.

4:40Now you can do pretty much anything you want in a read evaluate print loop, a REPL. So if we want to create variables, and once again, I'm going to talk about this in more detail later, I'm just kind of showing you more the mechanics of running Python code than actually getting into the syntax yet. But we can define variables. So we can do that like, we can do that by saying things like x equals five, and we can hit enter. And as you can see, this doesn 't have any direct result, right, creating a variable doesn't have any output as did printing something out to the console. So we're going to say x equals five, y equals, I don't know, let's just pick another number six. And if we say x plus y now, well, that does have a result, and that is 11, right, it adds together the values of these two variables. So again, this is just the, this is just the basic read evaluate print loop, this is really great for doing things like running very simple Python programs or experimenting around with different Python statements, right , as you're learning Python syntax, this can be a great place to come and just kind of, you know, play around with different expressions, try changing things, see what happens, right? So you

5:58could say, you might try, you might see if spacing matters in Python, as far as like having space in between the variable name and the value. So you could come back here and try changing x by saying x equals seven with no spaces. And you could see that that works, right? So again, this is just a tool that you can use to run simple Python expressions and statements. So, you know, feel free again to as you're learning different pieces of syntax come, come back here and try them out. It's just, it 's a little bit easier than having to write the Python code in a file and run it manually. And speaking of writing Python code in a file, that's the next thing that we're going to take a look at here. So now that we've seen how to use the Python REPL, which again, you can open up just by just by typing Python three. And by the way, you can exit this by saying exit with two parentheses after it, that's going to close this read evaluate print loop by calling this exit function. All right , well, if we want to write our Python code in a file, which very often we will just because this makes it a lot easier to reuse that code, come back to it later, make changes, etc. All you

7:16have to do is create a new directory. So I've created this directory called PCEP examples. And inside here, what we'll do is we'll just say something like my first program dot pi, all right, so that dot py extension is what Python files will always have. So if you see a file with dot pi, you'll know that it's a Python file. And inside here, what we're going to do is we're going to type, we're just going to write some simple Python code. So I'm going to take things up a little bit and define two variables. So we'll say x equals five, y equals six, just like I did in the in the read evaluate print loop. And then I'm going to actually print out the sum of those two by saying print x plus y.

8:11And in Python, or rather, when we're writing Python in files, as we are here, this print function is going to be especially important because unlike in the terminal, right, when we were using the REPL to run our Python programs, when we're running Python code in a file, it's not going to print out the results of every statement or every expression by default, right? It's not, you know, if we were to just say x plus y here, instead of print x plus y, oops, let's try that again, there we go. If we were to just say x plus y and run this code, it wouldn't do anything. So by adding that print function, that's actually going to print that out to the console where we can see it. These are just little details that you'll get used to as you work with Python more and more.

9:01So don't worry too much about remembering all of them right now. But anyway, now that we've written a little bit of Python code in this file, let's run it. And you can run Python files by saying Python three, right? So that same command as the way that we ran or the way that we opened up that REPL. And then after that, you're going to put the name of the file that you want to execute. So I'm going to say my first program dot pi and make sure you write all of that correctly. If you change a letter to it won't work. So if we had enter, sure enough, we see that that will print out 11, right? x plus y here to the console. And just to demonstrate what I said before , if we were to just say x plus y without the print function, and run it again, sure enough, you'll see that nothing actually gets printed out. So again, when you're running Python in a file, the only things that are going to get printed out that we write from our code are things that we specify with that print function. Okay, so let's just add that back and run our file again to make sure everything is looking good. So that's the second way to run Python code. And really, these two ways are going to be are going to be are going to form the majority of the examples

10:17that we'll do in this course. There are other ways, though, one way, which you've undoubtedly heard people talk about is by using something called a Jupiter notebook. All right, Jupiter with a clever little pie here right Python, or py for Python that is, this is sort of like, this is sort of like what we saw with the read evaluate print loop in the terminal. But it's much more interactive and visual. So we're actually going to be going through just a we'll do a skill about how to work with Jupiter, just the very basics of running Python in a Jupiter notebook a little bit later in the course. So if you're curious about that, feel free to jump ahead and take a look at that skill. But anyway, that's one more way that you can run Python code and a very common way at that, especially if you're getting into something like data science. But you can also use there's plenty of online interpreters.

11:20And by the way, this word interpreter, we'll talk about that in a little bit more detail later. But this is Python itself, unlike languages like C++, which are compiled languages, Python is actually what's called an interpreted language, right, which basically means that this code that we're running doesn't get converted into a bunch of binary before running it. As would be the case with C++, right? So with C++, you talk about the compiler with Python, you talk about the interpreter. So in other words, when we run Python three with the file name, we 're telling the Python interpreter to execute the code that that file contains. All right, but anyway, there's lots of online interpreters, one that I like, just for basic demonstrations is called replit.com.

12:15And it's pretty nice, especially if you're having trouble getting Python set up on your computer, or if you want to be able to code interactively or collaboratively with other people, right, you can actually work on the same Python code at the same time, which can be great for things like tutoring or, you know, doing a hackathon, right, if you're, if you 're working on this with a friend. But anyway, the way that this works is pretty simple. It's, it's pretty much like an online version of visual studio code that we've been working with here, right, just like how visual studio code has a space for you to write code in files, and a space for you to work with the terminal. Over here in replit.com, you have your files over here, and you can create new files just by creating them over here on the left hand side. And you can also run that code by clicking this run button. So again, if we do x equals, let's do 10 this time, and maybe y equals 20. And then you say print x plus y. Well, if you click this run button, what that's going to do is run your code, and print out the output right here in the console. All right, so that's, um, again, this is just a really nice way to work with Python without having to worry too much

13:30about get, about getting things set up. So if you want to follow along in this instead of with visual studio code, feel free to do that. It'll work at least while we're learning the basic syntax for, you know, as just a basic way to follow along. So anyway, those are three basic ways of writing and running Python code. We're going to be using pretty much all of these at some point in the, in the skills of this course. So what I'd like you to do right now is just take a few minutes and play around with these right simple programs experiment around with just the, even just the simple few things that we've learned so far, defining variables and printing things out, just, just so that you get a feel of doing it and can kind of do it by heart, right? That'll, uh, that'll make everything that comes after this a lot easier for you. So anyway, I hope this has been informative for you, and I'd like 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 programs in a few different contexts, let's move on now to talk about the basics of Python syntax. Right now, we're going to be learning about Python syntax in quite a bit of detail. We're going to go into a lot of depth in different parts of Python's syntax. But what I'd like to do here before we get into all of those details is take a look at just some of the basic concepts of Python syntax.

0:31So the first thing to know is that in Python, instead of using things like curly braces, right, so in JavaScript, for example, with curly braces, what you would usually do is use them to define blocks of code. We use these to group pieces of code into things like functions or classes, things like that. So in JavaScript, if we were to create a function, which is just a reusable piece of functionality, we'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 JavaScript, we would actually use curly braces to sort of denote where the body of that function starts and ends. However, in Python, you're not really going to see too many of these, at least not used in that context. In Python, what you'll see is that almost always indentation is used to group code. Now what this means is that unlike in many other programming languages with Python, you can'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 in the IDE, it recognizes that there's something not quite right with that syntax. And if you try and run this program, I will say Python three, my first program dot pi again, you'll see that instead of giving us the instead of giving us x plus y, which is 11 in the console, it says indentation error, unexpected indent. And it even shows us the line here. And then by the way, this is a, this is an error in Python when something bad happens, either, you know, whether that's something that we've written that doesn't work , or whether that's something in the code that breaks, you're going to see it printed out to the console like this.

2:27And usually it'll come along with a nice little, a nice little description describing where the error occurred, and what the problem is, right? So in this case, we see unexpected indent again, basically just telling us that we're not supposed to have that unexpected indent. So in Python, right, if we were to define a function, and again, we'll talk about this in more detail later, the way that we do that is by creating the function name. All right, so we could say something like define my function. And then the body of the function is simply going to be underneath that with a single indentation before each line, right?

3:05So you know, we could say x equals one, y equals two, z equals three, right? All of those are going to start a little bit with a little bit of an indent ation past the function name itself. And again, this is how this is how code, right, different lines of code that are related to one another are grouped together in Python. We don't use curly braces or other symbols like that to do the job. All right, so we'll talk about the indentation rules in much more detail later as well.

3:38But that's just a main concept that I wanted you to understand in Python is that indentation is very important for grouping your code together. All right, so besides that, in Python, a few other syntax related things that I wanted to mention is any line that you write, such as this x equals five, y equals six , print x, y thing, is by default, when we run our code, the Python interpreter is going to try and execute that.

4:09So if we write something that isn't valid Python syntax, right, if we write something like hello, Sean, right, something like that, well, that's not valid Python syntax. And if we try and run our code with that in it, it's going to say, yeah, I really don't know what that is. It's going to give us a syntax error in this case saying invalid syntax. However, in many cases, it might be nice for us to be able to, you know, write things like notes next to pieces of our code so that we can remember what they do.

4:41Right. In this case, with x and y, why are we setting x to five and why are we setting x to six? Well, it might be really nice to sort of leave a note for ourselves. Right. If we say something like x is the number of people invited to the party, something like that. Right. Again, because we've written this in the file, it's going to cause a syntax error if we were to try and run this, right. And I'll just show you that again.

5:12Yep. Sure enough, it doesn't really know what we're trying to do there because again , that's not valid Python syntax. So if we want to write notes next to our code, we can do that by using something called comments in Python. These are a very common thing that are used really no matter what programming language you're using. And comments in Python are indicated by using a number sign, right, or hashtag as the young folks these days are calling it. So by putting a number sign at some point in our Python program, we're saying that whatever comes after that number sign on the line, we want Python to completely ignore.

5:53Right. So Python's not going to try and run this now. And if you run the code, sure enough, what we'll see is that it, you know, it doesn't have a problem with that now. We could write really anything we want after this little number sign. And it's really for our eyes only the Python interpreter isn't going to look at it. All right. So we could say X is the number of people invited to the party. Maybe this is like why is the number of people who showed up uninvited.

6:24Oops, that's uninvited. There we go. And then you could say the total number of people at the party, right? And this, the, you know, the idea behind this is that these comments would make it much easier to remember what this code was supposed to do when we look back at it a day later or a week later or a month later. So especially as you're learning Python and getting comfortable with the Python syntax and writing your own Python programs, you're going to want to make liberal use of these comments 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 here with X and Y, you could say something like, uh, this is how to declare a variable, right? And that's all you need to do there to remember. So in this way, uh, I would highly recommend you do this as you're going through all of these videos. In this way, you can have kind of, uh, all of the example code that you write will end up being just, it will end up being sort of like a cheat sheet for you that you can use to review everything that you've learned so far.

7:38All right. Um, so anyway, that's really the, the main pieces of syntax that I wanted to cover in Python will cover a lot of other things such as how to declare variables, uh, simple, uh, arithmetic operations, declaring functions, things like that, uh, in later videos and skills, but that's, those are really the main two things that I wanted to, uh, show you here because I'll be using them quite a lot going forward. Uh, so just as a review here, the first thing is that indentation in Python is very important.

8:12It's used to group together pieces of code. Uh, so as we start seeing how to do things like loops or functions, things like that, we'll be using indentation and only indentation to group everything together. So that's really something that you've got to get used to in Python, which is why I wanted to mention it right up front here. And the second thing here is that you can create comments by using the little hashtag or number sign. So that's really all that I wanted to go over here, uh, in terms of general Python syntax.

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

Declaring Variables

0:00Okay, so at this point we've seen the basics of running Python programs and we 've also talked about just a few fundamental concepts in Python syntax. So let's move on and start taking a look at a few useful tasks. If you want to call them that, that you're going to be performing in any Python program that you write no matter what it is. All right, so what we're going to take a look at here is the basics of working with variables in Python. Now we've already seen how to declare variables when we said x equals five and y equals six.

0:35So essentially when you're working with variables in really any programming language, there are really two different ways that you might use them. One is basically you're just giving another name to a value. So you could say something like pi equals 3.14159. However far you want to go there, right? And 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 to say things like print pi or if you were to multiply something by pi later on in your Python program, you could just use that name pi instead of having to type out this value each and every time. So that's really the first usage. And in recent years, this is actually an increasingly common usage of variables , even in situations where you might otherwise want to change things.

1:43Don't worry too much if that doesn't make sense. I'll discuss that in more detail later. But the second way that you might think about a variable is as sort of a container that might hold different values over the course of its lifetime. So, you know, if you say something like x equals 5 and then later on, you change the value of x. So you say x equals 10 and then maybe you add something to x by saying x equals x plus one, which would be incrementing x. Well, in this case, we can't really say that x is another name for any of these values, right?

2:20x doesn't mean 5 because later on in the program, x could mean 10 or 11 or really anything else. So that's kind of the other potential way of thinking about variables is as containers that can hold different values over their lifetime. And as you're going to see, you know, as we go through learning Python syntax, we're going to actually use both of these ways of thinking in our program. So these are really just tools that you can use, right? Ways of thinking that you can use in order to really understand the purpose of variables.

2:53Now, just as a sort of side note here, this first way, right, since the variable value doesn't actually change and therefore it's not truly a variable in the meaning of something that varies over time. This is sometimes referred to as a constant. So really a constant is just anything whose value doesn't change and therefore remains well constant. But Python, unlike a lot of other programming languages, doesn't actually have a, it doesn't actually have a keyword that will prevent you from changing the value of a variable.

3:35So if you want something to actually be constant, you have to be a little bit disciplined and not change it later on, right? Later on, if we were to say pi equals four, well, that's not really a constant anymore, right? And from that point on, pi would have a different value. So you might hear me use the word constant sometimes when talking about this way of doing things and variable when, you know, talking about this other way of doing things where the value actually changes. But really in Python, they're technically all variables because they can all change.

4:07So variables, we've seen, we've already seen how to create two variables here, but variables can store a pretty wide range of data. In fact, pretty much any thing that you can, you know, any type of data that you might have in a Python program, and we'll talk about these types in much more detail later on. I feel like I keep saying we'll talk about these later on and we will. But besides simple integer numbers, like five or six, right, counting numbers, again, we'll talk about integers and different number types later on.

4:41But besides those, you can also store things like text in variables. So if you wanted to say something like message equals hello, right, you can do that in Python. If you wanted to store something like just a yes or no value, well, that's something called a Boolean in Python. And again, we'll get into more detail on that later. But that might look something like this. We could say something like Sean likes coffee equals true. And in Python, by the way, in case you're already familiar with true and false values from another programming language in Python, there's, there's really only two values here true with a capital T or false with a capital F, right?

5:24Those those represent really yes and no, respectively. And besides this variables can also hold more complex types of data. And we'll cover these later on as well, but they can hold things like lists, which in Python are just ordered series of values. So we could say something like lucky numbers, something like that equals 123. And 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 want to work with later on in our program. And we want to work with it by name instead of again, having to specify the value each and every time. So anyway, that's some some of the different data types that variables can store. And in case you're eager to know the names of these things, these up here are called numbers or more specifically integers in Python.

6:26We'll go into more detail on how those work later, as I've said, these are called strings. So really any piece of text in Python represented like this is called a string. These as I said are called booleans and they're just yes or no values, one or zero, right? And these are called lists. And these are just a few of the data types that you can use in Python. All right. But anyway, really, the only point that I want to make at this time is that variables can store pretty much any type of data that we want.

6:59And we can change it whenever we want, as I've said, right? So let's take a look at just one possible, very simple application that you might want to use this whole variable thing for. Let's say that we have a few numbers, which are called x, y and z. And you want to add all of them together, but you want to do it like one by one , right? So you want to add x, you want to add y, and then you want to add z. There are multiple situations where you might want to do this in Python. For example, if you want to add all of the numbers in a list, which is something again that 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 me here for a second, we could say something like sum equals zero or total equals zero. And then we could just go through and add each of these variables one by one. So we could say total equals total plus x. And then total equals total plus y, and then total equals total plus z. And after we do that, we could just print out total and total would contain the sum of all of those values.

8:13So if we run our program here, let's just run Python my first program dot pi again, we'll see that that gives us 18. All right. And basically, in cases like this, all that happens, at least when you're working with simple data types, all that happens is Python basically goes through and replaces any instances of that name, right, that variable name with the current value of that variable . So when it sees total, it's going to look, okay, cool.

8:46What's the current value of total? Oh, it's zero. Okay. Well, that means zero. And then it comes over here to x. It says, okay, what's the current value of x? It looks to see that it's currently five. So it basically just kind of substitutes that in there. And that gives us zero plus five. This whole thing turns into the value five. And total is now equal to that, right? That's sort of the process that Python goes through when it's executing our lines of code. All right. So now that total is five here, and let's just delete that now, now that total is five, the next line, total again is going to be replaced with five.

9:25And it's going to look and see what y is. It's going to see that y is six. So it's going to turn that into five plus six, which is equal to 11. And total is now 11. And lastly, it's going to substitute 11 for total. It's going to substitute seven for z. And that ultimately gives us 18. So total is now 18, which means that when we go to print the total, we're really just saying print 18. Okay. Now, obviously variables are much more useful than what I'm making them out to be right here.

10:01Really, they what they allow us to do is work with data in a slightly more abstract way. Right? So it allows us to say things like, you know, a equals two B plus C or something like that. And we don't need to actually know ourselves, right? We don't need to actually know as humans what B is or what C is. We just know that A is two times B plus C. All right. That's really all that we need to know.

10:32And that's where, again, variables allow us to work with data in the abstract. You'll, I think you'll get a sense of this, a much deeper sense of this as we continue learning about Python. But anyway, that's the basics of declaring variables and just a few things that you can do with them. One thing that you'll notice in Python is that there are a few, there are actually a lot of shortcuts, right? That allow us to write less code and achieve the same thing. So as an example, if you look at these three lines here, you'll see that there 's a little bit of repetition, right?

11:08We're saying total equals total plus X, total equals total plus Y, total equals total plus C, right? Well, it would be nice if there was a shorthand way that we could just say something like add X to total, not total equals total plus X, right? And as a matter of fact, there is this is something that you'll see very commonly used in Python. In fact, you'll almost never see or you'll very rarely see what I wrote here. What you'll see instead is total plus equals X, all right?

11:40And total plus equals Y and total plus equals Z. And again, this basically means add X to whatever the value of total is. You might also think of this as saying something like increment total by X or increment total by Y. So I hope this has been informative for you and I'd like to thank you for viewing. [BLANK_AUDIO]

Naming Variables

0:00All right, so now that we've taken a look at the basics of declaring variables in Python, the next thing that I'd like to talk about before I give you a challenge to kind of sum up what we've learned so far, the next thing that I want to talk about is the rules around naming variables in Python. So so far, the variable names that we've used have been pretty straightforward, right? We have x, y, z and total. So in many cases, you're actually going to want to have your variables be much more descriptive than this.

0:33Frankly, just because it makes it easier to read your code when your variable names are, you know, when your variable names are much more descriptive again, picture that we were to say something like a equals x plus y plus z and compare that to something like total people at party, something like that equals people or invited people, you could even just say 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 code than if we were to just name our variables rather obscure, you know, letters or things like that. So keeping in mind that our variable names are often going to be a little bit more complex, sort of like what I just had here. Let's talk about some of the rules around naming variables. The first rule around naming variables is that they need to start with a letter or the underscore character.

1:46So we'll just write start with and then we'll say letter or the underscore character, right? That's what that is there. You just you hold down shift and type a dash character and that's what you get. So that's the first rule. And what this means is that you can't have variables that start with that start with numbers, right? That just doesn't make sense. And 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 can be either letters. So we'll say rest can be letters. They can be numbers. So you can include numbers in your variable names. So if you wanted to say person one or person two or something like that, you can. And the underscore is allowed as well.

2:47All right. So basically what that means is that variable names can consist of letters, numbers and underscores, but they can't start with a number. I suppose you could kind of combine those two rules into one. But anyway, the third rule is that variable names are case sensitive. So if you were to write something like, you know, person one, all in lower case , that's not going to be the same thing as if you were to write person one all in upper case, right? That'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 that spaces are not allowed in Python variable names, right? So Python would think that those were supposed to be two separate variables or something like that. All right. And one last rule here is that you can't pick a variable name that is already a reserved keyword in Python. Now we haven't talked about reserved keywords in Python, but we actually have seen two of them.

3:54And that is when we, you know, when we were talking about how variables work, we saw that with Boolean variables, which are yes or no values, there were two values true with a capital T and false with a capital F. So as you might have guessed, you can't have a variable name with either of these names. You can't have a variable with either of these names because Python would get very confused, right? So we'll say no reserved keywords.

4:24All right, and that would again be things like true or false. So anyway, those are the main rules behind creating some variable names. All right. And I suppose let's just, I'll just go through a few examples of what are valid and what are not valid variable names according to these rules. And feel free to take a screenshot of this if you want. But anyway, as I said, the first example that I'll give you will be a valid variable name or you know what I'll start off by giving you an invalid variable name, an invalid variable name 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 whether something was on sale for 50% off, we're not allowed to have that as a variable name because it starts with a number. So in this case, you would actually have to say, you know, you'd actually have to type out the word 50. Or if you wanted to, you could fix it by putting a underscore before it, right? That would be allowed. Or you could say percent off 50, right? And you could, you know, you could put the 50 at the end.

5:38It just can't be at the beginning. So that's the first example I'll give you. And in fact, let me just show you this, we can actually use comments here to keep notes of this so we can keep that there and we can say no. And then we can create a better version of it where we say something like 50% off and that is allowed, right? So next example that I'll give you is something to do with the subsequent characters, right?

6:09Something except for the first character in a variable name. And that might be something like, you know, if you want to say user name, well, you might want to use, you know, you might want to separate those two words using a dash. And that's not going to work either. Here, let's just give it a value here, Sean. That's not going to work either because dashes are not allowed in words and in variable names rather. The question is that to Python, this looks like user minus name, right?

6:41So that just isn't going to work in this case. Dashes really aren't allowed. In fact, no symbol is allowed in variable names except for that underscore character. So we would have to change that to user underscore name if we wanted to do that . So let's just, let's just add that dash back in. I'm going to comment that out and I'm going to add no at the end of it. Like so. And then we'll add a fixed version underneath this. You can use this as notes if you want. We'll say username equals Sean.

7:11All right. So another example here, let's take a look at case sensitivity. Let's say that we were going to say something like print total. If we wanted to print the total out up here, well, that's not going to work because we don't have any variable called total all in caps like that, right? So that's not going to work. I'll just comment that out so that that doesn't, that doesn't stop our program. And by the way, the reason I'm commenting these out is because if we were to try and run it without them commented, we wouldn't get past that line, right?

7:41Python would run into this. It would produce an error and it wouldn't, you know, we wouldn't get to see the output of anything that came after that. So anyway, we could of course print the total. In fact, we've already done so up above here. So I'll just, in fact, just move that down there. All right. Cool. So next thing, no spaces. You know, obviously we can't have something like my favorite color equals, and then, you know, let's just say blue, you can't have that, right?

8:14Python has no idea what's going on there. It thinks that we're having, you know, things that one of these is supposed to be a keyword or something. It just doesn't like that at all. So we'll put no next to that as well. And that would have to be something like this, where we actually use underscores, and this is a very common usage of underscore characters in Python is to basically serve as spaces in the word. And by the way, you might also see in Python variable names that instead of using underscores to separate words, you might see something like this, where someone starts the first letter of each new word with a capital letter.

8:57And this is something that's referred to as camel case, right? The reason being that these look like camel humps apparently. Someone came along and thought that and well, the name stuck this rather fittingly considering the name of the program and language we're learning is called snake case, right ? Because apparently someone thought that this looks like a snake somehow. Anyway, those are the names for those two different types of casing snake case in Python is usually used more often. You might see that someone coming from another language such as JavaScript will use camel case for, you know, for file names, but snake case is far more common from what I've seen.

9:38All right, so one last one last example that I'll give you fitting in with the fifth rule is if we were to try and say something like true equals, you know, true, maybe we wanted a variable called true that held the string true. Well, we obviously can't do that because Python thinks that this is a reserved keyword. And in fact, it is a reserved Python keyword. So we can't do that either. Let's just write no there like so. And you know, if we wanted to do something else like that, we could in theory use true or lowercase like that.

10:13But usually with with keywords like that, it's better to just find another, you know, it's better to just find another way to express that, right? You could say something like is true or true string or true text, something like that. Any of those would be better than just saying true equals true, right? One last thing that I wanted to point out here too is that aside from reserved keywords such as this true thing here in Python, and there are many other reserved keywords that we'll talk about later on in other skills, you might also come across things that aren't aren't actually reserved keywords, but are already something else.

10:55So this print function, for example, this already, you know, the word print already means something in our program just by default in Python. So we wouldn't want to say, you know, print equals hello, right? And the problem with this is we could do that. And we'll let us do this. But what that does is it gets rid of the default functionality of print, and it makes print just in this case, it makes it just a piece of text, right, a variable with some text inside of it, which is generally not what we want.

11:29And another instance of this, which, you know, especially in beginner programming exercises, you will see, you know, you'll be very tempted to use this is some equals zero sum is actually already a function in Python that you can use to get, well, the sum of a series of numbers. We'll talk about this in more detail later, but you don't want to do either of these things. So let's just comment those out. And 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. All right, so you don't want to do either of those things. You'd have to just find another word for it like we did up here, right? So total, you know, or for print, you could say something like message equals hello, right? That would just be a better way to do that. So we'll say total equals, but I won't use total now. I'll use something like count equals zero, right? So anyway, that is the basics of naming conventions and naming rules in Python.

12:39So I hope this has been informative for you, and 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 syntax and we've also learned some details about Python variables. So it's time for you to solid ify what you've learned so far by doing a challenge. Now in this challenge, what we're going to do is I'm going to give you a few different situations that can be expressed by using variables and your job is going to be to write that situation as a Python program. So to give you an example here, let's say that I give you the situation of calculating how much of a tip you want to leave at a restaurant and using that to calculate the total bill. Well, if we think about what that would look like here, right, there are really going to be two main quantities involved. The first is the bill amount, right? So let's say that you go to a restaurant, you spend maybe $40 just to give you an example here. So let's try that again. There we go. And let's say that you want to leave a 15% tip, right? Well, so we'll just write that as tip percentage. And notice here that I

1:16'm not using variables yet. I'm just kind of drawing this out. And that can be very helpful sometimes to, you know, just to kind of work out your thoughts and get them more clear so that you can actually express them in code. So let's say that we want a 15% tip. And a few other things that we might know about this would be that the tip itself, right, is the way that you calculate the tip is by multiplying the bill amount times the tip percentage, whatever that number is, divided by 100, right?

1:53So we'll say tip percentage divided by 100. And that'll give you the tip amount , right? And finally, we would know that the bill amount, right, that the total amount, the bill plus the tip would be equal to the bill amount, plus the tip amount. All right. So those are just a few things that we know. And if you wanted to express this as a program in Python, here's what this would look like really the main purpose here is just to get you used to taking situations and mapping them into a fairly simple series of variables. So here's what that would look like here.

2:41What I'm going to do is create a new file, we'll call this something like variable challenge dot pi. And let's start off here by defining those things that we had previously as variables. So we would say something like bill amount, that would be a variable. And maybe that's $40. We'd have the tip percentage, which would be 15. All right, 15%. And from there, we can pretty much figure out the rest just by defining variables as combinations of these. So as I said, we could have the tip amount equal to the bill amount, plus, or sorry, times, and then we have the tip percentage divided by 100. All right.

3:36So we now have the tip amount, which means that we can figure out the total bill. So we'll say total bill equals bill amount plus tip amount. All right. And what you can see there is if you print this out, right, we can say print, and we can print out any of this that we want. So if we want to see the tip amount, we can say something like print, well, here, we'll say the tip is, and then we'll print out the tip amount underneath that I'll show you how to actually add values into strings a little bit later. But then we'll say the total bill is, and then we'll print out the total bill underneath that. All right, so I'm going to run this now and what this will look like. Oops, let's rephrase. All right, so I'm going to run this now and what this will look like as we'll say Python three variable challenge dot pi. And sure enough, we see that the tip is $6. And the total bill is $46. So the cool part about this now, and you know, the reason that expressing situations in code like this is so helpful is we can now go up and change either of these two

4:52variables at the top. And everything else will still work, right? So let's say that we go to a restaurant now and we spend $100 and we want to tip 18%. Well, that's still the rest of the program is still going to work in exactly the same way. In other words, if we make a line of, so let's try that again, if we make a line of comment signs there just to kind of create a visual separation, we can we can basically change the entire rephrase. We can use this program in basically an infinite number of ways just by changing this stuff above the line . We don't really need to change anything down below. Unless of course we want to change the program significantly, right? So if we wanted to start calculating tax and add that in, well, we would have to add a tax percentage or something like that to this, maybe that's 4% or something, and we would have to incorporate that into the into this equation, right, this system of equations down at the bottom. But I'll just leave that off for now. And let's just run this again. And sure enough, we see that this works for those, you know, those modified numbers as well.

6:03So anyway, that's that's kind of the basic idea. So now that I've given you a demonstration, it's time for you to do a few of your own based on situations that I'll describe for you. All right, the first one, which should be pretty straightforward because it's pretty similar to the code that we just wrote, I'll give you that hint, is calculating the discount price. All right, there we go. Based on the original price and the discount percentage , right, so those original price and the discount percentage are going to be the two variables there.

6:41Another situation that you can do here is doing something like calculating the travel time. And this one would have probably two variables as well, the distance and the average speed. Okay, so again, you can express that as a as a program as well. And finally, I'll give you one more. Let's say that you want to calculate the total, your net calories for the day, right, the number of calories above or below zero based on diet and exercise. And in this one, what you could do is you could have like four variables, let's say you could have one variable for the calories for breakfast, one for lunch, one for dinner, and you could have a fourth variable for the number of calories you burned while exercising today. All right, so that would be, that's another program that you can feel free to work out on your own as well. Again, this is all just to get the, just to get the feel for defining variables and using them to express programs.

7:50So anyway, this is your challenge here. Feel free to give this as much time as you need, probably about five to 10 minutes. And once you rephrase. And once you've given this a try, feel free to move on to the next video where I'll walk you through the solutions. So I hope this has been informative for you. And I'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. Let me walk you through these solutions. So the first project or mini program that I gave you as the challenge, and I'm just gonna open up the variable challenge here. And in fact, I'll use the comment marks to kind of separate the first challenge or the demonstration that I did from the first challenge. This was to calculate the discount price of something. It's something in a store given the price and the discount percentage.

0:33Well, this was going to be pretty much like the tip calculator, except a little bit in reverse, right? We'd be subtracting the discount price instead of adding the tip. But here's what this looked like. This required two variables, the original price. All right, which you could have set to anything, right? You could have said maybe 199 as the price. And then for the discount percentage, which is the other variable that we need, let's just say maybe 35%.

1:06All right, so now we just need to kind of figure out what the basic equation is for getting the discount price. And here's what that would look like. We'll say discount price. And as I said, this is gonna be very similar to the tip calculator. We're just gonna say discount price equals original price minus. And then what we're gonna do is we're going to figure out how much we want to subtract. So if we're removing 35% of the price, what that means is we need to multiply the price, or multiply the original price by 35, or we could maybe subtract 35 from 100 if we wanted to do it the other way.

1:51But what this is gonna look like, 'cause I'm just gonna say original price minus discount percentage. All right, that's gonna be divided by 100 because we need to actually get that into the right form. And then we're gonna multiply that by the original price. So this is removing 35% of the price. There are other ways you could have done this or slight differences in the equation that you could have done, and that's fine too. So what I'm gonna do is I'm just going to say print, and then we'll print out the discount price is, and then we'll print out the discount price underneath.

2:27So let's say discount price. And if we run this code now, let's try running Python3 variable challenge.py. Sure enough we see that the discount price is $65 and around 64 cents. Don't worry about that long string of decimal digits. We'll see how to format strings and numbers a little bit later in our Python journey here. Anyway, that was the first solution. So the second one was calculating the travel time.

2:57So there were two variables for this one as well. The first was going to be the distance, and you can say this in miles or kilometers, whatever you want. I'm gonna say that it's in miles. So we'll say that the total distance is equal to maybe 100 miles. And then we'll say the average speed. We'll say that that's maybe 60 miles per hour. So now in order to figure out the travel time, all we have to do is say travel time equals distance divided by average speed.

3:33And that would give us the travel time. So we can print that out now by saying print. The travel time is, and then we'll say print travel time. And that should work. So let's try running this again. And sure enough, it looks like the travel time is around one and two thirds of an hour. And that looks about right to me. So let's do the third and final challenge, which is calculating the net calories for the day. Basically the calories you consumed minus the calories you burned through exercise.

4:06So let's just assume that we have all of these numbers ready. The first one would be breakfast calories, as I said in the challenge video. So we'll say that maybe you consumed 1000 calories for breakfast. Maybe you had a big donut or something like that. We'll have lunch calories. Maybe we were a little bit more well behaved for lunch and ate 400 calories. And then for dinner, we'll say dinner calories. And maybe we went back to our old breakfast habits and had 1000 calories, all right?

4:37So there we go. We have our three variables for calories consumed. So let's do exercise calories burned, we'll call them. And maybe we, I don't know, maybe we went for a two mile run in the sun carrying 100 pounds on your back, something like that. We'll say that this was maybe we burned 1500 calories. So now the net calories, this is a pretty easy equation here. The net calories would just be breakfast calories plus lunch calories.

5:11All right, so we'll add those on like so, plus dinner calories. And then minus exercise calories burned. All right, so that should be all we need. So let's just print out the net calories now. We'll say net calories and we'll say print net calories. And if we run our code now, we should see that sure enough we have 900 net calories. So we have a little bit more exercising to do, I suppose, unless well, it depends obviously on how many calories you burn just sitting around as I do.

5:44But anyway, those are the solutions for the challenge that I gave you, right? The three sort of mini challenges. So I hope this has been informative for you and I'd like to thank you for viewing.

Frequently asked questions

What does the Writing and Running Python Code lesson cover?

The lesson shows a team member how to run a simple Python program from the terminal and in the other contexts used throughout the PCEP course, how Python uses indentation to define blocks of code, and how to declare and name variables. It closes with hands-on challenges that apply variables in small programs.

Does a learner need Python installed before starting this lesson?

Yes. Because setup varies by operating system, the trainer links to an article that walks through installing Python on most computers and suggests pausing the video to complete it. The first way the lesson then runs Python is in the terminal.

How long is this lesson and where does it fit in the PCEP course?

The lesson runs 1 hour 5 minutes and is the starting point of the Certified Entry-Level Python Programmer (PCEP) course, which IT Directors and Training Managers should plan at roughly 23 hours per learner. Every later topic, from control flow to the project builds, depends on the ability to run Python code that this lesson establishes.

Why does Python use indentation instead of curly braces?

Where languages such as JavaScript use curly braces to group code into functions or classes, Python uses indentation to define those blocks. The trainer covers this early because the syntax is used constantly in the rest of the course.

What happens if you name a Python variable print or sum?

The code still runs, but the variable overrides the built-in print or sum function. The lesson recommends choosing a safer alternative, such as message instead of print or total or count instead of sum, and also shows how case sensitivity means total and TOTAL are different names.

What practice programs does a team member build in this lesson?

After a tip calculator demonstration using bill amount and tip percentage variables, learners write their own discount price calculator and travel time calculator, budgeting about five to 10 minutes per challenge before reviewing the trainer's solutions. For Team Leads onboarding junior developers, this gives each learner early, repeatable practice with descriptive variable names and simple calculations.

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