Python Workshop for Beginners: Find The Odds For A New Lottery
Skills:
Python for Data70%
Key Takeaways
Writes Python code to calculate the odds of a new lottery
Full Transcript
Hello, hello, hello, hello. Good morning, afternoon, evening, depending where you are in the world. Welcome to this Python for beginners workshop. Um, some of you may have been here for one of the previous workshops we had last week and the week before. Uh, in that case, welcome back. Some of you maybe it's the first time you're coming across these workshops. In that case, welcome. Uh quick intro. I'm Stephen Steven Grapet. I'm one of the core team members here at Real Python. And I'll be with you for the next 60 to 90 minutes. Um today's workshop is not as long. Those of you who came last week and week before, they were slightly longer. Today is a slightly shorter project, but that also means we may have more time to expand more on some of the basics and fundamentals of Python. So 6 to 90 minutes depending on how many questions you have for me later on. Um just to make sure everyone is in the right place. As it says on the tin, this is a workshop for beginners. If you've never done any programming before, you're in the right place. If you've never done um in fact, let me let me stop the timer here. I don't need anymore. Uh there we go. And let's move on to my first page. Still says Python for beginners. So you still so if you've never done any coding before this is the right place. Uh if you've done a bit of Python may maybe you've done the basics this is still the right place. You'll find some of the things I'll talk about may not be entirely new. But one thing I like to do a lot when I'm introducing concepts in Python is to not only show you how to do things but why we do things and to go a bit deeper underneath the surface. So even if a topic I cover today you may have covered it before uh I'm sure you you still get a deeper understanding of it. If however you're someone who's very proficient with Python this is probably not the workshop for you. You're more than welcome to stay. Um you know um um that's perfectly fine. You might enjoy it. Um but just so that you know what to expect. This is a a workshop for beginners. Um it is a project we're going to be working on. of course limited in scope because we have about 60 to 90 minutes. So, we're not going to try to reinvent uh sort of reinvent the wheel. However, um it's a hands-on project. So, as much as you're more than welcome to uh watch this workshop um while eating popcorn like you're not Netflix uh series, the best way to get to this workshop is or rather the the best way to get most out of this workshop is to code along with me. I will not be going too fast so that those of you hopefully most of you who are coding along with me you can code along you might have some issues and that's where the interaction comes in um the format of the so code along with me is my message um and if you have questions um ask me now there's going to be a Q&A at the end but do not wait until the end of the session to ask questions depending on where you are there are different audience audience is here. Those of you who are on Zoom, you know how Zoom works. Raise your hand and uh I'll I'll bring you up and you can ask your question. Otherwise, depending on where you are, you may be able to chat and I'll be able to see your chat. So, if you're on uh YouTube or Facebook, um um I'll I'll be able to see the comments you put in. Some channels, for example, if you're watching on X, the the messages don't come through to me. So, um, if you have a question, feel free to jump to one of the other platforms or jump on Zoom. Those of you who want to switch to Zoom, simply go to realpython.com/live, live, and if you're on some other platform and you want to be on Zoom, you can always switch by going to realpython.com/live. Um, good. I can see some messages coming in. Uh, this is your first time. Brilliant. Um, welcome. Um, someone is watching on Facebook. That's great. And I can see the messages obviously as you can see. Um, okay. Let's make a start. One thing I wanted to mention is we will be covering quite a few topics in the next 60 to 90 minutes. It's 60 to 90 minutes. So, we won't be able to cover every single topic we talk about in extensive detail. We're going to be we're going to cover them for what we need for this project. However, um, we've got some resources for you. Um, I'll keep showing you this page, so don't worry if you can't write it down yet, but realpython.com/bonus, uh, you'll find all the resources related to this workshop. I'll tell you more about what you're going to find later on. And this includes additional resources. So, every topic we'll talk about today, if you want to take the next step and go deeper, I've uh cured a list of resources for you, which you'll find at realpython.com/bonus. Um, good. I've mentioned everything I wanted to mention. I will keep an eye if you see me look to the left, my left, whatever it is. I've got my second screen there with the chats, so I'll be able to see your chat. Those of you on Zoom can raise their hand. Um, I'll keep an eye on the chat all the time, but if I miss any questions, I'll come back to them later. And if I miss anything, let me know and ask again. Uh, good, good, good. Let us get started. So, um, I'm using PyCharm as the editor, but it doesn't matter. As long as you have something to code on, you're perfectly fine. Uh, and notice if you if you can't remember the URL I gave you with where where the resources are, that's realython.com/bonus. So, at some point before the end of the session, make sure you go there and get all the resources and I'll tell you more about what's there later on. Um, good. We have a project. Let me move things around on my two screens. We are going to write a program. The purpose of this project is of course to get some to get a concrete project done, but it's to learn Python along the way. So the main purpose is to identify a number of topics in Python and and explore them as we go through. But we have a project to work on and the project is the following. Um let's assume that someone came up with a new lottery. Um it's a bit of a weird lottery. And the way this lottery works is as follows. Um you get two random numbers between 1 and 20. And you're a winner if one of the numbers is exactly the double of the other. Maybe it's not an exciting lottery, right? It doesn't matter. It's it's our exercise, our lottery. And what we want to do, let's assume you're the person responsible for setting up this lottery. Um you want to figure out what the odds of winning are. Now this lottery is not those of you who are comfortable with the math side of things. Uh you can actually work this out relatively not it's not too difficult to work out the the sort of the the maths of it the mathematics of it. But we are going to solve this problem using Python. And sometimes there are problems that are significantly complex but the math solution is either not there or it's not straightforward. or maybe you want to confirm the math solution you came up with with a Python simulation. So that's why we're going to focus on solving this problem. What are the odds of this lottery? What are the odds of winning? So if I have a thousand people playing this game, how many do I expect to win? That's of of course that's an important uh aspect of a lottery, right? Um good. So a reminder uh two numbers between 1 and 20 are picked at random. the winner. There's a winner if one number is exactly double the other number and we're going to figure out the odds. Okay, let's make a start. Now, um the first thing I want to do, so we're going to start simply, right? And then we're going to make it a bit more complex. So, we're going to start running this lottery once. So, we would like our program to say, give me a random number between 1 and 20. Give me another random number of 20. Check if one of them is double the other. If it is, say you won. If not, say you lost. We're going to start with that and then we'll make it a bit more interesting because that doesn't tell us the odds, right? That's just running the lottery once. So then we'll have to see what we need to do in order to figure out the odds experimentally if you like. So using Python. So the first thing I want is to say I need two numbers that I want to store in my Python code. And these are storing numbers or storing information is something that's really important in coding. So I'm going to call my first thing first number. Um notice I did not put a space in there. We'll talk about these labels. First number is a label. Uh you can see it's underlined in red. If I try to run the program now, in fact, let me do it. Let me run the program now. Oops, I don't want to do that. I want to do that. And Python is going to tell me, I'm sorry. Um, I have no idea what first number is. It's not a word that Python understands. However, I'm going to follow this with an equals. And when I put an equals, Python says, okay, you're going to tell me what it is. So, these labels, it's up to me to call this anything I want. There's nothing special about the word or words first number in Python. I could have called this that if I wanted to. I shouldn't, right? Because we should create labels that describe what they're going to what what they're going to be attached to. But one of the rules is we can't have spaces. So, we put an underscore if you want to separate words. Or if you don't like the underscore, just put them together, but not a space. And for the time being, let's let me pick a number at random. Five. Of course, that's not random, right? Or rather, I've picked a number. I want my computer to pick a number. We'll come to this in a bit. But first, I want to focus on this is one of the most important aspects. Uh, random number 14. What I've done so far in these two lines of code, I've done quite a few things. So, on line one, you can see the line numbers there. First, I created something. In this case, it's the number five. In fact, Python calls this an integer. um because it's a whole number. And I told my program, I want you to take this bit of information, the number five, the integer five, and store it somewhere in the program because I'm going to use it later on. I like to think of this as as a, you know, a cardboard box like the ones you might have in in in in an office or in a storage room. So, the number five is the information I want to store. it went inside the box. But then I want to label the box and that's what I put before the equals on the on the on the left hand side of the equals. And this is why this label can be anything you want. Anything which describes what's on the box. Um it's like when you're moving house, you might have a box and then kitchen items or office items. That's what we're doing, right? We're putting a label so we know what's inside this box. And then I did the same thing again. So I've got two boxes now. One labeled first number and inside is number five. one label second number inside the number 14. So from now on whenever I use those labels for example I can say print first number and if I run this code it's not very exciting but it prints it doesn't print fir etc etc it finds first number and says ah this is a label let me check what's inside the box number five so it prints onto the screen the number five incidentally print is one of the most used most common it's quite boring but it's quite useful. Um, commands in in Python. Um, and it does what it what you've seen it do there, right? So, whatever I put in the parenthesis, it is showing it to us. Um, the user of the program is seeing what's at the bottom of my screen in this case, right? So, the top of my screen is the program, but when I run it at at the bottom, it's what the user sees. In this case, number five. Um, I've already mentioned two topics. The first one is storing information. We call these variables sometimes. There's lots lots lots of names for them, right? So you often hear we're creating an object and storing it in a variable. Uh in realpython.com/bonus, you can see the link there on the right of my screen. One of the resources focuses a lot about variables and objects and how they link together. This m might seem very simple hopefully the way you I've described it, but there's a lot more to it. So if you want to dive deeper um real python.com/bonus likewise print and its cousin input are common ways of inputting and outputting information. So it's the way the computer program communicates with the user. Once again in realython.com/bonus one of the resources there is about input output like print input and more. Um okay so far any questions I'll keep an eye on the chat. A reminder if you have questions doesn't matter what the questions are. Uh if you're on Zoom or Facebook or LinkedIn or um not all the platforms allow chat so ask the questions. I'll keep an eye on them. If you're on Zoom you can also raise your hand. If anyone on Zoom wants to speak it gives everyone a break from my voice which may maybe not now but maybe in half an hour's time you'll want one. Okay let's move on. Um let me use print again but now instead of just printing first number I would like to print a more sensible message. Right? So for example I want to say here uh your numbers are now if you're coding along with me do not type what I'm going to type in next. Well you can but I'm going to change it in a bit. Um your numbers are five and 14. Uh, first of all, you may have noticed that I've put this text in quotation marks. In Python, it doesn't matter whether it's double quotes or single quotes. So, pick one and stick with it. Um, you may recall that when I showed the five and the 14, I said Python understands these as integers, whole numbers. Uh, this is the type of data. Every bit of information has a certain type. Integers are numbers, right? So, we can do with them whatever you can expect numbers to do. Anything in quotation marks double or single is a string of characters. In fact, we call it string for short. And um I'm telling Python here that anything within the quotation marks, it's not a special Python word. It's just characters that a human being will understand. And if I run this code, print now shows me your numbers are five and 14. But there's a problem here. um this will always show me five and 14. So when these numbers change and very soon we're going to be picking numbers at random. Well this five is not the number five. This is the symbol the character that represents the number five. This might seem weird right? You might think isn't that the same thing? No in Python this is the character for number five. the you know whichever way you write it and this is the character for one and the character for four. Python does not understand these as numbers because they're inside quotation marks but it does understand those number as numbers. If this doesn't make full sense don't worry um it will as you understand the different types of data more. So I cannot do this. I would like this string to be a bit more fancy than this. I would like Python to figure out itself what to put here. Um now I cannot do this. What will happen if I do this? Um remember first number should be the label I used for the number five and second number should be the label I used for the number 14. I'm going to pause for a few seconds so that you can think what do you expect to see when I run this code. 5 4 3 2 1. In fact, someone there asked me in in someone in the Zoom chat said it will print that as words and you're correct. Python is going to take this literally. So, first number there is just the character fir. We don't want that. Luckily, there is a way of making this string fancier. And the way we make it fancier is by adding an F in front of the quotation marks. The F does not stand for fancy, unfortunately. It would be nice if it did. Um, it stands for a formatted string. Why? Because I'm going to be able to format the string. And what does this mean? This means that I can put some placeholders in there. And I'm using the curly brackets or braces. Call them whatever you want. Uh, by the way, the type of brackets and parenthesis we use is really important. Um, I didn't quite mention this when I typed in print, but after print, we've used parenthesis. The round brackets, call them whatever you want. U, this is something very common. Uh, this is what we call a function in Python. And a function is it's like the verb. It does something. And in Python, a function always has parenthesis. Sometimes there's something inside it, such as in this case. Sometimes you might have a a function with parenthesis with nothing inside. But the parentheses in a function are do this thing, print this stuff. Now, um, however, here I've used a different type of bracket. It's the curly brackets, braces, whatever you want to call them. And I'm going to do the same for second number. So, open parenthesis, second underscore number, open, right, apologies, open curly brackets, second underscore number, close curly brackets. Now this is different because I've put the f there formatted string. Python is going to look for these curly braces and it's going to treat them as placeholders which means rather than showing me f i r s t is going to say ah let me go and fetch this. You may notice the color change here. Yours may be different colors but the string in this case is being shown as green letters but first number is displayed in black just like this label. So, Python now understands this as the label in my code is going to fetch what's inside this box box five and therefore it will replace this curly brackets with five in this case or 14 in that case. Let me run the code your numbers are five and 14. So now if I change one of these numbers for example if second number is nine or eight I said nine but pressed eight doesn't matter when I run the code I did not change anything in line four but now it says your numbers are five and eight. This is how the computer program is. This is why we're storing this information because I'm going to be using it over and over again. Um these are a very powerful type of string in Python. They're called formitted strings or no one no one calls them that. Everyone calls them frstrings. Once again in realpython.com/bonus, one of the resources, two of the resources there actually um an article and a video course, pick whichever you want, is about frings. If you want to dive deeper into fstrings in Python, uh they're they've been around for a few years, but they're relatively new in Python. Well, now they're not that new, but compared to the lifetime of Python, they they're relatively recent, but they've become a really powerful tool in Python. So, uh, more about them in the resources I've shared with you. Um, good. I did promise you I'm going to start slowly. Um, if there are any questions, ask me. I will now speed up a bit. Uh, so again, I won't speed up too much. However, good. So far so good. But I'm having to put in numbers here. I want Python to pick random numbers for me. Now, Python is a programming language. It has lots of commands or words such as print um um the equal sign. These are the only two we've used so far, right? As it were. But so these are the ones out of the box, right? Any Python program you're writing, you can always write printed to be there. You can always use the equal signs. It's always going to be there. And there are many others like this. But Python is a is a vast language. It has lots of tools for you to be able to use them in any application you want. Um maybe you're working with graphics. So you have um sets of commands to do with graphics. Uh maybe you're working in in science. Um I used to be a scientist in the past. So a lot in fact the type of simulations we're doing here um I created slightly more complex ones perhaps as part of my scientific work right where you're creating a Python program to simulate the real world there are lots of commands specific for science for finance for web development anything you want to do Python is going to have a set of commands for you but they're not automatically there like print so what we need to do is we need to bring them into our program and we normally always do this right at the top of our program. And the Python word that does this is import. It's like we're importing something from somewhere else, right? And in this case, I'm going to import a batch of commands called random. So now random is one of these. We call this a module. So think of it as as a folder with lots of commands inside. So you brought this folder from wherever it was in the library onto your desk. So now uh your desk is your computer program, right? uh whenever you want to use random everything inside random is available on your desk. Now random comes by default with Python. So if you have Python on your computer or if you're using Python you will always have random. Some of these modules do not come by default but you can easily install them. So the more complex ones you might come across some that when you type them there Python says oh I don't know what this module is. You can download it and from that point on you can use it but random you don't need to download. um it's comes part of Python. Good. So, we've got this folder called random. Now, let me replace number five. Why? Because now I don't want to type in a number myself. I want my computer to fetch a random number for me. And no prizes for guessing what sort of commands are inside random. They're commands that are going to deal with randomness and random numbers. So how do I fetch a command from this this folder this module to use it proper name? Well, first I give it its name so that Python knows to fetch the random folder from the desk and then I'm going to put a dot in the dot full stop period. Call it whatever you want is a very common punctuation mark in Python. In this case it means open the folder and look inside. And you can start typing the command. Um, we're going to use a command called rend int. Rand stands for, you guessed it, um, and int, I've mentioned integer before, stands for integer. So, this gives me a random integer. But this is not enough. Rend is a similar structure in Python to print. Remember when we show print, I said this is a function. It's like a verb that does something. Um, and I said functions always have parenthesis after them. So let's put the parenthesis after rendint. Now does rendint need anything in the parenthesis? And the answer is yes. Now I am telling you all of this stuff, right? Because I know how rendant works. If you're ever using something that you're not familiar with, that's where you can use the Python documentation and look it up and it will tell you rendant needs two numbers. Uh the minimum and the maximum. So it needs to choose a random number. Our lottery was between 1 and 20. A reminder for those who maybe have just joined. Um we are simulating a lot lottery in which you come up with two random numbers between one and 20 and if one of them is the double of the other you win otherwise you lose. So from the random module use the rand command or function and provided with the minimum and maximum. So now when I run this code, Python will pick a random number between 1 and 20 and store it in first number. If I run the code now, you'll see that your numbers are six and eight. But if you run this code, you may very well not get six. In fact, there's a a one in 20 chance, right, that you're going to get six. In fact, if I run the code again, your numbers are now 10 and eight. So you can guess what we're going to do with second number. We're going to do exactly the same thing. Random.rand. And by the way, notice that we like to be lazy in programming. Right? So, the editor I'm using, and I'm fairly sure yours does the same thing. When you start typing something, it autocompletes. And we want to be lazy. The moment I see that the first one in the list here is the one I want, I hit enter, and then I don't have to type more. Um, it might not seem like much, right? But when you're writing lots of code, this helps. So now every time I run this code, I'm going to have two random numbers. Your numbers are 18 and one. So far so good. Good. Um later on in this session, I will change how we're picking these numbers because there's a depending on how we want our lottery to work, right? There's a slight flow with this version. But let's move on. We'll come back to it later on. I've got two numbers. I've got the computer to choose them. I've stored them into two variables, first number and second number. And then I'm printing them out using an fstring. A reminder, real python.com/bonus gives you more about all of these topics. So, get the resources um at some point. Um what do we need to do next? We need to get the computer to decide whether we've won or lost. And the rule is if a number is double the other number, then you win. Otherwise, you lose. Good. Now, this is where programming comes in. We need to think of the logic. In fact, the first part of this doesn't need programming. Let's think of what all the issues are. um am I guaranteed that first number is going to be the larger one and second number is going to be the smaller one as it is here. So should I compare first number and check whether it's double second number? Well, I can't do that because sometimes let me run it again. There you go. In this case, I got two and nine. In this case, the second number is larger. I always want to check whether the larger number is the double of the smaller number. Now, we have a number of ways of doing this. And uh one thing that's common in all of programming is that there's always more than one solution to a problem. And in fact, as you become more proficient, you'll always find different solutions that are better, more efficient. But today, we're not looking at efficiency, right? So we're going to look at two different ways of doing this. Um and what I'd like to do here is I'd like to say, well, hey, Python, you figure out which of these is the larger one. and check whether it is twice the size of whichever one is the smaller one. And it's up to you to figure out which is larger, which is smaller because I can't know whether it's the first one or the second one. So there are two things I need to do there. First, I need to ask Python, can you find which is the larger of these two? And can you find which is the smaller of these two? There are many ways we could do this, but some of the built-in tools that Python has just like print. Uh, in fact, let me print. But inside print, I'm going to use another function, a function called max. You can guess what this does, right? And I'm going to say first number, second number. And line six has printed 20. There your numbers are five and 20. Max will simply find the largest of them. And surprise surprise, there's also a min. Uh these are two of the many we call them the built-in functions like print, min, max. They're always there in Python and you can see why they're useful, right? So I have a way of telling Python you figure out which is the maximum from first and second or the minimum from first and second. So let's park this on the side, right? So we have max and min. I'm going to delete this line, but don't worry, we'll write it again in a bit. So I have a way of saying Python, you decide which is the largest, which is the smallest, max and min. But I need Python to make a decision for me. The decision now is, is the larger number exactly equal to twice the smaller number? And if it is, for the time being, we're going to say, "Tell me, well done. You've won the lottery." Whatever. If it isn't, I want you to tell me, "Sorry, better luck next time." This is where Python needs to make a decision for us. I cannot make this decision when I'm writing the program. Why? Because every time I run this program, first number and second number will be different. So, this is a decision that I cannot make as a programmer. I need to ask my computer program to make that decision. And whenever Python needs to make a decision, we have a tool to do this. And this tool is remarkably similar to how you would say this in English. Let's say it in English and write it in Python. If means the same in Python as it does in English. There are some more subtleties to it, of course, as you would expect. If what? If the larger of the two numbers, and we've seen how to do this. max first number second number. So the bit I'm highlighting there is the function max which gives me back we often say a function returns one of these numbers whichever is larger. Um in this example it would have returned 16 but depending what the numbers are it will return something else. If this number is equal to twice the minimum of first number and second number. I'm not trying to write the the neatest solution right here or the most efficient solution. I'm just getting us to think of how we build computer programs. Okay. There are a few things I've written in this line that I've skipped on. Right? First of all, that's the multiplication as it is in every bit of program of of computing. Right? So, um, I'm multiplying two by the minimum number. Um, notice I put a colon there. I'll tell you what the colon does. A colon is another bit of punctuation that appears very often in Python. But, uh, you may have been saying, "Hey, Steven, you've got a typo there. By mistake, you typed in two equals instead of one." No. Um, I can make mistakes, and I do make mistakes, but this is not one of them. Um, double equals has a different meaning to single equals. Even though when we're speaking in English, we would use equals and get the difference from the context. Let's see what the two mean. The first one we've talked about before, right? The first one, the technical term is it's an assignment. In my vision, I'm taking some information which is on the right hand side, random number, storing it in a box labeled first number. So I'm assigning this number to the variable first number. That's what equals is doing. It's storing information somewhere in my program. The double equals is doing something different. Let me highlight the relevant part there. This is the double equals expression. There are two things something on the left and something on the right. And double equals is not assigning something to something else. It's asking Python, can you look at these two things on either side of the double equals and check whether they're identical? If they are, give me a thumbs up, which in Python speak is true. The the word true. If they're not, gives me a thumbs down. False. And if needs to be followed by something that Python understands is true or false. That's what this line is doing here. So the highlighted part will give me either true or false depending on whether the larger number is twice the smaller number if it is. And that's where the colon is going to come in. If this is true, so if the larger number is twice the smaller number, I would like Python to do whatever comes after the colon. I'm going to hit enter. And you may notice that the cursor did not go to the beginning of the line. Again, that's not a mistake. I'm going to write tell you what what this gap is in a bit. I'm going to simply print here. We've used print a few times, right? Um, well done. You won whatever. And you can have more lines depending on what you want to do. In this case, I only want to do one thing, right? And print. So whenever there's something like if, Python is going to check whether that expression is true or false. If it's true, it's going to look at what comes after the colon. It's going to look at every line that has this indent, this gap at the beginning of the line. In this case, there's only one line seven. But I could have more. I could have here um print another print. See you again next time. Whatever. Um they don't they don't have to be prints, right? They can be any lines of code. But the moment I have a line that does not have the indent, uh, blank lines don't mean anything in Python, right? So lines two, five, and nine are there to make it look pretty, to make it easier to read. What matters here is that line 10 is the first line that does not have that indent. So, Python knows that these are the lines is going to do if the maximum number is twice the minimum number. Let's try running this, right? And um five and 18. You can see what happened. It did not print well done you won. See you again next time. Why? Because 18, the larger number is not the double of five. But it still printed out your numbers are five and 18. We could argue that this line should be before, but in this case, it actually makes sense to put it here for you to understand what's happening. Right? So line 10 will always run because it does not have an indent. It's not the technical jargon is it is not part of the if block. The if block after the colon are all the lines that have an indent. In this case, lines seven and eight. That's the if statement, a way of making decisions in Python. And once again, there's a lot more to find out about uh conditional statements as we call them. Uh realpython.com/bonus. Uh I I'll keep reminding you so that if someone hasn't written down uh that's where you have the resources kit that I've compiled for you exploring all of these topics. So you'll find out a lot more about the if statement in the articles and video courses there. No questions so far. I'll keep going. A reminder, you can ask questions at any point you want. Don't wait at the end. There is a Q&A at the end, but feel free to ask as we go along. Good. We told Python to decide whether the larger number is twice the smaller number. And if it is, do something. What if it isn't? So, you can follow an if with an else. Now, else doesn't need a condition, right? because else will happen if that is false. So either this expression is true in which case we're going to do line seven and 8 or else it is false. So then we can do something else. I'm using print here, right? But these could be lines with any Python any valid Python code. print. Sorry, better luck next time or whatever you want to write. So, let me run the code now. And um the numbers are three and 16. So, we didn't win. Sorry, better luck next time. You can keep running this, right? Um we don't know the odds of this this lottery, right, yet? So, I'm going to run it a few more times just in case I'm going to be lucky one time. Why? Because we need to make sure this works, right? Um, we can I'm going to do it five more times. Five, four, three, two. I'm not very lucky, am I? One. There we go. Um, I wasn't very lucky here, but if I want to check that this works, I could um go to these lines and temporarily remove them. Now, we don't want to delete them, right? Because if I delete them, I have to type them in again. Um, you know, even that line, I don't want to have to. So, what I'm going to do is um I'm going to put a hash symbol in front of them. The hash symbol is a way of telling Python, ignore whatever comes after it. You can see it's grayed out. So, lines three and four, it's as though they're not there. In fact, you can see that lots of red has popped up on my screen because now first number doesn't exist. If I run this code now, Python is going to tell me on line six, I have no idea what first number is. Name first number is not defined. Instead, I'm going to do the following just to double check that this works. First number equals five. Second number equals 10. I have hardcoded numbers which should give me a win, right? Well done. You won. See you again next time. Good. It works. To be double sure, I should swap them over. 10 and five. Well done. You won. So, this shows me that the logic is sound. Now, I don't need these. However, now I've done that. I've confirmed that this chunk of code, lines 8, 9, 10, 11, 12, does what I'm expecting it to do. I can go back to what I had before. Remove these lines and bring these back in. You see why I didn't delete them? because otherwise I'd have to type them back in. I want to be lazy. I don't want to be typing all over again. And we have our lottery. But so far I can only run it once. And every time I need to run the lottery, I have to run my program over and over and over again. So, we are going to fix this and get the program to run this lottery a million times, 10 million times, a huge number of times, and see how many times someone wins and how many times someone loses. And that's going to give us the odds. Now, you might say, "Oh, running this a 10 million times is a long time." Well, it is a long time if I give you a 20sided die and say, "Oh, can you do it manually?" But if we're asking the computer to do it, that's not going to take a huge amount of time. That's why we love computers. They don't complain. They do exactly what we tell them. Unfortunately, that's always that's not always a good thing, right? Because we need to be precise in how we talk to them because they will do exactly what we tell them. They don't complain. They don't get tired. Um, so we can get we can get them to do stuff that takes a long time for us humans to do. But before we do this, there is a problem here. Depends on how you define the lottery, right? But let's say that um to make this lottery fair or make sense, we want to make sure that the two numbers cannot be the same number. You might say, I don't care, right? In that case, leave it as it is. But at the moment, there's nothing say there's nothing stopping the program from picking exactly the same number. If I run this several times, there we go. Um, it's like I've planned this, right? Um, in this case, I got seven and seven. That's exactly what I was trying to show you. Um, I was lucky this time. Um, in terms of what I wanted to show you. I don't want that. Now, there are many ways I could sort this problem out, right? I could before check whenever I pick the second number, I could say, "Hey, Python, can you check is the same as the first?" And if it is, pick another number. But in order to introduce a number of concepts, I'm going to introduce a different concept here. So, I'm going to get rid of these lines, but as I've done before, I'm not going to delete them just so that if we need them again later, or if you want to see them, they're still there. Instead, I'm going to do the following. I'm going to ask Python to create a list of numbers, the numbers 1 to 20. So I'm going to have them all. 1 2 3. Now I could do the following, right? The way we create a list in Python is using the square brackets. So I could do the following. 1 2 3 4 5 6. Yon. I don't want to do that even though it's only numbers from 1 to 20. That's too much work. Um, luckily Python has a way of giving me the numbers from 1 to 20. And this is another built-in tool similar to print and max. Range is already there, right? And range also needs two numbers. But there's a quirk here in in Python. Um unlike rendint, in fact remnant is a bit of an exception in Python. Almost everywhere else in Python. Whenever you have a range of numbers from 1 to 20, the way the range works is that the first number, the start of the range is inclusive. So if I say from 1 to 21, I'm I mean include one but exclude 21. Notice that was in the case with rendant. It's one of those quirks, right? Once you know it, you know it. Uh it can lead to bugs because rendant is a bit of an exception. But range um uh Leani, I'll come to that in a second because I'm aware I've used the shortcut, but I didn't tell you about it. So, 2 seconds. Um, range 1 to 21 gives me the numbers 1 2 3 4 5 6 7 8 9 10 up to 20. Pause. Um, I've got a question on LinkedIn. How do you comment several lines of code together? And I actually did this without telling you how to do it. Let's assume I want to comment all of these lines. You do not want to go at the beginning of each line, press hashtag, and again and again and again. So, in every almost every programming environment you'll be in, select all the lines you want to comment. If you're on a Mac, command question mark. If you're on a Windows, control question mark. So, I'm on a Mac, so I'm going to do command question mark. And you can see that all of them have a hashtag in front of them without me having to go and do it manually. Next question. How about uncommenting them? Exactly the same shortcut. Select all the lines you want, command question mark, or if you're on a Windows, control question mark, and now they are uncommented. So that's what I was doing there. Um I wasn't typing in the hashtag myself. Uh thanks for the question, Elan. That's these shortcuts are really useful just like the autocomplete. Um there are many of them, right? You learn them as you go through. Um, so Joe asked me, "Any idea why they use control question mark instead of control hash symbol?" No, I do not know why. And there probably is a reason. Um, uh, the shortcut is probably actually control forward slash, but the forward slash is same same key as question mark. So I find it easier to say control question mark. I do not know why it's not control hashtag. Um, you know, however, that's a good thing because you may be aware the hash key is in different places on different keyboards. Um, if you if you swap between Macs and Windows, you probably know that the hash symbol swaps around. So, so, so in some ways it's good they don't use that. I do not know why is the answer. Um, uh, yeah. So, control slash the forward slash and the question mark are the same key, right? So it's control or command whatever key has question mark and forward slash is the same one. Okay um now range 1 to 21 I could use this as it is but uh I'm going to go off to a tangent here for a bit. So I'm going to go and use what's called an interactive environment. So on my editor there's a sign there um whatever you're using it will be called console or shell or number of things not not the terminal shell you'll know you're in the right place because you have these three chevrons arrows uh this is another Python environment where we can code um I'm using it only to demonstrate things right so if you can't find it don't worry what I write here is is for me to show you only so you don't need it for your code um incidentally uh this interactive environment which in Python is called the rappel RP l it's a useful tool once again in real python.com/bonus I've collated a number of to of resources for you if you want to find out more about this interactive environment you will find it there um uh yeah thank you someone on Zoom here said yeah to press the hash symbol you have to press control or shift or too too many things to press so that could be a good reason Um, I talked about types of data earlier. For example, I could have uh my number equals 10. A reminder, this creates a number 10, stores in the box with the label my number. Or I could have my name. And now I need to use the quotation marks to put in characters. Steven. Um, these are different types of data. In fact, there's a command in Python called type. And if I say, what is the type of my number? It tells me it's an integer. What is the type of my name? It tells me it's a string. Str is short for string in Python. So these data types, we're not going to talk about them in much detail today, but as you move on to your next steps, you'll learn more about you need to know every time you have a bit of information, what type of data is it? because you can do different things with types of data. Um, let me go for my numbers. Uh, my is just you can write anything you want right here. You can call these just numbers or whatever. And a list is that's square brackets. So, for example, I can do 1 2 3 4 separated by commas. And if I ask what type of data is my numbers, it's going to tell me it's a list. Now, range 1, 21, it feels like it should give me a list. But it doesn't. It gives me something else. Um, it gives me something called a range object, which is a a slightly more efficient way of storing numbers 1 to 21. But I don't want that. So I'm going to convert it to a list. And the way I do this in Python is by saying I would like you to see how I'm using once again list parenthesis. I've done this before, right? I've done it with range, with max, with print. list parenthesis, which means take that range of numbers and give it to me as an actual list. And you can see here it's got square brackets 1 2 3 4 5 6 7 up to 20. So this might look like a strange line because you might say, can't I, you know, if range 1 to 1, 21 gives me the numbers 1 to 20. Why do I need to do a bit more? It's because by default it gives me an efficient way. The reason it does this is because when I say range 1 to 21, unless I ask it to, it doesn't store all of these numbers in memory. That's too much space in memory. But when I say list, it means I do want them one next to the other. If this last point didn't make sense, don't worry. Um, I've got a question there before I move on. Um, oh, good. Okay. The question I have is specific about machine learning and AI. I'll come to it in the Q&A since it's not specifically about my code. That's okay, Ali. Um, if I forget, let me know. Um, uh, the short answer to your question is yes, it's still worth it, but I'll answer it at length later on. This was a question on LinkedIn for those on LinkedIn. Good. So, let me therefore do the same thing there. I've gone back. Let me close this interactive environment. So I've got list open parenthesis range open parenthesis 1 21 close parenthesis close parenthesis. We can nest these things inside each other. But this list I would like to store it somewhere in my program so I can use it later on. I've used the word store many times. Do you know what I need to do there? So I need to put it in a box and come up with a label for the box. I can call the label anything I want, right? So, let me call it numbers. Uh, once again, um, this could be any label you want. Uh, Mupavaram has a question for me. What's the difference between a list and a tpple? I'll give you a very short answer here because I want to move on and it's not relevant for this session, but in the Q&A, uh, come back to me if you want a longer answer. So for those of if you have never heard of a tpple, put your fingers in your ears for the next 30 30 seconds because you don't need to know. But a tpple is very similar to the list, but you cannot change its values. It's what we call immutable. So when you create a tpple of numbers, for example, 1 2 3 4 5 once you create it, you cannot make changes to it. And that makes it safer because you create it once and it can't be changed. A list is flexible. You can take stuff out, put stuff in, add more to it. That's the main difference between a list and a tle. In the Q&A, ask me again if you want a longer discussion about this. Um, that was a question on YouTube. Thank you, Mafaram. Good. So, I've got now all the numbers between 1 and 20. Remember, 21 is excluded. And I'd like to shuffle them. Why? Because I well I have two options, right? I can either shuffle them or I can ask Python to pick you know one of them. There are Let me do it this way. I would like to say first number I'm using the same label as before just for simplicity is now I can't say first number equals numbers. Why? because numbers is a list of 20 numbers. I want first number to be one of them. Um I would like Python to fetch one of these numbers. But when we fetch from a list uh we fetch by position. So for example I can say can you give me the first number in the list. Now another quirk in Python this is true for many programming languages not all. If I want the first number in the list, notice I put square brackets here, right after numbers. This is different from the square brackets I used before because I'm not creating the list here. I'm fetching a value from the list. You might be tempted to put in one there because it's the first one. But Python is a bit weird and it starts counting from zero. So if I have five fingers in Python, there 1 2 3 4 if I want to label them. This is weird, but you will get used to it. I promise. Um so this means fetch the first number from numbers and second number I would like to fetch. Now if zero is one, if zero is the first number, one is the second number. But I have a flaw here, right? This will always be the number one and this will always be the number two. Let me show you. Let me run the code. Your numbers are one and two. And you will always get this. I want to shuffle these numbers. Now, it's important to get the order right. Right. So, first I created my list of numbers. 1 2 3 4 5 up to 20. Now, shuffle is something that you do. You It's a random tool, right? So, where do you think I can find the command? There is a command called shuffle but I can't type it in. If I type in shuffle you can see it's underlined in red and uh sunder and zoom told me yeah it's inside the random module. So random.shuffle and now I can say I would like you to shuffle numbers. Now I'm going to skip on this right but when you use commands like these there's a bit of uh something you need to be aware of. Sometimes these commands shuffle the actual list and they say, "I'm going to take this list and shuffle it." Sometimes they give you a new list. So the original one stays unchanged. If you're unsure, all you have to do is print numbers. So notice how I'm creating numbers, which is 1 to 20, shuffling it just to check that it worked. Let me print numbers there. And you can see that when I printed it, it's 13, 11, 14, 15, 18. It's shuffled. And therefore, first number is now the first one in the shuffled list 13 and second number is the second one. That's how I don't need to print it anymore. Right? So I can remove this or now we know the shortcut. I can comment it. Okay, we're 55 minutes in. We're almost there. There isn't much left, but there's an important part left. So, we'll we'll we'll take another 10 minutes or so, 15 minutes or so to finish this project and then we'll have as much time as you want for questions. Um, I've been having a look at the questions there uh on uh Aditia on Zoom lists. Oh, sorry. Yeah, you were answering lists are containers. Um, now Adita, what you mentioned there is and I can see Sandra commented tpples are not key value pairs. No, that's dictionaries. So there are many of these data types. We've talked about list here. Someone asked um Mavaram asked about tpples. I briefly gave an answer. And then there's another one called dictionary. Um aditia that's the one that has a key value pair. Again in the Q&A at the end feel free to bring these questions up and I'm happy to discuss them since they're not relevant for my project. I'll leave them till till later. Uh good good. Okay, sip of water for me then. Um, plenty of topics so far. We've imported modules. We've used lists which is a data type. By the way, one of the resources in uh realpython.com/bonus uh is more about lists. So any of these topics, every single thing we talked about today, you can go much deeper if you want. Um I've collected a list of resources. Uh, make sure you get it before the end of the session. realpython.com/bonus. Um, there are some other goodies there, right? And I'll I'll tell you more later on or uh I've got a question from Curry which may or may not hear his name. Uh, I will talk about death uh in a in a bit. So, I'll get there right at the end of the session. Okay, let's uh move on. I would now like to run this lottery. This is one one play of the lottery, right? One person bought one ticket. I would like to do this one million times. Um and instead of printing well done, you won. I actually don't care about that. Uh by the way, I cannot leave these lines blank. If I have an if with a colon, I need at least one line that has something in. Um, in fact, I'm going to remove the else. We don't need it anymore. You'll see why. Okay, for now, I'm going to put three dots there because I have to put something in there. And three dots is actually valid Python code. It doesn't do anything, but it means that uh Python doesn't complain that after a colon, I did not put anything there. So, three dots. Sometimes you could put pass as well. It's a valid um way of doing it. Um Sunder, it's not the same as pass, but in this context, yes, it it does the same thing, right? It does nothing. Um it is the same as pass really. Yes, you use it in different context. I would like to I'm going to run this a million times, right? We'll see how to do it in a bit. Every time I get a win, I want to keep a telly. So, I start with zero. Every time uh there's a the larger number is twice the smaller number I'm going to add one and then two 3 four keep adding one. So I am going to create uh another uh variable as we've called it. Let's call it number of wins. I'm just going to call it n wins because it's shorter, right? But you can call it number of wins if you want. And I'm going to start by giving a value at the start of my experiment, my simulation. I'm going to start with zero. Why? Because every time I'm going to remove the three dots now because now I can say every time the larger number is twice the smaller number, I would like you to change the value of n wins. I would now want it equal to whatever it is at the moment plus one. Add one to it. So the first time this happens, n wins is zero. 0 + 1 is 1. So n wins is going to be one. But next time it happens, if it does, this will be 1. 1 + 1 is 2, etc., etc. So n wins will keep a tally of how many times we've won. How am I going to repeat? Those are the lines I want to repeat, right? Um, maybe, maybe not. How do I want How can I repeat this a million times? No, I'm not going to copy and paste a million times for obvious reasons. Um, we have whenever I want to repeat something in programming, you may recall with the if I said this is a decision, right? Whenever Python wants to make a decision, we use the if statement. Whenever you want to repeat the same chunk of code over and over and over again, Python has tools to do this and these are called loops. Now, there are two main types of loops in Python. Sunder mentioned one, the while loop. That's not the one I'm going to use here, but in the Q&A, I can tell you the difference between them. In this case, I'm going to use a different type of loop, and I'm going to write this line. It's going to look weird if you've not seen it before, but then I explain it. Um I'm going to say for uh I'm going to call this something deliberately right this could be any word I want in. Now remember range we used it to give us the numbers from 1 to 20. In this case I'm going to use it slightly differently. I'm going to put 1 million. This is the same by the way as doing zero to 1 million. So it's the same as writing 0 comma 1 million. But if you're starting from zero, you don't need to put zero in there. Um so the reason I'm using a for loop is because I know how many times I want to do this. It's a fixed number. Now you may say I want to change this number. But um to answer your question briefly, it's under the while loop is something you would do if you don't know if the program if you don't know how long the program wants to repeat and you're waiting for something to happen in the program. We won't be covering the while loop in this session, but we can discuss further in the Q&A. Um Aditia, is something a keyword? That's your answer. This is perfectly valid Python code. In the same way that these labels like numbers and first number and second number could be anything you want. The word you put in between for and in could be anything you want. Um I've put something there. Um you could call this anything you want. Right? So for repeating whatever you want. I'm going to keep it something here just to put some put something in there. Okay. So, uh, I've put a colon in there. We've seen the colon before. I'll tell you what it's doing in this case, but you can already guess from our previous use of a colon. Um, let me go to this this uh line for something in range a million. This is asking Python, hey, Python, can you please repeat the following lines of code, the ones after the colon, we'll come to it in a bit, a million times. So the colon is once again the lines I want you to repeat a million times are the ones coming after it but they need to be how does Python know which lines is it one? Is it two? Is it three? Is it all of them? I need to put an indent. And here's another shortcut for you. I'm not going to do it one line at a time. Just like with comments, I'm going to select both of these lines. These are the lines I want to put inside the for loop. These are the lines I want to repeat over and over again. I'm going to hit tab. It doesn't replace it with a tab. Tab means move them all forward by one tab or one indent. So now notice that after the colon at the end of four something in range 1 million, I've got two lines that have at least one indent. That let me do it manually. That and that. Now, line 16 also has another indent. Why? Because it's also after the colon of if. And this is something that until you get used to Python, the indents matter, right? These are not just to make the code look pretty. These gaps at the beginning of the line are part of the Python program. So, after the colon, I'm going to look for all the lines that have at least one indent. Those are the lines I want Python to repeat. Um, Petra had a question. Uh yes, this is a variable. Something is exactly the same variable as numbers equals or first number equal. So you're creating a variable in this for loop. I'm not using it. So I'm going to ignore it. But in some other loops, you might want to use this variable. It's going to be zero the first time, one the second time, two the third time, etc. That's answering a question on Zoom. Can you see a problem I have here? So this is going to repeat a million times. It's going to check whether the larger number is twice the smaller number. Every time it's going to add one. And what I'm going to do here is uh in fact I'm going to move this. You know the line that says your numbers are I'm going to move it further up. Cut and paste. Uh this is normal cut and paste. No special shortcuts here, right? The normal cut and paste you do everywhere else. Um, so as soon as I create the numbers, I'm going to show them. Then I'm going to run all of that and let's see what's going to happen. Uh, Daniela, no, Petar gave me a an answer there. Yes, sort of. Um, yes, Daniela gave me another answer. So, let me run this. I've got a couple of answers on Zoom. Um, your numbers are 17 and 15. And um well actually sorry my mistake I need to print here something right print um let's put another f string formatted string a reminder if you want to find out more about formatted strings uh resources are in /bonus um I'm going to say here proility of winning is Now I would like to here um do some calculations right um I would say I would like this to be now remember the formatted string I can put a placeholder just like I've done there with curly brackets and curly brackets but inside the curly brackets I can put any Python expression anything that Python understands and gives me a value it doesn't have to be a variable name for example Um here I can say as Sand suggested in one of the chats I want this to be n wins divided by 1 million and um I can actually go further right but let's do it like this for now let me run this code again now my goat doesn't work right because probability of winning is zero that is not true right that's not good what's the logic what's the problem here. How many times am I shuffling my numbers? Once. Remember, the only stuff that's repeated is whatever is inside the for loop. So, in this case, only lines 17 and 18. Once I shuffle the numbers, I need to reassign first number and second number. So, I need to take these three lines. I don't need print numbers, right? I'm going to remove it. I'm going to take these three lines, cut them from there, and paste them at the beginning of the for loop. So now I'm asking Python, here are the numbers. Here are the lines I want you to repeat. Shuffle the numbers. That's important. Now take the first one and the second one from the shuffled numbers. And now check whether the largest one, larger one is double the smaller one. Notice that this line doesn't work there anymore. I could move it inside the for loop, but do I really want this to be printed out a million times? No. So, I'm going to get rid of it. I'm I'm not going to display numbers, right? I've run the code. It's going to take a tiny bit longer. Right. There you go. Probability of winning is 0.05. And if you go and do the math, for those of you who are happy with the math, you'll find that yeah, that is correct. Um, of course, if you want to get a more accurate value, what do you do? Instead of 1 million, you run it 10 million times. And once you're happy with this, you can even run it a 100 million times. Run your code. Go and make yourself a cup of tea. Come back. Even if it takes a long time, right? It doesn't matter. Incidentally, let's assume I want to run this 10 million times. I've got a problem here. Not a problem. But I need to remember that I've used the number 1 million in two places. There and there. In fact, um, let me go even further. Um, by the way, I'm going to go quickly to this, right? Uh, when we use fstrings, there's lots of stuff we can do. So, inside the curly brackets there, can you see I've got um number of wins divided by 1 million. I'm asking Python to do the to do the math for me there. I can and I'm only going to show you this because I'm not going to explain it in detail, but a reminder, realython.com/bonus has resources about fstrings. I'm going to put at the after the 1 million a colon. And this colon is not the same as the one after the if and the four. We're reusing the same symbol. And I'm going to say, I'd like you to format my number a bit prettier than that. For example, I can say 2 means give me only two numbers after the decimal point. And if I run it now, we'll wait a bit and you'll see that it's going to only give me two numbers after a decimal point. Uh um well 0 uh 053 uh I think it's overridden that. Why did why did it override that? Um uh never mind. Um, again, we'll read more about F strings because I want to do one more thing here, which is even more interesting. I think I'm going to put a percentage sign there. Um, and we we'll wait. And this means format is a percentage for me. Sure, I can do the maths myself, right? Multiply by 100, etc., etc. I don't want to. I'm lazy. So, this means it's now showing me probability of winning is 5.24%. Notice that this number was 0.052. 0524. This is simply saying display it as a percentage. Python has lots of tools that are there to save you um you know um time and effort. Uh so then I'm going to write just to practice f strings um this is equivalent or rather this is n wins wins out of and here I'm going to put 1 million again and this is the problem right trials so I wanted to write more detail right so when I run this I I'll let it run while I talk to is also going to say look the percentage comes from so many wins uh 52668 wins out of 1 million trials that just so I know what the raw numbers are. So I want this to be 10 million I need to remember that there are three places I want to change the 1 million we avoid this in Python in coding in general I would like to make my life easier. So what we do in this case is instead of hard- coding a number there we can create another variable number of repeats again call this whatever you want right so I'm creating a new box now the label is n repeats and I'm going to put 1 million here now there's another trick in Python you can put an underscore in numbers it doesn't do anything this is the same as you know when you put a comma between to separate thousands or sometimes a dot um that's just to make it more visible. You'd agree that that's easier to see as 1 million than that. So now, wherever I want the 1 million, all I have to do is put and repeats. There are two places, right? And repeats and repeats and repeats. Why is this great? Because if I now want to run this 10 million times, all I have to do is change number in one place. Number of repeats is 10 million. Of course, it's going to take longer to run, right? So, I may or may not let it run. But while we're talking about this and I'll have a look at some of the good uh some of the questions, um I'll let it run. We've done a lot. Um I may um I'm thinking there's one more thing I could do. Um I might do it quickly. Yes, I might carry on. Uh but let me pause first. Are there any questions about what we've done so far? Anything? There we go. It's finished. The probability is 5.27% and now it's 526,000 to 822 wins out of 10 million trials, etc., etc. Um, I will add a bit more before going to the Q&A. Uh, by the way, I understand people will come and go from these sessions. Some of you may want to stay for the whole session. Um, I will share two things with you. Um, the code that I'm writing, uh, it's shared in real python.com/bonus. Once you get the the resource pack, you will have all the code we're writing. Um, you will also get a recording of the session. This won't be in the pack you get. Why? Because we're still in the session, but you'll get an email later today or tomorrow um, with a link to the recording. So, if you want to if you're missing parts of this, I should have mentioned this in the beginning as well for those who had to leave. Uh, so I'm going to keep going, but if anyone wants to leave and then you want to come and catch the recording, uh, make sure you go to real python.com/bonus in addition to getting all this stuff. Um, I'll also email you the link to the recording so you can watch it again later on. Aditia on Zoom. Uh, can you explain the use of end repeats here? Yes, I can. So, let me go back. I'm undoing, right? I'm going back a few steps. So, I'm just undo, undo, undo there. At the moment, I've put in 1 million there. So, I'm saying, can you repeat the following lines 1 million times, but when I printed this out, in order to get the the odds, I had to divide the number of wins by the same number. This number has to be the same number as that one. Because if I've run it one million times, I need to divide the number of wins by 1 million. And same there, I'm showing this is so many wins out of one million trials. Now the problem is that every time you want to change this number, you will have to change it in three places. There, there, and there. And these are close together in the code. You might have this number being used on line 10 and then on line 5,23 in your code. if you want to change it, you don't want to have to go and search your code to change it. So, what you're doing is you're creating what we call a variable. You can call this anything you want. A reminder, call this whatever you want, right? And I've made this 1 million or 10 million, whatever. So, what have I done here? The number 1 million, I've put it in a box and labeled it and repeats. So from now on whenever I want to refer to the number 1 million I don't have to say 1 million. All I have to do is use the label and repeats. Uh normally you do this the first time you're writing the code, right? I'm I'm doing this to demonstrate. But once you get used to it, when you get to this point, the first time you're going to write a million, you say, "Oh, wait a second. Let me not write a million here. Let me create a variable. Call it whatever you want." And then every time you need to use the 1 million, you use the variable name. This means that now all I have to do is use the label of the box. And if I want to change what's inside the box, I only have to do it once. I might want to run this 100 million times. This means it's going to change there, there, and there, and any other place I've used it in my code, which in this case is only three. Happy with that? Let me know AdT if that is not clear and in the Q&A part we can dive a bit further. Good. Um okay one last thing now I'm going to go through this super quickly because this is sort of an additional part. This is our program by the way the task uh n uh not what was it uh 75 minutes ago when we started I told you our aim is to find what the odds of this weird lottery are. You have two numbers. If the second one is the double of or if one of them is the double of the other, you win. What are the odds? Well, five there's 5% chance of winning. 5 point something% chance of winning. We've answered the question. We've done it. But let's make this code a bit neater. Why? Let's assume that um you would like um hello to Ignana. We're towards the end of the workshop, but it's recorded. Um, so for those joining now, I can see some of you mentioning um, realpython.com/bonus. In addition to resources, we will email you the link to the recording if you want to watch it from the beginning. Um, let's assume you want to experiment with different logics of how to do this, right? So maybe you say, well, you know what? Um, here I'm shuffling this list and then getting and then I'm using what if there are other ways I could do this? Of course, you can come in here and change the code, right? But sometimes it's convenient in Python. And I had a question earlier from uh someone who on on YouTube goes by the name of Curry um about defaf and defining functions. So that's what I'm going to introduce in this next five minutes. Sometimes it's useful if we take a chunk of code and we package it into a mini program. We give it a name so that every time we want this chunk of code to run rather than copying and pasting it, we can um use this mini program that we have packaged. So how do we do this? Um I'm going to it doesn't really matter where we do this, right? Um I'm going to do this right at the top just to make it easier. I'm going to remove these lines. I would like to create a new command, a new function. I've used function before, right? So, print was a function for example. Max was a function. These are the verbs of the language. Don't worry if you have to leave um the recording. As long as you've gone through your Python/ bonus, we'll email you a link with the recording. So, anyone who wants to leave u I've seen a message on Zoom. Um I would like to create a function of my own. Print already exists in Python. Max already exists. I would like to create a function for example called check for win which checks whether you know does that basically right? It checks whether we've won or not. And the way to create a new function in Python in fact we define a new function. And the word we use is def short for define. So here we're going to create our own function, our own verb if you like, an action, a mini program that does something. Give it a name. Uh you can call this anything you want, right? The same rules apply. We can't have spaces, so I'm going to put underscore there. And since this is a function, I need to put parentheses there and a colon at the end. Hopefully, you're getting used to these. The colon tells you what to expect, but I'll tell you as well in a bit. Now I could do this but I'm going to say look when I use check for when I use this command later on. Here I'm defining the command later I will use the command. That's the whole point of defining a a command right? I'm I'm I'm defining a new word and then use it later on. I'm going to tell Python whenever I use this command I'm going to give you two bits of information. You can call them anything you want. Right? I'm going to stick with the same names I had before. First number and second number. Again, these are names you come up with, right? So, what describes these best? So, I'm saying whenever I use or call this function is the term we use. I'm going to give you something and something else, two numbers. I hit enter. And you know the pattern now. There's a colon. So after the colon, we're going to have a chunk of code, a number of lines of code that has this indent. Whatever I write now is the definition of this function. Now, since I'm lazy, I'm going to go to my lines 15 and 16. These are the if statement, the one that says if max equals 2 * min, number of wins equals number of wins plus one. I'm going to get rid of it from there using cut. and I'm going to place it inside check for win. Now I need to bring more things in. Right? So here I'm I'm there's a fancy term for this refactoring. I'm taking the code that worked but I'm moving things around to make it a bit neater. So um um in fact um I'm not going to put this here. I'll tell you where we're going to deal with number of wins later on. So what I want to say is if the maximum number is twice the minimum number, I would like you to return. Now return is a keyword we use inside a function definition, which means that when this function finishes running, it's going to return something to the main program. And I want it to return true. I mentioned true or false before. um an expression like this in Python where there's an equals equals Python evaluates this as true or false. In this case, I'm saying um there's an easier way of doing this, right? By the way, um um I could simply return that for for those of you who are a bit more proficient, you might say, oh, but let me do it the long way here. Um sometimes the shortcuts are great later on, but when we're learning, we don't want too many shortcuts. Um same here. This else is not technically needed but it's nicer to do it. Else return false. So um there is a simpler way of writing this but this is better for us to understand. So I'm going to go for the long way here. This means that whenever I use this function I'm going to give it two numbers and then the function is going to give me either true thumbs up or false times down. So what what am I going to do now? Now I'm going to say here if same as before but now all I have to do is call this function if check for win first number second number and this is where the line I removed before I should have left it there my apologies. Now you might say this seems more complex than what I had before, right? It does the same thing, but it's like the code has gone all over the place. Let's see what we're doing here. So the code I have here is very similar to what I had before. But now the if statement follow what should follow an if. Something that Python understands is either true or false. I've got this function, the one I've created myself, check for when it still has parenthesis. Remember the parenthesis like print has parenthesis, max has parenthesis, and I'm giving it two numbers. Why? Because that's what I said I'm going to do when I created it. Now, check for when. What does it return? When it runs, it's going to return either true or false. Therefore, the if statement is still happy. Now, why did I do this? Because let's assume you say is there a better way of doing this? You could now and I'm not going to do this right but I'm just going to give you an idea of how we could do this. I could define check for when version two for example oops first and second it doesn't matter what you call them right let's be consistent first number it doesn't matter right you don't have to be now colon again Python doesn't like me to put a colon and nothing there so I'm going to put the three dots again but this is something that if you have another idea of how to check for a win. You can do this and then all you have to do is swap it in for instead of calling check for win, you can call check for win version two, but you're your your code remains the same. You're not doing lots of copying and pasting and you can easil easily swap between them. Also, you may want to use this function somewhere else in your code. So, you don't have to copy and paste it. Now, every time you need to check for a win, maybe you have a longer bit of code, you may want to reuse this bit of code. you've repackaged it in a function. Therefore, every time you want to run this code, all you have to do is say check for when and then give it the two numbers. We call this defining a function. So, that's the function definition. And once you've defined the function, you can call the function. So, you can use the function. And a reminder, there's a lot more about functions. You know what I'm going to say, right? If you've been here for a long time, uh realpython.com/bonus. some of the resources I've created there will talk about functions so you can dive further into these topics. Um yes, Adita in on Zoom asked me um yeah so effectively what we're saying here is we're only the logic is the same as what before the if statement was checking that line. What we've done now is we've taken that logic out of our main for loop and we've packaged it into its own mini program, its own function. So here number of wins equals number of wins plus one will only run it will only execute if this function check for win returns true. If it returns false, notice there's no else here. Why? Because I don't need an else. The else here is do nothing. So if the else is do nothing, I don't need to put it in. The else is only useful if you want to do something else if whatever it is is true. We made it. Um including this extra bit with functions. A reminder uh both versions of this code are available for you. So if you've been typing along with me, that's great. But if you want to check my versions, um, real python/bonus, you'll get access to the two versions, the first one and the second one. And that code there has a few extra things as well. Good. Um, I've got a question from uh, Mupavaram, but this is where I'm going to move to the Q&A. So, let me quickly shift to u here. You've already seen this, right? Um um um did I mention that there are a list of resources at real python.com/bonus? There we go. So um that's where you can get uh a list of tutorials and video courses about every single topic we talked about the code, a few extra goodies, and there's a lovely Python cheat sheet there with lots of useful stuff and by email later today or tomorrow a link to the recording. Now, those of you who need to leave, by all means, you can catch up with the Q&A on the recordings, but I'll stay for as long as people have questions within reason, but hopefully. Um, and I'm going to start. I've got uh um I've got a few questions. So, I've got uh YouTube questions. I've got some Zoom questions. Anyone else who is there and has questions, uh please come and ask. Let me go back to my um editor. Um, Mupavaram has uh instead of numbers zero and numbers one, can we have numbers three or any? So, let me uh reask the question and uh the answer is yes. But let me go back to this interactive environment um which we call the RPLE RP and let me create a list of numbers. I'm going to do it the boring way, right? So, square brackets and then put any numbers you want, right? Um, separated by commas there. So, I've created a list of numbers. Um, hello Poo. I've just seen you there on F on Facebook. Um, if you've just joined, by the way, the recording is available. Go to realpy.com search bonus to get an email after the session with the recording. Here's a list with a number of items. Now I can fetch the first one. I can fetch the second one. Yes, I can fetch the third one. Remember the numbers are off by one, right? So the first one is index zero. We call this an index. The second one is index one. Um I can go for the sixth one. 0 1 2 3 4 5. So index five is the sixth one. And you can see 0 1 2 3 4 5. That's number four. But you have to be careful because what if I say can I fetch the 13th one? Python says no. List index that number is the index is out of range. Why? Because there aren't 13 numbers. So uh yes you can but there are limits. And again when you learn more about lists and about we call this indexing. uh you'll find that there are many other ways you can uh play with this. I'll show you one for example, right? You can for example put a minus one in there. Um if you've not seen this before, have a guess what minus one might do. It gives me the last one. So there are a few other quick so so you can fetch whichever item you want and there are a few other fancy things. Um, I did provide a list, sorry, I did provide a resource about lists in realpython.com/bonus. So, if you're interested, go and have a look at those resources and you can explore more about indexing. Um, let me swap to Zoom questions. I've got I've got two chats, right? So, you might not all be able to see the questions, but I'll read them out. Um, Petar has a question. How does Python know that N wins acts only on true? Let me see whether I understand your question. Um, by the way, I don't want to put anyone if since you're on Zoom, if you want to jump in and uh if if I misunderstood your question, you're more than welcome to raise your hand if you want to, but if not, we keep using the chat. Don't worry. Um, so the question was, how does Python know that N wins X only on true? So it doesn't in some ways what it's doing is uh let's let's follow the life cycle of n wins. I'm creating it on line 14. So on line 13 n wins doesn't exist. On line 14 that's when I'm creating the label n wins. Remember my vision. It's a box. The label is n wins. Inside the box is the number zero. And I've placed it on a shelf in my office. Um, the next time n wins is used in the program is on line 22. But line 22 has an indent after an if statement, which means line 22 will only ever occur if that is true. and check for when is the function I wrote up there which will return back to the main program either true or false. So when first number and second number aren't correct as in one is not the double of the other check for win will give false. So Python will ignore line 22 it will skip it. So nothing happens to n wins. But when first number and second number are right, one is double of the other. Check for when returns true. So the if statement says ah I have true here. So I'm going to go and execute line 22. And only now Python will go and fetch the box and wins. Look what's inside. At the moment is zero. 0 + 1 is one. And then places one inside. So from now on the same box and wins has the value one. Line 22 will only ever happen when check for when is true. It will be ignored otherwise. That's how we are only incrementing the value when the numbers give us a win. Um so that was uh uh so Peter I'm looking at the number of your uh so n wins here will only increase if check for win is true. When check for when is false line 22 is ignored. Are are we happy with that? So line 22 will not happen every time. Um to answer your question uh yes you've given me a shortcut plus equals um I wanted to keep things simple in this workshop so I didn't want to introduce a weird looking shortcut but if you're familiar with plus equals absolutely that does the same thing. Um let me know Petar if there are still issues with what's happening there. I'll go to another question in the meantime but then I'll come back. Um, can we create two functions in the same file or program? Yes, you can create. In fact, that's what I'm doing here. I've defined check for win and check for win version two. Now, in check for win version two, I was just showing you you can do this. I did not write any code. So, check for win version two doesn't do anything here, but you can write and you can define as many functions as you want. So every time you want to define a function you'd go to a new line always starting from the beginning define some name you can call it whatever you want you need some parenthesis colon and then here is where you write the code so you can define as many functions as you please there's no limit in fact in most programs you will define many functions um let me know krie if that answers your question Um, is teacher available to students? Uh, thank you. Um, we do run live courses at Real Python. Um, and I I tend to run many of them. So, so the answer is yes. Um, we will have um, sorry I wasn't me I wasn't planning to plug in here, but since you're asking me um, we do have longer life courses. There's a beginner's course. Uh, we haven't published it yet, so you can't find it yet on the website, but it will it will start mid late September. That's an 8week course going through the fundamentals from the beginning. Um, and I run those courses. So, if you're interested, we do run longer courses. These are just oneoff workshops. Um, another question. Uh, let me go to Adita on Zoom. Oh, no. Sorry. Yeah, you were you were answering Petra's question there. It will only increase on true. Um, I think I think. Yeah. Yeah, you're answering your question. Good. Uh, can we use the input and def? I'm not sure, this is on a question on YouTube. Uh, Curry, I'm not sure I I understand the question. Um, if you're asking, can you use input inside the definition? Yes, you can. You can put, in fact, let me put a comment here. Remember the comment? Um, you can write any valid Python code here. If it's valid Python code, you can put it inside the definition. So, you can use input in a function here if you want. Any code that works anywhere, you can put it inside a function. Um, this is why I like to think of a function as a self-contained mini program. It's like a mini program inside a bigger program. So it does one specific thing. When it does it, it returns something back to the main program. In this case, it's returning true or false, but the function can return all sorts of things. Hopefully I understood your uh question, Curry. Um, oh yes, Alexander, I think I think I had asked this question earlier, but I missed it. Uh, can you please explain what dictionaries are? And someone else asked about tpples earlier. Now, um I'll do this very briefly if that's okay. Um just because we're steering out of the topic, but since you ask, I won't say no. Um we've seen lists, which are a list of it doesn't have to be numbers, right? It could be other things. Um doesn't matter what they are. And lists are great. they're an ordered sequence of things, but sometimes that's not what you want. Sometimes you want um two bits of information that relate to each other. Let me give you an example. Let's assume we're playing a game, you, me, and a few others. And we want to keep how many points each one of us has. I could create a list of names. Um, oops, I didn't mean to do that. So, I could say Stephen and Oops, sorry, that should be a comma. And, uh, Alexander, you're asking the question, so I'll put you in the Oops. Sorry, it's it's been too long. I can't type anymore. Let's assume it's me and you playing, right? So, this is also a list, but instead of numbers, I've put in strings. And then I can have a list of points. I've got 10 points and you've got 12 points. You're beating me. Anyway, but the problem is that these are two separate bits of information. There's a box with the names and the box with the points. I would like Python to know that Steven and 10 are connected to each other and Alexander and 12 are connected to each other. Why? Because 10 is my points and 12 is your points. That's where dictionaries comes in. Um, let me call this points dictionary. Now, the way we create a dictionary is using once again the curly brackets. But notice that these are not the same curly brackets we used in fst strings, but as in they're the same key on the keyboard, but they're not related. They're just we're reusing the same punctuation marks. And every item in a dictionary contains two parts. What we call the key and then what we call the value. And notice how again we're reusing the colon here. We're just reusing it. It's got no links with what we've done in earlier. This is a different column. It's saying Steven is associated with the number 10. Then I can put a comma and add another item. Oops. Alexander colon 12. And you can keep going, right? Comma, comma, comma, comma. Now this is different because now I cannot say can you fetch the first item. This is not a list anymore. The order doesn't matter, right? So if I do this, it says no. Doesn't make sense. Now I can say, can you fetch the number of points that Alexander has? And it says, yes, I can. 12. So you can see that the link is different here. In a list, what matters is I've got a number of items in order. In a dictionary, it's the link between the key and the value. And therefore, it's like a lookup table. From now on, whenever I want to check how many points Alexander has, I just put points dictionary Alexander. And if I want to say um Alexander say you got another 10 points. Uh there's the shortcut for this by the way, but I'm not using it. Someone asked me about the shortcut. I can say the Alexander's points are whatever they are now plus 10 more points. So now if I look at the points dictionary you can see that I'm still on 10 but Alexander you are on 22 because we've changed the value that stored linked to Alexander to whatever the value is now plus 10. There's a lot more to say about dictionaries. I'm trying to keep it super quick. Um and yes maam uh tpples. So let me go back to my numbers. This is a list. Now let me uh go to my tpple numbers and we don't need the parenthesis but I'm going to put them for clarity. Right. Um to create a tpple I'm going to use the same numbers 3a 4a 5a 2 comma 30 comma 455 nothing special about those numbers. So remember type my numbers is a list whereas my tpple numbers not because I put tpple in the name right but because I've created it in a different way is a tpple. Here's um thank you to LinkedIn user. Yeah. Um feel free to um those who need to leave need to leave. Let me make a change to both of them or try. For example, I want to take my numbers square bracket zero. That's the first number, right? I want to change it to be equal to 999. Remember my numbers is a list. And Python says, "Yeah, no problem. Have a look at my numbers now. The first number is now 999. The three has gone. The second number remains four etc etc. A list is something I can change. It's what we call mutable. So I can make changes to it. I can add. So we haven't seen this, right? But append will append another number to it. So you can see that now it starts with 99 because I had changed it and I've added 100. If I try to do the same thing with my tpple numbers, I can fetch a value from a tpple. Same thing square brackets zero gives me the first number in the tpple three. But if I try to change this, Python says no, no, no, no, no. You cannot make changes to a tpple. So this means that you would use a tpple when you want to create some information and make sure it doesn't change by mistake. Um, for example, you've got the um latitude and longitude of cities and you say London equals whatever, you know, well, they're not going to change, right? They're not values are going to change. The latitude and longitude of a city will always be the same. So, you might want to create them in a tpple to make it safer so that they can't by mistake change in your program. So, that's the main difference between list and a tpple. A list can be changed. We call it mutable. A tpple once you create it it's fixed and set in stone. There is more to say about these topics of course right but I'm giving a very um uh Aditia. Yes. So again I I don't want to get into yet another data type enums. Um enums are useful when you want a list of constants put together. uh tpples are a bit more general, right? Tpples are just a collection of data and you want it not to change effectively. Um there's always overlap between these data types, right? So um but yeah, enums are more meant to be as a collection of constants and you can sort of use easy to use names for those constants. Um what does append do? Uh you're right, I was quickly going there. So we talked about data types and then maybe I'll unless there are more questions I'll end on this. Uh you may recall let me create my name again which is a string and you may recall my numbers which is the list. Um I've made changes in this right. Now every type of data can Python can do something with those things. For example my name it's a string. It's a string of characters. If I press dot, Python is showing me all the things it can do with strings. Um, you can see lots of commands there. Um, I'm not going to go through them all, right? But for example, I can use upper, which means change the string to uppercase. Now, this makes sense for a string. It's a string of characters, right? But it doesn't make sense for numbers. If I try to sue to if I try to say my numbers. upper. You can see it's not autocompleting it for me. Python is going to tell me I don't know list doesn't have upper. It doesn't make sense. However, lists like my numbers have other things they can do including append which means add something to the end of the list. So currently my list is 9945 230 455 100. If I want to add another number to it, uh, 525. Now my numbers is the same as it was before, but I've appended another number at the end. I've added a number number at the end. Again, I'm going really quickly here because each one of these topics, we could spend an hour talking about them, right? So, um, this is about lists. uh at realpython.com/bonus. One of the resources I've g I've given you is about lists and you will read more about append and other of these we call them methods but don't worry about what they're called. Um I have a question from Joe on uh Zoom. Uh yes sorry there was someone who asked earlier. Uh let me find the question. Um, and then I have a question on YouTube as well. Uh, someone asked me earlier, is it still worth uh learning Python for AI? Let me find it. Um, I think it was on Zoom. So, I have two chats in front of me. This is on Spectator Sport, right? Uh, maybe it was not on Zoom then. It was Yes, here. Okay. It was on LinkedIn. Um someone asked me uh I want to ask how do you see the role of Python in ML and AI machine learning and AI well artificial intelligence in future is it still worth uh is it worth doing it with Python because almost everyone is doing yeah so two parts of this question right so Python has been one of the most popular languages for a long time for many reasons recently Python is the language of machine learning and AI. All of this AI stuff we're seeing, the primary language is developed with is Python. So obviously, Python has become even more important because of all of this AI stuff. So I think the question that Ali had asked me on LinkedIn a while ago was if I want to get into machine learning and AI, the answer is yes, you need Python. If you want to get into those fields, you have to learn Python. it is the language of AI and by and large language of machine learning and in fact sort of yeah these fields are very much um so so that was the question that was asked uh hasn't been asked here but a question that's been asked often recently elsewhere is with all of these LLMs that are doing coding for us do I still need to learn to code um depends what you want to do right um but for anything other than small projects uh you are never going to be able to trust blindly any code given to you without anyone checking what it does. If anything um the people who are making the best use of the AI tools for coding are the very proficient programmers. The more proficient you are in programming, the better you're going to use those AI tools. The quicker you're going to write code, the quicker you're going to learn. It is not a substitute for coding. It may be at some point in the future, but at the moment, I haven't seen a single project that's more than a toy project that's been done entirely by AI without a programmer being involved. Anything which is useful in the real world. Um, what AI is doing is making programmers a lot more proficient, a lot quicker, a lot better suited to upscale and upskill. Um I'll keep going because there are a few more questions. Maybe I'll go till on the hour. So another eight minutes. Uh two questions. One on YouTube. Uh can we use upper parentheses my numbers? We did the function earlier. This is a great question. Mavarum I this is another topic. So the question is why when I used upper I did not put upper open brackets my name um as we did before uh that's because upper is a different type of function. Uh you may see here that let me look at the functions we used in our program. Right? There is um max and min. Where are they? Max and min. I wrote max open bracket something. Min open bracket something. Uh print open bracket something. Even check for win which is a function we created. Those are standalone functions. Upper it's also a function but it's a different type of function. It's attached. That's what a dot is. It is attached to something else. In this case, it's attached to the string inside my name. So, upper this this function will always have access to the information in my name. So, I don't need to pass it to it. That's why I leave the brackets empty in this case. So, can you see that upper is already attached to my name and my name is the string Steven. So I don't need to put in the information there. Uh when you learn about these are called methods. They're a special type of function. When you learn about data types like strings and lists and and and then you learn about these methods, this will make a bit more sense. And in a few steps time, not yet, you learn about object-oriented programming. That's a bit more of an intermediate topic. And that's where all of this will make more sense. But that's the that's why I've used upper differently to the functions I've used in that code because it's already attached to what we call an object. It's already attached the string my name. Rafy on Facebook just to clarify does list.append make another copy or modify the existing list? So great question. uh because this links to the question about tpples and lists. When I explained the difference between tpples and lists, I said a list is a mutable type fancy word which means it's something that can change. So when I use let me go to my numbers at the moment it's that list right 99 when I put append let me put a th00and because lists can change this makes a change to the list itself. So notice how my numbers now has changed. But remember my name which is a string. Strings are like tpples. Once you create them, you cannot make changes within them. So when I say my name dotupper, notice how it seems similar to my numbers of append, right? The format. I've got the the variable name dot and then the the function or the method name. But this behaves differently. This gives me a copy. But my name has not changed. My name remains the original one with only the uppercase s. This has given me a copy. So then if you want to do something with it, for example, you would have to say something like this. My name equals my name.upper. This is very different now because I'm saying whatever copy of the string you return, I want you to store it back in the same box labeled my name. You can only have one variable. Therefore, this will replace whatever was inside my name, which is the normal case Steven with the uppercase my name. Uh we've gone off a bit. These are all topics we can discuss in a lot more detail, right? But hopefully I'm giving you very short answers to what are really long topics. Uh does this make say um Rafi? Good. Um we've gone through all these data types, right? You've asked me about lists and tpples, lists and dictionaries. Um one more question list and set. A set is yet another different type of and I'll I'll do this again very briefly. Let me create another list. Good. Um, I'm glad Rafy that worked. Um, that's my list. 1 3 5 6 4 3 7 9 4 6. Notice that there are repeats. The three appears twice. The six appears twice. The four appears twice or whatever. A set. So I can create a set from this list does not contain repeats. So a set only has unique values. So you can see that here it's showing me the order is not important anymore. Right? One, three is only there once. Even though in my list I had three twice. The set only contains unique numbers. So a set is useful when you want to ensure uniqueness that every value appears only once. Good. We are approaching the twohour mark. This was one of the shortest project in these workshops, but there were plenty of questions, which I love. Often the questions, the Q&A is where we can really get into it. It would be nice if we had lots more hours, right? But I think this would be a good time and also there are no more questions popping in. I can see. So, this is a good time to finish off. Let me go back to um my my slides. I've mentioned this as we went along. There is a um set of resources that I've compiled for you at realpython.com/bonus. You will get a number of links to real Python tutorials and video courses about every topic we talked about. You have the code. So if you want to see my version of the code and tidied up versions of them, there's a cheat sheet which is very useful. and not there yet, but you will get an email once you're um on that page. You'll get an email eventually once we have the link to the recording that usually take a couple of hours. We'll send you the link to the recording so that you can watch it again or if you had to miss bits of it or if you joined halfway through, you can go and watch the bit you missed. Good. I hope you enjoyed this. Um, well, those of you who've been here from the beginning, it's a 2-hour workshop. Um, hopefully you enjoyed it. First of all, hopefully you found it useful and hopefully you're going to go and build on all the things you've learned here. Um, use these resources at /bonus and uh, there's plenty more at Real Python, of course, to help you out. And, uh, you'll find me around at Real Python or elsewhere on social media. So, drop by wherever you see me, drop by and say hello. Good. Thank you all for joining. Um, thank you for Thank you to everyone. Those who've been here for the whole time, well done for sticking with it to the end. Um, we will have more of these workshops. Um, we had a set of three. So, this was the third one. We will have more, but they're not scheduled yet. So, keep an eye out on Real Python. Um, if you're signed up to Real Python newsletter, you'll see it there. Keep an eye on social media. We will have more of these workshops. We don't have dates yet, but we will have them soon. Take care. Bye-bye.
Original Description
Learn Python Fundamentals in this free live workshop. Join instructor Stephen Gruppetta in this hands-on session in which you'll learn Python basics while writing code to work out the odds of a new lottery
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Real Python · Real Python · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A better Python REPL – bpython vs python interpreter
Real Python
Introducing large-type.com – A Utility Website
Real Python
Reading Hacker News Without Wasting Tons of Time
Real Python
Forward References and Python 3 Type Hints
Real Python
Using Sublime Text as your Git Editor
Real Python
Python Code Linting and Auto-Complete for Sublime Text
Real Python
Make your Python Code More Readable with Custom Exceptions
Real Python
Write Better Tests with Sublime Text's Split Layout Feature
Real Python
How to Use Sublime Text from the Command Line
Real Python
Rename Variables with Multiple Selection in Sublime Text
Real Python
Sublime Text Settings for Writing PEP 8 Python
Real Python
Write Cleaner Python with Sublime Text's Indent Guides
Real Python
Sublime Text Whitespace Settings for Python Development
Real Python
Function Argument Unpacking in Python
Real Python
Python Code Review: Debugging and Refactoring "Conway's Game of Life" + Automated Tests
Real Python
Using "get()" to Return a Default Value from a Python Dict
Real Python
A Python Shorthand for Swapping Two Variables
Real Python
Python Code Review: Refactoring a Web Scraper, PEP 8 Style Guide Compliance, requirements.txt
Real Python
Click & Jump to Test Failures from the Command Line (iTerm2)
Real Python
Setting up Sublime Text for Python Developers
Real Python
Sublime Text + Python Guide Overview
Real Python
Python Code Review: Adding Pytest Tests to an Existing Python Web Scraper
Real Python
Type-Checking Python Programs With Type Hints and mypy
Real Python
A Shorthand for Merging Dictionaries in Python 3.5+
Real Python
Python Code Review Flask Web Security Tutorial + Virtualenvs, requirements.txt
Real Python
My Python Code Looks Ugly and Confusing – Help!
Real Python
Setting Up a Programmer Portfolio/Developer Blog – How To Get Started
Real Python
Do I Need a GitHub/GitLab/Bitbucket Profile as a Developer?
Real Python
Programmer Portfolio – Example and Walkthrough
Real Python
How to Get Your 1st Speaking Gig at a Tech Conference
Real Python
How to Build Your Public Speaking Skills as a Developer
Real Python
The Object-oriented Version of "Spaghetti Code" is "Lasagna Code" ?!
Real Python
Setting up Sublime Text for Python Developers – Lesson #1
Real Python
Cool New Features in Python 3.6
Real Python
"is" vs "==" in Python – What's the Difference? (And When to Use Each)
Real Python
Emulating switch/case Statements in Python with Dictionaries
Real Python
Python Function Argument Unpacking Tutorial (* and ** Operators)
Real Python
What Code Should I Put On My GitHub/GitLab/BitBucket Profile?
Real Python
A Crazy Python Dictionary Expression ?!
Real Python
String Conversion in Python: When to Use __repr__ vs __str__
Real Python
Method Types in Python OOP: @classmethod, @staticmethod, and Instance Methods
Real Python
Optional Arguments in Python With *args and **kwargs
Real Python
Python Context Managers and the "with" Statement (__enter__ & __exit__)
Real Python
Installing Python Packages with pip and virtualenv / venv
Real Python
"For Each" Loops in Python with enumerate() and range()
Real Python
Python Code Review: LibreOffice Automation and the Python Standard Library
Real Python
Managing Python Dependencies With Pip and Virtual Environments – Lesson #1
Real Python
Python Tutorial: List Comprehensions Step-By-Step
Real Python
Leveraging Python's Implicit "return None" Statements
Real Python
What's the meaning of underscores (_ & __) in Python variable names?
Real Python
Python Data Structures: Sets, Frozensets, and Multisets (Bags)
Real Python
Writing automated tests for Python command-line apps and scripts
Real Python
How to find great Python packages on PyPI, the Python Package Repository
Real Python
Immutable vs Mutable Objects in Python
Real Python
PyPI vs Warehouse, the Next-Generation Python Package Repository
Real Python
pep8.org — The Prettiest Way to View the PEP 8 Python Style Guide
Real Python
My Experience at PyCon 2017 in Portland
Real Python
Pylint Tutorial – How to Write Clean Python
Real Python
"Reverse a List in Python" Tutorial: Three Methods & How-to Demos
Real Python
Python Refactoring: "while True" Infinite Loops & The "input" Function
Real Python
More on: Python for Data
View skill →Related Reads
📰
📰
📰
📰
Swift typealias — What It Is, What It Does, and Why It Matters
Medium · Programming
s3fifo 1.0: Zero-Allocation S3-FIFO Cache for Node.js is Ready for Production
Dev.to · JeongSeop Byeon
Node.js Error Handling Patterns for Production Queue Systems
Dev.to · Faisal Nadeem
Make Your CLI Config Write Survive Ctrl-C Without Leaving Truncated JSON
Dev.to · Sam Rivera
🎓
Tutor Explanation
DeepCamp AI