Yesterday, you saw that we could use the len() function to get the number of characters in a string.
So for example, when I write len("hello"), and I print this out, we end up with 5, there is 5 characters
in the string
"hello".
Now here's a question.
What happens if instead of counting the number of characters in a string, what if I put in a number
instead and I wanted to know how many digits are in this number?
Now, if I go ahead and run this code, you'll see that it actually crashes.
I get a whole bunch of red text, and it tells me that there is a TypeError and something about the
type 'int'.
So what is all of this about?
Well, in order to understand it, we first have to learn about Data Types.
Now, on day one we already explored this data type called strings.
Now, you're probably not going to be surprised to learn that there's a whole lot more other data types
out there.
And today we're going to explore some of the most important and some of the most basic data types,
such as Strings, Integers, Floats, and Booleans.
And we'll explore each of these in detail.
So let's take a look at some of the data types.
So we already learned about strings, right?
And we know that this is just a string of characters.
So the word "hello" is comprised of these five characters strung together.
And we always know that strings, when we create them, we have to create them with these double quotes
around.
Now, because this is a string of characters we can actually pull out each character individually.
So we could, for example, instead of just writing "hello", we can add some square brackets.
So take a look on your keyboard and see where those are.
And inside the square brackets we can put the index or the position of the character that we want.
So for example, if I wanted to have the first character out of this word, "Hello",
I would put 0 right here.
And if I go ahead and print what this actually will give me, you'll see that you get capital H, because
that is the first character of this string.
And it's really important to remember that programmers always start counting from zero because we work
with binary zeros and ones.
So whenever you want to get hold of the first character or the first of anything, it always is at 0.
So this method of pulling out a particular element from a string is called subscripting, and the number
in between the square brackets determines which character you're going to pull out.
And it just goes up from 0 to 1 to 2, and so on and so forth.
So whenever you get a result that's just off by one, then remember to check whether if you've started
counting from zero or if you started counting from one.
Now you can of course extend this and get hold of the last character.
So pause the video and see if you can change the code so that O gets printed out here.
All right.
So this is as simple as simply counting from zero, one two, three, four.
So if we change this to 4 and we run our code, then you'll see that O gets printed instead of the
H.
Now there's also a secret trick that not many people know, even seasoned Python developers,
and it's the fact that you can actually use negative indices.
So instead of specifying the position as a positive number, you can also use a negative number, and
-1, means get hold of the last character in the string so we can go ahead and run this, and you
can see it's also O, and we can start counting backwards as well, starting from the end going to
the beginning.
So that's just a quick tip.
So by using these square brackets and putting a number inside, we're able to dissect our string and
pull out individual characters as and when we need it, and this will come in really handy in a lot
of the programs that you'll write in the future.
Now, it's important to remember, though, that just because I can write a number like, "1, 2,
3," as long as it's capped inside these double quotes, then this is not treated as a number by
the computer.
It's treated just as any other piece of text.
You can't, for example, say, what is "123" + "345"?
If I try to print this, what do you think will happen?
Do you think it will give me 123+345, in the traditional sense, like
calculating it?
Or do you think it'll do something else?
All right, let's hit Run.
And we get 123345.
So it's basically just concatenated these two strings together, just as we have done with other strings
like Hello and World.
Right?
Because it sees the data type of these two pieces of data as strings,
when we use the plus sign, it will actually just concatenate these two things instead of doing a mathematical
operation.
Now, if we wanted to do that, then we actually have to declare our number as a number data type.
So one of the most common that you'll see is called an integer.
So this is programming lingo for just whole numbers.
Numbers without any decimal places.
And in order to create an integer or declare an integer data type, all you have to do is just write
the number without anything else.
So now if I just write the numbers 123 + 345, and then I go ahead
and print this, then you'll see that we actually get 468.
So it's actually being calculated because I've got actual numbers instead of strings.
Just as we have some useful things that we can do with strings, there's some really handy things that
you can do with integers.
Commonly, when we write large numbers, at least in the UK or in the US, we like to put commas in
between the thousands.
So when we think of large numbers with these commas in between to split it into an easier to understand
number,
in Python we can replace those commas simply with underscores, and it will be interpreted by the computer
as if you had written this.
So the computer actually removes those underscores and ignores it.
The benefit is just for us humans to be able to visualize it more easily.
So I mentioned that all whole numbers, no matter if they're positive or negative, are called integers
in programming.
So what do you call it when you actually have decimal places?
Well, they're called a float.
And this is short for a floating point number.
So for example, if you had the numbers of pi, you have 3.14159.
And this because it has a decimal place is now a float data type.
So if you think of the decimal point as being able to float around the number because it could occur
at any point, then you've got yourself a floating point number.
Now the final data type is something called a Boolean.
And this is very simple.
It only has two possible values True or False.
Now note how these values always begin with a capital T or a capital F, and they don't have any quotation
marks around them or anything.
So this is actually a data type which is going to be used a lot in your programs to test if something
is true or if something is false, and for your program to respond accordingly.
So we're going to be using this a lot more in the future.
Now that you've seen a lot of the basic data types Strings, Integers, Floats, and Boolean, I want
you to head over to the next lesson where I've got a quiz for you to see if you've made this knowledge
your own.
So head over to the quiz and give it a go.