1 00:00:00,000 --> 00:00:06,000 This is the solution of the three small exercises I gave you in the previous video. 2 00:00:06,001 --> 00:00:08,000 So let's start with the first one. 3 00:00:08,001 --> 00:00:13,000 We have to ask for the name and the age of a user and print the info on the screen. 4 00:00:13,001 --> 00:00:16,526 So I'm going to create a variable, user name is 5 00:00:16,538 --> 00:00:20,000 equal to input and then let's print a message. 6 00:00:20,001 --> 00:00:24,000 What is your name? 7 00:00:24,001 --> 00:00:31,000 Click by the space like this, press enter. So what is your name? Let's say Bob. 8 00:00:31,001 --> 00:00:36,000 Okay, so now Bob is inside the username variable. 9 00:00:36,001 --> 00:00:42,000 I'm going to say user age is equal to input. 10 00:00:42,001 --> 00:00:46,000 How old are you? 11 00:00:46,001 --> 00:00:51,000 With another space and let's say 36. 12 00:00:51,001 --> 00:00:55,000 And now, well, we have username and we have user age. 13 00:00:55,001 --> 00:01:04,000 So what we can do, we can print, for example, hello username, you are user age. 14 00:01:04,001 --> 00:01:06,000 So let's do that. Hello. 15 00:01:06,001 --> 00:01:10,000 Okay, let's put a space and let's do plus. 16 00:01:10,001 --> 00:01:14,000 Okay, so we can concatenate the different strings. 17 00:01:14,001 --> 00:01:20,942 User name plus and then, for example, a comma, 18 00:01:20,954 --> 00:01:27,000 you are space and another plus user age. 19 00:01:27,001 --> 00:01:31,000 And if you remember the input here will give you a string. 20 00:01:31,001 --> 00:01:34,387 So as for now, the user age is stored as a string, 21 00:01:34,399 --> 00:01:38,000 which is nice here because that's what we need to do. 22 00:01:38,001 --> 00:01:42,000 We need to print a string. So we don't need to cast this actually. 23 00:01:42,001 --> 00:01:47,000 So let's press enter and you can see hello Bob, you are 36. 24 00:01:47,001 --> 00:01:50,000 Okay, that's it for the first exercise. 25 00:01:50,001 --> 00:01:53,688 Now the second exercise, you need to ask the user for two integer 26 00:01:53,700 --> 00:01:57,000 numbers, add them and then print the result on the screen. 27 00:01:57,001 --> 00:02:05,000 So because we need to add the two numbers, I'm going to need to cast what we get here. 28 00:02:05,001 --> 00:02:07,000 So the string to an integer. 29 00:02:07,001 --> 00:02:10,000 So let's say A, we're simply going to use A and B. 30 00:02:10,001 --> 00:02:13,000 Okay, for this A is equal to input. 31 00:02:13,001 --> 00:02:24,000 So let's say first number and I'm going to put int like this. 32 00:02:24,001 --> 00:02:27,000 Okay, int input. 33 00:02:27,001 --> 00:02:30,000 First number, let's say five. 34 00:02:30,001 --> 00:02:39,000 And then B is equal to int input second number. 35 00:02:39,001 --> 00:02:41,000 Okay, same thing. 36 00:02:41,001 --> 00:02:43,000 Let's give three. 37 00:02:43,001 --> 00:02:49,000 Okay, so now we have A and we have B, which are both integer numbers. 38 00:02:49,001 --> 00:02:54,000 I can create another variable, which is sum is equal to A plus B. 39 00:02:54,001 --> 00:02:57,000 Okay, now the sum is eight. 40 00:02:57,001 --> 00:03:00,000 And what I simply need to do now is print the result. 41 00:03:00,001 --> 00:03:05,000 So to print the result in one line, I'm going to do strA. 42 00:03:05,001 --> 00:03:11,000 So I'm going to cast A to a string, okay, so we can concatenate different strings. 43 00:03:11,001 --> 00:03:22,000 And then I'm going to add plus between two double quotes, okay, plus strB, like this, 44 00:03:22,001 --> 00:03:29,000 another plus, and then equal and then plus str. 45 00:03:29,001 --> 00:03:33,000 So, and actually I'm going to add some spaces here. 46 00:03:35,000 --> 00:03:37,000 To make it nicer. 47 00:03:37,001 --> 00:03:40,000 And we have five plus three is equal to eight. 48 00:03:40,001 --> 00:03:46,000 Okay, and let's just see what happens if I don't cast the integer to a string. 49 00:03:46,001 --> 00:03:52,000 You can see type error can only concatenate str, not integers. 50 00:03:52,001 --> 00:03:56,000 Okay, so you can only concatenate strings with strings. 51 00:03:56,001 --> 00:04:00,000 Okay, here this is an integer, so I need to cast it again to string. 52 00:04:00,001 --> 00:04:05,000 Okay, so to make the operation, you need to cast two integer. 53 00:04:05,001 --> 00:04:07,000 And then to print, you need to cast two string. 54 00:04:07,001 --> 00:04:14,000 Okay, and for the third exercise, so we need to create a list of four float numbers 55 00:04:14,001 --> 00:04:18,000 and then compute the average and print this average. 56 00:04:18,001 --> 00:04:20,000 So I'm going to create the list first. 57 00:04:20,001 --> 00:04:27,000 So number list is equal to, and let's put brackets and let's give some numbers, okay. 58 00:04:27,001 --> 00:04:33,000 Let's say 4.4, 5.5, 6.6, 7.7, okay. 59 00:04:33,001 --> 00:04:34,000 Like this. 60 00:04:34,001 --> 00:04:38,000 So number list, we have four values. 61 00:04:38,001 --> 00:04:41,000 I'm going to create a new variable named average. 62 00:04:41,001 --> 00:04:47,000 So to compute the average, we have to sum all the elements of the list 63 00:04:47,001 --> 00:04:51,000 and divide that by the number of elements, okay. 64 00:04:51,001 --> 00:04:55,000 So to make the sum of all elements, what I could do, 65 00:04:55,001 --> 00:05:01,000 I could do number list zero plus number list one plus number list two, etc, etc. 66 00:05:01,001 --> 00:05:02,000 But that's really not convenient. 67 00:05:02,001 --> 00:05:07,000 So as I showed you before, you can use the sum function. 68 00:05:07,001 --> 00:05:12,000 So sum number list, we'll just sum all the elements in the list. 69 00:05:12,001 --> 00:05:16,000 And then you need to divide by the number of elements in the list. 70 00:05:16,001 --> 00:05:20,000 And to know that, you can use the len function, 71 00:05:20,001 --> 00:05:23,000 which is actually going to compute the length of the number list. 72 00:05:24,000 --> 00:05:29,000 So length number list, I press enter. 73 00:05:29,001 --> 00:05:32,000 And now you have average 6.05. 74 00:05:32,001 --> 00:05:35,000 So that is the average of the list. 75 00:05:35,001 --> 00:05:39,000 Okay, so you can see with Python, you can go quite fast. 76 00:05:39,001 --> 00:05:44,000 And just with a few lines of code, you can compute the average of a list. 77 00:05:44,001 --> 00:05:45,000 All right. 78 00:05:45,001 --> 00:05:50,000 And that's the end of this first section for the Python basics level one.