1
00:00:01,420 --> 00:00:06,360
Let's continue talking about methods and how they differ from functions.

2
00:00:07,550 --> 00:00:14,970
So unlike functions, methods are associated with a certain type.

3
00:00:14,990 --> 00:00:22,520
For example, strings have an upper method which will capitalize all the letters of strings.

4
00:00:24,830 --> 00:00:29,480
Strings also have a capitalized method which capitalizes the first letter.

5
00:00:29,510 --> 00:00:36,020
They have a title method which capitalizes the first letters of each word in the string.

6
00:00:37,220 --> 00:00:44,540
And of course, methods can also be applied to the variable which is holding the object.

7
00:00:44,750 --> 00:00:51,230
So in here we apply the method directly on the object itself, and here we apply it to the variable

8
00:00:51,230 --> 00:00:52,670
which holds the object.

9
00:00:53,300 --> 00:00:55,250
Either way, it's fine.

10
00:00:56,270 --> 00:01:04,010
But normally you work with variables when building applications, so most commonly you'll do this year.

11
00:01:08,140 --> 00:01:14,520
Now there are methods which return an output and methods which don't return an output.

12
00:01:14,530 --> 00:01:21,670
And this is a concept similar to functions functions which return outputs and functions which don't

13
00:01:21,670 --> 00:01:22,630
return an output.

14
00:01:23,080 --> 00:01:29,740
This method here title is applied to the greeting variable which holds the string.

15
00:01:29,770 --> 00:01:30,520
Hello there.

16
00:01:33,660 --> 00:01:39,270
Now, when you execute this code in a python console, you will get this.

17
00:01:39,270 --> 00:01:43,020
Hello there with h and PT capitalized.

18
00:01:43,020 --> 00:01:44,610
So this means that.

19
00:01:47,570 --> 00:01:50,840
This method is returning that string.

20
00:01:51,080 --> 00:01:53,060
Or you can also have this.

21
00:01:56,400 --> 00:02:03,600
Stored in a variable world is equal to a title and then print out the words and you'll get the same

22
00:02:03,600 --> 00:02:04,110
thing.

23
00:02:04,140 --> 00:02:04,850
Hello there.

24
00:02:04,860 --> 00:02:09,240
So the output of title is stored in this variable.

25
00:02:10,789 --> 00:02:20,510
In other methods, though, such as we have this append method which applies to the grocery list, so

26
00:02:20,510 --> 00:02:25,010
this method does not return an output.

27
00:02:27,650 --> 00:02:31,400
So if you assign a variable here, such as variable.

28
00:02:33,940 --> 00:02:42,610
Equal to that variable will be equal to nothing because no value is returned from the append method.

29
00:02:42,640 --> 00:02:45,880
However, the append method.

30
00:02:48,230 --> 00:02:51,230
Modifies its object.

31
00:02:52,560 --> 00:02:57,960
A method modifies its objects when the object is mutable.

32
00:02:58,080 --> 00:02:59,400
So lists.

33
00:02:59,400 --> 00:03:01,380
All lists are mutable.

34
00:03:01,380 --> 00:03:04,110
Strings are not mutable.

35
00:03:07,780 --> 00:03:15,430
So if I print out groceries here, you'll see that apples have been has been added to groceries.

36
00:03:15,520 --> 00:03:19,720
So groceries was a list of three items up here.

37
00:03:19,720 --> 00:03:23,560
But then here it became a list of four items.

38
00:03:25,010 --> 00:03:32,660
Similarly, we can apply a sort method to groceries, so this sort method will not return an output,

39
00:03:32,660 --> 00:03:35,780
but it will modify its object.

40
00:03:36,050 --> 00:03:44,600
So this is not the same as the title method tied to the returns and output, but it does not modify

41
00:03:45,320 --> 00:03:51,230
its object because the object here is a string and strings are immutable.

42
00:03:51,230 --> 00:03:53,870
They cannot be modified.

43
00:03:57,510 --> 00:04:04,380
If you really want to modify a string, you'd have to store the new outputs in new variable.

44
00:04:04,380 --> 00:04:13,110
For example, creating new is equal to that, and then creating new will be the new string with H and

45
00:04:13,110 --> 00:04:14,430
T capitalized.

46
00:04:14,610 --> 00:04:17,910
But it's a new object, right?

47
00:04:17,910 --> 00:04:26,010
You are not modifying the existing object which is currently connected to the greeting variable.

48
00:04:28,170 --> 00:04:28,740
Right.

49
00:04:29,220 --> 00:04:36,660
That's about methods which apply to mutable objects and methods which apply to immutable objects.

50
00:04:36,750 --> 00:04:42,320
To get a list of methods for a particular object type, you can use the function.

51
00:04:42,330 --> 00:04:50,580
For example, for strings, you can do SDR and you'll see that, for example, you have the lower methods

52
00:04:50,580 --> 00:04:51,120
here.

53
00:04:52,390 --> 00:04:56,530
The title method which we used, also the upper method, etc..

54
00:04:57,040 --> 00:05:02,320
Alternatively, you can also apply deer to an instance of strings.

55
00:05:02,320 --> 00:05:05,110
So hello is an instance right?

56
00:05:05,800 --> 00:05:10,510
SDR is the class which represents all strings.

57
00:05:16,010 --> 00:05:23,210
So a class is like, for example, let's say here we could say human.

58
00:05:24,930 --> 00:05:28,230
And so the human is the class, right?

59
00:05:28,260 --> 00:05:34,740
But then if you refer to a particular human which has a name and he lives somewhere or she lives somewhere.

60
00:05:35,660 --> 00:05:37,530
He has an identity and so on.

61
00:05:37,540 --> 00:05:40,270
That is an instance of human class.

62
00:05:40,270 --> 00:05:49,180
So the person, one particular person is one particular instance and the abstract concept human is the

63
00:05:49,180 --> 00:05:49,840
class.

64
00:05:49,840 --> 00:06:00,010
So the same goes here as the R is an abstract concept and hello is one instance of that concept.

65
00:06:03,170 --> 00:06:07,190
Of course, you can apply deer to other object types such as list.

66
00:06:08,960 --> 00:06:10,670
This could be int as well.

67
00:06:10,700 --> 00:06:12,650
Float, etc..

68
00:06:19,110 --> 00:06:21,090
How to create new methods.

69
00:06:21,570 --> 00:06:28,710
So for function, for example, we have functions which we access from Python, for example, the print

70
00:06:28,710 --> 00:06:30,210
function of the input function.

71
00:06:30,210 --> 00:06:34,110
But we can also create custom functions using the dev keywords.

72
00:06:34,230 --> 00:06:40,320
Now, with methods we have these methods for example operand and title and append.

73
00:06:40,620 --> 00:06:43,050
But how do you create new methods?

74
00:06:44,290 --> 00:06:53,500
Well, since methods are connected to their object types, then first you need to know how to create

75
00:06:53,500 --> 00:06:55,360
object types, right?

76
00:06:55,360 --> 00:06:58,570
How to create a human blueprint.

77
00:06:58,570 --> 00:07:01,240
For example, how to create string blueprints.

78
00:07:01,270 --> 00:07:03,580
That is something you haven't learned yet.

79
00:07:04,450 --> 00:07:11,950
But you learn that in the second half of the course, that's an advanced concept that the ultimate level

80
00:07:11,950 --> 00:07:13,720
of python you can get.

81
00:07:16,580 --> 00:07:24,380
So once you know how to create these types object types, then you also learn how to create methods

82
00:07:24,380 --> 00:07:25,760
for these types.

83
00:07:28,730 --> 00:07:37,310
So perhaps you can create an app, a booking system for airplane tickets, and you'd have like a ticket

84
00:07:37,310 --> 00:07:39,680
class, A ticket type.

85
00:07:40,760 --> 00:07:48,980
We should create ticket instances for customers, and then this ticket class could have a cancel method

86
00:07:48,980 --> 00:07:56,420
which cancel the ticket or a delete method which deletes the PDF ticket or it deletes the ticket from

87
00:07:56,420 --> 00:07:57,800
the database and so on.

88
00:08:03,340 --> 00:08:04,990
Lists and tables.

89
00:08:07,020 --> 00:08:09,130
So that's a list.

90
00:08:09,150 --> 00:08:09,870
That's a table.

91
00:08:09,900 --> 00:08:11,070
What's the difference?

92
00:08:12,460 --> 00:08:15,310
Well, temples are immutable.

93
00:08:16,820 --> 00:08:22,100
So, for example, you cannot have an append method for this value staple.

94
00:08:22,130 --> 00:08:28,400
You'll get an error if you try to execute that because the append method is meant to modify.

95
00:08:29,360 --> 00:08:31,730
The object it applies to.

96
00:08:34,120 --> 00:08:38,140
But tablets are immutable, so this will not work.

97
00:08:41,440 --> 00:08:46,660
Usually lists are used when you want to store homogeneous items.

98
00:08:46,660 --> 00:08:49,870
For example, this list contains strings only.

99
00:08:51,540 --> 00:08:59,040
Even though you can store various types, but usually you want to use lists for homogeneous items and

100
00:08:59,040 --> 00:09:06,030
you also want to use lists when you foresee that during the course of the program.

101
00:09:06,030 --> 00:09:07,380
In the program.

102
00:09:07,530 --> 00:09:11,190
During the execution of the list may be changed.

103
00:09:11,430 --> 00:09:14,700
New items can be added to the list and so on.

104
00:09:16,020 --> 00:09:22,170
That is something that you will better understand when you build real applications throughout the course

105
00:09:23,400 --> 00:09:24,750
and tuples.

106
00:09:25,730 --> 00:09:30,380
Are more meant to store heterogeneous items, for example.

107
00:09:30,890 --> 00:09:37,640
Let's say you have a web app where the users can upload an image and they want it resized and they want

108
00:09:37,640 --> 00:09:38,750
it grayscale.

109
00:09:41,590 --> 00:09:47,020
So they may input some numbers in input boxes in the web app interface.

110
00:09:47,020 --> 00:09:54,120
And so in the backend in Python, you will get those numbers in a tuple, you might get the grayscale

111
00:09:54,160 --> 00:09:54,850
string.

112
00:09:54,850 --> 00:09:58,810
So the user is saying that I wanted grayscale and I wanted a jpg.

113
00:09:58,810 --> 00:10:02,830
So you may store such data in a tuple.

114
00:10:05,680 --> 00:10:07,020
Right indexing.

115
00:10:07,320 --> 00:10:11,760
You can use indexing on strings, lists and tables.

116
00:10:11,910 --> 00:10:16,890
For example, Grocery Zero will give you the first item off the grocery list.

117
00:10:18,300 --> 00:10:21,570
Groceries, too, will give you the third item.

118
00:10:22,170 --> 00:10:24,570
So 012.

119
00:10:24,630 --> 00:10:25,860
That's at the third item.

120
00:10:27,330 --> 00:10:31,600
The same goes for tuples, so that will give you great scale.

121
00:10:31,620 --> 00:10:34,860
The third item for strings as well.

122
00:10:34,860 --> 00:10:35,580
So.

123
00:10:36,360 --> 00:10:39,990
And is the third item with index two.

124
00:10:40,500 --> 00:10:42,600
And we also have negative indexing.

125
00:10:42,600 --> 00:10:50,610
For example, minus two will give you a this because our is minus one, A is minus two, minus three,

126
00:10:50,610 --> 00:10:53,140
minus four minus five and so on.

127
00:10:53,160 --> 00:11:01,800
You might want to use negative indexing when you're accessing items which are closer to the end of an

128
00:11:01,800 --> 00:11:06,420
array array, I mean at least to pull or string.

129
00:11:09,530 --> 00:11:11,420
Also called sequence.

130
00:11:11,420 --> 00:11:14,630
So these are also known as sequence types.

131
00:11:15,470 --> 00:11:17,120
Of course, this is a string type.

132
00:11:17,120 --> 00:11:19,320
This is a list type.

133
00:11:19,340 --> 00:11:23,330
This is a type of type, but they fall in the category of sequences.

134
00:11:28,480 --> 00:11:40,270
So that's how you access individual items, but you can also access a slice by using that column symbol.

135
00:11:40,270 --> 00:11:48,370
So that will give you the items from index one up to index two.

136
00:11:49,210 --> 00:11:52,840
So the item with index three is not included in the slides.

137
00:11:52,840 --> 00:11:56,080
So you only get 1080, which is this.

138
00:11:56,470 --> 00:12:00,310
So zero one index one and index to grayscale.

139
00:12:03,630 --> 00:12:05,520
So that's the rule for indexing.

140
00:12:05,550 --> 00:12:09,090
It works for strings and lists as well.

141
00:12:09,330 --> 00:12:11,670
Then we have negative indexing.

142
00:12:11,850 --> 00:12:14,130
Minus three, minus one means.

143
00:12:16,410 --> 00:12:18,450
From here, which is minus three.

144
00:12:23,270 --> 00:12:26,600
Up to here, which is before minus one.

145
00:12:27,830 --> 00:12:32,660
So always the second index in the slides is not included in the slides.

146
00:12:32,690 --> 00:12:35,260
So index three was not included here.

147
00:12:35,270 --> 00:12:40,550
Index minus one JPG is also not included in this slides.

148
00:12:43,810 --> 00:12:46,630
Lastly for this video, let's talk about dictionaries.

149
00:12:47,770 --> 00:12:57,430
You should use dictionaries to store your data when your values are more heterogeneous and it's important

150
00:12:57,430 --> 00:12:59,860
to have labels on them.

151
00:13:00,160 --> 00:13:06,020
So the data here are, for example, John Smith for it.

152
00:13:06,670 --> 00:13:11,830
And so these are heterogeneous because these are strings, right?

153
00:13:11,830 --> 00:13:17,350
And this is a number and the labels are first name.

154
00:13:17,350 --> 00:13:20,920
So this tells you that John is a first name.

155
00:13:21,040 --> 00:13:22,660
Then we have last name.

156
00:13:22,990 --> 00:13:24,790
So Smith is the last name.

157
00:13:24,790 --> 00:13:27,820
And then we have age, which is 40.

158
00:13:27,910 --> 00:13:28,570
So.

159
00:13:30,530 --> 00:13:32,280
These are known as keys.

160
00:13:32,300 --> 00:13:33,420
First name is a key.

161
00:13:33,440 --> 00:13:34,640
Last name is a key.

162
00:13:34,640 --> 00:13:34,970
And.

163
00:13:34,970 --> 00:13:36,910
And age is also a key.

164
00:13:36,920 --> 00:13:40,250
And the values of this dictionary are John.

165
00:13:41,460 --> 00:13:49,900
Smith and Foley dictionaries can also be insight other items such as lists, for example.

166
00:13:49,920 --> 00:13:51,660
Here, this is a list.

167
00:13:51,660 --> 00:13:55,380
It starts in here and it ends in here.

168
00:13:55,380 --> 00:13:58,590
You see the square brackets.

169
00:13:59,790 --> 00:14:02,220
So this list has three items.

170
00:14:02,580 --> 00:14:03,870
That's the first item.

171
00:14:05,760 --> 00:14:07,020
That's the second item.

172
00:14:08,160 --> 00:14:09,240
And there's the third item.

173
00:14:09,240 --> 00:14:15,360
So each item is a dictionary because at least can have any type of object.

174
00:14:15,360 --> 00:14:21,450
And dictionary is also an object, just like we have strings and integers and floats, etc..

175
00:14:24,420 --> 00:14:30,420
So this structure here would be appropriate if you had multiple persons.

176
00:14:32,280 --> 00:14:40,050
So you would store data about those persons and you'd have the same key in all the dictionaries but

177
00:14:40,050 --> 00:14:48,240
different values, for example, John here Smith and the H, and you repeat that information but with

178
00:14:48,240 --> 00:14:50,010
different values, right?

179
00:14:50,400 --> 00:14:55,500
So that's one way to store these data, data about persons.

180
00:14:56,250 --> 00:15:03,080
However, in certain applications, depending on the scenario, you may want to store those data differently.

181
00:15:03,090 --> 00:15:09,750
For example, here we also have data about persons, but this will be a dictionary.

182
00:15:10,710 --> 00:15:12,060
So it's one dictionary.

183
00:15:12,060 --> 00:15:14,400
It starts here and it ends in here.

184
00:15:14,580 --> 00:15:18,000
And then this dictionary has keys and values.

185
00:15:18,390 --> 00:15:20,850
So the keys are the same.

186
00:15:21,060 --> 00:15:26,130
They are just strings first name, but the value is a list.

187
00:15:26,280 --> 00:15:30,210
So the value of each key now is a list instead of a string.

188
00:15:30,690 --> 00:15:35,600
So this list here has the first names of all the persons.

189
00:15:35,610 --> 00:15:42,330
And then we have another key, which has also the last names of the persons and the age.

190
00:15:45,390 --> 00:15:46,440
Which one is better?

191
00:15:46,470 --> 00:15:49,540
Well, there's no answer for that.

192
00:15:49,560 --> 00:15:52,410
Whether this is better or that.

193
00:15:52,410 --> 00:15:56,190
So it depends what you want to do with these data.

194
00:15:56,370 --> 00:16:00,210
Do you simply want to print them out or do you want to modify them?

195
00:16:01,050 --> 00:16:05,040
That is something that you realize as you build the application.

196
00:16:05,040 --> 00:16:06,540
So nothing to worry about.

197
00:16:06,870 --> 00:16:09,660
Now what is better and what is not?

198
00:16:12,090 --> 00:16:16,110
And lastly, to access the values of a dictionary.

199
00:16:16,110 --> 00:16:20,250
You use that syntax, so you refer to the variable which holds the dictionary.

200
00:16:20,280 --> 00:16:22,170
John In this case.

201
00:16:22,170 --> 00:16:26,520
And then in square brackets you refer to the key in quotes.

202
00:16:27,870 --> 00:16:39,390
So that will give you Smith the value of the last name key of John Dictionary for this here, which

203
00:16:39,390 --> 00:16:42,270
is a list you'd first have to do too.

204
00:16:42,540 --> 00:16:48,690
So with that, you'd get this dictionary because this list has three items.

205
00:16:49,020 --> 00:16:56,580
So that has an index of zero, that has an index of one and that has an index of two.

206
00:16:56,610 --> 00:17:03,840
So with that, you get the last dictionary, and then from that dictionary you access the first name

207
00:17:03,840 --> 00:17:04,359
key.

208
00:17:04,380 --> 00:17:06,000
So that will give you some.

209
00:17:07,060 --> 00:17:08,619
That's what we get here, SIM.

210
00:17:12,400 --> 00:17:17,740
Since this is a dictionary, first you have to give a key.

211
00:17:18,740 --> 00:17:22,550
So here you give a number because that was a list.

212
00:17:23,089 --> 00:17:30,350
But here you have to give the key of the dictionary first name and then this up to here.

213
00:17:30,380 --> 00:17:39,230
You will get the list as outputs and then out of that list you access index to.

214
00:17:39,260 --> 00:17:42,730
So the item with index two, which corresponds to SIM.

215
00:17:42,740 --> 00:17:44,480
So you get SIM as outputs.

216
00:17:46,100 --> 00:17:47,890
That's above this video.

217
00:17:47,900 --> 00:17:48,800
We have some more.

218
00:17:48,830 --> 00:17:50,000
So you in the next one.

