Fast Track Python for Newbies | Brett Romero | Skillshare

Playback Speed

  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

Fast Track Python for Newbies

teacher avatar Brett Romero, Bitesize Business School

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

28 Lessons (3h 13m)
    • 1. 101 Introduction

    • 2. 102 Python 2 What Is Python & Installation

    • 3. 103 Editor

    • 4. 201 Introduction

    • 5. 202 Variables

    • 6. 203 Conditional Flow

    • 7. 204 List & Dictionaries

    • 8. 205 While Loops

    • 9. 206 For Loops

    • 10. 207 Working With Strings

    • 11. 208 Tuples

    • 12. 209 File I O

    • 13. 210 M2 Challenge Instructions

    • 14. 211 M2 Challenge Solution

    • 15. 301 Introduction

    • 16. 302 Functions & Parameters

    • 17. 303 Lamba Expression

    • 18. 304 Classes

    • 19. 305 Inheritance

    • 20. 306 M3 Challenge Part 1

    • 21. 307 M3 Challenge Part 2

    • 22. 401 Introduction

    • 23. 402 Try:Except:Finally

    • 24. 403 Raising An Exception

    • 25. 404 M4 Challenge Part 1

    • 26. 405 M4 Challenge Part 2

    • 27. Course summary

    • 28. Django promo

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels
  • Beg/Int level
  • Int/Adv level

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.





About This Class

As a developer, you're super busy. Who has time to sit through hours of learning? Now you don't have to!

Instead, you can fast track your Python learning. By the time you're finished with this course, you'll have the confidence to speak and do Python. Best of all, you can basically do it in only two sittings.

We'll start with Python language fundamentals. The course then progresses into advanced topics from there. To make sure everything you're learning is staying in your head, each section has a challenge. You'll try the challenge then come back and view the solution walk through. All challenges include code you can download.

Once you've completed this course, take your Python skills to the next level by learning how to create websites using Python. Click here for the course:

Meet Your Teacher

Teacher Profile Image

Brett Romero

Bitesize Business School


Combining the skills of an #entrepreneur, #writer/#author and software engineer to deliver a unique perspective on creating profits.

See full profile

Class Ratings

Expectations Met?
  • Exceeded!
  • Yes
  • Somewhat
  • Not really
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.


1. 101 Introduction: I like to go over an introduction of what we're gonna cover in this first module of the course. So this is gonna be a short module we're going to start off with. What is Python? So you're gonna introduction as to why you'd want to use it. What you can do about programming for Web, which requires some additional software, Um, which is python basis to see how the differences worked there. In this course, we're just going to stick to the python language and you'll learn in this module how to install it, how to execute python files. So it's It's fairly simple in that regard. And from there we're going to also look at an editor we're gonna use. So the editor will let you type in your code, will have syntax highlighting It will help you with indentations. It's just a lot easier to manager code using an editor, and the one will uses cross platform. So Windows, Mac Lennix, it's also free. So this is what we're gonna be covering in this section. It's kind of meant to just get you up and running with Python, and I let you start executing some python files and have that under your belt. So let's go ahead and jump into this module 2. 102 Python 2 What Is Python & Installation: I'm in python dot org's and this is the website for the python programming language. I want to give you an idea of what Python is before we begin. So python is used for a lot of different purposes because it is a general purpose programming language. That means it could be used on the command line to two different types of analysis. It's really popular in the data sciences for different kind of data computations. It's also used to create websites now not at the command line, like you might you see rotating right here in this console window. But with frameworks like a lot of different languages are used with frameworks. When you get into website development. Now, Django for python is a very popular framework to do Web development. And there are other things like, Flask is well, but what we're gonna do in this course is really get a grounding in the python programming language. So we're gonna be dealing with the console quite a bit. So right here you can see they have different examples of code. And if I click on this lunch interactive show, what it does is actually put you into a Python show. So let's see if I can clear like maybe there's no way to clear. But, ah, these are some of the commands that I had previously typed in so I can actually start executing commands. So one plus one, you get the output of two. I can do a bully in evaluation, so it's five greater than 10 which it is not. So I get a false. So this is just the way to do some quick commands and, um, get a little bit of introduction toe what Python can dio. Now let's talk about installing Python so you'll want to go to downloads. And depending on which computer you're on a PC or a Mac or Lennox, you'll have a different window that appears. So this is for Matt, because I am on a Mac now. I'm using Python to seven for this course. There's a little bit of difference with 36 and we'll talk about those differences sharply, but you can go ahead and download, Then do the installation on your Mac or on Windows, and make sure to check the option as you're installing that you want to add it to your path at Python to your path. Now that we've got these two different versions display here, let's go and look at what are some of the differences then between the two. Okay, so we have a comparison and pipes on two and three. Let's look at Python to so we have more documentation because it's been around longer. And really, this is about Python 2.7, which is what you would use if you're in the two dot store version ing then on the side of Python three and compares to the documentation. There's just not quite as much, um, because it's It's a little bit newer branch of Python. We have more libraries and modules and python to so talk about data science, for example, which requires actually a number of those libraries and modules to make use of the different kinds of computations you're going to do in data science. So python to would probably a great candidate for that. Now, if you look at Python three, there's just not quite as many. Some of them are getting ported over to Python three, and when you even do have a library in both versions, the one Python 3 may not be fully ported, meaning the version two would have more functionality inside of its library. So there's a larger community with python to just because of mo mentum inertia that has been around for such a long time, and maybe not quite as much in Python three. Then we have Python. Three features reported the Python to. So even though they're doing newer things and Python three, they're still making use of them in Python to so you kind of get a little bit of benefit of Python three, even if you are in version two. But of course, Python three is the future of Python, so it's the cutting edge of anything going on a python. There's not new development in Version two. There is in three. So if you want the latest in the greatest Python three is probably where you will want to go and is progressive. So again it's the latest and greatest. That's where everything is happening. Now, if you're doing a brand new application, um, and you're pretty familiar with the libraries that you're going to use your familiar with what functionality exists in those libraries, you may want to go with Python three. But a lot of legacy applications are gonna be written in Python to If you're starting up with the new company, they will probably be using python to to support ah, lot of the older code and to ensure that they get all the functionality. Because again, remember, in Python three, some of it may not quite be there. All right, So what I want to do now is just go out to a command line. I have python installing, unjust show you real quick. What? That looks like a command line. All right, so if I want to check the version of Python, I would do python space dash dash version. So this works the same. Own windows are on, um, Mac or Lennox. You can see here the version. I have him in the 2.7 version, and I've got a few files. Someone do an ls Here, go into this test folder. Ls so you can see these air. Some of the different follows. Actually, we're going to go through these and work with them as we're moving to the course. But so just to show something that would, uh, it might look like if you're doing it from the command line. I'll have Python test P Y soapy wife for Python. The extension on these files. If I run that, then we'll get some output. The only thing in there is a print for Hello, World. We'll look at that a little bit more, but that's how you would run it. If you want to do it from the command line and you can see here, there's a few other things that are printing out as well the pitting on what's in that file . So that's what it looks like and how you do it from the command line. Now in the course, we're gonna use an editor. We're gonna have an integration of command line functionality in the editors that we can do all of this from our editor. We don't have to jump out to the command line to do it, but this is an option, depending on what editor you might be using. So we're gonna next look at the kind of editor we're going to use in this course 3. 103 Editor: Now we're going to see how to download the editor. That's gonna be used throughout this course. So what I'm going to be using is visual studio code and that is gonna be at code dot visual studio dot com. So if I go into here, I'll get a download link right here and depends on what your operating system is. The link will change. Now, this is a cross platform editor. So is gonna work on Mac on Windows on Lennox, and all we're gonna do is download Click the download button, go through the typical installation for your operating system and make sure special windows that you have add to path checked and that you've got the other options for the integration and to say Windows Explorer. So you can open visual studio code from there and for find her own Mac, and then that's all there is gonna be to it, and you'll have visual studio code installed on your computer. So now what we're gonna do is look at visual studio code and see how to open it from finder . Alright, so I've got a folder here or my terminal window, and I'm in a folder that I'm everything is gonna be in. So if I do an ls There's nothing in here right now and what I'm gonna do is code die. So if your own windows at a command prompt inside of the folder you want to open this is the same thing you would do and I hit. Enter, that's gonna open visual studio code. Alright, so I've got it open now and just gonna stretch this window out a little. What we're going to do is look at the different extensions that we're gonna need to install . No notice over here. Also, you can see I've got my folder. There's nothing in it right now. Um, I'm gonna go ahead and add a file, which is the one we're gonna work with before we get into the extension. So you can just see, and it looks a little more filled out something. Have first ap dot p y so dot p y. Because this is a python file that I want eventually execute. So I had that there, and I'm gonna type in print. Hello, world. Okay, so this is just a simple print statement. We run this program. That's what we're going to see is Hello, world. Now notice. I've got this circle up here. When I hover over it, it changes to an X. The Circle manger file has not been saved. So we're gonna be able to execute these files right here inside of visual studio code, and we're going to see the output down here. I have the output tabs selected. It's really the same as if you are in terminal or at the command prompt executing these files. But when you execute, it's not going to save your file before it executes. So you're gonna need to save it now when I say this file is gonna go from a circle to an ex . So if I see and you can see over here my recent files, it's got a circle there as well, so I'll save. I've got the X. I know it's ready to go now before it can execute. I need a couple of extensions. And if I go over here on the right, this is the extensions panel. I click it. These air the various extensions. If I want to go back to the file Explorer, I just go appear to the top and click that and I've got my files again. All right, so in the extensions, we're gonna type python that's gonna narrow everything down to just python extensions. There's only two of them you need here that is gonna be magic Python. So you'll click your install and install it and which are also gonna want is something called Code Runner, which is this one here. If I click it is the dot run win. You'll install that as well. Now you may get a blue button that says to reload the window after you do an installation of an extension, go ahead and do that. Now that you've got those installed going to go back to the file Explorer, I'm gonna show you how to execute these. If you got a view, there's the command palette, and these were the various commands that you can run. I've got some in here that are left over for the rest language, which is the cargo build and debug and Ryan And we want, um, run code. This one here is gonna be from the code runner. No, on a Mac, this is option control in. It'll be a little different for windows, but you'll see it there in the command palette. Whenever you do run code, you'll see whatever that command is for your operating system. Now, if I do option control again, you can see I get my output down here in a little bit of manna information about how long it took to execute that. So now I'm running my python code right here inside of visual studio code, which makes everything really easy. Another thing I want to show you is that you also have a terminal window. This is just like the terminal window we were at earlier. So for doing ls I can see I'm still inside my, um, folder that we opened visual studio code from. But now there's something in and, of course, which is the first app dot p y file. So I can go out of this ls I can see the other followers that are back there. I can do a CD python tab to let stand in a capital P and tab that'll fill out the rest of it. I'll go back in here. Ls I can clear. And if I want to build this, I could do a bill just like I did at the command, prompt and running so I could do python first app dot p y executed basically the same thing that we're doing over here with our output and so I can do hello world again. You see, I've got a diet. I know I need to say this. There's my ex and option control in and we execute again. So it's really that easy. Everything's gonna be integrated in one place, basically to make our workflow pretty seamless. So that is visual studio code. And we've installed a couple extensions to ensure that we have syntax highlighting, which is Ah, something I didn't mention. So if I go back, let's bring this window. I'm gonna go back to the extensions. Magic Python is a syntax highlighter, basically. So that's why I'm getting this highlighting here. And if I do something like a one plus one and you could see now I've got a little bit of different highlighting. So my a and e quarrel white, the numbers of yellow, the pluses, white. And if I want to see what it does, I just do that. So save and option control in now I get my two down here. So that's what Magic Python is all about. And then we talked about the code runner. Basically, letting us execute this code right here is what that does. Okay, So what we're gonna do is next Get into learning about the python language. So when you start with some fundamentals and we'll slowly progressed into more advanced topics 4. 201 Introduction: in this module. We're gonna cover language fundamentals. Now we really get into the meat of python and start going over various kinds of syntax the operations that you can use and really start building out some useful small programs and work flows. So just to give you an idea of what's gonna be happening in this module, we're going to cover variables. So this is gonna be the first part we're going to see. How do you do? Variables and python. We're going to get into different kinds of loops a while, loops and four loops. We'll get into conditional, which are, if condition ALS in most languages. So if if else are, you can have if else if and then an else behind it. So various kinds of conditional is and how that works in Python, we'll get into a structure called two polls as well to Apple's really allow you to, for example, in a function, return multiple values back in a sign it into this to pull structure. So if your function is returning three values, which in most cases is going to return one. But if for some reason you just want to return three values. You don't want to create a class for those values. A Tupelo is a great way to handle that. Now we'll see how toe work with a basic to pull structure in this module without getting involved in functions or classes. Then we'll get into file input output. So how do you work with the file system and Python? So reading and writing of files and each module is gonna have a challenge. So in order to really solidify what you're learning inside of these modules, you're gonna be presented with a challenge, which is gonna be a set of instructions to create a particular program. So I'll show you a finished program and how it works. Then you're gonna go off from that and build it, and it will use what you've learned in that module. Then you'll be able to come back and see how the program was put together. So there's gonna be a walk through of the solution after the challenge, instructions are presented. So that's what we're covering in this module. Let's go ahead and get into it. 5. 202 Variables: we're gonna start with variables and shrinks, and I am going to create a file that's basically when contain our entire module of examples here. So I'm gonna do a module to P y. All right. And to start with, I'm just gonna create a variable it's gonna contain of values. Let's do in a equals one, and then I can print this out so I'll save and writing and there's our one. So that's simple enough. We convince. Take an ad. So if I want to do at a 12 are variable A. I can save that in print. This is a new value for B and I'll do something like this. Then I'll do plus, and I'll put our be here. Save and run. No notice here. What? We're what's happening. Basically, this down here. Connect. Can Katyn eight as string with a manager. So because of what I've done here with the string and then I've got an energy, there's an incompatibility. Now I can take and do this and then I'll just comment this out by putting a pound in front of it and run. So now it prints out. But again, I wanted to print out with this sentence in front of it. So what I need to do is what's called casting. So I'm gonna cast this from a integer to a stream. Well, basically retain. It's presentation of too. But now I have compatibility with the strain here. So if I run it, you can see here I get the new value that I'm looking for. Then, of course, we can declare other variables. So let's say this Boolean and that'll be a false So then I can print this Boolean save and run. So you noticed the output we're getting down here named False, is not defined. So is thinking that we're using some pre defined variable here which were not so what we need to do. So this could be a key. Word is changed to a capital F, and if we run that now, we'll get our output. And again, we want to print this with another string. So this is the value of this bullying plus this bully in save and run notice. Here we have the same issue as before. So again, we're just going to do a string on that And now we'll run and we get our value right there . So we've got this string. This is the value of and we can actually have done that right there. It's starting to become quite common. So let's take this and we can just create a new variable. So print string equals will just do that. And I'm gonna put a space so we can have whatever it is we want over here. And now what weaken Dio is print string right here. So print string plus and we have our bullion and what we want to do is put that back in quotes and save run. So here we have basically the same output. This is the value of this bully in false. Now we can do the same. Up here is well so we can take all of this and we'll create a new string like that. Let's see, it's gonna tryingto add double quotes this, see if I can remove that. There we go. All right. And then if I run that now will have the same output up here as well. And let's see, here we have we don't need to put a space there. Say that one, rerun it and and that's going to be the same output is what we have down here, and we've got some good code reuse. So really defining variables is an implied kind of thing. And you don't worry about, for example, specifying the tight. Now you notice here the naming conventions. I'm doing the underscore. Everything is lower case, and I'm just separating compound words with an underscore. It's a normal convention in Python, so let's look at this bullion and what happens if I do a five here? So let's go ahead and print that out. So this Boolean save and run notice. There's no problem with that, although it's probably a rare case that you want to take your boli in and actually change it to an inner jer. So but just to be aware, you can certainly do something like that. And down here, let's see if I take prints string and I actually assign it true. And now I print print string. That's not an issue either, So I understand. Here I get my true. So, then, is a look at how to defying variables and do manipulations with different variables 6. 203 Conditional Flow: Now we're going to look at conditional flow and what I'm gonna do so we don't have all of our other content printing out down here is I'm going to separate things out by function. So that way it's it's easier to see what the output is and deal with things. So for this one, I am going to do death Terrible's and we don't really need to worry about functions right now. We're gonna get into those a little later on. Just move that over then. Down here. All have deaf conditional flow. Let's see, I want to do that. All right, Print. Just going to print this. Make sure this is doing what I want. And then down here, I'll call conditional flow, save an run. So there we are. Now, we're only dealing with the one part. So if I would have taken variables, then we would see the output of that run. That So there we are. Now we're out putting durables. All right, so I'm just gonna put everything down in here. So let's see. Start conditional flow and everything on one's gonna be indented because that's how it works with functions. But again, we'll get into functions in more detail a little later. All right, So to start with, I am going to create, um or actually reuse this Boolean is gonna create available for us that we can start doing some comparisons on. So if this bullion and I'm going to do print, this is false and I'll save and run. See? Scroll down a little further. All right, here's our last one. All right, so this is false, Which means this is not gonna print. All right, So you noticed We have no output down here. Now, if I do the comparison like that and run now, we have our print. So just to show something a little different we're gonna do this is true. Then we're gonna want to say, actually, let's just take that. We're gonna do where In the initial conditional. And I am going to do nothing here just that and run. So noticed. Now, unlike before where there was nothing here, this evaluates to true and we print now, earlier we had this false we didn't print. That's because this is only going to allow the conditional down here to run whatever is inside the conditional to run if it's true. So that's why when this is true, this down here runs all right, then we could have had but say this Boolean, too. And that could have been five. And we'll just change this a little bit. So we have our bullying to here and we want to do comparison. So let's say if it's six and go ahead and run now, we don't see our output down here anymore. And if we go back to the five, save this and run, we say our output again. So the double equal is actually inequality, and we're what we're doing now. Whereas before we just had this right here, straight off evaluation. If if this right here is true, which basically only time is gonna be true is that the variable was a true like it was here . It'll run. Now. We're doing what's called in its explicit comparison and checking to see if we're equal to some variable Now. We could also say, if five is greater than five, which we know it's not. So this is not going to print out, so you can see we're not getting anything down here. Of course, if it's six and we run this, then we'll get our print out again and let's see weaken due greater than or equal to. So if we change it back to five, that should work, and we still get our conditional the print out. Now, we can also do something like this. So if we have, um, else we'll just say a print and actually need my colon right there. So after these conditional statements, you'll do a colon and usually with whatever editor you're using. If it has some kind of python syntax integration, it will do the Indian for you because you will need an indented after that conditional statement. So down here, I'm going to say where in the house save run. So we don't see the else print because the first condition is met. Now the first condition is not met. This will run. So let's say four. So now this is four, and we can run, and we just continue to get our initial because we are, in fact, greater than four. So how do we make this fell? Well, if we have a conditional here, that is not greater than or equal four. So what would that be six? We say that. Run it now, notice we're in the else. So this conditional fall fells and we go into the else, which is basically like a default. There's some of the things we can do here. What if we want to test for another specific condition? Let's say we want to check if we're three. For whatever reason, we may want to do something with the three. Basically, we're going to need another if which is going to do an explicit comparison, the way to do another, if is an else. If which looks like that, and we're gonna say we're a three and right here, what we're going to do is this Boolean two equals three. So again, an explicit comparison. Now let's do a three here. So three is not greater than or equal to six. Three is equal to three. This will print. Nothing happens with the else. So save and run. There we are now what if we want to do some other kinds of comparisons with strings, for example? So let's say we have a constant or basically what's going to behave like a constant. So we're gonna call this good foods. And this right here, just all caps is also a common naming convention for Constance. All right, so if I do if Otto in store, what I'm looking for is the character set a character's auto inside of this string. And then if it is, I'm gonna print This is an automobile store. Then I want to do another explicit condition. So we know if we're gonna do that, it's gonna be in else. If so, we're gonna have a nail if and for this, we're going to say food in store, and then we can print. This is a food store. They will have an else we need, actually. All right, so we have our Collins at the end of those statements, we have it at the end of our else. That looks good. So we're gonna say this store is act me. All right. Say that now we're going to run. Analysts say we have a sin tax issue, so it's telling us right here on line 34 instead of the good thing about visual studio code , we have these line numbers that we can immediately go and figure out what the issue is basically because we capitalize our print keyword That needs to be lower case. So now let's try this again. And I see Here we are. So this is a food store. That's what we fell into this explicit conditional here. And let's say if I want to do, um, gyms, Otto Repair, do another comparison. Run that this is an automobile store. So it did find auto right there in the sharing. Now, what if I have some other store so fishing goods in Run. This store is Acme. So we're just saying all the other stores are gonna be an Acme brand type of store because it didn't find auto inside of this string. It didn't find food inside of this string. And so we're just going to say it's Acme Weaken, Do another else. We want toe, look for a specific title. So else, if we're going to say something like most of fishing goods and we're going to take store now, we're not going to do an end. We don't need to worry. Want an exact comparison. So we're gonna use our equality string, and that will work. And then what we can say here and we need a notice if I hit return. It's not doing an end in. If I do my colon, I get the NDN so very important to do those Collins at the end of these conditional statements. So I have my print I'm gonna do. This is a fishing goods store. Save and run. There we are. So our exact comparison on an explicit comparison working just fine. So that is a look at conditional flow. 7. 204 List & Dictionaries: Now we're going to look at list and dictionaries, so I am gonna create another function, forests, and just comment out these others that we've been using. So we're gonna have something, Let's say list and dictionaries, all right? And so this is where our codes gonna go, and then we're going to call this so we can execute whatever it is that we're wanting to look at inside of this function. First thing I'm gonna do is create a list, so that's gonna be inventory items. And to do that, we're gonna have brackets on both sides to enclose our items. So I'll have, say, apples and bananas. And then, of course, I can print this and down here, we can see what it looks like. So these air strings, you can see we have quotes, they're separated by a comma. So we added that common there, and we could just keep expanding by adding a comma and then the other item, whatever it might be, we want to check what is the length of this list, then weaken? Surround it inside of a land function. Run that now. See, we're not getting our output for some reason. Try and run this one more time. There we go, so two items inside of there and to access items. So let's say we want to access the first item that's going to be a zero enclosed in brackets. So this is what's called an index. And with list, it's an index zero index based system. So if you want the first item, it zero. If you want the second item, it's one. So take whatever the position is and subtract one and you'll have your index. So in this case, we should see apples, and we do all right if we want to add another item. So let's say we want to add grapes and I am going to do in append. Then I'll do grapes. And now, by print out the list, we should see grapes inside, and there we do. And of course, I can also print the left, and now we'll see that we have a length of three. There we are. So let's look at overriding some of these items. So let's start with our 1st 1 apples. If we want to change it out for something else like red apples, we simply access the index of that item, and then we just reassign it. Now, when I print this, we'll see we have red apples, in fact, and there we are. All right, So what about removing an item I don't want to do? Let's say, remove grapes. So I'm gonna do remove, and I'm gonna call it by the name of the item. In this case, we're dealing with the strings, so I'm just gonna pass in that string, then we'll print our items, and here we are. So we have read, uh, apples and we have bananas, which is a misspelled Just change that there. But we don't have graves anymore, so just run this again. So we can see now are items. So now we're gonna move on to dictionary. So I'm just gonna put a comment in here to signify, um, we're working with dictionaries. All right, so I'm gonna create a few items to use, so we'll have apples, aunt. That's gonna be 11 will have bananas, and that's going to be 25 will have grapes. And we'll just do that as 20 items and will create inventory this time for a dictionary. We're going to use braces since that our brackets. So that's one of the main differences. Also in a dictionary gonna have key values rather than just say, for example, of the values. So the key is gonna be, let's say, Apple's the value is gonna be apples ends and then we'll do a comma and just keep working our way down so we'll have bananas. Then we'll have the value Bananas, aunt. So key value will have grapes as the key and the value is gonna grapes. And now we can see we have an item, the name of an item and then we have basically the number of items that are available. So if we print this, see what it looks like at run time? Let's see, Ryan, there we are. So you can see here are keys and values separated by commas, all right. And so we can also look at the length of this. So if I want to print, let's say items and dictionary and at our length inventory now, this is an imager. So we're gonna have to do that cast to a string because we're starting with this string here. So everything in that we're referencing inside of this print needs to be a string so wouldn't run that we can see our items or three. So the key values basically count is one item, and we have three sets of those is how we're getting that three. Now, if we want to get an item, we can pull the item out by its key. And the way to do that is similar to how we do it within a race. So we have our brackets. But this time, instead of an index, we're gonna use the key. So if we want the key for apples and we run that, you can see we get the 11. And we know that is the case here. And if we want to print all of the keys, for whatever reason, weaken do inventory keys and we'll do a parentheses on this because that's it a function. So we'll save it. Looks like I've got a misspelling here. Save it and run and all of our keys or printing out and as well we can print out the values , Say this and run. And there we are. So keys and values printed out separately. All right, so we know we have three items, or if you want to remove an item to do that, we're going to what's called Pop it. So I'm going to create a variable called Captured Value and it on to inventory top. So what I'm popping is apples, and then I'm going to print captured value because this sends back of value. So we're going to see what the value is. And plus, I'm going to put that in a cast for a string because it is the value that's coming back. So let's go ahead and run this. Now let's say we have an issue here, all right? Looks like that was a misspelling. So just copying this and pasting there. Ah, satisfied the error. All right, so this is our captured value, which is 11. And now what we can do is look at our inventory is see if we have any kind of difference at this point. So save it, run and we do so there's no longer any apples. And when we pop the key, the value goes with the key because they're a pair. But we can capture that value is being popped if we want, and that's what we did here now. What if we want to change a value? So let's do inventory and or add a value. Rather, we're gonna do Cherries because we don't have any of those right now will do 26 of those. So notice Here I am accessing this just like we accessed our key appear. And instead of pulling out a value, I'm creating a new key and assigning a value as well. And if we print inventory, save and run, we have Cherries. Apples are no longer there, of course. But Cherries is taking the first slot when we add it like this, and then we have our other two remaining items and then we can print inventory. Whatever Cherries is so well, access that key. We'll see that value print out, save and run. And 26. All right, so that is a look at using list and dictionaries, and we done various kinds of manipulations to them as well 8. 205 While Loops: never going to get into two different kinds of looping structures. We're going to start off with a wild loop, so I'm going to create a new method for wild loops. And let's see, down here, we want to comment out our list and dictionaries and put in wild lives. All right, so this is just ah, one kind of loop that you could do in Python. I'm gonna create a few items so that we can actually look through them, so we'll have apples, bananas, and we'll have grapes. All right? They won't create a list for those. And it's gonna be apples, bananas, grapes. All right, so there's our list. We have three items, which means potentially weaken iterated through three items. So I'm gonna create an index of zero because we're gonna need this while we're doing our loops. Remember, this is a list. So we're using a zero index based system to access these elements inside of that list. All right, so for the while, we're gonna do while index less than the length of the inventory, do a colon that will create whatever we're going to dio on each iteration. So let's look at this a little bit before we move forward. We have one while, which is a key word. Then we set up a conditional and so the while says while the conditional is true, do the it aeration. So basically, we're creating this conditional right here. So we start off with zero for Index. If it's less than the left, which one of the length is gonna be three? We're gonna do the loop, So this is gonna be true. And we're going to do whatever's inside of this wild loop. The first thing we're gonna do is print inventory Index. Then we're going to incriminate Index. Otherwise, we'll never get out of this loop. So eventually, by incremental minute by one will get out of the wild it because it's gonna be greater than three at some point. And so when that happens, what we're going to do is add and else So if this conditional fells then will run this conditional, then we'll say print. No more inventory to print. I will say index actually was to the lower I, since that's what it actually is equals plus one at the cast. This because it is an injure like that save and we'll go ahead and run. All right, so here we are. We have 51 55 actually, I'm going to do a print starting while loops like that. Yeah. And save and run. Let's see. All right, All right. Click on this and run. That should run. Let's see. Here we are. All right. So, starting wild lips and you can see here 51 55 14 Each of the items in the inside of the list. Let's go ahead. And in addition to printing out the actual item, let's see what's happening to Index. So we're going to do some casting. We don't have string, and I'm just gonna say index equals and this is going to be a string. Then we'll have a plus. We'll have a plus. Space can actually do a comma. I will say item equals will have a plus here. We'll do our string and save. So this is gonna print before we do the increment. So now if I run it, see down here, we should see what we have there we are all right. So you can see as index increases slowly, we get to the point where we were at a two. All right, so that's gonna be the last iteration. Now, indexes a two. We add a one to the two. We have a three. So we continue back up here and we check the condition. Three is less than three. That's a false. So this is not true. So this is not going to execute. What we do now is fall into the house and we see that printed out here, and we do see our index is equal to three. So that is how to set up a wild. Remember, it's got a condition that must be checked in order for it to execute the body of the while . Just make sure that your doing something that's gonna cause the condition to eventually be false otherwise is gonna continue looping. So if we never had this last line right here, it will go into an infinite loop, because it was it would always be true. So that's just one thing to keep in mind. And then you do have the option to do an Elsa default so that when the condition is false, you can just say something about that right there 9. 206 For Loops: last time we left off, we looked at while loops. Now we're going to work with four lives, so I'm just going to create another function here, which is gonna be four of four lifts. And I'll come into out the wild lives and we want to go ahead and create a definition Someone grab what we had up here and change it for our four loops. All right, so here's where we're going to start and we're gonna have, let's say inventory list, and that's gonna be a list of strings and bananas. Then we can have the back over here grapes, and we're also gonna have a dictionary. So when we have inventory dictionary, use braces there, So in here, we're gonna have integers and we're gonna have our strings. So I'm gonna take thes, put them here, and also just put some numbers account for each one of these basically and nine. All right, so now we're going to go ahead and loop, and that is gonna be first for the array for this list. We have basically salutes to inventory lists, and once I n and then down here, whatever we're going to do, which is print. So we have the four key word we have, what it is that we're iterating through and in the item that were pulling out per iteration . So the syntax here makes sense for saying four. So that kicks off. What exactly this construct is, which is a four loop. Then we have I is in inventory list, so we're pulling this eye out. So if I save, go ahead and run. Let's see all 10 c. Maybe it's here it is. All right, So each items getting printed out and I am actually just going to do a print for loops, make sure no other code is getting involved. There we are, So that's pretty simple. Now, if we move on to a dictionary so this right here is list and then we're gonna have dictionary. So for the dictionary, we have key values. So we're gonna do something very similar, except we're gonna do K V. So instead of just a ni, we have a key in a value that we can access. And this is gonna be coming out of, um yeah, inventory dictionary. And also we need to call a function called items. And so we're going to print will do K plus count um se equals and we'll have the now k is a strain. This is a string discount and V is an integer So we're gonna have some compatibility issues there want to do is a cast to a string and we run scroll down. We can see our dictionary items printing out here in the format that we've indicated. Now, of course, we can also Luke through the keys. So in this case, we're just going to use one item like we did with a list and because basically, that's what we're going to get back. So we have inventory dictionary, call this function keys. It's acting just like this up here. So we couldn't use We can use an eye right there if we want, but just using a K just to make it more representative of what it is and save run. And then with down here, it prints out just like the list apples, grapes, bananas, and we keep going where we can do the same for our values. So I'm just going to use a V in this case, I could have used an eye or even a K, but again, just keeping the descriptions pretty simple. And we're going to call values. Now again, this is behaving just like this list behaved, and then we'll print V save and run to scroll down, and there we are. So those were the items that just printed out, and that is how to use four loops with list and also with dictionaries. 10. 207 Working With Strings: We're going to see how to use strings or do some different manipulations on strings. So let's see down here we have our functions that we're making use of. Now we're going to do strings, and I am gonna create a function for that. So we'll have deaf strings. Let's see. All right, that was good. Okay, so I'm going to create a bearable long string, and this is going to be, let's say, apples. 10. We'll do a pipe Bananas 15 and we'll do space pipe and we'll start with grapes. 19 Space pipe space, Cherries 14. So let's say we're getting this information inventory information from 1/3 party and this is how they send it. So it looks like a dictionary. We have our key. We have our value. We have a key. We have a value, but they're using different ways to delimit thes. So instead of a comma, we have a pipe, and sometimes we have a space between the pipes before the next key value set starts. Also, we have a hyphen separating the key in the value. So we're going to get into some parsing because ultimately we want to put these items into an array because then it will be set or into a dictionary rather cause then it would be in a set format. So what we're going to do is create an empty array and it looks like that and we're going to do new string. And here we're going to do long strain and we want to split it on these pipes. So we're going to call this function called Split. And what are we splitting? We're going to split on pipe. So we're splitting apart the key values, and we're gonna go four with that, and then we're just gonna print new strain to see what this is gonna look like. So we go ahead and run that scroll down and we can see here. Great. This is looking good. So far, we basically have a dictionary now of four strings. And then what we're going to do is loop, so we need to continue splitting. These were going to do s in new strain because now we've got basically this array down here of strings. So for that we're jumping ahead. Now we're creating what's called a to pull, and we will get into the details of Tupelo. But I'll explain for this simple scenario. What exactly is going to go on? So we're gonna do replace because, remember, we have some inconsistency. We have a pipe with no spaces. Then over here, we have a pipe of spaces. So what we want to do, it's strip out the spaces so weakened. Do another split on those pipes. But we need to get rid of the spaces first. So we're going to replace a space with no space. And then what we're going to do is continue to split. So we're gonna have what we're splitting on, which is a hyphen, and one. Now, I'm going to go ahead and walk through that shortly. But I'm gonna continue building out the body of this for loop. So we want a print to make sure this is all starting to look. OK, so we're gonna do this. A whatever it is, whatever the associative value with a is. So we're printing out the values of our to pull because it is splitting this right here. It's splitting the key values for us. And then what we're going to do is add the key values to the new dictionary. and I'll go ahead and run that now. So here we can see this is looking very good. So we went from what we have here to this here. And let's talk about how this works. So starting with the replace, we have these spaces and we can see we have spaces appears sometimes those came through in our ray. You can see we have a space after 15 after 19. We don't have a space after 14 but we have one before cherry, so they're kind of in different places. And then apples and 10 look good. There's no spaces. So this is gonna strip a space out wherever it sees it, as it iterated through each of these elements that will just remove it, replace it with nothing, basically. So then we have a good format. We're getting rid of spaces. Okay, so then next we want to split. We're splitting on a hyphen per element. So we're just That's why we have one here because we only want to do the split one time. Whereas here we're just basically doing whatever the count is. When we started with these pipes, whatever the count of the items were expecting or so then down here we split on the hyphen and we add to our dictionary. And that's why we're able to do this now. Because we have a key. We have a value. So that's how that part works. Then let's go ahead and print out the final pieces of this. So we're gonna do a print on the dictionary items. Then we can just do also a print on items keys. We could do a print on items values, save an run and let's look, what do we have? So here we are with our dictionary so you can see string, string and in the keys and in the associated values. Now you might look at this and you'll say, Okay, well, we know these air imagers and there are always gonna be in injures. Let's say that's very reliable from our vendor, that these last parts are always gonna be in injures and we want them to come out as integers as well. So in other words, come out into our dictionary as integers instead of strings. So where were assigning right here we can cast. So we want to cast that from a strain to an an injure because it is always an integer so fit. If that was unknown, we would need to do other things. May be conditional, is to check. Is it a string or not? And then we would do this cast. So now if we run this, let's see. Down here you can see we have integers. There's no longer quotes on the values for our key values. So that is a look at doing various kinds of string manipulations. And then we saw also how to get our string integers in tow, an integer format, and put them into our new dictionary. 11. 208 Tuples: we're going to work with to pols now. And I am just going to date are functions so that we have a new and we're going to call for two pools and let's see, we're going to create to pols as our function. Now, last time we're working with strings and you can see here we actually did make use of a to pull. So basically what this did Waas returned to values, and that was our key in our value and would put them into this to pull A and B. So then we can just use them basically independently, like we have done here. And then we did here. We didn't have to, For example, go off and create a custom construct to capture this move. Just made use of the to pull. So that's where the use of two pols come in handy is whenever something is returning multiple values, You don't have to worry about creating a class to capture those or anything else. You just captured them in a to pull. So what we're gonna do is create a few variables toe work with. So I'm gonna have apples. I'm gonna create, Let's say, Apple's quantity do it. 10. And actually it should be 10. Then we'll do an item and this is going to be apples, plus colon plus and string for the quantity. So if I print this out, we'll see what this looks like. And what this might be is say again, a vendor may be sending us something back and we need to handle it. So here it ISS looks like a key value with the colon. So what we want to do, it's split this similar to how we were doing in about. So let's say we have A and B and then we have an item like that. We're going to go ahead and split on this colon so we'll have a colon. We're just doing this for the one. And then we can go ahead and print a print, be and save. Run. So here we are in our items have now been split. So I am going to do another variable here. Just call this actually two polls in doing an item and go ahead and split this again just like we did. Now you'll notice I don't have independent bearable. So how do I work with that. So what you can do, in that case is actually access the items like an array like that. I run this, you can see we have our 10 is the second item. If I do a zero, that's gonna be the first time. So there's just zero in one for the indexes on this. So here you can see I'm getting apples, so that puts it into an enclosed construct and you can pull them out if you want. So let's say a to and I'll just do two poles. Zero and I can print a two. And now I've got that. Wherever I want to use it, you can see I'm getting Apple's again on a to So the really useful in that regard. Now I'm gonna create a variable called Inventory. It's gonna be a dictionary. So now we're going to have more items and went to a few key values of items here. So that say, I have 15 of those. This should be here an grapes, and I'll have 19 of them. So I want to look through these we saw in a loop construct for four loop. We were doing this K and V for our key value in. Then we had inventory and we accessed items. Then we were able to print the key to strain and we get our quantity. Gonna put it space Q two watt Q t y equals. And then I need to cast the value, save an run. And so there we are. So we were actually using two poles a while back, even when we were doing these four loose. So this is going to get the two values out because of how the dictionaries constructed with key value. So now I'm going to basically declare an empty to pull someone to one comma here, and what I'm gonna do is print items zero and run. So here we just have the one and that's it. The comma there is really needed. So if I run this again, you can see we're getting an error. So it wants the comma to create the construct for the to pull. Now I'm going to start filling this with items, so I have perent to see and let's do first item and second item, and I'm just gonna go through, do about four of these. So third item and fourth item. So those are all too pols. Never. Now I can go ahead and print items. One, 41 through four. Save and run and you can see here. I'm getting my different items and notice the members zero index base. So that's why I'm not seeing the first item. So let's do this again. I'm gonna do integers this time, so I'm gonna have 12345 and I can print numbers. Start at index one and go to three. Save an run, but see, save. Run that. See? Here. This is not running. There were. Okay, so what is the issue here? Lying? 157 Um, my 1 49 something it's not liking right here, ain't it, Colon? All right. And now run. Here we are, two and three. So we started index one and we go to three. And the three is actually the position, so it's not acting like an index. So if I change this and I say I want to go to to save run and we have a different output down here, too, and then there's nothing left. Um, we're all into their. So what if I want to go to four, try that one. And then we have our different items here up to the fourth position. And of course, you can take thes print and access them like array elements, just like we were doing earlier. So if I have zero, that'll get me a one scroll down. We have our one. And then, of course, if I try to do something like this numbers zero and I want to reassign this thing, Say I do a 22 here. Save and run, I get an error. This error basically says you can't overwrite a to pull so you can't do anything like that . They're pretty much set, so can't over right now. I can take numbers and do something like this. So I have 20 to 33 44 in 55 Then I can print numbers. Save run. Let's see, Here we are, so I can overwrite the to pull construct. I just can't overwrite the individual elements and notice here I'm even using a different number. Have 44 here and then five up here. And of course, I can access thes individual elements, so save and run and I get 22. So that is a look at two pools 12. 209 File I O: we're going to do some file io operations, so input output operations and I am going to start by creating a new function for us that's gonna be called file underscore io, and I'll create a definition here for that function. The first thing I'm gonna do is define a file path, and this is going to be a full path, so from the root. So if your own Windows machine, for example, from your see dry all the way to wherever this file was gonna be located now it's easy enough in visual studio code to find that because I am in this Python beginner folder and when I hover over it, I can see the full path right there. So what I'm gonna do is slash users and I'll have my name. Then I'll do python tyshon beginner without an s, and then the file is gonna be created. So I'll do that. That's the file that I want to work with. Now I'm going to create another variable that's gonna hold a handle to this file. So I'm going to call a function called Open, and I'm gonna pass it this file path and I also need one more parameter, which is gonna be wt So this is gonna allow me to write to the file. Then whatever it is, I want to write to the file. So I'm going to do another call to file output, and this is going to be a right. So what I want here are some items like this first item and I'm gonna do a line return second item line return third item line return. And when you're done with file operations, you need to close your file out. So I'm gonna call close as a function like that Don't need a cynical in there. I'll save. And we should see this file get created right here. So I'll go ahead and run. And there we are. If I click this file, weaken CR items with the line breaks or there. So it's that easy to write content out to a file. All right, so let's go ahead and read this content. I'm gonna create a new variable toe, hold this content called file input. And so with file input, I'll do another open are. Actually, this file input is another handle to the file. But for reading operations, we're gonna have another variable that will hold the actual contents. This is just gonna be a file handle going in the reverse direction so we'll have our open again. Will use file path now, the parameter second parameter we're passing it is gonna be a little different this time. We're using an RT for writing. I mean, reading. So w for write are for reading, and then we'll have a new variable called file contents and to read in the content. I'm going to file input diet read, just like we had our right. So we don't really need any anything to go in to read right here, because we have our file past specified. So we have the content, we can close out the file handle, so I'll do file input close again. And let's just print the file contents save and run. All right, So if we go down here and we can see our contents right there and these air actually strings right now, we want a construct that we can more easily access thes as an item. So each of these as an item, because again, they're just one big blob of string, so way to do that is, let's create another variable. That's gonna be an array of arable, and we're gonna take our file contents and something we've used before. So when I have a replace, we want to get rid of the new line. So nothing. We're just gonna replace it with nothing and then split on a comma. So let's saved for that toe work. It means we should have commas here, so we'll have Ah, comma, new lying acting actually like Eden limiter. All right, so that's gonna help us here now when we go to split. Otherwise, we probably could just could've have instead of this replace Just don't split on new line. But likely we probably will see some other dilemma utter like that comma in there. Okay, so we have our array. Let's sprint out this array, save and run. Now let's see if we have our outputs and we do and we're getting this extra one here only because I've got an extra comma that we don't need. So with this array, we can now do other manipulations using built in constructs like a four loop. So I have I, in array of content are let's do an s on this contents, and we're going to just print once, just print our I. And then next we're going to do print array of contents and we'll access the second element just to show, you know, we are in fact, dealing with this array. Run that. And down here there's our race. So we printed it out right there and let's see, Here's here's our loop right here. And then our single item we printed, which is the second item using an index of one. And that's it right there. So that is a look at file Io operations. 13. 210 M2 Challenge Instructions: before we get into our first exercise, I want to show you how to capture user input because you're going to need it for this exercise. We're going to be taking user input and using yet to do different things in sight of the exercise. Basically, you're going to be creating a game, and it's gonna require several bits of user input in order for it to work now for user input. You can't actually do that in visual studio code. So if I go here to the terminal, you can't run your file from here. You'll have to go out to the terminal to do it, and we'll see how that's done. So let's look at how do you get user input? The first thing I'm gonna do is create a bullying called True. So playing equals true because we're gonna put everything in a while loop and that way we can continuously prompt the user for input. And if they guess wrong, we can prompt them again, and I'll show is, well, how to break out. So as long as playing is true, this wild loop is gonna execute. So here we're going to take SMEs or input and it's gonna be stored in a variable called user guests. And to do that, we're gonna do raw input I'm gonna do Give me a number and space, but a little space between the two. This is gonna come back as an integer. So in that case, I need to cast it. So if it was a string you would just use s to your and cast is a string. Now we want the user to give us a number other than zero, and we'll just keep asking him, Give me a number, Give me a number. And if they give us a zero, we're gonna break out of this. So if we get something other than zero, we can go ahead and print whatever it is, the user tight. And this needs to be a colon in an indention, and we're just going to print out user guests. We want to keep going through the while loop. So we're gonna do a continue now If the user doesn't give us a zero, we're going to break out of this wild loop in the game is basically over. So the game is over. We're gonna print that. I want to say game finished her game over. Save. All right, so that should get us some user input. Let's go see how this works at the terminal. Okay, so I am in the terminal now. I'm gonna go ahead and run our file and you can see here. We're asking for a number. Gonna do a nine in it. Prints it out just like we saw in the loop. So we're still in that loop. Playing is still true. So I could do 34 Aiken, do 6 90 Whatever I want to do, as long as it's not zero. Now, if it is zero, we're going to break out of this wild loop and game finished. So that is how you capture user input. Now, we're actually going to look at the particular game we're gonna be creating, and I'll explain, and then you will go off and give it a try, and I'll have a solution. Ready for a walk through, um, when you come back. Okay. So our game, this is gonna be the game. It's called cow Bull. So it's just a silly game, but it's really gonna stress the skills that you've recently learned all right. So how is the game played? And I've got some instructions up here. So you all just want to print these out for the user. So it says I will generator number, and you have to guess the numbers one digit at a time. You can see here you actually get a hint. So there's four digits and play, and you have to guess two of them. So for every number in the wrong place, you get a cow so you don't want cows. You want bulls every number. That's right. You get a bull after four bulls, you're good to go. So basically, you get two bulls off the bat, right, cause you're getting this hint. So you have to guess the other two numbers and they can be the same. So it could be 81 01 So 8101 so you could have two ones are the numbers could be different numbers, and then you'll press a capital letter O to see what the original number is. So if I do a capital Oh, I can see the original number. And if I were to plug that in, then you can see here I get all four bulls. I went after one try. Of course I cheated. So I'm gonna go ahead and run this again. Now we've got this number. The same number I'm gonna see. Get us a different number. It's never go. And you can see it is generating different numbers. Is just It's using a random number generator to accomplish that. Basically. All right, So if I do a 7136 I'm going to get two bulls. I know that. How many cows? Um, I'm gonna get though. So this will give me a hint. So I didn't get any numbers, right? I got to cows. So that means both of those numbers were wrong. So I can rule out one. I can rule out six. Let's do seven zero 32 So now I'm just going to sequentially walk up to nine. So I've got a one. I'm gonna do a zero. That next number after one is too. Let's see what I get here. So those air all incorrect as well, and I can look up. I could see what I've tried, so I'm just gonna keep going. I'm gonna do three, and we have a number three and what I want to do next is a four. Let's see where we are. Here. So one of those air, Correct? I don't know which one but one is. So in this case, I'm gonna do 733 five and they're not correct. So the three is not the one before is. So in that case, I'm gonna do seven, four, three. I know five is not it. I know six is not it. Let's go to seven. And so the game basically is over at that point. Actually, I guess I was still plugging numbers in and didn't really notice. But here we are, and we got our numbers correct. So that's basically how it is and gonna clear this. Let's look at this one more time. And so if I want to do exit, I'm out of the game. So let's try one more round of this. So I have to I don't want to just start at the bottom again and kind of work my way up, But let's do 27 to see what this does. So the twos air no good. Let's do 2071 So, too, is rolled out. Okay, So 012 or no good. Let's do 2374 So we're trying three and four now. Those are no good. We're gonna keep walking up this this ladder here. So 2576 And those were no good. Now, six could be the one that goes here. And five could be the one that goes there so it could reverse. I'm gonna do 2675 That's no good. And then I'll do 2170 Those are no good. So we're kind of running out of numbers here, but we're all the way up to six. So I could do to 778 All right, so too 879 All right, so there we are. It took a while, but those numb numbers were way in the back. So that's why it took so long because we started way down there. Zero. So that's the game. That's how it's played. You give the user a hand, and then they have to figure out the other two numbers. So once they get those right, then you'll just give them the number of tries that they did so down here we have you in the game after eight, basically eight tries, the number waas, so you'll be accumulating the number of Try storing it in and Ray or whatever you want to do and then display that back to the user. So next I am going to do a walk through of Exercise one. So go ahead and pause the video golf and give it a try. Come back here and you'll see the walk through. 14. 211 M2 Challenge Solution: I'm gonna go through this solution for this challenge. So this space is gonna be our module to challenge. And I'm just going to go ahead and put that up here. And I think what I'm gonna do is rename this file. Just so it kind of coincides with what's going on. Makes it a little bit easier to understand. So m two challenge and a comment at the top him to challenge. Okay, so we don't need this input information. That was just to show how to accomplish that. And I am going to do print him to challenge. I just want to test make sure this right here is not gonna get in our way now, so I'll go out to terminal when the file are a So we need to do the updated file. This time I am to challenge most great. All right, let's go ahead and start coating this. Now. What I want to do is import. I'm gonna put this at the top, Lashley. I should build the import right here. Random, and what I'm gonna do is create a main and put everything in there. So here's what I mean by that Put space when they have, if name equals main, and then we'll have our methods. So this is how we define Main and Python. And when he have playing equals true, we'll have number. So we're gonna do a random number generator here. So we do random dot rand ends 0 to 999 and that should handle it. We're going to start with guesses or zero. So the number of guesses All right, let's make sure this all executes before we get going to for, um All right, so we have some issue with string here. Looks like maybe I don't have my equal. Right? So try this one more time, looks good and want to keep going. So we got our guests is now we're just gonna do a bunch of printing of those particular kinds of instructions we had. So let's play a game of cow bull and let's see here. I don't need the parentheses using Python to seven. And next. Let's go ahead and print. So I will generator number and what we're going to do here, comma. And you have to guess the numbers one actually don't want princey one digit at a time all right. And what I'm gonna do next is for every number in the wrong place. You get a cow, and for every number in the right place, you get a bull, all right? And then we're gonna do the game is when you have for bulls. So the game ends when you have for bulls. And we also want to say press letter or capital letter o for the original number and then type exit at any prompt to exit game, save us. Make sure all of this is good. Should see all of that. And we do. All right, Now we're going to create our main loop. So while playing, we've seen this before and the demonstration about how to capture user input. So this is true. It's gonna just gonna keep going as long as it is true. So we want to do generated number is gonna hold our random number, Basically, when I have number zero. Plus, this is gonna display to the user. So we know we're doing the first and second numbers, which this is zero index based. We're gonna do zero and two for those numbers. So here, we're gonna have our to and we want to do one more. Plus, actually, because we have the other number there and we're gonna do user guests. So here, we're going to do a raw input. You can see I've got the cast on a string here because that number is coming through is a string. And I'm gonna say Give me your best guess and we're gonna do a hint. This is going to be the number we just put together and we'll have a colon And let's see here. Did I miss? Because I have two of these closing right there. It's gonna go ahead and take one off. Yeah, it looks like I should not have closed here. All right, All right. So percent. So we'll separate, and we're gonna have our placeholder here, which is generated number, and we want to leave three parentheses. Yeah, so you can see here. It's highlighting the one way over here by str That looks good. And so the user must enter a number which must be of the four digits else. They're going to try again. So if user guess equals exit, we're gonna handle this scenario. We're gonna break. That's it. Then We're gonna say if land user guests gonna have to put some boundaries on this greater than four. Our land user guests, he is less than four. So we're just gonna print to the user. Please enter four digit number, and we can continue, so keep the loop going. All right, So now we're gonna go ahead and capture account, Kabul count. We're gonna go moved on to initialize this and then But this here, what I'm gonna do now is create another loop. So when have I n range? Which is gonna be Lynn number and we can at our colon. So if number remember, this is our random generator. So we're going to check. What did the user do? So let's see, user guess, and we want to compare the same, and we need a colon. All right, so inside of the conditional cow bull count, we're gonna go with one plus equals one else here, we're gonna do cow bull count zero plus equals one. Then we're gonna have guesses, and this is actually gonna be aligned with our four loop. So we'll do guesses plus equals one. And so we will show the results from here. Let's go ahead and print. You have. We'll do it. Plus, this is gonna be a string for the Count Kabul count. We're going to access first element plus cows and string Kabul count Gonna show the bulls and plus bulls. All right, so we look at this and all we're doing, we basically have two slots were just overriding those slots. So this one here is gonna be for cows. And actually, this one here's for bulls. And then this is gonna be for cows, and we just keep adding them up and keeping track of them. All right, then. Next, what we're going to do, we're gonna handle the user win scenario. So if cow Bull count one equals four playing game Weaken, do false print. We're going to say you win the game after and will display the guesses and go ahead and do it exclamation more. The number was weaken. Display that as well. This will be string. We have number. And let's see here is that close off? Think? Yeah, that does. And we can break. All right, then we would have in else. So one more Here, print Your guess isn't quite right. Try again. save. All right, we're gonna give this a run and then want to come back to do a little bit more. Walk through on this. I'm gonna go ahead and clear. Let's see here. So we have a syntax issue. If this needs to be to this is line 53. So two equals online. 53. And to think that will do it. Ryan. All right, so here we go. Let's go ahead and display the original. Let's see here, please enter a four digit number. It looks like I am not handling the original case. Let's go into the code and see. Here s so we have exits that see, we have boundary check when? No, I'm not just I'm not handling the case to display the original number. So that's gonna go in a while. Loop. Of course. Um, I'm gonna go ahead and display right above the exit. So if user guess equals capital o, we want to go ahead and display the original number, so original number will do this percent s and will create a new number. Now, our placeholder value is gonna be just number cast as a string. All right, that it's safe to try this again. Well, we need to exit now. All right, so the exit works. Great. Run this again. Capital O. All right, so that's good. We handled that scenario. What if we do too many numbers, please enter a four digit number. So the boundary check is there. What about one number? The Belgian check. Is there a swell and let's see here. I'm going to go ahead with the original number where we have it. 10. 57. Want to make sure the wind condition is good? All right, so we have, ah, situation with our print. A run time situation here. Uh, supported operandi. All right, This is online 53. See here. What exactly are they referring to? And this right here? We're not closing on that. So are cast was left open? That didn't look good. Run this again. So there's our number that we want to check for our win condition. All right, So you have zero cows for wolves. You went after one try. Excellent. Now let's go ahead in. Try a user scenario of all guessing. So 709 Nothing there. Seven, 293 Nothing there. 749 five, 7697 7899 Okay, someone of those air correct. Question is, is aiders at nine? Um, let's do. 78 98 is the eight. Ah, seven, 890 All right, so there's our number and we exit out. Of course, because we went after seven. Try. So that's a walk through of the solution for the module to challenge Hope. You found this pretty engaging. A good level of difficulty. Um, it wasn't exactly easy, but it certainly is a good exercise to try and stressing your new skills and working through it. So I would suggest going off in charge and work through it on your own. Even if you've just seen the solution walked through one time, just try and put all that together on your own again. It's really gonna help you learn this. All right, So thanks again for watching the model to challenge 15. 301 Introduction: Now we're going to get into functions and classes. So in this module, we're going to start off with, of course, functions and classes. Which means we're going to see how do you create a function? How do you pass in various kinds of parameters? How do you create the function body? We'll get into classes, which are basically templates or a data structure templates that you can create instances off as well. We'll get into Lambda functions. So see how those work in Python so coming off of learning about functions. Then we'll see how to use a shortness in tax to create functions that are a little bit different than the conventional functions that we just covered. So again, it's gonna be a compressed kind of syntax. Ah, lot of the times you can just do them on one line. We'll get into class inherent in. So if you created a class that could be used as the base class, how do you then create another class that uses that base class? So you get some really good code reuse? In that case, then, of course, there's a challenge, so you'll be presented with something that will really stress what you've learned in this module. You'll golf and give it a try, and you can come back and watch a full walk through of the solution. 16. 302 Functions & Parameters: Now we're gonna move into the details of functions and in this module to file, you can see we've been basically ah, delimiting everything by functions are using it as a demarcation for all the different lessons. Now we're going to get into the details of functions, and I'm gonna create a new file called Module three funked p. Y. And the reason I'm doing that rather than creating ah, multiple functions to contain these various lessons is because we are working with functions, so we don't wanna get into a situation. We have functions inside of functions. So I'm just making sure that this file here is gonna be more descriptive since it's gonna contain basically one lesson, which is the one on functions. All right, so I'm gonna create available with two plus five, and we'll just print this result and down here, let's see, we have our seven. Now. One thing we can do is create a function instead of just writing that out if we're going to reuse it. So I'm gonna create a function using the d e f keyword in the name of my function, which is gonna be ad, and from there I want to have two parameters because we're using two numbers to add. So I'm going to do an N one and in two as my parameters. Then I'm gonna do the same thing we did up there, which is to add them and put them in a variable. So now I have my result. What do I do with it? I'm just going to return result and let's now print this out with R two and R five. Save and run and we get the same seven. So our function the deaf keyword, the name of the function, whatever the parameters are and a colon and in the indentation for the remainder of the function body. So, of course, this is indented all the way to the left, which means we're no longer dealing with this function. So that is a full body function. There's another way to create this. So if we have results to weaken, do it basically in line, which means kind of putting everything on a single lying rather than creating the body of, ah, function like we did above. And that's gonna make use of what's called a lambda. So I'm going to do Lambda keyword. I'm gonna have my two parameters again. That's how they're defined, Colon. Now in defining the body of the function like this. So that's quite different than what we did above. But you can still see the flow of it. Now, to use this, I am going to do print and I want funked result. And actually, I'm gonna change this to just funked results like that. Okay, so right here this print is the same as our print with our add two and five. And actually, it's gonna be the same all the way through. So let's go ahead and run that and we get another seven. So the end result in the call for the user of the function is not any different. But for us, the developer, the way we created it is different. So that is Lambda as so Let's look at another one of these. Since there a little bit odd syntax, we're gonna do funked results too. And what we're gonna do now is use a map and inside the map, we're gonna have our land. The key word, and they won't have a parameter. And then what are we doing with it? We're multiplying at times, too, and then we have the map. So these are gonna be the values that we're going to manipulate now to make this work lunar print a list and in the list were using our function to and you'll notice there aren't any parameters. Let's save this and run in down here you can see where our output So we're basically taking each element, multiplying it by two. That's why we get this to 8 40 So two times one is 22 times four is eight and then we have two times 20 is the 40. So where did the X go? The parameter for the function? It's implicit because of how we're making use of it with this list and passing in the function. It knows now with this construct to use each of these items as the parameter X and then just continue looping through them because everything is defined in a map. All right, so let's continue with some other functions. Let's have calculate de Miles and in here what we're doing is basically maybe a taxi or something, and there's some feet per miles, so we're gonna do result equals miles times 0.7. Plus, we're gonna have a flat fee as well. So, having many miles, you're gonna go. It's 70 cents per mile and then you're always gonna pay a flat feet, and we want to do return result, and we don't want results. We just won't result. All right, then we're going to calculate fee were passing in a 10. Saving and run. We have a 100.5, So let's do that again. But this time we're going to set are flat fee inside of the parameters. So I'm gonna have rate of 1 50 And what this means is we're setting rate as a default, but we're allowing the caller to pass in their own rate. And if they don't will make use of this 1 50 colon and never going to define the body of this, which is really the same one we have above. So miles times 10.7 plus 1 50 or whatever the user may want to pass in, then return results. So let's see how that works when he have print, calculate fee and we have basically two different ones. And this time I'm going to do a 10 are flat fees gonna be three. So that's gonna be another dollar. 50 more. We should end up with $10. Right? Let's run this. And there we are. So we also know if we don't pass anything in, then we're just gonna go off of that. So let's go ahead. And then if that's the case, I'm going to remove this for now. Save, save, run. We'll get our 10 now. What if I don't pass anything in and run? We get our 8 50 so you can see it. While we actually are overriding what happened up here with this one? Because it's the same name were just using additional parameters. It works by itself. Even if we aren't overriding this one up here, we still get to use the 1 50 default and whenever we're not doing anything with it. And so I just put our three back there and we have our 10 now. What if we want a variable number of parameters? So let's do this default calculate multi rate. And I am going to do Star Miles Colon and to find the body so we're gonna have rates as an an array and empty array for now, then we're gonna look through miles, so four am in miles, we'll have rates upend AM times. We're gonna use a flat fee here. 1 50 Return rates. Notice. I'm in denting out of the four loop here with this return. Okay, so let's see how that works. Then we can talk about it a little bit more. We're gonna have calculate multi rate. So the multi rate is gonna be 10 2030 multiple parameters air going in a variable number of them, in fact, because we can have one. We could have two or three or whatever we want, and we're still able to calculate all of them. Let's do I equals one here and create a loop. So four are in rates we're going to do print, customer and percent de cause we're gonna use a number here, then dollar sign when he have percent de again. And then then, ah, parameters that we're gonna use inside that string. What you're gonna be I are, And we're gonna do an increments on ice, so it's not stuck. All right, let's save and run. Let's see here. I don't need a comma right there. And here we are, so everything getting printed out very nicely, and basically what we were doing with our Miles is customers different customers may have different rates, so maybe there's a loyalty program or something like that, whatever it might be. But anyway, we're seeing how we're able to take in using the star here, available number of parameters and just loop through without any concern about how many there are. And then down here, you can see we just get the result back as an array and just loop through it. So nothing special going on in regards to the result that came back and then put them out in a nice format. So that is a more formal look at how to define and use functions and various types of parameters that we can use as well. 17. 303 Lamba Expression: we want to. Now look at Lambda Expressions of more detail and from our last lesson, we did have an introduction to them here. But now we're going to formally work with them. And so I'm gonna create another file. This is going to be another module three, and we're just gonna call it Lambda P Y. All right. So to start with, what I want to do is create just a regular function so we can get a comparison. So we're going to calculate fee will have Miles, and we're going to do a results. This is coming from our last lesson, plus 1 50 return results. And we can then, of course, see the output of that scroll down and we have a 6.4. So it's Lando's. We're basically doing everything in one line. So if we have our add function, when we do Lambda Key word to start this off now we build up our function, so we'll have two parameters in one into, then two separate the parameter list from the body. We do a colon, and then whatever it is we're wanting to do, and this is going to automatically return, so there it is. And if we print ad to five save and run, we get our output. So let's go back to our other function. So you notice also with the Lambda single line here and then we assigned to a function name quite different than what we were doing here. So let's create it of another version of calculate fee and we're just gonna call it calculate fee to so calculate fee to So that's our function name. So what is in the function? What? We're working with the land that we need to land the key word. And then we're gonna have our parameters, which in this case, the only one is Miles. So we'll do that. Then a colon. We separate out the body of the function. Now we look at what went on inside of this function. It's basically this and then we return that now we could have just put the return, were keyword here and just returned out, and this last line would have went away. So let's take this since it's basically what's going on and paste it and that gets returned . And if we run this again, or for the first time in this case on this particular version of the function, we get 6.4, same as our result from the traditional function definition. So let's do another function. And this time we're gonna do a mixture of traditional function, and land is being used in the function which is going to help, Um, some of the code. Ah, verbosity. I guess you can see in compressing it. So we'll have miles. And what we want to do is, let's say results template equals, and we're going to have this functionality right here. So we're gonna paste it here. So that goes into result Tim Plate. Now, this is a very simple example. But if we then call result template, we can take miles and calculated. And then down here, we print out. We have calculate fee three. Actually, we want three, and we have seven. Run it and we get 6.4 once again. So all this doing is showing that you can actually use Lando's within a traditional function. This is probably not a good case to do it because we're not really accomplishing much, but it just demonstrates the fact that it is available. So that is a look at how to use Lambda us 18. 304 Classes: Now we're going to work with classes and classes are basically constructs for holding data . All right, so what I'm gonna do is create another module three and call it classes. P y are just class. This is good. Okay, so we're going to create a template, basically classes or a template of your data structure so you can have properties, you can have methods. What I'm going to start with is an employee, and this employee is gonna have several properties, and I'm gonna initialize these two just empty strings. So we'll have city name and department I d. And then we're also gonna put a function in here. So I'm going to go a little further down and noticed I'm still indented, and I'm going to call this get profile, and we're gonna pass in self. So explain how that works were actually going to a call to the function, and it will help with the explanation of this self. So we're returning self died name, and we're gonna do a space. Then we're going to do City Self dot City. So eventually, just printing out the attributes for the employees, and then we'll do a plus and department I d equals and here will have self dot department I d. And to say that. Okay, so that's our template Properties in one function. Now, I'm gonna call this, so I am going to create a variable toe, hold the employee, and I'm just gonna do employees. Yeah, I've got an instance of an employee. I can assign properties. So I'm going to do, let's say, John Doe and city how to say Seattle. Then I'll have department I d. I'm gonna do 100. And now I can call this function diet, which is gonna be get profile groups like that. And Ryan, Now you notice here we're getting an issue with this stream and saying we're trying to integrate an inner jer into a string. So here we gave it a value of 100. Although it is initialized to this basically an empty string at run time. We're overriding this thing. And so what we need to do is a cast. So it doesn't matter if initially it was a strange because remember, in Python you can change things on the fly like that. So if we run this again here, you can see the output. John Doe, Seattle Department I d. Equals 100 and notice there's nothing being passed into this profile, although here we have self. So this happens implicitly now if we didn't use self, try to run this again. We're getting an error because it does take one argument, although it's implied so that we can didn't do this kind of accessing of the properties. So I'm gonna put that back. And that's how self works self is the instance that you've created here and is working within this employee class. Although on the caller side this here, you don't need to worry about it or any of this here. You don't need a war about self just as long as whoever is creating the class is taking care of it. All right, so let's keep going with another example. I am going to create a person class, so I have my class keyword person colon, and now I can start defining some properties and properties don't need to go first. You could put your methods first. It doesn't really matter. You can makes them. I'm just doing this for readability, though, and city States All right, so we're gonna have another method. This is a special method called the initial Isar, and it's too underscores and it to underscores. And basically, whenever I create a person, I'm gonna have to also pass in some values, whatever I specify. So I do want to pass in my self, which on the collar side, they're not going to know anything about it. And then I'm gonna have some parameters, which they're gonna be all four properties and I'll do it, Colon. Now I'm going to do the assignments to our property so we'll have Effin won't have Ellen. We'll have city States, and then where everything's a sign, I'm going to do another function. This is going to be similar to our other get profile Again. The color doesn't have to worry about any parameters here, and I am going to return. So we're not printing were actually just sending back the profile. So in this case, I'm going to see first name is, and I am going to use a string, so I'm going to do percent s and last name is percent at, so I need to different parameters. So I'll separate my parameter list out with this percent and put them in parentheses. So have self first name, self last name Save now need to create an instance of person. So I'm gonna do person. What I have to do now is pass in values. Otherwise it's not gonna work. So if I just do this and run and scrolled the own, you'll see here it is warning the different arguments. It says five arguments. Although the caller really only needs to do four and notice here one given. So we're only going to specify the four. So we'll do Jane Doh, and we'll have Denver. Then we'll have si o se Colorado and friends. So we're going to do p dot get profile like that. Run. And here we are. First name is Jane. Unless name is doe and basically that's all we're printing out. We could, of course, use these other values. But here you can see the difference in the creation of thes instances. Here we created an employee with no guarantee that any of the properties had values. So we had to manually go and do that. At some point, it was up to the collar to make sure they had values. Whereas here we are forcing the caller to provide some initial values, and when they go off and use this person instance, it will have those values to it. They don't have to worry about assigning them at some later point. Now these properties can be over it. And just like we did with the first and sense where we did this explicit assignment. So I have first name I can do Geo, save that run and we can see here we're getting Jill, whereas before we had Jane so they can be overridden at some later point, and that is a look at how to create classes and python. 19. 305 Inheritance: Additionally, what we can do with these classes or a different types of overwrites This gets more into, say, inheritance. So let's say we have a read the definition of employees, and in this case, we're going to do class employees person so that there is an awareness for this employee class. It could make use of person items. So here's what we're going to do. We're gonna create an initial method. So initialize er and we're going to do self name City Department, I d. And these air coming from not person but employees these the properties were passing in. All right, so for the the body of this, we're going to do person diet. Annette. So we're calling initialize er on person. We're sending it some information that it may want in a for last name, because we don't have that. We're assuming they're just passing in their first name. We do have city. Um, we don't have the zip the ah abbreviation for state. So we're gonna do in any there as well. And then we're going to do an assignment for department I. D. So let's do department. I d. No notice here. I don't have one underscore. I have to. And this is kind of odd, because what are we doing with department? I d This is going to be basically a private property, so we'll have department I d like that in return. All right, so now what we're going to do is create this private variable two underscores department I d. So these two underscores. Signifying is not visible to anybody else but us. And that's how we're getting away with assignment to the base class department. I d. And the base class is going to be the one appear, which is employees. All right, so now what we're going to do is create an instance of this, and I am going to do a demarcation just so we can see what's going on. So we're going to do Dan Johnson, and I'm gonna do e to I wouldn't have employees, and we're gonna pass it, Dan, we're gonna pass Houston and one a one. All right, so next we're going to start calling some of these. So let's print e to died first name and then we're going to do is we'll save and run. Let's see this air here. We're getting looks like it wants to focus in on this line here. And what it's saying is that we don't have a an attribute called name Employee doesn't have an attribute called name. And what I've done is, instead of separating these, I put a diet, made it look like it was trying to use an attribute called Names. So it should be a comma up there. Run this again. And here we are. So now we're creating this instance. So the only thing we're printing out is Dan. All right, we're gonna keep going now, And I'm gonna do a print e two dot get profile and save and run. And down here, first name is Dan, and last name is in a Now, this is making use of a profile that's already been created, which is this one right here coming off of person. So we're making use of the person profile method, and up here, you can see we do have one for employees, but because this is being passed in this instance is being passed in. We're gonna call the one on the base. All right, So what if we want to then create our own so for that, we're going to do a definition that's gonna be get pro foul and we're gonna have self department name. And in here we're gonna do return employees profile when he have percent s percent, as I want to do, department equals percent s and they want to do a percent D, which would be for the number needed string quotes. Here. I'm going to separate the parameters we're gonna use. So put those in parentheses. Will have a self first name, will have self city. We'll have self are actually gonna have department name. We'll have self diet, our private department, I d save. And then down here, we're going to call the one we just created. So we're gonna put advertising for a department name, save and run. And that's the employee profile day in Houston. Department is advertising the ideas 101 The ideas coming from what we passed in right there for the department. I d. All right now, what if we want to take one of these profiles? We have either this one or let's say this one. They're both the same signature and create our own here somewhere. So I'm gonna try to do that, we're gonna have department get profile. Let's say we just want our own. We don't wanna be calling any from the base class or any from the employee class. And so I'm gonna have a return employee profile will have percent as percent s, they won't separate. So our parameters were gonna be self, That first name, self dot city, and then here, go ahead and just comment out this one. Actually, we don't need to, because the one we're gonna use isn't have any kind of parameters. So we're gonna do that. Let's see what happens when we try just that scrolled the on You can see here it takes one argument to given. So there's a little bit of confusion about which one Are we actually using someone a comment that out, save and run and let's see what we run into now. So the employee profile was Dan Houston, and that's using the one we just created, which is this one here. So we're no longer using that one. And if we try to go back to it, we get that error that we had. So we're not really able to do this kind of overriding. In other words. So what I'm gonna do is comment out that comment out this one and we're basically back to where we started. Save, run. And there we go. So that is Ah, look at how to make use of other classes basically through inheritance, which you can see here you pass in whatever it is your basis because employees air always gonna be persons than that should work. And we called our person initialize er You'll want to do that because there's likely properties in sight of person you're gonna want to initialize and provide that information back to We created this private variable department that we could make use of. We gave it an initial value. And then we created our own get profile here with a different signature from the methods that were inside of person and also different from the one that was inside of employees. And we saw how if we just try to create the same exact method, we basically do what's called shadow the methods and employees and inside of person when we do that kind of technique. But we also saw we can only basically have one of the two kinds of get profiles because of the way the signatures work. So that is how you could make use of a base class extended for your own as well. 20. 306 M3 Challenge Part 1: Welcome to the Module Three Challenge. So in this challenge, we're going to go over a number of topics that we've currently been through. So classes, functions, function parameters, inheritance file, I Oh, that's a big part of this challenge. Will be using dictionaries and list Aziz. Well, so what we're gonna do for this challenge is read a given file, and if there's no file, you're just gonna read a default file. So the user has that opportunity to go ahead and just say, read the default file. Now you're gonna read the contents of the file and show repeating words with their count. And if the word is not repeating, still show the count. So it just be a count of one, for example. So we'll go through all the words and show the count of the particular word, and you'll also then display the longest word in the file. So I'm gonna show you a file that we're going to use and I'll show you how the exercise works. So here in visual studio code, I had this file. So this one has Hello. Hello. So there's two hellos in here and we're going to see now how the follow gets process. So I'm gonna go ahead and run this. I'm gonna let it use the default file, which is the one we just looked at. And this is the output. So it just displays the file data in file data. Then here, the repeating elements, if there are any so we could see where our hello has to our how has to. I is one. We still display it. So we just go through all the different words and display How many times we there inside of the file? The largest one here is we're using Hello. In this case, you could see we also have about So we're just taking the 1st 1 really and displaying it. There is another file as well, and I'm gonna show you that one. So here's test and there's not much in here. There aren't any repeating words and tests. So to show how to use that one, we're not going to use the default. I'm gonna tight test dot txt. So the p Y file and these files were using or in the same folder In here you can see everyone has one. The largest word is displayed here. And if I l s you can see I've got my P why? I got the text files in the same folder. So that is the exercise you'll wanna do basically the same kind of output as what's being displayed here. So a little bit of formatting and what I'm gonna do next is a walk through of the solution . So we didn't pause the video, try it out and come back. Once you've done that, I'm gonna go ahead with a walk through of the solution for this project. So I'm gonna go into visual studio code, and we're going to have another file here, which is gonna be the M three challenge P y all right. To start this off, there's gonna be a number of functions. So we're going to create a class as well said, make file, I o and let's see, we want to do I owe gonna pass the end an object. And basically what we're doing now is it's the best practice. So we're inheriting class from object class. And now what we're gonna do is construct a method, um, for getting the file name as a parameter. So here will have our net and we'll do ah course elf file name and will do the assignment of file name equals file name. So in this class, another method. So it's going to get f. D. So basically filed data and we're gonna do return Self dot file scripter wouldn't create another method here this creamy, close, open file and I'm gonna do one called close file. So passing in our ourself, as usual, will have a file descriptor equals open self will do file name and the flag or for reading it. We'll do a close now. Closed file passing in self from yourself. File descriptor close. Actually, we don't need that. Okay, So next will create another class. This is gonna be the file processing class, so we'll call it file processing and Incomes file. I owe so our file io class for making use of it, and we'll create a constructor. So that's gonna be getting the file. Name is a parameter, so we'll have d e f are in it. Self file name and want to set this as a default. Gonna use single quotes here. Txt. All right. It's a file name. The file underscore Name is an optional parameter, of course. And if no parameters given the function will go ahead and make use of file dot txt. All right, we're gonna call our super so file processing self. This is a call to the unit on the super Well. Good do file name. Actually, we want to make sure lower case. So this is a call to this one here, and we're gonna do self file data equals nothing. Now we're going to create another method read file self and in here, self open file. So file data. And that's gonna equal self file descriptor. So this coming off of our base and read and now we need to also wouldn't return file data as a list of elements. So get or definition, get file cleaned data. So no print parentheses are quote sat. Actually, we're gonna return. This is going to be element for element in self dot File data. Replace any new lines with nothing. Now we're going to split on a space. Oops. If element save. So basically in Lambda inside of here, That's what the compressed in taxes. Basically, we have a loop. We do a split, and when we check if the element is valid. And so the element then get sitting back. And as that list of data now we're going to work on repeating elements. So when I have calculate repeating elements pass in ourself. Okay, so file cleaned data, and we're going to do self get file clean data. We want to create a dictionary, Justin initialized. And this is supposed to be brackets, and we're going to then loop. So for elements in file clean data so we know we have elements in here, We're gonna do element for the dictionary item and that is going to be key and now value. So file clean data element. So that cleans it off. And that starts building up element dictionary. And then we're going to check if an element already exists in repeating elements of the dictionary. To do that, we're gonna do if elements dictionary not in my dictionary. So if that is the case, we can upend Element dictionary, and now we'll return. This return is aligned with our If so, return my dictionary and let's see here. I think there's a little bit of invitation issues. This should be out here, aligned with the first tab off of the definition. Otherwise, it's not gonna work. So are four loop. This goes in the for loop. I think that is looking good. All right, so I'm breaking this into two videos. What we're gonna do next has got to calculate the word sizes and returned those, and we're gonna do our output formatting as well. 21. 307 M3 Challenge Part 2: Okay, So picking up on our Model Three challenge, we are going to go ahead and keep going. What we want to do now is create another definition. This is going to be word sighs in files. And then we start doing these calculations on the size, so we're gonna find the smallest so smallest equals. True, It's an optional parameter, and we're going to do now file clean data equals self get file clean data, and we're going to do sorted words should be sort of words sorted. And you can see here that's a built in function. We're gonna have filed clean data and then we're going to do a key equals land that should be a key save and in return sorted words accessing the first element if smallest else sorted Words Last last Hilleman. So this is a lambda here. You can see everything is compressed into one line. So basically we're trying to find the smallest word. So we're using our clean data, passing it into the sordid function, and it's sorting by the length, So that's how we're getting that. That's how we're getting the sorted by the smallest. So if smallest else were returned the next word and that should store it. Are sorting forests down We're going to do, let's see, taking user input. So taking input and we're going to do return raw can put please enter a file name and it's even want to do press. Oh, to start with a default file and I should do it. Let's see. Leave that there. And that's all we need for that function. Then we're gonna do start processing and put data. Okay, so this is where we're gonna output the data, so I'm gonna have our formatting in here is well, so input data lower equals we're then going to do data equals viol processing. Else we're going to do data equals file processing, and we're going to file name input data, and we're still inside of this method. Now we're gonna do data read, file and start with some formatting. So print file data print gonna do bunch of hyphens here gonna go ahead and copy this cause we're gonna use it several times. And so next one have. Actually, I'm not gonna do any quote, screen of data, file data, and we're gonna do a line. I want to do print in file data and it's gonna be colon. We can camp. Relies this and in next room to say repeating elements capitalizes he I'm gonna put our hyphens there and extraordinary calculate repeating elements, and we're gonna have another set of hyphens, and then we're going to print and repeating data elements, and we can go ahead camp allies that then we're gonna do largest element. And for that whoops, we want to do our hyphens, and then we're going to say data, word, size and file, and that is going to get smallest equals, falls, hyphens. And next we're going to do and largest element data close file, all right, And then we can go back to where we're defining. So let's do if name equals main and we're going to start this off, so I'm gonna have input. Data equals, taking input, story processing, input, data save, and I'm gonna move this over. We want this all the way to the end because this right here is actually what's gonna kick off everything, and then it just it's just going to start going through. So let's go ahead and give this an initial try and see what happens so clear? Ls we have RM. Three. So I'm gonna try and run that. And the sea airline 48. All right, so if we go to line 48 see what is the issue here? Ah, we don't have our colon. All right after that. Conditional. And see Syntex there again on 53. This line here. All right, so this should be lined up in here, and we don't need those. Coghlan's there because these are all just gonna run sequentially. Give that another trying 72. We are not defined on taking input. Maybe I don't have my parentheses on that 1 70 to see. That is correct. Input equals taking input. Must see. Put a line between these. I don't think that matters that see taking input. All right, so that looks good. Try this once more. Yeah, still some issue with that. All right, so going back over here, eso I see what's going on. So we have our file processing class, and if you'll notice down here, I'm just accessing these methods like this without any class instance on them or anything, as if they are not part of any class. There in the global space. But in fact, I don't have my indent a indentation. Correct. These need to be all the way over. So I'm just going to go ahead. Let's see. I think I can roll this up and move it. Yes, and opened that back up save. And that's a year now. Great. So press zero to start our capital. Oh, and yes. So we know we're gonna have a few issues here. There's a lot going on. Let's look. So we have lying. 49 local variable data reference before assignment. And let's see here, 74 starts it. But I think the actual errors in lying 49 go upto line 49. We have data, so I think that looks OK. The other was lying. 74. That scene here, 74 inset. 49. So 74 input data is coming from there. Start processing kicks off would go into here. All right, so I see the issue. We're using inequality here. So valence an assignment. And if I go back out, it's gonna clear this. Do another run. See where we are now. Okay. So let's see here. Read and write here. No, such file. So it wants this default file. We're gonna go ahead and add that. So here. I'm going to create file dot txt. And I am gonna pace content that we saw earlier. I'm also gonna create our test file, so test txt and I'm also gonna pace content in it. So the same content that we saw earlier Okay, so let's try now with our default and let's see, you were getting closer. Um, right here, global name element is not defined. I think we can look at lying 24. So going to line 24. All right, here we are with our element, and this is a misspelling. I look over here, I think that seem, Yeah. So that was the misspelled win. All right, try this again, and we are now lying. 31. Calculate repeating elements. It is list. Object is not call a ble. So it's good to work through these to see what the different errors air coming out. Or, um, and kind of interpret them with you toe, you can have an understanding of what may be going on. So here we are, on line 31. This right here should be fine because we're doing key value. And let's see, here it is saying list object is not Call a ble online 31. This is not culpable, is what it's saying. Basically. So I think I see what is going on. We want to do account of these like that. And now we're gonna try another run shift. Oh, and let's see, here we have a misspelling know that looks good. Um, 66 word size and file. Got an unexpected arguments. Small tests. Yeah, that is it should be small s all right. So lying. 66 run through here and we can remove our terminal. Give us a little bit of more room. Small s all right. And make sure that looks good up here where it was actually defined. So smallest and smallest, those were good. All right, we're gonna do a default again. There we are. So that is looking a lot better. Now, let's see here. Here we are, just like we can do. A little bit of formatting are on here, and let's see. Hello is one. Hello is one. So it looks like the counting is maybe not working the way it should you can see here. This is not quite what it should be. A swell. Okay, so in our code, the probably the only place that can really happen And because some of the words are still or getting combined like this right here, I think that would be in this right here, where we're actually cleaning the data. So if we live through this, we've got are split on space, replacing new lines with nothing, which is not what we want to do. We want a space here because then we split on space. So everything gets split. Because if there are new line and we're splitting on nothing, which is what we had before looking at this, that's gonna cause what we're seeing where we wrap. So we replaced the new line with nothing. Then it causes these two to become one word. But if we place replace a new line with the space, then they behave like all the other words that have spaces in them. So that has now been adjusted. And we're gonna run this again, do the defaults. And that was a lot better. So this is what we would expect on our output there's our regular file, our biggest word or in this case, the first of the biggest words. Because again we have about in here. Now let's try our other one, which is test txt and that was good. So now we've completed the walk through of the solution. So of really going through various kinds of issues that can arise as well as you're trying to put something, length is together. It was a little bit. There was quite a few things going on with several functions. So it was good to do the walk through and understand what the errors mean, how to isolate things and fine the solutions to those errors. So that completes this challenge for our model three. 22. 401 Introduction: in this module, We're gonna cover how to handle exceptional cases. So a case that throws some kind of system error, we want to be able to gracefully handle that. So what we're gonna do is cover the block of code in python that is able to handle that. So it's a try where you try the code that may contain or throw an exceptional case, then the except which is where you actually dio with the exceptional case. And the except mayor may not execute. It just depends. If the exception occurred in your try block, you're going to get the accept being hit, and whatever code you have in there is going to run, you have the finally which will always run and the finally is an optional block of code. You don't have to have it. There we're going to see is, well, how to raise our own exceptions, and we're gonna see how do you create your custom exceptions and raise them? So there are times that you want to have your own exception class versus the exceptions that are already available in python. And then we want to be sure we're really solidifying what we're learning There's gonna be a good challenge at the end of this module, and that's what we're going to be covering. So let's go ahead and get into the module. 23. 402 Try:Except:Finally: we're going to now see how to create. Try catch blocks in python and also the finally block What's wall always execute when you have the try? Catch? So what I'm gonna do as we've been doing it, Just create a new folder and call this exceptions P Y, and we're gonna have a function to contain. This is let's call it try catch Finally, and then we'll call that down here. All right, So to start off, I am going to declare a try with the exception on an ability to catch the exception. And this may be for any code that I believe can potentially throw some exception. And I want to do some sort of handling. So to start off, I'm gonna have a try. And then I've got a colon so we can see we're creating some kind of body for this try. So I'm gonna hit enter, and when I want to do here is six plus ABC. So I'm trying to add this integer and a string which I know is not gonna work. So if I just run this, let's go ahead and comment this out. Save and run. You can see here we're getting unsupported, um, type errors. So the exception is type error. So what I'm gonna do is bring this try back. So we're going to now handle that and I'll go back to the indentation where the try is, and do the key word except and then accept Sean as e so declaring this e as an exception variable. Which means it's going to contain some information about the particular exception that's getting thrown. And so I've got my colon here. I'm gonna create a body for the Except so what I want to do is print type e whatever he is , print that out, and then I'll just print the E itself. So these two lines, or the body for the except and I'll save and run and scroll down. So this is our code here that's getting printed out, and the exception now is getting handled. So it's different from here, which could see we at the trace back. And then we get the full print out of python showing us there was a problem. And here we're just handling. So if I comment out this last line, we're not going to see unsupported operating types. So we've taken control of whatever is going on with the exception. So here you can see we only get the type error print out. And then down here, this one is gone in the second case. So I just spraying that back in. So that is a very simple example. We're gonna keep going with ease and get into different kinds of exceptions that we can have. So if I do a 1/0 and then I create my block here for the except which is basically catching this exception and again, I've got my variable, I'm going to print out the type e so we can see what it is and print out the raw exception as well. Now, this is a general exception. What if I want to actually catch the exact exception which in this case is gonna be a zero division error? Now, this implies have got knowledge ahead of time that this exception can occur, which this is a simple case. So we can tell that we're gonna get this division zero division error and the exception zero division error will see as it gets printed out is really just contained in Python's error output, and then I'm going to do as well a finally, and I'm gonna put a colon on the finally so that finally is going to always run. And I'm just gonna do print insight finally so that we can see it does, in fact, run. And I'm gonna go ahead and run this Now, scroll down, but see a need, Something with syntax. So what am I doing on the print? Let's see. That is okay. Now, this right here should be the run that. Okay, here we are. So we've captured this exception. Um, actually, is this one right here? So we're gonna go ahead and put a print this print print second try, except go ahead and run that scroll down. Here we are. So that breaks it right there. Integer division or Mongie Low by zero. You can see our zero division error notice in this case, if we look close enough, we were getting the print out of whatever E is. We've actually fallen into this one here, so our specific case did not occur. And the question is, why didn't that occur? Even if you look at the output, this is actually a capital D we have a lower case, so it doesn't matter. Gonna change that to a capital D now, But let's keep looking at what is out. Putting down here, you can see are finally is executing regardless. So that finally is always gonna be there. Now, we should be able to capture the specific condition here on the exception and see our print right here. And then this one will not execute because what is going to do is just walk through the number of accepts that we have. It's going to see, OK, this is a general one. It's going to keep going. It's gonna come to this one here and say, OK, this is of specific one. So let's just go ahead and see what happens. Now you notice I have fixed R D right there. We're still actually falling into the general exception. And right here we have entered your division or my Jell 00 because this is being printed out so we can clearly see we are inside of that because both of these lines or the two pieces of output we're getting here, So then the question is, why didn't this actually catch it? so it will walk through the different accepts and it's gonna come across the general. And no matter what you have down here, the general is going to go ahead and catch it anyway. So what you want is to place the general as the last one and let your specific accepts go ahead and catch. So I'm gonna save and run once again, scroll down and let's see. So that looks like it did not output. All right, there we go. And this is the one right here. So demarcation, Here's our zero division. That's this here and then, of course, are finally is executing. So you can see in this case, we now have captured this specific exception that is occurring on our try. Here are attempt to execute that coat. All right, so let's go with another example, and I'm going to print third try except and we're going to do Try a equals one plus ABC. So I'm gonna do it. Except and in this case, I want to do let's go ahead and capture whatever the general condition is of. This has e. It's that way. We can see what exactly it is, and then we're gonna This will let you know then what you can do to capture the specific case. So this is gonna be just any when have print inside generic exception and we're not gonna do it. Finally, right now it's going execute. Need a colon. New lion Save. Run. All right, let's see here, Global name ABC Not defined. So this is really, like a type are available. And the exception we're looking for is called name error. So let's go ahead and build that in. We're gonna have another except which is gonna be called name error. And we're going to create a variable, which is gonna be any and then we're gonna print inside name error, exception and print any save, run and scroll down. So here we are, inside name exception. And then we printed out the specific one here. So we are catching that now. If we take it and create one more these, we're gonna do a try and something similar to what we did earlier. We have ABC and then we're going to do and accept exception e. We have an idea of what will happen in this case because that see up here we had something like that which resulted in we saw the generic print out. We're gonna rebuild this this time and we're gonna have print type E. That's really all we need there. Immunity print inside. It's just copy this and we're going to take this. Could be the 4th 1 and then down here. Wanna push this over, create the specific exception? Well, actually, let's look at the output of this that we can create thes specific exceptions so scrolled they own. But see here invalid. And this is online 33. We need to move over. Just move that over, Save, run. So you can see the pythons indentations. Very much aware of what's going on in It gave me an error for nine dinning correctly. So here we are. We're looking for a type error and we're going to now create and except for that specific condition, So we're just going to type air. I'm not creating a variable. We're just going to print type air, save Ryan and scroll down. Here we are. So we've caught that specific exception, so that gives you an idea of, ah, how to figure out what is the specific type of exception. If you have some idea that you're going to throw some particular exception in your code block, and then you also see how to handle the generic and the importance of bartering when you're creating. And then as well, we saw that the finally block will always execute when we're building our try except blocks . 24. 403 Raising An Exception: we're going to see how to raise an exception. So in these cases where we're doing these tries on this code that can potentially throw an exception, we're going to look at conditions in which we have a scenario that we actually want to throw. The exception basically raised the exception and say Something bad has going on here. We're going to completely change the code path now because this really should not have occurred. We don't even put it under a conditional like an if else or anything would just say this should not be happening. This is an exceptional case and it needs to be handled as such. So we're going to create another definition here on a function, and we're gonna call this raise exception what's and then we can take this comment out this one and put this here. All right, so we're going to start with a try and we're going to right off the bat rays and exceptions . Let's that there was some code in this try and we get to a point that we say OK, so whatever just happened shouldn't have happened. We're going to raise an exception. Hussein exception has been raised, and then we're going to catch it. So we're gonna have our except block here, exception as e. And we can just go ahead and print this out to see what it is. Print out the raw bits here and and say in side exception raised. I'm gonna go ahead and put our either run. All right, so here we are, you can see it's just a generic exception. And in our our text or actually r Texas the last one and then we have an exception has been raised from the message that we passed into it. So that's all this is doing is just some additional information about the exception So you can pass that metadata there. And maybe when you're catching that exception, you can use that meditated printed in your logs are other places that you may want to have that additional information. All right, so we're gonna do another here and raise system error. So this is a different one, and we're going to say a system error has been raised. We're gonna go ahead and create our exception block or accept block here, have variable, and then we're just gonna print it out to see what it is all about. Print E, we're going to say, gonna copy this Inside system error has been raised and run. Here we are. You can see system error. There's our custom message. All right, so we're gonna keep going with ease. Now we're going to create a class, and it's going to be of, ah, particular exception because you can see all the exceptions that were gathering are really just the ones that are built into python. What if we want our own kind of exception? So for that, I'm gonna have to break out of the function we've got here, and we're going to create a class. So I'm gonna call this class in exception run time error, Colon, I'm gonna create an initial Isar and self orig colon self dot org's equals Argh! And then we're going to go ahead and try this and what I'm gonna do, I'm gonna take this. Actually, I believe we should be able to run it like this and put a comment raising exceptions that we kind of know this is all part of the same thing. And then down here, we're going to do a try. So raise an exception. We're gonna dio a. An exception has been raised. And let's see here, try raise. All right, makes your mind entin tae shins Or there I'm going to start with except indu system error as any then print inside system air. And it's going to just be print custom exception raised just so we're not confused with our others that we have there. All right, we're going to another, except it's going to be an exception as a and print inside and exception raise and let's go ahead and do our other meta data here. So when a print, the tight gonna print the actual exception and these air all a ese and then we can go ahead with are generic exception, so now we can really see is our custom exception actually gonna fall into our custom block right there? And it's gonna be as e we'll just print inside. Generic exception save, hurry. So let's go ahead and run, see if our classes accessible and everything that we want. So you notice our text is being printed out like this looks like it's maybe an array or something in each character is getting divided up into some kind of element. Basically, this is a to pull, and we're getting two of them because we're doing two prints. So I think what I meant to dio here was type, save, run and let's see. All right. So, yeah, you could see them. We get are an exception. And then we get this down here, which is not really what we want. So we need to handle this because of it being a to pull and to do that were based going to send back like this a to pull structure that were this thing can get unwrapped. And if I go ahead and run it again, and here we are. So that is our message that we passed in right here. So when defining our class will want to make sure we're handling this as a to pull structure and then down here, it'll unwrapping a nice format on a lot better than what we saw earlier. And then you could see our regular string that we printed out here is well, so that is how to do raising of exceptions. And then, of course, we saw How do you create a class of your own kind of exception. And then we saw we can get the type of our exception printed out, and then the message that's passed in from the collar basically and that can be printed out as well as long as we handle that to pull case, which we did do up here by doing this. 25. 404 M4 Challenge Part 1: We're gonna go through the challenge for Model three now, so I'm gonna show you what this program does and explain what you'll want to do when trying to meet the challenge. So here we have some prompts and you can see down here the user needs to enter in some option. So the options are one or two, which we're going to be a calculation on age, our math. Now, the way the program works, there's two parts. So this is appear at the top where I'm explaining our program will have two parts so you can do age and which will do validation on age. And basically you're just checking is the age between, say, one and 99 if it's not, it rolled. Throw an exception and you'll print out a friendly message about what happened. So, for example, they didn't do an age within the range restriction. You also handle exceptions when the user doesn't enter in a one or a two. So if they do a negative one or a letter that's gonna throw an exception, you again handle it in printer friendly message. So I also display just the Topic seven recovered in this particular program. So let's see how this works. If I do a one, I get an input for age. So they were running the age part of the program, and I could do 85 and we can see here. Option executed successfully. This is the following block. It will always print at the end, so everything is wrapped in a try except Anna finally. So the finally is always going to go. Let's do this again. I want to go back with age. I'm going to do a negative one. So that created a custom exception that also recognizes the ages out of range. And it prints a message that you need to be between zero and 100. So basically, the way that works is you're raising an exception at that point. So you'll define several of your own custom exceptions. And in some cases the exception may be thrown on its own, or you'll raise it and print out a message. Okay, so if I go back with age, I can also do something like 150 that's still going to display this type of message. Now, if I don't do a one or a two on my options, and I try six. That's going to create a problem. And we get the custom exception than it says what you need to do in regards to choosing one or two. And again, it's handling letters, so option must be an integer. Now let's go into the math part. So I need to enter dividend so I could do 10 divisor 10 AM again one and you could see again. The finally block is out putting. I also output what the division is with 10 and 10 now. I can also do something such as, let's say, a 10 and a zero, and so the dividend may not be zero. So, basically, that's what's happening here. You're handling various exceptions scenarios by creating your own kinds of exceptions. So those exceptions will have their own messages, and you'll look for those particular exceptions. While the program's running in its main loop and you're going through the calculations, you'll be able to, for example, one except might be zero division error, and so that drops into an exception. You print out your message, so that is the challenge. What I'll do next is a walk through of the solution I have. And go ahead and pause video, Try it out. And if you can't get it, come back in your bill to see the solution. Okay? I'm gonna go ahead and walk through the solution for this particular challenge. And what I want to do is go ahead and get out of this and go into my folder with our exercises, okay? And so now I'll just bring this down to a manageable size. Let's see, it doesn't look like it wants to go. Knowledge is Put it here, and we're gonna go ahead and create a challenge for module for which is are exceptions. So here will have, um, challenge P y. Okay, So the first thing I want to do is start creating some classes for the various kinds of exceptions. So we'll have to start off just a regular custom exception, and it's gonna take inheritance from the exception class. I'll create my initial unit and passed in self. Then I'll set a default message, and that's gonna be option. Must be one or two. Now, what I want to do is, um, override a method for our stream and the way that's gonna work is created of the method and we're going to do str self that will return self default message All right, And then we're going to now move into some of our other classes. This is gonna be one for the age exception. So here I'm going to do class is gonna be user age exception custom exception. And we'll have an an it self. So here I want to do default message. You could see I'm getting default message from our class we're inheriting from, so this will be user age must be greater than zero are less than 100. So the way you can see this working is we just kind of defined some generic exception here . We've got a message that's going to run, and then we build off of it. So here we just were building our particular cases out is what this basically is doing. Now we're gonna do a class for our divisor exception. It's gonna be a custom exception as well will create our and yet we'll have a self default message that will say divisor must not be zero. So next what we're gonna do is start with our loop um it's gonna be inside of a function. So when I have math functionality So we're taking the two scenarios you want to age or math and putting them aside methods. So while wine, we're gonna try And here's where we're doing our calculations. So here's our divinity in We're taking the input from the user. So please enter a dividend, and we're gonna take a divisor so similar kind of scenario we take user input, cast it to a new integer please enter the divisor. So these run one after the other. We get assignments into those variables, and we're gonna print those values. So we're gonna have the formatting of division here and then equal, so it's gonna be afloat, and we'll separate that out. So here or the values were going to use, we're gonna have our dividend, our divisor, and we want to do the actual computation. So here will just do that in line like that and break so next. What I'm gonna do for the try were gonna create and accept. This is going to be a name error in here. We're going to say print dividend and divisor must be numbers. See, I hear, We'll do continue. Keep the loop going. And what we want to do is well, And these were built in, by the way, these exceptions. So we'll have zero division. Erin, I want to print as well. So this is going to say the dividend may not be zero. So that handles are while it we're gonna continue on this and another video. So I'm just gonna break this up into two parts, and the walk through continues in the next ones, so I will see you there. 26. 405 M4 Challenge Part 2: We're gonna continue now with our Model Four Challenge. And we stopped here in our math functionality with our division error here, zero division error. Now, what we're going to do is move on to our next method, which is gonna be for our age functionality. So we're gonna call that age functionality. All right, so here we're gonna start with our try this creative arable called user age input where we're capturing the user input and we're casting it as well. So we're gonna say input a valid age. And next, we're going to handle that case. The exception for that. So I'm gonna have a name error as e creative bearable for that and print, which is going to say age must be a number, and we're gonna have a conditional if user age input. I want to say less than or equal to zero. Our user age input greater than equal to 100 raise user age whips. Exception is what we want here. So that handles both of our calculations, math and age. Next, we're going to define a method called our program, and what I'm gonna do down here is just create where we're gonna start with our searching for the main entry point here. So that's how we're gonna kick this whole thing off and then we'll start calling these methods. So this is the 1st 1 that's gonna get called. This should be our and in here, what we're going to do is just print are prompt, and I went to do our program will have two parts. So we'll just do age for one. Get rid of the hand there and math for the other. Well, let's just call this one here dividend and divisor. And then we'll say we will cover the following topics and we'll just have our do it this way. Um, exception handling will have, try, catch, will have trying else. Finally, And we're gonna do custom exception three quotes, single quotes And let's see, you want to get back to our main entry point here. So, Colon, next, we're gonna call our program, so that will start running. And we're gonna start off with a prompt select one option. He's gonna be on new lines, so we're gonna have age, and we'll have also to That's gonna be math now will kick off a loop. So when you have an outer try, we'll have an inner try, and what I'm gonna do is start aligning up a few things here. So we're gonna have on the outer and accept. And this one here is gonna be value air as e we're gonna say, print. This is gonna be please select option between one and two, and then we're gonna have another. Except this is gonna be catching two different ones. So custom exception user age exception. Either one. And then we're going to say print. This is gonna be custom exception occurred, and we're actually just going to put here whatever it ISS. So we're doing a string. The placeholder is e We can do this string right here off of the exception. So if you go back to the custom exception where we did this override on string, so that's how that is gonna work. All right, so we're still out here in this outer try doing these various kinds of exceptions. Now we're going to do in else. So you may say, Well, where's the if this is coming off of the try? So this is working in conjunction with it. And for that we're going to see one. A new line. This is gonna be options executed, successfully break, and we'll have our finally on this outer try. And that is going to do. We don't need a space. This is the finally block it will always print at the end and new line because just in case will keep running. All right, so we have our inner try. This is where we're gonna do some prompting for the user. So options selected is gonna be cast as an end. And the prompt is going to say, um, option. And we can have a name exception here, so except name here will do a variable just to do it. And here we're gonna have a new line, and we want to say in this case, option must be an integer value, and we want to ensure the loop keeps going. So we're gonna have our continue and then let's see, I don't want that many new lines. We're gonna line up with our inner exception. We're going to say if option selected equals one, we want to calculate the age. So age functionality, we'll say Elliff option selected equals two. We want to do math functionality and then we're gonna have a boundary check is well, Hammer's gonna do a new F. So if option selected, we're gonna do from zero 23 So if option equal to or less than zero and options selected is greater than or equal to three. So in this case, we're going to raise custom exception safe, and then we start next with our outer value error exception. So I think that Whoa, be everything that we need at this point and we can try and test this out. So I'm going to save. We'll go to terminal and run. All right, so that's looking good. Let's try age Interval Ege Age Older 35. That looks great. Try age again. It's to 100 and we get our exception here and we can see we get a print out telling us what to do. Let's try a minus one, and that's looking good. Also, never gonna try our math calculation. Let's do the success case and we get our print out of the computation. It succeeded would get the finally block as well. So let's try. And now on our math with zero case, so we're gonna have 10 0 and we get the dividend may not be zero message, so that succeeds. Now it wants us to try again. So will do. Zero divided by 10 which should succeed. And it does. Let's try now to choose an incorrect options to eight. All right, so that's looking good. Let's do a letter A. That's also looking good. So and handled the case that you need to put an integer in. And I think now we are good to go. It's go ahead back with age and will just exit. How so? I think we're covering our cases now. The program is running and executing as expected. So that is a walk through of our solution. Our final solution, actually, for this challenge, the final challenge. And I hope you found it to be a pretty good range of skill set testing and also a decent level Difficulty 27. Course summary: congratulations. You've now reached the end of the course. I just like to go over some of the main points that we have hit in the course and where you can go from there. So we've looked at a few major areas and starting off just really getting our feet with with what is Python making sure we fully understand what it's about, what it can do, what you would want to use it for. And we looked at a particular kind of editor that we can use, which is very diverse. You can use it with a lot of other languages. Then we got into our fundamentals of the python programming language. This really set a foundation for python and gave you all the basic kind of constructs that you need the different kinds of flows In regards to condition, ALS and loops. We saw a bunch of different kinds of data structures as well. We moved on into more advanced topics, such as how to create functions passed in various kinds of parameters. We looked at Lambda Functions and we got in the class structures and python, which are templates, data structure, templates that you can then and Stan. She ate and we saw a swell how to create a base class and inherent from the base class with a more specific concrete type of class moving on from there. We rounded out the course with some exception handling and seeing how to work with exceptions in Python, so the built in exceptions, we created our own custom exceptions. And so how to raise exceptions. And each module ended with a challenge to make sure you really solidified what you were learning in that particular module. And the challenges had a fairly good level of difficulty, and I hope that they did challenge you and that you are able to make it through some of these or get fairly close to the end. And I do encourage you to golf and try and complete one on your own. And, of course, the solutions are there with walk throughs, full walk throughs of how to accomplish the challenges. So that wraps up the course, and now you are on your way to making use of python and having a skill that you could now add to your resume and market it. So I certainly encourage you to do that and just kind of briefly talk What you know about Python when you do list it as a skill? Meaning you're very familiar with all the basics, of course, But then you can put in there you also know really well different kinds of exception handling. Ah, lot of the data structures and you can go off from here and then create different kinds of abs just small APS, and then even put that on your resume as well. So good luck. And again, I hope the course was very beneficial and you enjoyed watching it and learning and adding this skill set to your toolbox. 28. Django promo: If you want to take your python skills to the next level and begin developing, websites are encouraged you to take a look at my other course. Learn python, Django. A hands on course. This will give you everything you need to know, even if you have no knowledge at all of Django. So just like you may have not had any knowledge of python. And that course took you from zero to very competent developer. The same will happen with this Django course where you go from zero to being extremely competent and knowing how to create websites using the Django framework. Then, with python alone, you've learned a great skill. But you can't really build websites with just python. Like most languages out there, you require a web framework that's based on your language. So it works with the particular kind of language that you want to create websites in In this case, the languages python. The framework is Django, and the course is learned. Python, Django, A hands on course