1
00:00:00,480 --> 00:00:04,500
In this video, you learn to use Python methods.

2
00:00:04,710 --> 00:00:13,410
We're going to need to use a method to add to do this to a list, because currently in our program,

3
00:00:13,410 --> 00:00:19,690
all we do is get the two due from the user and then we print out that to do and that's all.

4
00:00:19,710 --> 00:00:23,760
We are not storing the to dos in a list.

5
00:00:24,030 --> 00:00:31,590
So I will delete these and add some code here which stores to dos in a list.

6
00:00:31,770 --> 00:00:37,470
You remember we did have some codes previously where we wrote a list.

7
00:00:37,470 --> 00:00:42,930
We created a list using brackets and then inside we add it to the one to do two and so on.

8
00:00:43,710 --> 00:00:49,560
So now, of course, we don't have to do once and to the tools, we don't have those variables.

9
00:00:49,560 --> 00:00:51,380
We have a to do variable.

10
00:00:51,390 --> 00:00:55,140
So to do is one variable, to dos is another variable.

11
00:00:55,140 --> 00:01:00,720
Two different variables to do now is added to the to do list.

12
00:01:01,680 --> 00:01:06,570
So let's print out to dos in here and see what we get.

13
00:01:07,620 --> 00:01:09,150
Run enter to do.

14
00:01:09,180 --> 00:01:12,690
Clean through.

15
00:01:13,500 --> 00:01:15,720
So this is the output we get.

16
00:01:15,930 --> 00:01:19,680
Let's try to analyze this to understand the problem.

17
00:01:21,570 --> 00:01:26,880
So while true executes this line first, so we get that output.

18
00:01:27,600 --> 00:01:32,670
The user enters clean and clean, goes inside the list.

19
00:01:33,510 --> 00:01:39,030
So to dos list is created and then it is printed out.

20
00:01:39,030 --> 00:01:42,060
So we print out to DOS here.

21
00:01:42,060 --> 00:01:47,310
So currently we have only the clean string inside that list.

22
00:01:48,120 --> 00:01:49,620
So far, so good.

23
00:01:49,650 --> 00:01:54,990
Then the loop goes back again to the first line of the loop.

24
00:01:54,990 --> 00:01:56,700
It executes again inputs.

25
00:01:56,700 --> 00:02:01,980
So we get this prompt again, we enter through and then.

26
00:02:02,830 --> 00:02:05,020
The second line is executed.

27
00:02:05,050 --> 00:02:07,660
To do so is equal to that list.

28
00:02:07,750 --> 00:02:15,700
What happens here is that this variable will override the existing to those variable.

29
00:02:16,960 --> 00:02:27,580
So the list we had with clean inside is overwritten by another list with through inside.

30
00:02:27,760 --> 00:02:33,370
So this entire line here overrides that existing list.

31
00:02:33,370 --> 00:02:38,550
And so we print out that new list with through inside.

32
00:02:38,560 --> 00:02:42,430
So obviously this here is not the solution.

33
00:02:42,430 --> 00:02:43,930
We want to delete that.

34
00:02:44,800 --> 00:02:49,270
What we want to do is first create a list outside the loop.

35
00:02:49,300 --> 00:02:56,080
To do is equal to this empty list, so Python will execute the first line.

36
00:02:56,080 --> 00:03:02,230
It creates this string, it creates an empty list and then goes to the loop.

37
00:03:02,260 --> 00:03:04,510
It asks the user for input.

38
00:03:05,170 --> 00:03:14,080
The user enters a to do, and then we append that to do in the to do list in that list.

39
00:03:16,370 --> 00:03:20,360
And we print out the to do list.

40
00:03:20,990 --> 00:03:29,930
So let's rerun the code answer to do clean through neat.

41
00:03:30,620 --> 00:03:32,600
So that's the list.

42
00:03:32,870 --> 00:03:40,760
As you see, the list is being populated with items in every iteration of the loop.

43
00:03:42,090 --> 00:03:44,340
So the program is working pretty well.

44
00:03:44,400 --> 00:03:50,970
We want to improve it further, of course, and we'll do that in the next videos.

45
00:03:51,690 --> 00:03:55,860
But now let's focus on the syntax we used here.

46
00:03:55,890 --> 00:03:59,070
You see this dot and then this.

47
00:03:59,780 --> 00:04:02,210
Key word is coming after that dot.

48
00:04:02,240 --> 00:04:03,740
This is a method.

49
00:04:05,010 --> 00:04:07,650
So methods are like functions.

50
00:04:07,680 --> 00:04:14,490
You see, this function has this parentheses and an argument, and the method also has parentheses and

51
00:04:14,490 --> 00:04:21,180
arguments, but methods are attached to data types to other objects.

52
00:04:21,180 --> 00:04:26,280
In this case, this append method is part of this to dos.

53
00:04:27,270 --> 00:04:28,230
Variable.

54
00:04:28,290 --> 00:04:31,050
Actually, it is not part of the variable.

55
00:04:31,050 --> 00:04:35,540
It is part of the object associated with that variable.

56
00:04:35,550 --> 00:04:38,370
In this case, the object is a list.

57
00:04:38,400 --> 00:04:44,760
You see, it's a list type and every object has its own methods.

58
00:04:44,760 --> 00:04:46,320
So for example.

59
00:04:47,760 --> 00:04:57,330
If you want to access the append method of the to to to do this variable is associated with a string,

60
00:04:57,660 --> 00:04:59,100
not a list.

61
00:04:59,100 --> 00:05:05,700
So if you try to apply append to that string, you're going to get an error.

62
00:05:06,600 --> 00:05:07,770
Let's try this.

63
00:05:08,570 --> 00:05:09,410
We run.

64
00:05:11,440 --> 00:05:13,000
Enter it to do so.

65
00:05:13,120 --> 00:05:15,160
Now we are here at this point.

66
00:05:15,160 --> 00:05:23,800
So Python has not yet reached the point where this append line is executed.

67
00:05:23,800 --> 00:05:30,190
So enter it to do like a clean press enter and you see that is an error.

68
00:05:31,450 --> 00:05:33,350
It tells you where the error occurred.

69
00:05:33,370 --> 00:05:36,190
So line seven, Line seven.

70
00:05:36,190 --> 00:05:37,270
Is this one in here?

71
00:05:38,640 --> 00:05:39,520
In part charm.

72
00:05:39,570 --> 00:05:43,290
You can also just click that to go directly to line seven.

73
00:05:43,980 --> 00:05:45,390
So that's a convenience.

74
00:05:45,600 --> 00:05:48,630
And so the cursor is in line seven now.

75
00:05:49,990 --> 00:05:54,790
You also get the line printed out in here, which has the error to do that the pants.

76
00:05:54,790 --> 00:05:57,820
And then this is a type of the error attribute error.

77
00:05:58,790 --> 00:06:00,410
HDR object.

78
00:06:00,440 --> 00:06:07,280
So SDR is the type of object, the data type that to do is associated with.

79
00:06:07,280 --> 00:06:11,630
So to do is associated with an SDR with a string object.

80
00:06:11,960 --> 00:06:15,130
So an object has no attribute append.

81
00:06:15,140 --> 00:06:17,960
So methods are also referred to as attributes.

82
00:06:18,760 --> 00:06:21,970
So a string does not have that append method.

83
00:06:22,780 --> 00:06:24,370
That's why we get this error.

84
00:06:25,000 --> 00:06:26,950
So how do we fix this error?

85
00:06:27,100 --> 00:06:31,870
Well, this is based on our experience as a programmer.

86
00:06:31,900 --> 00:06:38,500
The error cannot be fixed automatically by Python, so you have to take a decision because Python here,

87
00:06:38,500 --> 00:06:43,060
the interpreter which interprets our code is confused.

88
00:06:44,440 --> 00:06:48,970
So it doesn't make sense to append something to a string.

89
00:06:49,000 --> 00:06:53,290
It does make sense to append something to a list, but not to a string.

90
00:06:53,320 --> 00:06:55,900
A string could have different methods.

91
00:06:55,900 --> 00:06:57,100
Such as.

92
00:07:00,290 --> 00:07:01,550
Capitalize.

93
00:07:04,710 --> 00:07:06,630
Let's see what will happen now.

94
00:07:08,190 --> 00:07:15,690
Enter to do clean all use a lowercase string.

95
00:07:15,690 --> 00:07:17,460
Now press enter.

96
00:07:18,200 --> 00:07:24,710
So clean was printed out by this, but we didn't get any print out from this.

97
00:07:24,710 --> 00:07:29,090
So let me remove to reduce print to do so and execute again.

98
00:07:29,660 --> 00:07:34,240
You see, again, we will not get any print out.

99
00:07:34,250 --> 00:07:34,840
Why?

100
00:07:34,850 --> 00:07:37,550
Well, because we haven't used a print function.

101
00:07:37,550 --> 00:07:42,410
So if you want to see output, you won't use a print function as you see.

102
00:07:42,410 --> 00:07:49,250
Now, I put this entire expression inside the parentheses of the print function.

103
00:07:50,550 --> 00:07:58,420
Now, if I execute this enter to do clean, we will get clean with an uppercase C.

104
00:07:58,440 --> 00:08:05,670
So this method, it's capitalized is the first letter of a string and it returns the string with the

105
00:08:05,670 --> 00:08:07,440
first letter capitalized.

106
00:08:07,890 --> 00:08:15,510
So as you see, the difference between methods and function is that in functions, if this was a function

107
00:08:15,510 --> 00:08:20,970
which is not, it would get that to do as an input in here.

108
00:08:21,750 --> 00:08:24,120
It would be capitalized to do inside.

109
00:08:24,120 --> 00:08:28,740
But this is not the case and that is what makes a method a method.

110
00:08:28,860 --> 00:08:37,860
So a method to refer to an object using that dot notation and methods can also take arguments.

111
00:08:39,110 --> 00:08:47,090
So how do you know what arguments can a method take and how do you know what methods you can apply to

112
00:08:47,090 --> 00:08:50,930
strings and what methods you can apply to lists?

113
00:08:51,320 --> 00:08:55,100
How do you know what arguments you can pass to a function?

114
00:08:55,250 --> 00:08:58,460
So these are all questions which I'll answer.

115
00:08:58,460 --> 00:09:07,100
I'll give you the technique to answer all these questions by yourself so you can find the code you need

116
00:09:07,100 --> 00:09:08,390
by yourself.

117
00:09:09,020 --> 00:09:15,290
You'll see the list of methods you can apply to list, and you'll see all the methods available for

118
00:09:15,290 --> 00:09:15,630
list.

119
00:09:15,650 --> 00:09:19,940
You'll see all the methods that Python has available for string and so on.

120
00:09:20,240 --> 00:09:27,350
And I'll show you all that information in the video programming tool or concepts of today.

121
00:09:28,090 --> 00:09:32,460
So there is where you learn how to find the code you need.

122
00:09:32,470 --> 00:09:34,080
And here we end this video.

123
00:09:34,090 --> 00:09:34,920
So thanks a lot.

124
00:09:34,930 --> 00:09:35,410
See you.

