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 training IT teams trust.

Updated

52m

Skill 1 of 32 in PCED

What you'll learn

  • Create a Jupyter notebook in Anaconda cloud and run Python code cells with Shift+Enter to view output inline
  • Troubleshoot unexpected variable values caused by rerunning Jupyter cells out of order
  • Use Markdown cells in a Jupyter notebook to document and communicate analysis results
  • Assign values to descriptively named Python variables and update them with shorthand syntax
  • Identify integer, float, string, and Boolean data types and when each represents a value
  • Predict the result type of arithmetic that mixes integers and floats, including integer division

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

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)

Select all that apply.

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 variables and data types in Python, which will form the foundation for a lot of the more complex data manipulation and analysis that we're going to be taking a look at in this course. So the first thing that we have to look at, though, is we need to talk about how we're going to be writing and running Python code in this course. And the fact is that there are many ways that you can write and run Python programs, right? There's a pretty wide variety just because Python is such a widely used language across many different fields. Now, in this course, just to keep things as simple and straightforward as possible, we're going to be using Jupyter notebooks to write and run Python code. Now, you may have used these. In fact, these may have been what you used in order to learn Python in the first place. That's not an uncommon situation. Or you may be coming to this course just only having used things like IDEs to write and run Python code. But whatever the case, in this first video, I'm going to show you the basics of using Jupyter notebooks. And I guess as another side note, we're going to be using 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 your own notebooks. But essentially, this is just a nice, convenient way to write and run Python code interactively without having to download anything onto your own computer, right? So, you know, you should get exactly the same results here as what you're going to see me doing, which is always nice to have that kind of consistency, right? So once you've created an account, what you're going to want to do is open up the Anaconda JupyterLab, right? You should be able to do that. There should be a link as soon as you create an account that will allow you to do this. And JupyterLab, you can just, I don't know, you can think of it as like Microsoft Word, but for Jupyter notebooks, right? So it's just kind of a nice, convenient way of managing multiple 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 up JupyterLab is we're going to create a new notebook. Now, in case you're not familiar with what a notebook is, and we're going to select this Anaconda 2025 one, or, you know, you might see a slightly different version here. Just make sure it's not the Anaconda AI one. That one's slightly different. Just choose the regular old Anaconda notebook. And what that's going to do is open up a Jupyter notebook. Now, as I was saying, if you haven't worked with Jupyter notebooks before, I think you're really going to like them. Essentially what these allow you to do is write and run Python code interactively. Basically what that means is instead of having to write the code in a file, then open up a terminal and run the code manually, you can just write the code and I'll show you a hotkey here for how to run the code. And you'll just see the output of the program appear directly underneath each cell that we write the code in. There was probably a lot of jargon in what I just said there, so it might be best if we just get started and see how to write and run a simple Python program in Jupyter. Now, this is going to be a very simple Python program. What we're going to do here is just define two variables and 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 just defining two variables. Each one is going to hold a numerical value. So we'll just say like X equals, um, I don't know, 14 and Y equals 20. It doesn't really matter what numbers you pick. I just, those are just what fell under my fingers here on the keyboard. And then what we're going to do is we're just going to say X plus Y. And then what you're going to do is you're going to hit shift enter, and that will run the code that we just wrote there. And look at this, the output, right, which is basically the value of the last line that we have inside this little editor here is 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 happen. Now you can say print X plus Y and that'll have basically the same effect, but the default behavior of Jupyter notebooks is that they'll automatically print out whatever the value is of the last line. Okay. Now, you know, you could still use print if you wanted to do something after adding X and Y together. So, you know, if you were to define the variable called Z, let's say equals, I don't know, something like 30 after adding X and Y together, then what you'll see is that 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 wanted to see the value of this, that's when you would use print X plus Y. Okay. Just, just a few basic, um, you know, things to know when working with Jupyter notebooks. So that's the first thing I wanted to point out. The second thing is you may notice that after we ran this code here, another little code window appeared underneath it. Now these things in Jupyter notebooks are referred to as cells. All right. Now the idea of a cell in a Jupyter notebook is that a cell is a collection of lines of code that are all going to be run together. Now, essentially what this allows us to do, which is very helpful when performing data analysis or doing data science is this allows us to split up what would have otherwise been a much larger Python program into smaller chunks that we can run at the same time. Right? Um, so imagine that you want to do something and we'll be seeing the basics of how to do this a little bit later in the course. So you don't have to worry too much about it right now, but imagine that we're doing something like loading in data from a CSV file and then performing some cleaning on that data. We'll talk about data 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 without Jupyter notebooks, right? And without your code being split up into cells, that typically means that you have to write and run that code all in the same file at the same time. Right? So essentially if, if something goes wrong in the middle of that code after loading the data and then cleaning it, something like that, then, well, you have to start the whole thing over, which is very inconvenient in more complex cases. Don't worry too much about all the details here. I'm just describing why these cells are so important and why Jupyter does things this way. Um, so anyway, we already saw that you can run the contents of a cell by pressing shift and enter. And another important thing to remember about cells in Jupyter is that the variables that we create in any one cell in a notebook will be accessible in any other cell. So what this means is since we've defined X and Y up here in this cell, if we create another cell, which you can do just by clicking, you know, these little buttons here, right? If you click this one, that creates another 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 up here, we can access both of those inside this cell. So if we just type X and then press shift, enter here, that will run X and that will show us what its value is. If we say Y and then press shift enter, that'll show us the value of Y. Now where things can get a little bit tricky in more complex Jupyter notebooks is the fact that later cells can affect the values of variables in earlier cells, right? Now, this is not something that you typically experience with regular Python programs that you write and run inside a single file. So, you know, down here, if we were to say Y equals, and let's change the value to 999, right? And run it, that's going to change the value of that variable. And now if we go to the cell before it, where we're printing out the value of Y, we'll see that Y is now 999, okay? So essentially, each cell acts as sort of like a bundle of functionality 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, as long as you write and run your cells in a logical order, you can usually fix things that you've messed up like this just by rerunning the cells from top to bottom, right? So if we modified Y and didn't want to, then we could always rerun this cell up above, right? I'm running that with shift and enter again. And now if we rerun this cell where we're printing out the value of Y, we'll see that that gives us the value of 20 again, all right? So anyway, just a few more things that might be helpful to know as we're, you know, learning all about the basics of Python for data science and data analysis. If you want to move cells around, you can do so with these buttons here. So if we wanted to move this cell up to the top, which wouldn't really make sense because at this point we haven't yet defined Y, if we were to run all our cells from top to bottom, but you could move the cell up. If you wanted to, you can move the cell down just by clicking that down arrow. If you want to duplicate a cell, you can click this little create duplicate button there, and that'll just, you know, create another duplicate. This can be very helpful sometimes for editing 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 things. Cells in Jupyter notebooks can have, uh, one of several types. So these are all what are known as code cells, basically meaning that they just contain Python code that we can execute if we want to, right? It has an effect on the variables and data behind the scenes, but you can also set the type of a variable to mark down. And what that'll allow you to do, this is very commonly used for, um, you know, notation. So if we wanted to say mark down here, right, you just click on a cell and then set the type, uh, drop down here to mark down. We could say something like here are the basics of, um, defining and using variables in Python, right? And if we run this one by pressing shift and enter, what that'll do is that will simply render the mark down, right? And so this can be very helpful when you're doing things like communicating your findings with, um, you know, some regards to some sort of data analysis, right? It's really just used for communicating 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 any kind of markdown symbols you want. So if we wanted to be, uh, you know, if we wanted to add a heading here, a level three heading, we can use, um, three number signs or hashtags if you want to call them that. And we can say something like Python variables, right? And now if we run that, that'll be rendered as an H three heading. We'll talk about the syntax of markdown a little bit more detail later perhaps, but anyway, just know that there are really two main types of cells and that is code cells and markdown cells. You may have noticed this raw cell. This is just when you have some sort of raw content that you want to put inside a cell. The use cases for raw cells are a little bit beyond what we're going to talk about here. So for now, just know that we have code and markdown as options. Cool. So that should be really all that we need to know so far with regards to Jupyter notebooks. Um, so the last thing that we're going to do is we're going to save 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 so 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 of working with Jupyter Notebooks and writing code and running the cells, it's time to take a closer look at how Python variables work. So we've already seen how to define Python variables and add values to them. Essentially, we can create a variable in Python just by using a new name that we haven't used before for variables and saying equals some value, right? Now, there's a few things that I'd like to pull apart right there.

0:31The first thing is that, unlike with other programming languages, like, say, JavaScript, Java, C++, pretty much every programming language that I know of besides Python, you have to create a variable in a different way from how you change the value in a variable, right? So in other words, when you wanna create a variable in, say, JavaScript, you can say either var or let. Yes, JavaScript does have two different keywords for defining variables, but in JavaScript, we'll just use var.

1:04You would have to say var x equals 14 in order to define the variable, and then if you wanted to change the value later on, you would just say x equals 15, let's say. So Python's a little bit different from other programming languages in that respect where you can create a new variable just by saying x equals 14 or y equals 20. Now, there are a few situations where this gets a little bit confusing, but we'll touch on those a little later. For now, you just need to know that whenever you see a new variable name with equals something after it, we're creating a new variable.

1:42So that's the first thing. The second thing is I wanted to talk about this little equals sign because the fact is that equals signs in Python can have slightly different meanings depending on how they're used, right? So right here, we're using the equals sign as what's known as the assignment operator, which basically just means that we're putting a value into a variable, right? If you picture the variable as a container that holds a value, what the assignment variable does is it takes whatever's on the right-hand side of it and it puts it into that container, okay?

2:20Now, this isn't always the case. As we'll see a little bit later, the equals sign in Python can also be used as what's known as the equality or comparison operator where if we wanna check to see whether x is equal to, say, five, then we could say x and we use two equals signs, five, and that's gonna tell us, yes or no, is the value of x currently five? We'll talk about that in a little bit more detail later, but that's just something I wanted to get out of the way here.

2:51So what else? Once we've defined a variable, right? Once we've put a value into a variable, as we did here with x and y, as we've already seen, we can use that variable name just like how we would use a regular old value, right? So saying x plus y is going to figure out, okay, what's the value of x? Okay, we see it's 14. What's the value of y? We see it's 20. And what is 14 plus 20? That's essentially what's going on there behind the scenes when we say something like that.

3:21All right, so again, these variables are just sort of like convenient labels or containers for the values that we need to work with in our program. Now, as we've already seen down here, we can also do what's called reassigning variables, and this is quite simply when we change the value that a variable contains, right? So if y has 20 here and we say y equals 999, after that, as we've done here, what that's gonna do, right? Let's just imagine our little container y here with the value 20 in it.

3:54What that's gonna do is that's going to remove 20 from the container, and it's gonna replace it with whatever value we have there, right? So 999 in this case. And we can do this as often as we want. All right, so what else? Variables can depend on each other, right? So in other words, we have x and y here, which are both defined as simply, you know, a concrete or what's known as a hard-coded value. You'll hear me use this quite a lot in programming.

4:25So hard-coded values are basically just values that we have, that we've written explicitly in the code, right? So if we say, you know, if we wanna find out, let's say, the circumference of a circle and we have the radius, we could say r equals 10, and then we could find the circumference by saying c equals r times, and then we could hard-code the value of pi by saying 3.14159 and so on, right? All right, so that would be what's known as hard-coding the value of pi in this case, all right?

4:59Anyway, I just wanted to clear that piece of terminology up before we move on. But as I said, variables can depend on each other. So if we wanted, let's say, if we wanted to create another variable called z and set that equal to the sum of x and y, then all we would have to do is say z equals x plus y, and now if we print out the value of z down below, right, which we can just do by saying z at the very bottom of the cell and running it, we'll see that z now contains that value.

5:30All right, now just like how variables can depend on the values of other variables when we're defining or assigning values to them, they can also depend on themselves, right? Or that sounds a little bit odd. So let me just show you what I mean. We can actually update a variable with regards to its current value. And this is a very, very common thing to do in data analysis, right? Let's say that we wanna get the sum of all of the values in some sort of data set column.

6:04Then what we would need to do is we would need to basically create a variable, loop through all of the values in that data set column, and then add each one onto that variable. We'll see how to do things like that a little bit later, but for now, all you need to know is that if we wanted to maybe raise the value of z by one, we could do that by saying, let's just move this here and add a new cell above it, we could do that by saying z equals z plus one. You'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 after doing that, isn't that interesting? Look at this, we got 36, and that's actually because I ran this cell twice. So this actually goes to show you some of the tricky things that you can run into with using Jupyter Notebook cells is that because the values are stored behind the scenes, if you were to rerun this cell multiple times, let's just try doing that real quick, right? You'll see that that will increment the value of z every time we run this cell.

7:08So if we wanted to reset z, we can just rerun this cell above it first, and then if we rerun that again, that'll give us the expected value that we had from before, right? We see 35. So this is a very common thing to see. In fact, it's so common that there's a shorthand syntax for it. Well, there's actually two shorthand syntaxes for it. The first is if you wanna add some value to an existing variable, then you can just say z plus equals, and then the value you wanna add to it.

7:40So if you wanna add five to z, then you can say z plus equals five, and that will increment the value of z by five. In other words, the new value of z is gonna be whatever its previous value was plus five. Okay, so if we run this again, what we'll see is sure enough, it's now 40, right? Because z was 35 after adding one to it in this cell, now it's 40. All right, so hopefully that clears up some of the details of working with variables in Python. The last thing that I wanna go into a little bit more detail on, because it's very important in Python, obviously, is the rules behind naming variables.

8:16All right, so so far, we've just been using simple letters like x, y, and z. In practice, you won't wanna use variable names that are this opaque, right? In other words, you'll wanna use descriptive variable names that give you a good idea of what is actually inside that variable, right? X equals 14, that doesn't mean anything. Even if you understand what this is doing behind the scenes, that we're assigning the value 14 to the variable x, we don't actually know what this thing means. Whereas if we were to say something like shoe size equals 14, it's a pretty big shoe size in America anyway, but that gives us a little bit more context as to what that value means.

8:57So that's typically what's recommended in Python programs is that you give your variables a name that's as descriptive as possible while not being obnoxiously long to type, okay? Now, so let's talk about the rules behind naming variables because Python doesn't allow us to name a variable just anything. So really, the rules behind variable names are as follows. A variable, and I'm just gonna change that one back to x for now, we'll just set that back to what it was before, and I'm gonna create a new cell down below, okay?

9:30So a variable can start with either a letter or the underscore character. That's the first rule in naming Python variables. So let me actually write this down here. We'll say starts with a letter, and this can be a capital or lowercase letter, or the underscore character, which is a shift and then the dash key. All right, so what this means is we could create a variable called something like message equals hello, right?

10:02Fairly straightforward, that's totally fine. That starts with a letter, or we could start it with an underscore character and say underscore message equals hey, right? Now, why on earth would you wanna start a variable with an underscore character? Well, this is one of those things in Python that's referred to as a convention, right? In other words, a convention is something that you're not required to do by the syntax, right? The code will run just fine if you ignore the convention, but it's something that Python developers typically follow just in order to enhance communication between multiple developers.

10:43And so typically when you see this underscore character at the beginning of a variable name, it means that this is something that's referred to as a private variable, right? In other words, you're not supposed to touch it unless you're working inside the place where that variable is defined. This is a very common thing to see when we get into something called object-oriented programming in Python. All right, but anyway, don't worry too much about that right now. For now, just know that you can start a variable with an underscore character or multiple underscore characters if you really want, right?

11:15You're not often going to see things like that, but sometimes you will see something with a double underscore before it. That means something specific as well that we'll talk about a little later. So that's the first rule. The second rule here, and I'll write this out, is that any of the other characters in a variable name can be a letter, a number, or an underscore character, right? So in other words, we allow numbers just not at the beginning of the variable name, all right?

11:46So I'm gonna write this rule a little bit differently than the way that I said it, but I'm gonna say numbers are allowed anywhere else, right? In fact, I'll just write elsewhere because it's shorter. All right, so you can't start a variable name with a number, but the variable name can contain a number. So if we wanted to say something like message to, all right, that's totally fine. Hello again, we'll say. But you can't say to message, right? Python doesn't know what that means because, I'll say something like this doesn't work, all right?

12:20All right, Python doesn't know what that means because it assumes that this is supposed to be a numerical value and that we're trying to like multiply it by something, or it just assumes that we're trying to do something else with that number rather than refer to a variable name. So you can't do that. Just to leave this around and still be able to run the cell, I'm gonna do what's called commenting this line out. You can do that just by adding a hashtag mark, right? A number sign, you might call it, before this line. And what that'll do is that'll tell Python, hey, ignore this line so we can run this and Python won't complain.

12:55Whereas if we were to try and run this, look what happens. Python's gonna say invalid decimal literal, basically meaning I have no idea what you're trying to do here, right? So by commenting that out, we leave it around for reference, but it's not going to be, Python's not gonna try and run it when we run that cell. All right, so rule number three here. We're almost done, there's only four rules behind Python names. Rule number three is that Python variable names can't contain spaces. So I'll write this as no spaces.

13:26This is kind of obvious because if you were to try and put a space in a variable name, right? If you were to try and say something like another message, Python thinks that these another and message things are two separate items, right? Two separate variable names and it just doesn't really understand what we're trying to do there. So no spaces, all right? In fact, just for reference, I'm gonna put this down here after our other one and I'm gonna comment it out. So we'll say another message. This doesn't work either, right?

13:58And the final rule here, let me just scroll down a little bit there. The final rule here is that Python variable names are case sensitive, all right? So I'll just write that out here. And what this means is that the variable message here is different from the variable message with a capital M and that's different from the variable message with all capital letters and that's different from the variable message with two capital S's in the middle. All of these are different variable names even though to the human eye, they might look similar, right, they're all the same word.

14:35They're not the same variable and Python's going to treat these differently, all right? Now you might see in Python programs, just as a side note here, you might see variables defined all in caps. Typically what that means is that that's what's known as a constant in a Python program and that just means that you're not supposed to change the value of that variable anywhere else in the program. Python doesn't actually enforce constants like many other programming languages. So anyway, those are the four main rules of naming things in Python.

15:07And I guess one thing I wanted to clarify is I said it starts with letter or underscore and then I said numbers are allowed elsewhere but basically the only things that are allowed elsewhere are letters, the underscore character, right? So I'll say letters, underscore and numbers are allowed elsewhere and I'll say only there at the beginning, right? So only letters, underscore and numbers are allowed elsewhere in the variable name. So you can't have dashes, you can't have exclamation points, you can't have symbols, only letters, the underscore character and numbers are allowed in the variable name, all right?

15:48So hopefully that helps you understand the basics of variables in Python. As I said in the snippet before the last video, these form the foundation of working with data in Python because well, without variables, you wouldn't have anywhere to store the data that you're trying to work with and analyze. So hopefully this has helped you to understand the 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, the next thing that we're going to do is talk about data types. So, you know, so far, you probably noticed in the last video that there were two fundamentally different types of data that I put into these variables. So one type is these numbers here, right? 14, 20, and then 5, and well, 999 down here. But then in the last part of the video, I switched over and started using 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 into the world of data types that we have to deal with when working with Python programs. And, you know, I guess right off the bat, I'll start off by telling you that these two data types that we've been working with, right? These numbers, which by the way, are specifically numbers without a decimal point. These are referred to as integers. Okay. So integer numbers are basically 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, how many people are here where you're not going to have a, you know, a decimal portion in your answer. Now down here with the text that we put into our variables, specifically, these values are referred to as strings. Now that might seem like kind of a strange name for text, but that's just what they're called, right? They're called strings. So, so far we've seen integers and strings. And the interesting thing that I want to point out here as well, is that another way in which Python is different from other programming languages is that it's what's known as dynamically typed. That might seem like kind of a big, scary word this early in the course, but in dynamically typed languages, let me just write that out here, dynamically typed. The variables can hold any type of value you want, and they can switch halfway through the program, right? So in other words, we 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 it back to an integer. So we could say X equals 10. We could change it to what's known as a floating point number, another somewhat strange name, but this just refers to a number that has a decimal portion. And then later on, we could change it to something called a Boolean value, such as true, which we'll talk about in a little bit more detail shortly. So that's all that dynamically typed means, is that in Python, we don't have to specifically say, as we do in many other languages, which are referred to as statically typed languages, that X is only going to hold an integer value, that X is only going to hold a string value, and so on. Now, this does have downsides. For one, you have to remember what type of variable is 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 anyway, that's just the way that Python is. So for better or worse, that's just how Python variables work. They're dynamically typed. So let's get back to talking about those other types, right, besides integers and strings. The next type that I wanted to take a look at here is the float type in Python. And so, you know, floating point numbers in Python are just numbers, as I said, with a decimal portion. So if we wanted to say something like, you know, pi equals 3.14159, right, that would have to be a floating point number because it has a decimal portion, right? And by the way, Python will automatically type these things correctly, right? So as soon as it sees a decimal point in there, it'll automatically make this a floating point number. There's nothing that you as the developer have to do when you're assigning a value to a variable. Python will automatically figure out the type for that value. All right, so that's floating point numbers. And as I mentioned, there's another type called Booleans, and I'll just put that in another cell here. Boolean values, which is named after someone named 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 person, but I didn't remember exactly who it was. Anyway, that's why the name is so strange, is because it's based on someone's last name. All right, and Boolean values have two possible values. Now, they're not one and zero per se, although they do represent that basic idea in computers. Instead, in Python, these values are true with a capital T, which, again, is one more thing that makes Python different from most programming languages, is that the T is capital here. And the other possible value is false. Now, these two values represent those one and zero values that you'll often hear when talking about bits and bytes in computer science. True basically just represents a logical one, and false represents a logical zero. Now, how is this actually used in Python? Well, this may sound a little bit confusing at first, but typically Boolean values are used to represent the answer to a yes or no question. So let me just show you some examples of this, and that should help clear things up. Let's imagine that we have a variable name 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 with regards to liking coffee, they do in fact like coffee. So this would represent sort of the answer to the question, does, let's say, Sean like coffee? And in fact, we can make our variable name a little bit more explanatory by calling it Sean likes coffee, and that is in fact true. Now, if we were to define another variable here, right, and one that the answer would be no to, such as Sean likes anchovies, let's say, that would be false, at least when you eat them straight out of the can. I've never been a big fan of just eating anchovies like that. You can make some pretty delicious sauces with them if you dissolve them in a little bit of butter or olive oil, but anyway, all of that to say that maybe this isn't a great example because there are some, you know, there are some details to Sean not liking anchovies, but overall, we would say that this is false because, well, Sean doesn't like just plain old anchovies. And so anyway, that's the idea of these Boolean values, is that they allow us to gauge whether the answer to a yes or no 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 shoe size equals 14, like what we did up above, and let me actually make sure that I changed that back. I think I did. And then we say something like has big feet, right? Well, we could say shoe size is greater than maybe, I don't know, 12. And what that would do is that would actually give us the value true and assign it to the variable has big feet. Alright, so that's just another thing that you'll see there. Sometimes with regards to Boolean values, there's a lot of situations where this kind of thing is used. And we're, you know, a Boolean value can actually control aspects of our program, as we'll see. Alright, so anyway, those are really the main data types in Python. So once again, we have integers, which are just plain counting numbers with no decimal portion. We have strings, which are text, and these can be as short or as long as you want. In fact, we can have empty strings, which are just when you have two quotes like that with nothing in them. You can have single letters. So you could say just an A, there's not a separate character 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 string that is in fact possible. And in some cases, when you're doing, you know, text based analysis in Python, you will want to have an entire book contained in a single string. And then we have floating point numbers, which are just numbers that have some sort of decimal portion to them. And we have Boolean values, which can only take on the values true and false. Alright, so the last thing that I wanted to talk about here is that there are many situations where you might be given a value or a variable, and you don't immediately know what type it is. This often happens when you're loading data from like CSV files, let's say. In those cases, you may not know exactly what type Python has loaded a value in as. And so what you need to do is inspect that data, right, 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 functions in more detail a little bit later. But Python provides us with some built in tools, we'll call them for now, that allow us to inspect what type of value or variable is the first one is a function called type. Now the syntax for this, right, just the basic way that you type this out, is you say type, you immediately follow that with a set of parentheses. And inside those parentheses, you're going to put whatever variable or value you want to inspect. So if we want to look at the type of the pi variable that we defined up above, and do make sure you run that cell, otherwise, you'll get an error saying that pi doesn't exist, right? If you see that error, then that's why. So if we run that, what we'll see is that sure enough, pi is a float. All right, now if we run this on x up above, right, that we defined up here, which is an integer, sure enough, we see that that gives us int. If we run that on message, which was a string, right, that's up here, we see that that refers to that gives us the value str. And so anyway, that's the basics of using the type function. You can also call this on values too, by the way. So if you're curious about 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 numbers in Python programs that just have like 3.0 or 2.0 or 0.0 sometimes. In those cases, we're just making sure that Python is interpreting those as floating point numbers, even though they don't currently have a decimal portion. When we say 3.0, we're saying that they could have a decimal point portion, and Python stores these things differently behind the scenes. It stores floats differently behind the scenes than say integers. Okay. So the other tool here that you'll see used sometimes is a function called is instance. And the way that this works is you actually give it two things in between the parentheses. The first is a variable or a value, right? So let's, um, let's say Sean likes coffee. All right. And let's say that we want to check whether or not that is a Boolean. Well, the way that we do this is we say is instance. And then in those parentheses, we put the variable we want to investigate, and something called a constructor for the type. Now, this is a little bit strange here. And we haven't really talked about constructors 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 gives us bool, you just have to write that as the second, what's known as an argument, but we'll get to that, we'll get to that a little bit later as well. The second thing that you put in between those parentheses. So if we wanted to check if that's a Boolean, what that'll do is that'll tell us true or false. Is this, you know, is this variable or value this type? Okay. So another, another case where Booleans can answer a question such as is Sean likes coffee, a Boolean, right? Or we could say is, let's say the variable X a Boolean, that's going to give us false because X is in fact not a Boolean, it's an integer as we've seen. So anyway, these are the basics of working with data types in Python. In the next video, what I'm going to do is I'm going to give you 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, which is variables and data types. And so it's time for you to do a challenge that's going to help you explore these things a little bit more. Now, specifically, one of the biggest things that we need to get used to when working with data types like Boolean strings, integers, floats, is the fact that there are different operations that we can use on different data types. And in many cases, the same operation, say addition or multiplication, will behave differently depending on the data types involved. Now, this might all sound complicated, but the way that this challenge is going to work is I'm simply going to ask you a few questions that will help you probe a little bit deeper into the world of data types. And it's going to be your job to use what we've learned in this skill to answer those questions. So the first question here is what happens when you add two strings together, right? So if you have one string and you say string plus string two, we'll call it, what happens in this case, right? What's the result? Just, you know, you don't need to guess now. Just use Python to figure it out. The second question here is what happens 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 what happens when we try to add a, an integer or some other number, it could be an int or a float. What happens when you try and add an int to a string? Okay. Just try it out and see what happens. All right. So next I'm going to give you a few more questions and these are going to be slightly different, right? So what I want to know is what type do you get if you, let's say, add an integer plus a float. Okay. What, what type does that give you? I'll write type next to this, just so that you remember that if you're taking a screenshot of this, the next question is what happens if you divide an int by an int? And then the last question here is what happens if you multiply an int by an int? That is what, what type do you get in both of these cases is what I'm interested in. So type and then type of this one as well. All right. So that's your challenge is just use Python to investigate the answers to these six questions. And once you've given this a 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. So let's take a look at the solutions. So the first thing that you had to do was see what happens when you add two strings together. And you know, you could have assigned these to variables, but you know, just to keep things simple, what I'm going to do is I'm just going to hard code these strings. So I'll say something like, hello, and then I'll add someone's name to it, right? I'll just use my name. Why not? And what we'll see is if we do this, right, if we add two strings together, what we'll get is a single string that basically just has both those strings sort of smooshed together. Now this might seem like obvious behavior, and certainly this is the behavior that we want in most cases when working with strings.

0:44But I wanted to show you this because note how different this is from the way that numbers work, right? If we were to append numbers when we use the plus sign, then that would mean that three plus four would be 34, right? And that's not true, of course. So just, you know, this was just an introduction to the idea that the same symbol, right? This plus sign can implement totally different functionality when we're working with different data types. All right. So the next question was what happens if we multiply a string by a number? So what happens if we say like, hello, times five?

1:23Okay. Well, in this case, what we'll see is that this actually causes the string to repeat. All right. Now, once again, this is quite a bit different from the way that numbers work, right? If we were to say 10 times five, according to string logic, then that would give us one zero, one zero, one zero, one zero, one zero. And that's not what that's not what 10 times five actually is. It's 50. The third question is what happens if we try and add a number to a string, right? So 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 languages, try all the time. And they're surprised that spoiler alert, it doesn't actually work, right? So if we say something like the answer is, and then we try and add the value of a variable onto it, right? So we'll just say five here. What we'll see is that Python actually gives us an error in this case, right? Python says can only concatenate string, not int to string. So in other words, Python, unlike a lot of other programming languages doesn't allow us to add a number onto a string.

2:30And in case you're wondering how you can insert the value of variables into strings, Python actually has a special syntax for that. You add a little lowercase F at the beginning of the string, and then you can put whatever variable you want in curly braces in there. So if we have, let's say X, and here I'll define X as five here. And what that'll do is that will insert the value of the expression in those curly braces into the string. So that's the correct way of doing that in Python. All right. So anyway, the 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? Well, what you'll see is that that gives you a float. This really couldn't be anything but a float if it has a decimal portion. And the interesting thing is that this happens even if the floating point number doesn't have, um, you know, any actual numbers after the decimal point. So if we say 10 plus 4.0, what we'll see is that that gives us 14 as a floating point number. Now this is sometimes referred to as type promotion.

3:34Basically what this means is that because we have one type that's capable of representing sort of like more precise values and one that's not, we just take the one that's not right. The, the integer in this case and promote it to a floating point number before performing that calculation. All right. That's something that you don't really need to know too much about. All you need to know, right? The point of these exercises here is to show you that you really do need to think about the, the type of the result when you're working with different types of numbers in the same equation. This will happen a lot. All right.

4:10So let's see what happens when we divide an integer by an integer. So what happens if we say 10 divided by, um, four, right? Well, what we'll see is that that gives us a float still, even though both the numbers involved are integers. All right. So this is just something that Python does in order to avoid something called truncation, where the decimal point is just left off, right? And a lot of other programming languages, if you do something like this, then it'll just give you the answer too. And that's not quite right. Okay. So it's kind of nice that Python does this for us. And 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, and somewhat unexpectedly, perhaps this leaves the answer as an integer as well. Now, why does it do this? Well, this is just something that you have to get used to when working with Python. Basically, um, the designers of the Python language went through and they decided which type made the most sense for each combination of values. And so in this case, when we multiply an integer by an integer, the result is an integer because frankly, there'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, hopefully this challenge helps you to explore the combinations of data types in operations a little bit more and, you know, feel free to keep practicing this, right? So what happens if you say float plus string, is that any different from string plus float? Are there cases where the order matters? I'm going to leave it up to you to continue exploring those, but this is very important to think about when working with Python for data manipulation and data analysis.

Frequently asked questions

What does the Python Variables And Data Types lesson cover?

This 52-minute lesson shows a team member how to write and run Python code in Jupyter notebooks on Anaconda cloud, assign and update descriptively named variables, and work with integer, float, string, and Boolean data types. It also covers using Markdown cells to document and communicate analysis results.

Do learners need to install Python or other software to follow this lesson?

No local installation is needed. The trainer uses Anaconda cloud, which requires creating an account but lets learners write and run Python code interactively without downloading anything onto their own computer.

Why does a Jupyter notebook variable show an unexpected value after rerunning a cell?

Jupyter stores variable values behind the scenes, so rerunning a cell that updates a variable changes it again each time. The lesson shows how to fix this by rerunning cells in a logical order from top to bottom to restore the expected value.

What happens in Python when you mix integers and floats in arithmetic?

Python promotes the integer to a floating point number before performing the calculation, so the result is a float, while multiplying two integers returns an integer. The lesson's arithmetic challenge has learners predict result types, including what happens when dividing one integer by another.

How does this lesson fit into the PCED course and certification?

Variables and data types form the foundation for the NumPy, Pandas, web scraping, and visualization work later in the Certified Entry-Level Data Analyst with Python (PCED) course. The course supports preparation for the PCED certification path, exam PCED-30-02, and Team Leads can assign Jupyter and NumPy foundations as the first phase of a rollout.

How can a Training Manager track a team's progress through this course?

CBT Nuggets Playlists can sequence the course rollout by role or milestone, and Team Reporting shows per-learner progress and completion, cohort and group views, and completion exports for audit evidence. IT Directors and Training Managers should plan for 32+ hours per learner across the course modules.

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