1
00:00:01,710 --> 00:00:06,390
If you followed the previous sections of the course then you have come by a long way.

2
00:00:06,540 --> 00:00:09,320
So you now know the fundamentals of Python.

3
00:00:09,390 --> 00:00:11,830
You know how to solve fundamental problems.

4
00:00:11,850 --> 00:00:15,780
For example let's say you wanted the number of characters of this string.

5
00:00:15,990 --> 00:00:20,030
So you have methods and you have functions that you can use.

6
00:00:20,030 --> 00:00:26,970
So for methods you can search with dir str you'd see a bunch of methods here that could be applied

7
00:00:27,000 --> 00:00:29,160
to strings and then you could see

8
00:00:31,740 --> 00:00:34,290
functions, built in functions.

9
00:00:34,290 --> 00:00:37,020
For example the len method would be handy here.

10
00:00:37,050 --> 00:00:45,900
It would return the number of characters for the pypypy string which is six.

11
00:00:46,080 --> 00:00:48,240
Now all these here.

12
00:00:48,240 --> 00:00:52,980
So these functions and the string methods they are all built in.

13
00:00:53,250 --> 00:00:59,820
So these are methods and functions that are written inside the python software that you download and

14
00:00:59,820 --> 00:01:01,100
install from Python

15
00:01:01,100 --> 00:01:02,370
Dot org.

16
00:01:02,640 --> 00:01:05,480
So that software is written in C language.

17
00:01:05,550 --> 00:01:11,010
Therefore these methods and functions are also written in C language.

18
00:01:11,160 --> 00:01:17,020
And as you see these names here are available immediately in your Python interpreter.

19
00:01:17,070 --> 00:01:18,230
That means they are built in.

20
00:01:18,240 --> 00:01:23,280
They are inside that interpreter, and you can just call them without importing them.

21
00:01:23,280 --> 00:01:32,320
Now what if we had some other problems like let's say, you see this script here.

22
00:01:32,970 --> 00:01:35,080
What this script is doing is...

23
00:01:35,100 --> 00:01:35,760
It's a loop.

24
00:01:35,760 --> 00:01:37,200
It's a while loop.

25
00:01:37,440 --> 00:01:40,680
So while true which means it will run forever.

26
00:01:40,680 --> 00:01:48,330
So what it does is it's reading this file vegetable.txt in the files directory which happens

27
00:01:48,330 --> 00:01:51,930
to be in the same directory with basics.py.

28
00:01:52,170 --> 00:02:02,090
So that is the path of the file and this text file contains this string, so I'm reading it under this variable,

29
00:02:02,700 --> 00:02:05,760
and I'm printing all the content in here.

30
00:02:05,750 --> 00:02:14,630
So if I execute this file you're going to get the content printed out every split of a second and which

31
00:02:14,630 --> 00:02:18,380
depends on your processor speed, on your computer processor speed.

32
00:02:18,380 --> 00:02:23,090
Now what if we don't want this to happen every split of a second.

33
00:02:23,090 --> 00:02:25,820
What if you want to read the content every 10 seconds.

34
00:02:26,050 --> 00:02:33,860
Let me interrupt this with control c, you'd soon realize that you don't have any built in function

35
00:02:34,070 --> 00:02:43,260
that does that, that implements some sort of sleep methods in your script, so there's no function that

36
00:02:43,260 --> 00:02:45,020
you can use right away.

37
00:02:45,120 --> 00:02:50,250
In that case you may want to look for built in modules.
To do that

38
00:02:50,250 --> 00:03:06,310
please use this syntax import sys which stands for system and sys.builtin_module_names, and here

39
00:03:06,320 --> 00:03:13,640
you'll get a list of modules, built in modules, some modules that are built in again in the Python software.

40
00:03:13,700 --> 00:03:20,990
So let's use one of them, time, the way you use it is by importing the module name and then again you

41
00:03:20,990 --> 00:03:24,730
can use dir to see what you have for that module.

42
00:03:24,830 --> 00:03:29,600
For example here you'll see we have a sleep method.

43
00:03:29,900 --> 00:03:34,480
Use help to see how to use that particular method.

44
00:03:34,760 --> 00:03:40,420
So it says that it gets the sequence as argument so let's try it.

45
00:03:40,430 --> 00:03:41,970
time.sleep.

46
00:03:41,990 --> 00:03:46,180
Let's say 3. 1, 2, 3.

47
00:03:46,280 --> 00:03:49,640
And? So what time that sleep does? It sleeps

48
00:03:49,640 --> 00:03:54,340
the script execution for a particular amount of seconds.

49
00:03:54,350 --> 00:03:58,370
Three in this case, that means we can implement that in the script.

50
00:03:58,850 --> 00:04:07,580
It's a good practice to import modules in the first line of the script, and then use that module in your

51
00:04:07,580 --> 00:04:09,740
script wherever you need it.

52
00:04:09,740 --> 00:04:13,900
In this case what you want to do is let's suppose this vegetables.txt file

53
00:04:13,910 --> 00:04:19,880
is being generated on our server every 10 seconds.

54
00:04:19,910 --> 00:04:26,750
So let's say as software in the server is getting this data from somewhere and it's a writing a new

55
00:04:26,750 --> 00:04:32,600
content every 10 seconds, for example now is tomato, later it's going to be garlic.

56
00:04:32,600 --> 00:04:35,690
10 seconds after that it it'll be onion and so on.

57
00:04:35,840 --> 00:04:38,810
So we are getting new content every 10 seconds.

58
00:04:38,810 --> 00:04:50,210
So we want to print all the contents of the file and then have the execution sleep for 10 seconds.

59
00:04:50,210 --> 00:04:59,800
Now I'm going to save the script, go to command line, execute it, so you get tomato right away.

60
00:04:59,800 --> 00:05:05,800
Now if I change these to onion, let's say the software changed it and save.

61
00:05:05,800 --> 00:05:10,120
Click save an onion was printed out there after 10 seconds.

62
00:05:10,120 --> 00:05:12,640
If I change it to garlic, save.

63
00:05:15,520 --> 00:05:17,260
Wait, wait a few seconds.

64
00:05:17,260 --> 00:05:24,490
Garlic is printed out there so every 10 seconds the script is fetching the data from vegetables.txt

65
00:05:24,490 --> 00:05:28,140
and printing it out on the command line.

66
00:05:30,360 --> 00:05:33,400
So controlled C to interrupt it.

67
00:05:33,390 --> 00:05:38,170
This is how you use a module.

68
00:05:38,240 --> 00:05:43,300
Now not everything is built in in your Python interpreter software.

69
00:05:43,340 --> 00:05:50,060
Some things actually many of them come in a different form that I'm going to explain in the next lecture.

