#100DaysOfCode in Python Transcripts
Chapter: Days 34-36: Refactoring / Pythonic code
Lecture: Refactoring 4: use built-ins / standard library
0:01 Next up, use built in, learn the standard library.
0:04 There's some very powerful built in functions
0:07 you can use that save you a lot of code.
0:09 For example, you run the range of numbers.
0:11 Well instead of for e in, or do this classical for loop.
0:16 I'm not even sure how to do it anymore.
0:22 Now, in Python you can just numbers equals range
0:26 1 11 and the first.
0:27 The start is inclusive, and the end is exclusive.
0:31 So just issue a numbers range of 1 through 11
0:35 which doesn't say much but if I convert that
0:39 into a list there you go, 1 to 10.
0:41 And I didn't have to go through a for loop
0:43 specifying end boundary, etc.
0:46 So very nice.
0:48 What about sum?
0:50 Let's sum up those numbers.
0:56 But in Python, you can just use sum,
0:59 and look at that.
1:02 It's easier, and less code, and saves you time.
1:05 Let's look at max and min.
1:07 So let's create some data.
1:10 Again to stay at the gym,
1:11 I have routines and I have timings.
1:13 Let's make a dictionary.
1:15 Workout times.
1:20 Now see before I can use the zip with routines
1:23 and timings and put that into the dict constructor
1:29 to make a dictionary.
1:33 So here are the workouts, and the times
1:36 and minutes they should take.
1:38 What I want to do next is to get the workout
1:40 that takes most time and less time.
1:43 Let's do it in the proposed way,
1:46 if you wouldn't know about max.
2:02 Yeah, legs was 55 minutes which was easily visible here.
2:06 Now let's see the max building in action,
2:10 and you will see that we can do this in one line of code.
2:14 Workout times.
2:18 Again items gets me a tuple, a list of tuples
2:22 of key value, in this case routine and timing.
2:25 Then I can use the key optional arguments
2:27 to give it a or callable or lambda.
2:29 In the lambda, I'm just saying what I want to sort 'em
2:33 which is the value, the timing, the minutes.
2:35 So this is basically telling max,
2:37 that I want to get the maximum value based on the value
2:41 which in this case is minutes.
2:43 There you go.
2:45 That returns a tuple of key value.
2:46 Look at that.
2:47 Compare one, two, three, four, five, six, seven,
2:50 eight lines of code with one line of code
2:52 accomplishing exactly the same thing.
2:54 You can use min in the same way.
2:57 It should get me the core workout of 30 minutes.
3:01 I mean it takes the same inner logic,
3:04 but as it is min it takes the min value.
3:08 Look at that.
3:09 Less code.
3:10 Leveraging the built in functions from the standard library.