WEBVTT

0
00:00.080 --> 00:07.310
Yesterday, you saw that we could use the len() function to get the number of characters in a string.

1
00:07.310 --> 00:16.430
So for example, when I write len("hello"), and I print this out, we end up with 5, there is 5 characters

2
00:16.430 --> 00:17.060
in the string

3
00:17.060 --> 00:17.750
"hello".

4
00:17.990 --> 00:19.850
Now here's a question.

5
00:19.850 --> 00:28.100
What happens if instead of counting the number of characters in a string, what if I put in a number

6
00:28.100 --> 00:32.610
instead and I wanted to know how many digits are in this number?

7
00:33.180 --> 00:38.010
Now, if I go ahead and run this code, you'll see that it actually crashes.

8
00:38.010 --> 00:45.510
I get a whole bunch of red text, and it tells me that there is a TypeError and something about the

9
00:45.510 --> 00:47.010
type 'int'.

10
00:47.250 --> 00:49.440
So what is all of this about?

11
00:49.530 --> 00:54.540
Well, in order to understand it, we first have to learn about Data Types.

12
00:54.990 --> 00:59.820
Now, on day one we already explored this data type called strings.

13
00:59.850 --> 01:05.160
Now, you're probably not going to be surprised to learn that there's a whole lot more other data types

14
01:05.160 --> 01:05.970
out there.

15
01:05.970 --> 01:12.630
And today we're going to explore some of the most important and some of the most basic data types,

16
01:12.630 --> 01:15.960
such as Strings, Integers, Floats, and Booleans.

17
01:15.960 --> 01:18.810
And we'll explore each of these in detail.

18
01:18.970 --> 01:21.730
So let's take a look at some of the data types.

19
01:21.730 --> 01:24.580
So we already learned about strings, right?

20
01:24.580 --> 01:29.140
And we know that this is just a string of characters.

21
01:29.140 --> 01:34.450
So the word "hello" is comprised of these five characters strung together.

22
01:34.450 --> 01:39.940
And we always know that strings, when we create them, we have to create them with these double quotes

23
01:39.940 --> 01:40.570
around.

24
01:40.570 --> 01:46.940
Now, because this is a string of characters we can actually pull out each character individually.

25
01:46.940 --> 01:53.420
So we could, for example, instead of just writing "hello", we can add some square brackets.

26
01:53.420 --> 01:56.750
So take a look on your keyboard and see where those are.

27
01:56.750 --> 02:04.370
And inside the square brackets we can put the index or the position of the character that we want.

28
02:04.370 --> 02:09.720
So for example, if I wanted to have the first character out of this word, "Hello",

29
02:09.750 --> 02:11.940
I would put 0 right here.

30
02:11.940 --> 02:19.800
And if I go ahead and print what this actually will give me, you'll see that you get capital H, because

31
02:19.800 --> 02:24.030
that is the first character of this string.

32
02:24.030 --> 02:30.840
And it's really important to remember that programmers always start counting from zero because we work

33
02:30.840 --> 02:32.640
with binary zeros and ones.

34
02:32.640 --> 02:39.990
So whenever you want to get hold of the first character or the first of anything, it always is at 0.

35
02:40.260 --> 02:48.000
So this method of pulling out a particular element from a string is called subscripting, and the number

36
02:48.000 --> 02:51.750
in between the square brackets determines which character you're going to pull out.

37
02:51.750 --> 02:57.330
And it just goes up from 0 to 1 to 2, and so on and so forth.

38
02:57.330 --> 03:04.030
So whenever you get a result that's just off by one, then remember to check whether if you've started

39
03:04.030 --> 03:07.750
counting from zero or if you started counting from one.

40
03:08.170 --> 03:12.820
Now you can of course extend this and get hold of the last character.

41
03:12.820 --> 03:18.070
So pause the video and see if you can change the code so that O gets printed out here.

42
03:19.300 --> 03:19.780
All right.

43
03:19.780 --> 03:24.890
So this is as simple as simply counting from zero, one two, three, four.

44
03:24.890 --> 03:31.100
So if we change this to 4 and we run our code, then you'll see that O gets printed instead of the

45
03:31.100 --> 03:31.670
H.

46
03:31.850 --> 03:38.090
Now there's also a secret trick that not many people know, even seasoned Python developers,

47
03:38.090 --> 03:41.840
and it's the fact that you can actually use negative indices.

48
03:41.840 --> 03:49.310
So instead of specifying the position as a positive number, you can also use a negative number, and

49
03:49.310 --> 03:56.240
-1, means get hold of the last character in the string so we can go ahead and run this, and you

50
03:56.240 --> 04:02.030
can see it's also O, and we can start counting backwards as well, starting from the end going to

51
04:02.030 --> 04:02.810
the beginning.

52
04:02.810 --> 04:04.400
So that's just a quick tip.

53
04:04.400 --> 04:11.330
So by using these square brackets and putting a number inside, we're able to dissect our string and

54
04:11.330 --> 04:17.220
pull out individual characters as and when we need it, and this will come in really handy in a lot

55
04:17.220 --> 04:19.470
of the programs that you'll write in the future.

56
04:19.860 --> 04:26.040
Now, it's important to remember, though, that just because I can write a number like, "1, 2,

57
04:26.040 --> 04:33.750
3," as long as it's capped inside these double quotes, then this is not treated as a number by

58
04:33.750 --> 04:34.380
the computer.

59
04:34.380 --> 04:37.260
It's treated just as any other piece of text.

60
04:37.360 --> 04:44.140
You can't, for example, say, what is "123" + "345"?

61
04:44.140 --> 04:48.850
If I try to print this, what do you think will happen?

62
04:48.850 --> 04:54.130
Do you think it will give me 123+345, in the traditional sense, like

63
04:54.130 --> 04:55.300
calculating it?

64
04:55.300 --> 04:57.490
Or do you think it'll do something else?

65
04:57.760 --> 04:59.350
All right, let's hit Run.

66
04:59.350 --> 05:02.980
And we get 123345.

67
05:02.980 --> 05:09.400
So it's basically just concatenated these two strings together, just as we have done with other strings

68
05:09.400 --> 05:11.830
like Hello and World.

69
05:11.830 --> 05:12.310
Right?

70
05:12.820 --> 05:17.710
Because it sees the data type of these two pieces of data as strings,

71
05:17.710 --> 05:25.270
when we use the plus sign, it will actually just concatenate these two things instead of doing a mathematical

72
05:25.270 --> 05:26.350
operation.

73
05:26.350 --> 05:33.170
Now, if we wanted to do that, then we actually have to declare our number as a number data type.

74
05:33.170 --> 05:36.830
So one of the most common that you'll see is called an integer.

75
05:36.830 --> 05:40.400
So this is programming lingo for just whole numbers.

76
05:40.400 --> 05:43.400
Numbers without any decimal places.

77
05:43.400 --> 05:50.240
And in order to create an integer or declare an integer data type, all you have to do is just write

78
05:50.240 --> 05:52.650
the number without anything else.

79
05:53.040 --> 05:59.100
So now if I just write the numbers 123 + 345, and then I go ahead

80
05:59.100 --> 06:03.600
and print this, then you'll see that we actually get 468.

81
06:03.600 --> 06:09.570
So it's actually being calculated because I've got actual numbers instead of strings.

82
06:10.140 --> 06:15.330
Just as we have some useful things that we can do with strings, there's some really handy things that

83
06:15.330 --> 06:16.840
you can do with integers.

84
06:17.140 --> 06:24.940
Commonly, when we write large numbers, at least in the UK or in the US, we like to put commas in

85
06:24.940 --> 06:26.170
between the thousands.

86
06:26.170 --> 06:32.830
So when we think of large numbers with these commas in between to split it into an easier to understand

87
06:32.830 --> 06:33.550
number,

88
06:33.640 --> 06:40.900
in Python we can replace those commas simply with underscores, and it will be interpreted by the computer

89
06:40.900 --> 06:43.390
as if you had written this.

90
06:43.390 --> 06:46.960
So the computer actually removes those underscores and ignores it.

91
06:47.080 --> 06:52.150
The benefit is just for us humans to be able to visualize it more easily.

92
06:52.540 --> 06:58.240
So I mentioned that all whole numbers, no matter if they're positive or negative, are called integers

93
06:58.240 --> 06:59.380
in programming.

94
06:59.380 --> 07:02.800
So what do you call it when you actually have decimal places?

95
07:02.800 --> 07:04.750
Well, they're called a float.

96
07:04.750 --> 07:08.360
And this is short for a floating point number.

97
07:08.360 --> 07:15.560
So for example, if you had the numbers of pi, you have 3.14159.

98
07:15.680 --> 07:21.140
And this because it has a decimal place is now a float data type.

99
07:21.380 --> 07:27.860
So if you think of the decimal point as being able to float around the number because it could occur

100
07:27.860 --> 07:31.980
at any point, then you've got yourself a floating point number.

101
07:32.910 --> 07:37.110
Now the final data type is something called a Boolean.

102
07:37.770 --> 07:39.300
And this is very simple.

103
07:39.300 --> 07:43.950
It only has two possible values True or False.

104
07:43.980 --> 07:51.840
Now note how these values always begin with a capital T or a capital F, and they don't have any quotation

105
07:51.840 --> 07:53.280
marks around them or anything.

106
07:53.280 --> 07:59.760
So this is actually a data type which is going to be used a lot in your programs to test if something

107
07:59.760 --> 08:04.350
is true or if something is false, and for your program to respond accordingly.

108
08:04.350 --> 08:07.530
So we're going to be using this a lot more in the future.

109
08:07.530 --> 08:13.470
Now that you've seen a lot of the basic data types Strings, Integers, Floats, and Boolean, I want

110
08:13.470 --> 08:18.600
you to head over to the next lesson where I've got a quiz for you to see if you've made this knowledge

111
08:18.600 --> 08:19.200
your own.

112
08:19.200 --> 08:21.630
So head over to the quiz and give it a go.