Python 201: Learn intermediate Python programming | Kalob Taulien | Skillshare

Python 201: Learn intermediate Python programming

Kalob Taulien, Web Development Teacher

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
25 Lessons (2h 23m) View My Notes
    • 1. Welcome to Python 201

      1:35
    • 2. The IN operator

      9:47
    • 3. The NOT operator

      7:30
    • 4. How to read files using Python

      6:03
    • 5. How to create files using Python

      4:27
    • 6. How to read multiples lines in Python

      6:10
    • 7. Writing a file and executing it

      4:55
    • 8. Functions inside of functions

      6:35
    • 9. How to make simple API requests

      4:59
    • 10. How to make JSON API requests

      6:09
    • 11. How to read JSON in Python

      5:36
    • 12. What are function *args?

      4:59
    • 13. What are function **kwargs?

      8:25
    • 14. How to install pip

      4:33
    • 15. Mutable vs immutable

      5:29
    • 16. What is list enumeration?

      3:19
    • 17. List comprehensions

      4:09
    • 18. Dictionary comprehensions

      4:27
    • 19. How to get dictionary values

      5:19
    • 20. Create a virtual environment

      7:46
    • 21. How to learn Python by exploring and using iPython

      6:09
    • 22. Introduction to Python packages

      6:05
    • 23. A short API requests refresher

      3:38
    • 24. Your project

      13:06
    • 25. Python 201: Summary

      1:22

About This Class

Welcome to Python 201: Intermediate Python 3 programming.

In this class, you will be learning the ins-and-outs of Python. You will learn how to:

  • use more advanced conditional statements,

  • how to read and write files using Python,

  • how to make API requests,

  • how to use 3rd party packages,

  • list and dictionary comprehensions,

  • virtual environments,

  • and much more.

At the end of this class, you will create a Pokemon program that looks up a pokemon by its name using an API — this is great practice for consuming API's like you normally would as a professionally paid Python programmer.

To get the most out of this course you should have taken Python 101 and another Python beginner course.

Your project: Create a Pokemon data program using an API.

You will create a program that asks for a pokemon name and then look for additional data using an API.

The API is completely free to use, too.

This project mimics the type of work you'll do as a professionally paid Python programmer and is considered to be a real-life project.

The most important part of this course is learning how to manage (install and uninstall) 3rd party packages, how to use them, and how to make API requests.

API requests are how computers talk to each other. It's absolutely vital you know how API requests work in Python, and you'll learn all about them in this course.

Who is this course for?

  • This course is for anybody who has written a little bit of Python but is looking for more experience.

  • Programmers or web developers who are looking for some hands on experience while they learn

  • This course is NOT for people who have never written any Python.

What are the requirements?

  • You need a code editor like PyCharm or VS Code

  • You need to have Python downloaded and installed

  • You need to be ready to work with code and not just watch the videos. This is a hands on course.

  • You need to be willing to do a little work to properly learn Python3

To get the most out of this course you should have taken Python 101 and another Python beginner course. 

Transcripts

1. Welcome to Python 201: Welcome to Python 21 with your host Caleb Talia. That's me. If you are ready to level up your Python skills, then please keep watching. This course is going to take you from beginner to intermediate level Python skills. In this course, we're going to dive deeper into Python subjects such as the naught and in operators managing files nested functions basically decorators, Python objects, advanced scoping function arguments, keyword arguments, virtual environments using third party packages, working with API requests and so much more. Now if any of that didn't make sense to you or you don't know all of those, all of those things are, this course is absolutely for you. Look to get the most out of this course, you are going to need to know some basic Python knowledge such as functions, conditional statements, datatypes, and variables. If you don't know any of those, please check out the Python 101 course as it's going to be much more helpful to you. This course is also assuming that you've written some Python before and that you have an editor like VS Code or PyCharm installed and you know how to execute a simple Python script. Hi, I'm Caleb telling and I've been working with Python for a number of years now. I've worked with clients such as Mozilla, nasa, the NHS in the UK. And I've taught thousands of developers how to write Python in various courses of mine. And today I'm going to be teaching you Intermediate Python. Welcome to Python 201 with your host, Caleb Italian. I hope to see you inside. 2. The IN operator: Let's get started by talking about the in operator. And so in Python one-to-one, we learned about comparison operators. And I just made this bigger in a comparison operator simply looks like if something is equal to something else. Now if you took Python 101, we had a little project where we wrote a rock-paper-scissors program. And we can change this to a Python file here. And we saw something like if my answer is equal to rock, and it's actually does not equal rock. And my answer does not equal paper. And my answer does not equal scissors. And if I make that just a touch smaller here, exit the program. And that's all it did. Now, there is a better way to do this because we know that the three answers that we're comparing to rock, paper, and scissors are all going to be, well, they're going to be the same. They're never ever going to change. So what we can do is this operator, this in comparison operator. And so what I'm gonna do here is review my terminal. And if I type Python 3.9 because that's the version I want to use here. I can simply write the in operator to compare one thing against an array or a list. And so I get say, a list of names is equal to. And let's just make this way bigger here. And list of names is equal to Caleb, John, and gully. And so if we do names or if we do type of names, we're going to see that this is a list. And suppose B names, names, plural. There we go. That's a list. We don't see anything new. We should be familiar with the data type of a list already. But what if we wanted to check to see if a variable or one name is in a list of names. Well, currently what we have to do is for name in names. If my name, whatever that variable is, is equal to the name, print, this is the name. And so this is kind of Honestly no way to live your life because what if you were, if your list, but if you're a ray is really, really big, what if you have 1000 items in there, then you have to iterate over a 1000 different items just to find out if something is in there. Python has this built in. So I'm gonna do here is Control-C just counsel. And I can say Caleb, as a string in names and this is going to come out as true. I could also say the same thing with a lowercase k. And this is going to be false because our names has Caleb, John, and gully. But remember when we're comparing strings in Python 101, a string with a capital K is not the same as a string with a lowercase k. So that the same with a J or a g or any other letter. The fact is that it has to be an exact match. And so in the first example here, this is an exact match. And what I can do here is if I just type that out once more, I can say if golly in names, print, golly is in the list of names. And we see that gully is in the list of names. And so this is a really, really easy way to check to see if one answer is in a number of answers, a possible number of answers. And so if we go back to our example here, I'll just move that down a little bit and make this a wee bit smaller. If we go back to this answer here, we're gonna say, if my answer is not rocked, my answer is not paper. If my answer is not scissors, What I can say instead is if my answer, whatever that is. And instead of equal to or equal to, we can say in a list of options. And so we just need to pass this in a list and we can simply put in a list of options. And let's actually make these variables exist. So we can say My answer is equal to rock. And let's make the options to be a list of rock, paper, and scissors. And now we can say, if my answer, rock is one of these three, then do something. We can say print. Rock is one of the possible options. Now let's go ahead and save this file as the in operator. And I'm going to do, do, do, do, just get out of that. And I'm going to type Python in operator dot py. And it says Rock is one of the possible solutions. Now what if I change that? What if I changed it from rock to something else? Literally anything else we could say something underscore else and we're going to see that this no longer prints anything for us. And that's because if there were an else statement, This would be executed for us. Print wrong answer. Try again. And that's try this out and we see wrong answer. Try again. Now where this gets a little bit more powerful is, and again, we touched on this in Python 101. So this is a little bit of a recap. Instead of saying my answer is equal to a hard-coded value, we could say, input. What is your answer? And if that answer is equal to rock, paper, scissors, then it's going to say that one of these isn't option. We can say that option is a viable option. Otherwise it's gonna say wrong answer, try again. So let's save that and let's give this a shot. It says, what is your answer? If I type in rock. No, let's not do rock. We did rock already. Let's do paper. That option is viable option. Let's try this again. It says, what is your answer? My answer is going to be Python. Python a Yes because I have a zed in there for some reason. And it says, wrong answer, try again. And so the in operator just IN simply says, is the thing on the left, inside of the iterable, on the right. If it is, if it's true, then execute this conditional statement. If it's not, then it's false. Execute the else statement, or originally we had nothing, so don't do anything at all. Now if I were to comment that out, we can do the same thing with a dictionary. So let's give a value, not a value but a variable called key. And this is going to be name. And we could create a person in dictionary name. Caleb, profession, coding teacher. And then we can say if key in person, Print, Name is a valid dictionary key in the Person object. So let's go ahead and give this a shot. Now let's, let's type in Python space in underscore operator dot py. And it says name as a valid dictionary key in the Person object. What if we changed key here? What if we change it to just something that's missing? Nothing happens. And that's because what this is looking for is the key. And remember in the dictionary we have a key value pair. It's looking for that key in there. And so person with a key of fluorophore doesn't exist, but person with a key of name does exist. And so we can use the in operator in a dictionary as well. We can also use it in a tupple. Now what I would like you to do, to try this out, again, a little bit of hands-on experience is I want you to use the in operator on a set. That is a Python data type. We learned about this in Python 101. And I want you to use the in operator on a set, not a dictionary and not a tuple, not a list, but a set. I want you to see if that works for you. If it does or does not work for you. Because at this point we're getting into intermediate level Python. It's time that we start exploring and stop relying on someone showing you everything that can or cannot happen. It's time to start exploring and learning on your own. And, and so it's really important that you can do this because eventually you're going to have a job. And in that job they're gonna ask you to create some sort of unique feature and there's not going to be a tutorial for it. There's not going to be a person to hold your hand for it. You're going to have to sort of experimental little bit on your own and come up with your own solutions. And this is just really good real-life experience when it comes to just experimenting and you're not going to break anything. So feel free to experiment and if it doesn't work, it doesn't work. If you get an error, you can learn what that error is all about. But go ahead and give this a shot. Say if key is in some sort of set datatype, then just print something. That's all we have to do. 3. The NOT operator: Let's take a look at the NOT operator. So we learned about conditional statements and we said something like if something is equal to something else, DO a thing. And that's cool. And we also learned about this. If something does not equal something else, DO a thing. But what if we don't want to simply write all of that out? Well, we have a shortcut. We can say if something is true, whether that string is true, the dictionary is true, the list is true, the boolean is true. Does it really matter if something is true? Then do a thing. But what if we wanna say if something is false? So let's go ahead and set up an example here. We're gonna say my thing is equal to true. Just to set up this example, if my thing, print thing was true. And if we execute this, we can do Python, not operator dot py. And it says thing was true. And that's because it really boils down to this. Now, what if we wanted the exact opposite? Will currently, we have to do this. We have to write pass and then do an else statement. Print thing is not true. And let's go and change this to false just so that we can make this execute. So thing is false. This is not going to happen. The else is then going to get executed. And it's going to say this is not true. So if we run this again, sure enough, it says this is not true. Now this is the exact same thing as saying Is equal to false. Which is the exact same thing as saying is not equal to true. And let's just clean this example up and run this once more. This is going to say, this is not true because false is not true. In fact, this is not true. This statement is true because it's saying false. Is that the same is true? No. Okay. Well, then it's a true statement and this is where it gets a little bit confusing. So in terms of Python, we like to shortcut things whenever possible. And we just simply say, if my thing is equal to true, print a statement in here. Now, when we execute this, we're going to see that this simply does not run, this doesn't execute anything whatsoever. What we can say instead is the NOT operator. And so in other programming languages, you're going to see an exclamation mark in front of your variable. You see it a lot in PHP, and you see it a lot in JavaScript. In Python, we don't do that in Python. We like our code to be nice and readable. So we don't want crazy little symbols all over the place like we haven't JavaScript these days. We simply say, if something is not true and it's literally making a sentence for us. If something is not, and it's looking for, it's always looking for a true value. So if it's not true, aka, if this is false, print a statement in here. Let's go ahead and save this. And we're going to see that this now print out a statement that says print a statement. And here, as we expected. Now let's go ahead and create one more example. Name is equal to K_1 as g. We can say if name not in. Look at this now we're mixing the in operator with not. And we have to give us a list of names. Caleb, John golly. So if cane ascii is not in a list with these names in it, print, cane is not part of the club. And when we run this, first of all, we're going to see in this print statement up here, so we can just ignore that. But then we're going to see the print statement on line nine. And sure enough, the original print statement shows up from line four. And then from line nine it says Cain is not part of the club. And so what we're saying here is, if the name is not in this particular list or array of names, do a thing. And with the not operator comes a lot of power. We can now check to see if things are the opposite. We weren't able to do that in Python one-to-one Python 101, we had to take the long way in Python 201, intermediate level Python we need to know than not keyword is something not true, is something not in another list is something not something else. Now one thing to keep in mind is typically when we're comparing things like strings, we can have name one is equal to K_1, name2 is equal to Caleb. Typically we do not do this. We do not write if name one, not name2, print different names because this just doesn't really make sense. If name one is not named to. I mean, yeah, there is a way to compare using the is operator like that, but we actually don't do it that way. The is operator is a special on the is operator means is comparing pieces of memory, not just the value, which could be cane or Caleb. And so what we usually say is when we're comparing strings, if name one is not name2 and we do it with this operator. But when we're working with the not operator in a list, we put it in the middle. And when we put the not operator in front of something, we're checking to see if it's false. And so if you literally read this just like a regular sentence, it makes a lot more sense. If something is not true, statement. If the name is not in a list, do something. And if one person is not the same as a second person, do another thing. And that is the NOT operator. Now what I would like you to do is I want you to do the first two examples here. So let's get rid of this and just do the first two examples. Try checking a boolean and then try checking a list like this. Make sure you use the NOT operator in both situations. And the goal here is not to become an expert at the not operator because it's honestly, it's a pretty easy thing to learn for the most part. The idea here is to simply get some hands-on experience learning how opposite code works. If I'm not part of a club, I can't enter. If I'm not rich, I can't buy a yacht. If someone enters some user input and it's empty, that would be false. So you could say, if not userInput, then you can exit the script or tell them to enter more content or, or re-enter the script. So go ahead and try that out again. The goal here is just practice. We just want to practice, practice, practice. That's how you're going to get good. You just need to practice, practice, practice. So try this out, give it a shot and if you get stuck or, or this explanation wasn't good enough, feel free to leave a comment down below or in the learning to code Facebook group. I'm always there to help out. In the next lesson, let's go ahead and talk about reading files. 4. How to read files using Python: Alright, let's talk about how we can read files. And so we open files in Python with this thing called a context manager. Now we're not going to get into the details of a context manager too much. It is a little bit of a different way of opening a file, especially if you come from a different programming language. But it is also very memory performance, meaning that you can't accidentally break your computer until reading files is as simple as saying with open your filename, r for read as file. And then we can do something like print file.js. And that's it. That's all there is to it. Now we actually need to create a file in here. So let's go ahead and create a new file, and let's call it read me dot TXT. Hello from Python 201. That's what this read me is gonna say. And so what we're going to do here in this reading files dot py file, we are simply going to open up that readme file and print out whatever is in there. And so we could say with open read me 2.txt, that's the filename comma. R stands for read as. And then we give it some sort of variable name, we can call it file. We try not to use f anymore because F is sort of short For an F Strings. So we don't want to get that to confuse in Python. So we usually we just write something like file or CTX for context or something like that, doesn't really matter. It's just a variable name. And then we can say print file dot. And you can see that VS Code is already trying to auto-fill things for me. So if I just go on down here, I can click read. And this is a function, this is a method. It's actually going to perform some sort of logic, and so it needs those parentheses. Let's go ahead and save that and run this file in your terminal. Let's do Python reading files dot py and it says Hello from Python 201. You'll notice there's actually an empty line in there too. And if we go back here, I'm sure enough, I'm actually quite certain that there's going to be an empty line two. And yeah, there it is. Empty line to this is a new line. Let's go ahead and save that. There's an empty line three. Hello from Python 201. This is a new line. And that's really all there is to reading files. Now here's the thing, is when you are reading a file or when you're dealing with files. This context manager using the width keyword simply means we don't have access to file outside of this indentation. So if we comment this out and we type pass and then we try to print file dot read. Let's see what's going to happen here. It says value error IO operation on closed file. It's trying to read this file, but it's been closed already. So basically what Python's going to do is say, with the readme dot TXT file open as a reading status, not a writing status or anything like that. Just a reading status is going to assign that to a variable called File. And then that file is going to be scope. That file variable is going to be scoped to this indentation here. And then when it gets to the end of the indentation, which it does on line five, It's going to close that file automatically for us, so we don't have to worry about closing the file at all ever. Python will do that for us. This is a safe way to open your files. Now what I would like you to do as a little bit of homework here is go ahead and create a new file called README dot TXT. And write this out to do with open. And then your file name, comma r for read as some sort of variable name, and then print that out. And I'm going to clean this up because we don't need that. And then all we do is that variable name. I just happen to call it file dot read, and that's going to read the file out for us. So go ahead and give that a shot. Feel free to pause the video right here. There's one more thing I want to show you, but I would like if you could pause the video right here and just try this out. Now there's one more thing we can do if we want the contents of this file stored in a variable that's accessible outside of the width keyword here or the indentation here. We could simply say something like content is equal to file.js read. And then down here we can say print. The content is content. And when we try this, we're going to see that this works. It says the content is Hello from Python two at one, this is a new line and there's an empty line in there. And so now we have access to the, basically the entire contents of that file. But outside of the context manager that we opened with the width keyword. Now why is this important? Well, this is important because we have a context manager. We stored the contents of the file in a variable called content. And then behind the scenes, Python close this file. It did it for us, so it's not using too much memory, it's trying to be as efficient as possible. And because frankly, a lot of people forget to close their files, this is a safe way to do it. And then we can access that content variable outside of the context manager. Now I would also like you to give this a shot. So there's two tasks for this particular piece of homework. One is to open a simple TXT file and then printed. And another one is to assign the contents of that file that you're opening to a variable and then access it outside of the context manager, the width statement, including the indentation to make sure it's accessible from outside of that. Go ahead and try that out and when you're ready, I'll see you in the next video. 5. How to create files using Python: In the last lesson, we looked at how we can open files and simply read them, but we can also write to files as well. We can create brand new files. And so let's go ahead and create a brand new file from scratch using Python. And so it looks like this with open and then your filename. So lets say writing files, data.txt. So this is the writing files dot py file. We're going to do writing file.txt. We're going to create that file. And then in the last lesson we used r for read. In this lesson we're going to use w for write as file. So now we have a context manager. So this file is only ever going to be open for as long as we're indenting our code here. Then we can say file dot write. Hello from Python 201. Let's go ahead and execute this file by typing Python, writing files dot pi. And we're going to see a new file appears over here on the left. And look at that writing file.txt appears. Boom, it says Hello from Python to one. Now what happens when we run this again? Lets write Hello from Python 21 second time, save that, and let's execute this. Now, do you think this is going to append to the writing file.txt file or do you think it's going to overwrite it? So is it going to say Hello from Python to one, Hello from Python to 1 second time? Or is it simply going to say Hello from Python tool 1 second time as one individual line in this file. These are fun little experiments that we have while we're learning Python. Let's check this out. Click and it overwrote the entire file. Now if you ever want to append to a file, that's really easy. A stands for append. We do a. And this is going to be a second line. Let's go ahead and execute this. Check our writing file.txt file. And it says, oh, it's actually not as second line. But it did append to the end here. So it went right to the very end of the file and it wrote more what we actually want to do here. And this is a good example. I'm really glad this came up, is we can do slash n. And so we don't actually see these in files, but in this file here where that colon is and it goes to a new line, there's actually a hidden character in there. It's a slash n for new line. And we can write that new line in here. We can also add a new line and a tab slash t. This is tabbed. Let's go ahead and execute this and see how this turns out. So we have Hello from Python 21, second line, this is because I actually messed up. I'm not going to edit that o times. I want to show you that even seasoned programmers like myself will make mistakes. That's totally normal. A second line, because we ran that a second time. And a tab in here, you notice it's not spaces. This is actually a tab and it says this is tabbed. So now we can write files. And with this, we can take user input and we can write files directly from user input. But let's not jump that far ahead yet. In the next lesson, let's go ahead and read multiple lines at the same time. So how do we open this file? But then how do we just get this line and this line and this line? How do we do that? But before we head on over to the next lesson, what I would like you to do as your task is open a context manager using the width keyword, open a file, name it, whatever you like. Use the W writing property. So W stands for right, and we just put W for short as some sort of file. And then that context manager file dot right, and then a new line. And you can put a slash n for a new line in there if you wanted to or slash t. And then when you're done that, try appending to that file. So writing is going to overwrite the file every single time. A is going to append to that file, add it to the end. Go ahead and try that out and when you're ready, let's take a look at how we can read multiple lines at once. 6. How to read multiples lines in Python: Okay, welcome. Let's take a look at how we can read multiple lines at once. Now you might be thinking, well, why is this important? But if I gave you a list of a 100 emails and I wanted you to find the one that has my name in it. How are we going to do that? And this is a lot like scraping data from the internet, which is a very, very common thing. So before anything, let's go ahead and create a new file. And this is just going to be called e-mails 2.txt. There's going to be email one at gmail.com, email to email three. And then let's put Caleb at gmail.com. Let's put John at gmail.com and let's put gully at Hotmail.com. So we have six email addresses in here. And we want to try to figure out if, let's say Caleb, edgy.mil dot com is in here. How do we do that? Well, first we need to open this up. We say with open emails 2.txt, Let's just read it. We don't need to write to it or append to it as emails. And what we can do here is emails dot read lines. And if I print this out, we're going to see that this is going to look very, very familiar. So I can type, let's clear that out. And I can type Python reading multiple lines and dot py. And would you look at that? This all of a sudden is an array. So the first item here is email at gmail.com. Email to email three Caleb a Gmail, John at Gmail gully at Hotmail. This is an array. We can now work with an array. This is exciting because we can take nicer shortcuts here. So let's go ahead and throw this into a variable called e-mails. And we don't have to work within that context manager anymore. Its job is done. We can now access all these emails as an array or as a list as we call it in Python. We can access all of these emails in a list and a variable called e-mails, as simple as that. So now we can say for email, in emails, print, looking for, let's say no, let's look for a Hotmail account. Looking for a Hotmail account. Then we can say if Hotmail is in this particular email, we can use the in operator to check to see if a string is inside of another string. Print the entire email. And so this is going to filter out all of the emails in a file and give us all the ones that have Hotmail in it. Let's go ahead and run this code and we're going to see looking for Hotmail account, looking for Hotmail account looking for hot milk. Oh, look at that. If found guilty at Hotmail.com. Let's go ahead and delete that print statement because that's not useful. Let's say we just wanted all the Gmail account. So this is going to filter out the one that said gully at hub at Hotmail.com. We run this and email one, email to email three, Caleb and John. Now what's up with that spacing? Well, that spacing is because if I scroll back on up, you notice that slash n. And we talked about this in the last lesson. Slash n means new line. And so what we wanna do here is we want to take this email and we simply want to trim it. Now in other programming languages, we use the word trim. In Python, we use left trim or right trim, and we don't call it trim at all. In fact, we actually call it strip. So our strip and all that's going to do is take out those lines in there. So if we run this again, we're not going to see all those extra spaces. And in fact, that's actually not where we want it to be because we're printing it. This is not going to print anything out for us. We want to change what's being exposed in our terminals. We do strip here. And when we run this once more, let's get rid of that hover there. We run this once more. There we go. It's getting rid of that extra line for us. And again, the reason that's happening is because if we print all these emails, we're going to see that this array here, this list, this Python list has a slash n at the end. And that's because at the end of each line there is a slash n. It's an invisible character, we don't see it, but it is there. And that tells the program, that tells the computer that there's a new line here. And so all we're doing is saying, hey, instead of having slash n there, strip it out. Now what I would like you to do as your task is something a little bit easier. Try just opening a file called e-mails dot TXT. Throw a bunch of emails in there, make sure it's readable. And then I simply want you to loop through every email and print the email. Try this out. And if you notice you have extra lines in there, try using our strip that's simply going to get rid of the slash n at the end of every value. Go ahead and do me a solid, try that out. It's not going to seem like this is actually super useful at the moment. But when you get into web scraping, which is what Google does, Google scrapes every single website on the, on the internet and indexes it in a database. It's looking for strings inside of strings. It's looping through every single page out there. So instead of looping through every single email, it could be every single website. And then it's going to do a thing. And that could all be stored in a single file. So that's a fun real-world example. Yeah, definitely try it out. And when you're ready, let's head on over to the next lesson where we actually ask a user for some user input and then create a file that's then going to be executable. 7. Writing a file and executing it: Okay, welcome back. Let's talk about executing files, or I guess creating files from userInput. And so the idea here is that you can ask the user for some form of input. And then you can create a file out of that. And with that file you can add some content in there as well. So let's merge all the experience that we have with reading and writing and appending files into a program where you can basically create your own form of catalog or, or contact book or, or address book. So first let's let's ask for filename. Filename is going to be input. What is the file name? And let's also ask for content. Input is equal to enter the content, whatever that content is going to be. Then we can say something like width. We're opening a context manager here, open that file name. We're going to write or overwrite it every single time as file, then we can say file dot right, the content. Now if we try this out, python executable user files, it's going to ask me for a file name. So let's put Zed, Zed, Zed 2.txt just so it shows up at the very bottom here, it's easy to spot under some content. Hello, my name is Bob and I like to code. Okay, looks like it did nothing but, hey, check that out. Zed, Zed dot TXT showed up. And boom, myText is now in there. Now get this, we can actually start making this a lot more enjoyable for the user. We could say open file is equal to input. Would you like to read this file? And this should take a yes or a no. So we could say if open file is in Y or N, then we can do a thing. We say, if open file is equal to N, do nothing. Or we can say if the open file is equal to y, let's go ahead and open this file now. So we can say with open that same filename from above with reading properties as file, print, file dot read. And so the whole script looks like this. We're asking for a file name. We're asking for some content. We're then going to write that filename with content inside of it, and it's completely dynamic, it's up to the user. Then we're going to say, Would you like to open this file yes or no? Open file is in a list of either Y or N. Then execute this code. If open file is equal to y, then we want to open this file and print that file out. Let's go ahead and give this a shot. Let's do YoY dot TXT is the filename, enter some content. Content is king. Remember when people used to say that on social media content is king? And would you like to read this file? Let's type y. And it prints it out for us. It read that file for us, and that's exactly what's NY YY 2.txt. Now I'm gonna delete this file. I'm going to delete this file as well. And I'm going to leave this source code with you. Now. I want you to try this out, just tryout lines one through five. You don't have to worry about lines seven through 11. Drew just getting fancy there. We're just we're basically showing off her skills just because we can, which is a very programmer thing to do. We like to do that. But try this out and ask for a file name. You some user input asks for some content to use a more user input and use a dynamic filename and dynamically right, some dynamic content. And then make sure that file actually shows up where you think it's going to show up and open it up, make sure that it's doing what you expect it to do. And if everything is looking good, looking right? Let's move on to that next lesson where we talk about functions inside of functions because surprise, surprise. Everything in Python is called an object. Yeah, it's a mind-blowing concept, but we can nest things within ness things and you can actually see it here. We never had a with statement inside of an if statement, inside of an if statement. And this can go on forever. It's not performance to do that forever, but we can do that forever if we wanted to. And functions are no different. We can have a function inside of a function inside of a function. So the next lesson we're going to take a look at functions and some of the weird scoping that happens inside of that. 8. Functions inside of functions: Let's take a look at functions nested inside of functions, because in Python we can do that. In Python everything is called an object, and that means functions inside of functions or objects, files or objects packages are objects. Everything's an object, you get an object, Eigen object, we all get an object. But in this course, not in this course, but in this lesson, let's take a look at scope and how it affects functions inside of functions. So let's create a function, let's call it thing one. And then we can have a function inside of this. And let's just say print. Welcome to thing one. And that's tighten that up just a quick sec there. And let's have another function here. Thing too. Print. Welcome to thing too. And to execute this inside of this function. Or rather to execute thing too, we simply executed inside of thing one, so we say thing to execute it. And then when we call thing one, this is going to execute, it's going to print, it's going to then register this function and the code inside of this function and then execute that function inside of it. And so now we have two functions and one here, and it's really only required to call one of them. So let's go ahead and save this. And let's go to View. Hear me now. And let's type python executable user files. That's not right. That is Doo-doo-doo-doo. I got to undo that. Alright, so I just changed the name of that file there, so we're not overriding the file from the last lesson. So what I wanna do here in my terminal type Python functions inside of functions. And we're going to see it says Welcome thing one, welcome to thing do. And that's again because if we separate this, it might make a little more sense to our eyes. But thing one, well, we know that's a regular function thing too, is then going to register or be registered inside of thing one. And then we executed it. Now if we didn't want it to be executed, well, why would we read it at all? But that's beside the point we could just simply not executed inside. And this is simply going to say welcome to thing one. So that is a function inside of a function. And ultimately that is what is called a decorator, a deco rate or something like that. That is, the premise of a decorator will talk about decorators probably in a future course or a future module. But that is basically what a decorator is in Python. So now let's take a look at scope. Let's say thing one takes a name. But thing too is not going to take a name. So we can say Welcome to think one common name. And how were we going to pass thing to that name? Well, we could also do this and we could say name. And then we could say name in here. And then if I said Jacob, this is going to say welcome to think one. Jacob, Oh, we'd need Doo-doo-doo-doo name in there as well. And so it passes the name along, but honestly to having to type that same variable name over and over and over again is irritating. You see how many times I have to type name here. The nice thing about Pythonic scoping is if it doesn't find a name inside of this function, if it doesn't find this particular name and this could be called literally anything. But if it doesn't find that variable, that parameter, that argument inside of its function, this is going to look outside of the function. And it's going to look into things, one for a variable called name. And so let's go ahead and test this out. And let's not pass a name whatsoever. And so thing too is just going to say welcome to think too. It's going to have a name in there, but we're not actually passing anything n. And so we see this a lot in the world of JavaScript, but we don't see this a lot in the world of Python. Let's go ahead and execute this. And we're going to say what? We're going to see that it says, Welcome to think one Jacob and welcome to thing to Jacob. And that's because even though we didn't pass in explicitly the name parameter, what it did was look for a name inside of this particular function. It didn't find it, so it looked just outside of that function. It found it inside a function thing one. And the nice thing about this is it's scoped so that if we tried to print out here print name, It's not going to exist whatsoever. In fact, this is going to throw an error name error, name, name is not defined. And that's the nice thing about functions and scoping with functions is that once you have a function, if you have a nested function, you can use all of the same variables from that original function. And that original function i in this case, thing one, is not going to leak out any of its parameters or any of its arguments. And so that keeps your namespace and nice and clean so we don't have to worry about name being registered and magically are mythically doing something when simply it doesn't exist. All we've said was basically if you package this up, you don't see name is equal to anywhere in here, doesn't exist out here. And that is Python's scoping. Now, that's just regular Python's scoping. That's just how Python works. If it can't find the thing, it looks just outside of that thing. And as a recap, functions inside of functions is really you just declare a function, declare a second function inside of it, or a third or fourth or fifth function inside of it. You can do a thing, you can return a thing. You can do pretty much anything you like inside of that second or third or fourth or fifth nested function. And you can just keep nesting away if you wanted to just you have to remember whatever function you register you have to execute as well. And so if you're thinking, well, why would I even do this with one print statement? Honestly, you wouldn't. But if you had ten lines of logic in here and you wanted to reuse that logic two or three times inside of thing one, you would register that as a sub-function. Now your task for this lesson is to give this a shot. Creative function, nested function inside of it, execute the nested function, execute the original function, and make sure you pass in some sort of arguments or some sort of parameter that gets used in both the original function and the nested function itself. Go ahead and give that a try when you're ready. Let's head on over to the next lesson where we talk about making simple API requests. 9. How to make simple API requests: Let's go ahead and make a simple API request. All right, in this lesson, we are going to need to use a package called Requests. So first of all, let's try this out. Let's type Python to get into our shell. Pyt HON, If I can spell that. And then we can just type import requests. And if you don't see any errors, then you're good to go. We can proceed. We can use requests. If you see an error, something like this, import requests to have you see a module not found error, then we have to go ahead and install requests. Now if we have to install requests, that's fine, we can do that. Let's just quit our shell here. And you're going to need to have PIP now you should already have pip installed on your computer. If you type Pip dash v, it will tell you a bunch of stuff in here. And my Pip is using Python 3.8. Now if you do not have pip installed on your computer, if there's command doesn't work for you, head on over to the lesson where we install PIP and we talk about packages and things like that and then come back to this one. So that's a few lessons ahead and then just come on back. But modern versions of Python, if I recall correctly, comes with pip by default. So what we can do here, assuming you have the requests, package or module, you can import requests and then you can simply say REQ for request is equal to requests dot get, and then some URL. So let's do HTTPS, Caleb dot IO. And we can print the status here, X dot status code and not x. I'm used to typing x, it should be wreck. I just changed the variable name, their print req dot status code. And let's go ahead and give this a try. Python simple API requests dot py. And he gives me a 200, now a 200 status and HTTP codes means the site is up and running. Now at this point, you can make a site monitoring program quite easily. We could do something like this. Let's go ahead and comment that out. And we can do import requests. Import time. Wild true. Req is equal to requests dot get and then your website. So HTTPS courses dot coding for every buddy.com print req dot status code. And then we can say if that req dot status code does not equal 200, we could send an email, we can alert, we could break out of this script. We could do anything we want, so we're just going to pass. But ultimately this would ideally sent an email, send a text using Twilio API or something like that. Email me or text me, something like that. And this is going to run very, very quickly over and over and over again. So we don't want to spam our server with requests, but instead what we want to do is make one request and maybe wait for like 60 seconds. And so we could do time dot sleep 60 seconds. And so once a minute this is going to make a request. But just as an example, let's lower that down to like three seconds. And let's run this again and see what happens to a hundred, two hundred. So we're going to see that this has constantly pulling my sites to make sure that it's up all the time. Basically, we just created an uptime program or an uptime monitoring program in like seven lines of code with Python. And so to cancel all I did there was control C. So I'm going to actually comment out this one. You can check out that source code if you like. And I'm going to uncomment the original one because that's all we really need to do for this particular lesson. All we need to do is this. And this is a simple API request. In fact, actually this is hardly even an API request. This is more or less just an HTTP request. But in Python, it's so close to an API, it's basically an API request. In the next lesson, we're going to take a look at making an API request to an endpoint that actually accepts and JSON or not accepted but returns JSON and how we can turn that into a Python dictionary and work with that data. But for now we'll go ahead and give lines one through four a try, just try it out and have some fun with it. See if you can get a different status code from a different website. And if you get a different status codes, something that's not a 200, feel free to go and research what that status code means. If you're not familiar with HTTP status codes. 10. How to make JSON API requests: Alright, let's make a JSON API requests, just a simple one. So we're just going to get some information. We're not going to post any information or put or delete or any other API term. We're simply going to get some information. And this is going to be the most common thing that you do when it comes to Python APIs or API is really in general. So from the last lesson we did import requests. And then we said REQ is equal to request dot get, and then some URL in here. And we don't know if that turned out to be HTML or JSON or anything like that. So we need to find an endpoint. So if we head on over to swapping dot dot, this is one of my favorite API sites because it's themed for Star Wars name. Well, I like Star Wars, but we can also go to swap e dot dev slash api slash, let's say people slash one. And this is going to give us information about Luke Skywalker or people. And say six, whoever that is. O when liars. And we can get all this JSON information. Now what's nice about this is it's not a lot of information, but it's enough to be a great demonstration. So let's go ahead and get swapping dot dev slash api slash people, and then just a number. So that's going to be our URL, HTTPS, swapping dot dev slash, api slash people. Let's do person to whoever that is. And then let's say that person is going to be whatever that request turns out to written to respond with db.json and print person. Let's go ahead and try this out. Python making JSON API requests. And it takes a second and we can see we C3PO. And if you're familiar with Python and data types, this looks a lot like a dictionary, doesn't it? So now we can pull out the name and we can say print. Name IS tab, tab. And let's do a F String variable in here. Let's say person, name. And let's make this an F string. What else can we add in here? We've got a name. We can also add his birth year is person, birth underscore year. And i'm just getting that from here and here. So let's go ahead and run this once more. Name is C3PO, birth year is 112 BY let's add one more tab in here. Just to sort of line this up nicely. Name is C3PO, birth year is 1 12th, b, b, y. And now we have access to all of this information in here. Now what's cool about this is we also have movies don't either called films. And films is simply a list of films that let's say three. C3po isn't. So let's take a look at where 3P o is n, and let's print out which movie that title is called. So we need two. What does that URL? Api films five, films slash five. That's request that. And it's called Attack of the clones. And it's an episode too. So let's say we just want to figure out which movies he's in. We could then say for film in person. Films because this is actually a list REQ for request. Requests, dot get film, URL, print, film is. And then we can do film. Actually we need to JSON FI this first. So let's say film is equal to request dot JSON. And then we can do film title. And this is going to print out all the films for us. Let's also give us a print statement above films involved in. And let's run this once more. And it's going off and getting every single film. So this is making a different, a different API requests for every film that he's in and getting that title. And so now we're dynamically getting a lot of information. We're getting a character C3b. We're checking which videos are, which films he's involved in. And for each one we're getting that data, that whole film in data, including the title and all we're doing is printing the title. And that's why when we do this, you can see that it is coming back one at a time. Film is New Hope, Empire Strikes Back, Return of the Jedi, Phantom Menace, attack of the clones and revenge, or the CIF. Now as a heads up, I do actually believe if there is some data missing from this API, but regardless, it's still a fun API to use. So as a recap, how do we make JSON API requests? It's the same as making a regular request. But if that data turns out to be JSON, we can simply do dot JSON on that request object. And that's going to throw it into a variable. We call it person. And then we have access to all of that data. All of this data in a Python dictionary. So what I would like you to do is give this a simple shot. You don't have to do a for loop here. But what you can do is a simple request or a GET request, and then JSON file and then print out what that that data is. It'll come back as a dictionary once it's JSON defied or read from JSON, rather, it'll come back as a dictionary so you can have access to the name or the birth year or anything else that the API gives you. Go ahead and give that a try and when you're ready, let's talk about how we can convert a string that looks like JSON to an actual piece of JSON and a Pythonic dictionary. 11. How to read JSON in Python: Okay, let's take a look at reading JSON. So in the last lesson, what we did was we took some JSON from an API and we just sort of worked with it. And that was fine because the data was actually in JSON format. It wasn't a string, it was in JSON format. And so it was formidable for us and we can easily turn that into a dictionary using Python. But what happens if this comes back as a string? Now what I mean by that is just that data from C3PO in the last lesson looks a lot like this. So C3PO is equal to, we'll make this a bit smaller here. And it has all this information in here. Now, we can't simply say C3PO bracket name like we did in the last lesson because this is a string, this isn't a dictionary. So if we say C3PO name and we try to print this out. Python reading JSON dot pi, we're going to see that string indices must be integers. And that's simply because it's trying to slice this. It knows it's a string, and so it's not a dictionary doesn't have a key or a property called name. So we need to convert that. Now we can do that with the JSON library. All we have to do is type. I'm going to make this just a touch bigger, their import JSON. And we have C3PO up here. What we can do now is a C3PO is equal to C3PO. But we simply put JSON dot load, load s. It looks like the word loads, but what it actually means is loaded string. So we're taking that string and we're throwing it into basically a conversion tool that's going to turn this into a dictionary for us. And if this is properly formatted, we won't see any errors. And so now you're going to see something like this with this particular example anyways, it's going to say something along the lines of expecting property name enclosed in double quotes. And that's because, guess what? This is not properly formatted JSON and this is a dictionary from Python. Now if we go back here, people such T, Let's get C3PO. You can see all this data has double quotes, are not double-quotes, but not using apostrophes at least. So let's go ahead and copy that. And let's give it properly formatted JSON, which means we have to swap out the apostrophes we were using. And let's go ahead and use three like a doc string. So we can have multiple lines in there. Let's go ahead and try this out. Cool Cocoa, Cocoa, Cocoa, that work. So even though it's on multiple lines, it was totally fine. We have C3PO is equal to this giant JSON string. It's not a JSON object, it's not a dictionary, it's a string right now. And then we said C3PO is equal to JSON dot load s. And then that string, this string up here, and it simply converted it into a dictionary for us. So let's go ahead and type, print type C3PO. And we're going to see that this is going to be a dictionary, it's going to be a DIC t. There it is, class dict. Now what's cool about this is we can then put this out to another file after we added it. So let's go ahead and remove that. And let's change something in your let's change C3PO is name to something else. So we can say C3PO name is equal to my friend Caine as key. And now that's only going to change in the dictionary. That's not going to change it in the actual text here, we need to convert this back. And so to convert it back, we simply do JSON dot dump, S, dump string, C3PO. And let's do C3PO. Underscore string is equal to, and let's print that. Print C3 POSTAR. And due to do this has all of his data. This is coming back as a string and the name is Ksp. We have now taken JSON data as a string and you're going to, you're going to be working with this a lot. So it's important to know how JSON works. Not everything is perfectly formatted in the developer world or in the human world at all. And so we took a string, we said converted to proper dictionary using JSON load s. Then we change the name. Then we said, oh, actually what we wanna do is we want to dump that data. So we change that name, basically, changed it up here. And then we dump that data back into a string using dump S. Dump string, gave it a string and then print it. And that's really all we had to do. Now what I would like you to do is to give this a shot. I want you to try it out. This is really important if you're going to be using JSON a lot, maybe not every single day as a Python developer, but certainly every week or every two weeks. So it's very, very common. Feel free to even steal this code just because it's got the dictionary or not the dictionary, but the string JSON data in here. Don't forget to import JSON, JSON dot load s. And then again when JSON is imported, json.dumps S, and that stands for load string, dump, string. 12. What are function *args?: Let's take a look at function arguments. So in the wild, in a lot of, a lot of Python, especially in Lake the world of Django. You're going to see something like deaf thing, self, request, args, star, star, quarks. And so in this lesson, let's take a look at what args is. Because, well, that's going to come with classes. We don't need self request is a Django thing, but this could really be anything, let's say name. And then it takes some sort of arguments and then some sort of keyword arguments. So let's ignore the keyword arguments for now, and let's just work with args. So first things first, when we're working with our eggs, Let's get rid of this because we're not using it. If we just dissect this function here. What datatype does this look like to you? You should be saying this looks like a tuple. And that's what args comes back as. Args always comes back as a tuple. So let's create a function here where instead of Deaf thing, we're gonna say add numbers. And we're simply going to print the args and let's get rid of name because that is going to be a positional required argument. We don't want that and we just want to throw in any number of arguments. And the nice thing about args here is that you can pass in an unlimited number of arguments and it will just work. So now lets say add numbers 13579. And let's, yeah, there we go. Now this isn't actually going to add the numbers, but this is going to print out 13713579. So function args dot py, and it comes back as 13579. Let's take a look at this. Let's dissect this a little bit. Let's see what this actually is. Is this actually a tuple or was I just pulling your leg? Look at that. I wouldn't lie to you. I wouldn't do that to you. It's a tuple. And so any arguments, not positional arguments. For example, if we have a positional argument called name, I'll use a Caleb as an example. This name here Caleb gets assigned to the first positional arguments. Everything after this positional argument is an, is a star arg, or just a regular argument. And so what this is doing is saying, oh, let's match up that first one. And then if we didn't pretend, where, if we did pretend that didn't exist, the rest of them would be arguments. And so now we can work with all of these. What's cool about this is we can say total is equal to 0. And if we wanted to add all these numbers, we can say for num in args, total is equal to total plus whatever that number is going to be. So it's going to add 13579 together. Return total. And then out here we can say the total is, is add numbers. Caleb, 13579. Print the total. And let's actually get rid of this positional required arguments because we're not using it. So why is it there? And let's run this and we get 25. Let's add this together in our heads to make sure this works. One plus three is four plus five is nine, plus 716 plus nine is 25. And so now we can take unlimited arguments using the args keyword. Now it doesn't have to be a RGS. It just has to be delimited by that star. And that's how Python knows that this is going to be the rest of the arguments. In JavaScript. I believe we use a thing called a rest operator. So a dot-dot-dot. In Python we use a star or an asterisk. And that's simply tells Python that the rest of these functions or the rest of these arguments in this function is going to be a set of arguments. And so it knows to package them up, throw them in a tuple called args, and then we can work with all of those arguments. Now what I would like you to do is give this a try. I want you to create a function where you maybe add all the numbers together or you multiply all the numbers together, do some sort of basic addition or multiplication or something like that. And you can even take names. If you wanted to take a series of names, they don't it doesn't have to be numbers. We just use numbers because it was simple. But you could use a series of names and print out all the names regardless of what you end up trying. I want you to try this out because you're going to see star args everywhere. In professional level, Python, it's very, very important you understand how this works. When you're done that. In the next lesson, let's talk about this thing called quarks, keyword arguments, and that's delimited with two stars. It's a little bit different, kind of the same. 13. What are function **kwargs?: Okay, let's take a look at keyword arguments in a function. So often you'll see def thing, a positional argument, positional required argument. How you could have something like a positional optional arguments. So name aij is equal to 31 by default, then you can have some form of arguments in there, any kind of arguments. These don't have a name. These are simply like 1357 or nine like we did in the last lesson. And then we have keyword args. And this is if full function, this is pretty much everything we can do inside of a function. In regards to parameters. So we know that name is going to be required, absolutely required. Ages, optional, whether it's in there or not, doesn't really matter. Age is always going to be 31. Then we have args. And this could be any number of arguments, can be literally an unlimited number of arguments. It comes back as a tuple and we can loop through the data in that tuple. Keyword args, quarks is delimited with two stars, two asterisks. And what this means is, instead of coming back as a tuple, comes back as a dictionary because keyword args look like this. Thing is equal to something. Or if we do this in Pythonic terms thing something. So it's a little more Dictionary like. So let's go ahead and delete this and let's create an example where we use quarks. So deaf. Let's say a person has some sort of attributes. We don't know what those attributes are yet, but we can loop through them. So first of all, let's print quarks and print type of quarks. And let's execute person and let's say name is equal to Jacob. Aij is equal to 27. Brain is equal to huge. And what's great about this is I actually know a person named Jacob. I think he's about 27. And he's probably the smartest person I've ever met. Python function quarks dot py. And we're going to see that we get a dictionary with named Jacob, age 27, brain huge. And all this did was literally package this up and throw these keyword arguments into a dictionary for us. So now we can work with this. We have something to work with. So we can now say in our function, if, let's check to see if age is in quarks. Print your age is. And then we can do quarks dot-dot-dot because it's just a dictionary. And we know how to use the dot getMethod already. Quarks dot get name. And it's now we're using the in operator, we're using Get, we're using dictionaries were using a conditional, we're using keyword arguments, we're using a function and we're using print statements and name. Why did I put name? I should've put age. If you're yelling at your screen going app. Well, you could catch. It says your are. Just one after another here, just killing it today. You are 27. Cuckoo, cuckoo, cuckoo. Let's just pretend I got that on the first try. So now you might be thinking, where's it's actually used Lake. And this seems kind of far-fetched. And if you're thinking that you're not wrong, but you're probably not completely right either. A good example of this would be like, let's say you want to order pizza from a website. Deaf order pizza. We need a positional argument name. We need an address to deliver it to. And let's say toppings. Now toppings is going to be a keyword argument. And so we can say print order is for name. I'm going to use an F string here. And the address is ship it to the address. Now we need to come up with some kind of pricing. So the price right now is going to be, let's say, a default of $18. And for every topic that we have, we want to add another $2 to it. So now we can loop through toppings. We can say for key value in toppings items. Because we know this is a dictionary and we can simply loop through all the toppings so we don't really care what the topics are. Could be, pepperoni, could be Ham, can be Pineapple, could be extra cheese, could be helping you as I don't know, it doesn't really matter. We just want to figure out the price here. And so we're going to loop through all the dictionary items that gives us a key value pair. And frankly, we don't even need to use the key or value. So the fact that we're using dot items is completely useless. It's just good, for example, code in this video. So let's go ahead and say for every item in year, prices equal to price plus $2. Let's print that out. When we're done. The total price is price, and let's return price. So let's go ahead and now ordered this pizza. How do we instantiate this particular function? We do order pizza because that's the name. Let's say it's going to go to Amanda. She lives in Canada. And what kind of toppings does she want? And say how Pena tos is equal to true to keyword argument. Extra cheese is equal to true, and ham is equal to true. So we've got a positional screw that up just a little bit here. Positional required argument called name and another one called address. Everything else is considered a keyword argument. So if we order this pizza, that's, we can now say total price is equal to this. Let's go ahead and actually execute this Python function, quarks. And it says orders for Amanda ship into Canada. The total price is $24.024. And that's because we have 246 extra dollars. Because every topic is worth $2 and the base price is 18. And this is a real-world example of how you could possibly, if you wanted to use keyword arguments. You could also check to see if addresses in the keyword arguments. Because if address was in the keyword arguments, you can either ignore it or if it wasn't a positional required argument like this. If it was in there, then great, you know, or to ship the food to or deliver the food to, you know, do we ship food? Do we deliver food? To deliver the food to? Otherwise, if addresses not in toppings. And we're simply checking to see if there is a key inside of a dictionary there. If addresses not in toppings, you're going to need to ask the user for an address. And this is a real-world situation. This is how you would order pizza using Python. Now I would like you to give this a shot. I'm going to undo that to this code actually works. Go ahead and create a function that takes a couple of positional required arguments and then let it except keyword arguments with star, star. And again, it doesn't have to be called kw args and keyword args. You'll most likely see it that way. It can be called anything. But more often than not, you're going to see it as quarks. Go ahead, try it out. You're going to need to know how this works when we move into things like Django because we use keyword arguments everywhere in Django. In the next lesson, let's talk about how we can install PIP. 14. How to install pip: Let's take a look at how you can install pip. So first of all, what is Pip? Pip is a package. I guess it's considered a package, but people basically lets you install third-party packages using Python. So if you've ever used JavaScript, you might have used NPM. In PHP, it's called Posner, I believe compose composer. I think it's composer and it's been a little while. And in Python we call it PIP. And so how it works is we can say pip install package name, and this makes it available on our computer for us so we can now use other people's code on our computer. Now, if you don't have, for example, requests installed, you can simply do pip install requests. Now let's check to see if you actually have pip installed first, let's do Pip dash v inner terminal. And I have pip installed. If you don't have this, if this gives you some sort of error, like command not found kind of error, we're going to need to install PIP. Now if you already have pip installed and you might already have pip installed, just because Python might come with it. If Python comes with your version of Python comes with Pip, you can ignore the rest of this video. If you do not have PIP, let's go ahead and install PIP now. So to install pip, what we're going to want to do is we want to, first of all, curled is down to our computer. So we want to download this file to our computer. And so we can do that with curl on our terminal, HTTPS colon slash slash, bootstrap dot p, y p. Python packages dot io slash get dash, Pip dot py than we do dash o and we're going to call it get pip dot py. Now and see if I have any typos in year due to, due to due downloading the file. And if I do ls dash LA or if you're on Windows, you can write DIR. And do we have get pip? We have a function here called get pip. Great, that's what we want. So now once you have that file, all you have to do is type Python, get pip dot py. And that will go ahead and get an install PIP on your computer for you. Now that command again is just this. Curl HTTPS colon slash slash bootstrapped dot pi, pi a PIPA dot io slash getDish, Pip, dot PY, get dash, Pip dot pi. So feel free to pause the video here if you need to. If this isn't available for you on this platform, to basically copy and paste, you can just pause the video and write it out. And then you need to run Python, get pip dot pi. And once that's done running, you should be able to do Pip dot dash v. And Pip dash v is going to show you your Pip version where it's being used. So for whatever reason, my Pip version is using 3.8, Python 3.8. And it tells me python 3.8 in here. Now to install a package is as easy as saying something like pip install, color Emma. And this is gonna go off and download a bunch of stuff. It says it's already satisfied for me. I guess I already have it somehow. And I can uninstall by doing Pip uninstall colorRamp. Yeah, let's get rid of that. Doesn't need to be on my computer, so I got rid of it. Another one you might, you might possibly want if it doesn't come with your version of Python is go into your shell and simply type import requests. Okay, that worked for me. But if you see an error that looks a lot like this module, Not Found error, you're going to need to install requests. So let's go ahead and quit. And you can do pip install requests. And this allows us to then in our code, what we saw before was like import requests or import JSON. And that just allows us to import other people's code. Now Python comes with some of its own code, like you don't need the JSON library. It just comes with Python. It's just not part of the core part of Python. So you do import JSON, whereas requests may or may not come with your version of Python. If it doesn't, you have to do pip install requests and then we can in our code, import requests. 15. Mutable vs immutable: All right, welcome back. Let's take a look at this idea of something that can be mutable vs. immutable. And these are fancy words for is something changeable versus an changeable? That's all it is. Developers, programmers, we really like our fancy, fancy words. So we use immutable and mutable, really just means changeable and unchangeable. That's it. But there is something to note here is when you're working with certain variables, certain things are mutable or changeable and some things are immutable, unchangeable. And so for example, you might be thinking that a string is mutable, it's actually not, has not changeable. So if we have some sort of string in here and we get say, the fox jumped over the cow. And then we said string is equal to something else. We might be thinking that this is actually mutable, but what's really happening behind the scenes here is something very, very different, something you wouldn't necessarily expect. So string here is taking this particular string and is jamming it into a little piece of memory on your computer. And we nicknamed it string. That's the variable name. Now behind the scenes, we cannot change this whatsoever. And so if we did string dot upper, and let's print this, let's print string dot upper, and let's also print just the regular string. And let's do Python mutable vs. immutable. We're going to see that one is uppercase and one is not uppercase. And what you actually see here is string is taking all of this, turning it into uppercase, but it's not restoring it back into this particular variable. What it's actually doing is just holding that in memory and then letting you use it. Now if we wanted to, we could try to overwrite this. We can say string is equal to over written. And absolutely this is going to work for us. But behind the scenes is doing something you don't expect. Behind the scenes it saying, Okay, string is equal to this particular sentence here. And then that string is being overwritten. This has a piece of memory. This has a different piece of memory. So we're not putting this piece into this string and then putting this piece into that same string, we're actually overriding it altogether. So we're saying this has a piece of memory. This cannot be changed once it's in there and there forever, or until your script ends, at least. Then we're saying, okay, well we know that there's a piece of memory called string. We're going to overwrite that with a new piece of memory and call it overwritten. So behind the scenes, you cannot actually change a string. Now about wild, because you would think that you could be able to change a string, but it just doesn't work that way in computer science. So another example of this would be like a list. So we could have names is equal to Caleb, Jacob, godly, and Amanda. What's different about this is because we're using a list. And even though these strings in here, these individual items are immutable, the list itself is mutable, means we can change it, we can add to it, we can append to it. So we can do names dot append. And let's add the cat named rhubarb, print names. And you notice how we didnt do names is equal two names dot append. We didn't do that. We simply assign to variable here. And then we said with that variable, add something to it, and then print that out. Let's go ahead and run this and we see the overwritten example. First two lines. Then we see Caleb, Jacob, goalie, Amanda, and rhubarb. And interestingly enough, unlike this, where we did string dot-dot-dot, it didn't save it as uppercase whatsoever. We can see that here. It was uppercase once, but he didn't actually save it as uppercase. Whereas names with a list, it did. We set that variable, whatever that variable is going to be as a list. Then we said that list dot append a new item, adding new item to that list, and it automatically saved it for us. That means lists are mutable, but strings are not mutable. Now, is this important in your every day python life? No, not unless you're going to get into probably data science and you have giant amounts of data and you need to be very performant in web development. We don't really need to know this, but it's a good thing to know nonetheless. Now, as your homework, what I would like you to do is go off and Google is a tuple, mutable or immutable. We know, uh, list is mutable. We know a string is immutable, but a tuple is like a list that you can't add anything to. Is it considered mutable or immutable? You're going to have to go do a little bit of research for this one. When you are done that let's head on over to the next lesson where we talk about this fancy word called E numeration. 16. What is list enumeration?: Kelley Dhaka Lee, let's talk about this thing called E numeration. And really all this is going to do is we're going to loop through some sort of list and we're going to get the index number from it. So we could create a list, let's say animals is equal to a list of gulley, rhubarb, Zafar, and Henry. And then we can say for animal, in animals, print the animal, but how do we know exactly which order this isn't? How do we know that rhubarb is going to be number two? Or how did we know that Henry's going to be the last one? Currently we don't. And so if we execute this code, we're going to see that this is a standard for loop. We learned about this in Python one-to-one, nothing fancy, nothing new, nothing exciting. So if we wanted to get the numbering in here, we use this function called enumerate and we wrap our list in it. And what this is going to do is if we try to run this, check this out. It gives us a tuple. And it's actually every single item in here is a tuple. It's a tuple of tuples, a list of tuples rather. And so 0 and golly, one rhubarb to HHS effort three, Henry. So now we have those index values. And because there's two of them in here, we can do this thing called unpacking. So we can do index and then animal. And then if we print just the animal that's going to show up just like the first time. We could print index then that animal. And we can see here 0 gully, one, rhubarb to Zafar, three Henry. So now we're starting to get that index number. Now we can actually do something with this. We can say if index modulus two is equal to 0, so every second one continue. And then if we don't continue, if the index with a modulus of two has a remainder of 0 or doesn't have a remainder of 0, print that animal. And so it's not only gonna show every second one. This shows rhubarb and Henry. Or as an alternative example here, what we can do is we can print with an F String index plus one number. Let's do a tab that's not do space tablets, just do tab. And then the animal. And this is going to look like a proper numbered list for us. 1234, Kelly rhubarb is EFR and Henry. So now we're getting somewhere with this. Now, enumerate comes in pretty handy. I use as probably once or twice a week in professional level code. And so you're going to need to know what enumerate does. And really all it does is say, hey, let's give it some index values here. So this is going to be 0, this is going to be one, this is going to be two, and this is going to be three simple add Is that all it does is add index values because sometimes you need those index values. Go ahead and try this out. In the next lesson, we're gonna talk about list comprehensions. 17. List comprehensions: Alright, let's take a look at a list comprehension. So a list comprehension is really just a shorthand method of writing a for-loop and putting all of that data into a list. Now, we would see an old example, like items is equal to something like that. Actually it's not due items. Let's do. Numbers is equal to a list of numbers. For num in 13579. Numbers dot append whatever that number is going to be to the power of two print numbers. And so we see 19, 25-40, 981. And so all we did was exponent the number against the number two. So we said one to the power of 23, to the power of 25 to power two is having a priority of nine to the power two and you get it. There's a quicker way to do this in Python. This is a very Pythonic thing to do, and it looks very bizarre at first. But instead of doing it the long way, we could say numbers is equal to some sort of value for a value in a list of some kind. And so what we're saying here is look at this for loop. And this looks a lot like for num in a list of 13579. And check this out even when a highlighted, it's the exact same. And all we're doing is returning that value on the left. So now we have numb. We can use numb instead of x num to the power of two. And let's print numbers once more. And let's run this. We're going to see that this is the exact same thing. So now instead of writing 123 lines of code, you know, granted it's only three lines of code. We wrote it all in one line of code. And so again, we just said the answer for some sort of iteration or iterable in a list of things or a list of iterations. And we have brackets around it. And the brackets around it means that this is going to be a list comprehension. We can do something very similar with dictionaries called a dictionary comprehension, which we'll learn about in the next lesson. But for now, we just need to know that we have a hard bracket here, looks just like a list and we have some sort of weird stuff in here. Now what I want you to do is I want you to try this out because list comprehensions are very, very popular in Python. They're significantly more popular than a dictionary comprehension. And you're going to need to know how these work. So when you see it, you don't just look at it and you go, oh, I don't know what this means. I don't know what this is. I don't even know what this is called. You now know that this weird syntax here is your answer for your iteration in an iterable wrapped in a list. And that is identical to what we wrote here. It's just a little bit shorter. So go ahead, try it out. Have some fun with it. Try to break it, tried to understand it. Probably spend like five minutes on it in just mess around and see what you can and cannot do. If you want to take this one step further, I would definitely suggest going off and researching how you can add an if statement in here. Because you can have if and else statements inside of list comprehensions as well. So you have one big line with a for loop. And you can have an if statement inside of that for loop and only output something if there is a particular answer. Go ahead, try it out when you're ready. Let's head on over to the next lesson where we learned about dictionary comprehensions. 18. Dictionary comprehensions: Welcome to dictionary comprehensions. So like a list comprehension, a dictionary comprehension can be written in one single line as well. So let's go ahead and create a long example and then we'll recreate it with a shorter example. So let's do an example where we want to create a dictionary out of a list of tuples. And so we can say something like this. Names is equal to, we've got a list and then a tuple in here named Caleb, occupation, coder. Now to turn this into a dictionary, we'd have something like d is equal to and then four. And because this is a tuple with two items in it, we can unpack it. So we can say for key value in names, D0, C0 is equal to value. And then we can print out whatever D for dictionary is. So Python dictionary. And you can see there's a dictionary now name Caleb occupation coder. We could even double-check that it is in fact a dictionary, although, I mean, we can look at it by its syntax. But let's see what this looks like. It is in fact a dictionary. And so all we did here was create a dictionary from a list. The long way, there's a shorter way to do this. So let's go ahead and get rid of that. And I'm gonna make this just a little bit shorter. So we can see the code. So now we can say D is equal to a dictionary comprehension. So we usually put some sort of key, some sort of value for key value in, let's say names, which is an iterable. So let's actually write this out the long way. So this is a key colon value for every key value in names. Let's go ahead and print out d again, and we're going to see that it's the exact same, is going to look the exact same top and bottom, just like this. And so all we did here was we opened up and closed with curly braces. We said the key colon, just like a regular dictionary when we're creating a dictionary from scratch. It then has a value. And then we're saying that key and value are coming from unpacked items. In an iterable. Qi was the first unpacked item, value was a second unpacked item. And names is the iterable up here. Now, That's a lot easier to write than having to write these three lines. And you can also add if statements in here as well to make sure that you only add certain information, which I'm gonna let you research on your own because that's an important part of learning how to code. But there's also one more way. We could also simply say D is equal to DECT names. Check this out. Simple typecasting at its best. So we're going to show up, we're gonna print D3 times and check this out. We were able to convert names to a dictionary automatically with a single line of code. And we used like for six characters plus a variable name. So there's a few different ways to do this. Now, a dictionary comprehension is not as common as a list comprehension. List comprehensions are wildly popular. In Python. Dictionary comprehensions popular, not as popular, but you're still going to see these and you're going to need to know how these work. So go ahead and give this a try. Feel free to just run this example lines one through three. Just, you're going to loop through an iterable called a list. And that list is named names. Every item in that list is a set of tuples or a tuple set that can't remember what it's called there, but it's got a first value. Second value, comma, another tuple, first value, second value. And you can unpack that using key value in that iterable. And then you can create a dictionary comprehension. Then print that out, make sure that is actually what you expect it to be. I'm going to undo this code so you can see the long way. Go ahead and try it out. If you get stuck, definitely come check out the learning to code Facebook group. We are here to help you learn Python. 19. How to get dictionary values: Let's take a look at how we can get dictionary values. Now I touched on this a little bit at some point, a few lessons ago. But if we have a dictionary, we can check to see if a value exists and get that value by its property name. So let's create a dictionary here called courses. And let's say we have a JavaScript course, a JS course, and it's called java script 101. And we have another course for Python called Python 101. We also have another one called Python 201. That's, let's maybe let's make this one a list, Python. And there's also like HTML 101 and all that good stuff, CSS 101. This one's going to be called HTML 101. And so let's say we wanted to get JavaScript. We wanted to get this out of here. Now, we don't necessarily know all the time, all the values that come in a dictionary. Typically we do some print debugging where we do print courses. And then if we see it in there, we can do print course JS. We could do something like that, but we won't always know and sometimes print debugging. It. It's okay to do, but it's, it's sort of painful to do at the same time. So there's a better way we can do this. So we can actually say Courses dot get JS and you're often going to see this. Courses dot get JS, none. And this is what you're going to see. Dot get some property name which matches our lion on line two or property on line two. Or a key name sometimes call property sometimes as a key. They're interchangeable terms. Or if there isn't one set some sort of default. And the default here is going to be N O N E is going to be None. And so if we print this out, we're going to see that it gives us JavaScript 101. Now, that's good. That's exactly what we want. But what if we wanted to get CSS? Well, there is nothing in there, so this is going to print absolutely nothing, just none. None in there because there's nothing there. And we can also give it a default, default text in here. And that's going to give us the default text in here, or better yet, because we're going along the sort of pattern of JavaScript, one-to-one HTML, one-on-one, Python, one-to-one, et cetera, et cetera. We could do CSS 101. And so if it can get CSS 101 out of this dictionary, great, it's going to, otherwise it's going to default to CS101 to, when we print this, we're going to see it says CSS 101. And that's because CSS as a key, as a property inside of this dictionary simply does not exist. So it falls back to CSS one-to-one or whatever we decide to give it. Now where this is useful is if I comment that out, we can say if Courses dot get CSS and we're not going to give it none because that's what the default is. We can simply say print. You are enrolled in baseball that right, enrolled in CSS 101. Now, watch this. This is not going to print anything, does absolutely nothing. What if we say, if Courses dot get and gave it some sort of value in your CSS, one-to-one. Is this going to execute? And this is a good question to be asking yourself. It says you are enrolled in CSS one-to-one, and that's because it couldn't get that CSS value. So then it went and set a default one. And this is a lot like saying if a string is equal to CSS 101, which is often why you're going to see this. And this is just a little more explicitly written. So this is going to say if Courses dot get CSS, if there is one, cool, execute this if statement. Otherwise none. And we know in conditional statements with Python that if you say if none, this is going to be the same as saying if false, it's not going to execute. So when we try this out again, it's not going to print anything. I can do this over and over and over again. Literally just prints nothing. Now you're going to see this all the time, non-stop probably every single day as a professional Python developer, what you want to do as your task are what I would like you to do as your task is get a little hands-on experience with this. Go ahead and create a dictionary. And in that dictionary I want you to go and then use dot get and then one of the key values and give it a default. And just play with an if statement. Basically just do exactly what I did in this video, even if it only takes you thirty-seconds, you might think, oh, well this is meaningless. But really you're building up a lot of muscle memory here. And what's nice about muscle memory is as soon as you see it, as soon as you start typing it, eventually at some point it just becomes second nature. You don't have to think about it. It just happens. You just know Python. Go ahead and try that out. In the next lesson, we're going to talk about something a little more complicated called a virtual environment. 20. Create a virtual environment: Alright, let's talk about virtual environments. A virtual environment is a way to compartmentalize packages. So for instance, I can use pip to show my packages. I can say Pip show Django. And I have django installed here. I'm using Django 2.2.1 three, and that's on my computer. Now what if I have a project that's using a different version of Django, say Django 3.1. And then let's make one step more complicated. What if I have five different websites using Django, but they're all using different Django Packages. Currently what have to do is Pip uninstall Django and pip install Django is equal to 3.1, whatever it, I'd have to do that over and over and over again. And you can actually see when I'm using Pip show jangle here that it shows you exactly where Django is, and this is right on my computer. It's in library frameworks, Python framework versions 3.8 lib Python 3.8 site dash packages. That was a mouthful. But what if you have, you know, a project, let's say a Django website again, where you want to share this with everyone else in your company. And you want them to use Django as well. Well, instead of packaging up Django and shipping it with your website and sending it to everyone hits like zipping up a file and sending it to everyone. You can just simply tell them to install Django using a certain version. And we do this and we separate these different versions where we separate these different environments are these projects using a virtual environment? And this is a very, very common thing in the world of Python. And so to get into a virtual environment requires one of two things, one of two steps rather, either you're going to be on a Unix based system like Mac OS or Linux. They're very, very similar. Or you're going to be on Windows, I'm going to show you how to do it on both. So to create a new virtual environment, first of all, let's do ls dash l a, or if you're in Windows, you can type DIR. And if I scroll up here, usually you're going to see a folder called dot vendor or just vend or n. These are the three most common i have seen out there. So we don't have any of those yet. Let's go ahead and create that so we can do Python, dash m, v and v dot v and v. And that's the folder we're going to put it in. And it's going to take a second and do a bunch of thinking. Vs Code are smart enough. It said we noticed a new virtual environment has been created. Do you want to select for this workspace folder? Yeah, OK. Might as well. You can see over here I've got a folder. If I do ls dash LA or on Windows DIR, scroll up. I now have a folder in here called dot vendor. I also have a VS Code, one that's just VS code being VS code. We don't have to worry about that. But I've gotta VS, V0 and Vi folder in here, and it's got bin lib include pi n, v dot CFG. If you're using pi n, which I am, you might not have that. But that's how we create a virtual environment. Very simply, it's just Python dash m, v dot v NV or whatever you wanna call that folder, I usually call it dot v, n v, So it's hidden. And we try not to commit these to our Git repositories. Now, that's all well and good. But if I do Pip show Django. It's still using the exact same location. It's still using the exact same version as before. We're not actually using our virtual environment. We have to get into it, so we created it. But now we have to get into that virtual environment. So if you're on Mac or if you're on Linux, you can type source dot VN been activate. And you can see it says dot VN over here. I'm in a virtual environment now. Now watch this. If I do Pip, show Django, nothing shows up. It's not installed in her virtual environment. And what's cool about this is I can do pip install Django. And I'm gonna give you Windows instructions to get into your virtual environments as soon as this is done. So this is just going to go off. Download Django, Pip, show Django. And this is now using Django 3.2.4. It's in my Python 201 folder, dot Ven in its library, Python 3.8 site packages. Cool. So let's get out of this. We get out of this with deactivate. And that just got me out of my virtual environments now to get back into the virtual environment, if you're using Windows, this one is a little bit trickier. Typically we do something like backslash, dot, dot scripts, dot activate, dot bat. Now, this highly depends on your operating system. If you create a virtual environment inventive, it's going to be in dot v Drayton here. Otherwise, what you're often going to see a dot or not dot but slash ENV scripts with a capital S activate active eight dot bat. And that's how you get into your virtual environment using Windows, it's going to be one of those two options. So let's get back inside here, I'm going to do it and the Mac way, so source dot VN been activate. And now I'm inside of it. Pip show Django. And this is going to show me jangle with 3.1.4 right there. Deactivate to get out of it. Pip, show Django again. And my computer is using version 2.2.1 three. So my computer has access a Django, but it's an older version of Django. My virtual environment, which is brand new, has a brand new version of Django in it. And so now I can use two different versions of Django or any Python package and make sure that they're not going to conflict. I can have another virtual environment on another project using a different version of Django. And so the whole idea here is that if you have multiple projects on the go, it's really, really easy to just upgrade one project and not have to worry about upgrading another project. Because if you upgrade one project and you have to upgrade all your other ones, that's going to suck. I have over 200 websites on my computer from my job right now. And I could not imagine having to upgrade Django and 200 sites every time I wanted to do a little bit of a version bump. And so this just keeps it nice and simple and separated for us. So typically whenever you are working on some sort of Python project, you are going to create a virtual environment. It's always a good idea, even if you only have one project on your computer, make sure you create a virtual environment. And again, that's just Python dash m, v dot v and v. And then it's going to be source dot VN, been activate. Or if you're on Windows, it's going to be scripts slash, debate, dot bat. And that's going to get you in. And then once you're in, you simply type deactivate to get out of it. Go ahead and create a virtual environment and then get inside of it. Do something like pip install Django and then deactivated. Once you have that virtual environment up and running, you've been able to enter it and exit it. Perfect. Let's head on over to the next lesson where we talk about a better way to learn Python using a new type of Python Shell. 21. How to learn Python by exploring and using iPython: Let's take a look at how we can learn Python in a more interactive way. So we can always go into Python. And we can create some sort of object and then hit dot and then tab. And it's going to show us all sorts of methods on it. So for example, we can have a string called name. We're going to call this Python 201. And then we could do name dot, hit tab a couple of times and we can see all different things in here. For example, we could capitalize this. It's already capitalised. That's not going to do anything. Named shot. Ends with we could do 201 and this is going to return true for me. And so we can see all these different things and we don't have to do it with just a string either. We're just happened to be doing with this string. We can do it with a tuple, a dictionary, a list, pretty much any data type that has some sort of transformation on your data, you can do this with. So for example, if I wanted to put this into words, We could do name dot and then where are you? It's called split, right there. Split, name dot split and we want to split it on every single space. Now it looks like it did nothing but words is now a list. We can do words.py reverse. And let's take a look at this words DOD hit tab a few times, reverses at the very end. Reverse. It's backwards. And now we can join this back together. And then we can put this back together by saying courses equal to a string dot. And we had up here in string join. How do we wanna join these together are what do we want to join together? Rather, words, since we're saying, use a space to join all the words together. Course. And now I just reversed all the wording in a course. Now instead of saying 201 or instead of saying Python 21, it says 201, Python. And this is a really, really, really powerful way to learn how to code. So if I clear that out again, what we can do is words. I've got 201 and Python in their words dot. Let's pop that returned the word Python. And if I do words, I only have that one word in there I can do pop again. It gives me that last word to a one. Words is now an empty array or an empty list. If you have some sort of dictionary, you can always do a dictionary dot. And then hit tab a couple of times, make sure it's an actual dictionary, so have a proper value in there. But then you could do your dictionary dot tab or tab a couple of times sometimes. And it'll show you everything you can do. So what I want you to do is I want you to create a tuple, hit dot, then hit tab. And then I want you to see what kind of things you can do with a tuple. And then I want you to do the same thing with a list, because lists and tuples are almost the same thing, but tuples aren't really mutable. You can't really do a lot with a tuple. And I want you to compare the differences. And then just explore, feel free to explore you, you. The worst you're gonna do is a little bit of damage to your variable, not your computer, not your virtual environments. Everything else will be totally fine. You're just going to transform your variable. That's all you're going to do. And this is a very, very, very powerful way to learn python. Django, flask, really anything Python related like wag till CMS, anything really. You're going to be able to do it. You're going to be able to learn it a lot faster this way. And that's because simply a course should not go over every single item in here. For example, name dot. I'm not going to teach you format map or is numeric, or is printable or partition or anything like that because there's just too many options. Same with a list. There's a lot of options and you're now this doesn't sound like a lot of options, but imagine what you can do with a dictionary mapping, what you can do as a class, imagine what you can do with a tuple or a set, and having to go through all of those one by one is honestly it's painful for you to watch. It's also painful for me to teach. And frankly, you're not going to use most of them. So whenever you see some code in the wild, you, let's say you're working with the senior developer and a senior developer has some really fancy code and you're like, whoa, what does Words dot extend do? Well, now you know that you can go and experiment with words.txt or list.append rather. Now this is one way to use your shell and that's just simply typing Python that gets you in their Another way is you can use iPython and I really, really like iPython, So I do pip install iPython clear, and instead of typing Python, I simply type iPython. And you can see it's a little nicer in Your name is equal to k lobe, tall, lean. Name dot, hit tab a few times. And now we can actually move through these and it types it out for me. You can copy and paste a lot easier in here as well. You could do multiple lines, you can do all sorts of stuff. And this is an even better way to explore because it's a little more interactive. It's easier to be like, oh, what is zed fill takes exactly one argument, 0 we're given, let's fill it with a one. Let's see what happens. String can not be interpreted as an integer. Okay? Cool, I have no idea what zed fill does. I'm going to explore that after this video is wrapped up. So fun, fun. That's how we learn. And that's how you can learn on your own so you don't need to take another Python course or another full stack web development course or watch another hour-long tutorial on zed fill. You can explore it yourself and then figure out what you need to learn from there. So you're maximizing your time by doing your own exploration and getting your own experience. In the next lesson, let's go ahead and talk about packages. 22. Introduction to Python packages: Let's talk about packages now. Okay, so we have this idea of a package and we've dealt with this a little bit. We did pip install iPython. That's not how you spell python, IPython. We did pip install Django. We've been at one-pointed pimping pip install colorRamp, and we're going to do pip install called Rama again. But first we're going to get into our virtual environment. So if you don't have one Python dash M, ven van Gogh, Coca-Cola, source, bin activate. I'm now inside of my virtual environment. I can now do pip install colorRamp. And I know that color Rama is a package that we can use and it's gonna go and install it. Cool, cool, cool. It says successfully installed color Emma, 0.4. point. For now, let's go and take a look at some of the documentation, your color Rama on pi pi. And this is where you can find any sort of package pi, pi.org. You can search for anything. So for example, if I wanted wag tail packages, he could find different ways to tell packages in here. If you wanted to find Django Packages, you literally just type Django and you'll find all sorts of Django packages for you. And you're, the one I'm going to use is color Rama, color M0, 0.4.4. And it tells us how to install it. That's great. Now how do we actually use it? And it tells us in your applications should initialize color Emma using color Rama import init and then, and then actually instantiate that init. Okay, so let's go ahead and do that. So let's go into a regular Python shell nut, a IPython Shell, just a regular Python shell. And actually what I'm going to do is I'm gonna change my mind on that. Instead of using regular Python shell, what I'm going to do is use iPython because we installed it, might as well use it from colorRamp, call low RAM, import init. And then we wanted to run init. Okay, cool. It looks like it did nothing but were following the instructions so far. How do get colored output? So let's go ahead and do, let's honestly just copy and paste this. Copy that whole thing. Paste the whole thing in there, and look at that. We have some red text. We've got text with what presumably outside of IPython would have been a green background, some dimmer texts and normal text in here. So all we did here was we said import. For backend style. We said foreground is going to be read. The background here is going to be green with some green text. Then style Dim, Dim that text. Style, reset all of it and it's back to normal. That's all we did. And so to use a package, we really just do pip install the package name from that package name, import something. Now we don't know what exactly we can import. And that's why it's so important to read the documentation. And it tells us all sorts of things that we can do in here. Now, this is kind of an ugly example. So let's go ahead and run this in our regular Python Shell. Let's do Python dash v. I'm using Python 3.8. You can also try it just in your regular Python shell. It's probably going to turn out better in your Python shell. But what we can do instead here is we can do to do, to do, let's import this. Let's do from color Emma, colo Rama, import init. Then we need to instantiate lies in it. And let's go ahead and save this as packages dot py. And I'll make this a little shorter here. And what I'm gonna do is move all my imports to the top of the script here. And we can actually even merge those together. And we can say from colorRamp it import init for backend style. Let's go ahead and run this now so it save, and let's run this in Python. And there we go. It says some red text. And with a green background, that's actually a super light green backgrounds. Maybe IPython was right and I just can't quite see that it's super dim. We're keeping the dim red text with a green background. Then we're resetting it back to normal. And we're running all of this inside of our packages dot py script. And so that's really all there is to a package. Remember all you have to do is pip install the package name. And then we do from package name, import, thing one, thing, two things. And we just follow the documentation. And the key here is really follow the documentation. Even on pi-pi here, I typically don't even follow documentation from pi pi. I typically go straight to github, github, github, git hub. And this is going to tell me, I think this is the exact same, so great. But it also tells me other things like available formatting constants are for foregrounding. Black, red, green, yellow, blue, magenta, cyan weight, reset, background, same thing. Style. We can dim, normal bright or we can reset all. So what I would like you to do is pip install colorRamp. Go to this colorRamp GitHub page. Read through the documentation, make sure you import color Emma and import init from color Emma. Run init and then basically just copy and paste this. That's what I did. And just have some fun with it. Because now you can make your terminal look next n colorful, Which is amazing because terminals are notoriously boring. Now you're going to be using packages all the time, nonstop. Every single day. You're going to be importing things every single day. So it's important that you know how to use packages. Go ahead and try that out and when you're ready, let's head on over to the next lesson where we do a little API requests refresher before our final project. 23. A short API requests refresher: All right, welcome to the API requests refresher lesson. What we're going to do here is just a quick little refresher on API requests. So if I open up making JSON API request dot pi, let's go ahead and take a look at what we originally wrote. And so we're not going to do any coding in this video. We are simply going to go over how this works. So requests as a package, we import it and just like in the last video where we imported colorRamp, we imported requests. And with that came an object or a method called get until we do our EQ, this could be anything Is equal to request dot get, and then some sort of URL. And that's just going to fetch that data for us. Then if that data is JSON, we're simply going to say that request object dot read the JSON. And what that does is it turns that JSON into a Python dictionary, which we can use later. So in this example we were using, I believe this was C3PO, and this is now a dictionary and we can print person name because name is a property or a key on the person dictionary, birth year was also available. So we put person birth year just like a standard dictionary ends because this is coming in as JSON, which technically is just a string, but it's formatted in a certain way and Python recognized that and turn it into a dictionary. We also said for every film that this person was in, for every film C3PO was him make a new request to that film. So this was a list of films. And so this is going to go through every one of them. I think there was like seven of them or something. So the data wasn't completely filled out yet. But then we said, for every film, go ahead and get that fill. Then we said overwrite that on every iteration, so overrate film. And because we know it's going to be JSON, transform that JSON into a dictionary called film. And then we said the film is and then the title. And we got all of that from swapping dot dev. Now in our final project, we're not going to be using swapping dot dot. We're going to be using pokey api dot co. So it's very, very similar, but instead we're going to use something with a little more information. And so you can see that this as like a lot of different dictionaries in here. So ability is a dictionary, but this has a list of abilities in here. So this is a list and this is just a standard key value pair. So we're going to be working with a little bit of a more, I guess an advanced API. But we're going to be using things in a simple, simple way because Python keeps things simple. So for this video, instead of doing any coding, what I would like you to do is go to pokey api dot SEO and just read through it, experiment with it, click around. Tried to break things even if you're not familiar with Pokemon, it's totally okay. We can just use some base examples from what they provide us. Because in her final project, what we're going to be doing is using this API and we're going to be fetching a bunch of different information. So go ahead and get yourself a little bit familiar with the pokey API. Don't feel like you have to write any code. We're going to do this as our final project. The code part is going to be our final project. When you're ready for that final project and maybe set aside like half an hour to watch the video and work on the project. But once you have that time set aside, let's head on over to the next lesson, the final lesson, where we create a Pokemon character app. 24. Your project: Let's take a look at our final project, or final project is going to be creating a Pokemon character app. So in this, what I would like you to do is let's, let's write out some instructions here. What I want you to ask for user input. So for example, ask for a Pokemon, which Pokemon or we're going to talk about that could be like ditto, for example. Then you're going to need to create an API string in ABI URL. A lot like this where it says, we can go straight here and gives us all the information. We're just gonna take that URL, but we're going to change out ditto for whatever the user input. So step two is going to be created a dynamic URL based on step one. Number three is we need to then fetch that data using requests. So step three is fetch the data from the URL. In step two, step four is going to be print out the JSON and data. But because it's JSON, we can't actually print it out. We need to convert it. So step four is going to be convert JSON to a dictionary. Step five is then going to be print out some Pokemon data. So print out Pokemon data. And that data It's going to come from, for example here. So it has abilities, it has forms as base experienced game indices, a height held items, moves ID has all sorts of stuff in here. So I'm going to leave that up to you to whatever you want to display. So I have now walked you through basically the exact steps you need to create some form of Python program that can accept a Pokemon as a string. And then you can go and fetch that data from an API and display it in your terminal. What I'm going to do here is I'm going to save this as project dot py. Comment this out. And I'm going to then show you how I go about doing this exact project. But I want you to give this a shot, fair, so please do me a solid here and just pause the video and try this out on your own. Feel free to reference these instructions again, but try this on your own. Try to make your brain sweat. Tried to leverage what you've already learned in this course without having to watch the rest of this video. Now, our solutions guaranteed are going to be very different. And that's okay. There's a million different ways to do the same thing in programming. If I write this versus my mentor writing this, we're going to get different code anyways. And your code's going to look different from mine, but that's not the point. The point is, you give this a shot to try this out to the best of your abilities. Now if you get stuck, that's okay. If you get stuck, you can just simply resume the video and you can see exactly how I do it. So I'm going to fade out and fade in and fade out. I want you to pause the video and try this. Good luck. Don't forget to have some fun with this. This is a really good use case of Python and APIs. You're going to need this in web development and all sorts of Pythonic stuff down the road. Alright, let's go ahead and finish up this project, or I guess start the project and then we'll finish it up. So first things first, step number one, we have to ask the user for input. So we're going to say the Pokemon, Pokemon is equal to input. Which Pokemon Do you want to find? And then we're gonna say Pokemon is equal to Pokemon dot lower. And we know that we have to do dot lower and reassign this because strings aren't mutable. We have to basically take that value of Pokemon, whatever the user rates, lower it, and then jam it back into another piece of memory called Pokemon just happens to be the same name here. So that's where mutability versus immutability comes in. Then we need to create a dynamic URL based on step one so that URL is going to be this URL right here. Copy, throw that into a string, but it's not always going to be data. We need to change that. So let's change that to pokemon. And let's make that an f string. So that's step two. Step three is to fetch the data from the URL in step two. So we can say REQ is equal to request dot get the URL. And what do we use request, we have to then import requests, request. And that's actually plural. It's supposed to be requests, so we use requests dot get, then the URL. And that's step three. Step four is to convert the data to a dictionary. So then we can go down here and we can say that data is going to be whatever that request is. Json, we know it's coming in JSON form so we can use dot JSON on it. That's step four. Step five is print-out Pokemon data. So what do we want to print out? Let's print out the name that we found. So we can say print name is tab, tab. And then whatever that name is going to be data name. And if you're wondering where I got name from, data is now a dictionary. Or at least in theory, it's a dictionary. And name is coming from this little fella right there. Let's make this bigger right there. And let's maybe see what kind of abilities it has. So this is a list. We can see that it's a list and it has a list of dictionaries in it. So let's go ahead and look at the abilities. So then we can say print abilities is going to be something we don't know yet. And let's just loop through those four ability in data. Abilities, we can loop through this because we know that this is a list. And so this first ability is going to be limber and the second ability is going to be imposter. So then we can say print ability. And this is a dictionary as well. So for every item in this list is going to be a dictionary. And then we've got one called ability, which is also a dictionary ability. And then we want that name. And let's go ahead and give this a shot. Let's see what happens when we run python project. I don't even know if I have typos. I haven't even looked for typos. Which Pokemon do I want to find? Let's look for ditto. It has limber and imposter in them. Cool, calm, cool. Okay. What other Pokemon to actually know? And char is ARD. Its abilities are, and let's just move this up. I found chars art, its abilities are blase and solar power. Cool. If we wanted to get more information, we could actually get that URL and make a request there as well. And we can see what kind of ability it has. And it has different language settings. How cool is that? We've had an English-language. The first one is a German language flavor text entries. I don't even know what that is. That's beyond my knowledge of Pokemon. But anyways, we can, if we wanted to grab that URL for more details about what that particular ability is actually going to do. So at this point we're pretty much done. That's all we have to do. But what happens if you type in a Pokemon that doesn't exist? As, as, as, as, as, as, as. Oh, no, we get a problem. And this is interesting because where are we here? Json decoder, JSON decode or error expected value line one column one carry. It's basically saying the JSON is not formatted properly. So what we can do here is we can wrap all of this in an if statement. If req dot status code is equal to 200, that means we are getting a healthy status code. We can take all of this stuff, make that just a tiny are there and out dent that. Otherwise. We couldn't print Pokemon not found. And so now we're dealing with proper status codes here. Let's go ahead and try this again. Thread a bunch of junk in there. It says Pokemon not found. And so it went off and went to wherever that is. That's not a Pokemon apparently. And so Pokemon wasn't found. Let's make sure that the status code is actually supposed to be 200 because sometimes it's to, to one. Who knows which Pokemon do I wanna find? Bullosa sore? And look at that, that came back really fast. Name is bulbous or abilities overgrowth and chlorophyll. Cool, cool. Now that's really all there is to this project. Now if you want to take this one step further, what you can do is you can make this always ask for more user input. So let's, in fact, let's do that. Let's always asked for more userInput. So what we're going to do here is we're going to wrap this in a loop. We're gonna say while something is true, put all of that inside of this wild loop. And it's always going to ask for a Pokemon. It's always going to make a request. And then when it's done, it's going to ask for another Pokemon. And that's literally all we have to do to make this a permanent or a proper Pokemon look-up tool. So let's go ahead and type python project at pi. Let's look up ditto. Okay, found ditto. What about Char? Melian? Cool. What about one that doesn't exist, does not exist to spoke my not found. And it just keeps asking over and over again. And if I ever want to quit, I can always just do control C. And that gets me out of that while loop. So now we have a program that's going to constantly ask which Pokemon Do you want to find? This is a great way if you ever wanted to build an app down the road like a Pokemon finding app or Pokemon information app. This is exactly what you would do using Python. So that was my solution to this particular project. Your solution might be a little bit different. Your solution might be a little more simple or might be a little more complex. That's completely up to you. What I want you to do if you haven't done this is I want you to now try this out on your own. Don't reference my code to try it out on your own and just see how far you can get. Make your brains sweat is important to make your brain sweat. It's supposed to feel like a little bit of a struggle. And if you get really, really stuck in years, like I just don't know what to do either consult Google because honestly half of programming these days is just Googling stuff. And if your own research doesn't get you anywhere, then reference this code. And that's sort of going to move you away from being tutorial dependent Lake. This video is, this video is very tutorial style and it's going to sort of move you into the realm of figuring out how to solve your own problems. Which is really, really, really important because coding is all about solving problems on your own. And you're not always going to have a teacher, so you need to be able to do this on your own. I hope this was enjoyable for you. I hope this was a fun little project using APIs and, and all this good stuff we use a lot in here. We use a while loop, we use a package. We used input lower so mutability versus immutability. And F String, we used an API request. We check the status code, so it conditional. We turned code into a dictionary from JSON. Then we printed with tabs using more F Strings. Then we loop through every ability and got the ability name. Kinda cool. So that was nice and short. That was actually a pretty fun small project. Go ahead and try that out if you haven't. Last, but not least, I would like to say thank you for taking this course with me, your teacher, Caleb tolling. This has been a lot of fun. I'm very proud of how far you've gotten. Don't forget. Writing code is all about practice. Just practice, practice, practice, just writing code, solving more problems, writing code solving more problems. That's all it is. Over and over and over again. Once again, I know there's a lot of different Python courses out there, but you chose mine. And I really, really appreciate that. So thank you for giving me of all people a little bit of time from your life. I really appreciate that and I hope you get to build some really cool things in Python in the future. 25. Python 201: Summary: Okay, you have finished this entire course. That's amazing. First of all, pat yourself on the back. You did a great job. I'm super, super proud of you. I have been Caleb Italian. You can find me on Twitter at Caleb Tolkien, or if you want little coding tips and tricks or updates all my courses, you can always go to Instagram.com slash coding dot for dot everybody. Lots of coding tips on there, lots of course updates, if you like, the way I teach, that's usually where I now its new courses and things like that. If at any point in time you're like, oh, you know it a coding community, it would be nice because frankly, coding can be a little bit lonely. You can always go into you learning to code on Facebook. There's 62 or 65 thousand members in there as of the time of this recording. And it's growing by hundreds of members every single day. And you can ask literally any sort of web development question. It's a well-groomed group. We make sure that there's no spam in there whatsoever. We take a lot of care in that group. Me and a moderator. We spent several hours every day making sure that group is well taken care of and has the highest value for you. So you can always join learning to code on facebook.com for free. Thank you again for taking this course. One last time my name has been Caleb telling I'm super proud of you by the way. And I'm hoping to see you in the learning to code Group in another course of mine, or maybe one day I get to work with you. Happy coding.