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