1
00:00:00,510 --> 00:00:02,760
Hey, welcome to the Days experiment.

2
00:00:02,790 --> 00:00:10,200
Today, I'll run a few cold experiments so that you better understand the list indexing and type conversion.

3
00:00:10,530 --> 00:00:14,970
Now, the code that I'll be trying out are single lines of code.

4
00:00:14,970 --> 00:00:19,170
So for that reason, I'll be using the Python console.

5
00:00:19,200 --> 00:00:25,260
It's best to use the python console when you are just executing one liners.

6
00:00:27,610 --> 00:00:31,120
So you saw that if the user entered some inputs.

7
00:00:34,530 --> 00:00:36,870
And we store that input in a variable.

8
00:00:38,460 --> 00:00:46,260
For example, if the user enters five and then you see a now is a string, you see the codes here.

9
00:00:46,740 --> 00:00:50,700
So a plus ten would not make sense.

10
00:00:50,970 --> 00:01:00,360
Therefore, you want to convert A to an integer using the INT function and perhaps store the output

11
00:01:00,360 --> 00:01:02,860
in a new variable or maybe in the same variable.

12
00:01:02,880 --> 00:01:05,519
It's up to you all store it in a new variable.

13
00:01:05,550 --> 00:01:06,980
B So now.

14
00:01:06,990 --> 00:01:10,710
B plus ten makes sense.

15
00:01:10,950 --> 00:01:13,030
So B is an integer.

16
00:01:13,050 --> 00:01:14,840
You see, it's without quotes.

17
00:01:14,850 --> 00:01:16,830
And this has quotes around.

18
00:01:17,970 --> 00:01:21,720
Now you might ask, why are types important?

19
00:01:22,230 --> 00:01:25,230
Why can't we just have ten?

20
00:01:25,230 --> 00:01:28,500
And it doesn't matter if it's a string or a number.

21
00:01:28,530 --> 00:01:31,470
Why do values have to have a type?

22
00:01:31,500 --> 00:01:34,050
Well, I'll answer you that with an example.

23
00:01:35,270 --> 00:01:40,550
So again, A is a string, B is a number.

24
00:01:41,440 --> 00:01:47,050
Now, if you do B times ten, guess what you will get.

25
00:01:47,530 --> 00:01:48,810
Yeah, it's 50.

26
00:01:48,820 --> 00:01:52,630
So five times ten you get 50.

27
00:01:53,230 --> 00:01:54,340
Now let's try.

28
00:01:55,320 --> 00:01:56,790
Times ten.

29
00:01:59,110 --> 00:02:00,190
This is what you get.

30
00:02:01,800 --> 00:02:10,259
So in Python, it is not possible to add a number to a string, but it is possible to multiply a number

31
00:02:10,259 --> 00:02:19,560
with a string and the output you get is that string multiplied ten times in this case.

32
00:02:19,710 --> 00:02:24,300
So five, five, five, five, five, five, five at ten times, and you get a new string.

33
00:02:25,440 --> 00:02:30,090
And of course, you can have everything like high times ten.

34
00:02:30,570 --> 00:02:32,730
And you get high, high, high, ten times.

35
00:02:34,450 --> 00:02:39,310
So back to the question we asked, Why is it important to have types?

36
00:02:39,400 --> 00:02:46,100
Well, the interpreter wants to know the type because it understands better what you intend to do.

37
00:02:46,120 --> 00:02:55,030
So if you intend to have that string multiplied ten times, then you want to have that as a string type.

38
00:02:56,090 --> 00:02:59,570
And then the interpreter knows that it is a string type.

39
00:02:59,570 --> 00:03:06,830
So I don't have to do a math multiplication, I do a string multiplication if it's a string type.

40
00:03:07,130 --> 00:03:13,700
And if you as a programmer mean to to do a math operation, then you want to make sure that this.

41
00:03:14,690 --> 00:03:17,420
Value is an integer.

42
00:03:18,250 --> 00:03:25,720
So having types for values is important to communicate with the interpreter, so things have to be as

43
00:03:25,720 --> 00:03:27,250
explicit as possible.

44
00:03:27,560 --> 00:03:27,880
Right.

45
00:03:27,890 --> 00:03:29,080
The next experiment.

46
00:03:31,660 --> 00:03:35,080
If you do it high, you will get an error.

47
00:03:35,080 --> 00:03:36,860
So that's obvious.

48
00:03:36,880 --> 00:03:38,710
You can also use floats.

49
00:03:39,960 --> 00:03:42,390
Floats high, you will get an error again.

50
00:03:42,600 --> 00:03:49,110
So it doesn't make sense to convert that string into an INT and it doesn't make sense to convert that

51
00:03:49,110 --> 00:03:49,920
into a float.

52
00:03:49,920 --> 00:03:53,550
So floats are decimal points and numbers.

53
00:03:55,910 --> 00:03:57,170
If you convert.

54
00:03:58,410 --> 00:04:03,090
String ten to a float, you'll get 10.0.

55
00:04:03,540 --> 00:04:09,930
So that is different from converting string ten into INT you get ten.

56
00:04:10,830 --> 00:04:13,530
So again, we have a different type here.

57
00:04:15,120 --> 00:04:21,660
The distinction between floats and integers is not as important as the distinction between strings and

58
00:04:21,660 --> 00:04:28,880
integers, but still you don't use floats whenever you are working with continuous data.

59
00:04:28,890 --> 00:04:34,190
So if you have a variable which is the temperature, then you want to work with a float.

60
00:04:34,200 --> 00:04:40,860
If you are working with whole numbers such as the number of persons in a company, you want to work

61
00:04:40,860 --> 00:04:41,850
with integers.

62
00:04:41,970 --> 00:04:49,830
Now Python has this implicit declaration of types so you don't have to use the float class when you

63
00:04:49,830 --> 00:04:50,640
create a variable.

64
00:04:50,640 --> 00:04:59,700
So if you just say x is equal to ten points one and you check the type of x, you'll see that x is.

65
00:05:00,740 --> 00:05:01,770
A float.

66
00:05:01,790 --> 00:05:07,670
But if you do, y is equal to ten, you'll see that Y is.

67
00:05:09,170 --> 00:05:10,090
An integer.

68
00:05:10,100 --> 00:05:12,320
So if it doesn't have that dot.

69
00:05:13,410 --> 00:05:14,370
It's an integer.

70
00:05:14,400 --> 00:05:16,590
If it does have that, it's a float.

71
00:05:18,270 --> 00:05:24,960
And of course, if you use quotes around it, it doesn't matter if it does have the delta or not.

72
00:05:25,620 --> 00:05:28,410
Type of set is going to be a string.

73
00:05:31,640 --> 00:05:38,300
If for some reason you want to convert a number into a string, you can use a STR.

74
00:05:38,330 --> 00:05:43,460
You not only convert integers to strings but also floats to strings.

75
00:05:44,570 --> 00:05:49,520
You could ask when would you need to convert a number into a string?

76
00:05:50,270 --> 00:05:58,100
Well, you'll see that when we create apps, then this SDR function can sometimes become useful.

77
00:05:58,130 --> 00:06:07,160
For example, if you want to write some texts in a text file via Python, there is a method for that

78
00:06:07,370 --> 00:06:10,560
which you will learn later on in a few days from now.

79
00:06:10,580 --> 00:06:15,520
And in that case, you cannot write a number to other types to a text file.

80
00:06:15,530 --> 00:06:23,870
So if you have a number, you'll have to convert it to a string and then write the output in that text

81
00:06:23,870 --> 00:06:31,490
file and then the number would show in that dot txt text file or other file formats that Python is able

82
00:06:31,490 --> 00:06:32,390
to create.

83
00:06:33,770 --> 00:06:38,130
Let's run a few experiments on the list indexing now.

84
00:06:38,150 --> 00:06:39,350
So let's create a list.

85
00:06:39,350 --> 00:06:41,060
My list is equal to.

86
00:06:42,940 --> 00:06:47,380
A, B, and C.

87
00:06:48,960 --> 00:06:56,280
The first thing I want to make sure you understand well is that each of these items is treated as an

88
00:06:56,280 --> 00:06:57,210
object.

89
00:06:57,210 --> 00:07:03,240
So you can stroke the object, you can store it in a variable, for example, as that is equal to my

90
00:07:03,240 --> 00:07:03,900
list.

91
00:07:06,230 --> 00:07:21,320
One and Z will be string B, So the type of Z is be sorry, the type of Z is A string that B was another

92
00:07:21,320 --> 00:07:23,900
variable which we declared somewhere in here.

93
00:07:23,990 --> 00:07:26,000
So that is a string.

94
00:07:26,840 --> 00:07:31,340
And with that we extracted the item with index one.

95
00:07:31,340 --> 00:07:34,490
So a has an index of zero, B has an index of one.

96
00:07:34,490 --> 00:07:40,640
That's why we get the string B and of course you can do other operations.

97
00:07:40,640 --> 00:07:46,520
You can say that is equal to B, you get true.

98
00:07:46,730 --> 00:07:49,550
Similarly, you can say my list.

99
00:07:52,170 --> 00:07:56,590
One is equal to B, you get through.

100
00:07:56,610 --> 00:08:00,930
So my list one and Z is the same value.

101
00:08:00,930 --> 00:08:04,800
So both these points to the same value.

102
00:08:06,340 --> 00:08:11,200
Now, how do we get the index of one object?

103
00:08:11,230 --> 00:08:13,810
Let's say we want to know the index of B.

104
00:08:14,200 --> 00:08:16,720
To do that, you need to use my list.

105
00:08:17,320 --> 00:08:26,710
That index a method and provide the object you want to know the index of and you'll get one.

106
00:08:26,710 --> 00:08:28,540
So that is the index of B.

107
00:08:30,610 --> 00:08:34,900
So index is listed in the list methods.

108
00:08:38,039 --> 00:08:41,100
If you scroll here, you'll see index.

109
00:08:41,130 --> 00:08:46,020
Now let's try this methods with underscores.

110
00:08:46,020 --> 00:08:50,160
So it has double underscores in the beginning.

111
00:08:50,670 --> 00:08:52,590
Double underscores at the end.

112
00:08:53,730 --> 00:09:01,500
All these methods with double underscores are actually used by Python internally we don't need to use

113
00:09:01,500 --> 00:09:03,560
them but what if we do?

114
00:09:03,570 --> 00:09:05,370
So let's try that.

115
00:09:06,330 --> 00:09:15,930
My list dot underscore, underscore, set, item, underscore, underscore with two parentheses.

116
00:09:15,930 --> 00:09:18,270
So this is the same syntax.

117
00:09:19,800 --> 00:09:20,610
As here.

118
00:09:21,870 --> 00:09:24,270
Now, this method takes two arguments.

119
00:09:26,310 --> 00:09:29,390
The first argument is an index.

120
00:09:29,400 --> 00:09:31,830
So let's try index one.

121
00:09:31,860 --> 00:09:34,890
Index one points to string B.

122
00:09:35,220 --> 00:09:37,080
The second argument is.

123
00:09:38,580 --> 00:09:42,240
The new value you want to set for the item with index one.

124
00:09:42,240 --> 00:09:43,650
So let's say.

125
00:09:45,260 --> 00:09:55,850
DX You press enter, you check my list and you'll see that DH has replaced B So that was the previous

126
00:09:55,850 --> 00:09:56,420
list.

127
00:09:56,450 --> 00:10:08,450
Now the list is updated so that there is the equivalent of saying my list one is equal to the new value.

128
00:10:08,450 --> 00:10:10,430
So let's try e this time.

129
00:10:10,460 --> 00:10:13,400
Now my list will be updated.

130
00:10:13,400 --> 00:10:15,050
So now DH is gone.

131
00:10:15,080 --> 00:10:16,490
E is the new item.

132
00:10:17,600 --> 00:10:19,280
So which one should you use?

133
00:10:19,640 --> 00:10:21,470
This or that?

134
00:10:22,380 --> 00:10:24,420
The short answer is use this.

135
00:10:25,170 --> 00:10:33,060
And actually when you use this, all the background, the interpreter actually calls the set item methods.

136
00:10:34,080 --> 00:10:38,910
So every changes that are done are being done by methods.

137
00:10:39,300 --> 00:10:47,550
But the Python code developers have been nice and they have provided this simpler syntax for very common

138
00:10:47,550 --> 00:10:50,070
methods such as setting an item.

139
00:10:50,490 --> 00:10:53,640
Similarly, there is also a get item.

140
00:10:55,450 --> 00:10:56,320
Methods.

141
00:10:58,450 --> 00:11:04,090
So my list does get item with double underscores.

142
00:11:04,630 --> 00:11:12,040
And let's try to this time I did a typo obviously so get.

143
00:11:13,010 --> 00:11:24,020
Item with one T and we get C, So the item with index two is C, and so that is the equivalent of try

144
00:11:24,020 --> 00:11:24,860
to guess it.

145
00:11:28,810 --> 00:11:33,760
Well this so you get See, my list stays the same.

146
00:11:33,760 --> 00:11:34,570
And this time.

147
00:11:36,140 --> 00:11:39,440
So in this case, we're not changing the list.

148
00:11:39,440 --> 00:11:47,210
Some methods such as set item, change the list and some other methods simply access some information

149
00:11:47,210 --> 00:11:50,480
from the list and they don't change the list itself.

150
00:11:52,720 --> 00:11:57,280
So that was a long video on code experiments, but plenty of information.

151
00:11:57,460 --> 00:11:58,900
So thanks a lot for following.

152
00:11:58,930 --> 00:11:59,440
See you.

