1
00:00:05,100 --> 00:00:10,640
In this video, I want to talk about some of the built in functions that come with Python. So it turns

2
00:00:10,640 --> 00:00:13,460
out that Python has quite a few functions built in.

3
00:00:13,460 --> 00:00:18,200
You can see I've got my browser open, and the link to this page is in the resources section of this video.

4
00:00:18,200 --> 00:00:19,710
So refer to that if you need to.

5
00:00:19,850 --> 00:00:23,510
And here is an alphabetical list of the built in functions that come with Python.

6
00:00:23,540 --> 00:00:28,850
So let me just scroll down, and put them all on screen. So we'll use most of these functions at various

7
00:00:28,850 --> 00:00:30,780
points in the course.

8
00:00:30,820 --> 00:00:38,120
Now we've already seen enumerate a few videos ago, and we've used id, input, int and len in earlier sections

9
00:00:38,120 --> 00:00:38,900
of the course.

10
00:00:39,110 --> 00:00:41,300
And you should recognize a few more as well.

11
00:00:41,450 --> 00:00:45,710
We've used min and max to get the smallest and largest values from an iterable.

12
00:00:45,800 --> 00:00:50,870
We printed the largest and smallest values in the odd and even lists, in our examples. And the print

13
00:00:50,870 --> 00:00:53,460
function should be very familiar by now.

14
00:00:53,510 --> 00:00:58,220
There's more I want to say about print later, because it's more versatile than what we've seen so far.

15
00:00:58,960 --> 00:01:05,239
And the last two functions that we've used in this table are str and sum. Sum adds up all the values in an

16
00:01:05,239 --> 00:01:07,290
iterable, and returns the total.

17
00:01:07,360 --> 00:01:12,440
And if you recall, I used that in the last section when we extracted values from the input, and calculated

18
00:01:12,440 --> 00:01:16,970
the total. And we used str in the buy_computer program.

19
00:01:16,970 --> 00:01:23,420
So if I switch over to my browser now - sorry to my IDE - and open up our buy_computer.py program

20
00:01:23,900 --> 00:01:25,750
you can see it was used on line 11.

21
00:01:25,760 --> 00:01:27,380
That's str.

22
00:01:27,380 --> 00:01:33,500
So the code puts the list of numbers that correspond to the menu options. But because the customer's input

23
00:01:33,500 --> 00:01:39,620
will be a string, we want the list to also contain strings. That lets us check if the customer's choice is

24
00:01:39,620 --> 00:01:41,360
in the list that we're building.

25
00:01:41,360 --> 00:01:47,660
So we convert the input values to strings, using the str function, on line 11 as you can see there.

26
00:01:48,800 --> 00:01:52,160
So we'll be using some more of these functions as we work through this course.

27
00:01:52,160 --> 00:01:57,640
So I'll just switch back to the browser now, to that table, to the list of built in functions.

28
00:01:57,710 --> 00:02:03,880
I suggest you bookmark this page and refer back to it when you want to know what functions Python provides.

29
00:02:04,070 --> 00:02:08,830
One question we get asked is "Do I have to memorize all these functions?".

30
00:02:08,900 --> 00:02:15,740
The answer to that is no. It's quite likely that you'll never use some of them. compile, eval and exec, for example

31
00:02:16,070 --> 00:02:20,960
are only useful if you're running a program to work with python source code. If you want to write your

32
00:02:20,960 --> 00:02:26,030
own version of IDLE, for example, then those functions might be useful. Unless you're working with complex

33
00:02:26,030 --> 00:02:26,840
numbers,

34
00:02:26,840 --> 00:02:29,830
the complex function won't be of much use to you.

35
00:02:29,930 --> 00:02:33,150
So there's really no point memorizing a load of stuff that you'll never use.

36
00:02:33,230 --> 00:02:38,060
What we suggest you do is read through this page, to get a feel for what the functions do. But don't

37
00:02:38,060 --> 00:02:43,550
try to memorize it all. In fact, don't even try to understand it all straight away. When you need one of

38
00:02:43,550 --> 00:02:49,190
the functions, check back in here to see what it's called and how to use it. To give a quick example.

39
00:02:49,190 --> 00:02:53,870
You might find that your program has to save lists of data, but you don't want to save data that's got

40
00:02:53,870 --> 00:02:54,600
nothing in it.

41
00:02:54,680 --> 00:03:00,710
You'd probably phrase that along the lines of "I need to say this list if it contains any data". Checking

42
00:03:00,710 --> 00:03:06,560
in the table of built in functions, you'll see the 'any' function - up there at the top. Click on any in

43
00:03:06,560 --> 00:03:07,820
the table there.

44
00:03:07,940 --> 00:03:15,230
In this case you can see that any returns True if there are any items in the iterable that evaluate to True.

45
00:03:15,230 --> 00:03:18,830
That's what you'd need, to decide whether to save your list or not.

46
00:03:18,980 --> 00:03:21,730
We covered truthy values in the last section.

47
00:03:22,160 --> 00:03:27,290
So Python contains a function that you can use, and you don't have to write your own code to test your

48
00:03:27,290 --> 00:03:28,370
lists.

49
00:03:28,370 --> 00:03:35,720
We'll be using any in a later section, as well as the function above it, all. Alright, so scrolling back

50
00:03:35,720 --> 00:03:36,560
up to that table

51
00:03:40,140 --> 00:03:43,610
the next function I want to look at, in the next video, is sorted.

52
00:03:43,620 --> 00:03:44,640
See you in the next video.

