Python: coding from beginner to advanced | John Harper | Skillshare

Python: coding from beginner to advanced

John Harper, Cambridge Programmer, AI engineer

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
72 Lessons (7h 51m)
    • 1. Introduction to Python

      2:13
    • 2. Setting up python, pip and jupyter notebook

      6:51
    • 3. Jupyter notebook

      3:16
    • 4. Print and input

      3:33
    • 5. Practical - Welcome message

      1:10
    • 6. Solutions

      4:07
    • 7. Common data types

      8:49
    • 8. .format

      2:36
    • 9. Operators and comparisons

      4:58
    • 10. Practical - Basic calculator

      0:40
    • 11. Solutions

      6:55
    • 12. Comments

      1:41
    • 13. Practical - Weight converter

      0:25
    • 14. Solutions

      5:31
    • 15. Importing modules and using methods

      6:42
    • 16. Investigating errors

      4:54
    • 17. Practical - Dice rolling simulator

      0:42
    • 18. Solutions

      6:02
    • 19. If else elif

      8:10
    • 20. Practical - Number guessing challenge

      0:44
    • 21. Solutions

      8:46
    • 22. Lists

      1:43
    • 23. A few list methods

      5:24
    • 24. List indexes

      5:20
    • 25. List slicing

      7:42
    • 26. Practical - Twister spinner

      1:41
    • 27. Solutions

      6:00
    • 28. Practical - List within a list, within a list

      1:55
    • 29. Solutions

      8:04
    • 30. For loops

      8:48
    • 31. Continue, break

      6:04
    • 32. List comprehension

      9:02
    • 33. Tuples and sets

      4:18
    • 34. Disctionaries

      5:32
    • 35. Practical - Currency converter

      0:57
    • 36. Solutions

      13:48
    • 37. While loops

      2:18
    • 38. While loops in games

      6:28
    • 39. Practical - Number guessing revisited

      1:22
    • 40. Solutions

      5:30
    • 41. Practical - Five lives

      0:45
    • 42. Solutions

      8:41
    • 43. Functions

      7:11
    • 44. Function parameters

      14:46
    • 45. Recursive functions

      4:38
    • 46. Practical - Tic tac toe

      1:19
    • 47. Solutions

      14:01
    • 48. Solutions part 2

      21:11
    • 49. Solutions part 3

      14:00
    • 50. Requests

      18:01
    • 51. Practical - Real time currency conversion

      1:11
    • 52. Solutions

      9:25
    • 53. Practical - Live bitcoin values

      1:44
    • 54. Solutions

      16:38
    • 55. Create your own modules

      7:06
    • 56. Reading and writing files

      4:49
    • 57. Object oriented programming (OOP)

      4:42
    • 58. Creating a class

      3:23
    • 59. Methods in a class

      7:30
    • 60. Init

      12:23
    • 61. Objects - using attributes in a method

      5:44
    • 62. Changing variables in a class object

      10:04
    • 63. Practical - Adventure journey

      2:59
    • 64. Solutions

      33:43
    • 65. Solutions part 2

      17:51
    • 66. Practical - PayFriend, your own online bank

      0:44
    • 67. Solutions

      8:51
    • 68. Functional programming

      2:04
    • 69. Lambda

      3:59
    • 70. Map

      9:51
    • 71. Filter

      5:44
    • 72. Generators

      5:07
73 students are watching this class

About This Class

In the fourth part of this thirteen part series for full stack web development and AI integration series, you will learn the ins and outs of developing a mastery of coding through the incredible language of python!

Python is the best language to master the ability to code. It's highly readable, powerful and versatile. With python you can develop games, web apps, machine learning and AI, and much more.

I place a great emphasis on learning through doing, so in these lectures you will have ample opportunity to code along with me, and try out your new skills on practical portfolio projects.

One of the great benefits of this course is once you've learned python here you have the choice to continue your progression into data science, machine learning, AI and beyond! You won't regret trying this class.

Transcripts

1. Introduction to Python: Now, before we get stuck in with the actual coding, let's go into a little bit about the background behind Python and why it's my favorite language and why I think it may be yours as well. Once you've had a good goat, it so Python is simple, versatile and highly readable. Because it's simple and easy to grasp on. The code is relatively readable. It means that this is one of favorite languages for beginners to get, indicating it's versatile because it's using so many applications is used in creating websites. It's used a lot in math and statistics. Is uses one of most popular Colin languages, by far for creating artificial intelligence. It's used in creating games and smartphone APS. So we have pie game that's used for creating incredible games. We're gonna be using Django in this course to create Web applications. We're going to be using something called Paris, which is a library within Python to create our own machine. Learning models are in deep learning models are in artificial intelligence and also confused in the Internet of things. So pythons used quite a lot for creating smart devices, which makes us this really interesting and exciting area to be in. So both was created by greedy Van Rossen and released in 1991. So it's it's It's been around for quite a while, but not as much as some of the other languages is highly popular and is getting so much more popularity every day because it's so simple. Read about readable and versatile, and it's hide accessible for for beginners. But it's also used by professionals all of the time. So just like with JavaScript, the concepts that you learn in Python, they're very transferrable to all the other Kate languages. So I started in fightin, for example, on that made it incredibly easy for me to then and JavaScript and to also learn Java. So, as I said before with JavaScript, Python is well worth learning. It's one of the most in demand programming languages of 2 2019 on because of its applications, especially in machine learning, is because it's really gaining traction quite a lot, not only in how many job postings there are for it, but also in the salaries as well. So it's definitely worth letting Python from that standpoint as well. So up next, let's talk a bit about how we can get set up with Python 2. Setting up python, pip and jupyter notebook: It's another. We've been over what Python is and what it can do. Let's get started with setting up with everything you need to get Python working. So, first of all, visit www dot python dot org's so you can download python on your computer. If for any reason you don't want to download Python or you're flying difficulties getting any parts of this working and you just want something simple, then you can always just go to this. So if you type in code Lapp CEO lab dot research dot google dot com You can pretty much do the same thing that will be doing but online sued After down on anything, you just click on New Python three notebook. This is all online or through your browser. You don't need to download anything, and you can just do the python in your browser. So that's just if you don't want to have python running or you don't want Teoh, get it set up just yet on locally on your computer for everyone else, simply where it says, download the latest version. If you're Windows, it will suggest Windows. If you're on a Mac, you're suggest Mac. They literally just download this. No one's that's downloaded. You haven't e x e file, which you run. Okay, so I've already got Python set up on my computer. So is offering to upgrade. Now. I'm just gonna go ahead and do that for everyone else. It will. Pretty much to say. Do you want to basic installation or something like that? So the python installation may take a bit of time, so I'm just going to pause the recording here on day. I'll pick it up once once the insulation has come to the end. So now that the installation is complete, what we can do is we can just make sure that python is running properly. So if you're on a Mac, you go to launch pads and run on just type in terminal T e r m i n a l and open. Yet if you're on PC, then just type into your search command. CMD you should get the command prompt. When that opens up, you'll see something like this. No, you need stairs. Type in the python or python three. Try python fest, and this is literally just opened python. So it's saying python 3.7 point four. That's the version number. And then let's just try something basic like one plus nine and enter Perfect. So this means that Perth Python is now running correctly, so we can just leave this by typing and quit and then even brackets fantastic. So now we know that Python is working on DWI can work just using here. Or we can use a text editor like Atom Teoh. Run all of our codes, which is perfectly possible. Like I say, we can use this Google Carrabba's well, which we, for example, one past nine. This is running python in the background as well. There's one thing that I like to do called goop to notebook and Jupiter notebook or something that you condone. It launches in your browser. You're running it locally. So essentially, let's just try and get that set up. So we're now out of Python. We want to install something called Juice Notebook, so we have to do. There's something that comes with Python, and it's called Pip. So I just typed in pit on what we can do. Papers like an installation package manager. So if you want to install any other libraries that Python news is, then you can just do pit. Install this. I want to install Jupiter notebook. Some type in pit install Jupiter. So I advise you to do the same. So my in station was quite quick. That's because I already have due to notebooks installed. So just to catch up on what we've done so far with installed python going to python, don't talk and then we're we're toe are open. Either Our command prompted for on PC wherever. What? Open the terminal. If we're on Mac on, we typed in from here. We typed in python, and sometimes if that doesn't work, try Python three no spaces, and that's shown that we can actually open Python. And then we quit by Quit the program by writing quit and then, um, empty brackets. So that's how we would see the pythons working. We then typed in Pip, which is basically something that's built into Python that allows us to install other libraries of python that don't come as a default. Let's say so. We did pip install and then thing we wanted to install was called Jeeps notebooks. So we just type in Jupiter, and then I put in Enter on and it basically said that has already installed for you might take a lot longer cause, and he said, downloads and packages and then install them. So now if that's working, but we have to do is type in Jupiter notebook. No, wait for a few seconds. And once it starts up, it's basically just open up a new tab in my browser from which I can then show you How do you know what works? So that's no appeared. I haven't I'm not touching anything on my screen now. I can just great to new here in the top, right? We'll go over that next lecture as well, and select Python three. And now we can run Python in our browser in this cool little thing, which essentially allows you just to run your code very quickly. If we were to use a text editor, then we'd have to save our files and run them through the command line or something similar like that. Do the notebook at this point where you're learning, bison just makes it super easy. You can do it without it. That's fine, but this is how I recommend you do it If for any reason you don't want to use pit, there is something else that you can use called Anaconda. I don't use this myself, but it is very popular. So I thought I just mentioned it here. Essentially an a condom. You can think of it as containing a lot of different python libraries that are really useful in data science, so in the course were mainly going to be using Pip to install that stuff. But you can always use Anaconda, which makes things easier, at least at the start. So if you feel like using this, you can just download it on. Go ahead with. The installation process is relatively simple, Andi. Then you'll be able to run due to notebooks straight away. So, as I said before, if you if you don't want to install Python on locally or you having problems with both Anaconda and Pip, you can still just use use this. Use this online, which is co lab dot research dot google dot com. Open a new notebook and you'll be able to run your python code. The same is as we're going to be doing in Jupiter notebook. So there are lots of different options. I recommends going going forward, downloading python and using Pip to install Jupiter notebooks and then just opening joke duped notebooks Lately. If you really don't hassle, then literally we need to do is add that you may need to create a Google account, but that shouldn't take too long. So have a go setting up yourself, and then we can get stuck in in the next lecture. 3. Jupyter notebook: now, before we get stuck into actually learning or the contents of bison, let's just go over some basics on how we can use due to notebooks to be super helpful and efficient. While we're doing python now, just remind you, if you haven't got python downloaded, downloaded locally, were having any issues you can always use. This website, which is basically Google's, created something very similar where you can. You can do exactly what we're doing but online so you don't need to download anything. You go to co lab research dot google dot com. You might have to sell a Google account to use it. I'm not sure. And then you basically just select to create a new Python. Three notebook will be using Python three the whole time and not Python T. And that's very important. So let's just go over how to start up to its notebooks. I'll just close it here, and I just close my command problems. So if you're on a Mac, you get your terminal, and if you are on a PC, you go to your command line. So to get to the terminal on the Mac, you just go to launch pad and type in terminal, and you should be able to find it there otherwise, if you're on PC Search of the bottom CMD for command prompt Once that's opened, you religious right in Jupiter Spelled G jay you p Y t r. The notebook is another word. You just wait for a few seconds. Well, that's leading up. And then you should see a page like this, which is the home page that shows you of your folders along here. But then, if you go to the rights where my cursor is, just click on new and then notebook from three. And now you've got dupes notebook running nicely, and you can do it just to make sure it's working. You do things like that one plus nine Enquist. End that your input on that? Your output. These are all called cells, so you have lots different cells. You can run different. It's a code in different cells. If you want on dso a few shortcuts to do. If if you're in a cell like this and you can see this sort of popping up, you press ESC to exit it and then you can go up or down to go between them if you press d twice So you double tap d that deletes the cell. If you want to see all the potential things you can do, as long as you're not selected in one of these cells, you select out of it press H, and you can see all the difference shortcuts that you have, but really the most the most useful things near the very basic ones, which are essentially just that. Like I say, if the whole seven selected like this and this is Billy, you double tap d. It deletes the cell. And if you want to, if you want to run a cell, you can just do shift, enter. So you have all these buttons at the top, which are very helpful, so you could just press run, and that will run it for you. So if I just change the number, for example, run. But if you're selected in here anyway, you can just do shift, enter and it will run the cell for you. If you just press enter, it gives you another line, and you can do whatever you like, and it outputs. This is basically input code and This is the output here. If you want to add new cells, you can always just press the plus button here, and that's basically it. So now that we've gone over the basic use of duty notebook, now let's get stuck into Python. 4. Print and input: Okay, so let's get started with some python. One good the first time actually is going to give this a title. So here it says, untitled. I'm skeptical this print and input click rename. Then whenever I do control save you can say it just says last checkpoint a few seconds ago . Auto saved on. That basically means that saving my script and it's saving it is a i p Y N b file. You can also say it say it is a dark P Y file, which is the extension for python. But I'm gonna make sure that I saved these. So then when I come back to it a later date, I confined which group this is. So we're going to print an input so the 1st 1 will help us get introduced. Teoh basically python syntax. So you just type in print, then in brackets, and then you have either single or double quotations. I tend to often great for singles. Hi there. Why do you shift Enter to run the cell that's going out but for want to write another line ? My name is John Great. And so if you're running, it's through the command line for example, then that's just going to appear down here. So next we enter the cat is input, so impairs Quite cool. This is basically where you get the use input now. So I'm going to say how ah, Yew the news in blank space when I run, this is basically gonna offer a little box. Which is that what I can write stuff in? Quite thanks. In the output is just fine, Thanks. What's awesome is you might remember from my JavaScript about variables so we can actually save this in a variable. So I'm gonna call my variable. For example, we're call it question. You can call variables whatever you'd like so long as the first character is a letter. Don't use numbers or special characters and you can't use white space as well. Say we couldn't write Question one with the white space would have toe have an underscore or something like that. So I'll just call this question one than in how you. Okay, so now that's been that's been stored. This variable has been stored. So now I could do print and print the variable on what's cool here is because this variable name has been stored. If I press tab on my keyboard, it's going to also complete it. Okay, so there's the 1st 2 things. You can print things out, and then you can actually get input from the user by using this imports than brackets, then quotation marks. So three things we went over is how you can print text. You can also print numbers that's already went over. If we go for variable here, we can also print out variables, so that works as well. The second thing we went over was input on. This is how we can basically get input from the user themselves. Weaken store that invariable. So we run this. We can put what everyone end, but that's a use input. Now that's gonna be very useful when we're playing around with the FL statements and other things that have been over in JavaScript before. So three things have been over is print inputs and then using variables to print out the input. So have a play with those three things yourself, and then we'll go on in the next lecture 5. Practical - Welcome message: So let's jump right into our first practical. Now they have lent it a little bit about Python. So in this practical, I want you to provide a welcome message. I wanted to ask something about the user, for example. I don't know how you today or what your age or something like that. And then I want you to respond with another welcome message that includes their answer. So if they say I'm fine, thanks, you can say you've responded. I'm fine, thanks. That's good. Or something like that. And so just to show you one thing that might help it is when you do prints, you can. You can print different things. You could say hi there and then if you wanted to add something on. So let's say, for example, we had a variable cooled message. Uh, John, that's a so I've got a variable cord Message equals John. This is a string we wanted Teoh print both these both of these together we could have high there plus message, and that would work. So there's a hint for you so you can combine things in one print. Say, have a go yourself Onda. Then we'll go over the solution in the next picture 6. Solutions: Okay. Hopefully you had a good guy at creating a welcome message. That's ask them something and then provide a response. So let's go through these one time. So first I'm going provide a welcome message. I'm going to use print, then brackets and then inside the quotation marks where the friend. Fantastic. So you just remember, if you don't have quotation marks, is going to throw it. Our they get scarce. A syntax error, invalid syntax. So you may need to make sure that you have these quotation marks. No, we are something about them. So I'm going to do for asking in the previous actually did input. So we'll try some of that. I was gonna say, How are you today on then? I'm gonna add a space at the end because if I don't have the space, it just looks a bit weird Words. If you had a space, just looks slightly better formatted in my opinion. Anyway, uh, if you get things is storing like this when the Asterix is just still there, you just press the stop and then it should work in a second. If it doesn't work after that, what you get what you do is go to Colonel, and then you just do you reset restart, and that restarts holding. And then it should work just fine. Okay, Fantastic. So it's just ah scared of that because that's untidy. That's better. Okay, so we've got no going input. Fine things. Great. So that outputs what? I've what I've entered. But I actually want to be able to respond with what? What's been written. So in order to do that, I'm going toe What? You what? We could do something a bit convoluted, evokes a prince you for, plus input. But that's that's a little bit messy. And you know, who knows? It will actually work. Well, you want to do is store this in a variable? So I'm going to put user underscore Response equals and you may have noticed from in javascript we're often using camera case like this. If there were two words, I just have the second the first letter of the second word as a capital. Now in Piketon, a lot of people just use underscores to separate them, which I quite like doing in python as well. So I'm going to be using that kind of syntax for python, but is not required. You could do either or it's just better practice from what I've seen. So no gun store this in a variable. So we're gonna do this again. Good. Okay, So that that storing it. And now So we've done this dick. Well, given the welcome message with Arsene by them now, we want to respond with a welcome message that includes the answer to why I've asked. So I'm going Teoh, print off your response. Waas then coat on in a space so looks nicely spaced. And then you might remember the hint I gave you can Then add on this user response, and I'm going to add another string after that, saying with a full stop, because I'm going to imagine the user doesn't actually put in a four stops and go put full stop space. Um, thanks for letting me know. This is a very friendly program, as you can see. So either friend, how you today I am feeling good. Your response was I'm feeling it. Thanks for letting me know. So we can then take off this response. This Ah, to do item as well. Fantastic. So what have we done in this practical one over print. There have gone over input Now weaken. Store that in a variable on. Then how we can combine a variable and a string in our print to give a nice response message that includes what the user put in. So hopefully that makes sense to you. If no, go back over it and have another day and then I'll see you in the next lecture. 7. Common data types: let's now go over the common data types in Python. If you've already done the part of this course for JavaScript, then this will pretty much just be revision for you, The main different types. We have our numbers strings, which would be too much just text. We have lists which in JavaScript we called a race. So just to be difficult, they decided to call it something different. And then also we have dictionaries which were according objects in javascript. And then also we have what we haven't come across before, which are two pools and sets which will graver so start off with numbers known. Going toe adds a new cell below this and the way I do that is because I've selected outside of the cell press B. We have a new cell, I press enter to get started. Let's start with numbers. So with numbers, we have two different types of numbers. I'm going toe create comment here, which I'll be going over in her future lecture. That basically means that I could run this code and this will be ignored. So, yeah, we're going to focus on two of the main types of numbers. You get introduced and floats so in Tages pretty much just hold others. So a number you can think of, say, 908 that's an integer afloat is any with decimal points. That's a 1.453798 or whatever, or even if it's just one point for. That's the difference. We have interviews and we have floats. So let's let's have a few examples here and let's store them in variables. So let's go for injured your one within school. Let's say it's 108 then the float. Let's reflect on school one, because 9.6 now what we can do this quite corn python is if we put right type in types A, T, y, P E. And then in brackets we put in one of these variables would actually tell us what type is so saying the one is an interview. And just to show you that this isn't being using the name of the variable or any thought that let's change, it's number one. You'll see it still comes out who need to run this cell first. Ever get still comes out the same Netscape of floats underscore one. So there comes out this plate. Fantastic. So we'll keep this here and we can keep trying out. So the next thing I'm going to show you our strings. So strings is basically just text. So whether you use double double quotations or single, it doesn't really matter some good, right? Hi, my name is John. So that still this is variable. You recall that? My text. So now if we look at the type, you can see that's now stroke, which times for string. So so far, we've gone over the two numbers, which are integers and floats, introduced a whole numbers floats of 12 decimal points and then we've gone over strings, which is just text the next it'll go over our lists. So in JavaScript, because these race So I'm gonna call this one a list. So what we do here is we use the square brackets and then we can put in a Zeman e different elements as we like, so you can put in numbers you can put in where strings. You bet in floats. You can even put in another list two, but list within a list. So, essentially the important thing about lists is that its stores data and it stores it in order. So this is always gonna be the first object in the list. This is good with 2nd 1/3 4th and fifth. That's very important for lists. They're kept in a specific order. So if we look at the type of a on score list, you'll see that says is the list, so is happy to have left with the nests. We could even have lists with lists within lists, which is fun, and we'll get on to list a lot more detail in the future. But we need to know for now is that its stores data of different different types, and that's what you really need tonight. So next stop that we have dictionaries. So dictionaries, what do you my underscored addict and we use curly brackets. And for this we have key value pairs, so we could actually have, for example, like the names and heights of people say, Let's K four meters the team. It is that we separated with comments Bill. The bill is very tall then, Hank. So those are the numbers, strings list and dictionaries. I'm gonna leave to pours and sets for another time because I want us just focus on some of the main ones we're gonna be using. Two poles and sets are still used, but definitely not as much of use the other ones. So in this lecture, so far we've gone over the different types of numbers. So the main types of inter Gisin floats. Then we went over strings, which is text then lists which store data. Whether it's numbers, strings, lists, anything like that. It stores them and it stores them in a specific order. So you have a first object in the second, and so on. Then finally, we looked at dictionaries and in dictionary CF key value pairs. We can refer Teoh um, John, for example, and I'll give you the height that combined with it. It's the final thing that I want to go, go go into is that you consumptive times add things together and you can also convert things. So, for example, we can add numbers together, right? That's not that's not nothing special Say we could add a into gear to a flight when we get the results. What if we had two strings I'm gonna call A and be. We have two strings. No, what we can't you do, because we could print off a plus B so we can add the two strings together. One thing that we can't do is due print off a string and a number. We're gonna get era Comcast in eights you can think of as being the same thing as at. So it's basically saying you can't and together a string car cat a string, not interview. You can only sorry you can only concoct innate strings, not inches, so you can add numbers together. You can have strings together. You can't add a number to a string. There's one thing that weaken Dio on that's We can actually convert numbers into strings. Well, we have to do for that. This puts string str and then the number, So So let's just make sure that works. Let's say, um, my number equals seven. My string equals Hi. So if we try and add these together the thing, you know what's gonna happen? Going to give us some kind of error saying we know it's not. Add those two together, basically. But if we were to convert this number into a string by doing str than having my brackets. Then it's able to add that add seven to hide because it thinks it's actually a string just saying seven, So that's fine. So that's what we're gonna go over in this picture. We're going over numbers, and so why, why I suggest you do it is right each one of these out yourself. So what numbers? They could be images or floats. We have strings, We have lists, we have dictionaries and we'll go over the other ones. In another time, we can add together numbers. We can also add together strings, but we can't add numbers to strengths unless where were to convert a number to a string or a string? For example, let's say he writes, 89 is a string. Then we convert that to a number by doing integer. What we need to run this well, first of yet. So we've gone over quite a lot today. Eso Why I recommend you do is definitely have a player that try and create your own numbers . So interesting floats urine strings. If you're feeling courageous, you could go into doing lists and dictionaries as well, and you can always check what type they are by putting type, and then you can put the variable or actual value in the brackets and then try to see if you can add together some strings into numbers. You will undoubtedly come across a few errors as you go along, but just keep going and I'll see you in the next lecture. 8. .format: now, before we get stuck into our next practical, I just wanted to show you one really useful method that Python provides called dot format, which just makes it easier to output messages. So it's very simple. Let's say, for example, I'm going to create a few variables that are strengths. So let's say name he Who's John? Eight equals 30. Andi, Let's say Khobe, he who was hiking Now let's say I want to create a string. So I want to print out Jones so OK, so you won't have the name first. So that's but name, and I have to do plus, and then we want to add in a string saying is and they want to add on the age they want ad on etcetera. It kind of gets a bit tiring having toe at putting all these ads. Fortunately, there's a really useful thing in Python, which is called dot format, which is wearing just one string we can. Whether you do double or single, it doesn't matter where essentially, we just add these in by using these curly braces, so I'll show you what I mean in a second. So I've put in some carry braces and I'm going toe put the name in here and then is and I got put in the next one, which is going 30 on his main hobby is and then I put the third curly brackets is basically putting in a place holder, saying We're going to fill this up afterwards. So the end of the quotes after them we put dot formats and then we can literally just put in name, age hobby because we want them to appear in that order, and then it just outputs it really nicely. We'll see if I would change the order. So let's say we had aged first, then it would be incorrect. Well, I mean, it is still work, but it out put it in 30 years, John. So essentially well, you want to keep in mind is that this is put in in order so the 1st 1 is going toe coincide with the 1st 1 after the dot formats. So this isn't essential in python, but it's so, so helpful, and it's gonna be useful in up coming up, going practical. So definitely have a goat this so essentially just write out a string. But what you want this person curly brackets in it and then use dot format after the speech quotation marks on. Then in the brackets you add in what hurt him with. So I haven't got that. And then I'll see you in the next picture. 9. Operators and comparisons: Now that we've gone over the different dates types, for example, of strings and numbers, there's the king to operators. So the basic operators you can think of as plus or minus these kind things. Let's go over the real basics fest just we get so we get a bit of confidence with it. So we use the plus toe adds two numbers to get them. So, for example, 19 blustery. We can also use the plus to add two strings together. Hi plus Charlie. So the other things we can use a minus, that's we want us obstruct. Subtract one number from another so you want to take 98 from hundreds so hungry from 98 that's minus two. We can divide numbers, so 10 divide by five is giving, too. And when we do divisions is re important notice in Brighton that their floats so that float numbers, which means they're decimals. Whereas if we are to convert it to an interview like I was showing you in the previous lecture, that will be an insurgent. Another thing we could do is most apply. So, for example, 10 most played by nine, which issues the gastric sign for multiplication, then weaken due to the power off. So that would be 10 times 10 9 times. So if we make it bit simpler, this would be 100. We hear three would be 10 times 10 times 10 which the 1000 etcetera, the final one we're going to use. And just because this is quite useful when we're doing machine learning is, let's say, for example, 10 and this is called module ISS. That's what we call it use. The presented sign is called Module ISS, and it's basically when you divide 10 by three. What's the remainder that you get? It should be one, because you guys 369 you have one left over. So those are the main operators to consider. They should be relatively straightforward, apart from the modules of the end. Maybe, but, you know, temper. Six. It's all kind of basic stuff. The next thing, we're going to look at our comparisons so we could say things like 10 is more than 20 on that will return some either true or false. Andre. The proper name for is called a booty in, but you just need to think of it is true or false. So this will return false because 10 isn't bigger than 20 but 10 is bigger than five. So we run this. That will be treated so more than sign is the right carrots like this and the less than sign So tennis lesson five That would tend posts 10 assists and 500 were 10 tree. We can also have less than or equal to, So who's to 10 is less than or equal to 11? That'll be tree if we were to do 10 is less than or equal to nine. That's close by fridge. Do you tend his lesson records? 10 over 10 tree And you could do the same thing for more than a swell. So you do more than or equal to next up. We just have equal to. So you think maybe that you do 10 equals 10. But if you do that well, when we have one equal sign, that's like you're trying to assign a variable. So try and say the variable 10 is equal to 10 and that doesn't make sense. So when you're doing comparisons, you have to use to equal signs, say, 10 is equal to 10. We change this to nine. Of course they'll be false because 10 isn't equal to nine. But we can do not equal to. And for that we do an exclamation mark on an equal sign. So 10 is no equal to nine that will return tree. Finally, we have two main logical operators and and or this is where we want to say something like this. 10 is less than 20 and 30 is less than 50. Well, both those are true. 10 is less in 20 and 30 is less than 50. We should change that to less than so that will return tree. But either one of these is false. Then it's not gonna work. Some change this 20. So 10 years listen 20 but 30 is not less than 20. So that's going to return force. And then finally, if we change this toe or because one of these is true and the other one isn't this current entry. So if you that will make sense. So the main operators and just things that we use for basic maths plus minus divide, multiply power off and in the module ISS, which may be the more confusing one that basically says how Maney remains is do we have, after dividing this number by another number? Then we had the comparisons, which is exactly the same as JavaScript Mawr than less than more than equal T equal to not equality. And then, finally, we can combine these with ands and ors. And, as you may remember from the job script module, when we start talking about if else statements and other stuff, you realize the ant or very, very sweet things to have say in JavaScript and was two and signs or was the two pipes signs? But this but for five minutes makes it easier. You just right. And so that's the operators in comparisons, hope that's been useful, and I'll see you in the next lecture. 10. Practical - Basic calculator: So for this practical, I want you to start using the skills that have land with using different days types with using operators and finally using the dot format, which I showed you recently. So for this practical, I want to be able to do the following things with Kate, asked the use of their name. Have the user provide three numbers and then from those three numbers, add the 1st 2 numbers together, then multiply The wrote the result with the third number yes, and then finally output. This result in a long sentence that includes the user's name, the numbers they chose on the following result. So have I got that, and they will go over the solution in the next lecture. 11. Solutions: Okay, so hopefully you've had a good guy. That yourself. Now let's go on the solution together. So the first thing I want to do is ask the use of their name so we can use input for that. Were store that in a variable cord user underscore name. So I'm just gonna write input. Then in the empty brackets, we're going to put some speech to saying hi. What's so? I just tried to use a single there, and obviously that's gonna mess things up because we're using the single ones for that. So many things. Easy engages double quotes here and put high from what you can see now that that's no great effect it. So what's your name? Question? Mark Space. See how that works, John? Okay, that seems to be working Well, so we give us that was a big tick that it's now you have the user provide three numbers. So let's go for Ah, really. Print first we and say please enter three numbers and then I'm going to store three very bulls, one for each number. So I'm Jacqui. This number one equals inputs. Please choose your first number, and then I'm just gonna do that? Three times groups on different lines. Case that change that obviously to second and third number. Okay, so I wish. Name John, please. She's your first number, please. In three numbers. Policia, fast number 45 3 And then one. Okay. Fantastic sex. Like it's working. So now we have the user providing three numbers. The next we want to do is add the 1st 2 numbers together, Then multiply the result with the third number. Okay, So what we want to do, I'm going. I want to store this in another variable. Gonna call. That result is equal t numb on school one plus none on score too. And now we want to multiply this by numb three. However, we need to keep in mind how how the operator precedence is going toe at work. So what I mean by that is what's gonna happen first. Are we going to have this multiply first and then add that onto it? So the best way to be safe? What is actually going to happen if I run this show? You Oh, So first of all, actually, because whenever we taken input and you met when you tried to do this. You may have come across this problem. So when it takes something in his input always takes as a spring there's a string so mean to convert all of these two images. So we could either do it here, but each of them like this. What actually rather do is just in case we need just do more to it. I'm actually going to ing invalid this whole thing. And so we'll do that for each of them. Some, basically, I know that the variable when it takes the input is gonna be a string. I want to convert it immediately into an inter jet. But John 23 it's no. If we do it now and we print off the result, you see, we've got seven. Which would be which would mean that what's happened is first of all, is dumb to has most bye bye numb three to give six and then it's added plus one. And we don't that happen. We want needs to be added together first. So what we can do is we can use brackets here to separate it so we can say, First of all, we want to do numb one plus numb to on because the brackets, they're saying do that first and then we mortified by month none three. So now I've got NY, which would be one plus do which is three then three times three, which is nine. So now we have the result so we can take that off And finally we want out Put the result in a long sentence that includes the user's name, the numbers they chose in the following result. So it looks to me like this is the perfect time to use dot format some entity print, then in my quite something say hello. And then this is where I would want to add the name. If we stop, you chose, and then I'm gonna have three of these was get separated by commas because it would look nice. Has your numbers full stop, then curly brackets is your result. So it created a lot of place orders have created five in all. I'm really dot format. I'm gonna fill those in order. So the 1st 1 is going to be the user name. So use on school name and then number one numb to you. No. Um three And finally, we're gonna have result, Sir. Alex. Hello, John. Neutrals. 123 As your numbers might change that final into an and nine is your result. Okay, Fantastic. So it looks like it's working. This is go over one more time just to make sure. Bill, let's go from 9,101,007 on this. Hello, Bill. You chose 91 on 1007 as your numbers 9 91,600 ayes have news Will result. Fantastic. So that's why we can take this off as well. And that is the end of this practical hopeful that made sense to you would be able to work through a different a different range of concepts that going over so far. So the first thing we did was restoring things in very in variables and using the imports on one really important thing we did is when using input, we had to convert it into an integer because they get stored the strings so I'd but int with a bracket on either side to make sure that which converting that string into an integer. Now if the user have tried to enter a number, yeah, that wouldn't be able to be converted into history. And that's our John again would have an error because it's trying to convert the word John into a number, which doesn't make sense. So I kind of have to be numbers. We can deal with that another time. You're handling exceptions when users might do the wrong thing, but for now we're going to worry about that. And then we went on to something called Operator Presidents who were able to see that we need to put brackets around some of these things. So we want this calculation happen first. So we put in brackets on Ben. We multiply it by three outside the brackets afterwards. Finally, we used dot format when we used the's Kurdi bracket place orders in order to put creator sentence. That look nice on was much easier to do than having to just use plus signs all the time. So I hope that makes sense and see you in the next lecture 12. Comments: in this short lecture. I just wanted to go over the basics of creating commenting your code so you can use them like I've been. So you know, the great comments that are singular line? This is a one line comment. Well, you could get Morcha line where you do three of these on either side and then in between them, you can say, Are there those the apostrophes? So you know, not the double quotes, just the single ones, and you can This is a mole Teoh line comment, and I could go on for as long as you want. So what's really helpful with comments is that you can use it, for example, at the top of your cage to do to do this, like idea, or to explain how the program runs on DSO. You'd usually be wanting to do it. Either. Remind yourself of how something works water if you're working in a team to let one of the other coders know what a set and function does or what a certain line of code does and stuff like that. So essentially you want to use comments just to help either yourself term. Remember what something's used for or to help to communicate someone else what it's used for. Of course, when you're creating variable names and function names and stuff like that, you should be trying to make them Aziz self explanatory as possible. But sometimes you didn't have comments as well, so just let you know if they used the hash to do a one line comment. So if I were to run under the next line, wouldn't be a comment or you can write in between three of the apostrophes su acquits and you could rise May lines as you want. This is another line you can just keep going. So those comments no short lecture, have a go at creating there's yourself and I'll see you in the next lecture. 13. Practical - Weight converter: So in this practical, I'd like you to create a program using python that allows the user to input of weight in it , takes it from pounds and outputs it in the weight in kilograms. So you might need to use a few contacts they have picked up already. Maybe do a bit of research on what the conversion is between pounds and kilograms. So have a go at that yourself, and then we'll go over the solutions in the next lecture. 14. Solutions: Okay, so if you've had a good good that yourself now let's go over the way that I would solve this problem. So first I'm going to do is convert the requirement into more technical requirements. So let's see. I want Teoh creates a variable that's is through the multiplier. £4 two kilograms. So basically, if someone puts in the number of pounds, what do we need to times that by toe turn that into kilograms so needs to researched me to research that and then we want to create inputs, Um, for user sounds, then we want to have an output for use up. Should be kilograms. Okay, so let's give this a try. The first thing Wednesday is create this variable to convert pounds two kilograms, so you have to be of research for that. So that's have electorate say pounds Teoh kilograms. Perfect. Okay, so £1 is 0.45 above. So we need to do is times the number of pounds by this number to get the correct number of kilograms. So it was ever get that I'm going to call this the conversion more to follow. Okay, Great. And then that set about getting the user toe inputs, how many pounds they want to be converted. So let's still this in a variable cord use Ah, pounds. So use a pound equals inputs. You know how into the amount of pounds to convert Teoh kilograms. Now, one thing to keep in mind is as we've gone over before with in Burt's, that's going to be stored as a string. So we need to convert it or cast it into an integer. So which is int with brackets? Either site. So remember when we're changing something from a string toe interject for to a number in general, we call it casting. So we're casting the string here into an inter. So that's why over the user, right in. So now we have a number, we have the number of pounds. So then we can actually work out converted kilograms That's basically equal to the use of pounds most played by the conversion Most play Okay, No, we can print out something like, um, you entered and that's good for So that was the user pounds, which is the plus signs here to add them together. You entered this number pounds, um, the this converts to variant hurt. The converse is Kiggens. So let's see if we take this officer was crazy available for most Liar Dick. We've taken the impacts for users to get the pounds and then we're out putting for the user of this converted kilograms so great, we just were able to take off everything on our list. Now let's give it a try. So it's just make sure was working. I'm gonna put in the number one so expecting to get the kilograms out a 0.453 buffalo. Okay, Interesting, since saying Can't concussion it. So what's happening here so essentially is not happy that we're trying to add in these as images. So we want to cast these two strings basically tennis in ST version. So just like we were costing the interest before, were now costing these strings. So let's see if that number's right. So put in and let's face it, nicer. So you have put in one and it's outfitted this and I also want to put that pounds there. You thought it a bit better. So So, yeah, let's try a few other numbers just to make sure it's working. Let's go over 10 kilograms. So this should come out Is 4.5 fantastic e a great escape form at some random number now. Fantastic. So we now have a working converter that converts are kilograms. So our pounds into kilograms. So we we researched online. How many pounds are two kilogram? And we use that as our multiply. We store that in the variable conversion multiply. We could have called this wherever we wanted. We could have just called this. Ah, for example, we could have just called this in the e. R. Whatever. Doesn't matter too much on then. What we did is we took the inputs from the user to give us the number of pounds we casted that into inter job. And then we had to do was storing a variable are kilograms by multiplying the number of pounds by the conversion multiplier and they may output in this industry. So if that makes sense to you on hope for you gave a good go yourself. If you find that this is different, your solution maybe have another Go try it out this way. See if you've had a few days and then when you're ready, I'll see in the next sector 15. Importing modules and using methods: welcome to this lecture on important modules and using methods. Before we get onto importing modules, I just want to go over what exactly a method is. So Python already has built in methods, some of which, some of which have already been over. So for example, when we want Teoh convert say, an integer to a string, we use this method we just str and then with brackets around it, we can also use some things. For example, we used type if you remember in one of previous lectures so we could just be a lecture here . No, that would return the type off the data. So there are other things we could do. For example, we've got you can use length, which is this is a word we'll return how many characters is in this string. So we have all these built in methods, So methods is just a way of doing of carrying out something now modules and sometimes we might refer to as libraries are basically out a big collection of methods that are useful for a specific purpose. Sometimes you have to install them, but actually we've already got a lot that we can just use by importing. So for this I'm going toe to, for example, I'm going to use the random module. It's all I have to do is write import random. So now we have all of the methods available to us from London. So it's like a one now if I type in random and then put a dot and then if using due to notebooks, you can press tab. If you're not, you won't have this available. But when I press tab so it comes up with a with the difference methods I could use. So these are all the different methods that are available in the London module. Say, if I just search that now I can just put in by thin random module. Here we go. We get the python documentation. They're all systems in good articles here, for example, by thing for beginners telling Yankees runner module. But his or his or the documentation, the python documentation it could be a bit tough toe. Get through ballistic at one. Okay, here's a good one, random dot rand it rand int. Sorry says that stands for random interject returns a random integer Such that is between, basically, is between and be this kid I want to try. Let's see if we can find it here, Rand Fantastic. And we can just type out as well. So random dot run into and then brackets we wanted to be. Let's between 2010 and 20. Say if I do, that's good. 12. Try again. 18 is always going to be completely random. What number is going to be. So we always have to have to position to, uh, says here, missing one quiet positional argument. So we need to have two arguments within the method. That's a want It's being between 10 and 200,000 or two million, so you get the idea. So basically, Brendan has a number of different methods within its module, so we can import random. If we wanted to just import what's. The next thing I want to do is import, as so sometimes it's easier or you prefer to cool to refer to its on the other random, so import random as our end, for example, Then all I'm doing is saying, import the module random, but I want to call it RN. So now if I try to do this, for example, as our n. That would still work because I've basically important it and I want to call it, are in So we can import Brandon just as it is or weaken rename it if we want. There's one other thing we can do. It saves on memory, we can say from random and then we just want to import just one method. Say import Rand int. It's another we've done that. We don't need to put in random dots. Run, Didn't we can just put round it. So there are loads and loads of useful python modules on the way that most people would just discover them is by typing in by thin, useful modules or, yeah, something like interesting modules. And it comes up with things. That s 20 python libraries you can't live without. So why I highly recommend you do after this lecture? It's just take a look at all the different modules that there are out there. So, for example, this article 40 quite helpful. It tells you about things like requests. Um, it's that search for using hasty TP which will get onto in a little while. Beautiful soup number I, which is used for form or solve mathematical things which you can't just get from the basic stuff you get in Python Matt, put blocked lib you can use for plotting graphs and stuff like that. But for now, we're just going to stick toe random just to give it a try. So definitely, definitely. Definitely after this lecture, have a try at importing random. So all you need to do to try and it just right imports Brendan and then type random dot rand int, which stands for random in nature, and then say between one and 10 or whatever you want. And then you will be able to get your under numbers. And you could also stand that store that in a variable, something good court run ball my prince offer involved. If I bring this up again, it will be the same number. I have to run this self again for its change. Of course. Fantastic. So in the sexual, we've been over what methods are so they're basically useful functions that we can have. We've been over a few that already built into python, and then we looked at how we can import a module in different ways. So we looked at how we could just import it just by writing import random. We looked how we could import just import just one method from the module. And then we looked at how we could import it and rename it at the same time by doing imports. Brendan as new name. What's the matter? So that's that's importing modules. In a nutshell. Once you get really good a python, you actually be able to create your own modules. You can then import and actually will be doing that when we get more into a machine learning and integrating that into our websites. So but for now, it just looks on importing the random module. Play around with a bit. I want you being confident I'll see in the next lecture. 16. Investigating errors: So I just wanted to go over very quickly investigating areas because as you go on as a python developer, you'll realize that being able to look into errors and find out what's actually going on is incredibly important because, especially when you're starting a new project using new modules that you never used before , it may be confusing when you get areas that don't make sense. So just want to go over some ways that we can kind of help ourselves out here. Let's say, for example, we want to import random Okay, says an era. Now the first thing to do is look at where it's pointed to, so it's pointing to this line of coat. So let's say, if I were to then underneath say, You know, a equals this because that one plus T whatever it's gonna reporting, pointing to where the error is, which is on this line. It says module not found error, no module named random. Oh, so we look at this, we think OK, well, that's kind of obvious What the problem, ms. I've missed up about random Fantastic. So what happens if we come across know that we haven't seen before? Let's try doing a letter this writing like G plus eight, for example, it says name era name G is not find. Let's just say for arguments sake, we're not sure what's going on here. We haven't come across this area before. What you can do, and it's very helpful, is if you search for this online. She lets just copy the name of the era, what's what saying. And then we have a look and usually stack overflows could be the first thing that appears. So Stack Overflows a fantastic website for looking at similar errors that people have come across. So this is where someone's written what their problem is. We don't in this context need to see exactly what's going on. We have a look, some of the answers. So so it seems. What people are saying is that all the characters in coal went didn't have quotation, Drummond says. Have a look at the top what he's done. So we look at this cold and he's but a B C D. E f G without quotations. It seems like he got a similar error to us, and people are saying, Yeah, the problem is here supposed to contain strings. So let's go back to ours and see if that helps to. Okay, we realized that this hasn't isn't a string. It's enough. We try this time. Okay, now, of course. New era, Let's say for argument sake, we don't know what's going on here either. Let's have a look. Okay, so we go another stack overflow, Come up. We can have a look at it. This is the question that someone's someone saying I'm new, educating on there, saying that they get their getting this error so the air is off the top. Getting type cannot castmates string. So here he was saying, buy things working the different JavaScript, the value, your concussed unease and needs to be both into your or string. So, for example, the cable Oh, for an error. So it's saying, Okay, you could you could resemble change that industry so we could go back to here, make it, for example, changed this number to a string, and that's working fine now. So what I'm trying to get across is, first of all, if error comes up, you want to look on what line? Because it's kind of intimidating. The first time you see these things, you don't know where to look because there's lots of things things going on at the top here . It gives you the name of the era. Sometimes that that's useful, useful indicated to what's going on. Module not found error says obvious that the module you're trying to import there some problem there. It also points toe the line so you can see the exact code you have written. And in the bottom, you gives you a bit more detail. No module named random. Oh, if a this point, you're still not sure what's going on or hey can fix it. That's when you can just copy this problem wherever there says and just google it and you can look at the stack Overflow options. You have a Zinkhan See 1st 5 The 1st 5 They always take over flee. So you just have a look at that. So as you do, more practical is in bison. You may come have some errors coming up, and you're not sure why it's happening and getting into the habit. The first we're trying to work it out yourself and then googling it on stack overflow. If you can't find a solution, then it's a great habit to get into, because even if you've been coding for 10 years, you're still going to be looking at this guy stuff, believe me. So essentially have ago this yourself when you're going through, the practical is coming up. If you get errors, try and get into this habit of okay, let's read through the air first, see if I can solve it. If not, let's go on toe our search engine and we'll probably find something on Stack overflow. So that's the end of the section on. I'll see you in the next one. 17. Practical - Dice rolling simulator: Okay, we're now going to step it up a bit in these practical we're going to do a dice rolling simulator. And as you can see, even in what I've provided for the scope of the project, it's I'm not going into very much detail. I'm trying to avoid saying anything technical T. So I basically wanted to create a program that simulates two dice being rolled in this program are wanting to give a message that says what the combined value of those dice are. So I'm basically in this practical, leaving it up to you to break that down into smaller, smaller to do list for what you think you need to do to achieve the requirements that I've listed here. So give it a go yourself, and then we'll go over the solution on how we might design solving this. In the next lecture 18. Solutions: So congratulations on giving this a go yourself first. Hopefully, you've had a good go at it and you have at least tried toe design. Solving the problem first on that because that's a very, very important skill. Not only does it help me to become a better programmer, but I believe actually helped you in day to day life. Whenever you're faced with problems that are quite large, it's a good It's a good skill to have to be able to break them down into more bite sized chunks. So hopefully you're not in developing a practical coding skill here. You also developing a life skill. So let's have a look at this I'm going to created to do this below. That's a bit more technical, so we want to create a program that simulates two dies being rolled, So the first thing I want to do is to generates two numbers that are random and between 16 because we don't want to have zero duty, because when you roll, the dice is always gonna be at least one on maximum six. Obviously, you could choose on a 20 sided dice or something like that. If you wanted there's nothing wrong with that. I'm just going for the standard six sided dice. That's the first thing I want to do. And then I want to prince, um, two numbers and the some. So yeah, so I've been able to break it down into two technical points. So let's have a game first thing we do actually court random module. I always like having a smaller bits of my to do this because I love positive feedback leaps . So as soon as I've done one thing. So, for example, I've imported random Yea, that I can take something off my list. Obviously don't go overboard and have, like, 100 different to do things. But it's nice to have a positive feedback, like feed, feedback loop like that. Sometimes I always x messy, so I'm just gonna change that. There have imported random, be so now we want to generate two numbers, some skating numb on school. One equals number. Skis, random dots rand into. You may remember nothing I could do. Instead of doing this. I kids as went over the last lecture, I could say from random imports round it. Brandon stands for random integer and then if I imported there. I don't need to use the random dot here. You can do either way. It doesn't matter. They both worked just fine. So numb one already running been shipped with 16 So now let's print off number one. Make sure that's working just fine, okay? And I'm gonna run the white cells again. E s. It was just complete chance that ended up being to most of the times. Well, seemed to be just two and five. Okay, Okay, so it always get you with with running numbers. So we've got number one here. Let's go for number two, and we want to do the exact same thing. Don't mean there. We want to get another round of venture between 1 to 6. Nope. Doing a cut, its paste based. Okay. And then go for none, too, as well. Okay. Said both free number five and T. That's loving five and two today in that. Okay, Perfect. So I think we can enter this off stick and finally want to print the two numbers in their son. So I'm going to store the some in another variable called some. Except if it goes green like that that means it's already a special wet so in python, so you probably want to avoid it. So I'm going to dio numb for di stato, dice unscarred Taito. And then it's just number one plus numb to So now let's bring all this off. I'm going to use the handy dot format to make it very easy. Something to say, Um thise simulation complete no one. And then what was the I'm going to use curly brackets as a placeholder numb to total is and then third curly brackets. Then Doc formats. Then we're gonna add in. Number one is the 1st 1 I want to appear than two is the 2nd 1 and di stato is the third Will want to hear says Have a look dissemination complete numb one is one numb to his three No, to have another coat on their looks. Nice up 93 the total is for and its trail running One more time number 6 90 total is eight Fantastic! And there we have it. We have our dice dice rolling senior simulator. So just to go over what we've done first of all, we've imported the random module I've decided Teoh to import the method as well at the same time, So from from the random module have imported the rand into method which basically gives gives us back a random interject between one number another. I've generated two numbers here. I've had a random interview here, produced between one and six and then producing another random into Europe between 16 Here The method is being called separately both times. So we're gonna have a different random number, different running numbers. And then I've added them together here and stored in a variable cord ice total. And then I've been able to use doc formats with curly braces as the placeholder toe outputs the result. And that's it. So I hope that makes sense to you, Aunt. Hopefully RC in the next lecture. 19. If else elif: Now let's go on to, if else NFL else if statements you may remember having done this in JavaScript. So hopefully this is just a little bit of revision for you are the syntax is slightly different, but the effects just the same. So we want to say essentially, um, that she was an example off. Let's say we worked at a nuclear power plant and we wanted to make sure the temperature didn't go over well, I have no idea. With temperatures, let's say we don't want to go over 40 40 degrees. Okay, Andi, if it does, then we need Teoh. Make some kind of alert and say it's it's too hot. Otherwise, we just wanted to say everything's fine. So we're creating a program that checks the temperature off. Ah, nuclear practical. Just go right on the top Here. It sounds cool. Say program for checking timpte for Russia off a nuclear power. Cool our station call. Let's give it there isn't too important. So the arms go, creating new sounds neath by pressing beat. So why I want to do Let's say Max temperature must go home. Match temp equals by SS A 40. So now We want to have some way of saying if the temperature is over 40 than me to give some kind of alert house, we just say everything's fine. So let's also create a variable called current temp on which gonna call at 20 small. They say it's really hot, so there's something to be worried about. So you want right? If so, we start firing if and then we put in our condition. So we want. If the current temperature tim there's more than the max temp, So that's our condition. And in JavaScript we had to put these in brackets, but But in Python, we don't need to do that. We just leave it. So we're saying, if the current temp is mawr than the Max Temps, that's our our condition. So it is going to return a Boolean right, so either it's true or false. So if this is true that the current temperature is more than the maxim picture, we put a colon and then we press enter and you can see that the this line has been indented , so it's not starting from back here. It's indented, and that's really important in python. So when you have a colon and you want to execute some code underneath. It needs to be indented so much gonna print Ah, core temperature above safe levels. Everyone panic because this is a really helpful program. So that's what happens if the current temperature is above the maximum chur, but otherwise so basically else. So we're saying, If not so we're saying, If the current temperature isn't more than the max temperature, well, then that's fine. So we can just print and I press enter. You can see it's already indented nicely. Print. Everything is fine. Say when I run this Now you'll see core temperature above safe lowers everyone panic. That's because the current temperature is more than the max temperature. So let's say the current temperature is 20 so everything's fine. They get problem. Taipei. Everything is fine, because now the current temperature isn't more than max temperature, so you go straight to the else so that that's all looking good. But what if we want at another condition to this? We want to say, OK, that's fine, but also, if it gets too cold, then it won't be efficient. So let's say we want to know we want to have something happen if the current temperature is less than a minimum temperature. So let's add another variable called men Attempt 10. So this is where we won't start using else. If so, we don't it to be indented. We just put else. If so, this is our saying. Basically, if this happens, then do this. Or if this happens, then we get another one even. Or if this happens at, add as many as we want. And then finally we finish with the house saying, If none of these happened, the none of the ifs or else has happened, then just do something. For example, print. Everything is fine. So for our second condition, we won't say if the current temps is less than the miniature. The minimum temp. They put a coat on. So you might say we have on the same line by this. So you are condition else. If the current temperature is less in the minute minimum temperature. Indent here to say what what code you want execute? We get prints. Ah, see temperature levels low, please. Like the fire that makes sense, right? Cases saying invalid syntax. So I think what we used to do is have that is one. So I've still got my head in JavaScript. So in Python, it's just elliff, which is nice. So short hand for else. If LF the current temperature is less than the attempt, everything is fine. Okay, so let's make it so that the current temperature is less than 10. But is eight temperature levels low? Please make a fire. Okay, so in this lecture, essentially, we've gone over and hopefully it's just revisiting. If you've done the job script already, how we can use if else and else f or how we call in Python. LF so essentially we right if to start with and then our condition So condition here is that the current temperature is more than the next temp. We could do something like is more than or equal Teoh. We could do equal to or not equity anything like that. We can use any of the operators basically. So we say if the current temperature is more than the max temperature, then print this elliff or else if the current temperature is less than minute temperature. Now we're providing a second condition and then we put a colon and an indentation, which is about four spaces in its automatically done in most text editors. As always, it knows that it's a python file it's working with. Then we say, whatever we were, the code that we want to execute so we could put in more if we wanted, we could add in is much stuff was we want we could even add add in another if statement in there, which is called a nested if statement. So if neither of these conditions have been met and even if we have added Mawr Elliff conditions, no matter how many of them there are, If none of these have been met, then we have one final statement to say where if none of those have met, then just do this. So since you have gone over, if hell if and else so why I recommend that you do is have a go this yourself also, see if you do something interesting like no only having if this is more than this, we could also have Haddon or Born. And so you might remember we went over these before, so we could say if the current temperature is more than the Max temperature or the current temperature is less than the mint temperature. For example, have a think about how you can play around with these things. Teoh Expand on the if, LF and else statements and see or so we'll be really, really helpful for you is to think about a different application. So ignore this one about the nuclear power station. Think about another one that you think could be useful in the real world, and that really does help to improve your your skills in using if and if and else so have I got that and I'll see in the next sector? 20. Practical - Number guessing challenge: in this practical you're going to be applying. We have learned in the most recent lectures. So the requirements for this practical rto create program that chooses number between 100 and then you take a user's guests and tell them if they're correct or not. So basically, let's say your program chooses the number 46 on the user. Guess is that the number is 37. It will say that was an incorrect number or if the user guesses. So if the number is 56 the user guesses 56 tells them if they're correct as a bonus, have it that the programme tells users if their guess was lower or higher than the number that was generated. So have a guy that give your best shot and then we'll go over the solution in the next lecture. 21. Solutions: Okay, so let's take a look at these requirements. First thing we'll do is break them down into more technical requirements. So first of all, let's have generate Rand. So random number. I'm going to quote rand int because I remember that that's the method we use. So first want to import random and generate around since between one of the hundreds. After that, um, use inputs to get use a guess ST Forget Teoh hurt a cast string into on interject. So you may remember that when we use inputs it saved is a string So me to change the string into an interview on the proper word for that is cost. We're gonna cost the string into an insecure. So we go over how we did that previously. Or go over that again now. So we take the users. Guess so. Now we want to use Ah, if else so I'm gonna say if use a guess his rights print success house fail and then I'm gonna have Well, we'll go over the bonus a second. Let's get started with that. So first I'm going to do is import random. Kate Rafter. Good start. First take of the day that we want to generate a random interfere between 100. So let's save that in a variable. So we'll call this computer numb equals Brandon Dots. And then we want to use the method Rand int, you may remember we use that previously. If you ever unsure, you can always go onto a search engine and just say Python random number and then very quickly it will remind you so even if you look, just hear random doctor indent. But all of these articles will probably give you the correct answer, so we want the random don't run into between 100. So that's just print it off just to make sure it's working correctly. So when you just computer numb, capable, more time, make sure it's random yet Perfect. Okay, so generated a random into dick. Next we want to do is get that you get use input to get the users. Guess that's creates. Another very book would use a guess, and we're going to use inputs saying Please in tow number between one on 100. So now we said, Don't forget to cast a string engine insecure, so to cast it well, I need to do is write int and then put brackets around this whole thing. And now that's going to convert it into an incident Greats. And now we can print out both of these. Here's a guess. So we've got the computer number on the left on the user guests on the right. Now what we want to be able to do is using FL statements to say if if they're correct or not. So we could basically just say if the use of guests is equal to you. The computer number if you don't score numb Colonel. So I remember the two equal science means equal to if you try to use one, then it sent you to get in there like this. Well, I get invalid syntax, but also it might think that you're trying to store this is a variable and say that's the value on it would get quite confused. So we're using two equals science to say if the user guess is the same as the computer numb that we can print Congrats, you're guess was correct and everyone to say, Well, if that's not true, so we can say else because the union other condition really, Either it's right or it's wrong. So if it's right, if you use a guess is he would computing, um, then do this otherwise or else prints wrong. So, uh, okay. So we could also the top here just to make things easier for us, we can print out. Um, your guess. Well, here's the format. We can use those placeholders, curly brackets. Your guess. The rial, the actual computer number you stopped. Format. We enter in the user guest first, and then the computer. None that actually better, doesn't it? Your guest with 69. The actual confused numbers. 36. Wrong sucker. Okay, so we can take this off now on. We got this as well. So if user is right, prince success else fail. We've done that. So next we want to go into this bonus thing. Tell the user if he guesses lower high, so we won't say if use. Ah, guess is lower. Tell them lower if wire toe. Well, so now we want to add that into here. We don't want to have to do a whole of the NFL statement, so why don't we instead just use else you the else if or LF so we want to say another condition could be at if, um, use a guess. IHS more than computer numb coat on an indented here, prince. Too high hoops. It's a string too high Now. We want to also had a condition for if the user guests is more than the computer number. Print lo so that when we run this now, it's gonna come up with to high because we're going through this. So it said, if the user guest is equal to compute to numb that we do this else if it's too high, we say two high else was two way have today. So we don't actually need to have this else statement here because, well, it's always going to be is always either going to be equal to more than or less than right . We can leave it there anyway. This is no big deal, so we could have another go for 32. My guess was the attitude. The actual computer number was Center five. Wrong sucker. So that's why because we want this one to be lower, so this is pretty low for one more time. See if I'm lucky. Ah, wrong. way to go away again. But we've managed to get the practical completed at the very least. So just to recap what we've done, first of all, we imported the random or do so that we could use the Method Rand int to generate a number between one and 100. We then also took the users guest using input on. We had to cast it because it was gonna be sort of the string. Otherwise, by default we had to cast it into an incidents. We put that with starts with a bracket of the end as well. And then we wanted to use an if LF an else statement to basically go over the different conditions and execute certain code based on different eventualities. So the first condition was that the user guests is the same as the computer number. And then I would say congratulations. It was correct. Then if that didn't happen, then it then would look for if the user guess is more than the computer numb and say too high, then would look for the condition if the user guests was less in the computer and print too low on then we have this one, which actually is no longer useful. But just say it was wrong soccer. So that's the practical. Hopefully, you've been able to do it yourself. If you struggled the first time round and now you feel a little bit more confident. I recommend going back and trying this again yourself on, then I'll see in the next lecture. 22. Lists: Now let's go back over the common dates type that is lists you may remember We went over this earlier in this module on. We also went over it in JavaScript, in JavaScript. We called this a raise, but it's pretty much the exact same thing. Essentially, a list can hold as much data as you like on different data types within the list. So weaken store numbers like integers and floats weaken store strings. We can even storm other lists. And if we were feeling very crazy, we might even add after cover another list. So we have a list within a list within a list, which is pretty cool. So each of the different pieces of data we had in they need to be separated by a comma that's very important on was also important, analysts is they're kept in order, So this is the first element within the list. This is the 2nd 1/3 elements, then this large one is all know the fourth element. So things to remember about whenever you're creating a list is that you need to give it to variable name and then you choose the square brackets. Then you can put in whatever day do you like? As always, it's separated by commas and within lists. This is going to be kept in order, so this will be the 1st 2nd and third, and we'll be revisiting indexes in a lecture coming up. So you have a good creating a few lists yourself, see what kind of things you can and into It is good practice just to get used. Teoh creating them yourself. Maybe come across a few errors. For example. You might forget toe have a comma, so it's always good to give it a try. Come across these areas and work out how to create list correctly. So have I got that and I'll see you in the next lecture. 23. A few list methods: So now that you're comfortable creating your enlists, I just want to go over a few list methods and a few list functions. So it is quite easy to tell the difference between methods and functions. Let's so we have a list called on my list. Let's just for now, have one for for six, just a few rendered numbers. So methods will. This index will be like like this. My list dot ah upend, for example. That's the top one here, Doctor bent. So a function will be different in syntax. It won't be the list. Then dots in the name of whatever we want to do will actually be at the start. So v I, Lenin and then this in brackets. So function will have this. The word at the start, let's say the name of the function of start and then we'll have the list within the brackets. If it's a method, is going to be the name off the list or the object and then dots whatever we want to do to it, it's that's difference between methods and functions. Now let's just get started in how we connect what these methods do. These are just some of the most common ones. There, of course, are the ones that you can play around with. But these the most common honesty's. So here we're just going to try out the different ones one by one. So here we have a list, which is hasn't numbers in Andi. I'm going fishing a few, a few others, actually. So it's not. We're going up, so it's going up and then down. So that's appends. Um, let's say 1000 serviced. So what happens then? That's print off my list. Now you can see that's what happened. What's happened is that thousands been upended, or you can just say it's been added to the end of the list. Now, if we wanted to remove something from the list I would have to do is use the method. Thought Pop, so popular, literally just met, removes whatever the last element in the list is. So if we do that, you can see that thousands been removed. If I do it again, then the number 22 will be removed and so on and so forth. So that's what popped us. So basically appends add something to the end. Pop, remove something from the end. Whatever it is, don't remove. You can actually specify what you want to remove. So let's say if we rerun this so the list is full again so you can see that. Let's say you want to remove the number 44 we have to do is but my list dot or the name of your list don't removed and then what you want from me. They gay. Let's say we wanted to add a few different letters as well. Let's go a c be So if we wanted to remove there to see, for example, we just have to put seeing here. You can see we have A and B left. Now let's say we wanted to sort this we have to do here is put in sort an empty brackets. Obviously, we can't sort both letters and numbers. Let's just go for numbers and then you can see it's been sorted from lowest to highest. We could also have it with just letters. Some of you create another list called my list. Let us so in this one, I'm just going to put some random letters. Ah, that and be looking. So now we want to sort my list letters. I said No, you will see. We can also sort them with letters are not just going up a b ftz which works so that then we can do reverse, which pretty much just reverses the whole thing. So let's say we've got my list as it is here, going one for 56. Probable. Then if we just do my list, don't reverse and then we print that off. You will see the reverse of the list. So now Number 22 is the first element and this is the second dead and four etcetera. So those are the methods. And so all I can recommend is that you go through them yourself. Obviously, at first you won't be able to remember these when you when you step away from them. So just make a note of these different methods, what they do and have a play with them. So maybe pulls the video now and just try creating list and try applying each one of those methods. Now let's move on to functions Say these are three of the basic but very useful functions. Sometimes we want to know how many elements own arrest. It's not saying my this tear. This has six in there. So I'm going to put then my list just to check that in every time. Six. Fantastic. And if we do my this letters, that should return five because there are five elements within our list. So now we have to use Mina Max so we can say we want to know what the minimum number is in my list, So the minimum one should be just one. And then also, we can use Max to return the maximum one. We can also do this with letters as well, so this should return for Max said. And then men should return a. So those are some of the basic list methods and functions you'll find actually have a lot of uses when you get into using python quite a lot. So I recommend you do is have a goat. These methods and these functions and then when you're feeling ready, will move on to the next part of the lists 24. List indexes: another have gone over the basic lists and a few methods and functions. Let's look at a list indexing where essentially you can access just one specific element within list based on its index. So first of all, let's talk about what in indexes. So you may remember from the JavaScript lectures that we were doing the same thing. But essentially in a list, everything is stored in its position. So this is the first element in the list. This is the second, the third all the way to the last. No indexing starts at zero, so the first element in a list will start with an index of zero. So this number one will have an index of zero the natural having index of 123456 and yeah, so that that's that's it. I've just decided in the top here to create a variable that's a string, just to show you that you can also include variables within a list. Now let's look at how we can actually access an element in a list by using the indexing this in Texas. Very simple. We just put my list than straight after we use the square brackets, just like we used to create lists. And then we put in the number of the index. So let's say we want to say we'll just call this Rabelais for now and then we want to print this office were apparently so There we go. We've got the first number, which is a zero index. Now let's say you wanted to access the word high, have a pause now and think about what number you need to put in here to make that work. So hopefully you got that right. If if this one here, if the 1st 1 is zero next was 123 so high is going to be indexed three. Fantastic. So they finally let's go forward, Say, have a pause again. I think what you need to put in here. So it's gonna be if this 10 index area, then 123456 There's gonna be index off sex. Just try that. Fantastic. That seems to be working well, so that's all there is to indexing, Really? So we have to do is put in the name of your list, Then within square brackets, you just put the index number So in the next sector, we're gonna be going over how you can capture slices of the list. So you including more than just one element. So we'll go that in the next sector. So before then, just have a great this yourself, create yourself a list and then and then practice just accessing different elements in the list. Um, using this indexing, The last thing I want to mention actually, is that you can also index lists within less. So let's say right in the middle here, I've got one more list. I'm going for that as 1234 The syntax is again very simple. So we want to. First of all, access, this is still counts as one element in the list. So we go 01234 five. This is gonna be number five and index, it's going to run this first toe. We update the list every day. So in the fifth and next we get this list. Now, if we want to get one element from this list, all we have to do is then again uses another square bracket afterwards and put in the index . So let's say we wanted to get the number three. Well, that would be indexed 012 within that list. So basically what we're doing here is they're saying in my list access the fifth the five index get. Yet the index's number 5012345 is actually a six element in the list, of course, but its Index five. And they were saying within that elements, that's a list. So we want access the the third element, which is indexed to. So if we run, that will get the number three. If we were to put index theory, then we should get the number one now for a big challenge. I'm going to put that list within a list within a list. So now we have die access a list that's within a list that's within a list se. First of all, to access the main list, we use grab brackets and when you want to go 012345 So we put in the 5th 5 index, then we want to access this one here, which in that list is zero. Then once his index one, it's that should give us that list. We were on this every day and then we want to access. Let's say the number 35 which is one more square bracket we put one on. Now give us 35. So if you know, understanding this completely the list within list, don't worry about it too much we weigh won't be going over that kind of stuff for a while. And we will be revising if you do great. But otherwise just focus on having a basic list like this, where you have some numbers and some strings and just make sure you understand that in order to access one element from the list, we do indexing. So we put the name of the list square bracket and then the index number on the first element always starts at 0.0 here. I would just get the number one. So have a good about yourself, and then I'll see you in the next lecture. 25. List slicing: now that we've gone over the basics of how we can access the single element from a list, let's look at how we can take slices from a list, so I'll show you what I mean. Let's first create a list called a list because I'm batteries on score. A list equals. Let's just go for 16 since Evan there, 5700 um, and cheese. So it's kind of like a very strange shopping list anyway, so let's I should I'm going to just make add some more. Just We have quite a lot of them. So let's go into how lists list indexing works. So we've already been over how we can access a single element from a list. So we just put a list. And then in square brackets, we put the index number set. So you wanted to get bored. Bear that would be indexed number 0123 to train number three. Oops, way. Get the word bath now if we wanted to get a slice. That's why we wanted to take everything from bear to cheese. Then what would use is reduced in the square? Brackets were then used condoms so if I put in, for example, six. So what? This is saying? Let's just run it. Were saying from Index three and then take everything up to That's just before Index six. So Index six is eight. So it's basically saying, Take all the elements between Index three and up to index six, but doesn't actually take index six. So let's go for another example. I want access. Everything from the number six up. Teoh this up to 77 208,000 and 80. So would want to go from index one all the way up to 1234567 So it put number eight here. Okay, so we want to take index. Want eight. So just remind you why So the first number, that's the first number that we take and then we take everything up to, but not including the index number after the coat on. So this should give us everything between six and 700,000. Well, let's see what's happened here. So it hasn't actually gone all of them. So it started the right index. So one, 234 50 I know. I'm going to rerun this list. There we go. That's better. Okay, that's taking everything from the index one up to the index seven. So it goes upto but doesn't include index eight now. One other thing. We can do that. We can actually leave out one of the numbers if we leave out the number. The basic. Basically, Python always understands that the first number before the colon, that's the beginning index. If we leave it blank, it just assumes he wanted to go from the start. So it includes from the start everything up to, but not including the eight index, which is number 11. And so it works the same way the other way. So if I were to put in four and then leave, the second blank would basically take everything from Index four. So 01234 17 from 5700 all the way to the end. So even include the last index there. So that's essentially the basics of less slicing. So using this in tax, so the name of the list then square brackets and then you have a colon that separates the starting index to the end index. Remember, doesn't include that. It's just before that new return. A slice of your list if we weaken, leave either almost blank. And if we leave a blank than by thing will just assume that we're saying go from the starting index. If we need this blank, we're basically saying Go to the ended next. So, essentially, if I were to run, this would just return everything. So that's the basics of this slicing. I'm gonna add one more thing into the mix. Now, not only can you say that you want to go from the START index, and it ended in index, so that's a one a day we want to have index one to up to, but not including in Excite. So just pause the video here and try and work out yourself what that's going to return. So if you had to go that as you can see, we've gone from six or 8 1,708,000 now we can actually one more colon. And this basically tell tells by thin how many how many steps we want to take. So, for example, if I put to that, I'm saying only include the seconds occurrence, so skip out. So if I run, it will make more sense to you. So it's saying essentially, skip out every 2nd 1 So it's going for number six than skipping some seven and then Bear been skipping 5700 then Cheese is giving eight. So if I was put in number three, for example, this is taking six. And then it skipped out. These two nets got 5700 it skipped out these two. So this, say the third number basically says the steps we wanted to take. How many do you want it Teoh to miss out? Let's say you can also use negative numbers here. So let's say, for example, I wanted just to take the whole entire list. So if I left, if I Dakotan left this blank, then obviously we just leave the whole list because, like the same before by Thin just assumes that we want to go from the start and the end index if they're blank. If we had one more colon and we just put the number two, then it's just gonna take every other one, so it's gonna take the 1st 3rd 5th You said Francisco. Skip out every 2nd 1 you could get for every four. Now let's give us one and then skip up these 3 5700 give Are these three to give us 11? What was interesting is we can also put in negative numbers here. So this is just for the last number, and that's basically gonna tell it to you in reverse so we can actually get to reverse the list. We could get a reverse of a slice of the list so we could say, Start your going between. That's a there on and eight. That's could be for 567 about my mistake. We just need to switch these around because we reversing. We won't say. Starting one needs to be the 88th Index, and it goes up to but doesn't include the fourth in it. 012 three So over that makes sense. T The main thing T keep in mind here is this Intacs. So with this slicing, you can access a slice of the list based on the indexes. So the first index you put before the colon is the starting index. If it's blank than by thermal, assume you want to go from the beginning, then the second number is the end index and it doesn't actually include the index. So it says go up to everything before because I got go up from everything beginning at the third a AEX index up to but not including the eighth index. And then you can have 1/3 coat on that essentially says the number of steps you want to take. And so that's it from this license. So you have a great this yourself is definitely worth getting used. Have you play with this? Because it is a very useful tool to have so have a go at it and I'll see you in the next lecture. 26. Practical - Twister spinner: It's another. We've gone over a little bit about lists. I wanted to introduce a practical here called the Twister Spinner. You may remember Twister. It's quite a fun board game that many of us played his kids. Just a game. Let's have a look here says this game here, essentially, where there's a spinner and it tells you where to put your left foot, whether to use your left foot right hand, right, left hand or right foot on which colors to put it on on the mat. So we're going to recreate that so essentially, the program it needs to emulate, twist or spin up. So each time the program is run, I wanted to output one of the following at random so wanted to set tell you to use a color on. Then where to put your left or right hand or I should inclusion or here I left left or right foot. So maybe we just conclude this on one line, so it's not too confusing, so left or right hand or left to right foot. So one, I guess they left hand or right hand or left foot. All right, fit so we only want to have one of those. So we want to have one color and one body part either left hand, right and left foot. All right foot. So have a go that yourself. Essentially, there's going to be going to need to research how you can, randomly, We were looking at so far with the random module. But we've been looking at random inter ges, but now we might be looking it kind of using random in a different way for selecting from a list. So you may just a little bit of research yourself, so have a go at it, and then we'll go over the solution in the next picture. 27. Solutions: Okay, so hopefully you had a good day at creating your own twist to spin up. Now let's have a go at this together. So I'm first of all, as always, gets changes requirements here in something a bit more technical. So let's see. We want tohave so essentially won't have random choices from some covers. So I'm gonna change that because we won't have the colors on. I think the best way to store all the potential colors would be in a list. So I'm gonna put a list of colors and then I want to You have says somehow out. But choices from list of colors needs to research random from a list, then do the same for body parts. Then I'm pretty shocked that we need to really do is just output the results. So let's give this a try. So let's start from the top. So I'm going to create the list first of colors, so I'm going to just call it colors equals and then create this side shoes, the square brackets. And then we're gonna have the colors of red separated by commas, green that blue and yet greats. We have a list now we want to be ableto at random. Get one of these from this, too. What I'm going to do is I'm going toe quickly research that So I'm going Type I thin random andi list. That's the vessel that to see what comes up, how to randomly select an item from a list. Okay, that it's very hopeful, so she may perform. List. What's the simplest way to retrieve an item at random from the list? Okay, this is good to use. Random dot choice say if we import random and then use random dot choice and use the list fashion back. OK, well, let's give that a try and see what happens. So I'm going to import talk whenever you're importing things. It's kind of best practice just to put all your imports right at the top. It just got a bit nicer. Makes more sense. So now we've imported random. Let's see if this random don't choice works. Random choice colors Green. Please read blue. Okay, Perfect. That looks like it's it's randomly selecting elements from our list, so that's absolutely perfect. So now we found a way in which to you to get these Rendon's So let's create the next list, which is gonna be the body parts. Say, I'll just call this body parts. So I'm going to create the displays in the square brackets and then for left foot, right foot, left hand, then finally right. And so then we also want to get one of these random choices from this list. Now, let's store these in variables to make things easier. So we're gonna cover choice equals random dot choice here and then body on school choice equals random dot choice. And then we want to do this from the body parts list. Body parts is not defined. Oxide. Run this cell yet. There we go. Okay, now we Now, let's see. That's check out our to do this. So we've been able to somehow output choices from this two colors pick done the same for body parts. Tick. Now we seem to output the results. Course sums go print out, um, Twister Spinner Saiz. Then we go for the body part first and then to come up. I'm going to use the handy doc format into those in so nobody choice first and then color choice. Okay, Twisters. Mir says left foot red on and I'm gonna create this. Oh, just in one cell so that we'd have to run it with these different cells. Little time. So now when we run this, this doesn't need to be indented. There we get, we run this to a suspense, says rifle ble effort. Green left on green. Definitely a perfect. So I twisted spinner seems to be working nicely. So just to recap, what we've done here is First we had to find a way in which to store the different colors, potential colors, and I decided to use a list two stories. The next thing we need just work out was how do we get a random element from the list? So I literally just Googled random list python, Of course, because if you didn't use by thin, you come up with a different languages or random things. We then found the top the top line here. How to Regnery select an item from list that sounded like what we wanted to a quick three. We found this random dot choice with a helpful example so integrated that we realized random dot choice works nicely for both of our lists. So then once we did that. We let you just store those in variables for each of the choices, and yet we output it using print on the handy dot format, and that's it. So hopefully that made sense to you. If not, have a go to yourself on. Go back over this lecture and then I'll see you in the next picture. 28. Practical - List within a list, within a list: So in this practical, this is going to be less of us looking at a practical implementation of content. This is going to be more like a brain teaser. So for this practical, essentially, I want you to create a list. And then I want you to add a list within this list. And then I wanted to add another list within that list, so you should have one list that's within list that's within a list s Oh, yeah, it's a bit is a bit vers off brain exercise here, but it's actually very useful once we get to machine learning on the AI stuff. So I highly recommend you have a go. This make sure isn't Each list contains at least four elements, so the elements can just be like, you know, 1234 Um, yeah, and one of those elements could be the next list. So why and try and make the numbers different, It's It makes it easier when you're trying to see when you're tryingto do the next part of this practical below makes it easy to see that you're doing it correctly. So once you've created the list within the list within a list. I want you to try and access the 1st 2 elements off the list within the originalist. So let's say we have three lists by this. Oh, this is the first list this second Estes the third list in this second list. So the list within the original list, which is one so one, is the original list. I want you to get the list within the list, which is the second list, actually, the 1st 2 elements. Then I want you in this one in the third list. So that's the list within the list within the list are wanted to access the last three elements on. Then I wait out, put this whole thing so it's is coming out backwards. So give that a try. Like I say, it's a bit of a brain teaser, so it may take a few attempts to actually even get your head around that for me. Personally, I just like to get stuck in there with these kind of things and give it a try, but do have a works best for you, and they will go over the solution in the next picture. 29. Solutions: Hopefully, you did OK. Having a gate at this brainteaser. Now, let's have a goat. Are Abbas ourselves? So the first thing I want to do is to create a list. Okay, We have to make sure that each list contains four elements, at least so you can go start the 1st 1 I think I'm gonna have these as a words to birds, folks. Ah, mouse on you. So there's my first list. Officer need to store this within variable, so I'm just going to call this brain teasers. Okay, so that's the first thing done. I've created list. We take that off, so I have the positive feedback until a I created list. The next thing I want to add a list within this list. So I'm just going to put here an extra comma, and then in this list, some scale use of square brackets again, I'm going to do 102 103 104 105 100. So now we have a list within a list and notice how it's comma separated between this element here on the one that comes after. Okay, so we've now added a lend a list within that list successfully. Cool. That's good. Next you want to do is add a list within this list. Okay, So add a list in here and make sure it's comma separated between the elements. So use the square brackets to create the list on. Then for this one, I am going to do floats. I'm gonna have 0.34 hopes 0.34 0.97 0.333 d 0.5 on a quiet night in Monte. So this way, it's kind of easy discernible. Yeah, the first list kind of has. Well, the original list. That's a has the strings that we go in. We have hundreds within this list. There were decimals is just kind of easier to dio descend between them now. Okay, So if I had no added and this within a list with the list, take that one off. I'm just having equals because it looks nice. So each list does contain at least four elements. That's good. A case that's going to the second part access the 1st 2 elements of the list within the original list. Okay, so this is the original list, so I'm going to access the 1st 2 elements of this list. So the first thing we tried do in a separate cell, you go run this store first. I want to try and do is just access that list in general. So, Prince brain Teasers. So, essentially the way what we're gonna do first is just using indexing. We're going to access his list. So in order to access this whole thing here, let's see what index this is. Well, Burda zero Fox's One Bears to this one's going to be three. Get the index three. Okay, brilliance. That's out putting that whole thing Now it's saying I should output the 1st 2 elements, which is going to be the number 100 then this list is gonna be the 2nd 1 So now we want to start using some confidence, and so it wants to be doing a list slices. So if you remember what that is, we have the square brackets and then we use the code on to say we go from this starting index to up to but not including this index. So we could just put zero here, But we can also leave it blank and vitamin assume that our start should be right at the start of the list, but also, we want to access it. This within this list, I need to put square brackets, three first toe access, this whole thing. So if I just leave these both blank. Of course, this is going to fight discussion. We want just the start. The first index and the last index. So output the same thing. So now we want to actually the 1st 2 So we're gonna put zero. We're gonna leave that one blank. So we start from beginning on, we're gonna put in three here because, as you might remember, in slicing it goes up to but doesn't include this index. So we're just gonna have actually you want to put that at two? Because we want it to be. We want to get the zero index on the one index, but we don't want the two index, which is the third element. So there we go. We've got the 100 we've got that list as well. If I worked but three, then it would go up to but no include the next three, which would be so. This is the next one. So That's Index zero. This one's index zero. This one's index one the sun's index to So it include that. But it would include in next me, which is what we don't want. We only want the 1st 2 elements. Okay, so we've done this one so we can take this off. Okay, so the next we want to do is access the last three elements of the list with rest within the list. Okay, so let's first start off by just accessing that list. So we want to go into the brain teaser on dso to get the foot that may nous we go Index three. Now, we want to just get this list so again to gay in some more square brackets, and we're just going to go for index one because this 100 is the zero index. This is the next one. So now we have just the list that we want, so we want to get the last three elements. So what we can do How many elements you have? 01234 So we have five elements, but it goes up to index for, so you get the last three. So essentially we want to go from index was that 012 There you go. The way up the end so we can respect. We want to start from the next two. We're gonna go all the way to the end. Hopefully, this will output the last three elements in that list. Okay? And I believe on it. We want out for the whole thing backwards as well, which is actually very, very easy. We have to Do you spend print brain teasers so we want to have everything from the beginning and the end. Now we get going to include the step, If you may remember, if we put in minus one, that's going to of us everything. So as you can see, it's gone. Eagle Mouse and the whole list and then the bear folks. But that's good to me. Okay, So just to review, what we've done here is we've created less festival, we create list within, and then we had a list to that gradualist into that one. And then, first of all, we were able tojust access using indexing were able to access these two lists here. So we went 0123 and that was the index. So the syntax waas name of the list then square brackets three. And then we're able to access using indexing some some specifics within that list. And then we were able to go to list within a list within a list. Onda access some from there and then finally were able to reverse list by using to Golan's and the minus one. So yeah, that was that was a bit of them. Let me just add the tick, of course. So it was a bigger brain exercise. It's just very good to just get used to these kind of confusing, fiddly, difficult lists because it just makes you feel more confident with using this in the future . But really, what's important is that you're able Teoh at least create a list that contains another list on that you're able to use indexing, and you're also ableto get list slices, toe access, different parts off the lists. So if you haven't done this practical yourself because it was just a bit too much, then just have a good creating a list within a list and just play around with list indexes on their slicing as best you can. And then when you're ready, I'll see you in the next lecture 30. For loops: Now let's go over four leaps. If you've already done the JavaScript module, this will just be some revision for you, and it will be slightly different syntax to what you've seen. So in Python they make for leaps. Extremely easy now for lips could be used for mainly two things. One is for Iterating through a list. So, for example, if I had a list as 123456 I could use a for loop to print out each of these ones in 10. The other use of four loops is to have, ah, block of code executed a certain number of times. So let's go over those two examples together. First, we'll dio. The second I mentioned say, That's but a comment actually shooting code certain number off times and then the other one , is it? Rating three data sets, for example, List. So here, which can to use a very base example. So we're going to say for I in range 10. Okay, so what we're doing here, we're creating a variable called I, and in Python it automatically sets I to the value of zero, and it's saying what so food for? I in range. 10. Print I Let's see what what happened here. And then we'll talk about what just happened. So essentially, what happens automatically is that python set sights the value of zero. And it's basically saying in the range of 10. So it's basically saying until I is is less that so while I listen. 10. Basically. So for I in range 10 it basically says, Do this 10 times. Now I start off with a value of zero. We can set this to be wherever wherever you want. I mean, just for when, When teaching four leaves, people tend to use I pi could use number, for example, as long as I change it here as well. Basically, what it's doing is it's creating. Number is just a standard variable and zero, and then it executes this code. Each time executes the code number or whatever this is cooled increases by one, and it does that 10 times. So if I were to, for example, that's have range 40 I would go from zero up to 39 to its rate 39 times to do the same thing. Um, we could we could do a number of different things safe for Let's say we have a different one of us. Have another their ball could count and every time we want to count to be most played by five so we could dio count equals count times five. What do short hand, just like in JavaScript? We could do Multiply, it equals five. Now don't do the same thing, and then we can print out count as well using dot format, hip and count. And so there we go. So we can We can add lots and lots of code into our four loops, and we can do us everyone. It's this time or we're doing is the number here. This is the variable using, which is called number, but all of the time, if you just practicing, you can just use use. I It is printing that out, and that is giving you the account number each time. And then it's just going up each time we're straight through that. So that's one way of using for loops. Let's just change that. Three. Such a bit smaller. So the next way we can use four loops is to go through a date set, so most likely you'll be using list for this accusing interesting rates the most. The easiest way to do it, for example, you could say for we can cause wherever we want to be called for element in. And let's give this a very morning, My missed safe element in list. And then we put a colon. So in my list from the colon, enter and automatically in dense for us here. Three. Safer element in my list. That's just the print element. So basically what we should get each time there's the element being prince stuff so we can just iterated through a list. We can also it right through a string the same way by their I like Brighton. So I changed my list to my string. You will see their outputs each each character one of time. What was He doesn't have as much use as using list a lot of the time, but is there if you need it? So not only Queen just it rate through a list like that, but we could also make changes to elements. So we could, for example, we could put elements we could just print out element times. Well, we can't for example, print out elements. How do you dr Format Element? So it's been nicer. We could basically lots of different things. We can manipulate each of these in turn on however we like. So those are two basic uses off how you can use four lips. So in industry, for example, let's say in machine learning very often what we want to do is we have a huge block of code where we're essentially, you know, we have codes to train our Alka them. Don't worry about what that means. For now, if you don't know what algorithm is, essentially we want to train our algorithm 1000 times. So what we would say is would say for I in range one thousands. They would say you trained the algorithm 1000 times, basically because we wanted to train once. And then we want to do it again and again and again. So we just say, for I in range 1000 for example, do this many training sessions and it helps to make our AI smarter. Let's say so. That's one actual use off using for loops. So what we've gone over in this sector is said to have gone over how we can execute a block of codes numerous times by using for I in range. And remember, we don't have to use I here. We can use whatever we want, call number or blah, as long as we also refer to it in the foley. So for block in range and then weaken set here in these brackets. How many times we want our block of clay to execute better colon and then indented usually four spaces inwards. That's where we include all of our code. If we didn't have an indentation, for example, some study shift up expected an indented look. So his point of the line count times equals five. We will make sure that everything in here is is indented. If, for example, I were to have a line here, you will see that this happens first because everything that's indented here will then be within the for loop. So what happened 1000 times? Let's actually escape for 10 times, and this won't be included because no indented. So have that happened 10 times, and that the end will have hello just once, because it's not included in that block. So we went over how we can use basic for lips using in range. After that, we went into how we can iterated three data sets. Most commonly, we use lists so we can say for Michael this elements again. You could just call it I or whatever you want. Obviously, when you're working in a production environment, when other people looking your code as well, it's much better to give it names that are quite readable and make sense. By the way, if you just have I, it's kind of hard to understand what's going on. You can say for Element, for example, in the list. Print that out or we could dio my list plus sorry, not my nest element plus equals 100. And then, yes, we were manipulating the values as we go through. There are many things you can do that, but that's the basics of four loops. So I recommend you do is have a go trying this out yourself, so try doing a four in range. So I tried it afforded by that and then tried creating your own list and iterating through that, using the four in. So I've got that and I'll see you in the next picture 31. Continue, break: now in this that show we're going to be combining our four loops with R FL statements to use, continue and break. We're gonna go straight into how we can use these. So I'm going to use example with a list. That's I know we don't have a list of fruit. A couple, uh, manana Moti Banana on q B. So say this is some kind of list that we have. Excellent. So now let's just try to it right through the whole thing. So let's go for for lead to do that, say, for fruit. I'm gonna quote fruit. You can call this whatever you want, so some people call it I when they're just getting used to it. But have it worked? Used. The word fruit is kind of quite readable when it makes sense. Sony. So for this. So for each element, basically, in our list the colon, my press enter it automatically in dense four white spaces in them, said Indian. You could do four spaces because they're just press tab. Good, good takes registers, tend to automatically indent for you. And then let's just print them out so they could print fruit now just out of curiosity. What do you think would happen if we put in fruit list here, Have a pause, pause the video and have a think about this. So what's actually gonna happen is it's going toe print out the list five times so saying for each element, print out the list. So there are five elements in the list says just printing out five times. Just an interesting thing to keep in mind, just in case that happens to you. And you're like, Wait, what's going on? Okay, I've done the wrong thing here, so I've done this now, but now we can see if the FL statements do something really interesting. We could say if if the fruit is the morning banana, for example, which we don't want, we don't want to burn out that. Say, we don't want people knowing about our more even Ana. So usually we wanted to Brent. But in this in this instance, if the fruit is equal to Marty Banana, we don't want anything to happen. We just want to go back to that. We want to kind of ignore just this instance. This is where we can use something called continue and When we say continue, we're basically saying, if the fruit so it's going to go through and it's gonna do the first fruit than the second fruit were saying If the fruit is Morty Banana, then just basically skip it out, go back to the top and go on to the next element, and then we can just put else make sure you make sure your else isn't here. You don't want that in the F statements so that we said else this print out the fruit. So what's gonna happen here is we're gonna have alot the fruit printed out except from Woody Banana because of what's happened here is it's gone. So is gone. Okay, so for each element in this list, if it's more of an honor, or Apple isn't ordered banana, so we're going to print it. And then let's go to the next element, which is pair that's also not more banana. So again to print it. Nations banana. Same thing. Next one's Moti banana. If the fruits equal to Marty Banana, which is then continue, all that happens then is the basic continues right up to the top, and it doesn't execute any of the code that's below. So we could also you have print bluh. So you gonna see It's gonna print each of these, but it's not going to print for the instance off of the Madi banana. So it's just ignoring below this. Continue. If this if statement is true underneath the continued is gonna ignore everything and it's just gonna go straight back to the top of the top of the four leap and go on to the next element. So that's how we can use. Continue. Now let's say we it's a production line or something. Andi, if we see a morning banana, we want to stop everything. So then what we do is we use break, and that basically says, Break out of the Foley, Stop the four leave immediately. And so outside the four loop, we could imprint, um, production stopped. Moti banana found So you'll see here. Now we go, it's it rates through all of them until it gets the more he banana And then it says if fruit is equal motive Alana break. So it breaks out of the loop and ignores any of the code below. And then it goes on to the next line of code, which is to print this, and that's outside of the four lutes. That's completely irrelevant to the forties. If we were to invent this, for example, then it would be printed every time until we got to Moti Banana. This is how we use continuing break. So continue. Basically, we use an if statement. And if that's if that if that statement is true, then it's weaken to say continue that it ignores anything that comes below it. If we use break, it works the same way. Except instead of ignoring 18 below it and then continue with next element, it just completely stopped the four loop on It goes on to whatever code is below, so that's continuing break. What I recommend you do is do something similar, so you create your own list. Add a four out of four loop to it right through the list and then, at an if statement toe, say if, for example, it's more the banana, or you could have something like if you had a list of numbers, you could say, you know, if the if the number is more than 50 then continue or then break those are things. See if you can add your own flavor to this and see if you can add your own application. So essentially want to create a list for Luke to it right through it than an F L statement . And in the if you want to try playing around with, continue and break and see how that works. It's have a go yourself and I'll see you in the next lecture. 32. List comprehension: So we're now going to look a list comprehension, which is a really, really interesting tool and helpful for creating lists quickly on filtering through existing lists. So we're just gonna go through three applications now, the 1st 1 being a quick way to create new lists. So big good attention to the syntax we're using here. Obviously, we want to create Elissa first, say, my uncle, this is the variable. And then we're going to use square brackets here to indicate we're creating a list. And then I'm just going to create a basic list using a for loop within a list. Use any number I'm going to decide to use x x four X in range 10. So let me just bring this out. And now I tell you what's what's happened. So we're print off my list so you can see that that's created a nice list between zero and nine. So what's happened here? Well, within a list, I basically said Great element. That's X for X in range 10. So if you think about a four leap, that's let's right out for you. Say four X in range 10 Print X. So that should be relatively normal to you now I hope you've done. You've practiced for lose yourself as well. So it's quite familiar. Were basically saying for X in or for whatever I or number everyone to call it in range. 10. Print that out. So we're doing the same thing, but we're putting it within a list. So saying we could again change this toe I without the space. They get WiFi in range 10. And that's how you can quickly create a list. So you can basically say for I in range that say, hundreds. We want an element to be that number. So it's gonna be first was giving zero, and then it could be one and two. As we go through the for loop, penetrate up to 99. Okay, so that's that's the first way one wage create new list. The next one. We want to do how we can create new list, but using using a condition on I'm going to use the module ist sign here. So it's just the percentage character, but that's very helpful. So you may remember we went over this very briefly. Madras is basically a way of saying so. Let's say this right down here more just is a way of saying the number of remainders that we have after dividing something. So let's say we do 10. Divide by three. So three is going to go into 10 3 times because 369 and then we're gonna have one remainder . So 10 module is three. There's going to be cool toe one. Let's say we wanted to do 10 modules for so four goes into 10 2 times that eight and we have two left over. So if we use this condition here, if a certain number modules two is equal to zero. Basically, what we're saying is that if it's an odd number, because if you think about it 10 murderers too. Okay, well, that mean that's uneven number because the two goes into 10 perfectly. So there are gonna be any remainders. However, if we put 11 modesty, we're gonna have one, so we can use a condition so we can say if x more just two is equal to zero. Or basically saying, if X is an even number, say what we can do is we can create another less using this convention so you can say my list, so we just want what numbers in here We can just say in square brackets. Almost use the word number again. You can use whatever word you want has always used the same number both times. So number for number in range 50 and then we just add the condition afterwards, saying, if picks much, this team is equal to zero. Now, if we print this out my old list that's not currently working because no using X, of course, we're using number. So we run this now you will see we get to our numbers If I change this toe one. Well, I could put this back to zero and have not equal to. He returns with the numbers we just talked to equal to zero for now. Well, let's give something even easier. Let's go for if number is equal to there is no equal to 10. So then this is basically going to output. Let's do this up to 15. Skeptical the numbers except for 10 so you can see that 10 has been skipped out. So this is a very good way of creating list quickly the where you can add a condition too. So if you just wanted odd numbers or even numbers or, you know we could have we don't anything divisible by five. So you have been no equity. It misses out anything that's that's not divisible by five. So I miss out anything that's divisible by five. So finally, we can filter through an existing list on by adding a new condition. So let's say we have ah, good old fruit list. So within this list, we have a pool and then we have a pair on QE Banana and Modi. Uh, no. No. Okay, so now what we want to do is we want Teoh filter out anything that ahmadi banana. So we have to do here is we can create escort the fresh fruit list he calls When we did the square brackets to say that we wanted to be a list and then we can say four fruits in fruit list, remember? Remember this from our previous four leap where we to get Detroit to release. We just have to put in than the name of the list. And then we had our if statement, we can say if the fruit is not equal to, um MODY Banana. So what we're saying here is we want to have a list of all the elements in our fruit list, but only if the element is no equal to Marty Banana. Of course, we have to add in actual four so fruit for fruit in list. If readers know equal to Muddy Banana, so that's just test this. Now copy this, and every day we have a new desk that doesn't have any motive and honors. So just to reiterate what we've done here. So he said, fruits so said like an element for every element in the fruit in the fruit list. So so with square brackets were saying, We're creating a list They were saying in this this We want an element for each elements in the list if this element is no equal to Marty Banana, So just to go over what we've done, we've gone through this comprehension, which is a really good way to create new lists quite quickly. It's a very basic example. Here is my list is equal to than in square brackets I for I in range. Whatever we want, that you can create a list of numbers. Next we looked at how we can do that whilst in creating a conditional. So here, just that the end, we've said if the number modulates five. So if the remainder number after divided by five is no equal to zero, that we could go something a lot easier. So, for example, before if the number is more than 10 so we only want we don't want the numbers over 10 or something like that. So that's how we used if statements to basically filter through a new display creating on. Then we looked at how we can actually filled through existing nests and create new lists with this comprehension, doing something like fruits for fruit in this fruit list. If the element fruit is no equal Tamati banana. So have a go doing these. See things yourself. Try and create a new list using this comprehension. Then try and create a new nest with a condition. It could be anything just a simplest like if X is less than two or more than five or something like that, and then see if you can create an existing vist on filter through that also using this comprehension andan If self statement, so have a go and then I'll see you in the next lecture 33. Tuples and sets: in this sexual, we're going to look it to less commonly used but still very useful data types, two bulls and sets. So two pools. You can think off as being pretty much the exact same as this. Except for one big thing on That's that there immutable eso. When I say immutable, it basically means you can't change a given element within. It s so the use. The usefulness of two poles is that it saves on memory because the elements within the two people can't be changed. It means that it just it needs less memory to be stored, whereas with lists, unfortunately, takes a bit more memory because you can change any element to any time. So let's get straight and I'll show you what I mean sometimes create a variable called my people. And then, instead of using square brackets, you just use normal brackets. 1234 Now we can access any one of those, just like we do with with the list indexing. So I want to get the number two. So that's good. B Index one if I try and change that. So, for example, with a list, let me just show you up here. So in case you want to be ableto create a cell above you just tap the letter A. And that creates a new cell. So in creating new, less called, my list equals one t three whatever. Now what I can do is I can actually change any one of these by just doing my list. So, for example, let's go for the index one, which giving number 23 I can change that 200. So now if I print off my list, you can see that I've changed it. So just by using the index of mayor would change that with two pools. If I try and do the same thing, you can see that to Project does not support item assignment. So I'm not allowed to just change messing. An element within the to pull because its immutable or you can think of is not being changeable. And that's the main part of to Post, to be honest, So the way to make two pools is instead of using square brackets with lists we just use normal brackets on. The important thing to keep in mind is that you can't change an element by just actor accessing it Index Nation will look at is set So just underneath here to create something called my set and then for sets, we use the cardy brackets on it. Within that the the very important thing about sets is you can't have replicas. So let me let you give an example. You couldn't have two sets of 22. So if I run this now, when I printed off your C that only one of those remains and it also it also orders it. So what's important here is it doesn't have an index like it does with lists. So what's helpful here is when you want to, we want data where you definitely don't want to have any replicants. So, for example, I put five. Or let's put the word tiger three times. You see, the tiger only appears once five only appears once, so that's the real benefit of using sets. So just to go over what we've talked about in this lecture, we've looked at how well would lists. We can replace just a single element by accessing his index and doing equals, and then with two pools where we use the normal brackets the parentheses. We can see that you can't change any of the elements within A to pool their what we call immutable. I'll just write this above so you can see the spending double them. So they're immutable image comment that which means yet you can't change any of the elements within the too cool. Then we went on two sets where we used the curly brackets on sets. Many there because we if you don't want to have replicas. So if you have numerous occurrences of the same element, it would just whittle it down to one. So that's two bulls and sets. Why I recommend you do is you have a goat creating or into pools. Try to change them just like I did here. So you see this error and then try and create a few sets on. Try and add rep column elements and see what happens. And then I'll see you in the next lecture 34. Disctionaries: in this lecture, we're going to be going over dictionaries the very handy way of storing data in key value pairs. So let's get straight into what they actually are. And we've actually come across them in Javascript before. So I was going to create a very book. Would my addict equals? And then we use the QWERTY brackets. Now you might think, OK, that's what we use for sets as well. But because we use key value pairs in the dictionaries, ITT's NOTT a problem. So for dictionaries, we use the QWERTY brackets and then we have a key. So let's use an example off. Let's say we want to have different people was ages. I have John and then the value would be 30 for my age. And then we separated with the comma. So let's do another one. Let's do Anna and then coat on. And that saves. She's 29. Okay, so now we have a dictionary. So we have within here. We have two different key value pairs, and that's the basic dictionary we can keep adding as May as we want so sad. You know, Kurt on 29 as well. We can just keep adding them like this. So what if we want to access a single value? Well, we can do much the same as we do for our lists, but we don't use the index this time. When square brackets, we can just put the key where we on the body from. So let's say I want to get on his age. I can use of square brackets and then with single quotations big, it returns the value. So that's how we access the value. Now what if we want to access all the key value pairs? Well, we could use a four leaf for that. So you'd say, four. Prepare in my picked colon and then it's already invented for us going to say print that. Okay, so it seems to work well with prints off all the keys Wolf. We want the values they or would use another fully, but this time would say for Pair and my picked print on. Men would want to dio my unscored EQT and then in square brackets would put the pair, because if you think about it, each of the pairs is the the elements were getting back. Is this John and or Fiona. So we'd be doing my dicked and then in just like we did here with Anna would be putting it in just here. So that way we'll be getting the values. So that's how we access all the keep key value pairs. So how about changing a value? Well, that's actually very easy as well. So let's say I got my age wrong and I was 31 then. I'd want to go in and put my underscored addict and then access it by putting in square brackets. The key and I could just change it to the body. So every print off my addicts now you can see that that's changed to 31. So next up, we want to be able to add new ones. So we want to add a completely new key value pair we have to do is pretty much the exact same thing except in here. We put the new name. Let's say we want Phil to join us, and here's 26. Print this off afterwards. It's no vacancy. We we don't have to put in an existing name here. We can put in a new one and give it to value, and it will add it to our dictionary so you can see just how useful dictionaries can be. So now we want to look at how we can remove a key value bet and for that to be you something a method that reviews before there with lists and that's dot so we have to do is my addict dot pop. And then we put in the name off the key that we want to move. So let's say we know what felt. So now if we print this off, you'll see that I know nowhere contains coat. Okay, so that's pretty much the basics for for the dictionaries now. So what? You should definitely off the sections have a go yourself creating your own dictionaries. Remember, you need to have both a key undervalue and use the code on in between those and then to separate that from the your next pair. Use a comma to access a single value. You just need to put the name of your your dictionary and then with square brackets within that you put the name of the key, and then that will produce the value for that key. For example, Anna and 29. If you wanted to produce all of the all of the keys then we have to do is do four. Whatever name you want in the dictionary, print those off if you want to. Taxes the values or you have to do is down here. Use the name of the dictionary than in square brackets, the elements or the the key. Then, if you want, you can easily change the values by just using the exact same syntax, putting the key of which the value want to change in here. You can also add new ones in this way, and you can remove them by using the methods docked pop. And so that's the basics of the dictionaries. So why I recommend you do is go through this next yourself and have a go at making all of these work on that I see in the next sector. 35. Practical - Currency converter: so in this practical, and I'm sticking to describing the requirements in more of a way that a nontechnical person would describe them to give you the chance to practice, converting that into more of a technical requirements. So in this program, what I would like you to do is to create something that contains information on the currency conversion rates from from U. S. Dollars to four other currencies. So have a think about how you could contain the information for a number of different currencies and some sort of currency conversion number. And so you think about the recent lectures with what we've been doing next, I want you to create the program so that the user can enter in her amount and also the currency they want to convert it into. And then the program can output the new currency and the amount that it is to give that try , and then we'll go over it together in the next picture. 36. Solutions: Okay, I hope your attempt went well. Now let's go over a solution together or one possible solution that say so. The first thing I'm going to do is always has changed this from just sort of a basic description to more of a technical requirements. So let's see. The first thing I want to do is contain information, So the first thing I want to do is obtain current conversion rates between USD and for, well, the currencies. So we need to research that online. So the next thing we want to do is store this information. So let's say, for example, we want great British pounds on. Let's say that's the conversion rate is like 1.2, I don't know. So I think and because we're gonna have a number of different currencies, I think we could use the the currency names as keys and then we could use these of the values. So it looks like the best option is to use a dictionary. So we want to do something like this when then add the rest afterwards, So good put us a dictionary. So once we've got the information stored, will be it that will be pretty much what the first requirement needs. So let's go for the second thing. The user can enter into in tow, enter in an amount in USD and the currency, so create inputs for amount. It's convinced you and currency, this is going to be a string. So what? What This has missed out the the requirements above its only saying what it wants to happen . But there's a really important step in between this one and this one, and that's the calculation. So, um, we want to be able to calculate the currency. New currency? No. In order to do that, we're gonna have to work out which one to use. So we have to use an if else statement. For example, if currency input, whatever you call it, is equal Teoh tvp etcetera. So when you were gonna have to sell NFL statement for each of those two character, then calculate the output and then we want just outfit in a sentence. So if you're a tour confused by this line, don't worry about it will be going over it. Now let's get started with obtaining the conversion rates. Some is going to use a search engine under guns. Look up conversion rates table. Usually the top ones who get XY currency table. They usually start with us dollars. Yep, and here's a handy lesson on the side. So the euro the rate is 1.4. So it's going to use like, let's see. So we just look at the U. S. Dollars. Let's try actually, just having appear USD. Okay, let's try this one. X rates they had. That's perfect. Say these are their conversion rates, which were slightly different on but anyway so let's just go for these ones here. So we're gonna go for Euro British pounds Indian rupee here on Australian dollar. So let's get started. We're going to create aid, actually, so we're going to cool a squirt currency rates equals and then the dictionary. So you want tohave within here? Great dishes, pounds Australian dollar, which I believe is a u. D. Maybe doesn't really matter than the euro which you are, and then engineer appear. Which Indian my r p. A date thing? That's correct. But it would be so let's now get that information. So if the British pound is zero points there above about, so call me this over. We have our first key value pair. Excellent. Next, let's go for Austrian dollar. What if that's I N p Indian repair? Very. Um So next up, we're gonna get for the year. It's good that in there, remember, with the dictionaries, you have to have the key and then you won't have a colon, and then the value. And then you separated from next one with a coma. So if Australian dollar and finally engine repair and apologies for my Indian students for not not knowing what that what it is, In short, you know what? I'm gonna say that because I want I want to keep it create a Indian rupee. I n r every day. Perfect. Okay, so now we have our dictionary for the currency rates so we can take off that 1st 1 accents . Now we want to store this as dictionary, which was also done so we could take two things off at the same time. Excellent. Create inputs. Okay, so we'll have user amount equals inputs, and we can say please in tow amount, then right below. We can have a next one which is going to be user currency inputs please and toe currency and then in brackets tvp a d you're or, you know, just to make it easier for them. Okay, so using an entrant in tow, enter in and amounts on the currency. So that's this one hit. So done. That's actually one thing we haven't done is this one needs to be an interview. So me to cast this to instruct by writing into then putting brackets are on that. So now we can take this off. We've caught the inputs for the amount is insuring currency a string. So next we want to do the calculations. So let's just say this was simple and we knew it was going to be TVP every time. So there was only that option eso then we could do this quite easy. We could just do converted mounts equals use, ah, amount times. Ah, currency rates, currency rates, Then in this great brackets TVP on. That would be a simple as that, but because we have lots of different options, we need to use our NFL statement. So first we're going to say if use a currency is equal to TVP, then the currency amount would be equal to them. But there's actually so we could do this way. Let's go this way first, and then I'll show you an even sleeker way of doing it duty. Or if use a currency is equal to you. D, then this would happen. Seven being do you be obviously of the u. D. This is cocaine paste, this one. So this time we wanted to be You're sorry? You're right. Twice. And then finally high enough. And then for the else we could just put, um, you can print out you entered on invalids currency because if that if it isn't one of these than its invited for us float currency, try again. You're looking. So now we've got away to cope with new county and then output this in a sentence. So this pit in another, another cell prince you asked to convert, have dems. That should be the amount in the currency. The value Ah, convert this Yes, d obviously to that currency the value off. But he is dumb and they were put in the currency once more, so that would just use dot format. So we're gonna be entering in the amount first, so user unscored amounts and then we're putting in the use of currency. And then we will be putting in the converse it amount and that will be messing it again. Use a currency. So that should work quite nicely that see, please into the amount of this guy for hundreds on a converted to G B. P. You want to convert 100 U. S dollars to D V. P. The value is 80 on. Let's just look at the valley. Yep, so that looks like it's about right. Okay, Fantastic. Now we could use if nlf just like we've done above. There's actually more succinct way to do that. And I'm going to add that in just here so we could actually do this in a much simpler way. We could just bet converted amount equals user amount, times currency rates on then the square brackets. Whiffed puts is the use of currency because whatever the person's put in is either gonna be GBP au de. You are going up. It was something different than we would have a problem. But this is a nice, simple way of avoiding using the l. If and else the reason why I want to do this first is just so we could do a bit of practice with, if LF else. But this is much like a way to do it now. One thing that comes up often when people are putting in Imbert is that they might, instead of putting in a u d or capital was they may per in it in lower case, for example, there's a very handy method we can use to Jane that so that it automatically turned turns to upper case, which is what we want. So we have to do you for this because it's case sensitive. So if, for example, we would put in here tvp sorry in the string Stoke G D. P. It's basically saying that we don't care. We don't have that key press G B p goes fine. So we want to convert the user's input into uppercase straight away so that we don't have this problem. So all we have to do it that end here is we can just put dot tapper. That's a nice method that turns everything into uppercase someone 20 in tow. I and our you bring this out. There's a working just fine. So we successfully done that so we can now take this off. Well, so just to go over what we've done in this practical, first of all, we found a good way that which weaken store all of our conversion rates, using a dictionary with the names of the currencies of the keys on then the the actual conversion rates as the values. We then got the user's input. So we got the amount by using input and then casting it into an integer. We then got the currency by doing something similar, but we didn't have to cast into anything cause automatically a string. We also use the dot upper method, which is nice old thing just for you to remember which converts all of your strings into uppercase characters. We then used NFL statement to basically say if the use of currency is TVP, then weaken do the calculation here, which is just the amount times the currency rates accessing each of them in the way we do for dictionaries, which is the name of the dictionary. Then in square brackets, we have the key we had that in and then all we had to do was bring that out. We found a sneak away to Teoh bypassed all of this by Justin Convert. Amount equals user amount times the currency rates. And then in here we just had the user currency. And so that's it for this lecture. There's anything you're not sure off. Why I recommend you do is just have a good guy to yourself, maybe go for the lecture. And if you still want problems, let me know. But why I recommend you do is just give it a few guys, because the more you practice, the better. So you have a gay and then I'll see you in the next lecture. 37. While loops: so similar to four loops while loops run over a block of code. A number of times very different is that the number doesn't increment. Let me explain what I mean. So we'll go for a quick for leaps. A for I in range 10. Print I. So, since this is just going to apprentice 0 to 9 so automatically with the for loop, what's happening is each time this block of code is printed out or executed the eye, we're adding 12 It was being incremental with wild leaps. That doesn't happen. Which leaves us with a slight danger. So I'm gonna say, While, um, let's say I is less than 10. So with wild leaps, we also have to state the value ahead of time so we can say I equals zero. So what? Let's say, Well, I while I is less in 10 print working. If we just do this, this is going to go into an infinite loop because essentially I is not going to be changed In four loops. The I is automatically increment ID, so we get plus one with wild. That doesn't happen. So essentially, if I run this, you'll see it will just keep running forever, already getting lots and lots of working. So I'm just going to stop that before it goes out of control. So what we have to do with wild leaps if we want to do something similar, is we just do I plus Plus to say, Add one toe I each time Sorry I plus equals one, for example. And then that's gonna happen nine times. If you had spent I he would spend the exact same of the for lead so you can think of a while. Loop is being very similar to how use a for loop except the UNITA specifically say beforehand what the value is. And then you give a comparison, that's or a condition. And while that is true, this block of Cade will continue to work. So here we have two increments. The i ourselves. So that's the basics of a while Loop. Why I recommend you do is have a go yourself and just creating a couple of while loops on. Then, when you're ready will go into more detail, more loops in the next lecture 38. While loops in games: it's another. We've gone over the basic syntax of how we can create polyps. Let's look at them in application. So one thing we can do, for example, is if we want the game to keep going, let's say, for example, we wanted to play a guessing game where the person had Teoh. Guess gets a number on DWI, want them to keep guessing until they get the correct one. Or we wanted to play hangman where we use had to keep guessing the correct letters until they run out of lives. For those sort of things would want to be using a wildly so would want to be saying, for example, maybe we'd create some variable with cool. It's play. We concur, upset that to booty in like tree. And then we could say, while play is equal, true. And then we have lines of code playing the game. So then what would keep happening is that, let's say, for example, the user had to guess use. Ah, well, let's say the computer generates the number. User guesses the number, and then we get out of here. If the user guess is equal to computer guests, then here we could end the game, and there's two ways that we could do that. So let's actually let's think this through. So let's just do something random. Like, for example, that's just say, Ah, number. What's true? Real number equals 10 and then we have an input here. What use? Ah yes, equals an interview. Cast of the input. Guess the number in brackets. It is 10. And then we can say it. If the user guests is equal to real number, The one thing we could do is we could say play is equal to force, so that then when we come to the top of the while, loop again. If if the user is guessed it correctly, well, then it will no longer be cords true. And it will stop equipment here saying this outside the wildly game has ended. Let's give this a try, so if I type in the wrong number, O Brien is just gonna ask me again. So it's just describe what's going on here. So saying, while players true and play is equal to true, we're going to try and get the input from the user if the user guess is equal to the rule number, which then would say, Play cause force and it would end the wild leap. But because it wasn't we were playing in the incorrect number. He goes back to the top of the wild leap and it says Wall players true. So it still is. And then we keep going through that that loop until the use guess is equal to the rule number. So if I put in 10 here, that should and oh, I wanted to play equals false, not play is equal to force. It's, ah, small estate to make. So I just had to change that from plays equal to force, which were used for conditions. And we actually want to assign the play variable to the Value Force. Now forces working with these not liking Ben 10. Now it finishes on. Then it goes on to the code afterwards. So this could work. However, what if we had more things happen off underneath? So I right, more things print more off again. So let's say, for example, when the person guesses the right number we want, we want the game to end straight away. Well, if we do this so things are things each time. It's saying right, more things, doing the other bits of great off. But here. So when I put in 10 finally, it's actually going to still put in some more stuff afterwards. So the game is going to continue on. Let's say we had a lot of things going on. So, you know, we had loads of these going on. You wouldn't want the whole game to play out before ended. You want it to end right here. So although you could set plate two equals false, you then have to wait for a while. The other co two current carry out before the code goes back to the top, where it looks on the condition that says no players, no longer false. Now we're going to break Well. You have to do instead of paying. Plate was false. You can just put break. So now if we run this, I might need to restart the colonel again. So now if we run this for no working now, if I put in 10 is gonna end straight away. So we didn't have to go through all of this stuff first. So that's the best way in order to do so you still set your variable plate true at the top , and then you say, while plays equal to true, you do all that because you need to have some solve condition that stays true. You could. You don't even have to do this. To be honest, one thing you could do if you don't didn't want toe to read that well, you could just do while one is equal to one. You just want to have a condition that's always going to be treated. And so then you can just break any points, and that will leave the game. So what's going over in this lecture is essentially in games and in things where you want the user to keep going until they get it right or until something happens. Then essentially, you want to have a while with a condition that's always going to be true. And then when you want the game to end or the program to end, you just put in a break under some sort of, if statement. So that's the while. Loop in a bit more detail, so I have a great this yourself. Try creating while leap where you create a basic game, and then you have some kind of ifs. If statements where effort condition is true, then the coat breaks. Okay, so have a go yourself and I'll see you in the next lecture. 39. Practical - Number guessing revisited: So you may remember one of our previous practical is, for the number guessing challenge, where we essentially had to create a program where the computer generated a random number between 100 then the user could guess. And then we used, if our statement to let the user know if it was correct, too high, too low or just wrong. So we entered this. And here, for example, the next one. It tells us that it's too late, because the actual computer numbers four to do on my guess is six. So, essentially, in this practical, I want you to do the same. If you still have the code from last time, you can go ahead and use that or for good practice, just try and create it again. But this is the important part. So the number game number guessing game revisited. The first thing I want to make sure is that you're creating the same number gamers before, so the computer randomly generates the number, use us to get, and they get guests and they get feedback if it's correct or incorrect, too high or too late, and then what's new is I want you to make it so that user only has to run this once, but they can keep guessing until they get it right. As soon as they get their guests right, the program ends, So give that. Go on. Then, in the next lecture, I'm going to be going over very briefly how to create the game as we did before and then focusing mostly on how we can make it keep the program running until the user guesses it correctly. So have a go yourself and I'll see you in the next lecture. 40. Solutions: so hopefully had a good goat that yourself. Now we're going to go through this together and find a solution. It's the first thing I'm going to do is actually go start from scratch simply absurdly, because I'd like to revise those contacts with you once more. But I'll be going through it very quickly because by now hope for those concepts would have contents were stuck in a bit better. So I'm going to import the random module in order to generate the random Brendan number. Next thing I'd do is get the computer number. Peter number equals Brendan dot rand int those random insta between 100. Next, let's take the use a guess. So user unsought guests he calls. We're gonna cost this into an interview of the inputs because otherwise have listen, she's going be a string which went work. So I guess a number then we won't have NFL statements. We want to say if the user guests is equal to calm Futre number and I'm gonna change this to just come because just easy, it's type. I'm gonna be typing it quite that so if you use it, guess is equal to company on then we want to Prince, you win. He wants you else. If use a guess is more than company, I will say too high. So there any other possibility? So you see the equal to more than or less than so we can just put the rest, which is God be the less than or as the else so we can print too late. And so this should work. And I see that's just give it a try. So I guess the number three too late. Okay. So I'm just wondering, I might have this output something a bit more interesting. For example, your guess was over. So just the end here in print, your guest waas this computer waas This just so you can see the numbers makes a bit more interesting. So this is using Guess on the other one is going to be comfortable. Okay, Yes. Number five to hide your guess. Was five computer with Steve or that was closely. Okay, so at the moment, we can only guess once and then we have to run the program again. I wantedto happen so that I just run this cell once and it keeps going until I get it. Great. So what I'm gonna do is include this in a while and I'm going to do while I was gonna say play is equal to play. I know because that condition is always going to be tree. So now if I were to run this now, even if it isn't you, this is This is just gonna keep going. But I want to include the guests inside, right? The first thing wants do we want the computers number to stay the same Because it chooses it once and then we want to keep guessing. So each time this code each signed the wildly runs. First thing we do is we get the number and then we see if it's the same. It's more of his less than so for any of those essentially is going to go back to top and we'll do another guess again. But even if we guessed it correctly, the loop was still keep going. So under the if statement, where the user guess is the same as the computer, let's just add a break so that when they when they are the same, then it will break. So I'm also just to make it easier for now for testing this print out the cop there. Because that way we can just make sure you stop this, because that way we can actually just test it a lot quicker. So we just want to see if when we when we do this or how we can get it high or low. So the computer number 70. So I am going to put in nineties the first guest. Yeah, that works just fine. Someone try 50 Too low. 32 low this K for 71 too high now. Free type in 17. You win your guest. A 17 computer with 70. Fantastic. So that seems to working now. So what we've done in order to keep this constantly going until I get it right, we put it into a while. Leap within this wall, that wow leap essentially put some sort condition that's always going to be treated. So soup, for example, while plate was playing or yet you could do anything like that, any condition that's gonna always be true on then you put your code in here that you want to be repeated so the user makes a guess, and then they get feedback. Music takes a guess. They get feedback on. But that could just go on forever until we have the if. Statement where the user guessed that the same is the camp we add in a break. So that's it for the number. Guessing Change revisited. Hoping that makes sense. City. If you want to get some more practice, try and come up with another game or another application where you could use this wall loop to keep going until something good or something bad happens, and there on the world has broken. So give it a try and then I'll see you in the next lecture. 41. Practical - Five lives: Let's get stuck in straight away with our next practical. This time, I like it. Create a game where the user has to do something instant computer, for example. They have to guess around the number. Will they have to select a random element from a list on? You wanna have a condition where the user can lose lives based on their guests? So if the user guesses the same numbers, the computer or guess is a different element from a list, then the computer, something like that, and they lose one life. And so I want the user to have five lives per game on when they run out of lives. So when their their lives are at zero, then the game ends and they get a game over message. So this will be slightly similar to the recent practical have done. So have a go tits and then, when you're ready, will go over the solution in the next picture 42. Solutions: Okay, let's get stuck in creating a solution to this practical for the five lives. So as always, I'm just going Teoh, create some new specifications below that. A bit more technical. So we wanna create game where the user has to do something against the computer. So I'm going to go for is great game random dot rand int First his input 10 here and then the condition for the is lose life. If they are the same e g 11 user loses her life, we're gonna keep it Very simple. I can't see you has five bytes per game. So Creator user life equals five. That should be fine when they run out of lives. So we want to use a while loop if I use a life equal to zero break. So I'm just writing these small notes myself. Just I realized right What parts of python do I need to use a wild leaf? A while loop variable here on some some kind of game here using Rand int and Imbert. So let's get started. First I'm going to do is imports the rand int So from random, I could just import random. I'm just gonna import with one method because that's all we're going to be using, say, from Random Import Rand Inter Before we were doing import random, and then we'd have to do random doctor ran into. But you may remember from a previous lecture you can write in from random import redundant and then you can just straight up Use. Ran into is the function it says we will be doing him. So the first thing I want to do is have the computer number, so we'll go for Trump on school. Numb equals ran into there. One. This just trick that's working. Compact, go numb. Zero zero the, uh one. Okay, perfect. That's working. Well, now the next part of its that's create their user lives outside of the wild user lives equals five. Then we want to create a while leap. And we want this to keep running until these runs out of lives. So we're going to have the use when the use of runs our lives will use. That isn't with an if statement. So we just want to have the wild leap constantly going. So we're gonna do something that's always going to be treated So while one is equal to one . So in here this is where we want the user to make a guess. So use a guess is equality impact True's number zero or one? And of course, this would be a strings. We want to cast that into an interview. So then we won't say if the user guess is equal. Teoh on score. Numb prince, You win. And for now would say else print you lose. Now we know there were no actually out putting what the user guess what the computer guesses. Let's just do this up here for now. We'll just print computer. Then we use the cutie braces to do doc format se but dot former here. So have the computer, which is company, um, on the user, which is used, I guess. Okay, so 0140 computer one user zero. Okay, so for one this time, Yea, we win. But it still goes whether we win or lose. So you want to break that loop if we win? However, we want to do this with user life, so we actually have it whenever they lose. We want the user to Well, when the When the user runs out of lives, that's we wanted to break. So actually, what we want to do at the bottom of this, we want to say, uh, if user lives is equal to zero break we left having. Ls there anything that you could have that so if so, want Once users done the guests and they get the feedback, that's when we're going to say if the user lives zero, then break. Otherwise nothing's gonna happen. It's going to go back to the top. So the other thing we want to do is when they lose. We also want to reduce the number of lives by one. So we will write that as well. You lose a life and then user lives we could do use the lives equals user unscored lives minus one or in shorthand, we could just do use the lives minus equals one, which is the same thing, is just saying take one away from user lives, never print lives left curry Rockets. Still, for that, he's on score lives. So that should be working. Now this Okay, this just restart. It'll for while we're at it. That should check this this Get this running. Let's check the to do list. So we've created a game. We've got random in relevant versus imports. So got that. So, dick, next we've got. If they are the same than the use of these life for that now, using life equals five. Yep. So finally, if the user life is zero, then break so well. Let's have a look and make sure this works. I think we've got everything we need. It's escape. Zero computer zero users Are you in? Great. Oh, we got it wrong with these life. We got it wrong again. We used lose another life, while so the computer's gone for the opposite ones for the last three times. It's good for zero. Oh, so seem zero is going well, of course, the reason why we should always Q zero is because the company, um, is always going to be the same. So we actually want toe. So just to explain that I noticed that every time I was going for one, I was losing every time I came for zero was winning. That's because it's literally just gone for 40 the top. So we want to include that in the while loop. So every time we guess the computer is also generating a new random number. That's why it's so so important to always test your your cade, make sure it's working properly. So if there's one more time, I hope you'll wait this time where now that the computer is generating, um, new number every time as well. Release that one with these again. We won that one. I'm just going for zero every time. So wow, So the computer's gone for zero quite of times as well. We got down to one now, and there we go. This time we lost. We lost the life our lives, left Missouri and said the game ended fantastic. The main thing is, keep in mind here is reimported random as always. But this time we did different syntax, he said from random, which is the module import dependent method for function. So then we gave our user user lives number five, which we didn't including the wildly because if you think about it, we put in here every time that we went back to the top user lives would always be reset to five. That's why we wanted outside of the wild leap. We then had the computer generator Breneman share 01 on views against 01 And then we had to . Basically, if statements say if they're the same, then we find if their difference them user loses a life and then right at the end was saying, If these lives equals Syria, then break. But if that wasn't true in the USA's third Lives in Khost, rate backups adopt. So I hope that makes sense to you, Andi. Hopefully you've created a game that's similar in some way, and it's working for you. I'll be interested to hear what kind of game we came up with. So feel free to to share that with me, and then I'll see you in the next lecture. 43. Functions: OK, now it's time to get on. Something really exciting is one of my favorite concepts, and that's functions. So if you've already done the JavaScript module before this, then you should be. This should just be some revision for you on. You know how much I enjoy functions. So functions go by the concept of dry. Don't repeat yourself. We don't want you to be repeating code over and over when you could just ball it it together into a function on, then call that function instead. So as a ways I prefer just to explain through showing you rather than just a were intellectually. So let's get started. I want Teoh create a program here that's like an AI like robot. The welcomes people as they come into the into the office on Let's say they're only supposed to welcome certain people. Eso Let's say we want to set up its name of this so we could say if so, Yeah, so if, um, this is a person coming in So a person entering is equal to Bill, then print. Hi there. How are you? Lovely morning. I hope you have a good day. Then let's say it's a l f person entering is Mindy and print. Hi there. How are you? Lovely morning. Hope you have a good state then. That's a person entering IHS George. Now for George. He's always in various. There's not time to say hi very quickly. So we can just say had I than Elliff person and touring is equal to you. That's a Mandy, then. Print. Hi. They are How, uh, you lovely morning. Hope you have good day. So there's a reason why I just had that incredibly boring thing because now you'll never forget it. So, essentially, what? There are two things here that, first of all, we don't have to write this out. I mean, it's kind of tedious Taft of Rights. These whole messages again and again and again, Um, the code doesn't look very nice. It is kind of things are being repeated all the time, so it's getting a bit messy. Also, there's room for mistakes as well. Let's just say we're not using copy and paste. And if there's always room to make mistakes this way, so now enters our hero functions in order to save the day so that we don't have to worry about this stuff anymore. Let's just have an else if it anyone else print what up anyway? So the way that we create a function is the first we put death D E F. That's how we say that we're going there were creating a function here in JavaScript. We would write function, and then we put in the name of the function and they would have print season that kind of stuff. But in Python's a bit different, we just write D e f for define. And then let's cool this welcome underscore function. You can see it's turned blue because it knows that we're writing a function here, then empty parentheses, and we can go over that in the next picture. But for now, which can leave those empty and no, we can say Prince, probably. So now, in this coat, another of Christ our function, we can replace all those lines simply like this. So this way Arcade is a lot more clean, no more succinct, and we're not repeating ourselves. So just just like that in here, and sometimes of course, there's going to be lows of different things, say then we could get maybe We want to get the input, How's your day? And then calculates equals day times, whatever it is. But essentially, you might have lows of different things they want to happen. So that would, really if you imagine not just having, you know, one line taken up but having 10 lines each time they wanted tapping each time, you can quickly see why functions would be so helpful. You write out all the things that you want to happen, and then you can just put put in the name of the function and cool it with the parentheses to execute here. So which probably give actually person entering family. Let's say George has just come in. Hello. Now let's try Mundy. It should now output, that's. And if we run this again, how's your day? Great. Awesome. So that's the starts of how we can use functions. So this is the syntax. We want to write death to say that we're creating a function. Then we use the name of the function. You can call it whatever you want, so long as just variables. You start with a letter, um, and then use the empty brackets code on, and then indented. You can put in as many lines of code as do you want a different past nine one plus hates. Then we talked for for I in range hundreds print I all these kind of things you can. You can just have all of it in the function. It could be as long as you like, and then we have to do in the actual code where you're actually calling it is just write the name of the function with open brackets, and that's it. So that's the beginnings of functions. I definitely definitely recommend. You have a goat playing around with these first before you move onto the next lecture, because when it comes to functions there used a with the time in light when when you're working in the real world of coding, there used absolutely everywhere. So it's really important to feel confident with the foundations of creating your functions , understanding why you're using functions based on the dry principle of don't repeat yourself and then so you can create function and then you can execute it in some code. So give that try, have a go, have some fun and then I see in the next picture 44. Function parameters: so moving onward functions, we're now going to be looking it parameters. So as are sharing. In the previous lecture, we create function by writing death, which stands for define and then your function name. They have these empty brackets, then colon and an indented underneath. We can just print function, working and then by in order to run the function, we just need to right in the name of the function than the empty brackets. Maybe it's working. Sometimes we actually want to include something in in, ah, these brackets here. We call them the parameters of the function that parameters. So essentially we put in. Let's just say the number one or something like that, right? It doesn't matter again what you call it. You can call this whatever you want, because it's not going to affect how the code works outside of this function. So then we could say Prince one case, this is really basic. Now, when we call it, we have to personally in here. If we don't, there's no going to say it's missing an argument one. So we need to put something in here. Let's put in Hi there. So what's gonna happen now is this is actually going to print Hi there. So it takes whatever isn't these parentheses, which is says the same. Is this on that? It just says to print it. So is gonna work. We can have more than one one permitted first K for seconds on score per room. So go to whatever we want that we can print that and then we can print within the string. This is the second for that. We need to put in a second for him. So now if we try and run this now again, it's going to say that we're missing an argument. That's because now we need to have two arguments. So you just put a comma. And then of the 2nd 1? Yes. So we were working then. This is the second. So you can imagine when we're creating the functions. These are just like when using in Ah, the dot format. These are just placeholders. This is the first place holder were saying Do this. So, Prince, the first place holder in the 2nd 1 bring that out Another basically, Zombo is if we wanted to have a multiplier where we could easily reuse it so we could dio Steph to say it's a function. Then we're going to call it multiplier and then the parentheses. Let's say two numbers. So first and sick standing for second, then we want to do. We could store this in a very boys who you say third equals first times, SEC print, fed some of that. It's no have created our function. Nothing's happened because it hasn't been executed yet. So in our code now we need to cool that function more to player. That means paying two numbers and then that's going dark. But whatever we want four times had, for example, now let's look at some large examples and just to mention, actually, we can have as many parameters we want. We had a bunch of them anything. Is that we and you don't have to actually use them in here. But you do have to include that many in here. So what? We've got 36 there, So 4566 two days after use or the parameters they can just sort of be there. There's no reason why you'd want to do that, but is you have to then enter that many when you call it. Otherwise, it's complains that you haven't given enough arguments, so let's just get that to be a little bit less messy. Okay, so let's go for two tomb or practical options. Let's go back to our ai greeter. So in this one we had that we wanted our AI greeter to greet some of the people with a certain message this time how about we have it so that you actually greets them by name so we could have a function so death we could just quite greeter because greeting people on, then in here we just have the name I'm not. So we could just But we could let you put this is I and then say, Prince, hi there, Placeholder, I e have a lovely day, then dot format I Of course you can do this and you can even put the function is a but then it's really hot light. It's not a very readable code, and you want you're going to be readable so that not only can you remember and understand what this function, because then, if you if you look like let's say have lows of code and you just see this being called you might think What is going on here? What is this, a function? It doesn't make any sense. So you want to make it is readable as possible. So greeter is good. And then instead of I we have name that makes the most sense right? Because then it's you're seeing that you're putting your name in there and when you're doing, the greater the news have a name. So now the one known we call it is gonna be so much easier We have to do Is 17 those all those ifs and else's we can just say Preto and then whoever the business. So Bill Hope Mito from that cell fest of you Well, Monday Oh, Mindy, it doesn't matter. But what would be able to do in just three lines of code is create this ai greeter. It's obviously not really I but let's just have fun with it. We've created a greeter here that can essentially just greet anyone ever now, even if that came in and he never comes in there, we get perfect. So let's think of one more. How about a bank? Let's say that my current balance is equal Teoh 100. And then we should create function that we could so we could withdraw money easily. So instead of each time having to be like, um, your current balance bias equals 10 and then we update to that way we can create function so deaf for define. And then that's cool. Our function. Withdraw. Yeah, I just withdraw is fine on. Then we won't have the amount coat on on the next line, which is indented. Who convinced a current balance minus equals amount? It was not have you got? So now let's just have a nice little message as well saying you have withdrawn so much. Your balance is so much then don't formats. So withdrawn is the amounts and then the current balance This put some cones in so that's nice and professional. So now whenever we want to do withdraw or we have to do is call that So you do withdraw, escape, attend, start with. Okay, so it's complaining him seeing local very well come by this reference for assignment. Okay, So we actually to include this in here in this important reason for that, because what we could do, actually, if we had it in the function. The problem would be that it would always be reset 200. So let's say we had as before. Oops. So we have out there now. No, only do we need to include the amounts we need to include the current balance, so we'll just put this as balance. So we're not writing the different the same names twice and getting confused and then balance. My seek was amount. So basically, the problem here is it saying in the function we don't have access. We can't see this variable because it's outside of the scope. So if you're trying to reference, what I was trying to do was I was trying to reference the kind of current balance in here, but it couldn't wait. When it's within the function, it it's finds it difficult that, say, access variables that's not in the function. So if you're doing that, it's important to input it here as another parameter. So now we want to include withdraw, and instead of just putting the amount of 100 we can put the actual variable on. That's the way the two function can access variables that you're outside. So that's run this than this. So you have withdrawn 10 your balances. 19. That's a drummer. 50. This court should go down to 40 now. Interesting. So the problem we're having now is that we have the current balance variable, and we weren't able to reference it in the function without putting it as a parameter. So now we're putting it in as a parameter, and we're saying balance minus equals amount. But the same thing happens again. When you're in a function, you can't then just magically change the original variable. Which brings me to the final pots very nicely, which is using return, which you may have may remember us using in javascript When you say return it saying that you can actually get a value from this function now. So I want to return balance. So then thinks this value is variable can now be accessed outside of the function. So why do you know? So put current balance equals withdraw, Boba. So what I'm saying equals well, we have to think about is that it equals were at it whatever its returning at the end. So now for run it, he was drawn 50. Your chances of hundreds and Of course, I'm gonna change this current balanced actually balance because that's the parameter being put in, says Gay. For it from 100 to draw. 30 goes on 17 this withdraw 20 and sugar down to 50. Draw 45. No go down to oh heart for under 50 were now in in a negative. Okay, so we're going to go over each one of these at a time. So first we went over how we can create a basic function, including two parameters, and you can consider these as placeholders. So you're saying when we call the function, we're going to give to bits of information or you're going to receive two bits information with the first piece of information. Do this vets with second piece of information, Do this with it, and then when you call the function, wherever you do in your code, you need to include the two positional arguments. So this would actors one so it print off one, and then for the 2nd 1 would be formatted into this sentence, which, as you can see, there's worked fine. Just there. Then I used the example of using this to multiply numbers together and Then we went on Teoh , talk about this AI creator where instead of having to use loads and loads of lines of code , we could use a function where we have a parameter for the name. So we have the place older. And then we could just say hi there and then former 10 who have his name. It is at the time. And that got a nice greeting saying Hi there, Zack. I have a lovely day. Then finally and this might have been the confusing part for many of you. This is where we enter your a few different concepts, including scope Some school right this out here, scoop return. So with scape, what's really important toe to understand is that let's say, for example, in this function here, we can't just put print. So, for example, we couldn't dio current balance plus equals three. We can't change variables that exist outside of the function unless we put them in as a as a parameter. So what we did is we put the parameters balance. And then when we called the function, we imported the variable as one of the parameters. Now, also, we wanted Teoh not only access this, but we wanted to change it. And so, in order to do that, change the variable, we had to then return balance. So we're returning a value which has been changed in here. So we take in the original balance and then we change it and then returning it. Now in order to access that, we just put current balance equals and in the function Sophie, that will make sense for scope in return with scope. You have local on global, so if you're looking to access and change a variable outside of the function you're using global and there is the key word global to use, but it's kind of round upon. It's no great practice, so we're going toe. Can't miss that out. But those the two types of scope. So when, for example, I want if I want to change balance here, that's that's doing it locally rather than globally trying to do it through a function. So then we went over return, which is where, in a function you can include this return on using that you can then access the variables from with in a function by doing something like, Yeah, we could just get a equals on the function. If you could just put that was some of that. So then we would have this variable a outside the function to use, so hopefully I will make sense. If not, don't worry too much. What's really important here is that you're able to understand the use of parameters on how they kind of act like placeholders within the functions. Once you're able to do that and you're feeling confident, then you can look at considering how you can include return in. Some of your functions will be using that a lot of machine learning on will be going over it. Then again, So don't worry too much if you don't really understand it. But give your best shot, have a goat, creating some functions with parameters and trying use. Return if you can, and when you're ready, I'll see in the next lecture 45. Recursive functions: so I know the last three lectures have been filled with quite a lot of concepts to do with functions. Hopefully, you had to go yourself and just getting practical and trying to create them yourself. But I wanted to add in a lecture here there was a bit mawr lighthearted, not too much intense brainpower required. But we're still getting respecting using functions and parameters. So we're going to be looking at something called recursive functions, which have functions that can call themselves. So let me just show you what I mean. So we created function like we normally do with D E F Death which turns for define. And then we want to give our function name, say, this guy for re curse folk. And then we can leave those brackets empty for now and then a colon and then indented here is all the code that we want to run. So we could say, for example, print. So let's say X is equal to one Prince Hex. Well, exes on, then. Curly brackets in here dot format and x No, we do some like X plus equals one. So right now all that's gonna happen if we call this? Here's not much talk. To be honest, it's just going to say X socially. Rico's folk. What was going to do? You say X is one, and then we call it. So if you don't put the parentheses, nothing's gonna happen at all. It's just gonna caught with this strange thing. The X is one. That's because it's only happening once. But if we were to then call the function again within the function, so that seems a bit strange, but it does work. So what's gonna happen is we're going to call this function X is going to be equal to one. And thence against Dio X is one. An explosive was one. And the problem is, what's gonna happen again is because we call this function again. X is going to be equal to one. So we're going to get his exes won many, many times over. So, yeah, as you can see, it goes up control quite quickly because the it's like the infinity sign like their snake heating itself. So it's the function is just calling itself over and over and over on Until I stop this from running is just gonna keep calling itself from. So now what we want to do is we want have X actually going up. So instead, what we're gonna do is we're going to use a parameter which is going to put number in here . And then we are going to change hoops. It's changed that here. Who's that's try again the gate, and then we'll recall it. Him, then when we start, will just start with one. So we're going to call this and it's going toe number is going to be one says go print. Exe is one. Then the number is going toe We're gonna add one onto the number says go become to And then we called the recursive function. Now with the number two, let us go prints exes to And then we're gonna add one on Sanoma Scary three. And then we're gonna call the compunction again with the numbers three. So now it's gonna go up nicely. So if I run this and then this every day, as you can see, it's going up very quickly, So I've got to go upto one of the moment Now we're already up Teoh 2954 so and they stopped itself because, yep, maximum Rick Ocean. So it's realizing that I'm using recursive function here, and it's just saying, Yeah, we're not going to go any further because it's a waste of time, basically, so that's recursive functions. So you had a nice opportunity here to practice. Crazy are functions by using death than the function name and then the brackets. We've included a permitting here, which we can think of as a placeholder, then within our block of code for what we want to happen when we call the function, we've been able to use that place holder or the parameter to do stuff. So we're saying, Print off that number, change the number. And then we had the fun job off recording the same function with the number that's now slightly changed that recursive functions. It's not necessarily something you're going to come across every day is an interesting concept, and it's a good way for us to practice using our functions. So that's it for this lecture. Have a go yourself creating recursive function. Have some fun, and I'll see in the next lecture 46. Practical - Tic tac toe: Okay, time for a nest. Next. Practical. This may sound easy, but it's actually quite a big practical. We want to create our in game of tick tack toe or, as we call in England, noughts and crosses. So if you don't know what that is, you can just a quick look at it by searching for it. Tick tack to this came him. Well, you do X and zeros to try and get three in a row. That's what we're going to be creating here. So because it's quite a big thing, I've decided to split into three parts in the first part. All I want you to try and do is create a board for tick tack toe that looks something like this. You use the pipe character to create that. So when you two first of all creative board and as a hints, you can use back slash N with the quotation marks around it to print on a new line, well, you could just print each three using a separate print function. It's your choice. So after that, I want to warn you to make it so the user can enter a space between one and nine. So 123456789 and an ex will appear in that spot, and I want you to do it. So then the next user can do the same and an O will appear there. So you have a guy doing that, give your best shot and then together we'll go over it in the next lecture. 47. Solutions: Okay, so hopefully you've had a good goal. This practical yourself don't more if you struggled. This is definitely one of the harder practical is we've done in this course so far because it requires a number of different moving pieces and also takes a bit of four thoughts thinking of Okay, so we created board. But then how can we change the X's and the O's and stuff like that? So if you took a look at the problem and you had a quick gay and you felt kind of overwhelmed, you know this is too tricky. What I recommend you do is pause this video again and just give it a few more tries. It's not essential at this point that you are able to do this whole thing by yourself, but it's very, very important that you give it a go to the point of which is starting to feel frustrated because and even if that's very soon, give it a couple of days and just take breaks in between because what's really helpful at this point and when you're learning to code, is problem solving and coming up with things that are not sure how to solve and thinking about what tools you have. Toe, hand. What? What parts of python you know already? Because you already know the concepts required for this. But it's doing it in quite a interesting way. Let's say so. If you haven't really given it much of a go yet. Do you think you could try a bit more, please? Do you go ahead and pause the video. Otherwise, if you have really persevered and you Reiter, go on with me and this or you've completed yourself. Well, congratulations. That's awesome. Let's go over how I went. Went about solving this problem. The part one So first he wants to do is to create a board for tech Tyto. Okay, cool. So we could just do something like this, right? We could d space pipes based fight, and then backslash and then space, right? Space pipe space. It s in space pipe space, pipe space. And that would look something like it. Maybe I'm using the wrong and here they both seem toe included like this. Then if I bring this, that's kind by the boards. Okay, that's cool. But then what happens when we want to change some of these things would. We would just go in here and buy that, for example, where we could. But if that's the best, make weakened it. What I think would be a really good way when we start toe make changes based on the user's choice is we could use lists. So we use a list to store all the values of each of the spaces on. Then we could change update that list. When the user chooses an X or no, the first thing we can do we can create ourselves aboard. And in this board, we want to have just blank spaces for now. So it's not gonna be X or always got blank spaces on. We're gonna have a list of nine elements, and they're all just going to be blank spaces. So three prints off his board. Now we'll see. We just have these blank spaces. Now I'm going to create a few rows cloned, separate these into three race. I'm going to everyone equals board zero, and then I could do plus pipe class, etcetera or accused adopt formats where I just say what equates curly brackets, fight curly brackets, pipe cutie brackets, then don't format and I could do boards zero come aboard one and bought to. And then we can do the same thing for Rose to hopes two and three. So we just change these and b three for five, six, seven. So we do have a elements because our 1st 1 is of Index zero and then we could say Prince, right on school one then plus stash and plus very to us slash and us three. There we go. So now we have a board where you can actually you have all the values in one place, which is the board. And then we have been rose and printing other rose. So there are different ways of solving this problem. This isn't the only way. I just found that this is a nice, succinct and effective way to create your tic tac toe board. So don't worry. If you came up with another way, I'm sure it's fine. I've just decided to go for this way. Some scary move this cause bit messy. So now let's just have a play around. So let's say we want board p three bqool two x So there we There we go now on the on the three index. So which is the fourth element that's being there? Let's make it so that the 4th 1 and then we can change the 5th 1 Hey, say we've never been able to have three X isn't all right, so we have a board that's a really good first step. So the next thing we want to do is if the user tries to enter a space that's already taken overnight. So using an interest based between 19 and X will appear in that spot. Okay, says a couple of things we want to do here. The first thing I wanna do is include this in a wild leap so that the user doesn't have to run the program each time to change what excess. So first of all, it's going to include these on the same line. So let's do while one is equal to one, because we wanted to keep going until we decide to break. So I'm just using a condition here. That's always gonna be true. So while one is equal to one, let's go for, uh, ex use. Ah equals impact. Choose your space between one and nine and when the cost that to an interview. We needed to be a number then. One thing we also need to do because they're gonna be choosing between one night we could say zero Nate zero and eight, but just doesn't sound as good because of the index's. So what we want to make sure is that we're gonna have the numbers between zero Nate. So I'm just gonna dio use X underscore. User minus equals one. Because whatever they choose to do more than nine, we need to reduce that by one to fit in with the indexes so we can have a go this and then let's also create ourselves a function which is called Draw board, and we can put the board as our parameter and then we can have this. Every time we call this, the board is going to be drawn because the board's gonna get updated. We're just putting this right at the start, so we'd have to worry about that. Still, we even put on a separate line, so it's more obvious. We're just having that starts when we run the program. Just once. Then we were separating along this into a function. So then down here. We could just dio true aboard or function. And then what? So first thing wants to do is update the board. So now we can say, for example, boards. And then the index, which is going to be X underscore User will be the right number is equal T X. So let's give this a try. Choose. Based on one of nine. We came here for one. Okay, We've already come up against our first ERA draw board. Okay, I think I just literally need to run this code. Excellent. Still saying drove orders not to find interesting. So it's saying that draw board, okay? Of course. We tried death of the start to say that we're creating function. Try again. Okay, so we've already run in those excess perforce. That's fine. Open a T three. Okay, So second, get, Let's get I'm happy that we can stop there. We start the colonel. Sometimes it just legs, which is fine. Okay, so we've no made it so that we have created a board. Look, something like that. So let's put a nice that tick. I live here very happy that using an interest base between one and nine. Yep. done that. Excellent things they can get. User can do the same and an over appear OK, so we want to give the other user a chance. Otherwise, it's just not fair. So now let's go for doing the exact same things we've done here. What I'm gonna do you just to make things simple, is just copy paste that and change all the actually, Sir Teoh. Oh, user hands, Then update that toe. That's what we need to do. So I'm gonna slate that, then holding on control almost against, like this. Hold down control against like this. I can change them away at the same time, Which is nice Little gimmick on. Then I'll change this toe. So let's start from the top again. Start from drawing the board from nothing. Okay, if you just based on actually would like to do is no, that's fine. Okay, let's go for one. Okay. Say X has gone into one. Let's go for to you So those next to it. Okay, so we've forgotten to put draw board in as well for the let's change that it's try again. It's been mined. Restart the kind of not playing ball with us today. And also one thing I was thinking doing as well, which I would be now is drawing the board right to the top. So it's just It just looks a bit better that way. Hoops. Maybe that's better. No, it's try running again. Name draw Board is not defined. Should be. Oh, so the board here. Okay, So means you run this first. Okay? So draw on the board of the top basket and that's his to your space between one of nine. Good one. And then skate for two for the chaos taken. Good. Now, although it's calling that twice. So I'm going to go one step further and for that outside the world leap. So it just happens before the while loop and then only gets called once so we don't get it repeated like that. It's good for three. You've got X for which again. Okay, that's looking good. I'm quite happy that and just because doesn't like it's being friendly. Okay, so now we can take off when the user does the same again because the so it looks like we've managed to accomplish the first part, been able to draw board. We've been ableto enter spaces for X and A so it's like a war done. First of all, I made the decision that for all of the different squares, I wanted to keep those values in a list start start off as them just being blank space because they're neither X. Nor is so I created list. That's nine long, and you can you can use the multiplication here. I could have, of course, just gone this. I could have even used this convention, but this is a nisi way to do it. Then I decided I eventually ended up paying up into the function by decided to them, put them into Rose would have placeholders each one of the spaces, and I'll be putting in different indexes for each road and then printing them all off row one roti Row three with line breaks. And so that's how we're able to drop our board. That was a success. After that. We went over how we can then allow users to make choices and add excess knows. So we started off with a while. Loop had a condition that would always, always gone forever, so we have an infinite loop going on until we decide to break it. Within this world leap, we start off with getting the input from the user as an integer saying She's a space and then we we took one away from that because we asked them to do 1 to 9 rather than 0 to 8, because 1 to 9 teams cooler. But we wanted it to go from 0 to 8. So u minus one so we could have it between zero and eight, which are their numbers for the indexes of our list. Then when we had that number, we basically updated that index to be ex simple is that And then we use our function to draw the board and to show the user. Then we did the exact same thing for the user, but with those instead of exits. So the experts and goes first, then the person, and it goes right back to the top of the while loop. So the excuse Ercan go again And it just kept going like that. So I think has done a good job for the first part. If that will make sense to you, I highly recommend you give this a go. Now try and make this happen for yourself. I know when you ready will go into Part two, where the user is trying to enter space that's already taken and they're not allowed. So, for example, if the first one's already taken here, then how we make it so that if the O user tries to go there, we say that's not possible. How do we do? Have a think about that? And then also, we want to say If the user gets through in the growth of the game obviously ends and the winner is announced. So would like you to do is have a go at solving that yourself now that you're able to draw board and then once you've had a good goat, this we can look at the solution together. 48. Solutions part 2: so well done for making it through the first part of this practical. Hopefully, you've been able Teoh, get some confidence in creating a board for tick tack toe on your able to enter space history one of nine x and voted to appear. So now we're going to be looking into Part two, where we wanted that. We want to make it so that if the user tries to you to enter a space with Xolo that's already taken there, we print something saying, Yeah, they're not allowed to They have to try again. So hopefully you're also noticed that breaking down the problem into these little points these separate tasks make it a lot easier and a lot less overwhelming to try and then go step by step through. So let's get started with this, but they use it, tries to enter space that's already taken. So one thing we could try, at least we could say OK, so it's been just here, So the user's ended in the space they want to go into you on. We want to check to see if that's based. That they've chosen is already taken or not. So we want to use an if statement hip so we could say if, let's say, um the board, let's say the board space. So we put in I x underscore user, we could say if that's already equal to X, for Of course, we want to check in. It equals oas. Well, been bored. Excuse he was They won't say. You can print out something like space already taken. Try again. So what would happen here when we could actually make this even smaller and steps A is equal to X week One day we could just say if it's not equal to white space, so they see that it isn't if it's not just a blank space of the time so we could then put, for example, or continue making So then, Then what would happen usually might remember to continue is that basically makes makes it go right back up to the top of the loop that is in. So that would be awesome. So it'd be like a case of the space has taken. Okay, try again. It goes back here, and user can try and enter in this again. However, what happens if we wanted the O users to be able to the same Well, that would be problematic because even if, for example, we were Teoh But this in down here, we replaced this with Oh, user. When we did the continued, it would go straight back up to the excuse of having a 10. So this wouldn't work this strategy for both X and your user. So actually, what we want to do is create separate function that can check this for us on. Then we can basically say, you know, if if it's taken, then just run this function again. Else you do do other things may change, change the board and what not. So let's give that a try. So going to create a different function here on, we are going to call this, let's see using move and what he wants. But in it where we want to put the board in because we want to be able to change the boards like we've been doing here because if we don't put it in as one of the parameters, then we can't access it, and then we want to put in which user it is so we could put in user type or something. that that's gonna be whether it's X or so. This way, we can use this function for both X and and at the same time without having to create two functions one franks and one for you. So it's just a nice way of keeping our codes shorter and timber. Okay, so the first thing you want to do is we want to get the user to put their input so we can just take these lines of code put on here. Was there recording it? Actually, sir, this is quote user choice. Maybe India use a choice. A good, very bold. Call it and save using this line Now that we know what it's doing, Just minus in one. I'm gonna put minus one up here, so it's less lines of code. So then, at this point, then we could do something like this with our if statement. So let's go for if board, but this index. So let's say, for example, uses selected one that's going to be zero. So if board index zero, that's going to be basically the top left space is not equal to white space. And what do you want to do? What we want to print out space is already taken. By then, we could put in the the actual who weather is X or not, but that's not really that necessary at this point. Let's not complicate things. Let's just say space is taken. Try again and then underneath we have to do is call the function again. So you might remember we went over a cursive functions. This is what we're doing here. So we're going to use the same board, of course. And then we're gonna use the exact same use type. Okay, we can say else that's basically if it's is still white space. So if it's a valid move, then we can just say or redact anything. We can just change the board so we can say boards use a choice, he calls. Then used type that could be xolo. Okay, so let's let's give this a try and see out, guys. First of all, let's tidy this up so we don't need any of this anymore. You make sure we have a lot of it. What we want to do now is we want Teoh call the function, So users underscored me using the board on. We want to have it X That would probably want to draw the board out like with like we've always been doing. And then we can do the exact same thing. That's it. Just here. As you can see, with dressing drastically reduced the line of code metes this time, we're going to have it for you. Okay, let's give this a try. And also, I'm gonna put the board down here because I might forget to run that cell sometimes. I mean, we want to reset it to blank every time. Okay, let's give a try. So we're going for one for X two. Good for X spaces. Taken. Try again. Let's see what happens when we go for another move. Okay, great. So let's put in 440 no. Get four. X space is taken. Try against will put five francs. So that seems to be working just fine. Excellent. Now it's just restart the kind of Okay, so that's a good I reckon what we can do is take this one off now. So just to go over what I've done here, essentially with remove taken out most of the code that we knew that we were using in here and put it into a separate function because we wanted to be able to check when the use and when user makes their choice of where they want to go. We wanted to check if that if that choice on the boards was still blank or not. We're saying, If it's not blank, then just call the function again and that will keep going off busy until it's we get to the else Where is blank It's going to say OK, we'll change that that boards base into X or whichever one's being called. So the parameter second parameter where we called it is either going to be next. Okay, so now let's go into the second. By the problem, If the user gets through in a row, the game ends and the winner is announced. Okay, so this is slightly tricky, but definitely, definitely double so, essentially what we want to look at. That's just you. Look at one one example for now, and let's say that's when X is equal to 01 and two. So basically, when it's when it's covering the top, the top line basically three spaces on the top line like this. So what? Less thirsty. This is a different function. And that's called this check win for the boarding there. Onda. Let's look at the yet. That should be fun. So we can say, actually, I spend the user type is well, because we're only read again to need a check to check it after a specific user type has been used. Um, we call this Xolo because just so that we don't get confused, thanks to us. Yeah, this is basically gonna be X or Okay, so we can say if boards Siri is equality X, andi 41 is equal t score. Then just copy and paste that. Who's so now? What? What could we do here? So if in the f statement. So we basically want to break out of this world, Look, don't we? We can have a print down here saying End of the game or something, but we basically want to break out the wilding. That's what's really important. How do we do that from inside the function? Well, essentially, the way that we do it is we instead of having a condition like this, which is always going to be true because we can't say in here. If we say break in here, then that's not going to break out of the wild leap. Unfortunately, so what we have to do is we have to change variable in, in in this wild leaps somewhere there's going to be going to make this condition force. So what I'm going to do is I'm going to create a variable at the top. Coldplay go set to True. You can obviously call Call this variable whatever you like, but it makes sense to court play. And then I can say, Well, play is equal to tree. Do you do this game? And then I'm just going to put okay he caused in the function. Then the function. I'm going to return something cool play, so I'm going to say so if this is if this is correct, then we don't want to put play to be dream or so we're going to say play, he calls. First we could just say house play equals true, So if that makes sense, what we're doing is we're saying if the top row is equal, Teoh, just one of either xolo that's going to put into the function, then plays equal toe force. Otherwise keep playing because we won't play to be with false because the game's over, because someone's one. So what we're going to do is we're going to actually this but this this But this for now, just to keep it, not to complete, that's just a different, a different function underneath. So we're going to put play his equals. And then we got to check win and we had to put on the board and we're going to check for X and then underneath this one, reduce again. We're going to put it in for now. One thing we have to make sure is that if you think this through, when we do the check win here, the players going to going to equal false right, but the condition doesn't get checked until we go through this whole whole part of the code . So whilst this goes down, we're going to go to hear play calls, check, win board. And when we get to hear, it's gonna be looking for whether O is equal toward these and that's not gonna be true. So players, they're gonna be true. It's what we use Dio. It's just here. We're gonna put We want to see if this if players now we could force here. We don't have to go through all this again. So desperate. An if statement here and say we're going to use continue. So there's a If play is equal to force, then continue. So it continues. You might remember, basically says, Ignore any code below and go back up to the top of the loop. So that's keeping with this a try. So we go. We want X torch by Orry. Occupy the top space. Ah ha! Seriously, What's happened here? So it seems to have already decided that is the end of the game. So which shouldn't be correct. So, basically is being sent to force too early. So what's happening here? Let's see. We running the check win with X. So we're saying if Bordeaux is equal to X onboard, one is equal. Takes on board music, rejects, place it with force. So we're not We should actually be returning force to start play. So I think that's part of what the promises that we weren't actually saying. Return. So Skip Troy one for okay, this looks like it's working better now. So when I put in three, what should happen is that the loop is gonna break. So nobody asked D'Antoni. More space is going to get this nice print at the end. Say End of game, End of game. Fantastic. So it seems like that's that's working well. So I think we can take off if that if the user gets two in a row, actually, let's not to give up just yet because this is only one condition, right? We've got loads of other things that could happen. So, for example, if there are equal to the second or third rate, and this is gonna be a little bit tedious, But let's go for anyway. So we're saying, basically, if there's one thing that could happen and then we also want to have it the same, I'm just writing out like this in this in tax. No, because that's how it has to be. You could do it in one long line if you want, but this just makes it a bit more readable. So let's go for the next line, which could be three for five six seven nine. A case that's the horizontal is covered now we need to cover the vesicles, I say, Let's go for those. Oops. One more. So the verticals will be one for believe. And seven and three hopes three. They took 25 and eight. And finally 36 and nine. That's what the verticals covered. Now, now we just need to cover the personals. That would be one, five and nine. I'm sorry. That would be zero There. 1234 The nine villa would be 012 You should actually eight rather known will go back over that second. So that's a to And then it would be We're 123 and six, right? I'm just going to go. These major of the numbers are correct. I just had to update. A few of these numbers are getting indexes confused. Obviously, there wouldn't even be a nine. So change those two eights. So if you were following along with me, then just maybe pause the video and update these. So they're correct. Essentially, these are all the horizontal ones which were fined 012 345678 not nine at the end. Then the vertical ones will be zero. And then If you think of the next one down below that, that will be 30123 then six. And then you, for the other ones, is gonna be one. And then you said after had three onto each of these the next one's going to at threw into these And these are the two diagonal ones. Zero for eight. So going from top top left to bottom right than 246 is essentially going to be going top right to bottom left. So that's checking all the possible ways. That song could win now. So this give extra, so goes. So let's go for a diagonal for so exc unjust guy here. So I will go top right to bottom left said. Actually, 1212 Oh, so yet so three and then is put exit for five, six and six x isn't doing too well. So now we just need to put another seven, and that should have completed. But it hasn't. Let's look as to why that may be okay. Oh, I see this hasn't actually run because it's no happy with syntax. I'm assuming that's because I haven't put a coat on him so if we just restart the candle again, Okay, this time when we run, it should work a bit nicer. Se Teoh check Win does not find. Okay, let's see what happened this time, Student. Uh, of course. Okay, so it's no happy this intact like this. So that's just turn this into a love line, so oppose the video. And then I'll come back to you when this is in one line. Okay? So basically all have done here is I've taken all those conditions and with one toe, one line I've been gained to Usti using JavaScript waken. So play around with a bit more. That's fine. So I just run that. Now let's give this a try. Say strike and say excessive here. So he wants 02 b going top left to bottom. Right? So they go three five and then get four, for example. Then it's put nine for Okay. Fantastic. We had three in a row. Your top after born right says the end of the game. Fantastic. So we can take the self as well. I think we're making great progress now, So just to go over what? What we've done in this in this practical. So essentially what we've done is first we're looking it, having it so that if a user enter, the space is already taken, they're not. I would never try again. So what we did is removed a lot of our code into this function here, where we basically put in this you have user choice stuff into function. So we put in the board. So all the parameters and then the users types, whether it's excellent and we're saying if that space is is isn't blank. So if the space is taken, then just called a function again. So we just keep calling. You keep going around around until the user chooses a space that isn't taken, at which point that that board space will then be changed to either X or depending on which users choosing. Then we added that in here, So we basically just ableto put in these nice little function cause on then, finally, we wanted t o see if these had gone three in a row, and if so, we wanted the game to end. So you put in all these potential conditions. So you know these ones here saying If the top row. They're all equal Teoh, whichever one being called. So if it's X are being given that we did that for the horizontal norther, the two diagnosed as well. And then we're saying if that if that happens, then we won't play to be would to false Otherwise we won't Plato State which truth on return that value Because in our while loop Now we're saying, if play well, players equals true. We want this to keep going and then if plays he would force when we're doing this for X, just go straight back up to the condition and then if it so then it'll go up to condition at the end of that one as well. So we've gone over in this sector. So what I want you to do now you see whether you can find a way to take this game because we have now working games tick, tack toe. So you should be very happy if you have been able to go this as well. Now what we wanted to be able to do is essentially create this game that you can either play with a friend like that have been having it here you know, one person enters, and so if x one person into something for or coming Dopfer against the computer, so that's gonna be interesting on then. You want the computer to be able to make its own moves. Now, I'm not talking about a I or or anything like that where it's making smart moves. We just wanted to build toe move into unoccupied space. So it's not gonna be a crazy, intelligent game. There's not gonna be too challenging on. You can definitely definitely do this with the contents of already talked talky. So give it a go yourself and then, when you're ready, will go over the solutions in the next lecture. 49. Solutions part 3: in the previous lecture, we were able to get through Part two of this practical. So now we have a program that where we have a board for tick tack toe, the user can enter spaces so that they are either an X or no appears in the space that they decide on on, then in part to it. We had it so that when the user enter space, if that space is already taken, they're told about that. They have another try to try again. They could do that for as many times as they like. And then if the user gets through in a road, the game ends and the winner is announced. So now we want to look at how we can have it so the user can choose play against a friend using the same keyboard so one person decides X one person size or they play against the computer. We don't want to create any, for example, artificial intelligence, where we have this super smart computer making good choices. It can be completely random, but it has to be correct, and we want to design so the computer can make its own moves essentially so Let's take a look at what we've done so far, essentially were able to keep the code quite succinct. In the main part of our program, we're basically saying, while play is equally true with the user could make making me for X Weaken. You can check if someone's one using our two functions using move and check win then if the players ever equal to force the wallet breaks and the game ends. So I two fortunes here we got using move here and then we've got our check win just here, and we don't want to have that. So now we want to look at how we can actually have the computer plane. So let's have a look as to how we can have this work. So, first of all, we wanted to be something that the cheese you could choose a console ECT. We don't have that in the wildly because we don't want it to be. Every time they've made a move, they choose whether it's computer or player. So I put it outside the world just here on. We'll just say, let's say computer come for human equals, that we have an impact we can just say, Would you like to play against a friend or the computer and then in brackets? Because we won't tell them what they need to impact. That's just put C or B. So see or see your f because could be friends or computer will put computer first because then there goes in order. Better your friend. Okay, Course that then we can have it here where? When it comes. So let's just assume that the first person to move is always gonna be the human on the second person to be really be their friend or the computer. So just here, before we have to use and move, I'm going to say if come Oh, human is equal. Teoh, that's a human or friendless e this quick, this quote Come for friend. Keep people in line with each other. Change that friend. So if your friend is equal to F, which is different, then we want to use that. We want to use a move so we won't happen. Yeah, whatever happens, usually we don't include the check win because we want to have that. Even if their computer moves as well. Then we could say, LF your friend is equal to see for computer. This is where we want something to happen. Where the computer mixing me. I think we should make a separate function for this. I just belay using leave. Gonna create another function. Deaf score. We're gonna have the same thing. So we have the board and the use of type, which is always going to be over here, so we could even just remove that. But just in case that say, we wanted to one day change it so the computer could be Xolo. We'll just keep it here. Such clean eso now in here century. We want the computer to somehow be able to find which which space is still available, and then choose well, those at random to change in tow. Well, the user type. So first you want to do is for actually, we can just put this out here. So the first thing computer finds available Spaces, then the next he wants do is computer correct? One of these? That's Brendan. If every thing Wednesday weaken computer changes the board space with he's that type. Okay, It's the third thing I want to do is to find available spaces and I think the best option that is for us to create a new list here at the top. We just call this available spaces. We can update this because what's good about having a list like this? That we can update it as people make new moves using the built in functions and methods that we have for lists. So the available spaces to start off where they're gonna be all the spaces, so won t 3456789 And that that's how I would put it if it was the user choice. But because we're gonna be using the indexes, we should probably start with zero so things don't get too confusing. 012345678 Diesel the available spaces. So now what we want to do is essentially have it. So the computer confined these available spaces. But first we want to make sure this gets updated any time a move is made, its let's start with having this happen in the user move. So at this point here, once once. So basically here you might remember we've got taking the boards and then we're changing the index of one of them because our board is one long list and we change it to either X or so that point is no longer going to be available. So what we should do is take this user what user choice and we want. Teoh so wants to remove from the available spaces, so we're going to use the method so we got available spaces don't remove and then which want to remove that number. So because we're using indexes, you might remember that we're minus ing one here, so that's going to be the user choice. That's perfect. So we're just going to remove. I thought you use a choice. So what's gonna happen now is that when the user takes up this space, we're then going to remove this one from our less cool, available spaces. So let's say, for example, the user chooses the top left, which is going to be index zero. That's then going to zero. Is there going to be removed from this list? So then the computer can only choose from these ones that are left over case, hoping that makes sense so no, so you can remove that the next thing is the computer selection on these random. So for this, we want to use the random module. Is we write. My remember have used force a import random here at the top and in here we're going to say computer choice equals random. And then you might remember remember the method random dot choice on this basically takes one choice or one element from a list. And so it was Choose one of these numbers at random. So we've been neighbors. Lister available spaces. So we now have one of the choices around a fantastic. And now we want to change the board space with this year's type. So we have to do is just like we had here. So we had board use a choice. We just basically, for that in here, instead of using user choice, we just change it to computer choice. Come, Futre Truth. So the board at this index is going to be changed to whatever that is. So either an X ray make a simple is that we now have a new function. So what we want to do down here his work or if the so l f the combo forensic would to see where they want to call this fortune so I can't move and then within here we want to include the board and we want to have It has fantastic one thing we need to make sure we do here because we also if the computer tape takes new space, we still need to update the list. So me to remove whatever choice the computer made. So then, which can change this here, whatever the computer choice was. Okay, let's have a go. Let's just make sure first that we can do user against user. We judge play against you can be your friend. Let's go against different. So here, for one macaques, like it's working fine. Three. Okay, next we're going to do is five sets of wind vertically. Six. It's I can't, so Oh, God. Three in a row. So at the end of the game, Okay, so it looks like it's working fine for friend. Now, let's look, look into what happens if we try and have the computes play. Okay, so I'm going to put in one, so I'm playing his ex, okay. And as you can see, the computers put one in that. Let's go for two cases you can see here the computers. Now put in another. I'm gonna get for eight to random one. Okay? And then it's going for three. Well, what is going for 70 even. Okay, so the computer has gone three. Andre says end of the game. Fantastic. So that's also have in our check win, I think bigger. Be a good idea. Toe Teoh Also print. So we want to know who won. Basically. So we can just say Ray, then place Order has one the dot formats. Basically, we can just put in whatever we have here because that's gonna be an X or no. So the X underscore groups. Okay, so now we basically have everything we need for two Tyto were, And I'm just gonna take this off first. So the use is able to choose if it's a friend or the computer on, then the computer has been able to make it so moves. So there was take off all of the duties, but war three parts. So congratulations for getting through this with me. What? Let's just go over what we've done. Step by step. So first. Well, we created an empty board this is where the nine spaces are, with just blanks blank space for now. We then created a function called Drawer Board, where we're able to print out all of the different all of the different spaces in the formats. That kind of makes it look like it's to take toe. So when you first went off like we have down here, it is empty. It first stop sent it first, and then each time we run, the function gets updated based on what indexes of this initial board have been taken up. So the next thing we did, we created functions for for the user to move where we ended in the board so so we can update the board within the function. And then we also put in the used type. Whether it's X ray, we had the user choose which base to use, and then we had, if it if that space is already taken. So we're saying if the index that they've chosen within this board that we created the top is no is no y space anymore has been used as Exxaro. Then we just call the function against the use asked to choose again. Otherwise we update the board accordingly, and then we remove it from our available spaces list. And then we create self auction for to check if it's with someone's one or not. So basically, we've looked at all the different possibilities. So, for example, here we've got if the top row we walked to the same user type Exxaro, and then we added the oars in between to look all the different ones. And then we said, If that happens, fake was force End the game because of a while. Loop is based on while players equal to truth. And then we can print out who won, and that's essentially what we've been able to do. So like I say, this hasn't been the easiest. Practical. If you've been able to keep up with the concept, congratulations. That's absolutely awesome. There are a number of different, really important concept here that could be applied to so many different areas of coding. So I really do you recommend now that we've been through this together, you have a goat creating this yourself, and it's not essential that you come up with the exact same solution as I have, because there are so many ways in which you can program this so you can do it your own way . And you can refer to what I'm doing here for a few hints on the best way to go because you know that this way works. It's relatively succinct, but definitely have a go yourself. Make sure you give this a try, even if you find it frustrating and difficult. Definitely, definitely. Give it your best shot. So hopefully this has been helpful to U. S. A. Have a go yourself and I'll see you in the next nature. 50. Requests: So congratulations on making it through that recent practical I know it wasn't the easiest off the practical we've done, but this is a real milestone moment because you have essentially gone over most of the basic and now coming into the intermediate contents of python and coding in general. So congratulations we can now get Galati stuck in some really exciting and interesting parts of python. So this is one of my favorite parts to teach where we can actually start to take all those little bits and pieces we've been learning on start to use them in some ways that you used professionally very often on. This is just one example. So there's a module, your notice. We've imported things before. For example, there is a built in one's. For example, random would have been able to get random images. Now we're going to something called the Requests Library, which is a really interesting library that could be used for hate to tp requests. And so, with hasty book tasty to be requests, most often you see people using gets on post there are other ones, but these the main to ones that he used so basically, when you think patient to be? You can think of you RL's so you know, for example, sometimes you type in Haiti. T p colon slash slash www dot etcetera, and that's what this is referring. Teoh. So what you can now use python to do with this request library is you can use it to get information from websites for AP eyes. We'll be talking about AP eyes more later on in this lecture, or you can post, which is essentially you can send information. So, for example, things like Gmail and Hotmail. They use post a lot where they essentially ascending information through their server to another person who has an email address somewhere else that's posting where you send information getting is where you retrieve information from a website or from an A P I on. This is where python could become extremely powerful. So Heidi recommends, Do you give this a go yourself? First things first requests Library actually isn't built into python, so what you need to do is go to your command line so or terminal. So if you're using a PC s of using windows, then you go to your search type in. CMD your command prompt. You should see something like this. If you're using your Mac, then go Thio Thio Terminal, which you just go to the launch pad and search for terminal. So essentially, I've got my doodlebug running here. Some just go cancel. That we have to do is use Pip. So just like we used Pip to install groups, notebooks is type in pip install and then requests within s the and that's very important to our e Q u e s t s. Okay, months already had been in stewards, so it's just going to say yes, it's really been installed. You're going to see probably some waiting bars and it may take a few minutes. So if that's the case, then just I recommend you just pause of the year here and then start playing again. Once has been installed, So was that has been restored. If you're using Jeeps notebook, then just run that again, or go to your text editor or whatever you're using. Okay, so what we're going to do now is we're gonna see if that's worked. So for me, it was already installed, but we have to do is type in imports Norwest's and now it seems so. It seems to have important just find here. So we're going to just test something out. So we've installed it now were imported requests. And now we're going to use our first method, which is dot Get so dot get is just essentially a good way of initially just getting a response from the website. So I'm going to do the requests doctor gets and then we're scared type of website http and obviously do this in quotations I'm actually going to put We'll say this is different variable because it just makes it easier. So you are l you can say you obviously call your variable whatever you like. I'm going to call it hates TTP slash slash www dot and then I J K. Jha hoops. Okay, And then in here request Don't get on. Just the euro says you can see it's come up saying response 200. So you without noting it, you've probably come across responses before a response basically says, Have you accessed the information you wanted correctly? Where says 200 that tends to be a good thing. Whereas if I do something, we're spending there maybe not dot com That's care for It's like that. Let's see if it gives us some kind of response here, so it hasn't here, but I'll show you what I mean. So I'm just going to type into my search engine. Hey, CTB response codes so responsibly could basically say yes, you were successful in making contact with server or you're able to get the what you need it so he can see it's probably got niceness. So the 200 is good. It means they have been able to take six to successfully access or essentially, just excessive successfully. Talk to that server will get to get some kind of access to it. 300 is redirection. Eight were about too much 400. You almost definitely seen four fours before, which is basis. Same page, not found. That's often when you go to a website on and you're a paid you tried to find has been removed or something like that on. So I'll just say what I mean for four not found, we're going to images. I'm sure you would have seen something that looks like this before. For a four not found. You come across a little the time says this whole thing. So if you see a 300 or 400 or 400 story or five hundreds, which the server era you have seen bad gateway and suffered out before that basically means that you've been unsuccessful in trying to access it. But we got we got 200. Say, it's not something huge to keep in mind, But I'm basically just introducing you to a few concept here just to have got it in your mind. So to hundreds tend to be good. 45 hundreds means you weren't successful. It looks like we were. So what I want you to do now as pause the video and try and do this yourself put in you r l any euro that you want, It doesn't matter what one. Give it a gay and make sure you get some kind of response, Mitt. So I hope you had to go that yourself. So now what I'm entity is I'm going to try and do dot content, which essentially a way to get all of the information. So so for example, in the hay html more jewelry did earlier. What? What We will get back in dock content is all of the HTML. So I'm good to you. Requests don't get and I put in. You are all here. If I say this into a variable, I'm just going to call this response, for example. So then on this response, I'm going to do response, don't content, then get to see all of the content of the response, which is basically all of the HTML. So you might remember from all of our hates to know before you have the doc type html at the top, then you have you have hates to mail here. Then you have lows of tax. So you have a link here, for example. Hey, this is basically a well, the hates, you know. You see, you have some classes being used here with butter, and you have dibs. So is giving you the HTML response. So this is a good way. If you ever want to get the hate email from a page we have to do is use this request stock gets on the euro of your choice. And then you just with that, with the response they have saved invariable, You see, Is that doc content That's what you get now. Often what you want to get from from website is actually a Jason, so that's really important. Let me just come in this out. So we have a join amount here. So Jason files a really important so you can think of Jason's to be very similar to dictionaries, but they are actual files, so it's not like a python file. So we could have, for example, but if we were looking at names, Ages of people, so John 30. Okay, Adrian. So this was very important to realize is that Jason's almost the exact same in the basics as a dictionary. We just have these key pair values on DSO. If we were to save this result blurs my Jason or some of that in a variable, we can access it the same way as we do with dictionaries. My Jason in the square brackets, they put in the key. We get value out, but these actually stored his files could dot Jason, so there will be a file that literally just contains something like this. And what's really interesting and used very often is that you can store Mawr. Let's call them dictionaries within the within the whole thing. So we could then have this just for a one the two in here than in order to access for example. This one here is you would, of course, need a key here. So I'm just gonna call this new, for example. So here I could basically, if I missed another one here. My son, if I were to just put in the key, knew they would get back this whole thing. He is, too. So that so that if we just wanted to access the value of B, we could just put in square brackets. After it be way we get that body on, this is stored as a file, and this was all you'll see within the file. And they're very, very good at contain data and it's used very open. So that's the next thing to keep in mind is using is the Jasons are a way of storing information. So now I'm gonna show you how that's directly useful. So going to use this, you are all here, and we're essentially going to do the exact same thing. We're going to get the information from this ap I We're talking about why Piers a B I's are in a second, but you can think of them mainly. It's just blocks of code or programs, even that live inside that website that have a specific function. So let's access the information from this AP I hear. So first you entity is that store. Ah, you Earl on. We're just gonna call this because what we're doing is a currency exchange for the USD. Something critical. This currency, you are all equals than this string him t quotation marks. So now we want to get the information from here. So, against do requests for three of the module doctor get and then ah, currency, you are. So we get response 200. That's that's very good. Now let's save this in a variable. Were just quit response. So now we want to get the Jason from this response already know that it's going to be coming adjacent file. So that's why I'm doing it. You can't just go to any a p I on. Just expect that it's gonna have a Jason. You need to know in what format it might come in because it may come in. Is Jason or a different file type. For example, a CSP file. We don't know. So I already know ahead of time that this is going to be Jason file. So don't worry about it too much. So really response to Jason. As you can see, it's now returned this awesome Jason with all of these different currencies. So, as you can see, all of the currencies are actually stored in these cutie brackets here under the key rates . So I'm going to put store. This is data, Andi. I'm going to data and then square brackets rates. No. You see, we just have the rates now, whereas before I just show you what date awas? It wasn't just the rates. There's also things like the base currency, the date and time that we're accessing this. So no, we want to access. Let's they won't see the see Hong Kong dollar haste, Katie. So if you do then has been some square brackets, hates k d. Then it returns to us just that rate. So essentially, this is us access. Accessing a specific type of file from this website from the A. P I were accessing and Jason and all we had to do instead of doing response dot Content to read it above here we used response Stop Jason with the empty brackets, the end bulbs of end. Okay, so we've gone over Jason Falls and getting everything we need. The final thing I always talk to you about was how you confined ap. I say how exact How, for example, did I manage to find this one for currencies? What I did was I went into my search engine. I'm litigious, typed in a p I for currencies, currency exchange rates, and then because I wanted it to be in adjacent, I just typed in Jason the end. Say, for example, I'm sure this one would have been just fine and see what sting here is being very helpful. It's actually telling us how we can, how we can try and try and access it. So if we use, for example, this you are all here now, usually quite often you want to use you want to be talking to a prize that you trust me right now, we just access accessing this vaccine that's accessing this straight from the from the Internet. We don't know much about it. It should be fine. So we're gonna dio requests gets Listen here. So it's most 200. That's good that we do requests. So then we want to still this. So let's just call this. You are Jason, for example. We'll just do ur Let's quit. Which one do you are? Dr. Jason, It has been Say what? Okay, so now it basically the one we just looked at. It has been removed essentially, to continue to use. You have to sign up this This anything here This is ignore that essentially all I did direct us this 1st 1 in the first place was I just Googled it. And I found some reviews that said Yes, it works. I tried out myself before giving this nature, but the what? The emphasis on went bon was that you want to know what in what format the information will be given to you. So I already knew Was Jason ahead of time on dso to describe to one a p. I is so ap I just basically stands for application programming interface, which and there are many different ways of explaining this One thing I want to do just to show you two you recognize in their. This illustrates quite nicely. Essentially, where the where maybe I sit. So when we have a Web baby, I, for example, essentially what you need to know about AP eyes or application programming interfaces is that they are programs which you can interact with often, um, that you can either get information from ons or you can send information to Andi. Often there may be a number of different AP eyes sitting together. So, for example, you may send information into an A p I than that a p I would then do something with the information sent and then send the new information to another a p I to do something else with. You can just essentially think of AP eyes as programs that you interact with whether gets will receive some kind of data from. So in this lecture, we've gone over the request module. So we looked at how we can install that that was through using pit where we just typed in pip install requests. Now, if you've been using Anaconda instead of pip, which is anacondas on you, you would have downloaded. So if you haven't don't worry about it. It'll then he would have just used conduct in stool requests. We then imported the request module just like we refused import random before. After that, I showed you how to use dot Get, which essentially just getting information from any U R L you want online. If you get a response that's in the two hundreds, that's positive. If it's 400 or 500 O. R s 044 or something like that, the night indicates that you weren't successful. What you can do once you've actually got in, once you've done request, not get If you start in a variable, you can then do things like response stopped content where you can see the html content or you can dio dot Jason and we looked into what Jason Files were, which we can just think off as dictionaries and also weaken store dictionaries within dictionaries. We looked into how we can access dictionary, settle within dictionaries. Then we look to using dot Jason where essentially using a euro like this. We were then able Teoh using the dot get we got response and then we're able to get the data using DOT Jason and we were able to access. Our currency is there. So it's gone over. Quite love concepts. Hope that will make sense to you. I recommend you have a goat this yourself on. There'll be a relevant practical coming up. So have a go yourself and I'll see you in the next lecture. 51. Practical - Real time currency conversion: this practical. I wanted to do a little bit of urine. Researcher. I want you to try and find a working AP I where we can access currencies, preferably with USDA as the base currency. And what's really important here is that the output or the A P. I must provide a Jason file. Okay, that's very important, because that's what we've been working with so far. It wouldn't be an issue if we already knew how to handle other files. But for now, let's just make sure it provides Chasen's. So you want to be essentially using a search engine to find this out yourself. And it doesn't help in your searches to include the keyword python because you might find some or python friendly AP eyes on the community is quite good at finding good ones. Then, once you've got that, I wanted to create a program that can take a user's currency in amounts. So, for example, the user might put in you are for euros or GBP for great British pounds, whatever, but they're able to put in whatever currently that they want, and then they put in their amount will be converted into U. S. Dollars, So give that a go yourself on, then we'll go over the solutions in the next lecture 52. Solutions: okay, Hopes you have had a good try at this practical yourself as it really is. Good practice for trying to find working AP eyes. It can be quite a frustrating process. Sometimes on quite often, especially funeral profession environment. You'll be provided with the FBI's yourself, but it's also a good skill to be doing this and to be used in Request libraries to actually check out which ones work and which ones don't and then oversee. You had to go hopefully at creating our own program where you can take the uses. Currency in amount and converted into USD, says to a search now. So I was searching just before I'm going to look at a P I currency. Currency rates Jason for us dollars, says he. What comes up? Jason rates have tried before. I abandoned tried before, so that's hurt in to say yes. Deed Jason. Let's get for currency conversion rates. It's good for that's so since I take the cause of you say for exchange rate conversion, a p I. So it could be goods Have look at this. This is pricing. Some guess it doesn't come free. And for this because do you want something, He'll get currency there. A by free, reliable cape. That's also doesn't seem to be free subscription plans. Jason, Reach, we know. Doesn't work too well. Unlimited free currency conversion. Kate, this have a look at this one. So this is good to me. Some try this link, so let's give a game. So you may hopefully have realized that you were going to be wanting to use the request library for this. So to use that, we just import requests. Okay, That seems to watch by next. You want to use the get method, so requests don't get. And let's just make sure that this u R L works This one's 200 perfect. So let's now save. We'll store this in a variable. We'll just call it response include whatever you want, there just tends to be best practice. Not this practice. Just most people tend to quit response. Now that we have the response, let's see what the Jason looks like. Hopefully there will be a Jason SE response. Stop chasing okay. Yes. This is looking exactly what we want. So require USD base here. Go rates. So it's have a go at it. Let's just store this as ah, currency data. Okay, that is good to me. It's now that we have that it's just print out one more time so I can see what the formats like. Okay, so it seems like we have this base U S. D k. That's good. We don't need any of this information. What we want to these rates, you can see the key is is just a string. And then the rate, the actual information we want all of these different ones, they seem to be stored within another certificated brackets. So first he wants to do is access that. So let's look at currency data, then in square brackets rates. Okay. Says returning with that. Fantastic. Now, if you want to access just one, let's say a D for example, the now returns exactly what we need. Okay, this is absolutely perfect. So no, we want to do we want to take this off? Of course. So it's going to the end of this line. I'm gonna put the tick here. It always feels good to take it. Take these off. So next ones it is creative program that user can take contain the use of currency and amounts and converted to USD. Okay, so the first thing wants to just take the use currency. So I'm going to print off Welcome, Teoh The best currency converter have. Ah, next we wanted Dio question after that, printed the starts like to that kind of thing. So you want to take the input from Neuza? Please enter the currency that you I would like to use. So we're store that in a variable. That's just cool user currency. And then we want to store the amount. So user amount equals input. Please enter the amount to convert into U. S dollars. Okay, so now we have the use of currency and the user amount. So what we want to do is we want to have amount in U. S. Dollars. I want to do that. Essentially, we want to get the rate. So the currency some scope for placeholder, for the moment, just called rate times. He's a squat amount now for the user amount. Of course, we do want to cast this into an interview before I forget. It's no use to get this rates. Now the rate is going to be something that looks a bit like this. So against e currency later, then rates and then in this square brackets is actually gonna be whether the users put in is their currency. Now, one really important thing to keep in mind is that we need to have it iss upper case because all these air not case. So if, for example, we would try this with locates a D you see that? It wouldn't it wouldn't understand what i e. D is because it's all lower case and doesn't have that used to be a case. So what I'm going to do for the use of currency MBA here, just in case they put Inler keys by mistake, I was gonna do dot Upper Levis, So let's give it a go. This were essentially whatever I put in is gonna change into up case. So let's just start. First of all, with actually putting up case, I want it to be Japanese. J p Y he what? Please, in three months, let's say 100. Okay, lets see what's what's going on. There seems to not be okay, so it seems to not be very happy with this upper here. Let's try it with empty records. J p Y. Every day, this should work nice. You know, we want to obviously print out the amount in U. S. Dollars. Say you have the amount in this currency That is so much so that searches for murder in that's going to be user amounts. Use ah, currency and then amounts in U. S. Dollars. So one more day, JP, Why 100? You have 100 jp. Why, that's 10,864 in U. S. Dollars. Fantastic. And so you know, we can take off our second part because you can now take in the amount of the currency and out put it in U. S. Dollars. So I'm very happy with this. So let's go over how we've managed to come up with a solution. The first thing we did is we found a P I online. I searched FBI currency conversion rate usd Jason, because there's the kind of things you want to look for. We don't through a couple of them on because we're looking for something free. We avoided things that said something like pricing or in description. Certain amount per month would already try that one that wasn't looking looking like what we wanted and they were able to find one that did work. Eventually is exchange rate one gave us a nice, nice link, so we just tried out just using requests. Dot Gets were to import, request library, then request stock. Get we bring. You are all we got to response 200 Where Spacey means OK? Yes, we can access it. That's a good start. The next thing we did is we then stored in a variable. So we got the actual gets request, which contains the Jason. So then we just used the dot Jason method from the response, and we were able to get this nice, big thing, which kind of looks a lot like a dictionary. This is our Jason file. Then we're able to access the rates by doing currency data and then in square brackets, the key, which is refers to this one here. And I gave us this long list. So we saw that. Yes. Okay, this light was giving us the information we needed. And then we used a d essentially get the correct right back. And then from then on, we just had to use some pretty standard stuff that we've done before. I introduce the dot upper fortune here, which is quite helpful, makes it turns everything into uppercase on. They were able to create our our real time currency converter. So I hope you had a good good that yourself and you understand what's going on here. You've never to practice the requests like yourself on. I'll see you in the next lecture. 53. Practical - Live bitcoin values: in this practical. I want you to try to access the data from this. This AP I hear I thought I'd leave it. Leave you with the URL, while rather not Nieto Research yourself, so it's hasty to Bs. Colon slash slash AP I dots coin market cap dot com for slash v 14 slash ticker for slash Bitcoin four slash So what I want you to be able to do is to access the data from here like we've done in previous lectures. I'm trying to avoid using any technical words. Yes, that you're trying to work out how you can do yourself on. Guy went to output the current value off Bitcoin now for a bonus. I wanted to have the value off Bitcoin automatically update every five seconds. So once it's into your program to go and find the latest information from here because updated very, very rapidly or it's update every five seconds as a hint, you should research the time module in python. So search for that and and see if there's any way that you can find a way to I don't know, added delay or or something like that or just type in a question into into your search engine, make sure making sure to include the keyword python on DSI. What you come up with and there's another bonus. See if you can output the current time and date. Now. One of the reasons why I'm asking you to do these two bonus ones is because it's it's leaving you to have to do your own research research as to how you can do this. They both require the use off different module, different libraries or different modules, so you're gonna have to research that yourself. So give it a try yourself and then we'll go over the solutions in the next nature. 54. Solutions: So you had a good goat this yourself on. Let's go over the solution together. Say the first thing I wanted to do was access data from here. So I think what we're gonna do is the requests library going to import requests. Then let's just make sure that all is OK. Crystal get issues that you are all here and just make things easier myself. Just listen. A separate Berryville cooled you are case of medicine here. Response 200. Fantastic. So let's see if I can get to Jason from this. So I'm going to store this as response. Now I'm going to the response. Jason, there's no defines that's trying this again like a fantastic. So looks like we're able to get bit quite a Jason from here, which has pretty much everything that we want. Andi, let's say we have it in us dollars se price US dollars 9005 or 41. OK, fantastic. So we want to be able to access the data which had done so Take that off. First of all, congratulations. Output. The current value of Bitcoin says, Just get that, Get that done. So we're going to want to do for that? Uh, oops. You want to store that? So we got store this just new variable. I'm gonna call data. So to get that we want to print out data, actually, because we're using dupes notebook would have to use print here. Data square brackets price underscore U. S T run. That's make sure that's working. Okay, list indices, once we insure slices no string. Okay, let's just have a look at this data again. It's print off data. Uh huh. As you can see, it's actually stored within a list. Here, say, before we put this, we essentially we toe access the list. So this is the first index in this. I was gonna Waziri and then that will give us what we need. So now we can put into their brackets the That's a price USD. There we go. So the problem was is you can see we had these square brackets, which means it's in a list. It's the first thing that to do was accessed this actual object, which is the first element. So we did zero in square brackets and then price on school USD. So we're now able to output the actual price of Bitcoin for the very so take. Excellent. So now have have the very automatically update every five seconds. Research the time module. Okay, so first more. Let's just have this printed out in a nice way. So I'm going to bring the current value off bit coin It is. Then, Dr Formats, the store, this as it coin, uh, you and they will put this in him. Okay, so that's like, you've been nice enough. So the next thing I want to do is having update automatically every five seconds. So we want to have ah, kind of delay. So let's see if we can find something like that here. So pi thin. We want to use the time module, and then let's see if we can have a delay. How can I make a time delay in Brighton? Perfect. Look. So it looks like time dot Sleep is looking good. That's just check where they say here. So this is always had a lot of up votes. A lot of people have seen it, So this has got a lot of up votes. Basically, it's a high, positive number. It means a lot of people have said Yes, that's a good answer. So we import time and then we do time, not sleep. DeLay for having May seconds. Perfect. So let's get this over to our crate kit. So we import this up of the top just because that definitely is about best practice. Okay, then we got to time. Don't sleep. We'll put this hopes. I just got a hit. So now what you're going to notice is nothing is really going to change. So it's done that and that's going to sleep. We actually want this to be constantly changing, right? We want the current Bitcoin. Then we want the next body of the next value. So what we could do is actually had this into a wallet. So you say while we're on this to be an infantry, So I should say one is equal to one. Then this is going to keep printing off every every five seconds. The crime crime Bitcoin body. Obviously, we need to also do all of this so mean to get the get the latest response from from the a p I. So you want include all this in the world form thing? We don't need to clean in here is importing our libraries on our modules. So every five seconds we want essentially, that's ever gonna change. So we can leave this variable up here as well. We're always gonna be wanting to use that. You are. So you want to be able to get the data? Yeah. So we're going Teoh use request dot Get for that, you or else we get our response from which will get our data. And then we'll just be able Teoh work other Bitcoin value from that in principle. So what we doing here? So in the wild Deep. Essentially. I want this just to be constantly out putting the latest value of Bitcoin. So it's gonna do. You gonna get the response from the website that is gonna get the Jason and then we're going to get the price from that Jason and then printed out on. Then we're going to delay or we're going to sleep five seconds on. Let's go back to the top of the wild leap because this is infinite is just going to keep on doing that. Let's see if this works. Then we're gonna wait five seconds. Okay? Fantastic. I'm actually gonna make it. So it's not just five seconds. Mr. Producer. Over. Okay? Again. Yes, that's running. Running a bit A bit better now. Obviously, the Bitcoin value isn't going to change every two seconds or so. So we can just leave this for a little while while we go over exactly what we've been doing here. So I'm just going to take this off, okay? Fantastic. Sesno updating and to which is quite suspicious. However, you know, Freddie's own changed too often. So we're going to just leave this running in the background and let's send it to actually, So we have a huge thing to have to look up. Let's just that it forever you like 15 seconds and will focus on this next point, which is getting out up, but the current date in time, time and date. So let's go back again to our search engine. And this time we want toe output current time. And now the reason why I didn't create a lecture, for example, for exactly how to do this is I really want to encourage you to develop the skill of learning these things outside things yourself. Because there are so many parts and libraries and modules that you can't simply can't fit them into a course. So what's instead, what I aim to do is to equip you with the skills in which you can then go out and research these things yourself successfully. Since have a look, Stack overflows usually quite get how to get the current time in Python pretty clear, and it seems like cyber city dot Bears take out men. These all look like articles with tutorials, which might might also be useful. Let's have a look at this one first, so this one seems to have a love up votes. It's import daytime, daytime dot date time dot now, just the time. Okay, so let's have a look at this one. So free import date time and then we run this. That's how this is going on. Yep. As you can see, fantastic. The Bitcoin value is changing over time so we can stop that now because that's been confirmed and that's try running this. So first of all, we want to import daytime. Wished it down here for now, Import state time. Oh, perfect. That is great. But we don't really want these the seconds, So let's see a daytime dot now, So that's do python date time top now and then. Yeah, we want to format it or subtract seconds. That's exactly what we want. So subtract seconds. Okay, Another stack. Overflow one. Let's have a look. Well, have you get minus state time dot Delta seconds equals X from okay. I'm not sure about that one. Drift it. We have to add in this sign as well. It just doesn't seem very tidy, so that is definite option. But let's see if we can do we can take another look with something else. I'm going to look at how we conserved between subtract seconds, something a bit more substantive. It will broad like how to format it how to print a date in a regular for months. I like the look of that one. So he's saying, basically, you just want to simple date. Okay, So we could do dot today and then So if we just have the my list, let's give this one. Let's give this one try. See what it says the time. Got data today. Okay, second says the date. But we still do want the date and time, so Okay, So this is looking. This is looking what we want. So it's saying that we can actually output the day, the month on and the abbreviation of the month and the year. Fantastic. Let's just give this whole thing a try and see what we come up with. We can play with it however we like. Okay, Prince who? Yeah, from here. So everybody answers. Using Python T today is not defined a case. Let's see if they had to import something, huh? First what? They had to do this. Okay, that's taken great. Okay, So what? We're able to do the state time, Dr Day as we would do their formatting. So we've got here the day, the month from the year, and I'd also like to have the hour and the minute. So what's this saying here that's such this will do python this and then hour and minute. Let's see what comes up with. So this one So this looks nice. This actually, it's a lot more succinct. So let's get this one try. That's Kate. And try and print this one out. That looks much better. So we could also include this abbreviation off the month as well, if he wanted. So instead of it being 07 here, we could do, for example, the day come on. And then the abbreviation, which is to be and in the year afterwards, That's nice. So that seems. Seems to be pretty good. So now we're just going to store this whole thing in a variable up here so we could store this, for example. We're just the top here. We could say time. Now, Who is this Mick put? Well, she put instead of having just the love about this stuff. Let's just have something like this. Placeholder for time. Colon in the value. Keep it simple. The time on school now. Okay? And as ways that's just move this up to the top. Another way using it. Okay, so let's give this try again. Oh, and it's short in the time right now. We have to wait 15 seconds. Just wait one second each time I have to restart our colonel. Okay, So when this starts running well, we could just from you that when this starts running every second, that should be out putting the new value. So very fortunately for us, the value literally just changed. But it's telling us here the date and the time, including the seconds on giving us the Bitcoin value. Fantastic. So now we can take that off as well as you concede, Like we're now using. All these python Python concepts have been going over. But now we're able to use them in a really interesting way and were able to find to use our current knowledge of python and now kind of our our growing ability. Let's say to start, to be able to use our search engine and to you stack overflow to find out new ways of doing cool things. So what did we do so first? Well, we we had the euro for the eight how to access our FBI. So within that, we did a standard thing where we did request dot gets with you are oh, we store the as a risk in our variable response from which we got the Jason using the dot Jason function here. Then when didn't that we had a list that contained the dictionary that we needed? So we did data, which is name of the the whole Jason object. And then we just took the whole dictionary by because it was the first index within their. So we just did zero. And then we used the key price to USD on. We had the value and then lords to to make sure that we could get it printed repeatedly. Andi, keep in sort of real time. We put it within a while. Loop, which is an infant wildly, because the condition here is always true. And then we use this time dot sleep to delay by one second. Health in this comes out then finally, through our use of a search engine stack overflow were able to find a cool solution on how we can help it the current time, including the number of seconds, the dates, the hour and the minute. So if he had a lot of fun doing this, I definitely did. I always love solve stepping up from just having the basic concepts to now It's having something that's actually pretty cool and could be kind of useful to people. So anyway, hope that was useful on. I'll see you in the next lecture 55. Create your own modules: Okay, So you know how up until now we've been importing external modules and libraries like I don't import random. They would be doing like, random doctor Grand hint. So 345 or four or 500 or whatever. So essentially, what we're doing is we're taking this module. We're saying use dysfunction and that's just one function that's in this render module has loads of functions. Now, what's really exciting is I'm going to show you in quite a short lecture how you can now create your own modules that you can actually import or that you could provide to other people, and then they could start to use it. I just think this is so cool, and it kind of makes you feel like a proper developing now. So what? I'm going to show you essentially, what you want to do is create a load of different functions. So I'm going to do death to define. That's a function, of course. Help. That's underscore. Message empty prince sees. And then I want to be like, Hey, this is working. Cool. Okay, then we have another one called death return. Hardly deaf. Welcome by name. And then for this one. You have to obviously put in a parameter. So it's going to print out by the goods to meet to you and don't formats, then the name in there, Okay, And let's creates for more, which is gonna be calculate. So actually, we just quote deaf multiply. So you're taking three numbers. One, 23 Then print out. I'm driving and I have a better idea. I don't have any inputs which haven't any parameters was having input. He's ah inputs. The who's please provide number. We will multiply it by 100. And as so you want input here, This has also want to cast this into insecure just over time before on. Then let's just do, um, number he who was used input Times 100 print. You're number this year for fun. Okay, so now we have three different functions. We have output message working my name and multiply. Now I'm gonna show you how incredibly easy is toe create any modules. So we have to do Which one of donor this has FIFA file. So it has to be dot p y as an extension. Okay, so just saying me that bison, bison, fathers can home computers off. You have created this so I don't need to worry about that. So I'm gonna keep it. It's downloaded. Okay, Fantastic. So I'm just gonna copy this. That's gonna make sure what's really important here is that it's in the same photo. So it's good pissing up Python practice photo. Okay. And now, in a separate script So I don't have to have this open it'll so I'm just going to make a note. These are three functions help a message working my name and most play. So I just want to illustrate illustrate to you that this doesn't need to be running in the background or anything like that. All this important is that it was saved as a doctor p y file. And it's in the same folder from where I'm running this now. Okay, say now I'm just going to show you. So this is the only script I have open. So there's there's nothing else I'm going to write in imports. I think it was called custom Modu. Okay, Fantastic. So is imported just fine. So that's that was the name of the file, because the name was custom module dot p y you don't increase the extension. And now what we can do is custom module dot And so then we can choose any one of these. And when I press tab in Jupiter notebooks, it even tells me the different functions that I can use here. So let's start by being the 1st 1 which was output message. So if I just run this now, it won't do anything. It would just show me that it's a function. So you choose the empty rackets never go. Hey, this is working cool. So let's try the next one, which was welcomed by name. But this time, if I run this with nothing in their brackets, this guys has a problem because I need to put in. Ah, positional name proved provisional. Argh! Decisional argument. Science. Been a name. And here. So, John. Hi there, John. Good to me. Fantastic. And let's go for the final one, which is multiply. So we go. Please provide number. We will multiply it by 100. Okay. For 10 your number is 1000. Brilliant. So that's literally just how simple it is to create your own modules. And I hope that gives you the same thing that I had. When I first learned how to how to do this, it made me feel like I was finally becoming a proper python developer. So just to go over what we did, we essentially we saved our this file as a python files. You can call it whatever you want. We called it Custom Module on. Then the file extension was dot p y Just like when we were doing hey, html it had to be dot html files is the same thing on then, making sure that we were running our script here from the same place. Same folders. We're running our python file, which did imports in the name of that file. And then we could do that dot You are the different functions to get those to try. So now we've been able to create our in modules and so we're really on the right way to becoming proper python developers. So definite. Ever get this yourself, build up a bit of confidence and they will see in the next picture 56. Reading and writing files: I'm now going to show you how you can read from files and write your own. We're going to be looking exclusively for now, just a dots. TXT files. So those are basic files that contain text. And here I've created a very basic one called story dot txt with just made up text just to show you how it could be done. So what we want to do essentially is we can we want to open the fire to start with. So it's do bright open then in brackets we put in the file name change that to say, far name, which is story dot txt. Of course, that has to be as a string. And then we need to put the mode. So you know, they have. We have there are a number of different options. But for this lecture, I'm just going to be talking you through how to read and how to write. So we're going to start off with reading. So we're going to use we have story That's still listen, Variable. I was gonna call this file, for example, we've put eyes a string as well. So we have now opened this file in read mode. You have to specifically put it into read mode. Now that we have this in a variable weaken do file dot Reid, there were them two brackets you will read out, but allow the all the Texas in there. So what we've just done using the power of Python is we've been able to open up a file. We have been able to look inside. No one really important thing we have to keep in mind is that we have to use We have to close the file at the end, but filed, doctor close, then the brackets. So the re important steps here are that you use the open function and then inside the brackets, you put the name of the file and then you put R for read, which is the mode we're gonna next look it writing which for that you just do w instead of our And then we did follow that read which allowed us to see all the lines between the text if we wanted it to read it line by line with used for leap like this, we could just say for the line we could use whatever word you want to that in file Prince that line, then it prints out like this, which looks a bit nicer. That's another option, actually. Include this so you can look again just down here. I believe this one is Don't Don't read so another. We've looked at how we can open files and read from them. Let's look at how we can know. Right them. So just down here into pits, new file equals, then gives open function, and then I'm going to call this new score text dot txt and then I'm going to use the right mode. So is the w I'm going to write. So now a new profile dot right? And then I'm going to put hello. This'll is some you text and then the end. I definitely want to close it. Okay, so this is where it should be, because this is where I'm running my deep snow, but from this is where I've had the story file. So hopefully what's gonna appear? Actually, I'm just gonna pick quit story. Let's go. New text so that it should appear just next to this one as well. So I'm gonna now going to run this now. You should see, we have a new file here in the photo Would story new text and as well there is really for the basics of reading and writing. So what you should practice is both of these or you need to do is use the open function and then use the name of the file. If you're reading, it needs to be an existing TXT file. And you Yeah, I just use note pad for creating mine and saved it with just by typing story dot txt in the save us. And then I used the mode are in order to read it. I use the file. Don't read now for what? It's looked nice, the eulogies, the four line or for whatever in file print. Then we looked of writing in this exact same thing how you open it except to use the W instead of the r because we want to. Right then, we used new than we could have saved it In the very bored you file, we start it. Then we used new filed dot right, and then he right in some text. And then we closed afterwards that we had on you foe, so definitely have a good creating your own Fight your in text text file on there. Once you've created it using this right, then go back and see if you can read it using the R mode. So give that a try and I'll see in the next lecture. 57. Object oriented programming (OOP): now object oriented programming is often described as a slight hurdle for beginners. T learn. It's definitely a step up from beginner concepts with debt. We're moving into intermediate concept here. Why would say, is that it's well worth gaining confidence in what we call Opie because it has so many useful applications is very, um, very desirable skills for employers to see that you're comfortable with Opie because it is used a lot in other languages as well. That's why I thought I just put together a few lecture slides to explain the basic concepts before we get into the coating. So what is? Opie, four says, for creating what we call objects. Now that could be something like a bank account within a bank where the object would be, you know, a new customer and or that containing all of the details, like their names, their address, their date of birth, how much money they have in the account. So an object that contains love information on a specific entity. So another example is health care patients in a database. You know, let's say we're hospital and we want to add a new patient. We want to make that as easy as possible within. And we want to make it simplified within the code so that it's just one line of code for a new patient to be added in with their name. Maybe what disease they have, how long they've had it for Andi. Last time they went, the doctors stuff like that. So it's creating an object with lots of information on the entity. So, for example, let's say you want Teoh create a car class, so there's two things within a class within the object. You have attributes and you have methods. So attributes is what I've already talked about, like with the bank account in the health care patient rest information on the entity. So for a car that might be the fuel capacity, what the cars Max speed is what model it is, how big it is, how many wheels that has all that kind of stuff. And then you also have methods, and that's it. Methods of pretty much synonymous with functions have already gone over functions before. Methods are functions that are within the object. So, for example, here we've got methods for refuel for to set the speed to drive that kind of stuff. So all you need to remember for now, don't get too wrapped up in in the concepts too much. Why I want you to remember is that classes basically create objects and so objects then create attributes and methods. Attributes contain information on an entity. So, for example, you know, we we want to edit add in a patient to a health care records or we don't want a new bank out or we want to add a new car. Let's say we're we're, ah, car sales person. We've just bought a new car to sell. There were border in. We want to add in this new object of a car, for example, how much fuel looking, carry, have any wheels It's has. And then we we have methods as well, which are exactly the same as functions. But they're just within the object on. We're going more into how this actually works, and you don't need to fully understands for now, as long as you can appreciate, we have objects, the objects have attributes and methods, and you understand how that all works. The reason why we you use object oriented programming and creating these objects from classes is because it's clean. We can put together attributes with functions or methods, and it's it's It's relatively short amount of code. A small amounts of prayed for creating these incredibly powerful objects. So we're sticking to the dry principles in python. I don't repeat yourself, so we just talked about before with creative makeup. So let's save creates the bank. Come What attributes do you think that adding a new account would require so here, a couple balls video now and think of a few more than you could you could add in. So I added in here the name he addressed the date of birth, the bank balance. Other things you could have might be the date there. Person set up the accounts that the type of accounts things, their credit score, all these sort of things. Now one. Now this is more of a test. We have attributes and object, but we also have methods. We talked about methods in the previous slide. So pause the video Now have a think about what potential methods could be within this object that would be of use. Okay, so I hope you had a good thing about that to hear a few a few standard methods that you might expect to be within a bank account to withdraw money, to be able to view the VAT balance of that count toe, make a deposit or to change personal details. So for you might have gone a few a few ones that similar. So without any further ado, let's look into how we can create class using the bison coat. 58. Creating a class: You might remember in a recent previous lecture that we were going over how you can create your own modules in different files and then import them well. It's good that we're moving onto creating classes now and looking Maurin toe object oriented programming because it kind of transitions quite nicely. Say, as we discuss, having a class is a good way of being able to store a number of variables and different functions, and it can even act as blueprints to create your own objects. They trun, but we're just it with creating basic classes for this section. It's the first thing you want to do is write the keyword class. So as you can see, it's gone green because recognize that this is an important way. Say, I'm gonna call this my class now with crosses. It's usually best or common convention that, say, to give the first letter on up case some scared. In my own school class number, Laon Candy X equals 55 and now for the my class Kopecks opens 55. So we're now able to store a number of variables within a class, so it's the first thing we can do that store something else like a name. Mr Potato Leg. Andi, that's a tough name. My class. Don't name MySpace state like Fantastic. Let's have something called my list off potatoes because everybody has this of potatoes, right? Anyway, that's cool. Big one Ondas also have a small potato, of course. One as big as Mr Potatoes. Like her case, that's quite a random list. Now we gave her my list of potatoes and it will bring now as well. So essentially, this is a really nice basic way of showing you how we can use classes. So the first thing we can use classes for is storing as many variables as we want on. We've basically created an object here. Now, what will be going onto in later lectures is how we can then also and in a lot of functions . So they're contained within one class on. Then finally will be looking at how we can create a blueprint. So we could, for example, just have, like, a generic name that doesn't actually contain anything. And then I can come in and I can create an object with whatever I name I want. And I could create a new object with a different name, so wouldn't just always be Mr Potato. What's really important for this lecture is not that you fully understand or the court concept of object oriented programming, because that does take a little bit of time. So they worry what's really important that you're just able to do exactly what I've done here. So create your in class by writing class, and then you just give you an eight. Give the class name whatever you want, try and give. It's an uppercase letter at the start, so it's helpful and then within that just creates, um, variables like we've been doing all along on then see if you can then get those back up by using the name of the class and then dots, then any one of those variables. So give that drive and then I'll see you in the next nature 59. Methods in a class: now that we've gone over how you can create a basic class and you can put very was in there . It's the next logical step for us to look at how you can include methods or would call functions into a class. So let's have a look at that now. It's the first we want to do is create a class like we did in the last nature. We just write class. And if you haven't already had a good coating out, some crosses yourself from creating your own, using what we got went over the last lecture. Please, please do. That's very, very helpful at this point for you to get the foundations of creating classes before we start wrestle with some of the object oriented programming concept. So hopefully I've done that, and I'm going to get a class called Methods cross. And then in this class that's just nature store, very able just to go it, just to revise what we've been doing. Some cost or a string good high that so now I'm Grange creates what we're going to call the method, and we just referred to as a function before so death. This is called my unscored method, an empty backed is always in a coat on. And then that's just print. This method is working. Okay, so there's been no complaints. We've run this. That means we've created this methods grass. Now, if I literally just run methods underscore class is going to tell us that we have an object here. That's all it is. Not gonna has got nothing to do with this at all. It's just going to say Yep. You gotta see this weird underscore on school main dot methods class. And that's absolutely fine. That's what we want. Now, if we wanted to access that variable, can you pause the video for a second and think about what we have to do to access this variable Court X? Okay. Hope you got that right. We've just put Doctor X and then it accesses this variable. So we followed the same logic to access our function. So we have to do is spit my method empty records and never get that runs the method. So it's pretty much the exact same thing that's getting a variable. So just to revise this in order to create methods within a class, we do the exact same thing as we create a function normally, except we do it within a class so you can see that's or indented. And then all we have to do in order to access this function and run it is we just put the name of the class then dot in the name of the function the exact same way as we were using for accessing are variable. Okay, so now let's just practices again by doing one that requires a few parameters. So that's the subtract oh, subtract. Or and then we'll put two numbers to And then that's just do Prince placeholder minus placeholder equals place of that. So we're gonna have crowd number three, which can just subtract number one from now on to but these enhancing, um one. So no t minus 91 equals none. Three. Okay, so what I'm doing here is not important. I'm just creating a tractor. What is important is that I'm creating a function that has two parameters here. Number one numpty. I'm printing something out, so I just want to show you that we call this the exact same way. But we just use the parameters, so give the name of our class. And then we put in the name so dot in the name of our method, which is sub tractor, some tractor, and then we put in the to parameters. So let's go for a 20 minus 5 25 The case. It's saying methods cost has no attributes of tractor. I'm assuming that's because I didn't run this cell yet. Case that saying keyword can't be an expression. I said, Let's look at what that might be. No, that's just because essentially, I didn't use dot former properly. Atal Not sure what happened in my brain there. Of course, we want to have this in here, and then we do don't format. I mean, these And here not entirely short where my brain was for that one. He was right. So you run this from this, they every gate that's much better. So just make sure that wasn't too confusing for you. I'm going to do one more. That's very simple deaf print stuff. You know, Andy, I was gonna print a Andi, so I just want to show you the basically important thing here is that even if you have parameters, you run it the same way. But in the name of the class dot name of the method, Let me just put in the parameters. Okay, So what's that saying? There we go. It's No, we run. It is going just fine. Say, to revise What we've done in this lecture isn't you have just we've we've already been able to add in variables. So he creates our class writing class name of the class using a capital letter you don't have Teoh. That's just what most people tend to do because it helps you to differentiate in your code later. And then we created a variable. No way we would on. Then we created function. The exact same would we would normally. Then we were able Teoh get the variable by just doing the name of the cross, then dot name of variable. And then we do that. We get our method or function the exact same way. And if we're going to use parameters, it's the exact same. So we have to do is just use the name of the glass, then dots in the variable or the method that you want to call. Make sure you include the princes at the end on DSO. Now you can see that classes are very useful for storing a large number of variables on large number, off of methods or functions as well. On what we're going to be looking at very soon is how we can then use classist actors blueprints so that we can actually start to have variables that haven't yet been created. Until we, we decided to call the methods class and provide the data or the details that say on how we want our variables to be. So that's very, very interesting. Sleigh confusing. Possibly. That's why I really recommend you. You get confident with the basics so you have a go creating own glasses that have variables and methods within them and then have a goat calling them and accessing them. Basically, because you might come across some errors, you might explode that a little bit on doing. In doing that, you'll be able to build your confidence, so when we come to, the conceptual stuff will be absolutely, very, very easy for you. So give it a try and then I'll see in the next sector 60. Init: It's another done, a bit of groundwork in object oriented programming where you've learned how to create basic classes. Now let's look at how you can start to create your own blueprints. Then we can create unlimited number off objects. So this is very exciting on. I'm gonna take it relatively slow, um, and succinct. So you know exactly what you need to do. I decided to name this lecture. Underscore, underscore in it. Underscore underscore. Some get tithe out. Underscore. Underscore i n I. T unscored one score. You just think of that sending for initiate, for example. So this is really important on git I fight. I found it quite intimidating the first time I came across it because it had with these it underscores and looked a bit strange. But it's nothing to worry about. It's essentially when you create a class, it's a It's a function that you need to define at the start in order in order so that you can then create objects yourself. Let's get started and I'll show you what I mean. First, for its creator class, I'm gonna cook cool this class animals because we're going to create. We want to create objects where we just create a number of different animals. So we want to treat this like a blueprint. So we want to create something here where we have a number of different attributes. So, for example, what what attributes might an animal have? Let's say noise size, things like that. The first thing we have to do is create this function co ordinate. So you death on school. Underscore in it underscore underscore. And I know it seems strange at first, and it's kind of is it kind of feels like it's complicating things, but is that as soon as you get into the habit of doing it is no Roy's, so just go along with it for now, do a couple of times, and honestly, it won't be too much of an issue. You then need to put the keyword self. Then after you've done that, you add in the attributes that you want to be there. So what we're going to do, let's have size we don't have This is a string or anything like that. Have noise. Um, it's good for color. I think I would do for now. It's just that for a number off next. So we have four different attributes size, noise, color, a number of legs. Then there's always the function we put it on. And then what we used Teoh is put all of these like this cool itself dot size because sighs self dots noise. Whoever do this for each of them, self dot color equals color. Finally, self dot number of legs equals number off. Thanks. So, once, I, um, on this now we have our costs. So the things to keep in mind don't get overwhelmed by this, please, because this is kind of one of the big, big drop off point where people think just gonna get harder and harder. But if you actually get this down and you can get confident with creating just these, it's honestly not too much of a big deal from here. So this is kind of a place where people think it's this huge thing, but it's really not. The really important things to remember here is that if you want to create a class as a blueprint where people can, where people were, run your real program when the class is used that say it creates another different objects so in this example using animals. Another example could be cause, you know, like the color, the engine size stuff like that, where you can create a number of different cars or a number of different animals. Then you want to use this innit method so we do death just like we define your function. Underscore, underscore in it. Underscore underscore. I think I've said that enough times now that's stuck in your memory, which is good, then the brackets. You just put self first of all, and then for the other parameters there just the attributes that you want tohave for the object. So if the animal was traced to have size, noise, color, a number of legs, and then we just had to do this self dot number. The name of the attributes equals the attributes you have to that four of them, this kind of Ah, that's kind of a pain. But you know, it's just how is so now let's actually create an object. So I'm going to do is I'm going to create an object cooled tiger. So you know what? To do this. I just have to put animals So that's the name of the class capital A and I just let you put in the attributes. So I'm gonna say the size today. I don't know how we're using using to measure this bus. Just cut 100. Then the noise roll, the kind sounds about right color orange and black number of legs is for and notice how, when we're creating the cost. We don't have to say whether it's an integer or strange or anything like that. So when I run this now, we have now creates an object. So if I just put in Tiger here, we have an object. That's kind of what indicates you have an object. Now, in order to see each of these attributes, I could just dio tiger dot size or tiger dot noise tiger. Dark color tiger tops now off next. So let's create few other objects. And maybe this will start to Shea how useful classes could be. So you're crazy during a time. And in order to do, say, I'm going to write that a class name and then put the size, I'd say it's a bit bigger. Sysco from 50 as the noise and then orange is the color. The number of likes to. On what a minute. I'm going to also create a dolphin. So right, animals size 80 I say it's kind like a re noise. Uh, gray, the number of legs. Zero a string. So now we have three different objects. You create infinite number of objects from our class, and then we get tightening of legs weaken. Find out what the dolphins noises, for example, so you can do lots different things. So let's go over one more quick example. Now, I'm gonna do the same thing, but I want it. I want to do it for houses. So you class house death in it. So that say, for example, we were a company that, you know we are. We re sold houses and we wanted to keep a store off all of our different houses. Where then this might be a good way in order to store all this information so that whenever a new house came on to the system or would have to do is write one line of code on, we could enter in order information we need and store that house the information in the house as an object. So the first thing want to do is create our class. Our class of you write the word class name of the class with a capital H and then because we know we're going to be using it as a kind of blueprint, we write deaf underscore, underscore in it. Underscore underscore. So deafness how we create functions normally have to use a special method in it. Do we write in self and then the attributes that we want? So I'm gonna have number off rooms, market value. Um, that's a distance school. Yeah, these are the soul of things that people who sell houses were interested in, um, square meters. So just bring that shorts on. And finally, let's go for ah, back garden. That could be a true or false like a 1,000,000,000. So it's true. It does have a back garden. OK, so now we've basically put in all the different attributes we want. Now we just have to de fund bar to do yourself dot Num off rooms equals numb off rooms. We can kind of short on this a little bit. So how many attributes you have? 12345 So one hopes to three for flies I just moved back about. So 1st 1 is this So this change this oops and distance from school that square meters and finally the back garden. So now when I run, this would have basically creates not class. Now we can just create the objects song into Quote Perch House 1024 Let's say it's the 1024th and actually one. Probably one thing we want to include in here would be the address. I know this isn't really but let's pretend it is myself. I thought Dress whose address? So I say this is house number 1024 in our database equals oppression. The class name which his house he puts in the attributes. The address That's a one I one thank street. Then the number of rooms that eight market value 100 thousands distance from school that, say, two kilometers square meters. I have no idea what's going 120 back garden tree. And then let's say a week later we need to have another one Is number 3000 and 11 created in the house. There are three great road from a rooms over 100 exits, gigantic the market value is a bit better. 1,000,000 business in school. That's a 0.54 x the school square meters. Let's get 300 true. So obviously I don't want to run through a time creating lots of objects, but now we can now have a look at. Oh, so what was your house? 1 84 What was the distance from the school? We just put this antisense from school. No return that attribute. Let's get one for house three or one as well. Let's go for the square meters, 300 square meters. So hopefully now you're starting to see the power of how we can use classes and object oriented programming to start creating all these objects. So just to summarize what we've done, we just have to usar class like we've been doing in previous lectures. Give the cross the name capital letter, and then if we want to use it like we want to use a blueprint so we won't have all these attributes you can assign to new objects, you have to use this underscore underscore in it. Underscore underscore. Muster said that about 10 times now, so it's definitely new brain. Then you have to yourself, and then you have tow add in your attributes and then underneath, which takes a bit of time yourself dot attributes it was attributes. So then, when you want to create your objects, you did you just assign it to a variable right in the class than in brackets. You put in, well, the different attributes. So that's that. I can't recommend enough having going this yourself, getting practical with it, and then when you're ready, I'll see you in the next lecture. 61. Objects - using attributes in a method: So now that we've looked at how we can add in variables into a class on how we can add in methods, let's look at how we can use the two in combination. So, for example, let's say if we go back to our animals how we could have each of them with different noises as attributes. So the tiger went rather bird win squawk how we could have a methods within the class. There were then so print out the different noises for the different different animals. So it's give that train away. I just want to show you how this could be applied. We're gonna create a class and again let's just call it animals and then within this cost, because we're creating some kind of blueprint here we want Teoh used the in it so functions we do death unscored underscore in it. Underscore underscore. Then self measures have for now color Andi the noise. So then we just need to put in self got color equals color and so dot noise equals noise. So now we can What we call it isn't in Stan she ate. It's not important that you know that word, but when we create a new object. That's what we're doing. We're in Stan Sheeting. So I watched. The longer it is, create one that's creating our So in order to do this like we did in the last lecture which pain? Animals, they put in the attributes. So the color commute ground the noise could be Actually, that's had one more of musical. It's species species name. Or we could just given name, for example. Just keep it simple. Self doctor name. We didn't create it here as well. Whose name? So now listen, Stand. Shoot Our first object. Okay, Another is create 2nd 1 And we can call this dog crazy dog by putting in the animal animal . Kloss, of course. In the top, on a region of this name called Owl. This Give it a dog dog can be I had no like and then the noise could be bark. Okay, so I takes three position organs before were given. So So we've got three for each of these in this point of the hour. One Archie to run the sun again. There we go. That's perfect. Okay, So as it stands, we can just get one attribute by doing so. For example, owl dot name or Mr Color get the color brown. But now there's creates a new method in our class. Onda, we're just gonna quit describe, so we want to be able to describe what the animal is. So for this one always do spit itself. Then we can just print out something like this. The face older is in the name of the color on. It makes the noise that still format name, color, noise. Okay, so we've been able to create a new method within this class, and we have to do is include the parameter self. Just to say that we're referring to the to itself. So whatever the object is but referring to itself. So, for example, self here will be out because that's the name given a hit. So the our dot name got kind of nice and they were just running this function t to say what's name, color and noise is so well run. This seems to be happy here now, in order to actually run the method itself. I just put our dots describe, Of course, after include the empty brackets, but include the empty brackets would be happy. Much need to run these ones again. Problem here being I haven't included self. Here's herself. Don't name self color, self taught noise work. Better navigate the outers brown it makes a noise twit to. So essentially, we have to remember to use this self. In order to call these attributes, we have to use himself of the start of these. So now we're able to access the attributes off the object that's been in Stan. She hated with the methods insight. So we're now able to combine what we've had with grating object with new attributes and creating methods. So we have to, we have to remember is that when we're creating methods, if we want to be using the attributes we include, Self was a parameter on the winch to yourself dots than the name of the attributes that we want to use. Then we can call these methods simply by putting the name of the object that were created in this instance the hour they but dot in the name of the method. So if we did the dogs, for example, dogs black in the mix in all that noise buck. So now you have a lot of the pieces you need in order to get started with object oriented programming. So please, please do go away and have a good this yourself. Definitely make sure that you're able to do all of this yourself. So create across, use the definite Teoh, add in some attributes yourself on, then create a method. The could just be called without having to using attributes and then finally create a method where you can use the attributes within the object. So give that a go when you're feeling confident. I'll see you in the next lecture. 62. Changing variables in a class object: in the previous lectures were won over how you can create a class on how you can access Very brutal meant so that in in that sense, we created a class delicious quote Class A for now and then we could just store something. For example, X equals end, then to access that We just did the name of the class door ex. So you start stuff like that and then that grew into how weaken in Crete methods in here and finally, how you can even add are in create our own objects by providing attributes and using in it himself. So in this lecture, what we're going to be looking at is, let's say we create something here That's just but this is a jar, something I don't know. And then we initiated Norway. So self dot age equals age. Very basic class here that we can say me because hey, dots A 100. So now who put a dot age me dot age Then it will give us that the age of that. But what if we wanted to be able to you change what this ages over time so we could add a method for that we could call this death. Um, plus, yeah, let's say so. Like we were doing the previous lecture. When you're creating methods, that's a referring to to the after attributes in here, we have to use self. Then within a method we could just say self dot age plus equals one. So saying add one to the age every time that we run this this method. Basically, we could also have a method that would there could you print out nicely? What the ages. So should print death show age. Then we could just print your age. Your age is in the placeholders for it. Self storage. So hopefully this is all making sense to you. I'm going quite quickly through it because we've already gone over these concepts. So if that if that this point in the lecture you're feeling a little confused, then it's definitely a marker that you should go back to the previous lectures on. Make sure you're feeling very confident on creating or classes. You know, initiating things, initiating your attributes on, then creating methods in here. The only new thing that you should be surprised about is that you can now refer to attributes, and you can actually change them by adding in different methods. So let's give this a try. So in the school of something better, let's call this like class person. And then we could have me equals past who have a capital B here because we're creating class. Okay, says Run this. So now that's trying to you. Me? Don't show age should tell me that. Major 100 accent on Let's Nets now use the method Plus yet Okay, so now this should not be 101. If I run this again, she'd copped 100 t fantastic. So now you can see that we can use a method. We can refer to the the attributes within the class and we can change that number. Let's look at one more example. Let's say we're creating a computer game on. We want tohave in this computer game you create a character on. Then you can Let's say you have a certain amount health and you can take damage or you can heal. And also we want have ah method here that can show. Yeah, okay, this is how much health you have at the moment. Firstly, that let's do you class in the school that call it a warrior because that sounds like a computer character. So that's that's first initiated with the attributes that we want. So we have to include self to begin with, and then it's hard to things that's have name this. Have strength.