📱

Get Our Mobile App

Take your business learning on the go!

Download on the App StoreGet it on Google Play

Python for Data Science | Beginner Friendly Full Course in 5 Hours

Nicholas Renotte5:45:19

Transcription

What's happening, guys? Welcome to the live stream. My name is Nicholas Chernott, and in today's video, we're going to be going through a bunch of Python basics for data science. I'm so happy to have you here. Thanks everyone that's, um, tuned into the live chat. We're going to be going through a bunch of questions towards the end, but again, if you've got any questions at all, do hit me up in the comments below. So, if there's anything that I need to clarify, by all means, do let me know. Cool.

So, first things first, what are we going to be going through in this live stream? Well, what we're going to be doing is we're going to be, first up, taking a look as to why Python is the right language to learn, particularly when it comes to data science. So there's a whole bunch of languages, whole bunch of technologies out there—why is Python the reason, or why is Python the language that you should be learning? We'll be taking a look at that. Then what we're also going to do is we're going to set up a Python environment, and I'm actually going to show you a bunch of different types of Python environments as well. So again, we're not just going to stick to the standard Jupyter, and we're going to set up an installation, but we're also going to take a look at some different types of environments that you might choose to use for data science. Cool.

So, on that note, let's go on ahead and kick this thing off and actually start taking a look at why we should be learning Python. Alrighty, let me get the mic over. So there's a whole bunch of stuff that we're going to be taking a look at now. Quick thing: so why should Python be the language that you go on ahead and learn? Again, there's a whole bunch of different languages out there; there's people doing data science in, ah, people starting to do data science in JavaScript. Why is Python the language? Well, I decided to go and find some statistics for you. So this is literally something that I just brought up, and it sort of reaffirms part of the reason why data science, or part of the reason why Python is the language you should be taking a look at. So this is the GitHub Octoverse statistics, or October survey, and it's got a lot of information about languages that are really popular at this point in time. Now, let's skip all of this; let me actually show you this. So this is the chart that I wanted to take a look at, and it really starts to show you what are the most popular languages out there. So at the top, we have JavaScript. Now, JavaScript is a great language when it comes to building web applications and building back-end servers and front-end applications, but you can see, followed very closely by that, is Python. Now, Python is a great language when it comes to data science. I've probably said that a million times now, but there's a whole heap of development that's being done inside of the Python universe around that particular language, and there's a whole heap of development and whole heap of innovation that's happening in that space. So even though JavaScript's a great language, Python is the number one language when it comes to performing data science, and you can see that again; it is the second most used language within GitHub. So again, if you go through a bunch of GitHub repositories, you'll actually notice that Python is number two in this particular case, but it is number one for machine learning.

Now, the other reason why you should probably take a look at Python is the sheer number of people that are contributing it. So we're going to delve into this a little bit more, but this is the PyPI Python repository, or package index. So this—think of this as a big place in the cloud that has a whole bunch of different Python libraries that you can choose to use for your different projects. So say, for example, you wanted to make an API call, or if you wanted to buy a web scraper or use a web scraper, you can probably find the library that you're going to need in here. So it means you don't have to start from scratch; you can actually get started relatively easily, which is pretty cool, right? So there's a whole bunch of stuff, and just to, I guess, make that point hit home, there are 317,666 projects, 2 million, 2,755,973 different releases, whole ton of files, and 525,220 or 266 users out there. So there's a ton of people out there using Python. So again, it's a stable language; it's not going to go anywhere anytime soon.

Now, the other cool thing about Python is that there's again a whole heap of libraries which are really specific for machine learning, data science, deep learning—all of that good new stuff. So say, for example, you wanted to go and build a machine learning model; one of the most popular libraries is called scikit-learn. So if you wanted to build a classification model, or if you wanted to build a regression model, or perform some clustering, dimensionality reduction stuff, which we'll probably cover in a video in the future, but all of this stuff is available inside of Python via scikit-learn. Again, with Pandas—super popular library—you've got the ability to use this. Now that sort of establishes what types of packages are out there for existing data science use cases, but a key thing to note is that there's so much innovation happening in this environment and specifically inside of the Python universe, namely one of my favorites is all around TensorFlow. So again, TensorFlow is built by the Google team, and it allows you to build a deep neural network. So if you've seen any of the crazy advancements coming out using deep learning models, chances are you're going to be able to build this inside of TensorFlow. Another really popular library is called PyTorch, and again, there's a whole heap of innovation happening in that space as well. So PyTorch is—I believe it's actually developed by the Facebook team. So again, there's a whole him, a whole heap of really, really intelligent developers out there that are helping support that community.

Now, last but not least, my favorite library, which is actually growing like crazy at the moment, is Hugging Face Transformers. So again, Hugging Face Transformers allows you to perform state-of-the-art natural language processing. So if you wanted to generate text or generate code, you could do that; if you wanted to classify different blocks of text, again, you can do that, all using Python. Is it—have I made the case as to why Python's the language to learn yet? So again, there's a ton of stuff that you can do in here, and Python's a great language; it's probably my most used language out of everything that I do. Know, cool.

So that sort of establishes why Python is the language to learn. Now, the next thing that we're going to take a look at is how to get started. So in this first initial video, we're going to take a really, really easy—so we're not going to do too much coding; it's really going to be setting yourself up, and we'll really be going through a bit of a hello world example. So let's go on ahead and do this. So the first thing that we need to do is get Python and specifically a Python environment installed. Now, there's a bunch of different ways to code and work with Python, but probably the most popular or the most common way to work with Python, particularly when it comes to data science workflows, is to use an environment called Jupyter Notebook. So what we're going to go ahead and do is we're going to go ahead and install Jupyter Notebooks for the first time using an underlying framework called Anaconda. So if you want to do this yourself, all you need to do is jump on over to Google, and we'll try downloading it; we'll see what, how much bandwidth the stream is scraping, but what we're going to go ahead and do is just type in Anaconda and then Python. So you can see I've got that already there. You zoom in on that; it's not going to zoom in. Let's hit enter; Anaconda Python, and you're going to get this link up here. So it says, oh, it's https://www.anaconda.com, and you've got this individual edition here. The cool thing about the Anaconda distribution is that it contains pretty much everything you need to get started with Python, specifically for data science, and I've probably used this an absolute ton of times, but I'm also going to show you some other options as well. So namely, a free option if you want to use it for GPU and you don't want to install it on your machine, and I'll also show you a bit more of an enterprise-grade example as well. So what are we going to do? We're going to go ahead and install Anaconda. So if we step into this, you are going to—and you can see it's sort of exemplifying that there, your data science toolkit. So again, there's a whole bunch of stuff that Anaconda supports; you're able to do it all through there. Now, what are we going to do? So we are now going to go on ahead and install this for ourselves, and I'll show you how to spin up a Jupyter Notebook so that you can get started with your data science. Okay.

So now that we're on this page—so again, to get here, you just need to go to /anaconda.com/individual, and then you can hit download, and I'm not going to finish downloading it because it's probably going to slow down the stream, but in this particular case, we'll let it download whatever. So what we're going to be able to do once this is finished downloading—let's take a look at how long it's taking—oh, we're not going to download it. So I've gone and pre-baked one earlier and gone and downloaded it, so you can see that we've got Anaconda right there. Let me zoom in on that so you can see it. So you can see right there. So we've got our installation: Anaconda-3-2021.05-Windows-x86_64. So again, the version of Anaconda that gets downloaded, or the version or Anaconda that you use, is really going to be dependent on what type of machine you're using. So if you're using a Mac, then download Anaconda for Mac; again, it supports it or Mac or Linux; again, just download the version that's going to support your particular machine. Now that we've got that downloaded, what we can go on ahead and what we can go ahead and do is actually go ahead and install this. Now, this is probably the easy part. So all we need to do is double-click into it, and it's going to verify the downloader. What do you think about my water drinking glass? I got this, uh, finally; it was beer, right? Cool. So what we're gonna do is hit Next. So once that opens up, and then hit I agree, and in this particular case, I'm going to install it for everyone on the machine, so I'm going to hit All Users, hit Next, and that's probably going—uh, you probably won't be able to hear, see that, so it's just gonna pop up and say if you're gonna use admin, then hit Yes; you need to provide permissions if you're gonna do it for admin. If you don't have admin permissions, just install it for yourself; that's perfectly fine as well. So now that we've got this, you can choose your installation location, so you can see that there. So we've got the ability to change where we go ahead and install it, but in this particular case, that's perfectly fine. Then we're going to select Next, and then this is really important. So the best thing to do is to hit this: Add Anaconda3 to system PATH. This is going to make it so that you're able to access Anaconda from your command prompt. So again, I've, like, a bunch of times, I've gone ahead and installed this, and I forgot to tick that, and it means that I can't necessarily just type in Jupyter Notebook from my command prompt and open it up straight away. The best thing to do is just go ahead and tick that flag there, and you should be good to go. So once that's done, just go on ahead and hit Install, and this is going to go on ahead and install everything that you need, so that, like, it doesn't really take that long to install. So once that's done, you should be able to kick things off reasonably quickly, which we'll do in a second. All right, so we're almost done. There we go. Okay, so that is our installation now completed. So you can see it says Completed. All you need to go ahead and do is hit Next, Next, and then you don't need to open these up; that's fine. We're gonna—I'm gonna do the tutorial for you, so you don't need to go through the individual tutorial. So we can hit Finish, and that's done. Pretty cool, right? And that's the stream over? Nah. Okay.

So what we're now going to go ahead and do is start up Jupyter. So remember I said that the core place that you want to do, or that I find myself doing a lot of data science in, is inside of an application called Jupyter Notebook. So in order to open this up, all I want you to do is go to your command prompt, or in, if you're working on a Mac, open up a terminal and hit, or open up a command prompt. I'm going to go into a place that I want to work inside of, and I'm going to make this split-ish screen; you can see what I'm looking at. Beautiful. All right, so I'm going to go into my D drive, and I'm going to create a new Jupyter Notebook or start up Jupyter Notebook from there. So remember, Jupyter Notebook is going to be the environment that you can actually start coding in and that you can start writing Python code in relatively easily. So again, it's going to be a short tutorial; we're just going to get set up, and ideally, I want to get you to the hello world example. So what are we going to do? So in order to launch Jupyter, all you need to do is type Jupyter—and that's J-U-P-Y—and then Notebook, and then hit Enter. And so this is going to start up Jupyter Notebook inside and open it up inside of one of your browsers. Now, there is a bunch of stuff that you're going to see on the screen when you do open this up, so I want to take you through this. So first up, what you're going to see is all the files that you've got inside of your current folder directory. You're also going to see any notebooks that are currently running and any terminals that are currently running, but because we don't have any running, there's nothing showing under here, and you can also see any clusters that you're connected to. In this case, I'm not connected to any clusters, but you don't really need to worry about this when you're getting started. Again, this is more for when you go full pro, when you start actually using clusters, but I don't find myself using clusters all that much. So if you focus on Files and on the Running tab, you should be good. Now, what we're going to do is we're going to create a new Jupyter Notebook. So Jupyter Notebook is the, or Jupyter in this particular case is the application, and then what you tend to do is create Jupyter Notebooks from within the platform. So what we're going to do is create a new one. So right over here, you can see that I've got two buttons: I've got Upload and I've got New. What we need to do is hit New, and then in this particular case, if you're doing it for the first time, you're not going to have all this stuff under here; you're just going to have Python 3. So what I want you to do is select New and then select Python 3, and this is going to open up a Jupyter Notebook for you. So again, this is a huge achievement. So if you get to this stage, you're pretty much on your way to actually getting your environment up and running.

Now, from this Jupyter Notebook, I'm going to show you how to write your first line of Python. So we just want to print out Hello Nick. So what we're going to do is we're going to type out print, and this is a Python function; we're going to delve more into functions and more into different data types and variables in a future video, which is coming up. So what I'm going to say is I'm going to write print, and then to that I'm going to pass through some text. So again, you can just copy this verbatim for now because it's a really simple example. So I'm going to type print, and then I'm going to say Hello Nick, and that is your first line of Python written. So again, really, really simple. So this just sort of shows you how to get started. Now, let's break this down. So I've used a function here, and I've written print, and then I've included a break, a bracket, or whatever, what do you call it? I can't even think of the word now—parentheses—and then to that I've passed through a string. So in this particular case, the string is Hello Nick. If I wanted to change what I print out, I can type in Hello, who wants to be featured? Grav, we're going to choose you. Hello Gaurav. Now I'm going to print Hello Gaurav. If I wanted to print out somebody else's name, so for example, Dan's online, if I wanted to print out Hello Dan, I can type Hello Dan, and that is your first line of Python written. So again, a cool thing about this particular stream is that we're focused on setting up our environment. Now, I want to take you through a little bit more of Jupyter. Actually, I didn't actually explain how to run this line of code. So I wrote the line of code, then to actually run it, I've just hit Shift and Enter on my keyboard, and you can see that as I'm running that this little counter is incrementing. So I've run it once, and you can see I've run it again; it goes up six. If I run it again, it goes up seven; run it again, it goes up to eight. So I can go up and down using the arrows on my keyboard, and I'm gonna give you a couple of additional shortcuts that I use. If I wanted to add new cells—so each one of these is called a cell—if I wanted to add a new cell, I can just type in A, and that's going to add in a new cell. So you can see that there. If I wanted to delete a cell, I need to step out of it by hitting Escape. So you can see that when a particular cell is active, it goes green over the side here. If I wanted to actually perform one of these actions, so say, for example, I wanted to add a new cell, if I just type in AAA, it's not going to do anything. In order to do that, I need to escape out of it. So this needs to be blue over here; you can see it's green; needs to be blue, right? It's gone blue now, and now I can add in more cells, and if I wanted to delete cells, all I need to do is hit DD on my keyboard. So Day Day is going to delete a cell. So you can say we deleted one of the bottom. Day Day Day Day Day Day. Pretty cool, right? So those are two key things that you probably want to know: how to add in new cells, how to delete cells. Pretty cool, right? Let's take a look at what else we've got in here. So a common thing that I get asked all the time is, Hey Nick, you do a lot of your work inside of Jupyter Notebooks, but what happens if I wanted to do something, um, write a Python script or do something completely separate? The amazing thing about this is that you don't need to stick with a Jupyter Notebook; you can actually download this as a Python script. So I can hit File, Download as, and then you've got a whole bunch of different download options. So if I wanted to export it as an ASCII document, I could; you don't really—that I don't find myself doing that too much. If you wanted to export as HTML, LaTeX, Markdown, export as a Jupyter Notebook, which is the default; if I wanted to export as a PDF, I could; export it as a Python file, I could as well. So let's go ahead and export this as a Python file, and you can see I can hit Keep, and now what I'm going to have is a Python file. So if I go and open that, you can see that what we've gone and done is we've gone and exported our Jupyter Notebook into a Python script. So again, this is what it looks like inside of Python. So it's going to export all of these Jupyter Notebook properties as well, but if

You want, you can just delete these. Cool, that is your Python script. Now, if we wanted to go ahead and run this, we can actually open up a terminal. And I need to be in the same folder. So where is this right now? So we are in downloads, so we can go into—it’s actually put onto our desktop or D drive. So if I move this into the same repository that we’re currently working in, so you can see I’ve got it there, I can go into my D drive. And then if I wanted to run this right, I can just type in python untitled titled—let’s make this a bit bigger, why don’t we—untitled and then .pi. And it doesn’t look—let’s take a look, make sure it’s there. What do we call it? Untitled 2. That’s why it hasn’t run. So if you get that particular error, that’s because I typed the file name wrong. And you can see that by running that script down there, it’s gone and said hello Dan regardless. So again, we’re going to focus on mainly Jupyter Notebook, but I just sort of wanted to show you that if you wanted to write this inside of raw Python, you can still convert your code to do that.

Now let’s go back into Jupyter, because that’s our main focus. Really, really good practice with Jupyter Notebooks is to name your Jupyter Notebook. So we’re going to step into this title over here, and we’re going to name it—we’ll call it My First Notebook. And that is our notebook now named. Now, if we go into the same folder that we were working in—so remember we started out our Jupyter Notebook terminal from within our D drive—you can see that we’ve actually got that notebook saved there. So it says My First Notebook.ipynb. So that is the file format for Jupyter Notebooks. Cool. Then if we wanted to—so again, you can create a new notebook from here. So if you hit File, you’ve got New Notebook, you can open up a notebook, you can make a duplicate. So if I hit Make a Copy, it’s just going to copy over my notebook. So good if you want to try something out and not screw up your code. If you want to save it as, you could. Um, if you want to save your actual Jupyter Notebook and create a checkpoint, you can do that as well. But more often than not, I find myself really just working inside of these cells.

Now I’m going to give you a couple of additional tips. So if you wanted to run a cell, you can. If you wanted to clear output—so what you’re seeing down here, so the bit where it says hello Dan—this is referred to as the cell’s output. So this thing here is a cell, this is the output. If I wanted to clear this, all I need to do is hit Cell, and then Current Outputs, and then Clear. You can see it gets rid of that, so it sort of like cleans it up, right? But if I run it again, it comes back again. Now this particular cell that we’re running right now is known as a code cell. So you can see this up here, it says Code. Now the nice thing about Jupyter Notebooks is that you can get or set this up so it forms like a little bit of a story, and it’s kind of self-explanatory. This is why it is so popular with data scientists worldwide, right? So again, I’m going to hit Escape. So right now we’re green, right? So this cell is active. So if I hit Escape, hit A, I can now go into a new cell. Remember A gives me a new—gives me a new cell. Now I can actually convert this into a different type of cell. So over here you can see that we’ve got Code, we’ve got Markdown, we’ve got Raw NB Convert, and we’ve got Heading. Now I tend to find myself working inside of Code, but Code and Markdown the most. But Raw NB Convert allows you to—I believe it allows you to convert a raw Jupyter Notebook, but I haven’t—I don’t barely use that. Heading allows you to create headings—again, barely use that. I really use Code and Markdown. So these two are the most important. So if I leave it as Code, it’s just a code cell. So again, I can type in print test line test printing function, right? So that does that.

Now if I again hit Escape, add a new cell, what I’m going to do is I’m going to convert this cell over here from a code cell, which you can see there, and convert it into Markdown. So by hitting M on my keyboard, it goes into Markdown. So you can see that there. If I hit Y on my keyboard, it goes back to Code. So often you’ll see me, when I’m documenting code, I’ll switch between Code and Markdown. So again, you can see that really quickly there. So M Markdown, wi-fi Code and Markdown, wi-fi Code. There’s a whole bunch of shortcuts that you can see here as well. So if you hit this little keyboard over here, it gives you a whole bunch of information about the different shortcuts that you can use. Again, knock yourselves out, there’s a ton, but I tend to use D on my keyboard to delete a cell, A to add a cell, I think B to add a cell below. So if I hit B, it adds a cell below this one. If I hit A, it adds a cell above. Now we’re going to delete this cell. So what’s the shortcut for deleting? D, D, double D. Then what we’ll do is we’re going to convert this into a Markdown cell, and I’m going to give you a little bit of formatting, right? So right now it’s still a code cell up here. We’re going to convert this to Markdown, which you can now see, and now we can start to write. And so say, for example, we wanted to write a little bit of a story, so we can add in a header. And this is just Markdown. So if you haven’t worked with Markdown before, it’s sort of like a note-taking format which really allows you to document stuff really easily. It’s the same format that your homepage or your README inside of a GitHub repository will be formatted as, but think of it as just a way to add some commentary to your code. So we’re going to add in a heading, and I can add in a heading by hitting pound. And so you can see that’s my first heading. So this—what are we going to call it? This is the first heading. And then we could even add a subheading by adding in two pounds. So this one’s going to be a little bit smaller. Let’s add three pounds. Um, but this is a smaller heading. And then we can add some text as well. This is text about my Jupyter Notebook. Pretty sick, right? And if we hit Shift Enter, that actually runs our Markdown cell. So you can see now that this is a larger heading, this is a slightly smaller heading as I said, and then this is text. So this allows you to actually write information about what on earth is actually happening inside of your Jupyter Notebooks, inside of your Jupyter Notebooks.

Now again, you can actually add in HTML, you can add in images. So you’ll find some people on Kaggle, which is a really popular data science platform, um, having massive amounts of annotation and massive amounts of Markdown to help explain their code. But this sort of gives you an idea as to how to actually go about working with your Jupyter Notebook. Now the last bit of code or last bit of information that we’re going to go through before we take a look at the other platforms is how to get some information about what you’re doing. So say, for example, I wanted to learn about the print function, right? So I’ve written print, and I know that I can pass through this bit of text, and it’s going to print something out. So this bit of text—how do I know what the hell the print function actually does? Well, what I can do is get rid of the brackets—in this particular case, it’s a function—and I can add question mark question mark. Let’s get rid of this at the bottom, hit Shift Enter, and this is going to open up documentation for me. So over here, this is going to tell me how to actually use the function. It’s going to give me a bit of information. So it prints the values to a stream or to sys.stdout by default. So again, it just tells me that I can print something else, but this makes it super, super useful whenever you’re getting stuck or whenever you need to try to work out how to actually run something. Gives you a whole heap of help when you’re actually working inside of your Jupyter Notebook. So often I’ll be using a package or I’ll be trying to write some code and play, hey, how do I actually run this? Again and again, if you just remember question mark question mark, and this is going to go, oh, all right. So in order to use print, I need to pass through a value. So let’s go and pass through a value, one, two, three, four, five. And you can see now I’ve gone and printed out some numbers. So again, print—you can print out strings and numbers. We’re going to cover that in way more detail in our next couple of videos. Okay, so that is the first part of our Jupyter Notebook, pretty much set up. So again, this is all about setup and installation in this stream. I’m going to delete this last cell, but I want to show you how to do it in a slightly different manner. So again, this is going to be the Jupyter Notebook environment that we’re working in and that we’re going to be using going forward. But say, for example, you start work at a different company, and they go, hey, Nick, we use this particular tool, or we use this particular tool. If you’re comfortable with your Python and if you’ve worked with Jupyter Notebooks before, more often than not you’re probably going to be fine. But I want to show you two different tools and sort of what they look like. Again, they’re still sort of Jupyter Notebook based, but it sort of gives you an idea as to how to actually go about using them. So the first one is Colab. I know I get a lot of requests for, hey, Nick, do this in Colab, can you show me how to actually use it? So again, if this is available via Google, and the cool thing about this is that you’re able to use it for free, pretty low setup, and you can integrate it into your Google Drive. Now if I wanted to go and print something, again, I’m just going to type in print, and then what did we type in in our notebook? Test printing function. Let’s uh—who do we want to say hello to now? Who said hello to me? Uh, Belmo. Let’s say hello Elmar. Cool. So now I can hit Shift Enter, and again, this is going to connect to my kernel. Probably been had it open for a little bit too long. Let’s run it again, and there you go. So you can see it’s gone and printed it out again. Really, really similar. Now if we wanted to go and add in another cell, we could do that as well. The nice thing about this is that you can add text cells, you can add in code cells, um, and the also the other cool thing about this is that if you wanted to use a GPU, you can. So if you’re going into harder core deep learning applications or if you wanted to use the GPU, you can do that through here as well. So that sort of gives you a free alternative that you can test out. But again, the thing about this is that it’s ephemeral, right? So it’s going to last for a certain period of time, and then it’s going to disappear. Say, for example, now you’re going to move to a different company, and the different company goes, hey, we’ve got more of an enterprise grade data science platform. How do we go about doing Jupyter or using Python or building up our data science workflow? In that particular case, well, I’m going to show you a tool that I use pretty much almost every day for work, and this is called Watson Studio. So this is something that I use at IBM in my daily work. Now say, for example, I needed to create a Jupyter Notebook for here, I sort of want to establish that it’s really, really similar as to how you go about doing that. So I’m going to hit over here, and if you want to see a deeper tutorial in that, let me know. I’m going to breeze through this. So I’m going to hit Add to Project, hit Notebook over here, and then from here what we’re actually going to see is that we can again create a notebook. I forgot I got to name it. We’re going to call this My First Notebook. So right over there, I’m just going to name my notebook. So this is again more of an enterprise grade data science platform, so um, this is obviously me doing it on my local machine. This is something that I might do if I wanted to do it in the cloud for free. This is more of an enterprise grade thing, and really once you sort of step up to larger data science teams, you’ll find them using more sort of data science platforms. So I’ve written my name, and then I can hit Create, and this is going to create a Jupyter Notebook similar to what you saw before. So you can see now it’s starting up, and then in a second we’ll be able to use our specific runtime.

Alrighty, so that is our notebook now set up. So again, really similar look and feel, right? This is sort of what I wanted to hone in for you guys. So keep in mind, this is the Jupyter Notebook that we just went installed on our computer, this is Colab, and then this is more of your enterprise grade thing, but they’ve all got really similar features, right? So again, you’ve got um, all of your different running settings over here in Colab, you’ve got all of your different running settings in your File menu up here in Jupyter, and again really similar inside of Watson Studio. So again, if I wanted to go and print something out, I can print Hello World. Again, going to run exactly the same. The core difference is where they run, the amount of compute and hardware that sits behind them, and sort of like collaboration and security tools. So this is obviously running on my local machines; I’ve got like a really slow powered desktop or laptop that I’m running on and might not actually be able to run my data science workloads a lot faster. Colab gives me the ability to do it in the cloud, but again, it’s sort of ephemeral, so I’m going to need to reset everything up each time I want to come back to my notebook. And then this is more of like a big data science platform, and again there’s a whole bunch of these out there. So this is sort of just one that I wanted to show you as well. But again, same sort of look and feel, right? So I can add cells, I can delete cells, I can add Markdown. So over here you can see I can convert this to Markdown: Hello World. And that, in a nutshell guys, is how to get set up with your Python environment for data science. So again, we went through a bunch of stuff. So we took a look at the different reasons to learn Python. So again, it’s a really popular language. By all means, do take a look, and learning Python as your first language is not going to be an incorrect decision. So in other words, it’s going to be a good decision if you do that. There’s a whole bunch of support, a whole bunch of innovation happening in it. Then what we went and did is we went and installed Jupyter Notebook on our local machine, so we can get started with that. So again, we went to Anaconda, downloaded it, and installed it. So if you wanted to use that, you definitely can. And then we actually went and wrote some codes. Remember, we can create a bunch of cells, we can convert them from code cells to Markdown cells. Markdown sort of gives you the ability to add documentation and write a bunch of stuff inside of your notebook. And remember these get saved as .ipynb file formats. But if I wanted to, I can export this to a raw Python file, which will do a little bit later as well, so you’ll actually be able to see a little bit more information about it. And then we also took a look at some different notebook types around the world and what you might encounter as you’re going on your journey as a data scientist. On that note, that about wraps it up. Let’s drop this down. So thanks so much again for tuning in, guys. Hopefully you enjoyed this video. If you did, be sure to give me a thumbs up, hit Subscribe, and tick that bell. And again, I’ll see you in the next one. Thanks again for tuning in.

What is happening, guys? Welcome to Part Two in the series on Python for Data Science. My name is Nicolas Chernot, and tonight we’ll be going through a bunch of stuff, but specifically we’re going to be taking it step by step. So if you’re a beginner, this is perfect for you. So we’ll build up step by step and build up from going from absolute zero Python knowledge all the way right up to being able to build up stuff by yourself. So what is it that we’re going to be going through tonight? Well, specifically we’re going to be going through four key things. Now these four things sort of form the building blocks of what is Python, right? Everything that you learn in this particular video is sort of going to establish a really, really good baseline for how you go about stepping through this. Now specifically we’re going to be going through how to create a variable, and I’ll explain what each one of these is; how to create different types of data types, or how to create different value types; how to work with lists, something which is so, so common and so, so useful when doing Python or specifically data science style stuff; and then we’re going to take a look at dictionaries as well. So I’ll show you the difference between each one of those. But before we get to that, there’s one key thing that I really want to teach you, and this is something that I wish I’d known when I started out on my journey in terms of programming in general, right? So I actually started off as a consultant way before I actually got into Python. So specifically I was writing stuff in like a proprietary language, and if I’d known these things that I’m about to teach, or this framework that I’m about to teach you, it would have made learning any programming language so, so much easier. Now the thing that I want you to remember when you’re learning any programming language is CRUD. Now you’re probably thinking, Nick, what on earth is CRUD? Well, it stands for Create, Read, Update, and Delete. If you learn how to do each of these four things within any programming language, it’s going to set you up a baseline that makes it a billion times easier to go and learn anything going on from that. So learning how to create different types of data or different value types or different types of objects inside of a programming language is going to really, really put you in a good set. Once you’ve learned how to create it, the next three things that you really want to learn how to do are how to read that—so ideally how to view what you’ve just created—how to update that—so if you’ve created, say, for example, a string or you’ve made a or declared a variable and assigned a number to it, how do we make an update to that—and again, if you are not familiar with any of these things, I’m going to explain them all in really, really deep detail—and then the last thing that you want to learn how to do is how do I delete something that I’ve just created? So if I’ve created an object, how do I go and delete that? So say, for example, I create a variable which is my name, so the variable will be called myName, and I’ll set it equal to Nick. How do I go and read that? Well, I can type out that variable and see that return to the screen. If I wanted to update that, how do I go and update that variable? I might choose to reassign a value to it. And say, for example, I’m done with the variable now and I want to get rid of it, how do I actually go and delete that in Python? Learning this framework—so CRUD—is going to make your programming journey a ton easier. If there’s nothing you take from this, by all means…

Do take credit away, because it's going to serve you a whole heap longer than potentially this course will, because it's applicable to virtually any programming or coding framework. Okay, that's enough of that; let's actually get into some coding.

So we've covered the credit framework; let's bring this a little closer, and what we're going to do now is get into some coding. So remember yesterday we were talking about how to create a Jupyter Notebook. So remember, we opened up a command prompt or a terminal, and this would allow us to open up a command prompt and/or this would allow us to open up a Jupyter Notebook, and from there we could start writing some Python code. So that's exactly what we're going to be doing in this video, but I'm actually going to get a lot deeper into the different components of Python tonight. So let's go ahead, and what we're going to do is we're going to create a new Jupyter Notebook.

So remember, we need to open up a command prompt, and then I'm going to go into the folder that I want to create my Jupyter Notebook. So I'm going to put this side by side so you can see it. So I want to create it inside of a folder called Python Basics, but you could create this wherever you wanted to create it. Right? So if you wanted to create it on your desktop, you could; if you wanted to create it in a specific folder that you've got for learning, you could definitely do that as well. What I like to do is keep all my Jupyter Notebooks associated to a specific project in the same location. So over here, what I'm actually going to do is put it inside of a folder called Python Basic, so you can see that up there. So I've got a folder inside of my D drive, inside of a folder called YouTube, and then inside of a folder called Python Basic. So that's where we're going to be putting all of our notebooks for our variables, different data types, lists, and dictionaries. So we're going to be putting them all in there.

Now, first up, what we need to do is from our terminal, we need to navigate to that folder. So I'm just going to navigate to it. So in order to do that on my Windows machine, I can first go to my D drive, and then what we need to do is go into the YouTube folder. So I'm going to go, and again, if you wanted to do it inside of a different folder, you could; if you wanted to do it on your desktop, you could just go into whichever folder that you want to execute it from. I could execute Jupyter from this top-level folder, and I'd be perfectly fine as well. So I'm going to go into the folder that I want to create it in, so I'm going to go into my YouTube folder and go into my Python Basics folder, and then what I'm going to do is I'm going to start Jupyter Notebooks. Pop quiz: how do we start Jupyter Notebooks? Again, I've literally given a little tidbit, so you type in Jupyter Notebook. So that has now gone and opened, right? So you can see that they opened up on my other screen, but that's gone and opened up our Jupyter Notebook, so we can now start to write a little Python code.

Now, what we need to do is create a Jupyter Notebook. So again, what we learned from the first lesson is to create a Jupyter Notebook; all we need to do is go over to the right, over here, and all we need to do is hit New, and we're just going to use our default Python environment and hit Python 3, and that is our notebook now opened up. So this is going to be the notebook that we're going to use for pretty much the majority of our Python basics components. So what we're going to do is we're going to name it Item Basics. So remember to rename it; all you need to do is go right up here where it says Untitled, and we can just step into that and call it uh Python Basics and hit Rename. Pretty cool, right?

All right, so we've gone and done that. Now, remember the last thing that we also learned is that we've got different types of cells when we actually go and use a Jupyter Notebook. So we've got a code cell; we've also got a markdown cell; we've got this raw notebook convert cell, and we've also got a heading cell. So what we're actually going to do is we're going to convert this first cell into a markdown cell because we want this to sort of be um to allow us to break up our code. So we're going to step out of it, because remember it's green at the moment. So if I hit Escape, it's going to go blue, means it's not active, and then I'm going to hit M on my keyboard to convert that into a markdown cell. So you can see that that's happened there. Then what I'm going to do is we're going to add a pound symbol to give us a bit of a header, and we're just going to call this Initial Section Variables. Cool. So that is our first header now set up.

Now, what we need to do is we're going to first up learn a little bit about variables. Actually, let's give this a number as well; I'm a sucker for numbering and structuring. If any of you guys have seen any of my videos before, so first up, what is a variable? So I want you to think of a variable as a placeholder which you can repeatedly refer to when you're trying to grab a value. So say, for example, I wanted to have a placeholder that holds my name, I could create a variable and then pass it a value and access that variable whenever I need to. If I wanted to later on change my name, I can go and update that variable, so that again, all I need to do is refer to that placeholder, and I can get my current name at that point in time. So say, for example, um this particular component or these coupler series that we're actually going to go through are going to have a bit of a space theme to it, given uh I've been getting—we're getting really into Tesla—uh SpaceX, not Tesla—more I'm into Tesla as well, but really into SpaceX. So we're going to call it space_name. So this is how we define our initial variable. Now, as of right now, our variable isn't defined. So if I go and hit Shift+Enter, it's going to give me this error here. So it says NameError: name 'space_name' is not defined. Now, in order to define it, I actually need to assign a value to this variable. So in order to do that, all I need to do is assign it—all I need to do is add a space, type in equals, and then give it a value. So right now, I don't—I haven't actually given it a value, so I need to give it a value. Now, say, for example, we wanted to store our name in this, so what we can do is add quotes, and then my name is going to be Astronaut Nick. So that is our variable now created. Pretty cool, right? And that is variables done. No, okay, so we're going to go into this in a little bit more detail. So that's our variable now assigned. Now, if I go and take a look at this variable now, so if I type in space_name, I can now get that value back. So you can see that it's now printing out Astronaut Nick, and remember if we use our print function, we can print that out as well, and it's gotten rid of our quotes.

Now, a key thing to note is that we've gone and defined this variable, and we've given it this value, right? So what does this value actually represent? Well, in the Python language, it actually represents a specific data type called a string. Now we can actually check the different types of data that are assigned to variables, and the way to do this—so remember we've used the print function to print out a variable, but what we can also do is use another function called type to check what type of variable a specific variable is. So if I type in type over here, so t-y-p-e, and then inside of the brackets if I pass through my variable, so space_name, you can see that it's printed at str. Now this represents the fact that it is a string. There are a bunch of different types of variables or different data types inside of Python. So say, for example, rather than having my name as Astronomic, I wanted to set it as a number—again, it probably doesn't make sense—but let's say, for example, add a serial number, right? So say my serial number was 123456. Oh, let's actually type into it—typing in the wrong space—123456. So what I've actually just gone and done there is I've gone and reassigned a new value to my same variable. Now, if I go and print it out, you can see that that value has now gone and changed. So remember, initially we actually held our name in there, and now we've got an assigned—rather than having a space name or my actual name in there, we've now gone and changed it to a space name ID, for example, and you can see there that it's now 123456. Remember how I said that what we've actually gone and done here is we've assigned a specific data type to a variable. If we go and take a look at this type now, you can see that rather than saying str, it now says int. Now int actually represents an integer. So this is a number that doesn't have any decimal places. If I added .012 and went and did the same—and remember—so what I've gone and done is I've gone and re-assigned a new value to our variable now, and if we go and print this out, you can see that it's now returning the word float. This is again a different data type. Now we're going to take a look at all the different data types, but let's quickly recap on what we've done there. So first up, what we've gone through is we've taken a look at variables. Now remember, a variable is sort of like a placeholder or a reference to a specific value or data type that you might want to use inside of Python. Now, in your data science journey, you're going to be using variables all over the place. More often than not, you're going to have multiple variables that you're working with; you're going to have different things assigned to them. This is a really fundamental concept that you really want to make sure that you understand. So we've gone and defined our variable; we've gone and printed it out; and we've also learned how to take a look at what type or what data type is actually assigned to a specific variable. So remember, we can print out a variable using the print function, and we can also take a look at the type using the type function down here. So that is variables in a nutshell.

Now we've talked a bit about different data types, right? So remember, we first up assigned a string, so if we go and type in Ctrl+Z, so it was initially Astronaut Nick, and then we changed it to 123456, and then we went and added a decimal place and added some additional numbers to it. So what are we going to do now? We're actually going to take a look at all the different types of data types that we've actually got available. So inside of our new cell, again, we're going to get into a repeatable process, so we're going to structure our notebook really, really nicely. So we're going to convert this cell to a markdown cell. Now, how do we do that again? We need to step out of it, so I'm inside at the moment, and it's green. If I hit Escape, it's going to go blue, which means it's not active at the moment, and if I hit M on my keyboard, it's going to go from a code cell up here to a markdown cell. So if I hit M—watch this cell up—up here—let's actually zoom in so you can see it—if I hit M, boom, markdown. Remember, if we want to go back to code, it's Y; M is markdown; Y is code; M is markdown; Y is code. So that sort of gives you a feel for how you can quickly convert these cells. So we're going to hit M to keep it as markdown, and we are going to define a new section. Now, this next section just so happens to be called Data Types. So we're going to add in a pound symbol again, and we are going to define our new section as Data Types.

Now you're probably thinking, Nick, why are you spending so much time on variables and data types? Well, these are the fundamental concepts that you're going to want to learn when you go and start leveraging Python for data science. So often I've got stuck on a specific theme because, like, I haven't had a specific value in a specific type or a specific format. So understanding the different types of variables or the different types of data types that you've actually got available inside of Python, it's going to make your life a billion times easier. Believe me, like if there's one piece of advice I can give you, it's make sure you understand this; it's going to serve you a long while. And make sure you understand CRUD: C-R-U-D, create, read, update, delete—super important. Okay, so that is our variable section and now done. Now we're up to our data type. So we've already gone and taken a look at three different data types: so we took a look at our string; we took a look at an integer; and we took a look at a float. So if we go and create a new variable now, so we're going to call this one test_variable, and what we're going to do is we're going to assign it a string. So we're going to create a first data type, and we're going to call this—so remember the way to write a comment inside of Python—I can't remember if I covered this in the last thing—but we can actually add additional comments inside of our code cells using the pound symbol again, so sort of similar to what we're doing up here, so right down there. So you can see I've added a pound, and if I actually type in a comment here, This is declaring a variable—make sure we can see it—variable as a string. Cool. So this here is not actually going to execute, so this is what is referred to as a comment inside of our code, and it is very good practice to write comments within your code because it's going to help you remember what on earth you've written. So often I've gone and written giant blocks of code, and then I'm going, man—specifically the next day—I'm going, I really wish I'd gone and written some comments because yesterday Nick really wasn't thinking or looking at for future Nick. So adding comments, really, really good practice. Okay, so we're going to call this test_variable, and we are going to assign it a string. So this is our first data type that we're going to take a look at: so string. So remember, a string is really just a bit of text. So when you're working in NLP, you'll find that you're working with lots of strings; sometimes you'll also have variables or feature columns within your data science projects that are stored as strings as well. So again, good to know this. So we are going to call this test_variable, and again, keeping with a space theme, so we're going to say it's Pluto—so again, the planet. So that is our variable now assigned. Now what we can do is we can print that out. So if I type print test_variable, you can see that we've gone and printed out Pluto. Another cool thing to note is that we can actually execute more than one line of code in one cell. So if I cut this by pressing Ctrl+X—so again, Ctrl+Z, Ctrl+Y, Ctrl+X, Ctrl+V—or work inside of our Jupyter Notebook. So if I paste that there, this is not only going to assign our string data type to our variable, it's also going to print it out at the same time. So if I go and run this, you can see that we've gone and printed it. Now I'm going to clean this up, so I'm going to delete this cell here, and remember to delete a cell in our Jupyter Notebook, we hit DD, and that deletes it. So let's take a look at what we did there. So I wrote test_variable, so this is creating a new variable, and then remember we have to assign a value to that variable for it to be valid. So I've written equals, and then inside of quotes I've written Pluto, and then I've closed those quotes, and then to print out our variable and see what it actually is, I've written print and then test_variable. Now remember, we can take a look at what our variable type is by typing in type and then passing through our variables. So if I pass through test_variable, you can see that that has now gone and printed out a string. Let's actually keep this clean, so this is going to be our string variable. Let's make it so—what I'm going to do is I'm going to rename my variable—well, it's not actually renaming; we're actually declaring a new variable now. So we're going to call it string_variable and then paste that in there and then paste that inside of our type as well. So that is our first variable now defined or our first data type now taken a look at. So we've now gone and declared a string. Now the next type of data type that we want to take a look at is an integer. So remember we did that one up here as well. So we are going to create a new variable called integer_variable. Remember, you just need to type it out, and let's add in a few additional cells. Again, pop quiz: how do we add more cells below? It's BB, and that gives us the ability to add a bunch of cells into our Jupyter Notebook. Now what we can do is—what are we doing?—we're assigning an integer variable. So we've gone and done our string; now we're going to go on ahead and do or create our integer variable. So again, good practice, add some comments. So This is declaring a variable as an integer, and an integer is basically a number that has no decimal places. So if I type in 123852 and then print it out, you can see that we've gone and created that integer there, and if I change my print function to type, you can see we've gone and created our integer variable. So that is integer variables in a nutshell. Now think of your integer variables as variables that you're going to use pretty much everywhere. So whenever you're doing math, you're more than likely to use these variables. So say, for example, I wanted to add 10 to this variable. So we're going to look at math functions a little bit more later on, but say, for example, I wanted to add some values to this integer variable here, well I can do that. So if I type in integer_variable—so remember we can access it like that—and if I wanted to add a value to it, I can just type in + 10, and you can see that we have now taken our placeholder variables—remember our integer variable—and we've gone and added 10 to it. So before it was 123852, and then by typing in + 10, we've now gone and incremented it, so it's now 123862. So that is integer variables in a nutshell. Now, so—so far we've taken a look at string variables, and then we've taken a look at integer variables. The next data type that we want to take a look at is a float. So remember we took a look at a float up here as well. Now the key difference between an integer and a float is going to be the fact that a float has a decimal place and has trailing values. So if I go and do this now, so we're going to create a float variable, and we are going to set that to 128128.123 or 126, and then if we take a look at our type, you can see that we've now gone and declared a float. So again, let's add some comments. So This is declaring a variable as a float, and there you go. So what we've gone and written is float_variable = 128.126, and then—so that's basically gone and created a new variable and assigned a float value to it, and then what we've also gone and done is we've gone and taken a look at our

Type so I've written type, and then I've passed through our float variable to that function, and you can see it's gone and returned float. So that is our third data type. Now there are a bunch of others that we're going to take a look at as well.

So say, for example, you wanted to do some conditions and take a look at something whether or not it's true or false. So this is going to be particularly important when we actually get to our conditions section of this course, so we can actually create a new variable called a binary variable. Now this is also referred to as a bool. So let's actually name it correctly: bool variable. And the reason it's called a bool variable is it stands for Boolean variable; so either one or zero, true or false. So let's actually write our commentary: so this is declaring a variable as a Boolean value; it's actually assigning a Boolean value to a variable. Cool, and what we're doing there. So we've gone and created our variable. Now what we need to do is assign it. So let's take a look at our different types of Boolean variables. So I can assign it as true, and you can see that as soon as I sign it as true, so right down here, it's gone to this green value. So this represents the fact that we're now working with a reserved word inside of Python. So true is a reserved word. So this just basically means that you can't go and name something this particular thing, or it's not good practice to. So if I go and take a look at our type and type in bool variable, you can see that it's gone and exported the fact that it is a Boolean value.

Now there are two key types of Boolean values; so it can either be true or false, and you can see that is also a Boolean value. Now if I type in um lowercase—let's actually pass through a string rather than actually passing through the reserved word—so if I type in true, and remember a string is something that's wrapped inside of our quote, so remember we've got these quotes here, so no difference where how we went and defined our string variable up here, which was Pluto. So if we went and did this, this is not going to return—or what do you think it's going to return? Is it going to return Boolean? No, it's going to return string. This is because it is not using the reserved word; it's actually passing through a string. So in order to get a true true value or Boolean value, we need to get rid of these quotes, and again now it's going to define our Boolean value.

Now you're probably wondering where you're going to use a Boolean value—where, really, really often when you're processing data. And again, we'll get to this when we take a look at our Pandas components of this course. Say, for example, you want to check whether or not a particular set of values is over, let's say, 50, you might go and say is, or you might write an if statement: so if x is greater than 50, then return true, and basically you're going to have the return value assigned as a Boolean value, so either true or false. Now sometimes this is also represented as a one or zero. So I'm going to show you a something which I like to take a look at as well. So if I type in—this is known as type casting—so I can actually convert a value to a specific data type. So you can see that I've written bool here, and if I pass through zero, you can see it's still returning Boolean as the type, and if we take a look at our variable, it's actually gone and converted our zero to the reserved word false. So this is actually converting a specific value into a different value. Now again, there's a whole bunch of these, so the one for string is str, and you can see that that's in green down there; the one for integers, so let's actually add these as well. So if we can actually—I'll give you a cheat sheet if you want these—so there's str, which is type casting to a string; there's inte or int for integers; there's float, and again you can see that these are all green; there's Boolean, which we've just taken a look at; and there's a bunch of others. So again, this allows us to convert a value into a different type of value. So if ever you need to convert one value or one value data type into a different type of data, using these type casting tools makes it a lot easier to do that as well. Cool, so that is our Boolean value now defined.

Now what's our next one that we're going to take a look at? So the next one is probably the one that I use the most in data science, and this is a data type that you're going to come across a ton, and we're going to cover this now at a high level, but we're going to take a step, and we're actually going to delve into it a whole heap more in a second. So what is this illustrious or elusive data type? Well, it's called a list, and a list is also known as an array in other programming languages, but in Python it's called a list. Now you're probably thinking, what on earth is a list? Well, it's a number of values and stored within a specific data type. So inside of a list, I can actually have integer values; I can have float values; I can have Boolean values. But just think of it as like a bit of a shopping list; so you can add a whole bunch of stuff to a Python list and actually return all of those values. So let's actually go and define one. So we're going to create a new variable—again, consistent theme, I know it's repetitive, but it's going to help it sink in—so if we type in list variable, we are going to assign it a list. So how do we actually create a list? The core defining characteristic when defining a list is that it's inside of square brackets. So you can see that there. So I've gone and defined—oh, we haven't added comments, what are we doing? Let's add a comment: so this is creating a list variable. So what are we doing? So we're creating our list variable down here, and right now it's an empty list. So say, for example, we wanted to add in a bunch of stuff to it, well we can add in a bunch of numbers. So I can type in one, two, three, and take a second to think about this: what values am I putting into this list right now? Give me some thinking music—terrible singing—so these are actually integers, right? So remember they're taking this format up here, right? But I can also add floats; I can add in other values: 85.4, 22.1. Cool. So now what I've gone and done is I've gone and created a list. If I go and hit Shift+Enter, that has now gone and created our list variable, and if we add in our type function and press type list variable, you can see that we have in fact created our list. So what have we actually gone and written there? So we've written list_variable equals, and then the defining characteristic of when you go and define a new list is the fact that you have to put it inside of square brackets, and this is really, really important because there's other really similar types or data types which use other types of brackets, but a completely different data type. So again, lists inside of Python wrapped inside of square brackets. Cool, so that is our list now created, and so we've created square brackets, and then we've added in a value, and we separate each value by a comma. So you can see that there: so I've added in a value, and this particular value is an integer, comma; so my next value is 2, comma; my next value is 3, comma; my next value is 123.44, comma; next value 85.4, comma; last value 22.1, and then close our square brackets. So that is a list variable, and if we take a look at it, so that is what our return value actually looks like; so that's what it looks like when we go and print it out. Now I'm not going to show you how to CRUD this because remember we need to—so what we've only really done so far is we've taken a look at how we can create and read these values; we're going to delve into a little bit more detail once we actually get to our list section and specifically our dictionary section in a second as well. So we'll take a look at how to update and delete those. So that is our list variable now created.

So quick recap: so we've taken a look at string values, and we assigned it Pluto; so key defining characteristic of a string is it's wrapped inside of quotes; we've declared an integer, and the key defining characteristic of an integer is that it's made up of numbers with no decimal places and no trailing values; we've defined a float, which is a inter or numeric value with trailing numbers; we've defined a Boolean, which is really true or false, and we also took a look at how we can do a bit of typecasting, and then we went and created a list, and the key defining characteristic of the list is that it is a number of different values wrapped inside of a set of square brackets. Cool. The next data type that we're going to take a look at is a tuple. So again, a tuple is very similar to a list, but with one key defining characteristic: you can't add in values or the exact same value multiple times. So this is the yes, so you kind of add in multiple values time. So let's actually go and define a tuple. So if I type in um tuple variable, and so what we're going to do—add some comments—hit create a—and to be honest, I don't use the tuple variable that much—every now and then, and there's a specific library that I've used that actually requires a tuple, but more often than not I'm using lists. Um, tuples you'll find are pretty common when you're working inside computer vision and you need to define coordinate values, so x and y and z, but more often than not you're probably going to mainly be using them for that use case; mainly you're probably mainly going to be using lists. The other most common data type that I use outside of the base ones, which are integer, floats, and strings, is a dictionary, which we'll come to in a second. So um, what's that comment? So create a tuple variable, and let's add in some more cells. So how do we add in more cells? We add bbb to add more cells below. Cool. Let's add two balloon caps. So if I type in tuple_variable and set that equal to—right, so what is the defining characteristic of how we actually go and define a tuple? Well, this is why I said pay a lot of attention to the type of brackets that you use. So these are square brackets out here; here we're going to use parentheses or curved brackets, right? So parentheses, and then we're going to add in values. So I'm going to copy this over and paste this into our tuple. So you'll see that it's defined in a very similar manner; the only difference is the type of brackets that we use. So for a list, we use square brackets; for tuple, we use curve brackets or parentheses. And if we go and type in type and tuple variable, you can see that we've now gone and created a tuple variable. So if we go and type in tuple variable, that is our tuple variable. Now I said there is something which makes a tuple variable different to a list. So let's actually quickly take a look at how we can get values out of a list. So again, you'll see that this is similar when we access values from a tuple. So say, for example, I wanted to grab this first value out of my list, and this is going to be really common, right? So you'll put a bunch of stuff in a list, and you'll loop through each one of these values. So we're going to cover loops later on, so you loop through the values, and you'll apply some logic, or you'll do a check; again, you want to be able to access each one of these values. So if I type in—adding a set of square brackets after my list variable and pass through the value 0—this is going to give me the first value in my list. So again, this is referred to as indexing; so I'm passing through the zeroth index to this list, and I'm getting the first value back. So remember our values were 1, 2, 3, 123.44, 85.4, 22.1. If I go and pass through 1, I'm going to get the second value, which is 2. If I go and pass through 2, I'm going to get the third value, which is 3. And if I go and pass through—so 0, 1, 2, 3, 4, 5—if I go and pass through 5, I'll get 22.1. If I do that, 22.1. Now the reason why I sort of jumped the gun a little bit for this is because it's the same way that you go and tuples operate in the same manner. So if I wanted to get the first value out of my tuple, I can do that, and you can see I've now gone and grabbed the first value. If I go and pass through 1, I've gone and grabbed the second value; go and pass through the index 2, I'm gonna grab the third value. A core difference between tuples and lists is that you can't have multiple values of the same value inside of a tuple. So if I go and define this—actually, is that right, people? Let's uh—no, the core defining characteristic of a tuple is the fact that it's immutable. So let's take a look at this: so immutable—just checking my notes there—so when we are working with lists and tuples, the—so I could go and update this value inside of this list up here, right? So say, for example, I wanted to change the fifth value or the sixth value inside of this list to a different number. So say, for example, I wanted—and again I'm sort of blanking on these because again I don't use tuples and I don't use um I don't use sets which we'll take a look in a second that much. So again you'll probably find that you don't use these types or these data types all that much. So if I wanted to go and update a value inside of our list to a different number, right? So right now it's 22.1; that's our last value. If I went and typed in a new number—so remember this is a variable assignment; so we're now going and passing through a different number, and we're going to cover this in more detail in a second—so if I go and assign it to 123, and we go and take a look at our list now, list variable, you can see that we've now gone and changed that number. So before we assigned it, it was 22.1; when we went and did this—so I wrote list_variable, and then inside of square brackets I've passed through the index 5, and then I've set it equal to 123. So this is updating our list with a new number, and you can see that when we go and output it, we're going and outputting that new value there. Core thing with a tuple is that it is immutable, so that means you can't change values inside of that tuple once you've defined it. So if I were to try to go and do this same thing here to our tuple, so if I go and grab index 5 and try to set it to 123, we should get an error, and you can see that there: so it says tuple object does not support item assignment. So this basically means that you can't go and update values inside of your tuple once you've gone and declared it up here, and that is a core defining characteristic of tuples that they are immutable. So remember that: immutable. Cool.

All right, the next thing that we're going to take a look at is the set. So actually let's quickly recap on tuples: tuple variables are defined—so we've defined our tuple variables: tuple_variable equals, and the defining characteristic of a tuple is that it's defined using parentheses, and the main difference between a list and a tuple is the fact that a tuple is immutable, which means you can't change values—let's make that in simple terms: you can't change the values—cool. Okay, now the—and then—so we also went and checked the data type, and remember we went and tried to go and update our tuple, and that's going to throw an error because we can't go and update the values because they are immutable. So you can go and update the values. Core thing: if you wanted to go and update the values in your tuple, what you would need to do is convert it to a list first, make your updates, and then convert it back to a tuple, but this is sort of like recreating it, right? So again, just keep in mind when you've got a tuple, you can't go and update the values in that. The next type of data or the next data type that we're going to take a look at is the set. So again, this is one that I don't use all that often; um, it is used sometimes, and sometimes I use it because it actually has a really unique function, and let's actually take a look at this. So we're going to create a set. So let's add in our comments—I'm just going to overwrite what we had in that cell—so this is creating a set variable. So let's go and do it. So we are going to create a new variable called set_variable, and we're going to set that equal to—all right, so again, this is why I said the types of brackets are really, really important; I wish I'd known this when I was learning Python because I was like, why can't I update this value? And it's like, oh, it's using curve brackets rather than square brackets; that's why I can't do it. A set uses another different type of brackets—go figure—this actually uses curly braces. So again, we pass through curly braces, and then we can pass through the same values up here, and that is our set created. Now you're probably thinking, Nick, why the hell are you teaching me sets? I'm going to tell you in a second. So if we go and check—take a look at our type set variable—cool. So you can see there that it is in fact a set. All righty, and let's just close Slack. All right, so we've now gone and created a set. So what was I going to tell you about sets? Oh, okay, so the unique property about them. So let's take a look at sets, and let's try and update a set value. So if I go and try to update the fifth value like we did with our list, again you can see that sets are also immutable. All right, so immutable—you can't change the values—but they also have another property because you're probably thinking now, all right, Nick, you've taught me about sets, you've told me about tuples, but like a set right now just looks pretty much the same as a tuple, but it's inside a curly bracket. So well, sets have another property that you want to take note of as well. So sets can't contain values of the same value; so they can't contain duplicates basically. So say, for example, we go and add in another value to our set. So what I'm actually doing here is I'm not updating the set; I'm creating the variable all over. So this is why I'm allowed to reassign it up here, right? So this is not reassigning; it's creating it all over again. So if I go and pass through a new value—so let's go—what do we have in there already? One, two, three—so let's go and add a three. Cool, all good, looks fine, right? No errors. Now if we go and take a look at our set, you can see that it only has these—what is it? Six values—so one, two, three, four, five, six—it doesn't have that trailing three, and that is because you can't store duplicates, right? So if we go and try to do that with our tuple, and if we go and output a tuple—oh, what am I doing?—tuple variable, you can see that we do in fact have that trailing three on there. Our set is different from our tuple in the fact that again it's still immutable; that's a similar characteristic that it's got, but a core thing is that it doesn't contain duplicates. Now this actually comes in handy, right? So say, for example, you got a big list of values, and you want to strip out all the duplicates, having it set as a set can be useful sometimes, right? So say, for example, we've got our tuple variable.

Up here, so we've gone and defined it as a tuple, but we're like, well, let's let's actually do it with our list. So let's go and add in some duplicates: so one, two, three, one, two, three. Right, so right now a list has a ton of duplicates, right? So it's got one, two, three, one, two, three, .44, 85, .4, 22, .1, 1, 2, 3, 1, 2, 3. Now, say for example, and again this is what I actually use sets for in real life, so I don't like I'm sort of teaching you why I use these particular data types, so why on earth do we use that?

Say, for example, I taught you a little bit about typecasting up here. So say, for example, I wanted to get rid of all these duplicates inside of our list. Now with NumPy, you've got other functions, and we're going to cover that later, but what I can actually do is I can typecast this and actually convert it to a set, get rid of the duplicates, and then convert it back to a list. So what we're going to do is do this. Let's add a comment: the typecasting list. And I think I've actually got some more—yeah, I've got some more stuff planned on typecasting, but I wanted to give you a little bit of background as to why we go and do this. So what are we going to do? We're going to typecast our list to a set and then back to a list to get rid of duplicates.

So what we can actually do now is we can actually do this. So let's grab our tuples. Remember our triple variable is up here, right? So we've got our tuple, which is—no, what are we doing? We're doing it on our list because remember our list has got duplicates: list. And what we want to do is get rid of all these duplicates. So what we're going to do is we're going to typecast it to a set. And so what I've written there is—again, you can see that in green there—so I've written `set`, and remember it's in green because it's a reserved function, and then we've gone and passed it into this function over here. So we've gone and passed through our `list` variable, right? And so if I go and output this, you can see that it converts it into a set, which now has curly braces. But say, for example, we wanted to do more stuff with this, we want to convert it back to a list, so we can actually wrap it in another typecasting function. Again, these aren't actually called typecasting functions, but that's what I actually use them for. So they're actually used to set variables of this specific type. So if we go and go and wrap this inside of a list function over here, which you can see that there, so we've now gone and taken a list variable, we've wrapped it inside of a set, and we've wrapped it inside of a list function. So if we go and output this, that effectively takes this list variable that we had up here and goes and strips out all of the duplicate values and still returns a list. So if we take a look at our type now—still a list—how cool is that?

So that's what I tend to use sets for most often. So again, you're probably thinking about all of these data types; you're wondering why are we using all of these, or why are you teaching me all of these? These are the main reasons: tuples I probably don't use too much, but mainly in computer vision; uh, sets mainly for removing duplicates more often than not. Okay, so we've gotten covered a bunch of stuff now. So we've taken a look at strings, and we defined that as Pluto. So remember strings are wrapped inside of quotes. Gone and defined an integer variable; so the defining characteristic there is it is a number with no trailing decimal places. Gone and defined a float value: so 128.126. So remember it's got a decimal point, and floating values. Gonna define a boolean value: so `True` or `False`, with a capital. Gone and defined a list; so the defining characteristic there is having a set of square brackets, or wrapping everything inside of a set of square brackets. We've then gone and defined a tuple, and again the defining characteristic of a tuple is that you're using parentheses or curved brackets. And a key difference between a tuple and a list is the fact that a tuple is immutable, so you can't go and change the values once it has been declared. We've also taken a look at a set, and again a key defining characteristic of a set is that it's defined by squiggly brackets, or whatever you want to call them, and then again they're immutable, but they also cannot hold duplicates; they'll get rid of the duplicates if you do pass them through.

We have one last big one that I want to show you. So this last type or data type is actually called a dictionary. So let's actually go and define a dictionary. So we are going to create a new dictionary called `dict_variable` and add in a comment: defining a dictionary variable. And let's go and do this. So let's actually define it first, and then I'll explain it to you. So I'm going to set it equal to squiggly brackets—you're probably thinking this is a set—no, it's not. So it's going to be a dictionary, and then we're going to define a key, and I'm going to set this equal to `name`, and then `Nicholas`, and then `age`—actually, let's type in `favorite_color`—let's actually put these in the same type of quotes as well—is `red`, and to do a `favorite_number`, 20. That is our `dictionary_variable` now defined. Now you're probably thinking, Nick, what the hell have you gone and showed me? So a dictionary variable is made up of two key defining characteristics: these are keys and values. So let's actually take a look at what we went and wrote. Well, let's actually take a look; check our type. So if I type in `type(dict_variable)`, you can see it is in fact a dictionary. We're going to print it out; again, it's printed out our dictionary, but you can see here that this is sort of taking a little bit of a weird shape. So we've got these squiggly brackets, and then we've got these values followed by a colon followed by a value, or another string here; this one's got a number. You're probably thinking, what on earth is this? So dictionaries are used really, really often inside of data science. So again, the main debt type of data that I use is a list. When working with different data types, you're obviously going to be using strings, booleans, floats, and integers really, really often. The main sort of grouping data type that I tend to use is a list and a dictionary. Now a dictionary allows you to assign or create keys and assign values to those. So say, for example, I wanted to create an object which represents a person, I might actually create something which looks a little bit like this. So I might actually rename this `person`, and then let's go and create—take a look at our `dictionary_variable` or `person_variable`, and you can see there that I've now actually gone and output that out. Now this dictionary is made up of two key things: it's made up of keys, and it's made up of keys and values. So I've gone and defined `name`, which in this case is a key, and then by—in order to set a value to a specific key, we go and pass through a colon, and then we go and pass through a value. So this is one key pair—that is often what it's referred to—so it's got a key, and it's got a value, and the way that we set it is we pass through the key, we set it equal to, or we use a colon to actually set that value, and then we separate them using a comma. So then I've gone and set another key, and in this case I've set it equal to `favorite_color`, and then in order to set that value, I've equaled—I've set it equal using a colon, and I've set it equal to `red`. Then our last key is `favorite_number`, and again this is inside of quotes, and in order to set our favorite number, I've used a colon, and I've passed through the value 20.

Now you're probably thinking, all right, this is great, but how do I actually use this? So say, for example, I wanted to now go in ahead and grab the name of my person inside of my `dictionary_variable`. Well, remember when we use lists, we go and pass through the index value, right? So we go and pass through 0, 1, 2, 3, whatever. With dictionaries, we go and pass through the key value. So in this case, say, for example, I wanted to grab the person's name, I can actually pass through the key `name`, and that goes and returns the dictionary key's value. If I wanted to return the favorite color, I can pass through `favorite_color`, and that returns `red`. If I wanted to go and pass your favorite number, I can now return the value 20. So again, that is the last data type that I really wanted to focus on in this particular case. Now we're going to build up on all of this, but this sort of really establishes a core baseline for the different data types you're likely to encounter when you're going on your data science journey. So let's take a look at how we actually define that. So written `person_variable`, set it equal to curly braces, and then we've gone in to find a different key and value pairs, and the way to set a value is you use a colon, and you separate each key-value pair by a comma. Cool. That is all of our different data types now done. So these are the core ones that I actually wanted to show you. So we've gone through, and again I know I'm repeating this, but it's going to help it sink in: gone through strings, and again the defining characteristic of a string is we use quotes. Gonna define an integer, and the defining characteristic of an integer is it is a number with no trailing decimal places. Gone in to find a float variable, which is a number with trailing decimal places. Gonna define a boolean. We've gone and defined a list, and again a list has square brackets. Gonna define a tuple, and again a tuple is very much like a list, but you use parentheses, and it's immutable. Gone and defined a set; again, really similar to a tuple, but a set is immutable, and it also does not hold duplicates. And then last but not least, we went and defined a dictionary, which allows us to store key-value pairs, and again the way to define it is again similar to a set, but rather than passing through straight values, we go and pass through key-value pairs, and to set key-value pairs, we pass through a key, which in this case, for example, we pass through `name`, and we set it using a colon, and then we pass through the value. So again, I use dictionaries all the time; it is something that I use quite a fair bit. Um, so one practical example is when I go and train machine learning models; what I actually do is I'll normally create a dictionary called `fit_models`, and I'll create a blank dictionary like that, and as I go and train a machine learning model, I'll actually store it inside of that dictionary, so I can actually come back to it.

Okay, we've gone through a ton of stuff now. I'm going to take a bit of a break, and let's actually take a look at any questions that y'all have. Just saw a question there: so can we can uh convert a list to a tuple? Yes, you can. So if I type through `tuple`, wait, `list_to_tuple`—yes, so `tuple`. So remember we can typecast. So I'm going to pass through a `list` this variable, and you can see that I've gone and done that. Okay, so what are we doing now? So we are going to delve into lists in a whole bunch more detail. So so far we've gone through our different data types; now it's time to go through our lists. So we are going to create a new section, and remember how do we go and add new cells? We add in `bbb`, and this adds up new cells below. So we're going to create a new section, and remember to convert it to a different type of cell. So in this case we've got a code cell; we want to convert this one to markdown. So this first up we need to step out of it, so we're going to hit Escape, and it goes to blue; going to hit `m` on our keyboard, and it goes to markdown, and we are going to call this section—what number are we up to so far? One. We're going to call this section, sort of anti-climactic, that: Lists.

All right, so we've gone through this in a bunch of detail, but I want to delve into it in a lot more details. Remember we want to take a look at CRUD: so how to create them; we want to take a look at how to read them, and we've already done a little bit of this, but we're going to do it in a little bit more detail; we want to take a look at how to update them, and we also want to take a look at how to delete them, right? So if you remember this framework for when you're learning to code, when you're picking stuff up, going to make your life a ton easier. So remember CRUD: whenever you're learning anything, just think CRUD: how do I create something in this framework? How do I read something in this framework? How do I update it? And how do I delete it? This is how I learned Pandas; this is how I learned NumPy; this is how—when—pick up new programming languages quite quick. So sometimes you'll hear people talk about, hey, if you learn one programming language first, and then you go and learn others, it's going to be way faster. This is because you start to build up frameworks like this, and you learn how to pick up nuances in programming languages. This is one of the nuances that I've sort of picked up from all of my time learning programming. All right, cool. So we're going to create a list, and remember we're going to create a new variable. So we're going to create a list of names this time. So if I type in `names`, and remember how do we create a list? We pass through square brackets, and again in keeping with our space theme, we sort of lost track a little bit up there, we're going to pass through a list of famous astronauts. So first one's going to be Neil Armstrong; second one is going to be Buzz Aldrin. And so key thing to note here is that the list can be made up of pretty much any data type, right? So like this time we're passing through strings; before we had numbers, or we had integers and floats; we can actually pass you a bunch of different types of stuff. Another person says Sally Ride, Yuri Gagarin—did I spell that right? G-A-G-A—nope—yeah, Garen, that's better. Yes, and then um, possibly a future uh astronaut, we're gonna pass through Elon Musk. Well, he's—he's making some achievements. That is us creating a list, right? So we went and created a variable, and then we assigned it, and we've specifically gone and assigned it a list. So we've gone and used our square brackets, and will pass through a number of values separated by commas. Then say, for example, we wanted to go and read it, we can first up type in `names`, and that allows us to read the whole list, but we can also read a specific value in that list. So if I type in `names` and grab the first value, that's going to give me the first value in the list, which in this case you can see here is Neil Armstrong. Now I'm going to give you a bit of a trick. So say, for example, you wanted to read the last value in your list. So rather than—and remember this is called indexing—let's just reading—reading using indexing. What was I going to teach you? How to get the last one? Um, so say, for example, I wanted to grab Elon Musk. So right, go from the other way around. So I can type in `names` and pass through negative one, and this is going to give me the last value in that particular list. So rather than going through and going, hey, how long is this list? So is it one, zero, one, two, three, four? So grab value four. I can actually go and pass through negative one and actually grab that value. Now say, for example, I wanted to take a look at how long a particular list is, I can again use another built-in Python function and type in `len`, and then pass through my list, and that tells me that I've got five different values in that specific list. Now the next thing that we want to go and do is take a look at how we can update values in our list. So remember we went and replaced the value already. So say, for example, we wanted to replace—well, let's actually go and—what we're going to do now—let's actually go and update a value first. So we are going to change Elon Musk's name to Elon Muskie, right? So we can type in `names`, and then we can grab the last value, so negative one—let's not type Elon Musky, that's unfair—um, and let's say, for example, I'm gonna become an astronaut, so I can type in `Nicholas`. We're not—and then we're to output `names`, and take a look at our updated array or list. So you can see there that we've now got Neil Armstrong, Buzz Aldrin, Sally Ride, Yuri Gagarin, and Nicholas. From—maybe I'll become an astronaut in the future. So what we've actually gone and done there is we've gone and updated a value in our list. So remember keep in mind your CRUD framework: so create, read, and then update, and then delete, which we're going to get to in a second. So that's updating a value. Now say, for example, I wanted to add a value to our list. Well, we can do that as well. So in order to add a value to our list, and specifically add it to the end—so say, for example, we've been unfair to Elon, we want to add him back to our list—we can type in `names.append`, and then pass through the value that we want to add. So we're going to append `Elon Musk`, and if we go and take a look at our array now, you can see that we've gone and added Elon back to our list. So remember now we've got Nia, we've got Buzz, we've got Sally, we've got Yuri, we've got me, and we have Elon as well. Pretty cool, right? So we've now gone and covered how we can create, how we can read, and how we can update our list. Say, for example, we wanted to add a particular name at a specific point within our list. So right, so rather than just adding to the end—say, for example, we wanted to add a name somewhere in the middle, right?—let's actually add it right at the start. And I know this is kind of controversial, the FAA changed the rules as to who was the first uh billionaire astronaut into space, but space-themed names list. So we can type in `names.insert`, and this will allow us to insert a new value into our list. Now you're probably thinking, what is this dot, and then you're passing—writing this value here? This is what's known as a method, and it basically is—or what it is—is that certain data types have certain functions that are actually attached to that class of data type. So in this particular case, the `insert` method is a method which is attached to lists, so that's why we're able to use `insert` to insert a value specific to our list. So if I actually type in `names`, let's actually get rid of this dot, and then hit Tab on my keyboard, you can see that all of the different types of built-in methods that apply to our specific list. Let me do that again. So I can use `append`, I can use `clear`, I can use `copy`, `count`, `extend`, `index`, `insert`, `pop`, `remove`, `reverse`, and `sort`. So these are the built-in methods that I've got available within our list. I'm not going to go through all of them today because I don't want to take this to a three-hour stream, but there's a bunch of different methods that are available within your list. Now you've also got different properties that are available—or some of those might be properties as well; some of them are likely to be methods—but we're going to cover that in more detail once we get to object-oriented programming with Python. So what we're doing: we're going to insert a value into our list, and we're going to insert it at the start. So the first value that we need it passed through is where we want

it and remember, if you get stuck, you can get rid of the braces and pass through ?? and it tells you down the bottom, or it gives you a little bit of documentation. So you can see here that it says the index that you want to insert your object at and the object that you want to pass through. In this case, our object is going to be a string. We can close that. So we want to insert a value at the start, so we want to imposition zero, and the name we're going to add is let's add Richard, so Richard Branson. And now, if we take a look at our names, you can see that we've gone and added Richard right up at the start. So that, in a nutshell, is how we can actually go about updating our different lists.

So again, keep in mind the CRUD framework is going to make your life a ton easier when it comes to learning about different data types and different programming concepts. So we've taken a look at how we can create a list, how we can read it, and specifically how we can read a specific value using indexing. How we can take a look at the length of our specific array, how we can make updates to it. So remember, we can update a specific value, we can append to the end of it, and we can insert into a specific position as well. So remember, this first value that we pass through is where we want that new value to be inserted into our array. So we can add it into different, uh, into different positions as well. Last thing that I need to teach you is how to go on ahead and delete values. So say, for example, we're not happy with this guy here, why the hell do we have Nicholas there? So if we take a look, let's actually find out where that position is. So names, um, I think it's minus two, yep, cool. What we can actually go ahead and do is delete that out of our list. Now, this one's relatively easy, so all we need to do is type in del and pass through the value that we want to delete out of our list. And if we now go and take a look at our list, you can see we've gone and deleted the value out. So in order to do that, I've written del names and then passed through the index that I want to delete, and that's gone and removed it from our list.

So that, in a nutshell, is everything, or pretty much the crux of what you should know about lists. So we've taken a look at how we can create one, how we can read one, how we can update it, and how we can delete values, and that is lists done. Alrighty, cool. Back to it, we are now up to our last phase. So what did we say we were going to copy or cover? So we are going to cover variables, data types, lists, and last one is dictionary. So let's go back to our dictionaries. So again, what we're going to do, so I've just stepped into that cell, so how do we get out of it? We hit escape to get out of it, we hit m on our keyboard to go to markdown, and we're going to create a new section, and what are we going to call this section, uh, so we are up to two lists, so we're going to call this three dictionaries. And again, we're gonna do the same thing, we're gonna do CRUD: create, update, uh, I'm forgetting, why did I say update? Create, read, update, and then delete. Okay, so the dictionary that we are going to create is a dictionary for an astronaut. So let's go ahead and do this. So we're going to call it astronaut, and and again you can do this over multiple lines as well. So in this particular case, I can add in a space, and sometimes it is whitespace formatting, so best of not to do it, but in this case it should work fine. So I'm going to set our first key, so our first key is going to be name, so we're going to store our astronaut's name, we're going to store their suit size, and then what's another key that's probably useful, right? Whether or not maybe they have allergies, because that's going to be really, really important when they're in space, because you want to have the medication that they're going to need. We're going to say allergies. Cool. So the first thing that we need to set is our name, and so keep in mind, remember when we create a dictionary, we use squiggly brackets, we then define a key, and the way we set a value to that key is with the colon. So I'm going to set that value, and we're going to sell it to Elon Musk. Then add a comma, we're going to set the suit size, and we're going to set that to large, comma, and then we're going to set whether or not they've got any allergies. So in this case, let's say peanuts. I don't know if that's true for Elon Musk, I'm just speculating. Cool. So that's our dictionary created. So what we've gone and written is astronaut, so that's our variable name, and then we've gone and assigned it a dictionary, and our dictionary has three different key-value pairs: name, suit size, and allergies. And remember to set our dictionary, we use the colon to set the value for a specific key, and we separate them using commas.

Now, in order to read them, remember, rather than passing through our standard indexing with a number, we can pass through the key name. So let's add in a couple of cells. Remember how do we add cells? Double a on our keyboard to add cells above, and if we want to add cells below, remember it's bb or b, right? So we're going to add in a couple. Okay, so we're going to read our dictionary. So I can type in astronaut, and that gives us our whole dictionary. Let's add some more cells, and say, for example, I wanted to grab name, astronaut [Music], then what I can do is pass through the key that I want and write name, and that is going to give me Elon Musk's name. If I wanted to grab his suit size, right, I can pass through that specific index, so suit size, and that gives me his suit size. Now, I can actually store this value inside of another variable. So say, for example, I wanted to store his suit size, I could type in suit size equals, and now I can go back and grab Elon's suit size using that specific variable. But again, that's just sort of something that you might use in your workflow. So again, for now, we know that our astronaut suit size is large. Now, what the other thing that I also wanted to show you is how you can, we're going to cover this more in loops, but I wanted to show it to you now as well. So say, for example, you wanted to get all of the different keys that are stored within a dictionary. So if I type in astronaut.keys, this is going to give me all of my different keys that I've got here. So it's returning name, suit size, allergies. Remember, we can access them similarly, similar to the way in which we access the values in a list. Oh, it's not actually subscriptable. So to convert that, so what we need to do is type in list, and then we can grab our keys, right? So remember, this is a built-in method that is available because we are using a dictionary. So remember, we took a look at, uh, which one was I showing you? So append and insert up here for lists, for dictionaries, we've got keys as a core method that you want to learn as well. Now, if I want to get all the values, it just so happens I can type in .values, and this gives me all of the different values that I've got stored within that specific dictionary.

Now, say, for example, I wanted to go and make updates to a key within our dictionary, we can do that as well. So we can go and let's first up update an existing value. So we are going to update our allergies to none. So I can type in astronaut, and then we're going to grab a specific key that we want, so we want allergies, right? So it's peanuts right now, and what we can do is we can go and set that to, um, I don't know what's, uh, favorite fruit, just a mango, right? And then if we go and take a look at our dictionary now, astronaut, you can see that it's now gone and updated that key to the value mango, right? So that is how we go and update a specific value in a dictionary. Now, let's say that we wanted to go in add in a new key, right? So we've gone and updated an existing value for an existing key, but say, for example, we wanted to go and add in a completely new key to our astronaut, um, and say, for example, uh, I don't know, somebody goes and creates a schedule and they're like, hey, our specific astronaut is going to be flying on the ship called Galactic One, so we want to store these values within our dictionary, right? So we want to store that somewhere. So what we can do is we can actually create a new key-value pair. So if I type in astronaut, did I spell that right? Astronaut, yup, okay, and then we're going to create a new key this time, and we're going to set this equal to space_ship, not spaceship, and we're going to set it equal to Galactic One, right? And if we go and take a look at our dictionary now, so astronaut, you can see that we have in fact gone and added a new key-value pair. So we've gone and written space_ship, then we've gone and added a colon, and we've gone and set that equal to Galactic One. So that, in a nutshell, is how to create, read, and update dictionaries. So we've gone and created one, and remember we need to pass through squiggly brackets, define our key-value pairs, so we set a key using the colon, and then we separate them using commas. We go and read them, so we can print out the dictionary by default, we can go and access a specific key by passing through the key name to our dictionary, and we can take a look at all the keys using a built-in method called keys, we can call or look at all the values as well using a built-in method called values, and we access those using dot and then pass through the method that we want to use. We can go and update a single value, and we can go and add in a new key pair as well. So remember, in order to add in a new key pair, I just access the dictionary, pass through the new name of the key that we want to set, and then I've gone and set it to a specific value. Now, the last thing that we want to take a look at in dictionaries is how to go about deleting a value. So if I type in del, again, again, really similar to how we do it with dictionaries, say, for example, I don't know, the master scheduler comes through and they're like, hey, Elon's not going to be on Galactic One anymore, the um, spaceship is now going to be the spaceship that he goes on, so but for now we're going to delete the spaceship, we don't actually know what, what space shuttle is going to be assigned to, we can go and remove that specific key pair. So if I type in del and let's delete the specific key pair, so astronaut, and then we go and pass through space_ship, and then if we go and take a look at our dictionary again, you can see we've gone and deleted our spaceship out of our dictionary. So now we've just got name, suit size, and allergy. So we've gone and added one up here, we went and deleted it using the del method or built-in Python function. Cool. So that is that done. So let's quickly review dictionaries: so taking a look at how we can create one, how we can read one, how we can update one, and last but not least, how we can delete one. And all in all, we've gone through a bunch of stuff. So first up, we took a look at how we can define variables; we then took a look at all of our different data types that you're likely to encounter in your journey to working or operating inside of data science; we then also took a look at how we can create, update, and delete, create, read, update, and delete lists; and we also did the same with dictionaries down here. And on that note, that about wraps it up. So we've gone through a ton of stuff. So again, thanks so much for tuning in, guys. Hopefully you enjoyed this video. If you did, be sure to give me a thumbs up, hit subscribe, and tick that bell. And again, if you've got any questions at all, do hit me up in the comments below, join the Discord, hit me up on Twitter. I'm sort of a lot more active on there now, but again, thanks again for tuning in. Peace.

What's happening, guys? Welcome to the next part in the installment on Python for Data Science. I'm so glad you can all be here. So my name is Nicholas Renate, and in this live stream, we are going to be going through a couple of key things, but we're going to be building up from what we've gone and done previously. So let's actually go and take a look as to what we're going to be going through in this particular stream. So namely, we're going to be covering two key things, but we're going to be building up from all the variables, data types, and different, uh, components that we've already built on, on already. So specifically, we're going to be taking a look at conditions and loops in this video. So without blabbing on and going through too many slides, let's get straight to it, and let's get the coding. So let's do this. Okay, so what we're going to go on ahead and do is create or open up our existing Jupyter Notebook. So remember, we had our previous Jupyter Notebook that we set up in the previous video where we went through all of our data types and variables. So what we're going to do is start up a new Jupyter Notebook, and we're going to go on ahead and add on to that. So I'm going to go to our command prompt, and as per usual, we're going to navigate into the same place where our Jupyter Notebook currently is. So in this particular case, the Jupyter Notebook that we've been working with so far is inside of a folder, let me just bring this over to this side, is inside of a folder, and this is just where I've got it, you could have it in other places if you wanted to, perfectly fine to do that. So we, it's inside of a folder called python basics, and you can see I've got a Jupyter Notebook there called pythonbasics.ipynb. So that is going to be the main Jupyter Notebook that we're going to be working through, at least for the basics segment of this series. So what we need to do is navigate into that folder. So we can do that, so I'm just going to go cd into, actually we need to go into our D drive, and then we are going to go into the youtube folder, and then python basics. So cd youtube, cd python basics. Now I'm going into this folder, but again, you could have your Jupyter Notebook stored wherever you'd like, and it's perfectly fine to open it up from there. This is just because I've got it, it's stored inside of this drive, inside of this folder, inside of this folder. This is the reason why I need to navigate to help. So what we're going to then do is start up Jupyter Notebook, and how do we do that? We type in Jupyter Notebook, and that is going to open up a Jupyter Notebook. It's just opened up on my other screen, and you can see I've got my python basics file there. So this is pythonbasics.ipynb. So we're going to open it up, and this is all the stuff that we did in the previous part of the data science series. So we've gone through variables, we've gone through data types, and we have also gone through lists as well, and I believe we've gone through dictionaries. So we've gone through four sections so far. So all of this good stuff. Now, again, we're going to be building on, on top of this. So I'm going to create a new cell, and how do we create a new cell? Again, we just type in, particularly a new cell below our current cell, we just hit b, and that's going to give us some new cells. Now, if we want to smash through and create a bunch of new cells, we just hit bbb a bunch of times. And then, in order to get started for this particular part of the tutorial, what we're going to do is we're going to again keep our structure, because we want to have structure in our code. So remember, in order to create a, what we're going to do is we're going to create a markdown cell, and to create a markdown cell, remember we hit escape on our keyboard to convert it from green to blue. So this means that we are not currently active in that cell, and then we can convert it from a markdown cell or from a code cell to a markdown cell, we just hit m on our keyboard. So you can see it's gone to markdown over there rather than being code. We want to convert it back to code, we just hit y, and that's code. So markdown, code, markdown, code, micromode. All right, cool. We want it to be markdown. So the next part that we are going to go through is conditions and logic. So this is really, really important. So we are going to call it, and remember to actually add a heading, we're going to add in a pound sign, and then I'm going to name it what are we up to here? So we dictionaries was part three, so this is going to be part four, we're going to call it Conditions and Logic. Alrighty. Now, let me give you a little bit of background as to where you're going to use this. So when you go through a large amount of pre-processing, or when you go through, um, pretty much the majority of data science processes, you're going to want to perform checks. So say, for example, I'm going and building up an object detector, right, and I'm going to build an object detector to detect my face. Say, for example, I detect that I've got a face in my frame using my camera, I might want to do something with that video. So how do we actually go about doing that? Well, what we'd actually do is we'd use some conditions and logic to be able to detect that. So what we're basically going to say is if we have a face within our frame, then we go and do something. This is exactly how we go and apply conditions and logic inside of Python. Now, I've sort of given it away a little bit in that the if statement is probably one of the most important statements when it comes to conditions. So let's actually add in a comment. So we are going to use the if statement. So applying, so remember to apply a comment, we can, we have to be inside of a code cell, and we add in a pound symbol again, and then we can type whatever we want. So we are going to say, um, applying logic using the if statement. Cool. So what we're going to do is we're going to apply a little bit of, let's actually do a test. Remember, we're going through a space theme, so we are going to check whether a user's name is a particular thing, and if it is a particular thing, then we're going to print it out. So specifically, we are going to set up a statement. Let's say, let's say Neil Armstrong walks in the room, right, so and we've passed through a variable called name, and in this particular case, we're going to set name equal to Neil Armstrong, and if it's equal to Neil Armstrong, we're going to print something out. We might say, hey, what's up, Neil? So first up, what we need to do is create a variable. So we're going to say name, and this is going to be a placeholder to hold our data type, which in this case is going to be a string. So we are going to assign it a value. So I'm going to say equals, and then in order to create a string, remember the defining characteristic for a string is that it is inside of quotes, and we're going to set our name to Neil Armstrong, right? And then remember, we need to do this check. How are we going to do this check, right? So we want to apply some logic and go, hey, if the name or if the person walking into the room is

Neil, then we want to go and do something right. We want to print out his name and say what's up Neil. So we're going to say "if," and this is where the if statement comes in. So we're going to use our variable, so if name equals equals Neil Armstrong, and then colon, and then do something, all right.

Before we go on ahead, let's actually take a look at what we wrote there. So what we've gone and done—let me zoom in on this—is we've gone and created an if statement. So this over here is a reserved word; so you can see it's in green. So this if statement is a core component of applying logic.

Now, if we wanted to go and take a look at what the if statement does, we can type in "if ??" It doesn't look like it works for the if statement. Let's actually type it in: so if statement Python. So you can see this is what is a w3school. So again, so this is a Python condition; so Python if else. We're going to get to "else" in a second, but "if" is our main condition, right.

So what we've done is we've been: if name equals equals Neil Armstrong, and again this is a string, and then colon. So this colon is really, really important in Python. If I don't have a colon, it's going to throw an error. Now you're probably thinking, "Hey Nick, a single equals doesn't do a check, but a double equals does." So this is a key thing: if we use one equal sign, this is assigning a value to a variable; if we do double equals, this is actually a logical check. So um, this is also referred to as a logical operator. So if you type in "Python logical operators," you can see that there are a whole bunch over here. So we can see that plus equals, equals equals, a whole bunch of different checks. So this is referred to as a logical operator, but just think about it as saying, "Hey, if name equals equals Armstrong, then we're going to do something," right.

So there's a whole bunch of different uh logical operators as well. So I believe if we want to check whether or not it's not equal, whereas the not version—not is exclamation mark equals—so we'll take a look at that as well. All right. So if name equals equals Neil Armstrong, then we want to do something. So we're going to print "What's up Neil." So if we go and run this now, ideally what we should see is that—am I spelling Neil right? Yeah, I am—if we go and print this out, we should see that it goes and prints out "Sup Neil." So if we run it—and remember to run a cell inside of a Jupyter notebook, we hit Shift Enter—and you can see it's gone and printed out "Sup Neil."

Now, what would happen if we went and assigned that name something different, right? So let's say we assigned it "Nick," right? So my name now. What would you expect to happen right here? So it wouldn't actually print out "Sup Neil" because our name does not equal Neil Armstrong. So if I go and run this, nothing happens, and this is where a logical operator or a condition really starts to come into hand, or comes in handy. So we are able to perform a number of checks. Now, in this case we're doing a string check, but we could take a look at different numbers. So if this particular value is greater than a value, then we can do that; if it doesn't equal a particular value, then do that.

So let's say, how would we make this print out "Sup Neil"? At the moment, what we could do is we could actually go and change our variable to be Neil Armstrong again, but we could also do one other thing. So rather than checking whether or not our name is Neil Armstrong, we could say, "What happens if our name isn't Neil Armstrong?" Then we're going to print out "Sup um, not Neil," and you can see that we've gone and done this. So again, by changing our logical operator—or in this case our logical operator—we're able to change how we go and apply our logic and specifically our condition. For now, we're going to leave it as equals equals. So you're going to use this a ton when it comes to working inside of data science because often you're going to need to check whether or not a particular value equals something; whether or not a particular value is greater than a particular value.

So let's actually do one with a number, right? And so we're going to say, create a new variable, so test_number equals 10, and we're going to use the if statement again. So if test_number is greater than, let's say 5, and so again this is another example of a logical operator. So this is greater than; this is less than; uh, we can also say greater than equals to; um, and I'll include a list of logical operators in the description below so you can see those as well. But basically you've got the ability to perform a whole bunch of checks using the if statement, also known as a Python condition. So we're going to add in our colon again, and we are going to print out "Number"—actually we'll use a string—"Number is greater than 5," greater than equal to, because remember we've added this equals as well. If we go and run this now, you can see that we're now performing a check on a number. Now we could also—what would happen if we dropped our number to below five? So if I make it four, you can see that it's not printing anything out. So in this particular case, we've gone and applied a logical operator inside of a Python condition using numbers rather than a string. So this is an example for a string; this is an example for a number.

Now, once we get to the pandas section of this course, what you'll notice is that you use this a ton. So you'll actually check whether or not a particular value is greater than a particular value, or you—and based on that you won't just be printing out a value in this case. So I guess I'm trying to give you a little bit of context: you won't just be printing out a value; more often than not you'll go and apply a specific transformation. So say, for example, a really good example is probabilities. So when you use certain machine learning models, what you'll actually get back is a probability, and that probability is going to be between the values 0 and 1. More often than not, what you're going to do is apply a bit of a logic gate, so you're going to say, "If that probability is greater than five, then the probability of the thing that's actually being predicted is true; if it's below five, then the thing that's actually being predicted is false." So what—how—what might we do that, right? Let's actually take a look at this. So rather than leaving it as test_number, so let's call it probability, and we're going to set it to 0.73, and remember what type of data type is this? This is a float. So what we're going to then do is type in: if probability is greater than 0.05, or 0.5, then we are going to print "true," and then we're going—we should have the other side of this as well, but we're going to get to that in a second. So if we print "true," that is printed out "true."

Now you're probably thinking, "Well, how do I print out 'false' or return 'false' if it's not greater than 0.5?" This is the next step in our set of conditions. So what we can do is we can apply the next level of conditions; we can type in "elif," and we can also apply "else." Let's actually apply "else" in this case. So we are going to say "else," and you can see there that "else" is in green as well; that's a conditional operator, right? You can see that. So what we're going to say—actually let's say—let's leave it as "else." So if it's not greater than 0.5, then what we are going to do is print "false," right? So right now, probably it's still returning "true" because we haven't changed our probability. We change our probability to 0.04, for example; you can see it's now printing out "false," right? So that—that looks all good. But what would happen if our probability was a negative value? Right? In that particular case, we probably got an issue with our probabilities. So let's actually do this. So if we—and we wouldn't actually want to print out "false," we actually would want to print out, "Hey, double check your probabilities; there's something weird here." So what we can do is let's change this probability to a negative number, so let's say negative seven, right? And right now all it's doing is printing out "false." So let's actually recap on what we've done so far. So we've written probability equals minus seven, and then we've got our first condition, which is: if probability is greater than or equal to 0.5, colon, then we're going to print out "true," and remember "true" is a boolean value, so true or false; so the boolean values. And then what we've also got is another condition; in this case "else." So read "else." Let's actually add some commentary, as anything else. Oh, and I've also missed out on a key thing: when you're using if statements, it is white space sensitive. So you can see there I've actually gone and tabbed this in. If I remove these tabs, it's going to throw an error, so you can see that. So when you're coding up conditions in Python, you need to indent them like so. So I'm just going to hit tab, and that's going to work again. So a key thing to point out as well there. So we are going to—let's actually write some commentary for our condition. So our first condition is whether or not our probability is greater than 0.05, 0.5 sorry, and then our next condition is anything else. So basically this is actually checking whether or not we're greater than 0.05; this is just checking whether or not our—whether or not we've got a different value, right? So not 0 or less than or equal to 0.04 or 0.5. So you can see that that's all working there, right? But as of right now, this doesn't really meet our conditions at the moment. So what we need to do is we need to apply another condition to check whether or not we've got screw probabilities. So let's actually do another check. So what we're going to say is: return or print out "error" or statement if probability is below zero. And what I'm going to do is introduce you to the next part of a Python condition, and this is how we go and apply a secondary check. So what we can do is type in "elif," and the way I always remember this is I keep thinking of an elf; I don't know why, but that's sort of what reminds me of this particular statement. So "elif," and then we can apply another condition. So we're going to say: if probability is less than, let's say less than zero, then what we're going to do is print "Check yo probabilities," and if we go run this now, you can see that because our probability is -7, we are stopping at this condition right now. If we made our probability somewhere between 0 and 0.5, what we should print out is "false" now. So again, that's working correctly. So if we go and print out—let's set it to 0.04 now—let's do I don't know, 0.1, you can see that it's now getting stopped by this condition down here, so it's falling into the anything else bracket. Cool. So that is conditions in a bit of a nutshell.

So what have we got? Uh, all right, so that's looking all good. So we've gone through—let's do a quick recap. So we can create a condition using the if statement; we can create a secondary condition using the elif statement; and again, a core thing whenever you're writing up these conditions is that you start it off by writing "if," and then you pass through your statement, or whatever you want your operator to be. So in this case, if probability is greater than equal, and greater than equal is an example of a logical operator. So there's a ton of different logical operators. So you can use equals equals; you can use greater than; equals; less than; that; you can say "not," and remember "not" is with the exclamation mark and then equal sign, so you can see that there. And then in order to finish off your if statement, you need to end it with a colon. And then when you want to actually do something as a result of that if statement being met as true, what you need to do is tab in whatever you want to do after that. So in this case we're just printing stuff, so we need to tab in a print. If we want to apply a secondary conditional statement, or another set of checks, we can go and use the elif statement, so you can see that that is shown there. And in this case we're checking whether or not our probability is less than zero. And remember, we—it's sort of the same, right? So you use the statement, so elif, pass through your statement, and use your logical operator, and then you close it using a colon. Then if we want to actually go and do something, remember we need to tab it in. Now we can end our condition using the else statement. So basically this is sort of like the catch or bucket, right? So in this particular case we've just written "else," and then colon. So you don't need to pass through a statement, or you're not meant to pass through a statement when you're using the elf—else component. So that is conditions in a nutshell.

Now the other one that I want to sort of show you, which is one that I use quite a fair bit, is whether to check whether or not something is in a particular list. So let's actually create a list, and remember we can create a list of—we name a variable, so in this case it's going to be a list of names. So I'm going to type in "names," and let's actually just copy out—let's write it up. And so remember the defining characteristic of creating a list is square brackets. So we're going to create square brackets and then pass through some names. So we're going to do Neil Armstrong, Buzz Aldrin, Sally Ride, uh, who else? Yuri Gagarin, and the last one we're going to say Elon—again Elon Musk, right? So that is our list now set up. So in order to run that, we can just hit Shift Enter on our keyboard. Let's add some commentary—good practice. So create a list of names. And then say, for example, we wanted to do a logical check to see whether or not a particular value is within this array, or within this list. Now, rather than actually going and looping through—which we'll do in a sec—rather than looping through each one of these values, we can actually use the "in" statement from Python. So say, for example, I wanted to check whether or not the name Elon Musk is inside of that list, what I can do is say: if—actually we can just do Elon Musk in names—we should actually get rid of that. You can see it's printed out "true." So the cool thing about this is that this is—this "in" statement basically allows us to perform a check over our entire list. So what I've written there is—again I've created a string, so in this particular case I've started it off with quotes, and then I've written Elon Musk in name. So this is almost like looping through each one of our values, or it's actually going through all of our values really quickly and doing that check. Now we can have an if statement. So if Elon Musk in names, then what we can do is print "Elon's here." Cool. So that sort of shows you how—and again I use that quite a fair bit inside of a lot of data science work, and you probably will as well, because you want to check whether or not a particular value is inside of a series or inside of a list. So again we'll go through this in a bit of detail when we look at pandas as well.

Now what you would have seen there is that there was a little bit of an error where I tweaked this print. So say, for example, I did this, right? So I wanted to have an apostrophe after Elon's, right? If I go and run this now, it's going to throw an error, and this is because Python is reading this as though I'm closing off my string here with an apostrophe. What we need to do is do this thing called an escape to get past this, and to escape out of this we can just pass through a backslash. So this sort of tells Python, "Hey, skip that quote; don't treat it as closing off our string." So if ever you get an error that's sort of like this, or you want to print apostrophes inside of your strings, you can go and escape it, and you can see that now we're printing out "Elon's here." So this is an example of an escape using Python escapes; that's particularly useful whenever you're working with um strings or string formatting. Having that backward slash allows you to escape characters when you want to pass through uh—what would that be—grammatical components. Okay.

So let's do a recap. So we have gone through conditions, and we've taken a look at how we can use the if statement, and we've also taken a look at a few different logical operators. So we've taken a look at equals equals; we're taking a look at exclamation mark equals, so this is "not"; this is equals; we've taken a look at how we can use greater than; how we can use less than; and we've also taken a look at the different components when actually going on and applying conditions. So we've taken a look at the if statement, and remember—core thing when you're creating your if statement—pass through your actual statement, or whatever you want your condition to be, then close it off with a colon; really important that you close it off with the colon. And then in order to actually do something, or when that if statement is triggered, you need to tab in; in order for that to be valid, otherwise you're going to get an error, and you're going to have a bad time. Then in order to have a secondary elif statement, or do another elif statement as part of that if statement block, we can use the elif statement, which in this case—remember elves—that's an easy way to remember it; it's an e l i f; that's elif, or also known as else if; that's sort of where it comes from. And again, remember the way to define it is again very similar to how we define our if statement up here, except you're just using the elif statement. And then in order to have a catch-all, we can use the else statement down there. And then what else have we done? We've also gone and used the "in" statement to be able to go and check whether or not a particular value isn't in a list, and again you'll use this a lot when it comes to working inside of data science. On that note, that is our conditions and logic now done. Now we're up to our loops. So again we're going to create a—let's actually take a little break; see if we got any questions. I love the background image. Awesome. Can I teach you ROS? Yes, I don't know; we'll get to it eventually. I haven't done too much with—with it, but I'll definitely take a look at something in that. Ashley, what's happening? Lisa Jane, glad you're watching while you work. Oh yeah, the indentation can be a little bit tricky, but if you just remember that you need to tab in whenever you've got a condition, it's just going to make your life a whole lot easier. You can use double quotation marks. So this is a great question: so can we use double quotation marks when we're going and printing stuff out? So if I use double quotations, I can do that as well, right? Not going to throw an error. Cool. All right, so any additional questions? What else do we have? Was this—I think this was your question—so can we get rid of the escape if we actually use double quotations? So yeah, so again it's not going to pick it up there; it's more if you're using the same type. So if I use double apostrophes there, I'd need to escape out of it. So good question there. So if we use a different type of quote, so we can actually use single quotes or double quotes inside of Python. If I use double quotes, I don't need to escape this single quote, but if I use say, for example, a single quote here, I can put a double quote here without an issue. So that's another way to get around actually having to use the escape, but it's really, really important to understand Python escapes because they're going to make your life a ton easier when…

You get stuck with something like that, right? Okay, so that is let, and let's save this. So we're pretty good now. So that is conditions and logic now wrapped up.

Now, the next thing that we're going to go on ahead and do is loops. So I love loops. Loops are really, really important because they allow you to do something multiple times. So this is the power of Python programming and programming in general; you want to be able to repeat and automate stuff using a loop.

So what we're going to do is we're going to create a new section now, and we are going to convert this into markdown. How do we convert a cell into markdown? We hit escape on our keyboard, and you can see it's now gone blue. Then we hit m on our keyboard to convert it into a markdown cell, which you can see up there. And then we're going to create a new section, and this last section was four; let's just make sure, all right. So this section is going to be section five, and we're going to call it loops, all right. So we have a bunch of loops that we're going to be getting through. So let's actually do this.

So the first loop that we're going to go through is the for loop. So the way to use this—let's actually add a comment—so for loop. So the way to use this is to type in 4, which you can see down there. So let's add in a couple of extra cells. How do we do that? Hit b, and this is going to add more cells below. So the for loop is going to allow us to continue doing something and basically loop through a set of values; that's probably a terrible explanation, but let's actually take a look at how it's used.

So in order to do it, we write for, and then we need to loop through something. So say we wanted to loop through all of these different names here, and remember we went through and applied or checked whether or not Elon was in that list of names, but what happens if we want to apply different components or different Python conditions to that set of names? We'd actually want to loop through and apply some conditions. So what we're going to do is let's define this. So for name in names, then we can go and do something.

So before I actually go and write out the loop, let's actually take a look at what we've written there. So we've added it in comment, and we've written for loop on this is an example of a for loop. Let's actually write proper commentary. And then we've written for name in names. Now let me explain what this is doing. So basically what we're doing is we're looping through our names list, which we had up here, and we are going to be able to interact with each value in that list at a point in time. So you can see here that we've got name, so name is going to be the single value that we're looping through at that point in time.

Now let's actually do something. So remember this is similar to an if statement in that, in order to do something in our for loop, we need to tab in, right? Cool. So let's say, for example, we just wanted to print out each every and every name. I can type in print and then name, and you can see that is looping through each and every name that I've got inside of my list. So what we've written is for name in names, print, and then we're actually printing out the current name. So this name value is actually the value that we're actually looping through at a point in time. So we're basically going through and actually going, all right, print out Neil, print Neil, print Buzz, print Buzz, Prince Ali, Prince Ali. So we're basically going—this is the first name, so we're going to print out that name; this is the second name, we're going to print out that name; this is the third name, we're going to print out that name; this is the fourth name, we're going to print out that name; this is the fifth name, we're going to print out that name. So what we've written is for name in names, print name.

Now the nice thing about this is that we can actually go and apply some conditions to this as well. So let's actually do that. So say, for example, we—if our name was Buzz, we wanted to print out, um, let's say print Buzz, right? So if name equals equals Buzz Aldrin, then what we're going to do is print sup Buzz. So let's do this now, and what we're going to do is get rid of this print over here, and there you go. So what we're actually doing is we're looping through, and we're actually printing out sup Buzz, but right now it just looks like we've only done something once, right? And this is because our if statement doesn't do anything unless this condition is met. So you can start to see how this all fits together, right? So we can loop through and do something multiple times and then apply conditions to actually trigger something when a particular condition is met as we're looping through.

Now the next thing that we're going to go ahead and do is use—let's actually add some additional conditions that actually print out the rest of our names. So we're going to say elif, and remember elif is our next check. So elif name equals equals Yuri Gagarin, then we're going to print out hey Yuri. And if we go and run this now, we should print out sup Buzz. So we'll loop through, print out sup Buzz; we'll then loop through; once we get to Yuri, we're then going to print out hey Yuri, and there you go. So we've now gone and applied a second condition inside of our loop. You can start to see that we're actually looping through—let's actually print out our names at the same time—so print name. So we've—we're up to Neil, and then we're going to get up to Buzz, and you can see that when we get up to Buzz—let me zoom in on this so you can see it—so once we get up to Buzz, we're going to print sup Buzz; once we get to Sally, we're going to do nothing; once we get up to Yuri, we're then going to print out hey Yuri; and then once we get up to Elon, we're just going to print out hate Elon.

Now there's a bunch of other statements that you can use inside of loops. So say, for example, we wanted to break our loop once we got to Yuri—so stop looping, right?—we can actually go and add a condition call or add a statement called break, and this is going to stop looping through the rest of our loop once we get to Yuri. So if I go and run this now, we're not actually going to print out Elon Musk. Actually, before we do that though, let's finish off if statement—so or if statement block—so let's print else, and then we're going to say print nothing or name not part of conditions. I'm going to teach you one other thing inside of that else statement block, but we'll get back to that, all right. So let's focus on this break statement. So say, for example, we're looping, we're looping, we're looping; we want to stop, right? So in this particular case, we've gone and found that Yuri's in the list; we now want to stop our loop; we can use the break statement to stop our loop, right? So if we go and hit break, you can see that we are now stopping at Yuri down here. So we've got up to Neil, and it's printed name not part of conditions; we've got up to Buzz; we've printed out sup Buzz. So we've got up to here; we've gone to Sally, and remember Sally does not equal Buzz Aldrin; Sally does not equal Yuri Gagarin; Sally falls into the else bucket, so we're going and printing out name not part of conditions, which you can see there; we're then up to Yuri, and then we're going—Yuri does not equal Buzz Aldrin; Yuri does equal Yuri Gagarin, so we're printing out hey Yuri, and then we're hitting this break statement, and that's where our loop is stopping. So it actually stops then and there; it doesn't continue any further. Now that's what it gives you the or shows you how you can actually stop your loop. So remember, sometimes you'll hear of people creating loops, and they'll just go and run forever. Really, really important to understand when and where to use your break. So in this particular case, we're stopping our loop once we get to Yuri.

Now say, for example, you wanted to—once you get to a particular person, you wanted to go and say, all right, go to the next value in the list, don't do anything else—well, we can actually use a statement called continue. So say, for example, I got up to Buzz, and say, for example, we had a whole bunch of stuff that we needed to do inside of our loop, right? So outside of this if statement, we wanted to go and say print name. Actually, that's a bad example. Let's go print hello world, and let's actually add in a line break as well. So uh, there we go, all right, cool. So what I've gone and written there is print hello world, and then this over here represents a line break inside a string formatting, so backslash n actually allows us to print out a space between each of our prints. Now you can see here that what we're doing is we're first up printing out our name—well, actually, let's make this a little bit more relevant—so um, let's say—let's say we're welcoming them, right? Uh, pleased to have you here. So we'll print out whatever the person's name is, and then we're gonna say, pleased to have you here, right? So that—let's say they've just arrived at the space shuttle launch center, right? So if we go and run this, so we're going in printing Neil, name not party conditions, pleased to have you here; that's not the nicest welcome, right? Uh, Buzz, pleased to have you here; Sally, name not party conditions, pleased to have you here; Yuri, hey Yuri; and then remember because we're hitting this break over here, we're stopping, and this means that we don't actually get to this over here as well. Say, for example, because Buzz is part of some special program, we're not going to say pleased to have you here, right? We can actually go to the next word in our statement by using the continue value. So if I type in continue, we are not going to get to this over here. So if I run this, you can see that over here—let's actually print out a line after this as well—so you can see that a bit better. So you can see we're going—all right, so we're getting to our first value in our loop, so we've got to Neil Armstrong; we're then going to go—all right, so name does not equal Buzz Aldrin, so we're going to go to the next statement; name does not equal Yuri Gagarin, so we're going to go to the next statement, and we're going to fall into this catch-all bracket, name not part of condition. So Neil falls into name not part of conditions, and then it's going to print out this line here because it's outside of that if statement block. So let's make a comment out of this—outside of if statement block. All right, so we've got to Neil; we've said pleased to have you here; um, we've got to Buzz, and then—so we've ridden Buzz Aldrin, and then we've printed out sup Buzz, and I've added in a line break as well, so you can see that a little bit better, but we haven't gone over to here because remember we should go to this block over here regardless of what statement it falls into, but because we've got to continue, what actually happens is our loop actually goes to the next value within that loop or within—in this particular case—the next value in our list. So this gives us the ability to actually go on ahead and skip through.

Now there's one last thing that we want to go on ahead and do. So right now we're printing out name not party conditions; that's not all that nice. So remember we're welcoming our astronauts into the space station; we don't want to just go and say name not part of conditions. Let's say, for example, we didn't actually know their name; we just wanted to print out pleased to have you here. So what we can do is we can use another statement called pass, and this basically means do nothing. So the thing is with Python, if we have an else statement, we can't actually just have nothing down here; otherwise, it's going to throw an error. So what we can do is we can use this keyword, so pass, to actually allow us to continue gracefully. So if I go and run that now, you can see we've printed out Neil Armstrong, pleased to have you here; Buzz Aldrin, what's up Buzz; Sally Ride, pleased to have you here; Yuri Gagarin, hey Yuri. So that shows you pretty much how you can apply a bunch of different components inside of your conditions as well of you as well as your loops.

Let's take a—take a step back and actually take a look at what we did. So we wrote for name in names, and then we'll—remember we're printing out our name regardless—and then we've got our if statement block—actually, let's call this what it is; it's a condition block—and then we've got a couple of different components in here. So first up we've got our if statement, which checks whether or not the name is Buzz, and if the name is Buzz, then we're going to print or sub Buzz, and then we're going to have a line break. So remember, backslash n gives us a line break, which you can see down there, and then we're going to continue. So remember, continue allows us to go to the next value in the loop—so next value in the loop. Now in this case, because we're looping through a list, it's going to go to the next value in the list, but I'm actually going to show you how to loop outside of actually using a list as well. So this is going to go to the next value in the loop, and then we've gone to our elif statement. So elif name equals Yuri Gagarin, then we're going to print Yuri, and then we're going to break. So that is going to stop our loop, right? And this is why we're no longer printing out Elon Musk as well, because this break statement stops the loop. So let's write—break statement stops—and then we've got our else statement, and this is our catch-all, and then our pass statement is sort of like a placeholder—placeholder to do nothing. Cool. So, and that is pretty much loops in a nutshell.

So what you want to remember from this is we've used a for loop. So this is one example of a type of loop inside of Python—so for something in something. So remember, we need to loop through an iterator; in this case, we're looping through our list, so for name in names, and then what we're going to do is print out that name, and then we've gone and applied some conditions. So remember, we learned about conditions inside of step four, and then we also took a look at how we can use the continue statement, the break statement, and then the pass statement. So the continue value allows us to go to the next value in the loop; the break statement stops the loop altogether; and then the pass statement is a placeholder when we're using conditions, so in this case, it basically means do nothing.

Now say, for example, rather than looping through a list, we actually wanted to loop a set number of times. Well, what we can do is we can use a method called range, or specifically a function. So we can write another for loop—so for—normally what I say is for idx—so index—and let's actually add a comment—so loop x number of times—God, can't write times—okay. So for index or idx in, and then this is how we can create a sort of another type of iterator. So we can use range, and then we're going to pass through the number of times we want to loop. Say we want to loop 10 times, we can then print out our index, and you can see that gives us the ability to loop a number of times. So what I've written is for idx in range, and range is sort of our iterator, so this sort of forms the number of times that we actually want to loop. So for idx in range, and then we pass through how many times we want to loop—so 10—then you can see we've actually gone and printed it out 10 times. Now remember our loop is going to start at the value zero, so that's why it's actually gone and printed at zero. We wanted to start from the value—if we wanted to start from a different value, what we can actually say is for range one comma ten, then you can see it's going to start at one, but this time it ends at nine. So what we wanna do is we wanna bump this up by one, so we're going to loop inside of that range, so we're going to start from 1 and go all the way up to 10. So rather than passing through a single number, you can pass through a range—in this case, 1 to 11, and the 11 is excluded. Okay.

So what have we gone and done so far? So we've gone through loops; we've gone through a bunch of different statements; so we've gone through a for loop; we've gone through how we can use the continue statement, the break statement, and the pass statement; and then we've also gone and taken a look at how we can loop a set number of times. Now there's another loop that I want to take a look at, and this is the while loop. So let's actually go on ahead and use this. So say, for example, we are at the launch pad in our space station, and we're going to launch a bunch of name—a bunch of space shuttles out into the atmosphere, right? What we want to do is we want to launch—let's say, for example—five different space shuttles. How might we do this? Well, we need to start launching some space shuttles. So let's say, for example, we're going to create a variable to hold how many space shuttles we've launched, and we're going to call this launched shuttles, and we're going to set this equal to zero because right now we haven't launched any. And then let's say uh, how many shuttles we actually want to launch—so total shuttles equals five. What we want to do is we want to start launching. So what we're going to do is use a while loop, and so this is creating our variables initially. So to create launch variables, you're basically going to be able to create SpaceX after this guys, a kid, all right. So we're going to use that while loop. So what we're going to do is we're going to say while true, and then we're going to start looping. So the difference about a while loop and a for loop is a while loop is going to start doing something continuously, and it's only going to stop when you tell it to stop, and this is where our break statement really comes in handy because you actually need to break out of the loop in order to stop it, so or you actually need to set this condition to something else, but in this case we've just said while true, which means it's going to be a forever loop. Very, very important that you use our break statement here. So let's go ahead and loop through. So we are going to say—let's say we launch our first shuttle—so we're going to print out—print launch shuttle or shuttle launched—and then what we want to do is we want to increment how many shadows we've launched by one. So what we can do is we can add a number to that, and specifically we're going to add the number one because we're launching one at a time. So we can type in launched shuttles plus equals one. So this is some Python syntax here. So what we're actually doing is we're adding a one to our launch shuttle. So we're grabbing the existing value, and then we're going to add one to this. The way that we could also define this is we could say equals launched shuttles plus one, right? So this is just a little bit of—um—it's basically more Pythonic. So whenever you hear someone refer to something as Pythonic, it basically means that we're using or base using the majority of the syntax which is available inside of Python to make stuff a little bit cleaner. So rather than doing that, what we're going to say is plus equals one, and this is basically going to increment our launch shuttles by one. But what we want to make sure is—what we want to make sure that we do is that we don't launch more than—more shuttles than

We're meant to on that particular day, so right now we've got five shuttles that we want to launch. So we want to make sure that we only launch five. So we'll add in a condition, and you can start to see how our loops work with our conditions really well. So if uh, launch shuttles and what we're going to say if it equals 5, equals equals uh, we're actually going to say if it equals total shuttles, then what we want to do is we want to print that all of our shuttles have been launched. So print all shuttles launched, and then we're going to break out of our loop. So this is what we're going to use our break statement.

So let's actually take a look at what we've written there. So while True: so basically this is going to kick off our loop and start launching our shuttles. We're going to print shuttle launched, and then we're going to increment our launch shuttles by one. Then if our shuttles or our launch shuttles equals the total number of shuttles that we want to launch, so remember we're using our if statement here, we're ending it with a colon, and we're using an equals equals logical operator, we're going to print all of our shadows have been launched, and we're going to break out of our loop, which will stop our while loop.

So if I go and do this now, you can see we've gone and looped that many times. So what we've gone and done is we've launched a shuttle, launched a shuttle, launched a shuttle, launched a shuttle, and then once we hit five shuttles, all shuttles are launched, and then we stop our while loop. Now the nice thing about this is, say for example, we wanted to launch 10 shuttles, we can change our total shuttles variable to 10, and this is going to launch 10 shuttles. Now, now if we wanted to, we could also print out which shuttle we've actually gone and launched, and this is something that I wanted to sort of show you as well.

This is an example of using string formatting. So rather than just saying shuttle launched, what say for example we wanted to print out what shuttle number we're up to. Right? What we can do is we can use the string formatting function. So we can type in .format, and then the .format is a method that is attached to strings. Remember we talked about this a little bit inside of our data types; certain data types are going to have certain methods attached to them, so you'll sort of see this once we get to how to loop through dictionaries as well. So, so this is an example; remember up here we loop through a list, this is using a while loop; we're also going to take a look at how to loop through dictionaries because it's really really important to know.

So say for example we wanted to print out what shuttle we've actually launched, well all we need to do is create a set of squiggly brackets inside of our string, and then we need to pass through what value we want that particular value over here or our placeholder to be filled with, and in this particular case we are going to print out um the launched shuttle value, and as of right now this is going to print out from the number zero, so it's going to start off from zero and then go up to shuttle four. Now what we could do is we could just add a one to that, so that's going to start off as shuttle one. So let's actually take a look at what we did there. So we wrote shut, so we've still got our standard string, but we've gone and applied these squiggly brackets inside of it, and then we've used the .format function over here or .format method to actually pass a value into this string. So this is known as string formatting, right, and it makes it just a little bit easier to inject values into your string.

So if we go and run that now, we've gone and written something uh, this should be launched shuttles, so again, so we've got an error there. Let's actually take a look at that error. So it's written NameError: name 'launched_shuttle' is not defined. Now this is basically telling me that this variable doesn't exist at the moment. So let me get rid of that fix. So I've actually gone and I can see here that it's saying on line 8 I've got this error, and I've got this error over here, so NameError: name 'launched_shuttle' is not defined. So if we go to line eight, so zero, one, or does it start at zero? 0, 1, 2, 3, 4, 5, 6, 7, oh sorry it starts at one, so 1, 2, 3, 4, 5, 6, 7, 8. So we've got an error on line 8, which is indicated to us here. So this is an example of a Python error, and it's saying that particular variable is not defined. Now, right here we can see that we've actually gone and passed through launched_shuttle to this string formatting function, but that variable doesn't actually exist; that's why we're getting that error. The actual variable that we need to pass through is launch_shuttles with an s. So if we go and update this now and run it again, you can see that we no longer have that error. So rather than, so that's a, I guess I want to sort of show you how to debug some of these errors when you do encounter them as well.

Okay, so what we've got now is Shuttle 1 launch, Shuttle 2 launch, Title 3 launch, Channel 4 launch, Shuttle 5 launch, Title 6 launch, Channel 7 launch, Started all 8 launched, the Shuttle 9 launch, Shuttle 10 launched, all shuttles now launch. So that gives you the ability to use a while loop, right? So let's add some commentary: So create a while loop. All right, let's take a step back; let's see what we've written. So remember we've created our two launch variables, so launched_shuttles and we've set it equal to zero to begin with. We've then set how many shuttles we want to launch in total, so total_shuttles equals 10. We've then gone and written while True: so this is basically going to loop continuously until we tell it to stop. We're then printing out whether or not uh what shuttle we're actually launching, and remember we've used string formatting there, so the format function allows us to do that. We're then incrementing our launch shuttles by one, and remember this is really important because if we don't have this line here we're going to get stuck in an infinite loop. If you want to try this, try it, but you're going to need to cross stop your Python kernel if it gets stuck. So in this particular case, it's going to increment it by one, and then we're going to apply our condition, so you can start to see how our loops work with conditions incompa in conjunction really. So if launched_shuttles equals equals total_shuttles and then colon, then we're going to print all shuttles launch, and then we're going to use the break statement to stop our loop.

Now what else are we going to do? So we've already gone and looped through our lists; we've gone and taken a look at a while loop. The last thing that I sort of want to show you is how you can loop through a dictionary. So this is probably something that I use really really often whenever I'm working through when I'm wherever I'm actually doing real-life data science projects because sometimes I'll get my data inside of a dictionary, and if you remember correctly a dictionary looks like this. So let's actually go and create a dictionary now. So um what's it going to look like? All right, let's it looks like we're going to need to create a dictionary. So we are going to create a dictionary; actually we're just going to loop through all of the dictionary, the the keys inside of a dictionary. So let's create one dictionary. So we are going to create an astronaut just like we did yesterday, and remember the key defining characteristic of a dictionary is that it is defined using squiggly brackets. So our astronaut is going to have a key which is called name, and remember we set a key value pair inside of a dictionary, so name and then colon and then we need to specify a name. So this particular case we're going to set it to Eli, who's a good astronaut; I don't know, we'll set it to me, no, anyone want to be an astronaut? Okay, no takers; we're going to say Elon Musk, and then we're going to add in another key, and we're going to say um suit size; I'm going to set it to medium, add in a comma, and then we're going to say allergies, and we're going to say peanuts.

Now how do we actually loop through a dictionary? So say for example we wanted to do something with each one of these keys. Now I taught each one of these keys or values, these are probably, so this was something that I found a little bit tricky to grasp when I first started out inside of Python, but I'm going to show you three key statements which are going to serve you really well. So say for example we wanted to just loop through the keys and work with the keys, well again we can use a for loop. So we're going to say for key in astronaut.keys, and then what we can do is print out our key. So the .keys method which is attached to a dictionary allows us to access all the keys. So if we take a look at that statement, astronaut.keys, you can see that we get all of our keys returned, and so this basically allows us to loop through and print out or do something with each one of our keys. So this is how to loop through keys, keys in a dictionary. Say for example we wanted to loop through our values, so we actually wanted to get our username, so Elon Musk and then medium and then peanuts, right? We can do that as well, and again there's a built-in method which is attached to a dictionary which allows us to get our values. So let's actually add some commentary: So accessing dictionary keys, and then down here we're going to say accessing dictionary values, and to do that we can type in astronaut.values, and you can see there that we've gone and printed out uh Elon Musk, medium, and peanuts this time. Then what we can also do is loop through those, so for your value in astronaut.values, then print value. All right, I'm going to pause there; has anyone spotted an error with this? Right? So right now if I go and run this statement we're going to get an error, so I want to show you this one. So this is a slightly different error to the error that we had originally; this is called a syntax error, and you can see it's actually telling me where that error is, so in line 1 we've got a syntax error in this statement, and this is because we don't have our colon. So it's actually pointing us to where that error is. Now we can fix this just by adding a colon there. So if we now go and run this, we've now gone and looped through each one of the values inside of our dictionary. So this is how to loop through values in a dictionary, and the last thing that I want to show you for dictionary looping is how to loop through both at the same time. So how do we loop through the keys and the values? So let's actually add in a comment: So loop through keys and values at the same time. So what we can do is if we write for what we're going to say is key and then value in astronaut.items, so the items method allows us to access a, let's actually take a look, so astronaut.items, this is actually returning a list of tuples here. So you can see that we've got name, Elon Massoud size, medium, allergies, and peanuts. Remember braces or these curved brackets here are a tuple, and remember the defining characteristic of a tuple is that it is immutable. So what we can actually do is unpack them. So over here what I'm actually doing is unpacking that in real time, so maybe we'll talk about that or delve into that a little bit later, but just know that when you're looping through items you're going to write for key, comma value in astronaut.items, and if I go and print that, so print a key and then I can pass through a comma and I can print a value as well. So you can see there that we're now looping through our name. Let's actually print it out on separate lines. So we're going to print, print key, and then comma, and then a lot space, and then print the value. We should probably print it after it because that looks kind of weird. There you go, that's better. So what we've written is for key, comma value in astronaut.items, and then remember we've added our colon because we need to whenever we're using a for statement, and then we've printed out our key first and then on a separate line we've printed out the value, so print value, and then if we wanted to print a space between each one of our lines you can add in a backward slash n, and this gives you the ability to add a space. So that in a nutshell is how to loop through dictionaries. Remember if you want to loop through the keys, for key in astronaut.keys, and you could rename this whatever you want, so this variable up here you could name it, so say for example I wanted to call it um for dictionary values or dictionary keys, I can change that, and so that gives me the ability to loop through different things; I can name it whatever I want; this is just I normally call it keys for key, but that sort of shows you how you can loop through dictionaries.

So that in a nutshell was, there anything else I wanted to show you? Actually there's one last thing. So say for example I wanted to go and transform something and return back an array or return back a list; there is another form of loop known as a list comprehension, and again this is very Pythonic, so it it's going to look a little bit weird when I write it, but you'll see what it means in a second. So what we're going to say is for name in names, and you can see I've already wrapped this inside of a set of brackets; this basically allows me to loop through each value in a list and return something which has been transformed, and what I'm going to get back is a new list. So what I can do is store this inside of a variable; it's a new list, and say for example we wanted to add a z to everyone's name inside of our names list from where is it, up here, right? So this is our names list; let's say we wanted to add a z to everybody's name; I don't know why, but actually let's do a better one; let's actually convert it to title case. So I can type in name.title, and this is going to convert everyone's name to have a capital letter at with each name, right? So if I go and print this out, you can see that we've now uppercased each and every name. So this list comprehension comes in handy quite of quite often if you want a single line. All right, so in order to do that, written new_list equals, and then inside a square bracket, so what we're actually going to be doing is storing everything inside of a new array; we've then written what we want to do to each value, so this is sort of flips it on its head, so we've written name.title, so title is a string function that we can apply to a string, so this actually allows us to capitalize each of the string values inside of a string. So you can see there that we now have capital. So if I show you the original list which is called names, you can see that these are all lowercase; this allows us to perform some string formatting and apply title case formatting. So what we've gone and then written is for name in names, and then closed out our brackets. So this is an example of a list comprehension, and it's just another way to loop through and actually do something to a set of values inside of a list.

Okay, so we've gone through a ton of stuff, so let's actually take a look at what we did, so the recap, and then we'll wrap it up. So we went through, let's actually recap on loops first. So we went through a for loop; we went through how we can create a while loop; we also took a look at three different ways to loop through dictionaries, so we took a look at how we can use the keys value, the values value, and then the items value, and remember these are methods attached to dictionaries, and then last but not least we also took a look at list comprehension. So I tend to use list comprehensions particularly when I want my code to be nice and tidy, and overall what we went and did is we went through conditions and logics; we went through if, elif, and else; we also took a look at how we can use these specific statements inside of our for loop to, remember we went through for loops, so for something in something else, and then we can use continue to skip to the next value in a loop, break to stop our loop entirely, and pass to do nothing. We then took a look at our while loop, and then we also looked at our dictionaries and our list comprehensions, and on that denote that does wrap it up. So thanks so much again for tuning guys; hopefully you enjoyed this video; if you did be sure to give it a big thumbs up, hit subscribe, and tick that bell, and again I'll see you in the next one.

What's happening guys? Welcome to the next episode in the Python for Data Science series. So in today's video we're going to be building up from a previous video, and we're going to take it step by step as per usual, ideally so you can build up your skills when it comes to leveraging Python for data science. So what are we going to be covering in this stream? Well, we are going to have a particular focus on functions today, so specifically we are going to be taking a look at how we can use functions within Python, and specifically how you might want to use those for your data science applications. We're then going to take a look at arguments and keyword arguments, or something which comes quite in handy whenever you're defining your own functions. We'll also take a look at lambda functions, so something which is sort of has snuck into the world of Python, but it is being used a lot more lately, and then last but not least we're also going to take a look at string formatting. So we've done a little bit of this in the previous episode on what did we take a look at in the previous episode? Can't even remember now on um what was it on? Who knows? Doesn't matter. Well, actually it was on loops, conditions and loops. So we already took a little bit of a look at string formatting in conditions and loops. Now what we're going to do is take a look at how we can use that inside of some of our functions. So without further ado, let's kick this thing off and let's get coding. So we are going to go back into our Jupyter notebook as per usual, and what we're going to go ahead and do is start working with our functions or start creating some functions. So what I'm going to do is I'm going to open up a command prompt, and from here we're going to navigate into the folder that we currently have at Jupyter notebook in. So if we open up file explorer, so remember we had it inside of our D drive, and inside of do we have it inside of our D drive? Youtube, yeah, so D drive, then Youtube, then Python basics, so it was over here. So basically we're going to go navigate to the same place where you've currently got your Jupyter notebook, or wherever you want to actually start using your Jupyter notebook, and we're going to open this up. So let's go ahead and open it up. So I'm going to navigate to that same place, but again you can have your Jupyter notebook wherever you want, really; it doesn't doesn't matter all that much, and then what we're going to do is we're going to start up Jupyter notebook. So what do we do in order to start it up? We write Jupyter notebook, and this is our Jupyter notebook server now up and running. So again it sort of looks like this; what we might do is we might zoom in a little this time so we can see it a little bit better. So what we're going to do is open up Python basic. So this is our main Jupyter notebook that we're going to be working with, and remember

We've done a ton of stuff in here already, so we've already taken a look at variables, data types, lists, dictionaries, conditions and logic, loops, and now what we're up to is a function.

So, in order to go ahead and create a new cell, what we're going to go ahead and do is step out of it because remember it's—oh well, it's blue already at the moment—let's actually create some additional cells inside of this notebook. So, to create additional cells below, remember we can hit "b" on our keyboard. This is going to give us a bunch of additional cells below. Then, as per usual, what we're going to go ahead and do is create a markdown cell to really separate our code and give it a little bit of structure. So, we are going to convert this to markdown. So, right now this particular cell is active, so we want to convert that to being non-active, so we can hit escape on our keyboard, and now it's gone to blue. And then what we want to do is we actually want to step into it—oh, actually, we want to escape out of it—hit "m" on our keyboard to convert it to a markdown cell, and then we're going to have a pound symbol, and we're going to call this section "What are we up to so far?" Uh, so loops was our last section; we're going to call this one "6. Functions." Cool. All right, so we've got our functions section now set up.

Now, what we're going to do is we're going to start defining a function. So, again, in keeping with our sort of SpaceX theme, or I don't know, space travel theme, we're going to create a function now, and let's actually take a take a step back and actually take a look at what functions are. So, functions allow us to do something repeatedly inside of a set cell block, so or inside of a set code block. So, say, for example, you have to do something multiple times, rather than having to go and write that code multiple times, what you can do is store it inside of a function so that you can access it whenever you need. So, this means that you can package up your code a whole lot more nicely, and it just makes it a lot cleaner. Now, when it comes to actually using this for data science, what you'll tend to find is whenever you're defining um repeatable data processing scripts, or if you're doing stuff like, say, you're processing images with TensorFlow, what you might actually do is you might actually write a mapping function which actually maps an image to a specific format. So, functions really, really come in handy across the spectrum when it comes to data science.

So, we are going to create an initial function, and we're going to package it up quite nicely. So, we'll we'll actually just print out something in this particular case. So, let's create a first function. So, the defining characteristic of a function is is that it is defined using the `def` keywords or `def` statement. So, `def` is what we need to write to initially define a function. Then, we need to name our function. So, we're going to call it um—so, say, for example, this particular function is going to be to do with welcoming our astronauts, right? So, say, for example, they step into the launchpad, and we want to welcome them to the fact that they're now on the launchpad. So, we might call it `launchpad_welcome`. And then, to finish up our function, we add a set of parentheses and we add a colon. So, that is the crux of defining a function. So, let's add a comment first: "Defining a function in Python." And then what we actually want to do is we want to tell our Python function to do something. Now, again, in keeping with our Python consistency, in order to write our next line up for our function or add something to our function, we need to tab inward. So, if I wrote something here, that's not going to be part of the function. So, if I write `print hello`, this should throw an error, and you can see that it's saying unex—or expected an indent block for this particular function. So, in order for this to work, we actually need to tab this inwards. And if I go and run this, this isn't actually going to do anything as of yet. So, if I hit Shift+Enter, that's not actually doing anything because so far all we've done is we've defined our function.

Now, let's actually make this print; let's actually test it out first. So, in order to run our function, we can call it. So, if I type in `launchpad_welcome` and open up my parentheses and close them, if I hit Shift+Enter, now you can see that this is actually running our function. So, you can see it's gone and printed out "Hello there," and you're probably thinking, "Nick, why do I need this function? I could just go and print hello," right? Print hello. Well, at the moment it's quite a simple function, but say, for example, you wanted to do multiple things over and over, having it stored inside of a function or as part of a function is going to make your life a ton easier. So, say, for example, we wanted to print hello multiple times, or say, for example, we actually had multiple things: `print hello` and then `print 1 + 2`, `print 3 + 4`, right? And if we go and run it now, so you can see that now we're doing multiple things, right? So, we're printing out hello, we're printing out the sum of one plus two, we're printing out the sum of three plus four. So, what we can go and do is we can just go and loop through and run this multiple times. So, say, for example, we wanted to run it, I don't know, 10 times, what we could do is wrap it inside of a for loop. So, if I write `for index in range`, move this up a bit, `for index in range(10)`, then we can run our function: `launchpad`. Let's get rid of this `launchpad_welcome`, and you can see we're now able to do this particular set of steps multiple times. So, think of your function as a bit of a recipe, right? So, it's going to be a recipe for doing multiple things, or it's going to be a recipe for doing consistent a consistent set of things x number of times. So, again, you can call it really easily and use that function to be able to do something.

Now, as of right now, this isn't all that powerful because right now all we're really doing is we're doing the exact same thing multiple times. Now, the true power of a function comes when we actually start to leverage arguments. So, what we're going to go ahead and do is define an initial argument because right now I could just get copy this over here, so copy `print hello`, `print 1 + 2`, `print 3 + 4`, and paste it under this for loop, and it's going to achieve the same thing, right? So, again, looks identical. Now, this is where arguments come in, and really this is where the true power of functions start to come in. So, rather than just printing something out multiple times, we can actually pass different types of arguments to our function to be able to do specific things. So, this allows us to pass a value to a function, and then we can work with that particular value to do something in that particular space. So, let's say, for example, we had a list of names with all of our astronauts. So, we defined this in a previous tutorial, but let's go ahead and do it this time. So, who was in our list? Let's actually write it from scratch. So, we're going to create a list of names: `names = ["Neil Armstrong", "Buzz Aldrin"]`, and remember we're creating a list, so the defining characteristic of a list is using a set of square brackets: `["Buzz Aldrin", "Sally Ride", "Yuri Gagarin"]`, plus we got Elon Musk. Boom. Okay, cool. So, that is a set of names. Now, say, for example, rather than just having a generic welcome statement, we actually wanted to print out "Hello" and whatever the actual person's name is. So, we could actually tweak our `launchpad_welcome` function to be able to accept this name as a parameter, and then what we could do is we could actually use some string formatting to print out that name. So, let's go ahead and do this. So, we are going to create a new function now called `custom_welcome`. So, `def custom_custom_welcome`, right? And then we're going to set a parentheses and then close it off with a colon. Now, in order to define an argument—so, this is also known as a positional argument—we can actually create a variable inside of this, or we can actually pass a variable into this function. So, I'm going to call the variable `name`, and then by defining this argument over here, we can then work with the variable `name`. Now, as of yet, we haven't actually defined what `name` is; this is because when we actually go and run the function, we'll pass through what that variable actually represents. So, let's go ahead and create a `custom_welcome`. So, we are going to type out `print`, and then we are going to say "Welcome," and then remember we're going to use some string formatting, so we can pass through a parameter to a string by using squiggly brackets and then use the `format` method: `.format`, and then we can pass through a value `name`. So, what's effectively happening here is whatever value we pass to `.format` in order is going to be passed through to this set of squiggly brackets. So, basically, if I pass through um "Nick" over here, this will print out "Welcome Nick." So, let me show you that in a little bit more detail. So, `print "Welcome {}.format("Nick")`, you can see it's going to print out "Nick." Now, if I had a second set of brackets, what it's going to do is take that second parameter. If I run this at the moment, it's going to create an error. So, it's going to say "replacement index out of range for positional args tuple." Basically, that's saying is that I haven't passed through enough arguments to fulfill a squiggly bracket. So, I need to fill both squiggly brackets. So, I can say "Welcome Nick or not." So, I can pass through "or not" as my second argument to my `format` function, and you can see now it's taking both of those: "Welcome Nick Renaud." So, this is our first value that we're going to be processing, and this is our second value. Now, we can take this and use it as part of our function. So, what I've written here is `print`, and then open parentheses, parentheses, and then I've created a string, so I've used a quotation mark, and then I've written "Welcome {}", and then another quotation mark, and then the method—remember we can attach certain methods to certain data types—so one of the data methods that is attached to a string is the `format` method, and this allows us to update a particular string with a variable. So, we're going to pass through `name` into this string. So, if we go and create this function now, what I'm going to do is let's move this down a bit. So, what I'm going to do is now we're going to run this `custom_welcome`. So, again, we can run `custom_welcome`, or we can actually use the `custom_welcome` method. And the core difference to this particular function versus what we had over here, which was our `launchpad_welcome`, is that right now this is actually going to cause an error if I run it like this. So, you can see it's saying `custom_welcome` missing one positional argument. So, what this value actually represents up here is actually a positional argument. Now, we'll talk a little bit more about this in a second. So, what I need to do is pass through a value for our `name` variable. So, right now it's undefined. So, say, for example, we wanted to do "Nick or not," you can see that this is what our function is actually doing. So, we are passing through this variable to our function, and then that means that we've got the ability to use that variable inside of our function, and we've just done some string formatting in this particular case.

So, let's take a look at what we wrote there. So, to define our function, we wrote `def`. To define our function, we then name it. So, in this particular case, I've named our first function `launchpad_welcome`, and then we've finished it off using a set of parentheses and a colon. Then, in order to do something in that specific function, we need to tab in our code because this tells Python that this is what we want to run as part of this function. In order to run a function—let's actually add some comments on there—so "Running our function." In order to run a function, we can just call it, and remember we need to have a set of parentheses at the end to be able to actually run that particular function. Then, what we've also done is we have—so this is sort of an example of how you might just loop through and do a set of steps—so this isn't really all that relevant, but it sort of shows you what's possible. Now, we're not going to need this inside of a `val` function area, so we can actually just delete that by hitting "dd" on our keyboard: dd. And then we also took a look at string formatting. So, we can type in "String formatting," and remember we can pass through a set of variables to our specific string by passing through a set of squiggly brackets and then using the `.format` method to actually pass through those variables. So, remember if we run this, we're going to print out "Welcome Necron." I could change this and say "Welcome Elon Musk," and you can see that that's how we're able to pass through different values into our stream. And then what we've gone and done is we've started to take a look at positional arguments. So, "Positional arguments," right? So, as of right now, we only have a single positional argument, right? So, we've gone and defined our function similar to how we did up here. So, we write `def launchpad_welcome`, and then a set of parentheses and then a colon. We did pretty much the same for our `custom_welcome`. So, we wrote `def custom_welcome`, and then we wrote a special—we passed through a specific variable, which in this case was `name`, and we closed it with a colon, and then we did a little bit of string formatting to go on ahead and print out our custom welcome. So, that is—so what have we covered so far? So, basic functions and positional arguments.

Now, you're probably thinking, "Well, what happens if I want to pass through multiple arguments?" Well, you definitely could. So, let's say, for example, we wanted to print out what space shuttle they're going to be on. So, we can create another positional argument. Let's actually copy this and paste it down here. So, we can create another positional argument. So, we're going to update this comment: "Multiple positional arguments," and we're going to call this second argument `spaceship`. Right now, as of right now, we don't actually have a spaceship to pass to our positional argument or to pass through as that second argument, but that's fine; we'll just test this out for now. So, we can actually go and use that positional argument over here. So, again, similar to how we did it down here, we can say "Welcome blah blah blah to the," and then we'll pass through another variable inside of our string, and then we're going to update our `format` statement: `spaceship`. So, this should ideally breed "Welcome whoever the name or whatever name we're passing through to the," and then it's going to print out the name of this ship. So, let's actually go on ahead and update this function now. So, we're actually going to rename it. So, rather than leaving it as `custom_welcome`, we'll say `custom_welcome_to_ship`. If we go and run this now: `custom_welcome_to_ship`, we can pass through a name, so we'll pass through uh "Necronaut," pass through a name of a ship—uh, I don't know, "Galactic One"—and you can see that we've now gone and passed through two different positional arguments. So, it's gone and printed out "Welcome Nick Renaud to the Galactic One." So, that gives you the ability to pass through multiple positional arguments into a single function. Now, a key thing to note—the reason that they're called positional arguments is because the order matters. So, if I went and switched these, you can see that this is still taking through our variables, but now it's printing out—it's taking "Galactic One" as `name` because `name` is our first positional argument, and it's taken "Necronod" as the `spaceship` because `spaceship` is our second positional argument. So, you can see that when I've gone and run `custom_welcome_to_ship`, it's taking "Galactic One" as my name, so it's written "Welcome Galactic One to the Necronaut," which again, it's not—it doesn't exactly scream correctness, but that sort of starts to begin to show you what is the core difference with a positional argument or why it's called a positional argument because position actually matters. Now, if we switch this back, we should be back to normal, and there you go. So, now it's printing out "Welcome Necroni to the Galactic One." Now, we could also loop through this as well. So, let's actually just create a spaceship array or spaceship list, add in a couple of extra lines, and we're going to call it `spaceships`. I don't know, we'll just call it `spaceships`. How many do we need? So, we need one per astronaut: one, two, three, four, five. All right, so we're going to call this the "Galactic One," "Galactic II," and a "USS Voyager," "Maximum Amazing Wow Spaceship," uh, and then I don't know, I'm trying to—Apple ship—let's say Apple's building spaceships now: "Apple Spaceship." All right, cool. Now, again, we can go and write a loop. So, we can loop through each one of our names. Actually, this is a really interesting one, right? Because so far I've shown you how to loop through one list. What happens if we have multiple lists and we want to map them sort of together? Well, let's actually take a look at how we might go about doing this.

So what I can do is I can again loop through my list. So, for let's say `name` in `names`, and I can print out my name. Or let's actually just run our custom welcome, custom welcome. So that allows me to loop through each one of my names and print them out. But how would I actually go and get the same positions in that second array, which you can see up here? Well, what we can do is we can use a function called `enumerate`, and this is going to give us an index as well as a name. So I can type in `idx` and then `name`, and then so let's go ahead and print out—let's get rid of our custom welcome for now—so I can print `idx` and `name`. This is really, really important. So what is actually happening now is I'm not only just getting the name, I'm also getting an index of that particular value. So, by doing or using the `enumerate` function, I'm able to get the index, or specifically the position of the value that I'm up to, plus the value from that array. So you can see that I've got `(idx, name)`, and so this is unpacking our values that are returned from `enumerate(names)`. And by wrapping our array inside of the `enumerate` function, we're basically able to get an identifier as well as the value from that particular list. And you can see that I've got `idx`, `name`. Now, this is the reason why I've gone and defined this `spaceships` list, because I can now go and grab the specific value at a position. So remember, we can use indexing to grab a specific value from a list at a point in time. So if I type `spaceships[0]`, that's going to get `galactic 1`. If I type `spaceships` and then inside of square brackets pass through index 1, I'm going to be able to get `galactic 2`. `spaceships` and then inside of the square brackets index 2, I'm going to be able to get the USS Voyager.

So what we can now do is we can now use our `custom_welcome_to_ship` function. So if I type in `custom_welcome_to_ship`, I haven't actually explained how we're going to do this yet, so `custom_welcome_to_ship`, and then remember our first positional argument is our name, so we need to pass through our name, which we're getting from over here, and then we need to get our ship. So how do we get our ship? So because we're looping, our index is going to change every single time, so we can create a temporary variable called `ship` and set it equal to `spaceships`, and then pass through the index that we're currently up to, so `idx`. So this is basically going to loop through and update the current ship at a point in time. What we can then do is pass through our ship to our custom function, and there you go. So now what we've gone and done is we've printed out: "Welcome Neil Armstrong to the galactic 1", "Welcome Buzz Aldrin to the galactic 2", "Welcome Sally Ride to the USS Voyager", "Welcome Yuri Gagarin to the maximum amazing couldn't even spell amazing maximum amazing wow spaceship", and then "Welcome Elon Musk to the apple spaceship". This should probably—should be called the um, I don't know, Apple I ship, probably makes more sense. Yeah, way better. All right, now I'm happy. Okay, so that is what have we gone and done there? So we've gone and done a ton of stuff. So we took a look at positional arguments and how we can have multiple positional arguments. So `def custom_welcome_to_ship`, and then we've now gone and set two positional arguments, so specifically `name` and then `space_ship`. So this means that we're going to be able to get an person's name printed out as well as the spaceship that they're on, and we can go and run that function by passing through the different positional arguments. So `nick renaud` and then `galactic one`. And then we've gone and created a new array or a new list which is called `spaceships`, and again the defining characteristic of a list is that we wrap it inside of square brackets. And then what we've also gone and done is we've gone and looped through each one of those. So I'm just going to get rid of this cell here. So we've gone and looped through our `names` array, but this time we've updated our loop slightly. So we've gone and used the `enumerate` function. Let's actually make a comment: `# Used the enumerate function to get the positional index and the name`. Cool. So this basically gives us an index and the name, so that means we can then go and pass that index to our `spaceships` list over here, and that is going to get us the matching ship to the matching astronaut, because right now we've got the same number of spaceships as we do astronauts. So we've got one, two, three, four, five spaceships, and then we've got one, two, three, four, five astronauts. And then in order to run our function again, no different, so we'll just use `custom_welcome_to_ship`, and we'll pass through the name and the ship at the point in time. Cool. I think it's time for a little break. Let's actually take a look and see if we've got any questions. What's happening? Grav power cube, awesome. So happy, stayed up, thanks for tuning in, Gabriel. Are you doing Zinf? I think that's how you say it. And then how you doing, Nikon? Thanks, so glad to have you here. All right, doesn't look like we've got any questions so far, so let's keep going on ahead.

So so far what have we covered? We have covered defining a basic function, defining positional arguments, then defining multiple positional arguments. Now the next thing that we are going on ahead and do is define a keyword argument. So how do we go on ahead and do this? So remember, as of right now we've just written the specific name of that particular variable. So what is the difference between a positional argument and a keyword argument? Well, the difference is that we actually provided an initial value inside of our function. So let's say, for example, that we had a um [Music] trying to think of a good example. Now let's say, for example, we had our allergies. Right, and not everyone is going to have a specific allergy. So what we can do—actually that's a bad example—let's actually just create one and we'll run with it. So what we can do is we can define a—again we're going to define a—the same function. So write `def`, and we are going to—so we're going to write `def`, and then we're going to pass through, let's say, for example, the astronauts wanted to pick the color of their spacesuit, right? So we're going to call it—so we're going to define the same function, so `space_suit_welcome`. So what we're going to do is we're going to make our welcome print out: "Welcome whoever to the galactic one", and then we're gonna print out ", your spacesuit is whatever color they want", and we wanna set a default. So let's say, for example, the astronaut doesn't actually go and specify a spacesuit color, let's say we want to just give them the default, which is going to be blue. How would we go about doing this? Well, we can define a position, a keyword argument in this particular case. Let's actually just say `spacesuit_color`. We don't need the welcome anymore, right? So what we can do is we can set a keyword argument, and we're going to call it `color`, and we're going to set it equal to `blue`. So this is now a keyword argument because what I'm doing is I'm assigning a default value through the specific argument inside of the function. So I've written `def space_suit`, and then I've gone and specified `color = blue`, and we can close our argument as per usual, and then we can go and say `print "Your spacesuit is"`, and then again we're going to use some string formatting, so we're going to create a set of squiggly braces `.format`, and then we are going to use the `color`. So that is our keyword argument now set up. So we can then go and run it. So `spacesuit`. Now a cool thing about this is that because we've got a default keyword argument here, we can actually go and run this function without passing through our keyword argument. So if I run this, it's just going to take the default value from our keyword argument. So this is really good if you're trying to do specific error handling, or say, for example, there are specific values that you want to handle, or specific defaults that you want to set, this is how you might go about doing it, specifically with your functions. So in this particular case, because we haven't actually gone and passed through a keyword value, we're going to get back "Your spacesuit is blue". But if we wanted to go and change it, I could type in `color =`, and then this is effectively going and resetting our keyword argument to a different value. So we're going to change it to `red`, for example. So you can see here that it's saying "Your spacesuit is red". So rather than just having the default blue, we're now going and using the red method, or we're now going and printing out red. So let's add a little bit of commentary. So this is—this is using keyword arguments and how we're going to define that. So we've gone and written `def space_suit`, and then again similar to how we defined other functions, we open it up using a set of parentheses, we then go and define our keyword argument, and the defining characteristic of a keyword argument is that you actually go and set a default value inside of that function when it's being defined up here. So you can see here that we've gone and assigned our `color` variables and set it to `blue`. So we've actually gone and assigned that variable a default value. Then again, we've used some string formatting down here to go on ahead and print it out.

What happens if we wanted to combine our keyword arguments and actually do something with them, right? So say, for example, we wanted to combine our keyword arguments and our positional arguments, how might we go about doing that? Well, a key thing is that our positional arguments have to go first. So it's going to look for our different positional arguments, and then it's going to look for our keyword argument. So let's go ahead and define um a very special welcome. It's going to tell them—so this is the one that I was sort of talking about to begin with—it's going to welcome them to whatever spaceship they're on, and then it's also going to print out their space suit color. So let's go and define this function. So `def um suit`, I don't know, let's call it `spacesuit_welcome`, and we are going to go and define our positional arguments. So we are going to—what do we have? `name` and then `spaceship` again. So this is our set of positional arguments. Then we're going to define our keyword argument, and we're going to set it equal to `color = blue`. And then what we can go ahead and do is print out something. So we can print uh what are we going to say? Uh "Welcome to the" or "Welcome whatever name to spaceship, your space suit is", and then that's our last positional argument, oh that's our last uh variable that we're going to inject into our sentence, and then we're going to use the format statement and then pass through `name`, `spaceship`. Let's make that push that a little bit higher, `name`, `spaceship`, and then `color`, right? So if we go and run that now. So `spaceship space_suit_welcome`, sorry, and then we're gonna pass her a name, so we're gonna say `elon musk`, pass through his spaceship, so `galactic`. I know what was his spaceship? He was on the Apple I ship, and then we're going to pass through—let's actually leave the color empty for now, right? So if we don't pass through our keyword argument, it's just going to take the default. So if I go and run this, you can see it's gone and written "Welcome Elon Musk to the Apple I ship, your spaceship is or space suit is blue". Now we can go and change the color. So let's say we go `color = red`. You can see that it's now going and changing that value there. Now remember I said the positional arguments are important. So if we go and type in `color = red` at the start and remove this, you can see that we're now getting an error. So it's saying "SyntaxError: positional argument follows keyword argument". This is a key differentiator between the positional argument and the keyword argument. Your keyword arguments can be in any order, but your positional arguments need to go first before your keyword arguments and then followed by your keyword argument. So I probably messed up that explanation. So positional arguments always go first, and then keyword arguments can come afterwards, but the keyword arguments can be in any order. So if I say, for example, um we wanted a different keyword argument, so let's go and create a new one called `allergies`, all right, and we'll set that equal to um `None` in this particular case, or let's say `peanuts`. Actually we'll say the default allergies is `None`. Uh then we want to print out—or let's actually go and update this and say "You have whatever allergies". Hello geez. Then we're going to pass through our `allergies` variable to our state format statement. Now if we go and run this and let's test this out. So remember we need to fix up this function now. So we're going to take off our positional argument from up here. If we go and run this again, the grammar is not great, but we've written "Welcome Elon Musk—let's just make sure I'm not—my head's not covering that—Welcome Elon Musk to the Apple I ship, your spacesuit is blue, you have None allergies". Now if we wanted to go and pass through or set our allergies, we can go and type in `allergies = peanuts`. So you can see that what we've gone and written now is "Welcome Elon Musk to the Apple I ship, your space suit is blue, you have peanut or peanuts allergy". Let's get rid of peanuts. "You have peanut allergies", but you can see that I've actually skipped our `color` keyword argument. So this is the cool thing about keyword arguments, they can be in any order. So I can pass through `color` as well, set it to `orange`, and you can see that we've gone and updated our values there. So "Welcome Elon Musk to the Apple I ship, your spacesuit is orange, you have peanut allergies". So that gives you that sort of in a nutshell shows you the difference between positional and keyword arguments. So something really, really important to note, particularly whenever you're defining functions.

Now let's do a quick recap. So we've gone through—what have we gone through? So we've gone through our main function, which was our `launchpad_welcome`. We then went through positional arguments. Remember positional arguments, order is important, and they need to be before your keyword arguments. We then went and took a look at uh how we can define multiple positional arguments. So remember position is important. Let me make a huge: **POSITION IS IMPORTANT**. And then we also looked at the `enumerate` function over here, and then we went and took a look at how we can use keyword arguments. Remember we can have—as long as our keyword arguments are after our positional arguments. Remember that after `name` and `spaceship`, we can have them in any order. That's the beauty of keyword arguments. Now before we go any further, I want to—so right now all we've done with our functions is we've actually gone and printed them out. What happens if we wanted our function to actually return a value, right? So we wanted to actually get our function to do something and then store the results of that specific function inside of a variable? Well, right now we're printing out this big line here, right? So rather than printing out that statement, we could actually return that statement, and let me show you the key difference. So right now we're printing—if I copy this same function and we're just going to update the name of the function—so rather than just leaving it as `spacesuit_welcome`, we're going to write `spacesuit_welcome_with_return`, and rather than just printing it out, I'm actually going to get rid of the `print` statement. We are then going to return the value from this function. So if I type in `return`, this basically allows me to return something from this specific function. So rather than just doing something, I'm actually going to be able to get the output result from my specific function output and save it back to a variable basically. So if I go and run this now, and if we go and run `spacesuit_welcome_with_return`, and we're going to pass through our name as per usual, so `elon musk`, `apple i i ship`, and we're not going to set `allergies` or `color`. So if I go and run this now, you can see that it's gone and returned this value, right? So kind of similar to the `print` statement, but you can see that we've still got our quotes, so we're not actually printing stuff out, we're just outputting the return value. Now say, for example, we wanted to actually use that statement for something, right? So say you were building an API and you wanted to output this or return this as part of your API, we can actually store this value inside of a variable now because we're using the `return` statement. So if I go and save it, and so we're going to call it `welcome`, and by doing this our function is actually returning a value, which means that we can store it inside of a variable. So again, no different to how we define different variables. So I've just written `welcome =`, and then I've set it equal to our function because our function is going to return something, right? So if I go and `print welcome` now, that actually stores our value that we're returning. So again, that's a core difference between when to use `print` versus when to return something. So again we'll delve into `return` when we do a little bit more on object-oriented programming, because it is super, super important. Then now let's actually add some comments. So what do we take a look at here? So we had multiple keyword arguments in this cell. `# Keyword arguments`. And then over here we had a set up a return statement. Cool. And again, so the defining characteristic of a `return` is that it's going to return or bring back that value from that particular function, so we can tend to use it inside of a different application or store it inside of a variable. So again, this is an example, `# Example of storing the results of the function inside a variable`. Cool.

Now there's a one last thing that I want to show you. So let's quickly recap on everything we've done so far. So we have gone through defining basic functions, so think of it like a recipe. We've then gone and taken a look at positional arguments. We've then gone and taken a look at how we can define multiple positional arguments, and remember position is important. We then took a look at how we can use the `enumerate` function to get the position and the value from a list. We set up a function that uses keyword arguments. We also set up a function that uses multiple keyword arguments. We then also went and used a `return` statement. So this allows us to bring back a value from a function and assign it to a variable, and this is particularly useful when you're repeating results. There is one last type of function that I want to show you, and this is called a lambda function. So this is also known as an anonymous function. So we're going to create a lambda, and the definition of a lambda function is a little bit—it looks a little bit weird—it's very Pythonic, but it makes your life uh can be really, really useful, particularly when you're working with Pandas or you just need a quick function. So we are going to create a function that takes a value, and let's say we want to multiply it by pi, or an approximation of pi, 3.14. Let's go and do this. So `pi`. We're going to define a new variable, and we're going to set it equal to `lambda`, and then we're going to define the active variable that we're going to be using or the argument, so we're going to set it equal to `x`, and then we're going to return `x * 3.14`. Now I know that looks a little bit weird, but that is a function in a single line. So what we can then go ahead and do is run `pi`, and then pass through a value, `2.`, and you can see that we've now gone and multiplied two by three point one four. Just to prove to you it's the same value: `3.14 * 2 = 6.28`. Pretty cool, right? So that is a shortened way, also known as an anonymous function, that's a shorter way to go and define…

What's happening, guys? Welcome to the live stream. In this video, we're going to be going through a bunch of Python stuff, but specifically, we're going to be handling object-oriented programming and specifically classes, class inheritance. We'll also do a little bit on how we can define attributes and methods as well.

So specifically, what are we going to be covering? Well, we are going to be going through how to cover classes and specifically how to build custom classes if you need to. These classes come really, really in handy, particularly when you're doing some really hardcore deep learning stuff. So I've been doing a lot with the TensorFlow functional API, and what I've actually noticed is that in order to build really specific models or really slightly more advanced models, you're going to want to have a handle on classes. We'll also take a look at how we can deal with attributes and how we can attach methods to our classes. And then one of my favorite topics is class inheritance. So this is how we can build superclasses or subclasses from existing classes. And again, this is a common theme that you'll actually pick up when you start to use specific deep learning models like PyTorch or TensorFlow as well.

So on that note, let's actually get to it. Okay, so let's go on ahead, and we are going to start writing some code. So as per usual, what we're going to be doing is we're going to open up a Jupyter Notebook that we've previously used. So I'm going to step into what are we stepping into? Our command prompt. So I'm going to open up command prompt, and then I'm going to go to where my Jupyter Notebook is. So this Jupyter Notebook could pretty much be wherever, but we need to obviously go to that specific location and be able to open it up. So let's go do that. So I'm going to type in or go to a directory. So let's actually take a look at where that is first up. So I'm just going to bring it up over here, and we so my stuff is inside of YouTube and then Python basic. So we need to go to that location. So first up, what we need to do is go to our D drive, and then everything is inside of a folder called YouTube. And again, this could be in a different location based on where you're doing it, so that's perfectly fine; just go to where your existing Jupyter Notebook is and open it up.

So we're going to go to our location. So I'm going to go to `cd` and then YouTube and then go into Python basics because that's where we've been doing all of our Python for data science work. So let's go there. So `cd` nope `cd` Python basics, and then we're going to kick Jupyter off. So again, how do we start Jupyter? We write `jupyter notebook` from our command prompt. And what have I written there? `jupyter notebook`, my bad, `jupyter notebook`, not `jupyter notebooks`. Okay, then I can minimize that, and then we're going to bring it over here. So remember when we're working with our Jupyter Notebooks, we've got the extension `.ipynb`, so we can open this up, and this is all the stuff that we've covered in our previous tutorials already. So we've gone through a bunch of stuff: data types, lists, dictionaries, loops, conditions, functions. So we'll actually cover functions a little bit in here, but specifically methods which are attached to our classes.

So as per usual, we're going to start this off by creating a new section because we want to keep our things nice and structured. So we are going to create a new section. So I'm going to create—so remember to convert a cell into a markdown cell. So right now it's active, so you can see it there; it's green. Let's actually add some additional cells below so I can get this a little bit more centered. So I'm going to hit `b`, `b`, `b`, `b`; get a little bit more centered in the screen. And then right now it's active, so you can see that it's green. So how do we get out of this? We hit Escape on our keyboard, and then we want to convert it from a code cell to a markdown cell. So we're going to hit `m`, and that is a markdown cell. Now this section is going to be called Classes because class is now in session. All right, so that's Classes.

Now the class that we're going to create is we're first up going to create—and continuing with our sort of space theme—we're going to create a class for a person. Now we've already encountered classes a couple of times, so specifically when we're going through—what do we use classes for already?—well, a string is a—well, a string is a data type, but I'm trying to think if we've actually encountered any classes. We might not have yet. Okay, we're going to create a class. So the class that we're going to be creating—well, let's actually talk about what classes are first of all. So classes are objects with defined properties and methods. Now properties could be an attribute. So, for example, a string—a string had different methods attached to it, and it had different attributes. So we're going to create a reproducible class, and this is like almost think of it like a boilerplate for an object. So again, if you think of your classes—classes as boilerplates or objects—right? And the nice thing about classes is that you can inject different properties into them. So say, for example, you're going to be using this for deep learning—one example is you might be creating a new custom layer for a specific deep learning model—what you might want to do is inject a specific number of layers or ensure that it has a specific type of activation functions. Classes are perfect for this because you have a shell or a boilerplate or a template which you can just plug in a bunch of values and have it set up the way you want. So what's a really good example of a class? Well, a great example, which we'll also build upon when we go to use inheritance, is a person. So think about what a person is, right? Really, we're a template, and all that's been injected into us is different types of DNA to be able to have a unique individual, but really, in our fundamental—or in its fundamental sense—we are—we could be formed as a class. So let's actually create a class for a person.

So what we're going to do is first up write `class`, and this is the statement that we need to pass through to specify the fact that we're creating a class, and then we're going to call it `Person`, and then close it with parentheses and hit colon, and I'm just going to add in a few spaces. So again, as per Python syntax, in order to write anything under our class, we need to hit Tab and indent it. So in this case, that indenting looks a little bit weird, so that it's now indented. So what have we wrote—written so far? So I'm pretty lightweight at the moment. I've written `class` and then `Person`, and you can see here that I've written it in capital. So again, this is um just a preference; I like writing it in cap so I know what is actually a class, and then I've gone and written—what have we gone—we've put in parentheses and then a colon. So then what we can do is define a couple of things. Now the first thing that you need to define whenever you're defining a class is a method or a function called `__init__`. So if we type in `def __init__(`, and then parentheses and then colon, this is the method—the method that runs as soon as you—you create a class. So as soon as we start using our template, when we first run it, the `__init__` method is going to run. Now you're probably thinking, "Hey, Nick, this looks really, really similar to a function, right?" So we covered functions already; why—why are you calling this method? The methods that we're defining here or the function that I'm actually defining here is a specific function which is attached to a class; that's why we call it a method. Now classes are typically combined of two key things: you're going to have methods attached to them, and these are like functions that are attached to a specific class, and then they'll also have attributes or properties. So these are values that are attached to a specific class. Now I'll show you how to get these out and how to use methods and how to get properties or values, but let's actually go on a little bit further.

Now when we are defining our class, the first thing that we need to do is pass through the variable `self`. This is basically telling us that we are going to pass through this instance of this class through this `__init__` function. Now `self` is a little bit of a weird concept, but you got to keep in mind that when you're creating a class, you're creating a new instance of an object, so you want it some way to refer to this particular object, and that's where the `self` keyword comes in. So when we use `self`, we're basically saying, "Hey, we are using the current methods for this instance of this class," in this particular case. All right, so we've written `def` and then `__init__` and then `__init__`, and then inside of parentheses we've passed through our first argument, which in this particular case is going to be `self`. Then we want to pass through a couple of different parameters to our `Person` because remember a `Person`—or in this particular case, a `Person` class—is like a template. So what are some unique characteristics for people? Well, we're going to pass through `name`; we're also going to pass through their age, and we're also going to pass through their favorite color; we'll just call it `color` in this case. So right now this is pretty much a function, but it's a specific type of function which is a method, and it's a specific method which is attached to a class called `__init__`, and remember `__init__`, so it's short for initialize or initialization—initialize—it's going to run as soon as you go and create your new instance of your class. Cool. All right, so now what we need to do is we need to take these parameters that we've gone and defined here—so `name`, `age`, and `color`—and we need to assign it to our class because right now these are just arguments that we're going to be passing through to our `__init__` function. So how do we set these up? Well, what we're going to do is we're going to assign them as attributes. So I'm going to create a new comment and say, "We are going to create some attributes for our `Person` class," and the way to create an attribute is to specify `self` and then whatever that you want—that name of that attribute to be. So in this case, we'll specify `self.name`, and then we'll set it equal to the argument that we're getting from our `__init__` method, so we're going to set it equal to `name`. And then we're going to create another attribute, so `self.age`, and we're going to set that equal to `age`. And then we're going to create a last one: `self.color`; we're going to set that equal to `color`. Right, so that is—if we did nothing else—that is a class now initialized—or that is a class now defined, not initialized. So if I go and run that cell, you can see that it's gone and run. Now if I wanted to go and create an instance of this class because remember our class is like our boilerplate or our template, how do we actually go and create a—an actual person now? So we can go and say `new_person`, so we're going to create a variable, and we are going to assign it—so it equals our `Person` class. So we're going to type in `Person`, and then we need to pass through a couple of key arguments, and these are the arguments that the `__init__` function is expecting. So we don't need to pass through anything for `self` because our class is aware of itself, so we just need to pass through our `name`, our `age`, and our `color`. So in this case, let's go and do—I don't know—Elon, so Elon Musk; pass through our age, and we're going to say—I don't know—I don't know how old he is. Let's say—let's keep it on the low side, so I don't—don't make him feel bad—we'll say 38, I don't know, and then his favorite color is blue. Right, so that is a new person now created. Now remember, a `Person` is like a template, so what we can actually do with this template is we can actually go and extract these attributes—remember `name`, `age`, and `color`. So if I go and type in `new_person.name`, that is me accessing an attribute. Let's add some commentary: so accessing a class attribute. And you can see there that I'm actually accessing that person's name. So `new_person.name` is giving me the name for that particular person. Right now we can also access our age, and we can also access our color just by changing the value after the dot. Pretty cool, right? Now the beauty of this is that it is repeatable, right? So if we wanted to go and add additional attributes, or if we wanted to go and add additional parameters, we could definitely do that as well. In this case, we've created a really simple one. So now what are we going to do? So that is our baseline method. So we've gone and defined our `__init__` function, and again, this is required when you go and create a class, so you need to have an `__init__` function in order to set up your variables. The next thing that we might want to do is—let's say, for example—calculate the year of birth, right? And again, we're going to do a really simple calculation. So what we can do is we can actually create custom methods inside of our class. So we can go back into our `Person` class, and now what I'm going to do is create another method. So let's add a comment: and we're going to create a method to calculate that date of birth. So create—oh, let's actually say date of birth method. And we are going to—again, we're going to define a function, so `def`, and then this time we don't need to specify `__init__`; this is a built-in function for classes; we don't need to do it for our other methods. So what are we going to do? We're going to call it `year_of_birth`, and then we just need to pass through `self`, so that this function—or this method here—has access to these attributes here because we're going to need this `age` value, right? And then finish off our function with a colon, and then what we want to do is we want to return the year of birth. So we're going to specify `return`, and then we're going to keep it pretty simple and not fully parameterized in this particular case, so we're going to return—what are we doing?—so `2021 - self.age`. Right, so let's actually take a look at what we've written here. So we've gone and defined a new method, and remember it's a method because it's inside of a class, and our new method is called `year_of_birth`, and then to that we're going to be passing through our current instance of our class—remember this allows us to access our attributes from within different methods—and then we've gone and specified that this function is going to be really simple; we're just going to return `2021` minus the person's age, and remember this `age` is an attribute that we set up originally. So if we go and hit Shift+Enter, now that is our `Person` value—oh, that is our `Person` class now updated. So what can we do now? Now we can actually go and calculate their year of birth, and remember—so we haven't actually gone and passed through any additional arguments to this, right? So if we go and run this method now, we're going to get our—we should get our year of birth back. So if we go and run it on Elon again—so I'm just going to recreate it—hit Shift+Enter, and then for our `new_person`, we can now go and run our method that we just went and created. So I'm going to type in `new`—let's actually create a comment—so we are going to run a method. So if I type `new_person.`, and then this is the difference between using or accessing an attribute versus actually running a method. So when we run a method, we have to close it inside of parentheses. So we're going to pass through `year_of_birth` and then add parentheses, and then if I hit Shift+Enter, we are now getting our year of birth. So again, this is actually doing something now, so we're not just accessing an attribute; we're actually performing a calculation on some sort of value. So again, this could be repeatable. Another great application of this is—say you were—say you're doing something with layers—like neural network layers—actually, what's a better one? Say you were wanted to consistently create a Pandas class, right, or create a specific data set for your model, you might actually set up your data set as a class and actually have specific attributes and specific methods attached to that. So say, for example, you knew that you consistently needed to bring in your data set from some random source, you then needed to go and pre-process it, but not always, you then wanted to go and push it out somewhere, right? You would actually set up this class as—say, for example—`class` and then `DataSet`. Inside of your initialization function, you might choose to set your attributes, and then you might also choose to go and bring in your data from wherever it might be. You might then choose to have another method which is—say, for example—`export_data_set` or `import_data_set`. So you could actually structure this so you've got a repeatable set of steps that you can then go and run now.

So what have we gone and done there? So we've gone and defined a class, and this allows us to work with object-oriented programming inside of Python. We've gone through how we can use the `__init__` method to set up our initial set of values there, and we've also gone and defined our initial method. Now we can also define different methods—to say, for example, we wanted to pass values to our method; we can do that as well. So what are we going to do this one? Say, for example, rather than just calculating year of birth—say, for example, we wanted to project how old they were going to be—let's actually create a method for this. So we're going to add in a comment: a projected age. And we're—again—going to define a method, so `def`, and then I'm going to type in `projected_age`. So this is going to be the name of our method, and remember we're going to be able to access that—similar

To this time, however, we're again going to pass through self, but we're also going to take in a value for our projected age. So we are going to pass through a keyword argument. And remember, there's a difference between arguments and keyword arguments, or positional arguments and keyword arguments. Positional arguments—order matters, and they need to go before your keyword arguments. Keyword arguments have a predefined value, and they can go in any order as long as they're after positional arguments. So we are going to say, um, I don't know what's—how—let's add in a new argument; we're going to set it equal to years and five. So this is basically going to be the default. So if we type in project_age, this is going to take the person's age and add on five. Right? So we're going to create a new keyword argument called years and set the default equals to five. And then what we're going to do is we are going to return self.age + years. Yes. And so this gives us the ability to pass through additional values and again pass through parameters to our specific class.

So if we go and run this now, we need a colon there. Let's actually take a look at that error. So basically, we've got a syntax error, and it's saying invalid syntax, and it's on line 15. So 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15. So we've got an error on this line, and it's pointing towards the end, and that is because we don't have a colon there. And if we shift enter, all good, no issues. Now let's go and create our class again, but this time we're going to use a method that we are passing a value to. So in this case, our default is going to be 5, but we can also pass through our own value if we want to or project a different age range. So if we go and hit shift enter on our new class, so we've got our color, we've got our year of birth, now let's actually run our project age method. So if I type new_person.project_age, you can see that it's projecting a person's age as 43, and this is basically saying that in five years, Elon is going to be 43. Now we can pass through our own values to say, for example, we said years equals 10. You can see that we're now passing through a new keyword value or new keyword argument to go and project our age at a little bit further. And again, we can add in a comment because it's good practice, guys—comment your code. So we're going to say, "Run a method with a keyword argument." Pretty cool, right?

And so again, this is really, really important because you'll often encounter classes with methods and with attributes, and it's important to know how to access an attribute versus how to actually go ahead and run a method. And this makes it super easy once you've got a good understanding of this—the world's really your oyster when it comes to Python. So getting this down pat makes your life a whole lot easier. So that, in a nutshell, is how to create a class. So let's take a look at what we've done. So we went and created a class, and our class—in order to define it, we just need to write class and then the name of the class. We then went and defined an initialization method, so we wrote def __init__ and then pass through self because we want to refer to our current instance of our class, or this particular instance of our template, pass through a bunch of positional arguments, and we went and set those as attributes. We then went and defined a method, and remember a method is a function attached to a class, and this particular method is called year_of_birth, and again we're passing through our self because we want to be able to reference this current instance of the class, and that one's just returning a person's age to 2021 minus the person's age from our class. And remember, we can access our attributes from within our class as well. So remember, self.age is going to be able to get this value up here. And then we also created a new method called project_age, and this one is taking through a keyword argument, and remember we need to pass through self—that's our first positional argument that we need to be passing through—and then we're going and specifying a keyword argument, which in this case is years. And so this gives us the ability to do this and be able to pass through additional values to our methods when we're actually going ahead and creating our classes. To create a new class, we need to define that particular class, or in this case, we need to specify that we're creating an instance of that class, a person, and then we pass through the positional arguments that are required inside of the initialization function. So in this case, we're passing through name Elon, age 38, favorite color blue. Then we can access our class attributes. So again, we can type in the instance of that person, or the instance of that class—remember, the instance of the class that we just went and created is called new_person. So to get our attribute, we need to pass through the attribute value, and remember it's separated by a dot. So new_person. and then you can—the words yours—grab whatever you want. So in this case, we're grabbing color, but we could also easily grab the name, grab the age. So that's the way to access your attributes, and we can also access all of the methods. So we can type in the instance of that class, a new_person, and then we can run that specific method. So again, separated by a dot, and then we call the method, so year_of_birth, and then pass through a set of parentheses to get our person's year of birth. Then we can also specify our methods and pass through keyword arguments to those. All right, I think we're doing pretty well, so let's take a quick break and let's have a look to see who's talking, who's here. This is a really good question. So we just got a question: So why use um self.age over here instead of just using age? Keep in mind that this variable is going to be available within our initialization function. We want this age value to be available across the board. So by setting it through as an attribute, we're establishing good practices, and we're also ensuring that we are referring to the variable that we've defined as part of our class. So using self.age is going to mean that we're grabbing the specific variable that is attached to that class.

Alrighty, we are going to keep powering through. So we've now gone and defined our class; I'm not going to go through it again because we've done it. The next thing that we are going to go ahead and do is perform something called inheritance. So this is really, really important. So inheritance should probably be a separate section, but we'll include it still here. Say, for example, we want to extend our class, right? So in this particular case, our class is a person. What happens if we had specific types of people that we wanted to go and define? Let's say we had a Formula One driver; they might have some additional attributes—say, for example, they might have a specific license that they have; they might have specific cars that they've driven. In our particular case, what happens if we wanted to define a class for an astronaut? Well, we've already gone to all the hard work to actually go and define a person, so why can't we just take all the attributes and methods that are attached to a person and attach those to an astronaut, but then we just add some extra stuff to our astronaut to build it out? Doing this is actually known as inheritance. So basically, we can take an existing class and take all of the good stuff inside that class and just add in the extra things that we need. So this means that we don't have to go and redefine the entire class every single time, and when we go and update our base class, which in this case is our parent class, which is person, these are going to flow through to—eventually will flow through to—our child class. So in this particular case, that's an important thing to note. So this is going to be a parent class, and what we're going to do is we're going to inherit—or our child class is going to inherit all of these attributes and values. The important thing to note: So a parent is the person or is the class passing down attributes and methods, and your—let's add in another comment there—and your child class is the class inheriting the methods and attributes. So this is really, really useful when you have frameworks that you're using that you want to sort of base on. So if you actually go through to the temp—this is where I actually use this this week—if you actually go to the TensorFlow functional API documentation for deep learning, when you're actually going to find custom layers or custom models, what you actually do is you use inheritance to access all of the attributes and the methods that are available to existing TensorFlow models and TensorFlow layers. So you might think, Nick, I'm probably never going to use this—trust me, now you will definitely use this, or you'll at least see it at some point as you go through your data science journey. So understanding inheritance is super useful. So what we're going to do is we're going to step into our cell; we're going to give us a little bit more room to breathe. So I'm just going to hit shift enter, and actually let's convert this cell to markdown. So we've already got classes; let's actually do inheritance. So I'm going to hit escape out of this, hit m, and I'm going to get rid of these pound symbols, just hit -- so we can have some dot points, and we don't need this in caps because that's aggressive in hairy tense, and this is going to be section six. Cool.

All righty. So now what are we going to do? So we're going to create our astronaut class, and our astronaut is going to be our parent class, so—or sorry, astronaut is going to be our child class. So this is going to equal astro nor, and our parent class is going to be person. So this means that we don't need to go and redefine all of these parameters—name, age, color. Our child class is going to be automatically able to inherit from that particular class. So again, we define it as per usual, so we're going to create a class, and we are going to call it astronaut. So class astronaut, but this time what we're going to do is we're going to pass through our parent class, which in this case is person, and then we're going to hit colon. So that's the main difference. So now we're going to be inheriting from the person class, which is this class over here. Then we're going to create a new line, and again, as per usual, we're going to define our init function—initialization function—so def __init__, and then this time we are going to again pass through self, self, but we're also going to pass through a couple of additional values. Now, key thing to note is that we still need to pass through the values that our in child or that our parent class is expecting, so name, age, and color. So let's first define those—the name, age, color—but now what we can also do is we can also pass through some additional arguments. So we'll actually define—um, what's a good one—so how long the astronaut's mission is going to be, so the emission length, for example, submission length, and we'll do it in months, in months, and then close our init function. And then what we're going to do is we're first up going to set up a class so that it inherits from our person. So to do this, we actually use the super method, so super and then .init, and then we're going to pass through our name, our age, and our color. So what this is actually doing is this is actually telling our child class—in this particular case, our astronaut—to inherit all the stuff from person so that we then have name, age, and color available as attributes. This is inheritance in a nutshell. So this—if you take nothing else away from this—this is what gives us inheritance. Super method—just remember Superman—that's how I've got it in my memory path. If you haven't checked out that video, do check that out, but the visual cue that I've got to remember that is just remembering Superman doing random stuff. So super allows us to inherit from our parent classes. Now the next thing that we're going to do is create a another attribute. So we're going to save our mission length in months as an attribute, so self.mission_length, and then we'll say months equal to this value here. Cool. So that is our astronaut class now defined. So what we can then do is create a new astronaut. So we'll say new_astronaut, and again to define our class, we'll create our class; we're just going to type it out, and then we're going to pass through all of the positional arguments that we need or as defined by init function. So we're going to pass through—um, I don't know—let's say I'm an astronaut, so Nick, my age, let's say 99, let's say our color—have unknown purple—and mission length in months, let's say I'm going away for a while—uh, 48 months. Right? I'll just stream from space. What we can then do is access all of the existing attributes as well as methods. So if I type in new_astronaut., you can see that I've actually got all of the same attributes—let me bring that a little bit further down—I've actually got all of the attributes that are were attached to my person class now attached to my astronaut. So remember, we didn't actually go and define the age—the age attribute or the—what were the other methods—or the project_age or the year_of_birth methods; we actually just inherited those from our person class. This is inheritance in a nutshell. So this means that we don't need to go and redefine old—so that's our age method, bit of a shame in it—that's our h method, that's our name method—oh, that's our name attribute—um, and then what else do we have? Color. So you can see that we can actually access all of these attributes from our parent class, which makes it a whole heap easier. We don't need to go and define all of these custom classes every single time. So again, I've used this a ton of times, and you'll probably use it when you do get to this—specifically for deep learning—when we're inheriting different components from layers or inheriting different components from other models. This makes your life a ton easier. Now, again, we could also create a specific method. So again, let's actually just go through, so we can use color, and so this is accessing—let's say access parent—like e-a-r-e-n-t—parent-like attribute—uh, and then we're gonna specify our method, so new_astronaut., and let's—uh—what's a year of birth? That means I was technically born in 1922 based on my age being 99. Now we could also go and extend out our method. So let's go on ahead and do that. So we are going to add—what are we going to do?—let's actually calculate their age on return. So how old would I actually be in return? So we're going to create a new method called age—or def—let's actually add that comment first—a method for calculating age on return. I'm going to specify new methods—a def age_on_return and pass through self, and we are going to—again, we don't actually need to pass anything through because we already have our mission length in months and we already have our age. So what we are going to do is we are going to project our age, right? So this is the cool things about this is that we don't actually need to go and create a full new function to actually do this; we can actually use our existing project_on_age or project_age method that we had from up here. So I can type in return self.project_age. So this gives us the ability to access the methods from our parent class as well, because remember we've gone and used our super function or super—what is that—super function to be able to go and inherit from our parent class. So by typing in self.project_age, I'm actually accessing this method over here from our parent class. Then what I can do is pass through—um—what do we need? So self.mission_length in—what did we call it?—mission_length_month, and then divided that by 12 because right now our project_age method is expecting the number of years to be passed through. Actually, we need to specify this—this as years equals. So let's go on ahead and test that out. Let's make sure we've done that right. Project_age, yes, okay, cool. So let's actually test out that method now. So if we type in—uh—let's create it again—boom—new_astronaut.age_on_return—yup—so we've got an error there, and that's just me typing way too fast. So you can see it's on line one, and it's saying syntax error, unmatched parentheses. That's because we need one of these beforehand, and delete that one, and you can see that is projecting our age on return. So you can see there that we've returned 103.00. Now remember, right at the start we said that we could do something called typecasting. So how might we actually convert this into an integer rather than a float? Well, we can go on ahead and wrap it—well, we—let's wrap this—so we can wrap the value that we're passing through, so int, and if we can run it again, now you can see that we're just returning 103. So that, in a nutshell, is inheritance. So we've gone through that reasonably quickly, but let's go ahead and take a step back and take a look at what we did. So we went and created a child class. Remember, inheritance allows a child class to inherit a number of attributes as well as methods from a parent class. In this particular case, our parent class is our person, and our child class is our astronaut. So basically, our astronaut class is going to be inheriting our age attribute and a bunch of the methods that we define as part of our person class. Then to define a child class, we can type in class again, so as per usual, but then we're going to pass through the parent class that we want to inherit stuff from, and again this just makes it easier and reproducible; it also means that you have less code. So a big thing about writing code is that you want to write stuff that's going to be as modular and as reproducible as possible. So this is really where classes come in handy. What was I saying? So I've passed through the person to our child class, and then we've gone and again defined our initialization function over here. We've then gone and defined a super method or a super function, which allows us to inherit from our parent class, and we need to pass through the arguments that we want to use in that particular case, and then we're going and passing through—creating a new attribute—in this case, mission_length_months—and we're going and extending out one of our functions. So we're actually accessing one of the methods from our parent class and doing a little bit of—and we're actually using this or attaching that particular method to another method. So our age_on_return method is actually just using our project_age method from our person class, which is up here. So again, that gives us the ability to not have to write this all over. And then when we're going and defining our trial class again, it's exactly the same. So when you actually go and initialize it, it's going to look no different to you actually just using the base class, apart from the fact that you need to pass through all of the new positional arguments that you would have had to inside of the parent class as well as the child class. So in this case, these classes or these arguments over here—these are actually arguments that are required for the parent class. Remember, this is our name, our age, and a color, and this is required by a person class—a name, age, and color—but we've also tacked on the mission_length_in_months up here as well. Then again, we can access the attributes, and we can also access the methods—again, comment your code—access the methods, and then run a method. And that, in a nutshell, is classes. So we've gone through a bunch of stuff. So we specifically went through how we can define a class, and remember you need to use the class statement and then

You need to have an initialization method to be able to go and set up your attributes. You can then go and attach a number of methods. We also took a look at inheritance, and this basically makes our code more reproducible and more modular, so we can go and use it over and over and over again. So we could also go and create a new child class called, I don't know, Formula One driver, or lawyer, or accountant, and have specific attributes and specific methods that are attached to that particular child class. Move this out of the way. So again, thanks so much for tuning in, guys. Hopefully you enjoyed the video. If you did, be sure to give it a big thumbs up, hit subscribe, and tick that bell. And again, let me know if there's anything else you'd like to see. Hopefully you've enjoyed this tutorial. Thanks again for tuning in. Peace.

What's happening, guys? Welcome to the next episode in the Python for Data Science series. So we're going to be going through a bunch of stuff today, but specifically we are going to be going through modules and how to work with them. So modules give you the ability to modularize your code and store it inside of a separate file and bring it into your Jupyter Notebook. So we'll take a look at how to do that today. We will also be touching on packages. So packages open up a whole wide world of opportunity when it comes to doing data science with Python. And then what we're also going to be doing is we're actually going to be using a specific package called the Request package. So the Request package actually allows you to make API calls to just about anything around the web. So we'll actually test this out on a couple of my favorite APIs that we can actually access relatively easily. So on that note, let's actually kick this thing off.

So the first thing that we're going to do is we're going to start out, as per usual, we're going to go on ahead and open up our Jupyter Notebook. And so I'm going to navigate to where my Jupyter Notebook currently is. So we can just type in cmd and remember our Jupyter Notebook that we've been working with so far is inside of a folder called, I believe it was Python Basic. So let me open this up and show you where it is. Radio, there it is. Let's bring that over there. Cool. All right. So you can see that inside of our folder we've got a Jupyter Notebook called pythonbasics.ipynb. So what we can do is we can go into that and open it up as per usual. So we're going to cd into there: cd python basics. Uh, we need to go into our D drive first: D drive, cd youtube, cd python basics. And if we take a look inside that folder, we can see that we do in fact have our Jupyter Notebook there: so python basics.ipynb. So what we're going to do is we're going to start up Jupyter. So we can do that by typing in jupyter notebook, and this will open up Jupyter Notebook. You can see we've got that there. Let's zoom in to make it a little bit easier to see. And if we open up our Jupyter Notebook, we're going to have a bunch of stuff available inside of this. So we are now up to our next section. So we're going to scroll on down. And again, all of this code is going to be available once I finally get it together, so you'll actually be able to pick this up and use it all yourself.

So what are we going to do? We're going to add a couple of extra cells. Now remember, to get outside of a cell inside of Jupyter, we just hit escape to convert it from green to blue. Add in a bunch of additional cells by hitting b on our keyboard. Remember, b adds cells below our current cell. So if we wanted to add cells above, we can select a different cell and hit a, and that will add a cell into or above the current active cell. To delete that cell, we can hit dd to delete that. Now, as per usual, what we've been doing in this series is we've been starting out each section with a separate markdown cell. So to do that again, we're going to hit escape on our keyboard and convert this cell here to m, or markdown. So I'm going to hit m on my keyboard, and you can see it's converted it to markdown over there. Now we are going to name this section: What are we naming this? We are going to call it Modules. What are we currently up to? Up to Set 6. So we're going to call this Section 7: Modules. All right. Now I want you, I want you to put yourself in the hot seat as a data scientist. So say you've written a bunch of code, you're using it often, or you're using that exact same code often and often, and you don't want to have to go and write it again inside of your Jupyter Notebook. It'd be so much easier if you could just import this or inside from another file so that you can use it just about anywhere. Well, this is where modules come in handy, right? We can actually import code that we have inside of another file into our existing Python script or into a Jupyter Notebook.

So the first thing that we want to go ahead and do is actually create a separate Python script. So let's go ahead and do this. I'm going to create a new text file. Can we save this? Uh, we can't say this is Jupyter. Let's actually do it inside of VS Code. So let's delete this. Let's go and open up VS Code or edit your favorite code editor. So I'm going to open up VS Code in this particular case, and what I'm going to do is I'm going to create a new Python script. So we're going to hit—let's make this a bit bigger—so I'm going to hit File, New File, and we are going to call this—so let's actually save it now. We're going to save it in the same folder that we're currently working in. So I'm going to go into youtube, python basics, and I'm going to call this uh helpers, right? So we're going to call our file helpers.py, and we are going to save it as a Python script, so down here, right? So what we're doing is we're creating an empty Python script, and we're calling it helpers, and we're saving it as a Python script. So if I hit save, now you can see that we've got this little Python symbol up here, and it's called helpers.py. Now what we're going to do is we're going to create a function in here. So say, for example, you write a bunch of code, this is code that you use often and often and often again, and you want to be able to bring this inside of your Jupyter Notebook. So we're going to store some helpers. Now let's just say that we had like a unique number that we wanted to set as our random number inside of our Python or Jupyter script. What we could do is we could create a function. Again, we create a function by typing def, and then we can name it anything. So this function is going to store our unique launch codes for our space shuttle again. So we're going to call it launch_codes, and then this particular function is just going to return a number. So we're going to return 123456789. Super secure, right? And we're going to comment this return function to return launch codes, right? So what we've gone and read in there is the inside of our helpers.py file. We've written def to define a new function, we've then called that function launch_codes, and then we've set a set of parentheses, so we're not passing any positional or keyword arguments. We've then passed through a colon, and this function is going to return the number 123456789. So this is an integer in this particular case. We can then save this. So right now we haven't actually done anything with this helper code, right? So we've just got this blank piece of code, but we can't actually do anything with it. But what we can actually do is we can actually bring this into our Jupyter Notebook. So how might we go about doing this? I'm going to close this for now, and we're going to open up our Jupyter Notebook again, and you can see that helpers.py is now showing up in our same folder structure inside of Jupyter. So I'm going to step back into our Jupyter Notebook, and then what we can actually do is we can actually import that launch_codes function into our Jupyter Notebook. So let's go ahead and do this. So I'm going to create a new comment: so import launch_codes helper or launch_codes helper, yeah, that sounds about right. And then we can actually import this into our Jupyter Notebook. So let's go ahead and do that. So I can write from helpers, because that's the name of our file, import, and then if I hit tab, it's going to show what available functions I've got to import into my Jupyter Notebook. So this will show functions, classes, whatever you've got inside of that file. So in this case, I can hit launch_codes, and this has actually allowed us to import our helper module and specifically one function from our helper module into our Jupyter Notebook. What we can then do is run the launch_codes method, so launch_codes function actually, and you can see that we are now returning a unique launch code number. So again, this just makes it really, really useful, particularly when you're building up huge data science workloads. Having specific modules or helpers stored inside of separate files, it makes your life a ton easier rather than doing everything inside of one massive notebook. And again, notebooks are really flexible, so you can definitely do it in a single notebook, but having some of your helpers outside in modules definitely helps.

So let's quickly recap on what we did there. So we went and authored our helpers.py file inside of VS Code, and we created a function called launch_codes, which returns the value 123456789. And then, in order to import that into our Jupyter Notebook, we can just write from helpers, because that's the name of our file, import launch_codes, because that's the name of our function, and then we can just go and run that function as per usual. So that, in a nutshell, is modules. Now done. Now you're probably thinking, Nick, that's great, but what if I don't want to have to go and write every single module, every single function that I want to go and use? There's just way too many. Well, this is where the Python package repository comes in handy. So if you actually go to pypi.org, so PyPI, you will find that there are a ton of Python packages that are available for you to pick up that you can actually access from the global repository. So this is the collective knowledge of all the amazing Python developers out there that you can tap into and get started using. So in this particular case, or let's actually take a look at a couple. So some of the most popular ones when it comes to data science are pandas. So if you type in pandas, you can see that we've got pandas from here. Another one that's really, really popular is TensorFlow. So you can see we can download TensorFlow from here. What else is there? So there's NumPy. So NumPy allows you—so pandas, let's actually take a step back. So pandas allows you to work with tabular data really, really easily. TensorFlow allows you to build deep learning models. Another super popular one is NumPy, which allows you to work with array functions or work with—so we've talked about lists quite a fair bit—so NumPy allows you to work with specific NumPy arrays. They're sort of like lists, but they're wrapped up inside of our NumPy classes. Another super popular one, which we're actually going to use, is called Requests, and Requests actually allows you to make API calls. So when you think about using API calls out in the wild, so sometimes when you're a data scientist or when you're working data analysis type projects, you might need to grab some data via an API. Well, the Request library is going to make your life a ton easier, and it's actually reasonably straightforward to be able to use an or call an API via Request.

Now, if you haven't encountered APIs before, if you don't know what an API is, it stands for Application Programming Interface. Now the best way to explain what this is is that it's a remote URL, so sort of like http://google.com, it's a remote URL that allows you to call it and return a specific value. So one of or a couple of my favorite URLs are—what were they again? I had them written here—so icanhazdadjoke.com, icanhaz—what is it? Add joke. So this is an API. Again, it's not super data science specific, but I sort of wanted to make this a little bit fun. So this actually is an API that allows you to return or fetches a random joke from a variety of formats. Another one, which is a little bit more data-centric, is an API that actually allows you to get the location of the International Space Station at any point in time. So the API for that is where the ISS.at, and so there's actually a huge API that sits behind this that allows you to track where the ISS is at a given point in time, which is pretty, pretty cool. So this is the main page, but there's actually a whole API that sits behind this that we can actually go and hit to be able to bring into our Jupyter Notebook and work with it. Now these are just two examples that I like taking a look at, but there's a whole wide world out there when it comes to APIs. If you wanted to access the Twitter API, if you wanted to access the Quandl API for finance data, if you wanted to look at the Reddit API, there's so many APIs that you can start messing around with.

All right, we're getting a little too ahead of ourselves. What we need to do is we actually need to start using the Request library to be able to call these APIs, and it just so happens that it actually tells you how you can install the Request package into your environment. In order to do that, so rather than importing like we did over here, what we actually need to do is first up, we need to install Requests, and in order to do that, we can use pip install requests inside of our Jupyter Notebook. But before we do that, let's add a new markdown section because we want our code to be documented and structured. So we're going to create a new cell, and we're going to convert it into a markdown cell. So I'm going to step out of it, hit markdown, then we're going to add a pound symbol, and we're going to call this—uh, what are we calling it? Let's just call it Working with Packages. Okay. Then the next thing that we need to do is actually go on ahead and install the Request library or the Request package that we saw from up here. So this method over here, this little bit of string that we can actually copy, so it's pip install requests. We—let's actually write ourselves—so we can write pip install requests, and as of right now, if I actually go and run this, this is going to throw—it should throw a bunch of errors. Oh, it doesn't look—looks like we do that without—what you should actually do is add an exclamation mark before this to indicate that you want to run this from a command prompt. So if I go and run this now, shouldn't throw too many errors. All right. So you can see that it's saying Requirement already satisfied: requests in blah blah blah blah. So this line over here is actually going to go on ahead and install that package into the environment that our Jupyter Notebook is currently running from. So you can see we're currently running from Python 3, so that's the environment. Now, by running !pip install requests, we've actually gone and made this available. Now, if you want to see all the packages that you've got available in your current environment, you can act—oh, this is actually something I should show you as well. So see how we've got this big block of text here, right? If we actually wanted to hide that, we can actually select this box over there, so you can see that right over there, and this will actually—actually allows you to hide that output. Another thing that you might want to do as well is, say you want to get rid of this, so you still want the code cell to have run, but you don't want to see all of this junk over here. What we can actually do is hit Cell from our menu bar up here, and then go to Current Output, and then go to Clear, and that's going to get rid of that stuff down there. Now what we can also do or what we should be doing next is actually importing Requests. So far we've installed it. Oh, wait, I was going to show you how to see all the different libraries or packages. So in order to see all the different packages that you've got installed in your current environment, you can hit !pip list, and this is going to list all of the different packages that you've currently got inside of your environment. Now your environment might look a little bit different to this in terms of what packages you've got available because I've been installing a bunch of stuff this week, but ideally you should see all of the different packages that you've got available in your environment. You will also see the package name plus the version that is currently installed. So if we go to—and what do we just install? Requests. Where are you? Somewhere down here… uh, my alphabet’s not… Requests. All right. So you can see that we've currently got Requests, and we've got version 2.25.1 currently installed. Alrighty. What are we going to do now? The next thing that we need to do, and again we're going to toggle this so we can hide it, the next thing that we're going to do is actually import Requests. So far we've installed it, but we haven't brought it into our notebook. So we need to bring it into our noble. So to do that, we can import Requests. So I just need to type in import requests, and again there's a whole heap of different packages that are out there. So again, I'm just showing you one; there's a ton that are out there that you might choose to work with. If you want to see some other examples, do check out some of the other videos on the channel. There's a huge number where we go and use a bunch of different libraries and specifically different packages. So I am going to hit Shift Enter to import Requests. So now we've got Requests inside of our notebook. Now let's add in some commentary. So we're importing—it's a little redundant here, but I'm just being pedantic—import requests into notebook. Then the—let's actually take a look at what we've got from Requests. So if I type in requests., this allows us to see all the different methods, classes, and functions that we've actually got available for our Request API. Now probably the most important one is going to be this get method. So requests.get actually allows us to make a GET HTTP request to a specific API. So I'm probably not going to go into too much detail about APIs, but you have these different things called or different types of methods to call an API. So some of the most common ones are GET, PUT, DELETE. I think there's an UPDATE as well. Let's actually take a look. So uh HTTP methods or HTTP request method. So there you go. So you've got GET, POST, PUT, DELETE, CONNECT, or PATCH was the one that I was looking at rather than UPDATE. So again, there's a whole bunch of different types, and I'll make this link available in the description. We are going to be using the GET request method to be able to hit our icanhazdadjoke.com API and our wed—weather—iss.at API, where the ISS at? Sorry, couldn't help myself there. All right. So now what we're going to do is let's set up some variables to hold our APIs. Oh, we haven't actually taken a look at our get function yet. So if I type in ?? because remember that's how to get the documentation for that method, you can see that this is saying it sends a GET request. The parameters that we need to pass through are the URL, and then we've got some optional parameters. So we can pass through a dictionary, a list of tuples, or bytes to send in the query string. We're not going to do too much on that… we can…

Do a bunch of additional stuff, but the main thing that you need to know is that it is expecting a URL; it's also expecting some parameters, and by default, it's set to none, so we don't need to pass those through. And it's also expecting some keyword arguments, which again are optional in this particular case. So that's sort of how to read documentation. Now what we're going to do first up is we're going to set up two variables for our URL. So remember, we're going to be doing one for icann has that joke API and where the ISS at. So let's do those. So we are going to create one for the joke URL, and we're going to set that equal to `https://icanhasdadjoke.com`. I think that's the URL. And then let's add a comment: This is ican has dad joke API endpoint. If you hear anybody refer to an API or an API endpoint, the endpoint is really just the URL that you need to be hitting. So it's not always the case that the base URL is going to be the API endpoint. So in this case, you'll actually see—so when I show you the API for where the ISS at—it's going to be different to just `where the iss.at`.

Let's create our ISS URL, and that one is `https://api.wheretheiss.at/v1/satellites/25544`. This is uniquely different to the ican has that joke API, but it's also super different to `where the iss.at`. This is a specific URL that I went and found that actually gives us the specific details for just the ISS. So this is the ISS API endpoint. Cool. All right. Now we need to go ahead and make our request. So let's go ahead and do this. So we are going to first up create a variable, and a key thing to note is that right now we are currently using a new package. So this is completely new; we haven't covered packages before, but this extends out. So a good process whenever you're using a new package is to learn how to install it, learn how to import it, and then learn the core functions, methods, and classes that are available within that package. In this case, the most popular one inside of the request package is going to be the `.get` method, as you can see there, Julio. So what are we doing? We're going to actually make an API call. So we'll first up do the joke URL. To do that, we need to first up set up this thing called headers or a header. So we are going to create a new variable called `my_header`; we're going to set it equal to a dictionary. And think of a header as like some metadata that we send with our API call. So sometimes you might send through your authentication tokens if you have a protected API; you might also specify what type of data you want back; you might also send or specify what type of data you're sending as part of the API call. Keep in mind there's a ton of stuff you can do with APIs; that's probably a topic for a whole other video that we might need to make. Now let me know if you want to see a full video on APIs.

And then what we're going to do is we're going to say that we want to accept, and we are going to accept JSON back as our response: `application/json`. Cool. Now what we want to do is actually make our API call. So let's actually specify—so this is setting up a header, and then we can actually make our API call. API call. Let's add in some more cells below so we can center that up. And then in order to make our API call, so we first up need to pass through our URL, and then we're going to pass through our header. So let's do this. So I'm going to pass through—uh, results—it's actually yeah—so let's store our results inside of variable `code_results`, and we're going to call `requests.get`, and then the first thing that we want to do is pass through our URL, because remember our request API is expecting our URL first, and then it's expecting some parameters and some keyword parameters. In this case, our `params` is set to none, and our keyword arguments—there's a whole bunch of them if you actually take a look at the documentation. So go to `requests`, what is it, `requests` Python documentation; there is a ton of options that you can work with the request library. So we're going to take it super easy, but there's a ton of information about this. Really, really good practice whenever you're dealing with a new package to actually go ahead and read the documentation. More often than not, the most popular Python packages are going to have really, really good documentation because they're well-supported packages. So TensorFlow is a great documentation; Pandas has great documentation; so does NumPy; so does PyTorch. So if you want to play with some of those libraries, do read the documentation. This is what allows you to transfer from being a junior developer or someone that just tinkers around being a really, really good developer. Learning how to read documentation is going to make all the difference. So in our particular case, we can close this down, and we are going to pass through our URL first up, which in this case is going to be our `joke_url`, which is just a string. So if I type in `joke_url`, it's literally just a string that contains our API endpoint. So if we pass through our `joke_url`, and then we need to pass through some keyword parameters. So this particular case, our keyword parameters are going to be our headers. So we type `headers=my_header`. Right. So remember when we took a look at our documentation, let's run this again. We said that—so this little argument over here basically says that this particular method is going to accept a bunch of different types of keyword arguments. So the `**` basically unpacks a bunch of keyword arguments. So there's a bunch more than what's specified there. Now, in this particular case, one of our keyword parameters that we can pass through is `headers`, and this header is allowing us to return back JSON. So you'll see this in a second. So let's take a step back. So what we've gone and done is we've gone and installed `requests`; we've gone and imported it; we've then gone and set up two API endpoints, so `joke_url` and `iss_url`; we've then gone and set up a header, and remember a header is like metadata, metadata for your um API request; and then we've actually gone and set up our ho—our request in this particular case, or our API call. Then that particular line, we've written `results = requests.get`, and remember there's a bunch of different HTTP request methods. So if we actually type in—um—`requests.put`, that's another one; `requests.delete`, that's another one; `requests.patch`, that's another one. So again, there's a bunch of different type of HTTP request methods that you can make. So more often than not, you have to look at the API documentation to understand what type of HTTP request methods a particular API can take. In this case, I know that the `joke_url` and the `iss_url` can handle GET requests, so we're going to do exactly that. Okay, so we've gone and done this. Let's actually make our API call now. So if we run this cell, fingers crossed, that looks like it's been successful. So we don't actually know yet. So let's actually deconstruct this. So if we type in `results`, you can see that we've got a ris—a status code return. So if I take a look at HTTP status codes, there are a bunch of different types of status codes that you'll get back when you make an API call. In this particular case, a status code of 200, which you can see there, basically means that we're okay. I like how lackluster that is; it's like, yeah, we're okay, we're cool, but it basically means that your API request has been successful. So you've don't have any errors; you've actually got something back. Let's zoom out of that. So how do we actually go and see what our API call is actually returned back? Well, we can actually type in `.`, and we've got a bunch of variables that we can actually get back from this. So the one that we're going to take a look at is `.json`, and so you can see there that what we've actually got back is a dictionary, and in this particular case, our dictionary is represented as JSON. So if we actually store this result inside of a `json_result` variable, `json_result`, and if we type in `type`, you can see it's a dictionary. Cool. So remember `type` allows us to check what type our variable is; in this particular case, it's return dictionary, and we've got back—let's actually take a look at our result—so we've got back three different keys. So we've got back an ID; we've also got back the joke itself; and we've got our status code. So this ID is just a unique identifier; this joke is actually our joke; so this is the joke that we've got returned back: Is the pool safe for diving? It deep ends. Oh god, that's terrible. All right, let's do another one. Um, so if we go and run this cell again, it's going to make another API call and return a different dictionary. Yeah, that joke was terrible. Sorry, guys, my bad. All right, let's run this again. All right, what this is—our next joke is: Why did the magician get into the popcorn line and the line for hot dogs, but he wanted to stay out of the punch line? Oh man, ouch. Okay. All right. So let's go in—rather than taking a look at any more of the ican has that joke API—let's uh let's skip on over to the ISS API. So we're gonna make an ISS API call now, and this is live tracking where the ISS is. Pretty sick, right? So we can type `requests.get`, and then we're gonna pass through our `iss_url`, so this up here, `iss_url`, and then we're going to pass through some keyword parameters; we just need our headers, so `headers =`—let's make this a little bit more centered—`headers =` uh what are we setting it equal to? We're setting it equal to `my_header`, so from over here, I'm going to store this inside of a variable called `iss_results`, not in caps. Cool. So that looks like it's run; no errors yet. If we type in `iss_results`, we've got a response status code of 200. Remember, a response status code of 200 is okay. Again, super lackluster and chill; it's just okay, but at least it's not some of the other ones that you might get if it's not okay or not happy with you is uh a 400, 400 or 404; I think it's four or four—yeah, 400 is a bad request. So these ones are the ones that you need to look out for. If you get this, so 400 is a bad request—let me zoom in on this—400 is bad API, bad; 401 means you're unauthorized, so this typically means that you need to authenticate to the APIs that pass you a password or a token; 402 means payments required; 403 means it's forbidden; 404 probably means you've got the API endpoint wrong or it's not found. So those are just some others that you might encounter on your journey with APIs. Cool. What are we doing now? All right, so we've gone and checked our `iss_results`. Now what we want to do is we want to unpack our results. So we're going to type in `.json`, and that is our ISS stuff now returned. But this is so cool; I'm I'm absolutely in love with this. So you get a bunch of stuff back. So you get the name of the satellite that it's actually tracking; in this case, it's ISS; you get the satellite number, so 25544; yet its latitude, its longitude, its altitude, its velocity, its visibility—so right now it's in daylight—its footprint, its timestamp, the day number, the solar latitude, solar longitude, as well as the units that it's currently reporting in. So in this case, it's kilometers. Pretty cool, right? So there's a whole bunch of other APIs out there that you can start going out and exploring; there's a ton of stuff that you can do with this, and that really just summarizes what's possible with packages. So we haven't had to write any of the complex mechanics in the background to go and make an API call; the request package handles all of that complexity for us and really makes our life a whole heap easier. So let's recap what we've gone through, and then we'll take some questions. So we've gone through modules, and remember we can import some modules from another file. So in this case, we're importing `launch_codes` from our `helpers.py` file up here. We then took a look at how we can work with packages, and remember you can take a look at all the different packages that are available by going to `pypi.org`, so that's `pypi.org`. And then we've also taken a look at one of the most popular packages, which is `requests`, and `requests` allows us to make API calls. So this is the `requests` API documentation. Really, really good practice to go and read the API documentation because it tells you a lot about how the package actually works—not API documentation, package documentation. It's also good to read API documentation as well if you're using an API. Uh, where are we now? Then we went and set up two new URLs to go in here, or two specific API endpoints to be able to go and make API calls, and then we actually went and made our API calls there. Let's actually clean this up. So I'm going to delete that; I'm going to delete that. So these were API calls, and remember one of the most popular methods or HTTP request methods that you're going to use is the GET method. So this is a list of all the different types of HTTP request methods you might encounter: GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, and PATCH. Most often, you're probably going to be using GET or PUT. In this particular case, we can pass through our URL as well as some keyword parameters, which in this case is `headers`. We can then go and extract the results from our API call, because remember by default what we're going to get back is the status of our API call. So let's actually comment this: status, status of API request. So remember, by default, when you actually just output the result, that's going to show the status code. So in this case, a status code of 200 means it's okay. We can then go and extract the values from our API calls. Remember, we're going to extract JSON results from the API. We can validate what type of data we're getting back; in this case, when we go and pass the JSON results using the `.json` method, we're going to get a dictionary back. We can also unpack our dictionary to get our values back, and in this case, we've gone and made an ISS API course for making an ISS API call. Remember, there's a whole heap of different packages that are out there; you can build a ton of different modules, and some of my favorites, as I mentioned, are Pandas, PyTorch, TensorFlow; MediaPipe is another one that we've done a ton of work on. So if you want to go and check those out, what you'll see is in a lot of the videos that I build, first up, we go about by handling setting up different packages so that we can get started with our different projects. So this is super important when it comes to using these for data science. Alrighty, sometimes questions. This is a really good question: So why would we use most APIs? Okay, so certain companies are going to actually render or share their data using an API. Now, a lot of the time, this is how they'll share their data with external organizations. Often, when you're dealing with data internal to organizations, you're probably going to be accessing a SQL table, or you'll be accessing a NoSQL database, but having the ability to access APIs makes you a whole lot more powerful because in this case we're using an API to get data, but APIs are really, really powerful in that they do way more than just provide data; they can also be used to trigger processes; you can also use APIs to store data; you can also use APIs to control different aspects of software. So they are really, really powerful. Um, what are my favorite public APIs? One of my favorites is Twitter because there's so much that you can do with it, right? So you can actually scrape down the data; you can see what's trending; you can actually go and perform sentiment analysis; you can go and generate new text corpuses from it—so much, so rich. Another one that I like is the Reddit API as well; there's a ton of stuff that you can do with that. Um, okay, I don't think we've got any more questions, but if you've got any, shoot them through; I'm always happy to come back and answer them, and let me know how you go with modules and packages. On that note, I think we'll wrap this up. So once again, thanks so much for tuning in, guys. Hopefully you've enjoyed this video. If you have, be sure to give it a thumbs up, hit subscribe, and tick that bell, and I'll see you in the next one. Thanks again for tuning in. Peace.

What's happening, guys? Welcome to the final part in the Python for Data Science live stream. My name is Nicholas Renate, and in this video, we're going to be going through all the fundamentals that you need in order to be able to go through file writing—so writing stuff out to text files—we'll also take a look at file reading—so once you've written stuff out, how on earth do you read it back into a file or back into your Python script—and then last but not least, something which is actually really, really important: error handling. So you're probably thinking, I'm not going to write errors in my code, so how why do I actually need…

Error handling is important. More often than not, there might be errors when accessing a data source, or errors when accessing an API, or just errors with data coming into your machine learning model or your deep learning model. So, being able to handle those errors gracefully so your program doesn't just "crap itself" makes your life a whole ton easier. So we're going to go through a little bit of that and see how we can actually handle our errors. Let's get to it.

Okay, so what we're going to do is, as per usual, we are going to go into our Jupyter Notebook. I'm going to open up a new command prompt, so `cmd`. And then again, we need to open up the same Jupyter Notebook that we've been working in the entire time. So I'm going to go into my D drive, onto YouTube, and inside of Python Basics; that's where all of our Python good stuff is. Remember, we can start up Jupyter Notebooks from here, and this will allow us to access this specific notebook to be able to write some Python code. Let's go on ahead and do this.

So I'm going to first up go into that folder. So we're going to go into YouTube. Let's see, uh, actually, I need to be in my D drive, so `d:`. `cd YouTube`, and then we are in Python Basics, which you can see up there. So `cd Python Basics`, and then we're going to kickstart and start up Jupyter. Remember how do we start up Jupyter? We just type in `jupyter notebook`, and this will give us our interface to be able to write a bunch of Python code. Now I'm going to zoom in a little so you can see it a bit better, and we're going to keep on going with our same Jupyter Notebook that we've been going through through this entire series. So we can do that by opening up `python_basics.ipynb`. We're going to scroll on down all the way to the bottom, and we're going to finish up our last couple of sections.

So what was our last section? Our last section was Section 8, where we're working with packages and we made API calls to get the ISS data. So I think we've got latitude, longitude, all a bunch of that stuff. But say, for example, right, so we'll do working with files first, but this is—I want to talk a little bit more about error handling. If that API failed, it's just going to throw a bunch of errors, or you might get an error in your request. So having your try-catch block, which we'll talk about in a second, it's going to make your stuff a little bit more graceful.

So what are we going to do? We are going to write some stuff out using files first up. So, as per usual, what we're going to do is we're going to structure our code really, really nicely, because remember, you want to have structure, and you want to have comments so you know what on earth you went and did maybe yesterday, whatever time. More often than not, I get into a habit of writing a bunch of code, and then I go back and look at it, and I'm like, "Nick, what on earth have you just wrote?" I mean, that was literally me this morning when building up a Siamese network. So again, writing and commenting your code and structuring it really nicely is going to make your life a whole heap easier later on.

So what are we going to do? We're going to step out of this cell and we are going to convert it to markdown. So right now it's a code cell; we're going to convert it to a markdown cell by hitting `m` on our keyboard, and you can see it's gone and converted to markdown. And then we are going to—let's check out what our last section was—Section 8. Got a little Kendrick up in there, and we're going to convert this to Section 9. So we're going to hit the pound symbol, add in number 9, and we're going to call this "Working with Files". Key thing to know is that certain packages are also going to have their own functions for working with files. So, say, for example, you're working with Pandas, or you're working with NumPy, or you're working with TensorFlow, they have native file writing and file reading type functions built in. So with NumPy, you can write it out to a `.npz` format and read that back in. But again, if you want more details on that, hit me up.

So what we're going to do this time is, say, for example, we're an astronaut—where we're continuing with our space theme—we're an astronaut, we're out floating in space, and we want to log out a little bit of our log, right? Or let's say we want to write a journal. So how might we actually produce a journal? Well, we want to be able to save it down to our disk so that we can bring it back next time. Now what we can actually do in Python is we can actually write out to a file. So let's go ahead and do this, and we will be able to see how we can do that. So I'm going to add a comment: so we're going to write out our mission journal (j-o-u-r-n-a-l—spelling is a shocker, Nick!), and then what we're going to do, we're going to write it out.

To do this, we're going to use the `with` keyword, and this allows us to work with unmanaged data sources or data types. So if you're working with stream-based data or a service which you need to close automatically, the `with` keyword makes this really, really nice. So without `file`, normally what you'd have to do is you'd have to open up the file, write something to it, and then close it. By wrapping it up in this `with` keyword, it just makes it a little bit nicer, and this is a really common way of writing out to files; particularly, it's very Pythonic in that sense.

So what we're going to do is we're going to write this out. So we're going to then write `open`, and then we'll write—we're going to name our mission log. So we're going to call it `mission_journal.txt`, and then we need to pass through the mode that we want to write out our particular file in, or the mode that we actually want to open our file in. So if we actually type in `open`, so the `open` function, `??` to open up the doco, you can see that it's going to—first up, what we need to do is pass through a positional argument. So let me zoom in on that so you can see that a bit better. Gotta pass through a positional argument, which is `file`; then we need to pass through the `mode`, and the default mode is going to be `r`, which means read. So sometimes the most common ones that I tend to use are `r` for read, `w` for write, `rb` for read binary—this is particularly useful if you're using a file that is in a binary format—and `wb`, which is write binary as well. But you can see that we've got a bunch of other keyword parameters or keyword arguments, so we can pass through this as well. All right, enough on that. We are going to—because we want to write to our file—we are going to pass through `w`. And then when we're using this `with` statement, we need to pass through a variable or a placeholder variable that we're going to use to work with our file temporarily while it's open. So in order to do this, we can write `as`, and then common convention is you just pass through `f`.

Let's take a look at that full line so far. So we've used the `with` keyword; you can see there, it's in green. And then we've written `open("mission_journal.txt")`; so this is going to be the name of our file. And then the second argument that we're passing through is `w`, which means that we're going to be writing to our file. And then in order to work with our temporary file while we've got it open, because remember we're using the `with` statement over here, we've written `as f`. This means that whenever we reference `f`, what we're actually doing is referring to this document here: `mission_journal.txt`. Cool. Now what we need to do is we need to actually write something out. So we can do this by using the `write` method. So I'm going to type in `f.`, and there's a bunch of different methods that you've actually got here. So we can use `f.close()`; `f.closed`—that actually checks if it's closed—we actually want to use the `write` method down here. And then we can pass through what we want to write. Um, I don't know, "It's my first day on the space mission. It is very nice. Very… uh… uh… we'll make it to US… what the hell." All right, cool.

Now what we've written there is we've added `f`, because remember `f` is going to refer to our file while it's open, and then we've used `.write` to be able to write something out to that file. So if I go and run this now, what should effectively happen is—well, in this case we've returned nothing—but if we actually go into the folder that we're currently working in, you can see that we now have a file called `mission_journal`, and if we open this up, let's say in VS Code, you can see that it is in fact printing out a mission journal. So you can see there that it's gone and written "It's my first"—I didn't even write my first date—"It's my first on the space mission. It is very nice." Pretty cool, right? So that is, in a nutshell, how to write or write stuff to files and read stuff from files. I've gone through this relatively quickly because, more often than not, particularly in data science, you're actually going to be using the built-in tools. So if you're—this just sort of gives you an idea as to how to write something out quickly if you needed to—but when you're doing true data science projects, particularly with Pandas, there's a function called `.to_csv` method called a `.to_csv`, which will actually allow you to output your entire Pandas DataFrame to a CSV. In NumPy, I think you can save with TensorFlow; you can save weights, which allows you to save an `.h5` file. So just keep in mind that this is important, but you more often than not you're going to be using the native output methods. Let's take some questions. What's happening? What about an appender's use? Oh yeah, actually, this is a really good point to note. So another one that's useful here is `append`; this actually allows you to append something to a specific file—very, very important to note. So using `append` is also another useful read mode. But on that topic, that is our "Working with Files" done in a nutshell. Now again, there's a whole bunch more that you can do in this space, but it's nice to have a little bit of a refresher so you can sort of see how to do this. What I really wanted to focus on today is error handling. Now it's relatively simple to do, but it's going to add a whole lot more robustness—love that word, robustness—to your actual code because you're handling errors gracefully.

So what we're going to do is we're going to create another section. So we're going to escape out of this cell, and we are going to hit `m` as markdown, and then what we're going to do is hit the pound symbol and create Section 10. So, uh, we're going to name this "Error Handling". And then what we actually need to do is let's actually create something that's going to throw an error. So remember, way back when we were doing working with different data types, we learned about different types of data types. So we looked at lists, we looked at dictionaries, we looked at tuples, we looked at sets, and we looked at a bunch of different data types. Now a key feature of a set is that it is immutable, right? So you can't go and change the set once you've gone and created it. So let's actually go and create a set, and then we're going to try to change it, and you'll see that it throws an error. But what we can do is we can actually wrap it inside of error handling or inside of an error handling block, and that means it's not going to fail and crash; it's just going to print out an error quite nicely, and we can actually do something with that error and maybe change what the error message that's returned to the user is. So this is really, really useful, particularly when you're building APIs. So, say, for example, you're building a Flask API, and somebody sends through the incorrect data format, rather than just crashing your API, what you might want to do is return something gracefully and say, "Hey, what you sent through isn't the right actual format." Also useful whenever you're deploying your machine learning models. So say it's expecting an image of the size at 100x100x3, and somebody sends through something which is 120x140x3, then you might say, "Hey, the image file needs to be returned in this format," rather than crashing your API. So again, error handling—super, super useful.

First up, what we're going to do is we're going to create a set and we're going to try to update it and then crash it, or let's cause an error. Let's create a set first up. To create a set, give us some more room here. So we're going to call it `new_set`, and we are going to call it `set`, or we're going to wrap it inside of a set and then pass through just a bunch of numbers: 1, 2, 3, 4, 5, 6. Right, so that is our `new_set` created. Right, we could also get rid of this and just wrap it inside of curly braces—same sort of thing. Right, you can see that there. Now a key thing about our set is that if we go and try to update a value inside of that set, it's going to throw an error because you're not allowed to do that; sets are immutable. So if we go and do this, right, so `set` it equal to zero, or grab the first value—oh, it's not subscriptable—so that's going to throw an error regardless, right? So if we went and did this and said, um, `set` it to four, you can see that it's saying `TypeError: 'set' object does not support item assignment`. So we can't subscript our value and get a specific value out, but just as easily we can't actually go and assign a value to that set. So how do we actually go about this? So rather than actually just throwing a tantrum and causing a massive error, how might we actually handle this a little bit more gracefully? Well, we can use something called a `try` keyword. So if I hit `try` or type in `try`, and then `:`, and tab this in—if I go and run this now—what we actually need, we need to finish this block. `try` is the first part; the second part is `except`, and then we're going to capture our error. So in Python, errors are called or sometimes referred to as exceptions. So if I type in `Exception as e`, then I can actually print out my error.

Let's actually take a look at what we did there. So I've written `try:`, and then I've gone and tried to execute my error block, which is `new_set`, and then we've gone and tried to assign this value, so the value one equal to four. Then what we've actually gone and returned—so in this particular case, it's not actually running because we're going and throwing an error—rather, we're actually printing out the error. In this particular case, the error is `TypeError: 'set' object does not support item assignment`. Now, rather than printing out that error to our user, we might say `print("Something went wrong with your request.")`, and so you can see there that rather than throwing a huge error and failing and getting back this, which looks not as nice, what we can actually do is we can handle our errors a lot more gracefully. Now this is really, really important when it comes to the software development life cycle and building robust code; you want to handle your errors gracefully, not just cross your fingers and hope that they're not going to cause errors. So having stuff inside of try-catch blocks, or try-except blocks in this case, is going to make your code a lot more robust. But I've made an error, and I haven't gone and commented it, so let's do that.

So first up, the two parts to our error handling are the `try` statement or the `try` keyword; so this is the first part—so `try` something using the `try` keyword—and then we're actually going and running our code. So then run a piece of code which may cause an error, and then what we're doing is we're actually handling our error. So if we have an error, this code below will run, and you can see here that what we're going to then do is it prints out something that's a little nicer for the user, right? So that's sort of, in a nutshell, is error handling. Now let's actually…

What's happening guys? Welcome to a bonus round in the series on Python for Data Science. My name is Nicholas Renat, and in today's video, we're going to be going through specifically a couple of math functions that are going to come in handy when it comes to data science. And we are also going to go through a couple of data science projects that you can go on and start doing once you finish this series. So ideally, you've got some next steps that you can go on with to continue your journey in terms of becoming a weaponized data scientist. All right, let's take a look as to what we'll be going through.

So first up, what we're going to do is take a look at the core Python math functions. So these are things like addition, subtraction, how to create a power or apply a power, so on and so forth. Then I'm also going to show you how to leverage some additional math packages. So specifically, if you wanted to, um, if you wanted to leverage some more advanced functions, use a sine, cosine, a whole bunch of advanced things, I'll show you how to access some of those as well. Then last but not least, we're going to talk about our different projects that you can go on and pick up and start doing as well. So a bunch of stuff that we need to get through, so let's go ahead and kick this thing off, what do you say?

Okay, so the first thing that we're going to be taking a look at is Python math. So let's go on ahead and start up our Jupyter Notebook, as per usual. And in order to do that, I'm going to open up a command prompt. And again, if you're operating on a Mac, then it's going to be a terminal; if you're on a Windows machine, it's going to be a command prompt or even a PowerShell prompt. In this case, we're just going to use our command prompt as we've been doing so far. So what we're going to do is we're going to navigate to the same place in to be able to access the same Jupyter Notebook that we've been working with so far. So I'm going to go into our D drive, and then YouTube, and then Python Basics, and this is our main Jupyter Notebook. So remember, it's in an .ipynb format, so that right there. So we need to get to that and open it up using Jupyter. So let's do that. So I'm going to go to my D drive, go to YouTube, by `cd` into YouTube. `cd` also stands for change directory. I probably have glanced over that, but `cd` allows us to change the directory. So going to YouTube, go into Python Basics, and we're going to go on ahead and start Jupyter. How do we start Jupyter? Again, we start by writing `jupyter notebook`, and this is going to start up our Jupyter Notebook server. I'll minimize that and then bring this over, so it just opened up on the other screen. Then we can go on ahead and open up our Jupyter Notebook as we've been doing by opening up Python Basics, and we're gonna go on ahead and scroll on down to the bottom. So we've got the last section we were up to was Section 10, Error Handling. We're going to create a new section and start setting up for our math components. Now remember, right now our cell is active. Let's zoom in a little more so you can see that a bit better. So this cell is currently active, so you can see that it's green. And now what we want to do is we want to make sure that it's inactive in order to convert it to a markdown cell. So what we can do is hit Escape on our keyboard, and that shifts it over to blue. We can then hit `m`, which converts it to markdown. So remember, `y` for code, `m` for markdown. We're going to leave it as markdown, and we are going to name this section uh 11, and then Math. So we'll hit the pound symbol, type in 11, and then type in uh Math Stuff or DS Data Science. I would… let's just call it Math, keep it clean. Okay, so let's go on ahead and do some math.

So first up, what we're going to do is we're going to create two variables that hold a couple of uh integers. We'll just keep this simple. So we're going to create two variables first up. So let's create a comment: `# Create two variables`. And we're gonna name them `math_value_one`, and then we'll just notice that it's 123456, and then we're going to create another one: `math_value_two`, and then this one's going to be uh 5678914. Cool, just random numbers. You can make these whatever you want; it really doesn't matter. And then hit Shift+Enter. Remember, Shift+Enter is going to allow us to run our current cell that we're currently in. So in this case, we've gone and instantiated those two new variables now. So if I go and print them out or just type them out, you can see we've got 123456. Got typing was not the greatest there. And what's `math_value_two`? 5678914. All right, but this really just shows us our two variables, right? We haven't actually gone and applied any math as of yet. Now there's a number of built-in Python math functions that allow you to work with different components or do some math. Now these are called operators. So I believe they're called math operators or math… math operators Python… arithmetic operators. That's probably a better term, but math operators saw the same thing, right? So we've got addition, subtraction, multiplication, division, modulus or modulo, exponentiation, and floor division. So we're going to go through all of those.

The first one that we are going to go and take a look at is the addition function. So let's delete that value, and what I'm going to do is create a comment: `# Add or addition`. And we're going to add these two numbers together. So again, really simple, these math functions are going to be really simple but super useful to know how to do. So we're going to type in `math_value_1`, and then to add numbers together, we just need to use the plus sign, so sort of like what you'd enter into a calculator. So `math_value_one + math_value_two`, and that adds them together. Pretty straightforward, right? So what we've gone and done is we've created our two variables initially, so `math_value_one` and `math_value_two`, and we've set them to integers or random integers. And then to add them together, we just need to use the plus sign to be able to add those together. Now the next math function that we want to go through is subtraction. `# Subtraction`. And again, reasonably simple. So we can pass through, type in our variable, and then the sign or the operator for subtraction is just a minus sign, right? So you can see that there, and it's in purple, and then we can type in our second value, `math_value_two`, and you can see that that is subtracting our value. So we've got -444,000 or what is that, a million? So -4,443,458. So it's this value or this value minus this value. Cool. So that is addition and subtraction. Now the next two that we're going to go through are division and, as you might have guessed, multiplication. All right, so what's division? So we can type in our value again, so `math_value_one`, and then we're going to divide, and the division sign is a forward slash. Really, really important to note: a backward slash is our escape symbol, right? So you can see there that it's printing out `SyntaxError: unexpected character after line continuation character`. This is because the symbol for division is a forward slash. Important to note. So to divide, we can pass through our variable and then pass through the divide operator, which is a forward slash, and then our second variable, `math_value_two`. And then we can go on ahead and do multiplication. Actually, before we do multiplication, let's take a look at another form of division. So this is um dividing with the integral result. So it is basically going to set a floor. So what you'll see here is that it's basically called floor division, um, sort of same type of explanation, right? So if I go and type in this value, so `math_value_one`, and if I have two divisions and then pass through our next value, it's going to return zero. This is because you can't or this value here does not fit into this value, right? So uh, one… what is that, 1,235,456 is smaller than this. This means it's only going to return the first value. So you can see that there. So it's the floor. If we went and flipped these around, so if we divided `math_value_two` by `math_value_one`, we are going to get four because this value wholly fits into this value four times. There is a residual, however. So how do we actually work out that residual? Let's actually break this down. So if we went and divided `math_value_one` or actually two by `math_value_one`, what you'd actually get is 4.59661371… you sort of get the idea, right? Four plus a long number. Now what you're getting by dividing or using the floor division function, which is a double forward slash, is you're getting the first value, so the total number of times a single value is going to fit into the other value. To get the residual, what you can actually do is use a function called modulo or modulus. So to do that, what we can do is use the percentage sign. So if we go and try that: `math_value_two % math_value_one`, what it is going to do is give us the residual, which it is not… I got that wrong. Okay, maybe I've got that wrong. What's it saying here: modulus? Okay, clearly I've got that one wrong. Um, so `math_`, I thought that was going to give us the residual, but it's… oh, sorry, it's giving us the residual value, so not the uh, not the actual uh residual here. So if we go and divided `math_value_two`… wait, let's do `math_value_one * 4 / math_value_two` actually subtracted… it's giving you the residual value, not the decimal places. So `math_value_`… I mean, clearly I haven't used that one for a while. So that is the value that it's giving you there. So you can see there that `math_value_one` is going to fit into `math_value_two` four times. What you're getting by using the modular sign is the residual value. So this is the extra bits that aren't going to fit into our main value, right? So this is particularly useful if you're trying to work out what's sort of left when you're doing your math functions. Okay, so what is the next math function? Let's add a comment there: `# Modulo`. Cool. All right, that's our modular done. So what have we gone through now? So we've gone through… it's a little hot over here, guys. I'm getting worked up a sweat doing math. Okay, so we've gone through our two variables, gone through addition, and addition is just a plus sign. We've gone through subtraction; subtraction is a negative or minus sign. Gone through division, which is a forward slash. Gone through floor division, which is a double forward slash. Uh, what we're doing here… looking at modulo… let's just move that down. So in order to calculate the modulus, of the effectively the residual value, it's the percentage sign. In order to multiply stuff, it's a single asterisk. In order to calculate a power, it's going to be a double asterisk. And again, we could pass through three, so we could cube a value, as…

Well, again, you sort of get the idea there. And then we've also taken a look at some built-in functions as well. So we've taken a look at the round method, or the round function, and we can pass through a positional argument to round down to a certain number of decimal places. Also taking a look at the abs function; abs is a—yeah, I guess that's how you'd call it—the abs function to round or to retrieve an absolute value. Taking a look at the minimum function and the maximum function as well.

Now, I mentioned really, really briefly that we're going to take a look at the math package. Now, I'm not going to spend too much time on this, but I wanted to show you that there is a ton of additional stuff that's sort of built into Python if you need it as well. So say, for example, you wanted to do some more advanced stuff. Let's say, for example, square root—right? That I don't believe there's a built-in function for that. There might be; I can't remember. But let's say we wanted to access that function. What we can do is we can import the math package. And let's do this. So if I type in "import math," and in order to do that, I've written "import math," so in order to do—I'll use those functions—I can type "math." and there are a ton of different mathematical functions inside of here. So what do we have? So we've got acos, acosh. I haven't looked at trig in so, so long. I've got floor. We've got—uh—what's one that I actually use? So there's radians. I think we used radians when we're doing our MediaPipe angle calculation function. So again, that's sort of useful to use. Square root is one that you're probably going to use all the time. So say, for example, we wanted the square root of 25. By passing through our value, we are going to get 5 back. And just as a reminder, remember that when we want to double-check how a function works, we can type in "math," or whatever function we want to access—in this case, it's a method—and type in "??" And this is going to open up our documentation for us. You can see here that it's saying it returns a square root of x. Built-in—it's a built-in functional method. Pretty cool, right?

And that, in a nutshell, is the core set of math functions that you're going to want to know when you start going down the data science path. There's probably a whole heap more, but this is the core stuff that you ideally want to know when it comes to Python without using any packages. So say, for example, you're going to use NumPy; there's a whole bunch of additional stuff in there. Likewise with Pandas.

All right, super quick recap. So we've gone through a whole bunch of basic mathematical operators inside of Python. So we took a look at addition, subtraction, division, floor division, modulo, multiplication, power. And then we also took a look at some built-in functions: round, abs for absolute, minimum, maximum. And we've also briefly taken a look at the math package, where there is a bunch of stuff that you can go out and extend your Python workflow.

Now, that about wraps it up in terms of the coding portion of this video, but I wanted to not leave you hanging, and I wanted you to have a place where you can go and learn some additional stuff. So there are a ton of different projects on the channel as of right now. So if they're—if you're looking for something to go on ahead and do, I'd highly recommend go and take a look at the channel. But I'm actually going to give you three projects that I'd recommend you go to after this. They're going to be challenging, but that's what you want; that's what's going to allow you to learn. So the ones that I'm actually going to recommend are this playlist over here. So it's called "Building Full Stack Machine Learning Apps with Watson Machine Learning." So this is going to take you through a number of different components when it comes to building an entire data science workflow. So it will take you through the exact steps that I still use to this day to build up a full-blown machine learning model, deploy it, and get a REST API, as well as building a front-end application for it. So it will be a challenge, but you'll learn an absolute ton, and it will also allow you to build up from the Python skills that you've learned inside of this series. So that's project one that I think you should go and take a look at.

The other project, or the other set of projects—so this is a number of videos, right?—so I think you start off—uh—what do you start off with? So exploratory data analysis. And this will introduce you to NumPy and Pandas. You'll also learn a little bit of how to actually work with Pandas DataFrames. This is the big one. You'll also learn how to use or build machine learning models with scikit-learn. So this is a really, really popular package when it comes to the data science industry as a whole. You'll see a ton of people using scikit-learn, and in this case, we're doing regression, so we're predicting a continuous value.

The other project that I think might be useful—and I'll include a link to that in the video description as well, so you can pick that up as well—the other project that I thought was going to be useful if you want to brush up on some basics as well, there is a "Data Science Basics with Python" set of videos that I've gone and created, which covers NumPy in 15 minutes, Pandas in 15 minutes, and TensorFlow. This isn't project-based, but it will give you a good set of fundamentals that you can go on and learn from as well. Uh, what else do we have here? There was one other that I thought was going to be super useful if you're just getting started. Uh, maybe I didn't cover it here. Let's take a look at what else might be useful for a beginner. But plenty of videos. I think something in the NLP space. So sentiment analysis might be good. So we've done a bunch of stuff on sentiment analysis. So the scikit-learn tutorial is going to cover regression, so that is tabular-based machine learning. Sentiment analysis is going to introduce you to natural language processing. God, look at my face in some of these—a bit of a shocker. This one might be cool. So it shows you how to build a summarization model with a library called Hugging Face Transformers. This is actually really, really interesting. So you can pass through a big block of text, and it will actually try to summarize it into its component parts. So useful if you're trying to digest a lot of information at a specific point in time. What was—I'm sure we did something around sentiment—sentiment—oh, there you go. So if you wanted to do something around sentiment, that might be cool as well. Um, so this video here goes through how to perform sentiment analysis with a neural network called BERT. So that one's interesting as well. But there is a ton of stuff here if you wanted to do some stuff on number play. Actually, this was the one that I was going to recommend. Um, so PyCaret is a relatively new machine learning library which actually allows you to build machine learning models with like a couple of lines of code. So that one is really, really interesting, and in there we build a classification model. I can't remember what data set we use, but that one was super interesting. So there are a ton of projects. Take your pick. Um, if you need some recommendations, hit me up in the comments below. And if you need some guidance, again, I'm always here. Definitely don't hesitate to reach out, please. So once again, thanks so much for tuning in, guys. Hopefully you enjoyed this video. If you did, be sure to give me a big thumbs up, hit subscribe, and tick that bell. And again, thanks again for tuning in. Loved having you all here. Peace.