WEBVTT 1 00:00:00.000 --> 00:00:02.520 [Jose Salvatierra] Hi guys, and welcome back. 2 00:00:02.520 --> 00:00:03.353 In this video, 3 00:00:03.353 --> 00:00:06.190 we're going to be learning about variables in Python 4 00:00:06.190 --> 00:00:08.940 and how to show information to the user. 5 00:00:08.940 --> 00:00:10.842 After all, programming is mostly about 6 00:00:10.842 --> 00:00:12.966 taking data in from users 7 00:00:12.966 --> 00:00:16.420 and giving them data out after doing some processing. 8 00:00:16.420 --> 00:00:18.700 So being able to show users some data 9 00:00:18.700 --> 00:00:20.764 is an essential part of coding. 10 00:00:20.764 --> 00:00:23.600 Let's begin by defining a variable. 11 00:00:23.600 --> 00:00:26.700 I'll say age equal thirty. 12 00:00:26.700 --> 00:00:28.930 This is how you define a variable in Python 13 00:00:28.930 --> 00:00:30.250 and give it a value. 14 00:00:30.250 --> 00:00:33.290 You first state the variable name, 15 00:00:33.290 --> 00:00:34.690 then an equal sign, 16 00:00:34.690 --> 00:00:37.530 and then the value that you wanna give it. 17 00:00:37.530 --> 00:00:39.560 If you want to print an age, 18 00:00:39.560 --> 00:00:41.380 all you have to do is say print 19 00:00:41.380 --> 00:00:43.690 and then inside brackets, 20 00:00:43.690 --> 00:00:45.650 the thing that you want to print out. 21 00:00:45.650 --> 00:00:47.520 So here, we've created the variable age 22 00:00:47.520 --> 00:00:48.910 and given it the value thirty, 23 00:00:48.910 --> 00:00:51.410 and then we are going to print it out 24 00:00:51.410 --> 00:00:52.763 and show it to the user. 25 00:00:53.790 --> 00:00:58.790 All Python programmes start off by running on a text console. 26 00:00:59.070 --> 00:01:01.260 Something like what you see here on the right. 27 00:01:01.260 --> 00:01:03.650 What that means is that for the foreseeable future, 28 00:01:03.650 --> 00:01:06.910 all our Python programmes are going to be dealing in text. 29 00:01:06.910 --> 00:01:09.200 They're going to receive text from the user 30 00:01:09.200 --> 00:01:11.240 and they're going to show the user text. 31 00:01:11.240 --> 00:01:12.540 If we run this programme, 32 00:01:12.540 --> 00:01:14.640 you'll see that thirty gets printed out 33 00:01:14.640 --> 00:01:16.870 because that is what our programme does. 34 00:01:16.870 --> 00:01:19.280 I will be keeping this code for you to see, 35 00:01:19.280 --> 00:01:21.270 so if I'm deleting things and moving things around, 36 00:01:21.270 --> 00:01:22.103 don't worry about it, 37 00:01:22.103 --> 00:01:23.920 you'll be able to see the complete code 38 00:01:23.920 --> 00:01:25.940 in the resources section just linked 39 00:01:25.940 --> 00:01:28.220 so you can go and have a look. 40 00:01:28.220 --> 00:01:30.070 Instead of printing age, 41 00:01:30.070 --> 00:01:31.920 you can print thirty directly 42 00:01:31.920 --> 00:01:32.840 and that's totally fine, 43 00:01:32.840 --> 00:01:34.780 it means exactly the same thing. 44 00:01:34.780 --> 00:01:38.121 Because age was a name for the value thirty, 45 00:01:38.121 --> 00:01:40.640 you can just use the value thirty directly 46 00:01:40.640 --> 00:01:42.250 and this will always be the case. 47 00:01:42.250 --> 00:01:43.530 Instead of a variable, 48 00:01:43.530 --> 00:01:47.160 you can always use the value that that variable refers to. 49 00:01:47.160 --> 00:01:49.780 However, there is a key benefit of having variables, 50 00:01:49.780 --> 00:01:52.670 which is that if you define an age variable to be thirty 51 00:01:52.670 --> 00:01:53.900 and then you print it out, 52 00:01:53.900 --> 00:01:56.690 then you can change the value and print it out. 53 00:01:56.690 --> 00:02:00.340 And now you only have one variable or one name, 54 00:02:00.340 --> 00:02:04.330 but initially it was a name for the value thirty 55 00:02:04.330 --> 00:02:05.880 and then in line four we changed it 56 00:02:05.880 --> 00:02:09.773 so that the name age is now a name for the value forty. 57 00:02:10.680 --> 00:02:11.710 So if we run this, 58 00:02:11.710 --> 00:02:13.700 you'll see that thirty gets printed out first, 59 00:02:13.700 --> 00:02:15.110 as we do in line two, 60 00:02:15.110 --> 00:02:17.290 and then forty gets printed out after. 61 00:02:17.290 --> 00:02:19.600 So this is just how variables work. 62 00:02:19.600 --> 00:02:21.000 You give them the name, 63 00:02:21.000 --> 00:02:21.930 then an equal sign, 64 00:02:21.930 --> 00:02:23.540 and then the value. 65 00:02:23.540 --> 00:02:26.480 If you want a longer variable name, 66 00:02:26.480 --> 00:02:29.653 for example, something like friend age, 67 00:02:30.870 --> 00:02:34.010 usually in Python you will use the underscore character 68 00:02:34.010 --> 00:02:36.410 to separate different words. 69 00:02:36.410 --> 00:02:37.490 In other programming languages 70 00:02:37.490 --> 00:02:40.360 you may do something like friend age like this, 71 00:02:40.360 --> 00:02:41.960 but Python developers don't like that. 72 00:02:41.960 --> 00:02:44.283 We use friend underscore age. 73 00:02:45.140 --> 00:02:47.340 And variable names can contain underscores 74 00:02:47.340 --> 00:02:50.810 and they can also contain numbers if you'd like. 75 00:02:50.810 --> 00:02:52.740 But they can't contain anything else, 76 00:02:52.740 --> 00:02:56.070 so you can't put dashes in them or any special symbols. 77 00:02:56.070 --> 00:02:59.620 In addition, variable names cannot start with a number. 78 00:02:59.620 --> 00:03:01.500 Here's another example of a variable 79 00:03:01.500 --> 00:03:05.540 that uses this type of separation between words. 80 00:03:05.540 --> 00:03:06.373 In programming, 81 00:03:06.373 --> 00:03:09.380 this type of separation is called snake case, 82 00:03:09.380 --> 00:03:11.240 so maybe something to remember 83 00:03:11.240 --> 00:03:14.130 in case you want to search for it in Google later on. 84 00:03:14.130 --> 00:03:16.270 If you have a variable name that you are 85 00:03:16.270 --> 00:03:18.780 arguably never going to change, 86 00:03:18.780 --> 00:03:22.200 then we usually write those all in uppercase letters. 87 00:03:22.200 --> 00:03:23.640 So for example, the number pi, 88 00:03:23.640 --> 00:03:25.900 which is 3.14159, 89 00:03:25.900 --> 00:03:27.530 you would write that all in uppercase, 90 00:03:27.530 --> 00:03:29.720 because you're never going to want to change it. 91 00:03:29.720 --> 00:03:31.140 This is just a convention, 92 00:03:31.140 --> 00:03:33.300 and what it tells you when you're reading this programme 93 00:03:33.300 --> 00:03:34.910 is that this is a constant, 94 00:03:34.910 --> 00:03:36.630 so it's never going to change. 95 00:03:36.630 --> 00:03:38.340 You can still change it, though, 96 00:03:38.340 --> 00:03:40.133 but it is just not recommended. 97 00:03:41.150 --> 00:03:43.440 Another example would be something like this. 98 00:03:43.440 --> 00:03:45.080 When you do radians to degrees, 99 00:03:45.080 --> 00:03:46.770 this defines a new variable 100 00:03:46.770 --> 00:03:50.420 and the value is one hundred and eighty divided by pi. 101 00:03:50.420 --> 00:03:53.162 So this is how you create variables and give them values. 102 00:03:53.162 --> 00:03:55.380 Now we're going to be looking at numbers 103 00:03:55.380 --> 00:03:56.490 in a bit more detail, 104 00:03:56.490 --> 00:03:59.270 so that we learn more about how mathematics works 105 00:03:59.270 --> 00:04:00.603 in Python. 106 00:04:00.603 --> 00:04:01.903 See you in the next video!