#100DaysOfCode in Python Transcripts
Chapter: Days 34-36: Refactoring / Pythonic code
Lecture: Refactoring 4: use built-ins / standard library

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Right. Next up, use built in, learn the standard library. There's some very powerful built in functions you can use that save you a lot of code.
0:10 For example, you run the range of numbers. Well instead of for e in, or do this classical for loop. I'm not even sure how to do it anymore.
0:23 Now, in Python you can just numbers equals range 1 11 and the first. The start is inclusive, and the end is exclusive.
0:32 So just issue a numbers range of 1 through 11 which doesn't say much but if I convert that into a list there you go, 1 to 10.
0:42 And I didn't have to go through a for loop specifying end boundary, etc. So very nice. What about sum? Let's sum up those numbers.
0:57 But in Python, you can just use sum, and look at that. It's easier, and less code, and saves you time. Let's look at max and min.
1:08 So let's create some data. Again to stay at the gym, I have routines and I have timings. Let's make a dictionary. Workout times.
1:21 Now see before I can use the zip with routines and timings and put that into the dict constructor to make a dictionary.
1:34 So here are the workouts, and the times and minutes they should take. What I want to do next is to get the workout that takes most time and less time.
1:44 Let's do it in the proposed way, if you wouldn't know about max.
2:03 Yeah, legs was 55 minutes which was easily visible here. Now let's see the max building in action,
2:11 and you will see that we can do this in one line of code. Workout times. items. Again items gets me a tuple, a list of tuples
2:23 of key value, in this case routine and timing. Then I can use the key optional arguments to give it a or callable or lambda.
2:30 In the lambda, I'm just saying what I want to sort 'em which is the value, the timing, the minutes. So this is basically telling max,
2:38 that I want to get the maximum value based on the value which in this case is minutes. There you go. Boom. That returns a tuple of key value.
2:47 Look at that. Compare one, two, three, four, five, six, seven, eight lines of code with one line of code accomplishing exactly the same thing.
2:55 You can use min in the same way. It should get me the core workout of 30 minutes. I mean it takes the same inner logic,
3:05 but as it is min it takes the min value. Look at that. Less code. Leveraging the built in functions from the standard library.

Talk Python's Mastodon Michael Kennedy's Mastodon