Skip to content
CBT Nuggets
DemoBook a Demo

Python Variables And Data Types

This skill provides an introduction to Python variables and data types, essential for data manipulation and analysis. It covers the use of Jupyter Notebooks for writing and running Python code interactively, highlighting the flexibility of Python's dynamically typed variables. Learners will explore different data types such as integers, strings, floats, and Booleans, and understand how to effectively use them in Python programming. The skill also emphasizes the importance of proper variable naming conventions and the use of built-in functions to inspect data types.

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

52m

Skill 1 of 32 in PCED

Writing and Running Code In Jupyter

Before we can start learning the basics of data collection, cleaning, transformation, and analysis in Python, we need to learn how to work with Python's most fundamental data containers (called "variables") and the different types of values that they can contain (collectively called "data types"). In this skill, we'll be learning about both, but the first thing we need to discuss is how we'll be writing and running our code in this course. Instead of using the typical "scripting" approach, where we write a Python file and then run it via the terminal, we'll be using something called Jupyter Notebooks. Let's take a look!

Knowledge Check

The two main types of cells in Jupyter Notebooks are ____________

Python Variables In-Depth

Now that we've got our bearings with navigating the Jupyter Notebook interface, let's take a closer look at Python variables, how they work, and the rules around naming them.

Knowledge Check

Which of the following is a valid Python variable name? (Select 4)

Working With Python Data Types

Next, let's take a look into the world of Python's data types. Basically, there are many different kinds of values that we might want to store and work with in our Python programs, and they behave very differently from one another.

Knowledge Check

The variable x = "3.0" is a ________ (notice the quotes?)

Challenge & Solution: Basic Arithmetic

Now it's time for a challenge! In this challenge, you'll be exploring how basic operations work with different Python data types. Watch the video for more information.

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

Knowledge Check

What happens if you multiply an integer by a boolean?

View Transcript

Writing and Running Code In Jupyter

0:00Hi, Sean here, and welcome to this skill where we're going to be taking a look at the basics of

0:05variables and data types in Python, which will form the foundation for a lot of the more complex

0:11data manipulation and analysis that we're going to be taking a look at in this course.

0:16So the first thing that we have to look at, though, is we need to talk about how we're going

0:20to be writing and running Python code in this course. And the fact is that there are many ways

0:26that you can write and run Python programs, right? There's a pretty wide variety just because Python

0:33is such a widely used language across many different fields. Now, in this course, just

0:39to keep things as simple and straightforward as possible, we're going to be using Jupyter

0:44notebooks to write and run Python code. Now, you may have used these. In fact,

0:49these may have been what you used in order to learn Python in the first place. That's

0:53not an uncommon situation. Or you may be coming to this course just only having used things like

0:59IDEs to write and run Python code. But whatever the case, in this first video, I'm going to show

1:04you the basics of using Jupyter notebooks. And I guess as another side note, we're going to be

1:10using the Anaconda cloud in order to do this. Now, you can get here just by going to Anaconda.com

1:17or nb.anaconda.com. You will need to create an account on here just in order to create and run

1:24your own notebooks. But essentially, this is just a nice, convenient way to write and run

1:29Python code interactively without having to download anything onto your own computer, right?

1:35So, you know, you should get exactly the same results here as what you're going to see me doing,

1:40which is always nice to have that kind of consistency, right? So once you've created

1:45an account, what you're going to want to do is open up the Anaconda JupyterLab, right? You should

1:52be able to do that. There should be a link as soon as you create an account that will allow you to

1:55do this. And JupyterLab, you can just, I don't know, you can think of it as like Microsoft Word,

2:00but for Jupyter notebooks, right? So it's just kind of a nice, convenient way of managing

2:05multiple notebooks in the same place and creating them as part of a sort of file structure here.

2:12All right. So the first thing that we're going to do once you've, you know, once you've opened

2:16up JupyterLab is we're going to create a new notebook. Now, in case you're not familiar with

2:21what a notebook is, and we're going to select this Anaconda 2025 one, or, you know, you might

2:27see a slightly different version here. Just make sure it's not the Anaconda AI one. That one's

2:32slightly different. Just choose the regular old Anaconda notebook. And what that's going to do

2:37is open up a Jupyter notebook. Now, as I was saying, if you haven't worked with Jupyter

2:41notebooks before, I think you're really going to like them. Essentially what these allow you to do

2:46is write and run Python code interactively. Basically what that means is instead of having

2:52to write the code in a file, then open up a terminal and run the code manually,

2:58you can just write the code and I'll show you a hotkey here for how to run the code. And you'll

3:03just see the output of the program appear directly underneath each cell that we write the

3:09code in. There was probably a lot of jargon in what I just said there, so it might be best if

3:14we just get started and see how to write and run a simple Python program in Jupyter. Now, this is

3:19going to be a very simple Python program. What we're going to do here is just define two variables

3:24and then we're going to, I don't know, do something like add them together. Very simple

3:29again, but this will show us the basics of using Jupyter notebooks. So let's get started here by

3:34just defining two variables. Each one is going to hold a numerical value. So we'll just say like X

3:41equals, um, I don't know, 14 and Y equals 20. It doesn't really matter what numbers you pick. I

3:47just, those are just what fell under my fingers here on the keyboard. And then what we're going

3:51to do is we're just going to say X plus Y. And then what you're going to do is you're going to

3:57hit shift enter, and that will run the code that we just wrote there. And look at this, the output,

4:04right, which is basically the value of the last line that we have inside this little editor here

4:10is automatically printed out after that. Now there's a few things that I want to mention here.

4:15The first thing is notice that we didn't even have to say print X plus Y in order to make that

4:20happen. Now you can say print X plus Y and that'll have basically the same effect, but the default

4:27behavior of Jupyter notebooks is that they'll automatically print out whatever the value is

4:32of the last line. Okay. Now, you know, you could still use print if you wanted to do something

4:39after adding X and Y together. So, you know, if you were to define the variable called Z, let's

4:46say equals, I don't know, something like 30 after adding X and Y together, then what you'll see is

4:52that that's not going to print out X plus Y anymore because now Z equals 30 is the last line.

4:59And since this has no inherent value, that's why we don't see anything printed out. So if you still

5:05wanted to see the value of this, that's when you would use print X plus Y. Okay. Just, just a few

5:11basic, um, you know, things to know when working with Jupyter notebooks. So that's the first thing

5:17I wanted to point out. The second thing is you may notice that after we ran this code here,

5:24another little code window appeared underneath it. Now these things in Jupyter notebooks are

5:29referred to as cells. All right. Now the idea of a cell in a Jupyter notebook is that a cell is a

5:36collection of lines of code that are all going to be run together. Now, essentially what this allows

5:41us to do, which is very helpful when performing data analysis or doing data science is this

5:48allows us to split up what would have otherwise been a much larger Python program into smaller

5:55chunks that we can run at the same time. Right? Um, so imagine that you want to do something and

6:01we'll be seeing the basics of how to do this a little bit later in the course. So you don't

6:04have to worry too much about it right now, but imagine that we're doing something like loading

6:08in data from a CSV file and then performing some cleaning on that data. We'll talk about

6:13data cleaning a little bit later too. Um, maybe you're, uh, using some sort of machine learning

6:19algorithm on that data. That's a lot of different steps that you're performing on that data and

6:26without Jupyter notebooks, right? And without your code being split up into cells,

6:30that typically means that you have to write and run that code all in the same file at the same

6:37time. Right? So essentially if, if something goes wrong in the middle of that code after loading

6:44the data and then cleaning it, something like that, then, well, you have to start the whole

6:48thing over, which is very inconvenient in more complex cases. Don't worry too much about all

6:54the details here. I'm just describing why these cells are so important and why Jupyter does things

6:59this way. Um, so anyway, we already saw that you can run the contents of a cell by pressing shift

7:05and enter. And another important thing to remember about cells in Jupyter is that the variables that

7:12we create in any one cell in a notebook will be accessible in any other cell. So what this means

7:20is since we've defined X and Y up here in this cell, if we create another cell, which you can do

7:26just by clicking, you know, these little buttons here, right? If you click this one, that creates

7:31another cell above. If you click this one, this creates another cell below, right? And you can

7:35also delete cells by clicking the little trash can. But anyway, because we've defined X and Y

7:41up here, we can access both of those inside this cell. So if we just type X and then press shift,

7:45enter here, that will run X and that will show us what its value is. If we say Y and then press

7:51shift enter, that'll show us the value of Y. Now where things can get a little bit tricky in more

7:57complex Jupyter notebooks is the fact that later cells can affect the values of variables in earlier

8:05cells, right? Now, this is not something that you typically experience with regular Python programs

8:11that you write and run inside a single file. So, you know, down here, if we were to say Y equals,

8:18and let's change the value to 999, right? And run it, that's going to change the value of that

8:24variable. And now if we go to the cell before it, where we're printing out the value of Y,

8:29we'll see that Y is now 999, okay? So essentially, each cell acts as sort of like a bundle of

8:38functionality that affects the same set of variables that's shared among all of the cells.

8:45All right, now we may run into some tricky situations here with this, but for the most part,

8:49as long as you write and run your cells in a logical order, you can usually fix things that

8:55you've messed up like this just by rerunning the cells from top to bottom, right? So if we modified

9:01Y and didn't want to, then we could always rerun this cell up above, right? I'm running that with

9:07shift and enter again. And now if we rerun this cell where we're printing out the value of Y,

9:12we'll see that that gives us the value of 20 again, all right? So anyway, just a few more

9:18things that might be helpful to know as we're, you know, learning all about the basics of Python

9:24for data science and data analysis. If you want to move cells around, you can do so with these

9:29buttons here. So if we wanted to move this cell up to the top, which wouldn't really make sense

9:34because at this point we haven't yet defined Y, if we were to run all our cells from top to bottom,

9:40but you could move the cell up. If you wanted to, you can move the cell down just by clicking that

9:44down arrow. If you want to duplicate a cell, you can click this little create duplicate button

9:50there, and that'll just, you know, create another duplicate. This can be very helpful sometimes for

9:54editing more complex cells when you want to try an alternative. And of course you can delete the

10:00cells by clicking this little garbage can button as we already saw. Um, let's see a few other

10:06things. Cells in Jupyter notebooks can have, uh, one of several types. So these are all what are

10:13known as code cells, basically meaning that they just contain Python code that we can execute if

10:19we want to, right? It has an effect on the variables and data behind the scenes, but you can

10:25also set the type of a variable to mark down. And what that'll allow you to do, this is very

10:32commonly used for, um, you know, notation. So if we wanted to say mark down here, right, you just

10:39click on a cell and then set the type, uh, drop down here to mark down. We could say something

10:45like here are the basics of, um, defining and using variables in Python, right? And if we run

10:56this one by pressing shift and enter, what that'll do is that will simply render the mark down,

11:01right? And so this can be very helpful when you're doing things like communicating your findings

11:06with, um, you know, some regards to some sort of data analysis, right? It's really just used for

11:11communicating results of a Jupyter notebook, right? Or of specific cells with other humans.

11:17It gives a little more context. And, um, uh, because this is marked down also, you can use

11:22any kind of markdown symbols you want. So if we wanted to be, uh, you know, if we wanted to add a

11:27heading here, a level three heading, we can use, um, three number signs or hashtags if you want

11:33to call them that. And we can say something like Python variables, right? And now if we run that,

11:39that'll be rendered as an H three heading. We'll talk about the syntax of markdown a little bit

11:44more detail later perhaps, but anyway, just know that there are really two main types of cells

11:49and that is code cells and markdown cells. You may have noticed this raw cell. This is just when you

11:54have some sort of raw content that you want to put inside a cell. The use cases for raw cells

12:00are a little bit beyond what we're going to talk about here. So for now, just know that we have

12:06code and markdown as options. Cool. So that should be really all that we need to know so far with

12:10regards to Jupyter notebooks. Um, so the last thing that we're going to do is we're going to

12:15save this notebook and we'll just call this something like variables and data types. Okay.

12:21So it's typically a good idea to name your notebooks just in some way that makes sense

12:27so that you can remember what the notebook is about and open it over here.

Python Variables In-Depth

0:00All right, well, now that we've seen the basics

0:02of working with Jupyter Notebooks

0:04and writing code and running the cells,

0:07it's time to take a closer look at how Python variables work.

0:12So we've already seen how to define Python variables

0:15and add values to them.

0:17Essentially, we can create a variable in Python

0:20just by using a new name

0:23that we haven't used before for variables

0:25and saying equals some value, right?

0:28Now, there's a few things

0:29that I'd like to pull apart right there.

0:31The first thing is that,

0:33unlike with other programming languages,

0:36like, say, JavaScript, Java, C++,

0:39pretty much every programming language

0:41that I know of besides Python,

0:43you have to create a variable in a different way

0:47from how you change the value in a variable, right?

0:50So in other words,

0:51when you wanna create a variable in, say, JavaScript,

0:55you can say either var or let.

0:57Yes, JavaScript does have two different keywords

1:00for defining variables,

1:02but in JavaScript, we'll just use var.

1:04You would have to say var x equals 14

1:07in order to define the variable,

1:10and then if you wanted to change the value later on,

1:12you would just say x equals 15, let's say.

1:17So Python's a little bit different

1:18from other programming languages in that respect

1:20where you can create a new variable

1:23just by saying x equals 14 or y equals 20.

1:28Now, there are a few situations

1:29where this gets a little bit confusing,

1:31but we'll touch on those a little later.

1:34For now, you just need to know

1:35that whenever you see a new variable name

1:38with equals something after it,

1:40we're creating a new variable.

1:42So that's the first thing.

1:44The second thing is I wanted to talk

1:46about this little equals sign

1:47because the fact is that equals signs in Python

1:51can have slightly different meanings

1:53depending on how they're used, right?

1:55So right here, we're using the equals sign

1:59as what's known as the assignment operator,

2:03which basically just means

2:04that we're putting a value into a variable, right?

2:07If you picture the variable as a container

2:10that holds a value,

2:11what the assignment variable does

2:13is it takes whatever's on the right-hand side of it

2:15and it puts it into that container, okay?

2:20Now, this isn't always the case.

2:24As we'll see a little bit later,

2:26the equals sign in Python can also be used

2:28as what's known as the equality or comparison operator

2:32where if we wanna check to see

2:35whether x is equal to, say, five,

2:38then we could say x and we use two equals signs, five,

2:41and that's gonna tell us, yes or no,

2:44is the value of x currently five?

2:46We'll talk about that in a little bit more detail later,

2:48but that's just something I wanted

2:49to get out of the way here.

2:51So what else?

2:52Once we've defined a variable, right?

2:53Once we've put a value into a variable,

2:56as we did here with x and y,

2:58as we've already seen, we can use that variable name

3:02just like how we would use a regular old value, right?

3:05So saying x plus y is going to figure out,

3:08okay, what's the value of x?

3:10Okay, we see it's 14.

3:12What's the value of y?

3:13We see it's 20.

3:14And what is 14 plus 20?

3:16That's essentially what's going on there

3:18behind the scenes when we say something like that.

3:21All right, so again, these variables are just

3:23sort of like convenient labels or containers

3:26for the values that we need to work with in our program.

3:30Now, as we've already seen down here,

3:32we can also do what's called reassigning variables,

3:36and this is quite simply when we change the value

3:39that a variable contains, right?

3:41So if y has 20 here and we say y equals 999,

3:46after that, as we've done here,

3:48what that's gonna do, right?

3:50Let's just imagine our little container y here

3:52with the value 20 in it.

3:54What that's gonna do is that's going to remove 20

3:57from the container, and it's gonna replace it

4:00with whatever value we have there, right?

4:03So 999 in this case.

4:04And we can do this as often as we want.

4:08All right, so what else?

4:09Variables can depend on each other, right?

4:11So in other words, we have x and y here,

4:14which are both defined as simply, you know,

4:17a concrete or what's known as a hard-coded value.

4:21You'll hear me use this quite a lot in programming.

4:25So hard-coded values are basically just values

4:27that we have, that we've written explicitly

4:31in the code, right?

4:32So if we say, you know, if we wanna find out,

4:36let's say, the circumference of a circle

4:38and we have the radius, we could say r equals 10,

4:41and then we could find the circumference

4:43by saying c equals r times,

4:46and then we could hard-code the value of pi

4:48by saying 3.14159 and so on, right?

4:53All right, so that would be what's known

4:54as hard-coding the value of pi in this case, all right?

4:59Anyway, I just wanted to clear that piece of terminology up

5:02before we move on.

5:03But as I said, variables can depend on each other.

5:06So if we wanted, let's say,

5:09if we wanted to create another variable called z

5:12and set that equal to the sum of x and y,

5:16then all we would have to do is say z equals x plus y,

5:19and now if we print out the value of z down below,

5:22right, which we can just do by saying z

5:23at the very bottom of the cell and running it,

5:26we'll see that z now contains that value.

5:30All right, now just like how variables can depend

5:32on the values of other variables when we're defining

5:36or assigning values to them,

5:39they can also depend on themselves, right?

5:43Or that sounds a little bit odd.

5:46So let me just show you what I mean.

5:47We can actually update a variable

5:50with regards to its current value.

5:52And this is a very, very common thing to do

5:56in data analysis, right?

5:58Let's say that we wanna get the sum of all of the values

6:02in some sort of data set column.

6:04Then what we would need to do is we would need

6:06to basically create a variable,

6:09loop through all of the values in that data set column,

6:13and then add each one onto that variable.

6:16We'll see how to do things like that a little bit later,

6:18but for now, all you need to know is that

6:20if we wanted to maybe raise the value of z by one,

6:24we could do that by saying, let's just move this here

6:27and add a new cell above it,

6:29we could do that by saying z equals z plus one.

6:33You'll see this kind of thing very often in data analysis.

6:37So let's just run this, and if we print out z again

6:40after doing that, isn't that interesting?

6:42Look at this, we got 36,

6:44and that's actually because I ran this cell twice.

6:47So this actually goes to show you some of the tricky things

6:50that you can run into with using Jupyter Notebook cells

6:56is that because the values are stored behind the scenes,

6:59if you were to rerun this cell multiple times,

7:01let's just try doing that real quick, right?

7:04You'll see that that will increment the value of z

7:05every time we run this cell.

7:08So if we wanted to reset z,

7:09we can just rerun this cell above it first,

7:11and then if we rerun that again,

7:13that'll give us the expected value

7:15that we had from before, right?

7:18We see 35.

7:19So this is a very common thing to see.

7:21In fact, it's so common

7:23that there's a shorthand syntax for it.

7:27Well, there's actually two shorthand syntaxes for it.

7:29The first is if you wanna add some value

7:34to an existing variable,

7:35then you can just say z plus equals,

7:37and then the value you wanna add to it.

7:40So if you wanna add five to z,

7:42then you can say z plus equals five,

7:43and that will increment the value of z by five.

7:47In other words, the new value of z

7:49is gonna be whatever its previous value was plus five.

7:52Okay, so if we run this again,

7:54what we'll see is sure enough, it's now 40, right?

7:57Because z was 35 after adding one to it in this cell,

8:00now it's 40.

8:02All right, so hopefully that clears up

8:03some of the details of working with variables in Python.

8:07The last thing that I wanna go into

8:08a little bit more detail on,

8:10because it's very important in Python, obviously,

8:13is the rules behind naming variables.

8:16All right, so so far,

8:17we've just been using simple letters like x, y, and z.

8:20In practice, you won't wanna use variable names

8:22that are this opaque, right?

8:24In other words, you'll wanna use descriptive variable names

8:27that give you a good idea

8:28of what is actually inside that variable, right?

8:33X equals 14, that doesn't mean anything.

8:35Even if you understand what this is doing behind the scenes,

8:38that we're assigning the value 14 to the variable x,

8:42we don't actually know what this thing means.

8:44Whereas if we were to say something like shoe size equals 14,

8:49it's a pretty big shoe size in America anyway,

8:51but that gives us a little bit more context

8:54as to what that value means.

8:57So that's typically what's recommended in Python programs

9:01is that you give your variables a name

9:03that's as descriptive as possible

9:05while not being obnoxiously long to type, okay?

9:09Now, so let's talk about the rules behind naming variables

9:12because Python doesn't allow us

9:14to name a variable just anything.

9:16So really, the rules behind variable names are as follows.

9:20A variable, and I'm just gonna change that one

9:22back to x for now,

9:23we'll just set that back to what it was before,

9:26and I'm gonna create a new cell down below, okay?

9:30So a variable can start with either a letter

9:33or the underscore character.

9:35That's the first rule in naming Python variables.

9:39So let me actually write this down here.

9:42We'll say starts with a letter,

9:47and this can be a capital or lowercase letter,

9:50or the underscore character,

9:53which is a shift and then the dash key.

9:56All right, so what this means

9:57is we could create a variable

9:58called something like message equals hello, right?

10:02Fairly straightforward, that's totally fine.

10:04That starts with a letter,

10:05or we could start it with an underscore character

10:08and say underscore message equals hey, right?

10:12Now, why on earth would you wanna start a variable

10:15with an underscore character?

10:16Well, this is one of those things in Python

10:19that's referred to as a convention, right?

10:23In other words, a convention is something

10:25that you're not required to do by the syntax, right?

10:30The code will run just fine if you ignore the convention,

10:34but it's something that Python developers typically follow

10:38just in order to enhance communication

10:41between multiple developers.

10:43And so typically when you see this underscore character

10:46at the beginning of a variable name,

10:48it means that this is something

10:50that's referred to as a private variable, right?

10:52In other words, you're not supposed to touch it

10:55unless you're working inside the place

10:57where that variable is defined.

10:59This is a very common thing to see

11:01when we get into something called

11:02object-oriented programming in Python.

11:04All right, but anyway,

11:05don't worry too much about that right now.

11:07For now, just know that you can start a variable

11:10with an underscore character

11:11or multiple underscore characters if you really want, right?

11:15You're not often going to see things like that,

11:18but sometimes you will see something

11:19with a double underscore before it.

11:21That means something specific as well

11:25that we'll talk about a little later.

11:27So that's the first rule.

11:28The second rule here, and I'll write this out,

11:31is that any of the other characters in a variable name

11:34can be a letter, a number, or an underscore character, right?

11:39So in other words, we allow numbers

11:43just not at the beginning of the variable name, all right?

11:46So I'm gonna write this rule a little bit differently

11:48than the way that I said it,

11:49but I'm gonna say numbers are allowed anywhere else, right?

11:55In fact, I'll just write elsewhere because it's shorter.

11:58All right, so you can't start a variable name with a number,

12:00but the variable name can contain a number.

12:03So if we wanted to say something like message to,

12:06all right, that's totally fine.

12:08Hello again, we'll say.

12:10But you can't say to message, right?

12:12Python doesn't know what that means because,

12:16I'll say something like this doesn't work, all right?

12:20All right, Python doesn't know what that means

12:22because it assumes that this is supposed

12:24to be a numerical value and that we're trying

12:26to like multiply it by something,

12:29or it just assumes that we're trying

12:30to do something else with that number

12:33rather than refer to a variable name.

12:35So you can't do that.

12:37Just to leave this around and still be able to run the cell,

12:40I'm gonna do what's called commenting this line out.

12:42You can do that just by adding a hashtag mark, right?

12:46A number sign, you might call it, before this line.

12:49And what that'll do is that'll tell Python,

12:51hey, ignore this line so we can run this

12:54and Python won't complain.

12:55Whereas if we were to try and run this, look what happens.

12:58Python's gonna say invalid decimal literal,

13:01basically meaning I have no idea

13:02what you're trying to do here, right?

13:04So by commenting that out, we leave it around for reference,

13:07but it's not going to be, Python's not gonna try

13:11and run it when we run that cell.

13:13All right, so rule number three here.

13:16We're almost done, there's only four rules

13:18behind Python names.

13:20Rule number three is that Python variable names

13:23can't contain spaces.

13:25So I'll write this as no spaces.

13:26This is kind of obvious because if you were to try

13:29and put a space in a variable name, right?

13:31If you were to try and say something like another message,

13:35Python thinks that these another and message things

13:38are two separate items, right?

13:41Two separate variable names and it just doesn't

13:43really understand what we're trying to do there.

13:45So no spaces, all right?

13:47In fact, just for reference, I'm gonna put this down here

13:50after our other one and I'm gonna comment it out.

13:52So we'll say another message.

13:54This doesn't work either, right?

13:58And the final rule here, let me just scroll down

14:01a little bit there.

14:03The final rule here is that Python variable names

14:06are case sensitive, all right?

14:09So I'll just write that out here.

14:11And what this means is that the variable message here

14:15is different from the variable message with a capital M

14:18and that's different from the variable message

14:21with all capital letters and that's different

14:23from the variable message with two capital S's

14:27in the middle.

14:28All of these are different variable names

14:30even though to the human eye, they might look similar,

14:33right, they're all the same word.

14:35They're not the same variable and Python's going

14:37to treat these differently, all right?

14:40Now you might see in Python programs,

14:42just as a side note here, you might see variables

14:45defined all in caps.

14:46Typically what that means is that that's what's known

14:48as a constant in a Python program and that just means

14:52that you're not supposed to change the value

14:54of that variable anywhere else in the program.

14:57Python doesn't actually enforce constants

14:59like many other programming languages.

15:02So anyway, those are the four main rules

15:05of naming things in Python.

15:07And I guess one thing I wanted to clarify

15:09is I said it starts with letter or underscore

15:13and then I said numbers are allowed elsewhere

15:15but basically the only things that are allowed elsewhere

15:19are letters, the underscore character, right?

15:24So I'll say letters, underscore and numbers

15:28are allowed elsewhere and I'll say only there

15:31at the beginning, right?

15:32So only letters, underscore and numbers are allowed

15:35elsewhere in the variable name.

15:36So you can't have dashes, you can't have exclamation points,

15:39you can't have symbols, only letters,

15:43the underscore character and numbers are allowed

15:45in the variable name, all right?

15:48So hopefully that helps you understand the basics

15:50of variables in Python.

15:52As I said in the snippet before the last video,

15:56these form the foundation of working with data in Python

15:59because well, without variables,

16:01you wouldn't have anywhere to store the data

16:03that you're trying to work with and analyze.

16:05So hopefully this has helped you to understand

16:07the basics of how Python variables work.

Working With Python Data Types

0:00All right. Well, now that we've talked about the basics of working with variables in Python,

0:04the next thing that we're going to do is talk about data types. So, you know, so far,

0:10you probably noticed in the last video that there were two fundamentally different types of data

0:16that I put into these variables. So one type is these numbers here, right? 14, 20, and then 5,

0:23and well, 999 down here. But then in the last part of the video, I switched over and started

0:32using these, you know, this text as the values of variables, right? Hello. Hey, hello again.

0:38This doesn't work. This doesn't work either. All right. And so these kind of act as hints

0:42into the world of data types that we have to deal with when working with Python programs.

0:47And, you know, I guess right off the bat, I'll start off by telling you that these two data

0:52types that we've been working with, right? These numbers, which by the way, are specifically

0:56numbers without a decimal point. These are referred to as integers. Okay. So integer numbers are

1:04basically just, you know, in basic mathematics, these are just numbers with no decimal point.

1:09They're counting numbers. So they would be numbers that would answer the question like,

1:14how many people are here where you're not going to have a, you know, a decimal portion in your

1:19answer. Now down here with the text that we put into our variables, specifically, these values

1:27are referred to as strings. Now that might seem like kind of a strange name for text, but that's

1:34just what they're called, right? They're called strings. So, so far we've seen integers and

1:40strings. And the interesting thing that I want to point out here as well, is that another way in

1:46which Python is different from other programming languages is that it's what's known as dynamically

1:52typed. That might seem like kind of a big, scary word this early in the course, but in dynamically

1:58typed languages, let me just write that out here, dynamically typed. The variables can hold any type

2:07of value you want, and they can switch halfway through the program, right? So in other words,

2:12we could say X equals five. That's an integer, right? X is holding an integer value now.

2:18Later on, we could say X equals hello, right? Now it's holding a string. Later on, we could change

2:24it back to an integer. So we could say X equals 10. We could change it to what's known as a

2:29floating point number, another somewhat strange name, but this just refers to a number that has

2:34a decimal portion. And then later on, we could change it to something called a Boolean value,

2:40such as true, which we'll talk about in a little bit more detail shortly.

2:44So that's all that dynamically typed means, is that in Python, we don't have to specifically

2:50say, as we do in many other languages, which are referred to as statically typed languages,

2:55that X is only going to hold an integer value, that X is only going to hold a string value,

2:59and so on. Now, this does have downsides. For one, you have to remember what type of variable

3:06is currently holding, and that can cause some bugs and some crashes in many cases.

3:12But when you're writing code, it is a lot easier to not really have to worry about that. And so

3:17anyway, that's just the way that Python is. So for better or worse, that's just how Python

3:22variables work. They're dynamically typed. So let's get back to talking about those other

3:27types, right, besides integers and strings. The next type that I wanted to take a look at here

3:32is the float type in Python. And so, you know, floating point numbers in Python are just numbers,

3:39as I said, with a decimal portion. So if we wanted to say something like,

3:44you know, pi equals 3.14159, right, that would have to be a floating point number because it

3:52has a decimal portion, right? And by the way, Python will automatically type these things

3:58correctly, right? So as soon as it sees a decimal point in there, it'll automatically make this a

4:02floating point number. There's nothing that you as the developer have to do when you're assigning

4:08a value to a variable. Python will automatically figure out the type for that value. All right,

4:14so that's floating point numbers. And as I mentioned, there's another type called Booleans,

4:20and I'll just put that in another cell here. Boolean values, which is named after someone

4:26named George Boole, who was an English mathematician who invented Boolean algebra,

4:32according to Google. I had to look that one up because I remembered that it was named after a

4:36person, but I didn't remember exactly who it was. Anyway, that's why the name is so strange,

4:40is because it's based on someone's last name. All right, and Boolean values have two possible

4:46values. Now, they're not one and zero per se, although they do represent that basic idea in

4:53computers. Instead, in Python, these values are true with a capital T, which, again, is one more

5:01thing that makes Python different from most programming languages, is that the T is capital

5:06here. And the other possible value is false. Now, these two values represent those one and zero

5:13values that you'll often hear when talking about bits and bytes in computer science. True basically

5:19just represents a logical one, and false represents a logical zero. Now, how is this

5:25actually used in Python? Well, this may sound a little bit confusing at first, but typically

5:31Boolean values are used to represent the answer to a yes or no question. So let me just show you

5:38some examples of this, and that should help clear things up. Let's imagine that we have a variable

5:43name called something like, likes coffee, and that variable, oops, there we go, is equal to true.

5:51Well, what this means is whatever question we're asking here, right, whoever we're talking about

5:56with regards to liking coffee, they do in fact like coffee. So this would represent sort of the

6:02answer to the question, does, let's say, Sean like coffee? And in fact, we can make our variable name

6:07a little bit more explanatory by calling it Sean likes coffee, and that is in fact true.

6:12Now, if we were to define another variable here, right, and one that the answer would be no to,

6:19such as Sean likes anchovies, let's say, that would be false, at least when you eat them straight out

6:27of the can. I've never been a big fan of just eating anchovies like that. You can make some

6:32pretty delicious sauces with them if you dissolve them in a little bit of butter or olive oil, but

6:36anyway, all of that to say that maybe this isn't a great example because there are some,

6:41you know, there are some details to Sean not liking anchovies, but overall, we would say that

6:47this is false because, well, Sean doesn't like just plain old anchovies. And so anyway, that's

6:53the idea of these Boolean values, is that they allow us to gauge whether the answer to a yes or

6:59no question is yes, that is true, or no, which is false. And in many cases, these Boolean values can

7:07be the result of some sort of calculation. So if we define a variable called something like

7:14shoe size equals 14, like what we did up above, and let me actually make sure that I changed that

7:20back. I think I did. And then we say something like has big feet, right? Well, we could say

7:28shoe size is greater than maybe, I don't know, 12. And what that would do is that would actually

7:35give us the value true and assign it to the variable has big feet. Alright, so that's just

7:40another thing that you'll see there. Sometimes with regards to Boolean values, there's a lot

7:45of situations where this kind of thing is used. And we're, you know, a Boolean value can actually

7:51control aspects of our program, as we'll see. Alright, so anyway, those are really the main

7:58data types in Python. So once again, we have integers, which are just plain counting numbers

8:02with no decimal portion. We have strings, which are text, and these can be as short or as long

8:09as you want. In fact, we can have empty strings, which are just when you have two quotes like that

8:14with nothing in them. You can have single letters. So you could say just an A, there's not a separate

8:20character type like there are in some other programming languages like Java. Or you can

8:24have something a little longer. Or you could even have the entire text of a book contained in a

8:29string that is in fact possible. And in some cases, when you're doing, you know, text based

8:36analysis in Python, you will want to have an entire book contained in a single string. And

8:42then we have floating point numbers, which are just numbers that have some sort of decimal portion to

8:47them. And we have Boolean values, which can only take on the values true and false. Alright, so

8:54the last thing that I wanted to talk about here is that there are many situations where you might

8:58be given a value or a variable, and you don't immediately know what type it is. This often

9:05happens when you're loading data from like CSV files, let's say. In those cases, you may not know

9:11exactly what type Python has loaded a value in as. And so what you need to do is inspect that data,

9:20right, inspect that variable to find out which of these data types Python has assigned it.

9:27Now, the good news is that Python provides us with some built in functions. And we'll talk about

9:32functions in more detail a little bit later. But Python provides us with some built in tools,

9:36we'll call them for now, that allow us to inspect what type of value or variable is the first one

9:44is a function called type. Now the syntax for this, right, just the basic way that you type this out,

9:50is you say type, you immediately follow that with a set of parentheses. And inside those

9:55parentheses, you're going to put whatever variable or value you want to inspect. So if we want to

10:00look at the type of the pi variable that we defined up above, and do make sure you run that

10:05cell, otherwise, you'll get an error saying that pi doesn't exist, right? If you see that error,

10:10then that's why. So if we run that, what we'll see is that sure enough, pi is a float. All right,

10:16now if we run this on x up above, right, that we defined up here, which is an integer, sure enough,

10:22we see that that gives us int. If we run that on message, which was a string, right, that's up here,

10:28we see that that refers to that gives us the value str. And so anyway, that's the basics of

10:35using the type function. You can also call this on values too, by the way. So if you're curious

10:41about a value, right, if you want to see what type, let's say 3.0 is, you'll see that that is

10:47in fact a float. And actually, that's another thing I wanted to mention is you'll see many

10:51numbers in Python programs that just have like 3.0 or 2.0 or 0.0 sometimes. In those cases,

10:59we're just making sure that Python is interpreting those as floating point numbers, even though they

11:05don't currently have a decimal portion. When we say 3.0, we're saying that they could have a

11:10decimal point portion, and Python stores these things differently behind the scenes. It stores

11:16floats differently behind the scenes than say integers. Okay. So the other tool here that you'll

11:22see used sometimes is a function called is instance. And the way that this works is you

11:28actually give it two things in between the parentheses. The first is a variable or a value,

11:35right? So let's, um, let's say Sean likes coffee. All right. And let's say that we want to check

11:41whether or not that is a Boolean. Well, the way that we do this is we say is instance. And then

11:47in those parentheses, we put the variable we want to investigate, and something called a constructor

11:53for the type. Now, this is a little bit strange here. And we haven't really talked about

11:58constructors or object oriented programming. For now, all you have to know is that whatever gets

12:03printed out here, right? So if we say type, Sean likes coffee here, what we'll see is that that

12:08gives us bool, you just have to write that as the second, what's known as an argument, but we'll get

12:14to that, we'll get to that a little bit later as well. The second thing that you put in between

12:19those parentheses. So if we wanted to check if that's a Boolean, what that'll do is that'll tell

12:23us true or false. Is this, you know, is this variable or value this type? Okay. So another,

12:30another case where Booleans can answer a question such as is Sean likes coffee, a Boolean, right? Or

12:37we could say is, let's say the variable X a Boolean, that's going to give us false because

12:42X is in fact not a Boolean, it's an integer as we've seen. So anyway, these are the basics of

12:47working with data types in Python. In the next video, what I'm going to do is I'm going to give

12:52you a challenge that will help you take the idea of variables and data types a little bit further.

Challenge & Solution: Basic Arithmetic

0:00All right. Well, at this point, we've learned the basic building blocks of data in Python programs,

0:04which is variables and data types. And so it's time for you to do a challenge that's going to

0:09help you explore these things a little bit more. Now, specifically, one of the biggest things that

0:15we need to get used to when working with data types like Boolean strings, integers, floats,

0:21is the fact that there are different operations that we can use on different data types.

0:27And in many cases, the same operation, say addition or multiplication, will behave differently

0:33depending on the data types involved. Now, this might all sound complicated, but the way that

0:39this challenge is going to work is I'm simply going to ask you a few questions that will help

0:43you probe a little bit deeper into the world of data types. And it's going to be your job to use

0:49what we've learned in this skill to answer those questions. So the first question here is what

0:56happens when you add two strings together, right? So if you have one string and you say string plus

1:02string two, we'll call it, what happens in this case, right? What's the result? Just, you know,

1:08you don't need to guess now. Just use Python to figure it out. The second question here is what

1:14happens if you multiply a string by an integer? Okay. So if we say like hello times five,

1:22what happens? Just try running it and see what Python does. Okay. The third question here is

1:28what happens when we try to add a, an integer or some other number, it could be an int or a float.

1:36What happens when you try and add an int to a string? Okay. Just try it out and see what happens.

1:43All right. So next I'm going to give you a few more questions and these are going to be slightly

1:47different, right? So what I want to know is what type do you get if you, let's say, add an integer

1:56plus a float. Okay. What, what type does that give you? I'll write type next to this, just so

2:01that you remember that if you're taking a screenshot of this, the next question is what

2:06happens if you divide an int by an int? And then the last question here is what happens if you

2:14multiply an int by an int? That is what, what type do you get in both of these cases is what I'm

2:22interested in. So type and then type of this one as well. All right. So that's your challenge is

2:27just use Python to investigate the answers to these six questions. And once you've given this

2:33a try, you can move on to the next video where I'll show you the solutions. So best of luck,

2:37and I'll see you in the next video.

Challenge & Solution: Basic Arithmetic

0:00All right. Well, hopefully you gave this challenge a try.

0:02So let's take a look at the solutions.

0:04So the first thing that you had to do was see what happens when you add two

0:09strings together. And you know, you could have assigned these to variables,

0:13but you know, just to keep things simple,

0:15what I'm going to do is I'm just going to hard code these strings.

0:18So I'll say something like, hello, and then I'll add someone's name to it,

0:23right? I'll just use my name. Why not?

0:25And what we'll see is if we do this, right,

0:28if we add two strings together,

0:32what we'll get is a single string that basically just has both those

0:36strings sort of smooshed together. Now this might seem like obvious behavior,

0:41and certainly this is the behavior that we want in most cases when working with

0:44strings.

0:44But I wanted to show you this because note how different this is from the way

0:49that numbers work, right?

0:50If we were to append numbers when we use the plus sign,

0:54then that would mean that three plus four would be 34, right?

0:59And that's not true, of course. So just, you know,

1:02this was just an introduction to the idea that the same symbol, right?

1:06This plus sign can implement totally different functionality when we're

1:11working with different data types. All right.

1:13So the next question was what happens if we multiply a string

1:18by a number? So what happens if we say like, hello, times five?

1:23Okay. Well, in this case,

1:25what we'll see is that this actually causes the string to

1:30repeat. All right. Now, once again,

1:31this is quite a bit different from the way that numbers work, right?

1:35If we were to say 10 times five, according to string logic,

1:38then that would give us one zero, one zero, one zero, one zero, one zero.

1:42And that's not what that's not what 10 times five actually is. It's 50.

1:47The third question is what happens if we try and add a number to

1:51a string, right?

1:53So what happens if we try and add like an integer to a string?

1:56Now this is something that people coming to Python from other programming

1:59languages, try all the time. And they're surprised that spoiler alert,

2:03it doesn't actually work, right? So if we say something like the answer is,

2:08and then we try and add the value of a variable onto it, right?

2:11So we'll just say five here.

2:13What we'll see is that Python actually gives us an error in this case, right?

2:18Python says can only concatenate string, not int to string.

2:23So in other words, Python,

2:24unlike a lot of other programming languages doesn't allow us to add a number

2:29onto a string.

2:30And in case you're wondering how you can insert the value of variables into

2:34strings, Python actually has a special syntax for that.

2:37You add a little lowercase F at the beginning of the string,

2:40and then you can put whatever variable you want in curly braces in there.

2:44So if we have, let's say X, and here I'll define X as five here.

2:50And what that'll do is that will insert the value of the expression in those

2:55curly braces into the string.

2:57So that's the correct way of doing that in Python. All right. So anyway,

3:00the next thing was what happens if we add an int to a float?

3:03So what happens if we have the integer 10 and a floating point number like 4.5?

3:08Well, what you'll see is that that gives you a float.

3:11This really couldn't be anything but a float if it has a decimal portion.

3:15And the interesting thing is that this happens even if the floating point

3:18number doesn't have, um, you know,

3:21any actual numbers after the decimal point.

3:24So if we say 10 plus 4.0,

3:27what we'll see is that that gives us 14 as a floating point number.

3:31Now this is sometimes referred to as type promotion.

3:34Basically what this means is that because we have one type that's capable of

3:39representing sort of like more precise values and one that's not,

3:44we just take the one that's not right. The,

3:47the integer in this case and promote it to a floating point number before

3:51performing that calculation. All right.

3:53That's something that you don't really need to know too much about.

3:56All you need to know, right?

3:57The point of these exercises here is to show you that you really do need to think

4:02about the,

4:03the type of the result when you're working with different types of numbers in

4:07the same equation. This will happen a lot. All right.

4:10So let's see what happens when we divide an integer by an integer.

4:12So what happens if we say 10 divided by, um, four, right?

4:17Well, what we'll see is that that gives us a float still,

4:20even though both the numbers involved are integers. All right.

4:24So this is just something that Python does in order to avoid something called

4:27truncation, where the decimal point is just left off, right?

4:31And a lot of other programming languages, if you do something like this,

4:34then it'll just give you the answer too. And that's not quite right. Okay.

4:37So it's kind of nice that Python does this for us.

4:40And the last thing here is what happens if we multiply an integer by an integer?

4:43So what happens if we say 10 times five? Well, as we see,

4:47and somewhat unexpectedly, perhaps this leaves the answer as an

4:52integer as well. Now, why does it do this? Well,

4:55this is just something that you have to get used to when working with Python.

4:58Basically, um,

5:00the designers of the Python language went through and they decided

5:04which type made the most sense for each combination of values.

5:09And so in this case, when we multiply an integer by an integer,

5:12the result is an integer because frankly,

5:14there's not really any reason for it to be a floating point number, right?

5:17There's no chance of a decimal portion suddenly appearing in this case. So anyway,

5:21hopefully this challenge helps you to explore the combinations of data types in

5:26operations a little bit more and, you know,

5:29feel free to keep practicing this, right?

5:31So what happens if you say float plus string,

5:33is that any different from string plus float?

5:35Are there cases where the order matters?

5:37I'm going to leave it up to you to continue exploring those,

5:40but this is very important to think about when working with Python for data

5:45manipulation and data analysis.

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 PCED?

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