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's 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 type error 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 1, 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, float, and booleans.
And we'll explore each of these in detail.
So go ahead and get hold of the day 2 start coding sandbox and go ahead and
fork your own copy of it. Now once you've done that,
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 zero 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 zero.
So this method of pulling out a particular element from a string is called sub-
scripting. And the number in between the square brackets determines
which character you're going to pull out,
and it just goes up from zero to one to two 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 a simple as simply counting from zero, one, two, three,
four. So if we change this to four and we run our code,
then you'll see that 'o' gets printed instead of the H.
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 "123",
as long as it's kept 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,
um, 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 datatype 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 want 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'd 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 are called a float and this is short for a floating-point number.
So for example, if you had um, 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 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,
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.