WEBVTT 1 00:00:02.220 --> 00:00:05.970 So continuing on from the previous video in our introduction to Python, so 2 00:00:05.970 --> 00:00:07.730 we've learned about the print command and 3 00:00:07.730 --> 00:00:11.880 we've learned a little bit about streams and a little bit about operators and 4 00:00:11.880 --> 00:00:14.100 a little bit about adding numbers and so forth together. 5 00:00:14.100 --> 00:00:15.810 And let's now use another function. 6 00:00:15.810 --> 00:00:18.260 So what we can do to make this a bit easier and 7 00:00:18.260 --> 00:00:21.780 a bit more useful is we can create a way to actually show 8 00:00:21.780 --> 00:00:25.350 a name that's entered from the keyboard on the screen. 9 00:00:25.350 --> 00:00:27.890 So what I'm gonna do is I'm gonna delete this other code that has 10 00:00:27.890 --> 00:00:30.020 already been used, cuz we don't need that anymore. 11 00:00:30.020 --> 00:00:33.290 Complete delete all that and we're gonna start by typing greeting. 12 00:00:33.290 --> 00:00:35.060 And greeting again is the name of our variable, 13 00:00:35.060 --> 00:00:36.840 I will be talking more about variables shortly. 14 00:00:37.940 --> 00:00:38.860 Hello. 15 00:00:38.860 --> 00:00:45.465 We can type name = input ("Please enter your name "). 16 00:00:47.865 --> 00:00:55.440 Then we can type print(greeting + notice I've added a space there, plus Name. 17 00:00:55.440 --> 00:00:58.290 And we just run that to see what it does. 18 00:00:58.290 --> 00:01:01.410 And the other thing is once you actually have got the run window open, 19 00:01:01.410 --> 00:01:04.810 and we don't need to right click, although we can run it that way, 20 00:01:04.810 --> 00:01:08.050 you can also just come over here and click on the green arrow and run it that, so 21 00:01:08.050 --> 00:01:09.740 it's asking us to please enter our name. 22 00:01:09.740 --> 00:01:13.870 I can type in Tim, press enter, and it actually comes up and says, Hello Tim. 23 00:01:13.870 --> 00:01:15.770 So it's actually looked at what we've typed, and 24 00:01:15.770 --> 00:01:19.000 it's recorded that into this variable called name. 25 00:01:19.000 --> 00:01:22.800 And the message has then printed the greeting, which is hello, another space, 26 00:01:22.800 --> 00:01:25.280 then added what we've typed, which is in this case, name. 27 00:01:25.280 --> 00:01:28.160 So essentially as you saw, the input function displays the text that's 28 00:01:28.160 --> 00:01:30.960 been provided to it then waits for text to be entered into the keyboard. 29 00:01:30.960 --> 00:01:33.710 So, in other words, display this message: please enter your name, 30 00:01:33.710 --> 00:01:36.670 then waited until we typed something, then we actually pressed enter. 31 00:01:36.670 --> 00:01:40.020 The text, at that point, you typed is stored in the variable name as a set, and 32 00:01:40.020 --> 00:01:42.160 that's used by the print function to actually show the greeting. 33 00:01:42.160 --> 00:01:45.100 Now in terms of strings there's also a special character called 34 00:01:45.100 --> 00:01:46.110 the backslash character. 35 00:01:46.110 --> 00:01:49.710 That's actually got a special meaning in strings, so it's used to escape 36 00:01:49.710 --> 00:01:52.580 the character that follows it to give that character special meaning. 37 00:01:52.580 --> 00:01:55.030 So there's lots of different control characters, and 38 00:01:55.030 --> 00:01:56.390 we won't go through them all now. 39 00:01:56.390 --> 00:01:58.950 But some of them will actually do things like start a new line, 40 00:01:58.950 --> 00:02:01.330 or provide a tab to actually provide tabbed output. 41 00:02:01.330 --> 00:02:02.988 So let's just go through and look at a couple of those. 42 00:02:02.988 --> 00:02:07.525 So we can type something like 43 00:02:07.525 --> 00:02:12.631 'splitString = "This string 44 00:02:12.631 --> 00:02:17.547 has been, I am typing \nsplit 45 00:02:17.547 --> 00:02:22.860 over \nseveral\nlines"'. 46 00:02:22.860 --> 00:02:23.540 And if we print that. 47 00:02:24.650 --> 00:02:27.610 And that's when I typed the first few characters of the variable, 48 00:02:27.610 --> 00:02:30.580 IntelliJ quite nicely comes up and says, or do you mean splitString. 49 00:02:30.580 --> 00:02:33.410 So again if you want that, you don't have to type the whole variable name. 50 00:02:33.410 --> 00:02:36.230 You can just press enter at that point, and it actually puts the rest in for 51 00:02:36.230 --> 00:02:37.065 us automatically. 52 00:02:37.065 --> 00:02:40.365 So if I run that, or actually I need to finish off that from the last one, so 53 00:02:40.365 --> 00:02:41.775 I'll pop my name in. 54 00:02:41.775 --> 00:02:44.975 I can see how that worked, and let me just actually delete this first part up here. 55 00:02:44.975 --> 00:02:46.625 Actually what I'll do, is I'll actually comment it out. 56 00:02:46.625 --> 00:02:51.525 So I can press Cmd+Slash on my Mac, and notice how I put the slashes in, and 57 00:02:51.525 --> 00:02:54.545 down at the bottom it tells you if press Ctrl+Slash on Windows or 58 00:02:54.545 --> 00:02:55.765 Linux machines it'll do the same. 59 00:02:55.765 --> 00:02:58.090 So, what I've done is I've commented that code out, 60 00:02:58.090 --> 00:03:00.290 which means that that now won't run. 61 00:03:00.290 --> 00:03:03.010 Because, remember, a comment is there purely for your reference. 62 00:03:03.010 --> 00:03:03.810 So if I run this again, 63 00:03:03.810 --> 00:03:06.770 we'll finally get straight to this little example of the string. 64 00:03:06.770 --> 00:03:08.340 So you can see what's happened in this, 65 00:03:08.340 --> 00:03:11.790 \n has actually caused the cursor to start on the next line. 66 00:03:11.790 --> 00:03:15.090 So consequently, for each one of those \n's we added that actually 67 00:03:15.090 --> 00:03:17.280 spit onto a separate line each time we did it. 68 00:03:17.280 --> 00:03:20.080 We could do something similar with a tab string. 69 00:03:23.540 --> 00:03:31.260 So if we type tabbedString = "1\t2\t3\t4\t5" and another tab again. 70 00:03:31.260 --> 00:03:34.150 And if we do print tabbedString, if you run that, 71 00:03:34.150 --> 00:03:37.080 you can see how that's done something a little bit different. 72 00:03:37.080 --> 00:03:39.857 It's used a standard tab function which you've probably seen in a word 73 00:03:39.857 --> 00:03:42.818 processor before to tab things out which is a way of sort of organizing things 74 00:03:42.818 --> 00:03:44.170 in a certain position. 75 00:03:44.170 --> 00:03:47.200 So basically, the point is that the backslash character 76 00:03:47.200 --> 00:03:49.970 is actually interpreted specifically by Python. 77 00:03:49.970 --> 00:03:53.550 And then depending on the character that is typed after that backslash, and 78 00:03:53.550 --> 00:03:56.000 then it performs a special function in that stream. 79 00:03:56.000 --> 00:03:57.370 So that's your sort of slash end, 80 00:03:57.370 --> 00:04:01.680 the first one, it cause it goes to the start of a new line, and then the slash t 81 00:04:01.680 --> 00:04:05.610 cause it to tab to the next tab position before I started the printing again. 82 00:04:05.610 --> 00:04:09.100 You can also use that to escape special characters such as quote and 83 00:04:09.100 --> 00:04:12.848 double quotes, and that's useful if you've got a string containing both characters. 84 00:04:12.848 --> 00:04:14.820 And probably the example would be the best thing to show you there. 85 00:04:14.820 --> 00:04:16.860 I'll comment that out again. 86 00:04:18.770 --> 00:04:23.799 So I come down here and I'll type 87 00:04:23.799 --> 00:04:30.132 print ("The pet shop owner said "No, 88 00:04:30.132 --> 00:04:33.870 no, \'e\'s"). 89 00:04:33.870 --> 00:04:36.237 You'll probably recognize this if you're a Monty Python fan. 90 00:04:42.405 --> 00:04:44.240 I'll just close this down so I can see this a bit better. 91 00:04:44.240 --> 00:04:46.682 That should have actually been a single quote at the start and not a double. 92 00:04:46.682 --> 00:04:50.850 So what we are doing, is we are saying that we are using both single quotes and 93 00:04:50.850 --> 00:04:54.010 double quotes in a particular string which you may wanna use some time. 94 00:04:54.010 --> 00:04:57.260 Bearing in mind that we started off with this string by saying it's a single quote. 95 00:04:57.260 --> 00:05:00.640 But by putting the backslash in front of the quote you wanna use, that actually 96 00:05:00.640 --> 00:05:04.390 tells the string, or tells Python, to treat it not as a special command to close 97 00:05:04.390 --> 00:05:07.088 the string in this case, but to set the string that can actually be printed out. 98 00:05:07.088 --> 00:05:09.940 And you can also do something conversely like this as well, 99 00:05:09.940 --> 00:05:11.710 which will give you the same results. 100 00:05:13.510 --> 00:05:14.280 Pet shop owner. 101 00:05:16.680 --> 00:05:17.200 Said, 102 00:05:29.032 --> 00:05:29.610 Like that. 103 00:05:29.610 --> 00:05:31.979 So if we actually run that now so you can see what I mean. 104 00:05:34.400 --> 00:05:37.350 And you can see what we've done there, and basically got the same result. 105 00:05:37.350 --> 00:05:39.690 But how we've used the start and the end quotes and 106 00:05:39.690 --> 00:05:43.650 the slashes has caused the screen to actually print out in the same way. 107 00:05:43.650 --> 00:05:46.940 So in the first example what the pet owner said was in double quotes but 108 00:05:46.940 --> 00:05:50.130 his statement includes apostrophes so we had to escape one or the other. 109 00:05:50.130 --> 00:05:52.090 And that's actually what we did in those examples. 110 00:05:52.090 --> 00:05:56.150 So in this case the first method, the first part of this was actually the third. 111 00:05:56.150 --> 00:05:57.470 If there's print statement in other words. 112 00:05:57.470 --> 00:06:01.280 For no other reason other than the fact that if you type in two consecutive quotes 113 00:06:01.280 --> 00:06:04.930 either a single one or a double one, IntelliJ will type a third one for you. 114 00:06:04.930 --> 00:06:07.700 Now, you might be wondering why would you actually want to actually use three 115 00:06:07.700 --> 00:06:12.445 consecutive quotes, well this is actually a feature of Python, and what it does, 116 00:06:12.445 --> 00:06:16.900 it's actually provided by Python to make strings that spanned lines more readable. 117 00:06:16.900 --> 00:06:19.800 So, in other words, the string that we split over several lines earlier is not 118 00:06:19.800 --> 00:06:22.240 easy to read because of the embedded slash. 119 00:06:22.240 --> 00:06:26.270 And as you saw Python allows a string to be split over several lines without doing 120 00:06:26.270 --> 00:06:31.618 that by using triple quotes, and the way to do that, we type something like this. 121 00:06:31.618 --> 00:06:36.665 AnotherSplitString = """ This 122 00:06:36.665 --> 00:06:43.741 string has been split over several lines""". 123 00:06:44.880 --> 00:06:47.820 There's the three quotes, and my typing. 124 00:06:47.820 --> 00:06:51.960 Okay, so that's actually a way to actually create the string, and 125 00:06:51.960 --> 00:06:54.130 without having to use any backslashes. 126 00:06:54.130 --> 00:06:56.580 So, in other words, you wanna lay out a string with breaks, but 127 00:06:56.580 --> 00:06:58.940 you don't want every line to start a new line with printed, 128 00:06:58.940 --> 00:07:02.000 you can escape the end of the line with slash dot as well. 129 00:07:02.000 --> 00:07:05.495 Let's see what that looks like so print another string, anotherSplitString. 130 00:07:06.570 --> 00:07:10.110 Now, if I run that, this string has been split over several lines. 131 00:07:10.110 --> 00:07:12.910 I can see it has done the equivalent function to the slash 132 00:07:12.910 --> 00:07:14.270 ns that we were using earlier. 133 00:07:14.270 --> 00:07:17.730 The other thing you can do is, if you wanna lay out a string with lots of breaks 134 00:07:17.730 --> 00:07:20.450 but you don't want everyone to start a new line when printed, 135 00:07:20.450 --> 00:07:23.320 what you can do is escape the end of the line with a slash dot. 136 00:07:23.320 --> 00:07:26.410 So experiment adding a backslash to the end of the lines to see what happens when 137 00:07:26.410 --> 00:07:27.440 you run the program. 138 00:07:27.440 --> 00:07:30.020 And getting back to the Monty Python example again, 139 00:07:30.020 --> 00:07:32.700 we can actually also do something like this. 140 00:07:32.700 --> 00:07:37.342 We can type using the triple quotes, 141 00:07:37.342 --> 00:07:43.327 we can do (' ' ' The pet shop owner said "No, 142 00:07:43.327 --> 00:07:50.245 no, 'e's' uh,...he's resting" ' ' '). 143 00:07:50.245 --> 00:07:52.015 Like so, which is another way of doing it. 144 00:07:52.015 --> 00:07:56.135 So you can see that the triple quote feature is actually quite handy. 145 00:07:56.135 --> 00:07:57.695 And conversely we could do something like this. 146 00:07:57.695 --> 00:07:59.615 We can just copy that and paste it. 147 00:07:59.615 --> 00:08:03.006 I could change the single quotes there to double quotes. 148 00:08:04.563 --> 00:08:08.914 Like so, and again if you run that, we get the same result, so 149 00:08:08.914 --> 00:08:13.210 that can be quite handy to look at using those triple quotes. 150 00:08:13.210 --> 00:08:17.290 The other thing I just wanna point out is in the case where you've got a quote and 151 00:08:17.290 --> 00:08:19.260 you're gonna go to have four quotes in a row. 152 00:08:19.260 --> 00:08:21.110 That doesn't work too well, and 153 00:08:21.110 --> 00:08:24.090 you actually find that Python gets itself into a little bit of a knot with that. 154 00:08:24.090 --> 00:08:27.760 So if you are using the three quotes, you need to actually have a space before 155 00:08:27.760 --> 00:08:29.500 to make sure you haven't got four quotes in a row, 156 00:08:29.500 --> 00:08:32.400 which actually [LAUGH] again you saw that Python doesn't handle too well. 157 00:08:32.400 --> 00:08:34.570 So that serves as a basic introduction. 158 00:08:34.570 --> 00:08:36.740 And look it's only a basic introduction. 159 00:08:36.740 --> 00:08:38.850 What we're gonna do next, is we're going to the next video. 160 00:08:38.850 --> 00:08:40.358 We're gonna start talking about variables. 161 00:08:40.358 --> 00:08:43.460 Because we sorta glossed over that a little bit when you talked about, 162 00:08:43.460 --> 00:08:44.828 very basically what they are. 163 00:08:44.828 --> 00:08:48.300 But we're gonna actually talk about that more in detail, what variables are, and 164 00:08:48.300 --> 00:08:48.840 how to use them. 165 00:08:48.840 --> 00:08:50.350 So we'll start that in the next video.