This is the solution of the three small exercises I gave you in the previous video.
So let's start with the first one.
We have to ask for the name and the age of a user and print the info on the screen.
So I'm going to create a variable, user name is
equal to input and then let's print a message.
What is your name?
Click by the space like this, press enter. So what is your name? Let's say Bob.
Okay, so now Bob is inside the username variable.
I'm going to say user age is equal to input.
How old are you?
With another space and let's say 36.
And now, well, we have username and we have user age.
So what we can do, we can print, for example, hello username, you are user age.
So let's do that. Hello.
Okay, let's put a space and let's do plus.
Okay, so we can concatenate the different strings.
User name plus and then, for example, a comma,
you are space and another plus user age.
And if you remember the input here will give you a string.
So as for now, the user age is stored as a string,
which is nice here because that's what we need to do.
We need to print a string. So we don't need to cast this actually.
So let's press enter and you can see hello Bob, you are 36.
Okay, that's it for the first exercise.
Now the second exercise, you need to ask the user for two integer
numbers, add them and then print the result on the screen.
So because we need to add the two numbers, I'm going to need to cast what we get here.
So the string to an integer.
So let's say A, we're simply going to use A and B.
Okay, for this A is equal to input.
So let's say first number and I'm going to put int like this.
Okay, int input.
First number, let's say five.
And then B is equal to int input second number.
Okay, same thing.
Let's give three.
Okay, so now we have A and we have B, which are both integer numbers.
I can create another variable, which is sum is equal to A plus B.
Okay, now the sum is eight.
And what I simply need to do now is print the result.
So to print the result in one line, I'm going to do strA.
So I'm going to cast A to a string, okay, so we can concatenate different strings.
And then I'm going to add plus between two double quotes, okay, plus strB, like this,
another plus, and then equal and then plus str.
So, and actually I'm going to add some spaces here.
To make it nicer.
And we have five plus three is equal to eight.
Okay, and let's just see what happens if I don't cast the integer to a string.
You can see type error can only concatenate str, not integers.
Okay, so you can only concatenate strings with strings.
Okay, here this is an integer, so I need to cast it again to string.
Okay, so to make the operation, you need to cast two integer.
And then to print, you need to cast two string.
Okay, and for the third exercise, so we need to create a list of four float numbers
and then compute the average and print this average.
So I'm going to create the list first.
So number list is equal to, and let's put brackets and let's give some numbers, okay.
Let's say 4.4, 5.5, 6.6, 7.7, okay.
Like this.
So number list, we have four values.
I'm going to create a new variable named average.
So to compute the average, we have to sum all the elements of the list
and divide that by the number of elements, okay.
So to make the sum of all elements, what I could do,
I could do number list zero plus number list one plus number list two, etc, etc.
But that's really not convenient.
So as I showed you before, you can use the sum function.
So sum number list, we'll just sum all the elements in the list.
And then you need to divide by the number of elements in the list.
And to know that, you can use the len function,
which is actually going to compute the length of the number list.
So length number list, I press enter.
And now you have average 6.05.
So that is the average of the list.
Okay, so you can see with Python, you can go quite fast.
And just with a few lines of code, you can compute the average of a list.
All right.
And that's the end of this first section for the Python basics level one.