Python for Absolute Beginners Transcripts
Chapter: Organizing and reusing code with functions
Lecture: Intro to functions

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So far, we've written the most obvious and straightforward code that we can. And it's worked okay.
0:07 Our little guess the number of M&M's in the jar project that worked pretty well. But our code was lacking some really important things.
0:14 And that's organization. You'll find out that the simplest way to write code is not always the best for the long-term.
0:21 And Python has four major areas or ways to organize code. We have the ability to put it into multiple files
0:28 and group the stuff you put into individual files by the functionality or logic that all goes together there. We have what are called packages.
0:37 These are things we can install off the internet or share so it's kind of like multiple files but more so. And we have something called classes
0:44 for object-oriented programming. It's a bit of a more advanced topic. We're going to save that for another course.
0:50 But the most common and most important way to group Python functionality and to make code reusable is to use something called a function.
0:58 Now you've seen function used a ton. When we created that random number remember we had to randomly determine the number of M&M's?
1:05 Well, we went and imported the random library and we called a function called randint And it took a lower bound and upper bound.
1:13 It turns out that we can create these in our own program and they serve two really useful and important purposes.
1:19 First, they let us think at a much higher level. Instead of thinking about all the little tiny steps we can gather that up and group it in a function
1:27 and then we can just think about the core idea or the thing that that function does. For example, in that random int function
1:34 there's probably tons of complicated stuff going on but we don't have to think or care about that. We just know there's a random library
1:40 and we can call randint and it'll give us this random number between A and B that we pass in there. And that lets us think in just that level
1:48 not all the details about how you actually create random numbers properly and so on. So that's one important thing we're going to focus on
1:55 gathering the code up and just kind of giving it a name and only thinking about its general behavior. The other is reusability.
2:02 Remember when we created that loop so that we didn't have to ask the user 5 times and just copy that code 5 times, but instead
2:08 we put it in a loop, then we could do cool things like test, have they exceeded the number of attempts
2:13 or if they already won, let's stop going through the loop. That was kind of what we can do with functions but functions are way more powerful.
2:20 So we'll be able to gather up some functionality and the function will take data that goes in and it will return data that goes out
2:27 and we can pass different bits of data at different times and reuse that functionality. So in this chapter, we're going to focus on functions
2:34 looping our codes so that we can think more clearly and high-level about it. And then reusing that code through functions as well.

Talk Python's Mastodon Michael Kennedy's Mastodon