Hey, welcome to the Days experiment.
Today, I'll run a few cold experiments so that you better understand the list indexing and type conversion.
Now, the code that I'll be trying out are single lines of code.
So for that reason, I'll be using the Python console.
It's best to use the python console when you are just executing one liners.
So you saw that if the user entered some inputs.
And we store that input in a variable.
For example, if the user enters five and then you see a now is a string, you see the codes here.
So a plus ten would not make sense.
Therefore, you want to convert A to an integer using the INT function and perhaps store the output
in a new variable or maybe in the same variable.
It's up to you all store it in a new variable.
B So now.
B plus ten makes sense.
So B is an integer.
You see, it's without quotes.
And this has quotes around.
Now you might ask, why are types important?
Why can't we just have ten?
And it doesn't matter if it's a string or a number.
Why do values have to have a type?
Well, I'll answer you that with an example.
So again, A is a string, B is a number.
Now, if you do B times ten, guess what you will get.
Yeah, it's 50.
So five times ten you get 50.
Now let's try.
Times ten.
This is what you get.
So in Python, it is not possible to add a number to a string, but it is possible to multiply a number
with a string and the output you get is that string multiplied ten times in this case.
So five, five, five, five, five, five, five at ten times, and you get a new string.
And of course, you can have everything like high times ten.
And you get high, high, high, ten times.
So back to the question we asked, Why is it important to have types?
Well, the interpreter wants to know the type because it understands better what you intend to do.
So if you intend to have that string multiplied ten times, then you want to have that as a string type.
And then the interpreter knows that it is a string type.
So I don't have to do a math multiplication, I do a string multiplication if it's a string type.
And if you as a programmer mean to to do a math operation, then you want to make sure that this.
Value is an integer.
So having types for values is important to communicate with the interpreter, so things have to be as
explicit as possible.
Right.
The next experiment.
If you do it high, you will get an error.
So that's obvious.
You can also use floats.
Floats high, you will get an error again.
So it doesn't make sense to convert that string into an INT and it doesn't make sense to convert that
into a float.
So floats are decimal points and numbers.
If you convert.
String ten to a float, you'll get 10.0.
So that is different from converting string ten into INT you get ten.
So again, we have a different type here.
The distinction between floats and integers is not as important as the distinction between strings and
integers, but still you don't use floats whenever you are working with continuous data.
So if you have a variable which is the temperature, then you want to work with a float.
If you are working with whole numbers such as the number of persons in a company, you want to work
with integers.
Now Python has this implicit declaration of types so you don't have to use the float class when you
create a variable.
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.
A float.
But if you do, y is equal to ten, you'll see that Y is.
An integer.
So if it doesn't have that dot.
It's an integer.
If it does have that, it's a float.
And of course, if you use quotes around it, it doesn't matter if it does have the delta or not.
Type of set is going to be a string.
If for some reason you want to convert a number into a string, you can use a STR.
You not only convert integers to strings but also floats to strings.
You could ask when would you need to convert a number into a string?
Well, you'll see that when we create apps, then this SDR function can sometimes become useful.
For example, if you want to write some texts in a text file via Python, there is a method for that
which you will learn later on in a few days from now.
And in that case, you cannot write a number to other types to a text file.
So if you have a number, you'll have to convert it to a string and then write the output in that text
file and then the number would show in that dot txt text file or other file formats that Python is able
to create.
Let's run a few experiments on the list indexing now.
So let's create a list.
My list is equal to.
A, B, and C.
The first thing I want to make sure you understand well is that each of these items is treated as an
object.
So you can stroke the object, you can store it in a variable, for example, as that is equal to my
list.
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
variable which we declared somewhere in here.
So that is a string.
And with that we extracted the item with index one.
So a has an index of zero, B has an index of one.
That's why we get the string B and of course you can do other operations.
You can say that is equal to B, you get true.
Similarly, you can say my list.
One is equal to B, you get through.
So my list one and Z is the same value.
So both these points to the same value.
Now, how do we get the index of one object?
Let's say we want to know the index of B.
To do that, you need to use my list.
That index a method and provide the object you want to know the index of and you'll get one.
So that is the index of B.
So index is listed in the list methods.
If you scroll here, you'll see index.
Now let's try this methods with underscores.
So it has double underscores in the beginning.
Double underscores at the end.
All these methods with double underscores are actually used by Python internally we don't need to use
them but what if we do?
So let's try that.
My list dot underscore, underscore, set, item, underscore, underscore with two parentheses.
So this is the same syntax.
As here.
Now, this method takes two arguments.
The first argument is an index.
So let's try index one.
Index one points to string B.
The second argument is.
The new value you want to set for the item with index one.
So let's say.
DX You press enter, you check my list and you'll see that DH has replaced B So that was the previous
list.
Now the list is updated so that there is the equivalent of saying my list one is equal to the new value.
So let's try e this time.
Now my list will be updated.
So now DH is gone.
E is the new item.
So which one should you use?
This or that?
The short answer is use this.
And actually when you use this, all the background, the interpreter actually calls the set item methods.
So every changes that are done are being done by methods.
But the Python code developers have been nice and they have provided this simpler syntax for very common
methods such as setting an item.
Similarly, there is also a get item.
Methods.
So my list does get item with double underscores.
And let's try to this time I did a typo obviously so get.
Item with one T and we get C, So the item with index two is C, and so that is the equivalent of try
to guess it.
Well this so you get See, my list stays the same.
And this time.
So in this case, we're not changing the list.
Some methods such as set item, change the list and some other methods simply access some information
from the list and they don't change the list itself.
So that was a long video on code experiments, but plenty of information.
So thanks a lot for following.
See you.