All language subtitles for python.en-fix

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish Download
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,000 --> 00:00:04,080 In this course, I'm going to teach you everything you need to know to get started programming 2 00:00:04,080 --> 00:00:10,560 in Python. Now, Python is one of the most popular programming languages out there. And it's by far 3 00:00:10,560 --> 00:00:15,760 one of the most sought after for jobs. And so if you're trying to get a job or you're trying to, 4 00:00:15,760 --> 00:00:19,920 you know, automate your life, or you're trying to write awesome scripts to do a bunch of different 5 00:00:19,920 --> 00:00:24,560 things, then Python's for you. Honestly, more and more developers every day are moving their 6 00:00:24,560 --> 00:00:30,000 projects over to Python because it's such a powerful and it's such an easy to use language. A lot 7 00:00:30,000 --> 00:00:34,640 of programming languages out there just aren't very beginner friendly. There's a lot of syntax. 8 00:00:34,640 --> 00:00:38,960 There's a lot of like little things that if you get wrong, the program will yell at you. Python 9 00:00:38,960 --> 00:00:43,760 is the complete opposite of that. You basically just type out what you want to do and Python does 10 00:00:43,760 --> 00:00:49,040 it. It's that simple. There's not a whole lot of syntax to learn. The learning curve is literally 11 00:00:49,040 --> 00:00:54,160 zero. You jump in, you can start writing your first program in seconds. In this course, I'm 12 00:00:54,160 --> 00:00:58,560 going to teach you guys everything you need to know to get started in Python. I designed this 13 00:00:58,560 --> 00:01:03,840 course, especially for Python. And each lesson has been specially designed with examples that 14 00:01:03,840 --> 00:01:07,920 will help you along the way with so many people starting to learn Python. The question isn't, 15 00:01:07,920 --> 00:01:12,560 why should you learn Python? The question is, why shouldn't you? And I think for a lot of people, 16 00:01:12,560 --> 00:01:16,640 the reason they might not want to learn Python is because they're intimidated or they're afraid 17 00:01:16,640 --> 00:01:21,200 that it's going to be too hard. Trust me, I am going to hold your hand through this entire course. 18 00:01:21,200 --> 00:01:25,600 We're going to talk about all the core concepts in Python. We're going to look at everything you 19 00:01:25,600 --> 00:01:30,400 need to know to start programming in Python and start being confident and start writing scripts 20 00:01:30,400 --> 00:01:34,720 and start writing programs that are awesome and doing cool things in your life. Anyway, 21 00:01:34,720 --> 00:01:38,880 I'm super pumped to be teaching you guys Python. I can't wait to get started in this course. And 22 00:01:38,880 --> 00:01:43,440 I hope you guys stick around and follow along with the course and learn this amazing programming 23 00:01:43,440 --> 00:01:52,480 language. In this tutorial, I'm going to show you guys how to install Python onto your computer. 24 00:01:52,480 --> 00:01:57,920 And we're also going to install a text editor that we can use to write our Python programs in. 25 00:01:58,480 --> 00:02:04,000 So the first order of business is to actually install Python on your computer. So what we want 26 00:02:04,000 --> 00:02:09,040 to do is head over to our web browser. And you want to go over here to this page, it's just 27 00:02:09,040 --> 00:02:17,840 www.python.org forward slash downloads. And on this page, there's going to be two buttons down here. 28 00:02:17,840 --> 00:02:23,440 And it's just going to say download Python 3.6.3. And at least that's the version that I have right 29 00:02:23,440 --> 00:02:30,720 now or download Python 2.7.1.4. So here's the thing about Python, there's actually two major versions 30 00:02:30,720 --> 00:02:37,680 of Python that are in use currently. There's Python two, and there's Python three. So you can see over 31 00:02:37,680 --> 00:02:45,760 here, this is like 2.7.1.4. Basically, any Python two version we would just refer to as like Python 32 00:02:45,760 --> 00:02:51,120 two. And here's like the latest Python three version. And so there's these two versions that you can 33 00:02:51,120 --> 00:02:55,760 download. And the first thing we have to decide is which version do we want to download. And here's 34 00:02:55,760 --> 00:03:02,080 the basic difference, Python two is a legacy version of Python, which basically means that 35 00:03:02,080 --> 00:03:08,160 it's like an old version, and it's not being like actively maintained or supported like officially 36 00:03:08,160 --> 00:03:14,000 by Python anymore. Python three is like the future of Python. It's like the newest version. It's the 37 00:03:14,000 --> 00:03:19,360 one that's getting actively maintained and supported. And so there's pros and cons to picking either 38 00:03:19,360 --> 00:03:25,600 one. A lot of people will say like Python two has been around longer. And so there's more like 39 00:03:25,600 --> 00:03:31,280 libraries, there's basically just more Python code written in Python two. So therefore, like 40 00:03:31,280 --> 00:03:35,760 there's a few more things that you can do in Python two, then you can do in Python three. But 41 00:03:35,760 --> 00:03:41,120 then again, Python two is not supported. And you know, five or 10 years from now, probably no one's 42 00:03:41,120 --> 00:03:45,840 going to be using it. Python three, like I said, is like the future of Python. It's the newest 43 00:03:45,840 --> 00:03:50,560 version. It's the one that, you know, is sort of being maintained going forward. So for the 44 00:03:50,560 --> 00:03:55,760 purposes of this tutorial, I'm going to be teaching you guys Python three. So we're going to want 45 00:03:55,760 --> 00:04:01,200 to download Python three. Now here's the thing, like, if you learn Python three, or you learn Python 46 00:04:01,200 --> 00:04:06,960 two, the differences aren't very big. There are a few differences in like some syntax and just some, 47 00:04:06,960 --> 00:04:11,120 you know, little things here and there. But if you learn Python three, you'll be able to jump right 48 00:04:11,120 --> 00:04:16,400 in and code in Python two as well. Don't let anyone, you know, tell you that you have to learn one 49 00:04:16,400 --> 00:04:20,800 or the other, we're going to learn Python three just because it's I think probably the best for a 50 00:04:20,800 --> 00:04:24,400 beginner to learn. And so that's the one that we want to download. So I'm just going to click 51 00:04:24,400 --> 00:04:29,920 download Python three. And we're going to go ahead and download this version. And when that's done 52 00:04:29,920 --> 00:04:34,880 downloading, I'm just going to head down to my downloads folder. And you'll see here, we just 53 00:04:34,880 --> 00:04:40,400 have this file, I'm just going to double click. And we get this like Python installer. So I'm just 54 00:04:40,400 --> 00:04:45,600 going to click through here. And we'll be able to install Python three on our computer. All right, 55 00:04:45,600 --> 00:04:49,840 when that's done installing, we can just close out of this and we should be good to go. So now we 56 00:04:49,840 --> 00:04:56,960 have Python three installed on our computer. The next step is we need to choose a text editor. So 57 00:04:57,760 --> 00:05:01,200 we're going to be writing a bunch of code. So we're going to need some, you know, 58 00:05:01,200 --> 00:05:06,320 environment or some program that we can write all that code in. And really, you can write Python 59 00:05:06,320 --> 00:05:11,200 in any text editor, you could write it in like notepad or text edit, it doesn't really matter. 60 00:05:11,200 --> 00:05:18,160 But there are special text editors that are designed just for writing Python code. And these are called 61 00:05:18,160 --> 00:05:23,520 IDEs. And that stands for integrated development environment. It's basically just a special 62 00:05:23,520 --> 00:05:28,240 environment where we can, you know, run and execute our Python code. And it'll, you know, 63 00:05:28,240 --> 00:05:32,400 basically, like tell us how we're doing. So if we're, you know, if we write something that's wrong, 64 00:05:32,400 --> 00:05:36,800 or we have errors, it'll kind of point us in the right direction of what we need to do to fix that 65 00:05:36,800 --> 00:05:41,680 stuff. So in this course, we're going to be using an IDE. And one of my favorites, and one of the 66 00:05:41,680 --> 00:05:47,600 most popular IDEs for Python is called pie charm. So I'm over here on this website, it's called 67 00:05:47,600 --> 00:05:54,160 jetbrains.com forward slash pie charm. And this is the IDE that we're going to be using in this 68 00:05:54,160 --> 00:05:58,880 course. So I'm just going to come over here and click this big download now button. And we'll go 69 00:05:58,880 --> 00:06:04,560 ahead and download this pie charm program. Alright, so I'm getting this page over here. You can see 70 00:06:04,560 --> 00:06:08,960 there's two versions of pie charm. One is a professional version, and that one's like paid, 71 00:06:08,960 --> 00:06:12,480 so you have to pay for it. But then there's this community version down here, and that's 72 00:06:12,480 --> 00:06:16,720 free and open source. So I'm just going to download this one. And this should have everything we need 73 00:06:16,720 --> 00:06:22,000 to get started using Python. Alright, when that finishes downloading, I'm just going to pull that 74 00:06:22,000 --> 00:06:28,080 up in my downloads folder. And again, we can just sort of run this guy. And I'm on a Mac. So I'm 75 00:06:28,080 --> 00:06:33,360 going to have to drag it over to my applications folder. Alright, so we now have pie charm. And we 76 00:06:33,360 --> 00:06:39,680 have Python three installed on our computer. So we're ready to start programming in Python. 77 00:06:39,680 --> 00:06:48,480 And this tutorial, we're going to create our first Python program. And we're going to be able to run 78 00:06:48,480 --> 00:06:54,400 it and see how everything works. The first thing I want to do is open up pie charm. So pie charm was 79 00:06:54,400 --> 00:07:00,640 the IDE, the integrated development environment that we downloaded in the last tutorial. So I'm 80 00:07:00,640 --> 00:07:05,040 just going to go ahead and search for it on my computer. And I can just open it up. And when we 81 00:07:05,040 --> 00:07:10,800 first open up pie charm, we should basically just get a window prompting us to create a project. So 82 00:07:10,800 --> 00:07:14,960 you can see that down here. Actually, the first thing I want to do though is come over here to 83 00:07:14,960 --> 00:07:22,560 configure. And I'm just going to click on preferences. And I actually want to change the appearance. So 84 00:07:23,200 --> 00:07:28,000 I'm going to go ahead and give this a theme. So I just went over here to appearance and behavior, 85 00:07:28,000 --> 00:07:32,720 appearance. And I'm going to change the theme to dark. Just because I think it's a little bit 86 00:07:32,720 --> 00:07:38,640 easier to use and look at. But you don't have to do that if you don't want to. And so down here, 87 00:07:38,640 --> 00:07:43,600 I'm just going to click create new project. And it's going to give us this little window right here. 88 00:07:43,600 --> 00:07:49,440 So I can just name my project. And I'm just going to name this draft. And then there's this other 89 00:07:49,440 --> 00:07:57,120 option down here where it says interpreter. Now, what we want to select is Python version three. 90 00:07:57,120 --> 00:08:02,240 So if I click down here, you'll notice that there's a couple different options. So I'm on a Mac 91 00:08:02,240 --> 00:08:09,600 computer. And by default, Mac has Python version two installed on it. If you're on a Windows machine, 92 00:08:09,600 --> 00:08:14,880 I'm not sure if it does. But in this tutorial, we're going to be using Python three. So you want 93 00:08:14,880 --> 00:08:20,640 to make sure that you have Python three selected as the interpreter. Otherwise, you might not be 94 00:08:20,640 --> 00:08:24,240 able to fully follow along with what we're doing in this video. So now I'm just going to come down 95 00:08:24,240 --> 00:08:29,760 here and click create. And this is going to go ahead and create our project for us. All right, 96 00:08:29,760 --> 00:08:35,440 so now we have our Python project up and running. And I want to show you guys how we can create our 97 00:08:35,440 --> 00:08:41,760 first Python program. So over here in this draft folder, and this is just that project that we 98 00:08:41,760 --> 00:08:46,480 created, I'm just going to right click and I'm going to say new. And I'm going to come down and 99 00:08:46,480 --> 00:08:51,440 click Python file. So we want to create a new Python file, this will be our first Python program. 100 00:08:52,080 --> 00:08:56,320 And I can just give this a name. So why don't we just call this app? And I'm just going to click 101 00:08:56,320 --> 00:09:02,800 okay. All right, so now we have our first Python file open. And we can just start typing in some 102 00:09:02,800 --> 00:09:08,000 Python. So I want to show you guys just a very basic Python program. And this is just going to be 103 00:09:08,000 --> 00:09:12,480 a hello world program. So we're just going to print something out onto the screen. So what I 104 00:09:12,480 --> 00:09:18,960 want to do is I want to type out PRINT print. And I want to type in open and closed parentheses. 105 00:09:18,960 --> 00:09:24,000 Now inside of this parentheses, we can make some quotation marks. And inside of the quotation 106 00:09:24,000 --> 00:09:30,160 marks, I'm just going to type out hello world. So this is like a very basic Python program. And 107 00:09:30,160 --> 00:09:35,600 what this is going to do is it's just going to print out hello world onto the screen. And so now 108 00:09:35,600 --> 00:09:39,440 what I can do is I can actually run this file and we're actually getting a little error here. 109 00:09:39,440 --> 00:09:45,280 It wants us to create a new line at the end of the file. And so now in order to run our Python 110 00:09:45,280 --> 00:09:49,760 program, I'm just going to come up here to the top. And I'm just going to click run. And we'll 111 00:09:49,760 --> 00:09:54,560 click this run button right here. And now we'll see what happens. So it's asking me what I want 112 00:09:54,560 --> 00:10:00,480 to run. I can just click app. And down here at the bottom, you'll notice that we have this text 113 00:10:00,480 --> 00:10:06,080 that just got printed out hello world. And so whatever I print, or whatever I put inside of 114 00:10:06,080 --> 00:10:10,960 this print statement is going to get printed out down below in the console. So as long as you're 115 00:10:10,960 --> 00:10:15,760 getting that print statement to work, then you've actually written your first Python program. And 116 00:10:15,760 --> 00:10:19,520 as we go through forward in the course, we're going to be writing all sorts of awesome Python 117 00:10:19,520 --> 00:10:22,320 programs. And we're going to be doing all sorts of cool stuff. 118 00:10:26,000 --> 00:10:30,880 In this tutorial, I want to talk to you about writing a basic Python program. So we're going to 119 00:10:30,880 --> 00:10:35,360 write just a very simple Python program. We're going to look at how we can go ahead and write 120 00:10:35,360 --> 00:10:40,480 our programs, we're going to talk about how our programs are getting executed by Python. 121 00:10:40,480 --> 00:10:44,880 And we're also going to draw out a little shape onto the screen. So this is going to be pretty 122 00:10:44,880 --> 00:10:50,160 cool. I'm excited to talk to you guys about just the bare basics of Python, and really just sort of 123 00:10:50,160 --> 00:10:57,600 give you an introduction. So over here, we have our basic text editor. And so any of the Python code 124 00:10:57,600 --> 00:11:04,480 that I write inside of this file is actually going to get executed by Python. So this Python file 125 00:11:04,480 --> 00:11:10,000 has a bunch of Python code inside of it. And basically what we're doing when we're programming 126 00:11:10,000 --> 00:11:14,640 in Python is we're just giving the computer a set of instructions. Right, that's really what 127 00:11:14,640 --> 00:11:19,360 programming is, it's just like we're specifying a set of instructions. And the computer is going 128 00:11:19,360 --> 00:11:24,880 to go off and execute those instructions that we give it. And so the more complex the instructions 129 00:11:24,880 --> 00:11:30,480 we give to the computer, the more complex the tasks the computer can carry out. So I want to 130 00:11:30,480 --> 00:11:36,560 show you guys how we can use some Python instructions or some Python code in order to draw out a shape 131 00:11:36,560 --> 00:11:42,160 onto the screen. So I'm actually going to draw out a little triangle. So over here, we can actually 132 00:11:42,160 --> 00:11:47,360 print something out onto the screen and Python using something called print statements. So I can 133 00:11:47,360 --> 00:11:54,080 just type out print. And inside of these parentheses, I can just type some quotation marks and inside 134 00:11:54,080 --> 00:11:58,960 the quotation marks, we can put whatever we want to print out onto the screen. So I'm going to 135 00:11:58,960 --> 00:12:03,200 copy this and I'm actually going to make a few of these little print statements. And we're going 136 00:12:03,200 --> 00:12:08,640 to actually draw a shape. So we're going to draw like a triangle type shape. So down here, I'm just 137 00:12:08,640 --> 00:12:13,680 going to draw a forward slash. And then up here, I'll make a space and we'll do another forward slash. 138 00:12:14,240 --> 00:12:19,040 Here, we'll make two spaces, we'll do another forward slash. And over here, we're going to make 139 00:12:19,040 --> 00:12:24,480 three spaces and a forward slash. And now I'm going to draw like vertical bars going all the way down. 140 00:12:27,280 --> 00:12:30,400 And actually, we can do some underscores here as well. That'd be kind of cool. 141 00:12:30,400 --> 00:12:36,320 All right, so now we have our triangle looking shape and you can see it looks kind of like a 142 00:12:36,320 --> 00:12:43,440 right triangle. So I can save my file. And I can either come up here to run and click run app. 143 00:12:43,440 --> 00:12:47,120 Or if you're using PyCharm, you can also just come over here and click this play button. 144 00:12:47,760 --> 00:12:52,800 And when I click the play button, I want you to notice what happens down here. So down here, 145 00:12:52,800 --> 00:12:58,320 we have this little window and this is called the console. And basically, the console is just a 146 00:12:58,320 --> 00:13:03,840 place where Python is going to output some information. So when we use this print statement, 147 00:13:03,840 --> 00:13:08,800 we're basically telling Python like, Hey, can you print something out onto the console? So there's a 148 00:13:08,800 --> 00:13:13,440 lot of situations where we're going to want to see what's going on in our code. And we can use 149 00:13:13,440 --> 00:13:18,400 that print statement and print things out to the console. And we can basically like have a little 150 00:13:18,400 --> 00:13:22,800 window into what our program is doing. In our case, we're just going to be printing out this 151 00:13:22,800 --> 00:13:27,840 triangle onto the console. So I'm going to come over here and click this play button. And you'll 152 00:13:27,840 --> 00:13:33,040 see down here, we're actually printing out that triangle. So mission accomplished, we were able to 153 00:13:33,040 --> 00:13:38,880 print out our shape. So I could really draw any shape I wanted as long as I was able to specify 154 00:13:38,880 --> 00:13:44,960 it inside of these print statements. So essentially, what Python's doing when I click that play button, 155 00:13:44,960 --> 00:13:50,160 is it's going into this file and it's looking at all of these instructions in order. So the first 156 00:13:50,160 --> 00:13:54,880 thing it does is it goes to this instruction, and it says, okay, they want me to draw out these 157 00:13:54,880 --> 00:13:59,760 like little forward slash in this vertical bar onto the screen. Once it does that, it goes down 158 00:13:59,760 --> 00:14:03,840 to this instruction, it says, okay, they want me to print something out again. When it prints that 159 00:14:03,840 --> 00:14:10,000 out, it goes down to this instruction, etc. So Python is actually going to execute the lines of 160 00:14:10,000 --> 00:14:16,320 code that we write in order. So the order in which I write the instructions matters a lot. So for 161 00:14:16,320 --> 00:14:23,600 example, if I was to take this last line here, and put it up here at the top. Now when I save the file, 162 00:14:23,600 --> 00:14:28,960 instead of just drawing out that triangle, it's going to draw out this little upside down bottom 163 00:14:28,960 --> 00:14:34,080 thing at the top, and then the rest of the triangle down here. So the order of the instructions 164 00:14:34,080 --> 00:14:39,520 actually matters a lot. So this is basically how your Python programs are going to go. As we go 165 00:14:39,520 --> 00:14:44,000 through this course, we're going to learn more and more of these little lines. So here, we're 166 00:14:44,000 --> 00:14:48,560 just using this little print function, basically, it just prints something out onto the screen. There's 167 00:14:48,560 --> 00:14:53,040 a bunch of little things like this that we can learn as we go through, we'll learn more and more 168 00:14:53,040 --> 00:14:57,680 instructions that we can give to the computer. But in this lesson, I just want to kind of show you 169 00:14:57,680 --> 00:15:04,000 guys the basics, right? We can essentially just define these little instructions for the computer, 170 00:15:04,000 --> 00:15:12,800 and the computer will execute them in order. In this tutorial, I want to talk to you guys about 171 00:15:12,800 --> 00:15:19,840 using variables in Python. Now in Python, you're going to be dealing with a lot of data. So a lot 172 00:15:19,840 --> 00:15:24,960 of times in our program, we're going to be working with all types of information and data and values. 173 00:15:25,520 --> 00:15:32,560 And sometimes that data can be difficult to manage. So in Python, we have this thing called a variable. 174 00:15:32,560 --> 00:15:38,320 And it's basically just a container, where we can store certain data values. And when we use a 175 00:15:38,320 --> 00:15:44,080 variable, when we put those data values inside containers, it makes it a lot easier for us to 176 00:15:44,080 --> 00:15:50,560 work with and manage all of the different data inside of our programs. So variables are extremely 177 00:15:50,560 --> 00:15:55,360 helpful. And it's sort of like a core topic in Python that you're really going to want to know 178 00:15:55,360 --> 00:16:00,160 in master. So in this tutorial, I'm going to teach you guys everything you need to know to get started 179 00:16:00,160 --> 00:16:04,320 with variables, we're going to look at what they are, why they're useful, and we'll look at the 180 00:16:04,320 --> 00:16:09,840 different types of variables and the different types of data that we can store inside of variables. 181 00:16:09,840 --> 00:16:15,040 But over here in my Python file, I just have a very simple program. And it's basically just 182 00:16:15,040 --> 00:16:20,000 printing some text out onto the screen. And so this is actually a little story that I wrote. It 183 00:16:20,000 --> 00:16:26,720 says there once was a man named George, he was 70 years old. He really liked the name George, 184 00:16:26,720 --> 00:16:33,360 but didn't like being 70. So this is a valid Python program. I could go ahead and run this program. 185 00:16:33,360 --> 00:16:38,240 And you'll see down here in the console, everything prints out and it looks great. So 186 00:16:38,240 --> 00:16:43,600 we have an awesome little Python program here. But let's say that inside of our story, I wanted 187 00:16:43,600 --> 00:16:48,560 to change the character's name. So instead of naming the character George, let's say I wanted 188 00:16:48,560 --> 00:16:54,000 to name the character John. Well, I'm going to have to go through and I'm going to have to manually 189 00:16:54,000 --> 00:16:59,520 change the name George to the name John at every place inside of this story where it's mentioned. 190 00:16:59,520 --> 00:17:03,680 So I'm going to have to go over here and I'll say, okay, John, I'm going to have to manually 191 00:17:03,680 --> 00:17:08,880 change that. And then, okay, down here, we're gonna have to again, manually type in John and 192 00:17:08,880 --> 00:17:13,760 change it to John. So in order to change the character's name, I had to manually go in and 193 00:17:13,760 --> 00:17:20,240 change it in both places. And now let's say, okay, maybe we want to change the character's name age 194 00:17:20,240 --> 00:17:24,320 also. So in addition to changing the name, we also want to change the age, let's make 195 00:17:24,320 --> 00:17:30,320 them a little bit younger. So why don't we say John is going to be 35. And so you'll see here, 196 00:17:30,320 --> 00:17:36,160 I had to come here and manually change the age and then come down here and manually change it 197 00:17:36,160 --> 00:17:40,800 again. Right. And now that we changed it, like it's going to work, it's going to be updated in 198 00:17:40,800 --> 00:17:45,920 our program. But you'll notice that in order to make that change, like I had to look through the 199 00:17:45,920 --> 00:17:52,480 entire program, find it where that value was and change it. And this was only with four lines of 200 00:17:52,480 --> 00:17:58,000 the story. Imagine if I had a story that was like, thousands of lines long. And we mentioned the 201 00:17:58,000 --> 00:18:02,880 character's name and age, like hundreds of times. I mean, I'd have to look through each one of those 202 00:18:02,880 --> 00:18:09,360 lines and manually change the character's name. And that is not a very good way for us to do this. 203 00:18:09,360 --> 00:18:14,480 And it's really not a good way for us to manage the data in our program, right, the character's 204 00:18:14,480 --> 00:18:20,800 name and the age. And so we can actually use a variable in order to store the character's name 205 00:18:20,800 --> 00:18:25,920 and the character's age. And when we use that variable, it'll make it a lot easier for us to 206 00:18:25,920 --> 00:18:30,800 put the character's name and age inside of our little program here. So I'm going to show you guys 207 00:18:30,800 --> 00:18:35,120 how we can create a variable for the character's name and age. And I'm just going to go up here 208 00:18:35,120 --> 00:18:42,160 above this print statement. And up here, I want to create a variable. So when we create a variable 209 00:18:42,160 --> 00:18:48,240 in Python, we actually need to give Python a couple pieces of information. The first piece of 210 00:18:48,240 --> 00:18:54,000 information we need to give Python is the name of the variable. So I need to actually assign a name 211 00:18:54,000 --> 00:18:59,120 to this container where we're going to be storing information. And so all I have to do is just type 212 00:18:59,120 --> 00:19:03,760 out the name of the variable that I want to create. So I'm going to create a variable called 213 00:19:03,760 --> 00:19:10,960 character name. So I'm just going to say character. And I'm going to say underscore name. And so 214 00:19:10,960 --> 00:19:16,320 generally, when you're creating a name for a variable in Python, you want to separate different 215 00:19:16,320 --> 00:19:22,160 words with an underscore. So I have two words here, and I'm separating them with an underscore. 216 00:19:22,160 --> 00:19:27,680 And now what I need to do is I need to put a value inside of this variable. So basically, 217 00:19:27,680 --> 00:19:33,120 what I can do is I can just say equals, and over here, I can type a value. So I'm just going to type 218 00:19:33,120 --> 00:19:37,440 out the character's name inside of quotation marks. So we're just going to type out john. 219 00:19:38,080 --> 00:19:44,160 So now we have a variable for the character's name. And below this character name variable, 220 00:19:44,160 --> 00:19:50,560 I want to create another variable called character age. So we're just going to type out character 221 00:19:50,560 --> 00:19:56,720 underscore age. And I'm going to set this equal to the character's age, which is 35. 222 00:19:58,000 --> 00:20:02,480 So we now have two variables, one representing the character's name and one representing the 223 00:20:02,480 --> 00:20:08,560 character's age. So what I can do now is I can replace the character's name inside of the story 224 00:20:08,560 --> 00:20:13,680 with this variable. And I'm going to show you guys how to do that. So if we want to put a variable 225 00:20:13,680 --> 00:20:18,080 inside of this print statement, I'm actually going to have to do a couple of things. The first thing 226 00:20:18,080 --> 00:20:25,200 I'm going to have to do is end off this text in here. So I'm going to have to put a quotation marks 227 00:20:25,200 --> 00:20:31,200 here at the end. And you can see I'm basically wrapping this whole thing into a single like quoted 228 00:20:31,200 --> 00:20:36,000 line. And now I also have this text over here at the end. So I'm going to have to put a quotation 229 00:20:36,000 --> 00:20:42,400 mark here. So now I have a bunch of text here in quotation marks. And then I also have text over 230 00:20:42,400 --> 00:20:48,320 here in quotation marks. Now inside of here, instead of saying the character's name, I want to refer 231 00:20:48,320 --> 00:20:57,040 to that character name variable. So I'm just going to say plus character underscore name. 232 00:20:57,760 --> 00:21:02,880 And I want to say another plus sign. So I basically am saying I want to print out all of this text, 233 00:21:03,600 --> 00:21:10,880 plus the value that's stored inside of character name, plus all of this text. And what this will 234 00:21:10,880 --> 00:21:15,200 do is actually print out the character's name. We print this. So I'm going to go ahead and run 235 00:21:15,200 --> 00:21:21,120 this program. And you'll see down here, and actually let me put a space over here. And we'll run it 236 00:21:21,120 --> 00:21:26,640 again. So you can see down here, we still are printing out there once was a man named John. 237 00:21:27,280 --> 00:21:32,880 And so what Python is doing is when it executes this line of code, it's coming over here and it's 238 00:21:32,880 --> 00:21:38,640 seeing, okay, they want to print out the value inside of character name. So it Python is going 239 00:21:38,640 --> 00:21:44,160 to go up here, it's going to get the value. And it's going to insert it inside of there. So I can 240 00:21:44,160 --> 00:21:49,200 basically do this for every instance of the character name in my file. So I can, you know, 241 00:21:49,200 --> 00:21:55,760 I'll just copy this guy right here. And I can just paste it down here where the character's name is. 242 00:21:55,760 --> 00:22:01,120 So I'll paste this. And you can see now we're doing exactly the same thing that we did up there. 243 00:22:01,120 --> 00:22:06,720 I can also do this for the character's age. So I'm going to come down here and I'll paste in what 244 00:22:06,720 --> 00:22:12,240 we just paste it in. And I'm going to change this instead of character name, we'll change it to character 245 00:22:12,240 --> 00:22:18,000 age. And I'm going to do this in one more spot. So we're going to do this over here where we're 246 00:22:18,000 --> 00:22:24,960 mentioning the age. And again, I'll just change this to character age. And so now when I run this 247 00:22:24,960 --> 00:22:30,800 program, it's going to print out the same story that we were printing out before. But now I don't 248 00:22:30,800 --> 00:22:36,320 actually have to type in the character's name and the character's age. I can just refer to this 249 00:22:36,320 --> 00:22:41,120 variable. And the cool thing about variables is if I wanted to change the character's name, 250 00:22:41,920 --> 00:22:46,640 now all I have to do is change it up here. So I could change the character's name to like Tom 251 00:22:46,640 --> 00:22:51,920 or something. And we could also change their age. So I could change the age to be like 50. 252 00:22:51,920 --> 00:22:56,720 And now that's going to automatically update throughout our entire story. So when I click this 253 00:22:56,720 --> 00:23:03,760 play button, you'll see now we're using the name Tom. And he's 50 years old. So that's where variables 254 00:23:03,760 --> 00:23:10,080 can come in handy. And this can be a really awesome way to control and manage the data that's inside 255 00:23:10,080 --> 00:23:16,560 of our programs. With variables, you can also modify their values. So for example, let's say that 256 00:23:16,560 --> 00:23:20,960 halfway through the story, I wanted to change the character's name. Well, I can just make some new 257 00:23:20,960 --> 00:23:27,360 lines over here. And down here, I can actually assign a new value to one of these variables. 258 00:23:27,360 --> 00:23:33,520 So I could say like character underscore name. And in order to give it a different value, 259 00:23:33,520 --> 00:23:38,320 all I have to do is just say equals, and we'll give it a different value. So let's say halfway 260 00:23:38,320 --> 00:23:43,680 through the story, we want to change the character's name to Mike. Well, now when I run on my program, 261 00:23:44,480 --> 00:23:49,600 you'll see here in the first part of the story, it says there once was a man named Tom. And in 262 00:23:49,600 --> 00:23:55,840 the second part of the story, it's referring to the name as Mike. So I actually updated the 263 00:23:55,840 --> 00:24:01,680 variable inside of my program over here. And that's a really awesome way. You know, a lot of times 264 00:24:01,680 --> 00:24:06,240 in these programs, you're going to want to be modifying the values of variables. So you can do 265 00:24:06,240 --> 00:24:11,840 it just like that. So finally, I want to talk to you guys about the different types of data that we 266 00:24:11,840 --> 00:24:18,240 can store inside of these variables. So over here, I have the character name and the character age. 267 00:24:18,240 --> 00:24:25,440 And I'm actually storing what are called strings. So a string is basically just plain text, 268 00:24:25,440 --> 00:24:31,920 right? So the name Tom, that's just plain text. Down here, these guys are strings as well. So 269 00:24:31,920 --> 00:24:39,440 there's a data type in Python called a string, and it's just basically going to store plain text. So, 270 00:24:39,440 --> 00:24:44,080 you know, any text that you would have like in a story, or you know, someone's name, you can store 271 00:24:44,080 --> 00:24:49,840 inside of a string. There's also another type of data that we can store inside of Python, 272 00:24:49,840 --> 00:24:55,840 which is numbers. So in addition to storing text, we can also store numbers. So if I wanted to store 273 00:24:55,840 --> 00:25:00,880 a number, for example, let's say we want to store the character age instead of inside of a string, 274 00:25:00,880 --> 00:25:07,840 we could store it inside of a number. So I could just type out 50. And when we're storing a number, 275 00:25:07,840 --> 00:25:12,880 we don't need these quotation marks, you only need that quotation mark when you're storing a 276 00:25:12,880 --> 00:25:17,360 string. So if I want to store a number, I can just write it out like this. And in addition to 277 00:25:17,360 --> 00:25:24,640 using whole numbers, I could also use decimal numbers. So I could say like 50.5678213. And 278 00:25:24,640 --> 00:25:29,280 Python's going to be able to store that number as well. So you can store all different types of 279 00:25:29,280 --> 00:25:36,560 numbers. And we can also store what's called a Boolean value. And a Boolean value is essentially 280 00:25:36,560 --> 00:25:42,240 a true or a false value. And there's a lot of instances in programming where we're going to 281 00:25:42,240 --> 00:25:49,360 want to represent true or false data. For example, I could have a variable called like is mail. And 282 00:25:49,360 --> 00:25:54,320 this would tell me whether or not someone was a male. And in my case, it would be true because 283 00:25:54,320 --> 00:26:01,120 I'm a guy, right? So I can actually this needs to be capital. So I could sort of value of either 284 00:26:01,120 --> 00:26:08,400 true or false. And actually, if we were naming this in Python, we want to use an underscore. So I 285 00:26:08,400 --> 00:26:14,640 would say is underscore mail. And this can be either true or false. So true or false values is 286 00:26:14,640 --> 00:26:19,680 maybe not something that you're used to dealing with like in the real world. But in programming, 287 00:26:19,680 --> 00:26:25,520 we're going to be using true or false values all the time. They're super important. So like I 288 00:26:25,520 --> 00:26:30,320 said, there's three basic types of data that we can work with in Python. There's strings, 289 00:26:30,320 --> 00:26:35,440 which is just like plain text numbers, which would be like either decimal numbers or whole 290 00:26:35,440 --> 00:26:41,360 numbers like this. And there's true or false values. And these are the basic types of data. I mean, 291 00:26:41,360 --> 00:26:46,880 there's a bunch of other types of data that we can use. But I would say 99% of the time as a new 292 00:26:46,880 --> 00:26:52,640 user and a new programmer to Python, you're just going to be dealing with these three types of data. 293 00:26:52,640 --> 00:26:57,920 So those are the basics of the data and also with variables. And you're going to be using 294 00:26:57,920 --> 00:27:06,560 variables all the time in Python. So you definitely want to practice up and get comfortable using them. 295 00:27:06,560 --> 00:27:12,080 In this tutorial, I want to talk to you guys about working with strings in Python. Now one of 296 00:27:12,080 --> 00:27:17,280 the most common types of data that we're going to be working with in Python is going to be strings. 297 00:27:17,280 --> 00:27:23,520 And strings are basically just plain text. So any text that we want to have inside of our program, 298 00:27:23,520 --> 00:27:28,400 we can store inside of a string. So I want to talk to you guys about all the cool things we can do 299 00:27:28,400 --> 00:27:32,880 with strings. And we're basically just going to get a full introduction into why strings are 300 00:27:32,880 --> 00:27:38,800 awesome. So over here, I'm just going to actually print out a string. So I'm just going to say print 301 00:27:39,440 --> 00:27:45,040 and inside of these parentheses, I can type out a string. In order to create a string, I need to 302 00:27:45,040 --> 00:27:50,880 use quotation marks. So I can make an open and close quotation marks just like that. Now inside 303 00:27:50,880 --> 00:27:55,360 of the quotation mark, I can put whatever text I want the string to have. So we could say like, 304 00:27:56,000 --> 00:28:03,200 draft Academy. And so now we have a string with the text draft Academy inside of it. So if I run 305 00:28:03,200 --> 00:28:08,640 my program, now down here, we're going to print out draft Academy, as you can see. And when we have 306 00:28:08,640 --> 00:28:12,960 these strings, there's actually a bunch of cool things that we can do with them. So one thing I 307 00:28:12,960 --> 00:28:18,640 could do would be to create a new line inside of the string. So if I want it, I can come over here, 308 00:28:18,640 --> 00:28:24,400 and I could say backslash n. And you can see it got highlighted in a different color. And what this 309 00:28:24,400 --> 00:28:28,960 is going to do is it's actually going to insert a new line into the string. So now it's going to say 310 00:28:28,960 --> 00:28:35,200 draft on one line and Academy on another line. So I'm going to click play. And you'll see down 311 00:28:35,200 --> 00:28:42,080 here, we just get draft Academy. In addition to the backslash n, I could also use a backslash 312 00:28:42,080 --> 00:28:48,000 quotation mark. So if I wanted to put a quotation mark inside of my string, I can't just put a 313 00:28:48,000 --> 00:28:53,760 quotation mark like that, because Python is going to think that I'm trying to end this string. So if 314 00:28:53,760 --> 00:28:58,240 I want to include a quotation mark, I can just use this special backslash character. And that's 315 00:28:58,240 --> 00:29:03,760 called the escape character. And it basically just tells Python that whatever character comes 316 00:29:03,760 --> 00:29:10,160 after it, we want to render literally. So when I say backslash quotation mark, it basically means 317 00:29:10,160 --> 00:29:15,120 like, Hey, Python, I want to print out a quotation mark. And so now we'll be able to print out a 318 00:29:15,120 --> 00:29:19,760 quotation mark right there. I can also use this to print out a backslash. So if I needed to print 319 00:29:19,760 --> 00:29:26,240 out a backslash, I could say backslash. And it'll just print out a normal backslash now. So you can 320 00:29:26,240 --> 00:29:30,800 see just like that. So if you want, you can use that backslash to make new lines or print out 321 00:29:30,800 --> 00:29:37,280 quotation marks, or you can just use it as a normal backslash. In addition to just typing out a string 322 00:29:37,280 --> 00:29:42,880 here, I could also create a string variable. So I could come down here up here, and we can create 323 00:29:42,880 --> 00:29:49,840 a variable and we'll just call it phrase. And I'm going to set it equal to draft academy. So I can 324 00:29:49,840 --> 00:29:55,840 store this string value inside of a variable called phrase. And then when I want to print out that 325 00:29:55,840 --> 00:30:02,480 variable, or I want to access that string variable, I can just type the name of the variable. And 326 00:30:02,480 --> 00:30:07,440 you'll see that it's going to print out the value that was stored inside of it. I can also use 327 00:30:07,440 --> 00:30:13,520 something called concatenation. And concatenation is basically the process of taking a string and 328 00:30:13,520 --> 00:30:18,880 appending another string onto it. So I could come over here and I could say phrase, and I can say 329 00:30:18,880 --> 00:30:25,680 plus. And now I can add in another string. So I could say like, is cool. And now this is going to say 330 00:30:25,680 --> 00:30:32,400 draft academy is cool. So I'm basically appending another string onto another one, they call that 331 00:30:32,400 --> 00:30:38,960 concatenation. And so in addition to doing all that stuff, we can also use special things called 332 00:30:38,960 --> 00:30:44,480 functions. And a function is basically just a little block of code that we can run. And it will 333 00:30:44,480 --> 00:30:51,120 perform a specific operation for us. So we can use functions to modify our strings. And we can 334 00:30:51,120 --> 00:30:57,040 also use functions to get information about our strings. So I'm going to show you guys a couple 335 00:30:57,040 --> 00:31:02,320 common functions we can use with these strings. And they're actually going to do awesome stuff. So 336 00:31:02,320 --> 00:31:07,440 in order to access one of these functions, I can just say phrase, and then I can say dot. And I'm 337 00:31:07,440 --> 00:31:12,640 going to show you guys a function that we can use to convert this string entirely into lower case. 338 00:31:12,640 --> 00:31:17,920 So I could just say phrase dot lower. And I'm going to want to type an open and close parentheses. 339 00:31:17,920 --> 00:31:22,880 And this will take this phrase, it'll take my string and it'll convert it to lowercase. So you 340 00:31:22,880 --> 00:31:27,840 can see now we just have draft academy down here, but it's entirely in lowercase. I could also do 341 00:31:27,840 --> 00:31:34,080 the same thing for uppercase. So I could say phrase dot upper. And this will convert the entire string 342 00:31:34,080 --> 00:31:39,760 into uppercase. You can see now it's entirely uppercase. So in addition to converting the string 343 00:31:39,760 --> 00:31:45,040 into uppercase and lowercase, I could also check to see if a string is entirely uppercase or 344 00:31:45,040 --> 00:31:51,280 entirely lowercase. So for example, I could say phrase dot is upper. And this is going to give me 345 00:31:51,280 --> 00:31:56,960 back a true or a false value. So it's going to be true. If the string is entirely uppercase, 346 00:31:56,960 --> 00:32:02,400 or false, if it's not. And you can see here, we're getting a false value because this is an 347 00:32:02,400 --> 00:32:08,800 uppercase. I can also use these functions in combination with each other. So for example, 348 00:32:08,800 --> 00:32:17,680 I could say phrase dot upper, and then parentheses. And then after this, I could say dot is upper. 349 00:32:17,680 --> 00:32:22,400 And now what it's going to do is it's going to run this upper function, it's going to convert it 350 00:32:22,400 --> 00:32:27,760 into uppercase. And then it's going to run this is upper function right after that. And you'll see 351 00:32:27,760 --> 00:32:33,120 now we're going to get a true value back because it will have converted the whole thing into uppercase. 352 00:32:33,120 --> 00:32:38,480 So you can see now we're getting a true value. So you can use these functions one after another, 353 00:32:38,480 --> 00:32:43,360 it can be really useful. And there's a few other ones that I want to show you. So we can also 354 00:32:43,360 --> 00:32:49,200 figure out the length of this string. So if I wanted to figure out how many characters were 355 00:32:49,200 --> 00:32:54,960 inside of this string, I could just say LEN. And I'm actually going to make an open parentheses. 356 00:32:54,960 --> 00:33:00,080 And I'm going to make a closed parentheses. So I'm essentially saying LEN. And this is another 357 00:33:00,080 --> 00:33:05,680 function, it's the length function. And inside of this LEN function, I'm actually passing this 358 00:33:06,320 --> 00:33:11,120 phrase variable, I'm basically giving the length function this, and it's going to spit out a number. 359 00:33:11,120 --> 00:33:16,960 So it'll tell me how many characters are inside of this string. And so you can see here we get 15 360 00:33:16,960 --> 00:33:22,240 because there's 15 characters inside of draft academy. So that's how we can get the length of 361 00:33:22,240 --> 00:33:26,960 a string. And that's going to be really useful as we go forward and Python, we can also get 362 00:33:26,960 --> 00:33:32,560 individual characters inside of a string. So imagine if I wanted to just grab one of these 363 00:33:32,560 --> 00:33:37,520 characters, like imagine if I wanted to figure out what the first character in this string is, 364 00:33:37,520 --> 00:33:43,360 we can actually use an open and closed square bracket, just like that. And in here, I can 365 00:33:43,360 --> 00:33:49,040 specify the index of the character that I want to grab. So if I wanted to grab the first character 366 00:33:49,040 --> 00:33:55,360 in this string, I can put in a zero. So if I say phrase square bracket zero, this is going to give 367 00:33:55,360 --> 00:34:01,040 me that capital G. And you can see down here, that's what gets printed out. Now I want to point 368 00:34:01,040 --> 00:34:08,560 something out in Python, when we're working with strings, a string gets indexed starting with zero. 369 00:34:08,560 --> 00:34:14,320 So you'll notice that in order to access this G, I had to put a zero in here. And that's because 370 00:34:14,320 --> 00:34:20,880 in Python, when we use indexes on a string, we start with zero. So if Python is counting the 371 00:34:20,880 --> 00:34:25,120 characters, or it's indexing the characters in a string, it's going to start with zero. So it's 372 00:34:25,120 --> 00:34:33,040 going to say G is zero, i is one, r is two, a is three. So we would say that G is at position zero 373 00:34:33,040 --> 00:34:39,680 in the string, i is at position one, r is at position two, a is at position three, etc. So we start 374 00:34:39,680 --> 00:34:48,000 counting at zero. So if I wanted to access the first character in the string, this G, I have to say 375 00:34:48,000 --> 00:34:53,840 phrase zero, and that's just a special thing in Python, and actually most programming languages 376 00:34:53,840 --> 00:34:59,360 do that. So they'll start with zero. So for example, if I wanted to access this a, I'm going to have 377 00:34:59,360 --> 00:35:09,200 to put zero 123. So if I put three inside of here, now we'll be able to access that first a, 378 00:35:09,200 --> 00:35:14,720 as you can see, we get an a right here. So this is actually really useful being able to grab a 379 00:35:14,720 --> 00:35:19,120 specific character inside of a string, and you're going to be using that a lot as we go forward 380 00:35:19,120 --> 00:35:24,240 in Python, there's also another really awesome function that we can use and it's called the index 381 00:35:24,240 --> 00:35:31,280 function. And basically what the index function will do is it'll tell us where a specific character 382 00:35:31,280 --> 00:35:38,800 or string is located inside of our string. So I could say phrase dot index. And I can actually 383 00:35:38,800 --> 00:35:44,080 give this a value. So sometimes when we use these functions in Python, we can actually give them 384 00:35:44,080 --> 00:35:50,560 information. So I can give this a value. And we would call this passing a parameter. So I would 385 00:35:50,560 --> 00:35:56,480 call a value that I would give to a function a parameter, and you'll hear that word a lot. So for 386 00:35:56,480 --> 00:36:03,440 example, I could say inside of here capital G. And now this is going to return the index of the 387 00:36:03,440 --> 00:36:10,080 capital G inside of our string. So it should give us back a zero because G is at the zero of index. 388 00:36:10,080 --> 00:36:16,160 And you'll see down here that we get a zero because that's where G is. So for example, if I put an a 389 00:36:16,160 --> 00:36:23,760 inside of here, like a lowercase a, it should give me 0123, because that's where the first a is inside 390 00:36:23,760 --> 00:36:29,680 of this string. So I can click the play button. And you'll see it's giving us a three, you can also 391 00:36:29,680 --> 00:36:35,440 put actual words in here. So for example, I could put like Academy in here, or I could even just put 392 00:36:35,440 --> 00:36:42,240 like a C A D. And this is going to tell me where this starts inside of my string. So when I click 393 00:36:42,240 --> 00:36:50,160 play, it's going to give me an eight because that's where Academy starts right 012345678. 394 00:36:50,960 --> 00:36:55,920 If I put something in here that wasn't in the string, so for example, if I put a Z in here, 395 00:36:55,920 --> 00:37:00,080 that's not in here, it's actually going to throw an error. So when I play this, you'll see that we 396 00:37:00,080 --> 00:37:05,680 get this error down here because Z is not found inside of our program. So that index function, 397 00:37:05,680 --> 00:37:10,000 again, can be really useful. And we'll be using that a lot. There's also one more that I want to 398 00:37:10,000 --> 00:37:16,560 show you, which is called replace. So we can say phrase dot replace. And in here, I can actually 399 00:37:16,560 --> 00:37:22,960 give this to parameters. So I can give this replace function to values that it can use. And the first 400 00:37:22,960 --> 00:37:27,600 thing I want to you to give it is what I want to replace. So for example, I could say draft. 401 00:37:28,240 --> 00:37:32,640 And then I'm going to put a comma in here. And I want to put in here what I want to replace 402 00:37:33,120 --> 00:37:38,720 draft with. So I could just say like elephant. And now instead of saying draft Academy, this is 403 00:37:38,720 --> 00:37:44,000 going to print out elephant Academy. So you can see down here, it just says elephant Academy. So 404 00:37:44,000 --> 00:37:50,080 this replace function can be really awesome because we can replace certain words or even certain 405 00:37:50,080 --> 00:37:55,520 letters inside of our strings with other ones. So those are just some basic ways that we can work 406 00:37:55,520 --> 00:38:01,200 with strings inside of Python. And there's a lot of these different functions that we can use with 407 00:38:01,200 --> 00:38:05,760 strings. These are some of the most common the ones that I just showed you right now. But if you 408 00:38:05,760 --> 00:38:10,960 just do a Google search, you can find all sorts of Python functions that you can try out and use and 409 00:38:10,960 --> 00:38:15,440 you know, see what they do. But you definitely want to get comfortable working with strings in Python, 410 00:38:15,440 --> 00:38:23,840 because you're going to be working with them a lot. In this tutorial, I want to talk to you about 411 00:38:23,840 --> 00:38:31,360 working with numbers in Python. Numbers are one of the most common data types in Python. And any 412 00:38:31,360 --> 00:38:36,720 Python program you write is most likely going to be dealing with numbers at some point. So I want 413 00:38:36,720 --> 00:38:40,000 to talk to you guys about just the basics of using numbers, we're going to talk about the 414 00:38:40,000 --> 00:38:44,880 different types of numbers that we can represent in Python. And I also want to show you guys some 415 00:38:44,880 --> 00:38:50,560 awesome functions that we can use with numbers. So we can do certain like mathematical operations, 416 00:38:50,560 --> 00:38:55,040 we can do some awesome things with numbers. I'm going to show you guys all of that in this video, 417 00:38:55,040 --> 00:39:00,480 it's going to be awesome. So first thing we can do is we can actually just like print out a number. 418 00:39:00,480 --> 00:39:04,480 So if I want to write a number in Python, it's really easy. I basically just write the number. 419 00:39:04,480 --> 00:39:09,920 So I could say like print two. And this is just going to print the number two out onto the screen. 420 00:39:10,560 --> 00:39:15,120 So I can use a whole number like this. I could also use a decimal number. So I could say like 421 00:39:15,120 --> 00:39:21,040 2.0987. And Python is going to be just fine with that. We'll be able to print that out under the 422 00:39:21,040 --> 00:39:28,800 screen as well. I can also make numbers negative. So I could make this 2.0987 a negative number. And 423 00:39:28,800 --> 00:39:36,160 Python will be able to handle that just fine too. And Python, we can also use basic arithmetic. So 424 00:39:36,160 --> 00:39:41,280 I could do things like addition subtraction multiplication. So I could say like three plus 425 00:39:41,280 --> 00:39:47,440 four. And now this is going to print this out. Or why don't we say like three plus 4.5. And this 426 00:39:47,440 --> 00:39:53,280 will be able to print out the answer. So we get 7.5. And we can do addition, we can do subtraction, 427 00:39:53,280 --> 00:39:58,320 we can do division, or we could do like multiplication as well. So you can pretty much do whatever you 428 00:39:58,320 --> 00:40:04,480 want inside of Python. And if you want to make more complex mathematical equations, we can also 429 00:40:04,480 --> 00:40:11,360 use things to specify order of operations. So I could use parentheses. So for example, if I said 430 00:40:11,360 --> 00:40:17,600 three times four plus five, what Python's going to do is it's going to multiply three and four 431 00:40:17,600 --> 00:40:24,480 together. So it's going to be 12. And then we'll add five to that. So we should get 17. But let's 432 00:40:24,480 --> 00:40:28,960 say that we wanted to change up the order, right? I could actually put a parentheses around four 433 00:40:28,960 --> 00:40:34,960 and five. And now it's going to add them first. So it'll add four and five. So we'll get nine, 434 00:40:34,960 --> 00:40:39,920 and we'll multiply that times three. So we should end up with 27. So you can see we can use those 435 00:40:39,920 --> 00:40:45,840 parentheses to specify order of operations. So I could make like a very complex, a little 436 00:40:45,840 --> 00:40:52,320 equation here just by using numbers and parentheses. We can also use one other operation, which is 437 00:40:52,320 --> 00:40:59,040 called the modulus operator. So I could say like 10, and use this percent sign, three. And this is 438 00:40:59,040 --> 00:41:04,560 actually read 10 mod three. And basically what this is going to do is it's going to take the first 439 00:41:04,560 --> 00:41:10,000 number divided by the second number, and it's going to spit out the remainder. So it's basically 440 00:41:10,000 --> 00:41:15,840 going to say, okay, 10 divided by three, that's going to be three with a remainder of one. And so 441 00:41:15,840 --> 00:41:21,200 this is going to give us the remainder. And you'll see down here, we get one, because that's the 442 00:41:21,200 --> 00:41:26,720 remainder of 10 divided by three. And that modulus operator can come in handy quite a bit. We can 443 00:41:26,720 --> 00:41:32,800 also store these numbers inside of variables. So we can store them inside of these variable 444 00:41:32,800 --> 00:41:38,800 containers. So I could come over here, and I could create a variable called like my underscore num. 445 00:41:39,680 --> 00:41:44,560 And we'll just store a value of let's say like five in here. And so I could come down here, 446 00:41:44,560 --> 00:41:49,520 and I can just print out my underscore num. And we'll be able to print that number out to the 447 00:41:49,520 --> 00:41:56,240 screen just fine. So in addition to doing things like a basic arithmetic, we can also do some other 448 00:41:56,240 --> 00:42:01,680 stuff. So one thing I could do is I could actually convert this number into a string. So if I wanted 449 00:42:01,680 --> 00:42:07,520 to convert this number into a string, I can just say str, and I can put the number that I want to 450 00:42:07,520 --> 00:42:12,960 convert inside of these parentheses. And so what's going to happen now is this number is going to 451 00:42:12,960 --> 00:42:18,800 be converted into a string. So when I click the run button, you'll see it's just printing out five, 452 00:42:18,800 --> 00:42:24,320 but now instead of being a number, this is actually a string. And this is going to come in handy when 453 00:42:24,320 --> 00:42:30,640 you want to print out numbers alongside strings. So I can come over here and I can say like, my 454 00:42:32,160 --> 00:42:38,240 favorite number, and we'll be able to print this number along with this string. So I can 455 00:42:38,240 --> 00:42:44,640 click play. And it says five my favorite number. If I was to get rid of this string right here, 456 00:42:44,640 --> 00:42:49,840 and I wasn't converting the number into a string, now Python is going to have a problem with this. 457 00:42:49,840 --> 00:42:54,240 It's not going to allow us to do that. You can see we get an error. So anytime you want to print 458 00:42:54,240 --> 00:42:59,040 out a number next to a string, you got to make sure that you use that little string function. 459 00:42:59,760 --> 00:43:04,960 So there's a bunch of other stuff that we can do with numbers. And a lot of times in Python, 460 00:43:04,960 --> 00:43:10,880 you're going to want to be using specific, like more advanced math operators. So there's a bunch 461 00:43:10,880 --> 00:43:16,640 of different math functions that we can use on our numbers. And a function is basically just like 462 00:43:16,640 --> 00:43:21,840 a little collection of code that does something. So a function could perform an operation like 463 00:43:21,840 --> 00:43:27,680 a mathematical operation on our number. It could also give us information about our number. So 464 00:43:27,680 --> 00:43:31,920 I'm going to show you guys some of the most common functions that you'll be using in Python 465 00:43:31,920 --> 00:43:38,560 related to numbers. The first one is called ABS, and it stands for absolute value. So we can get 466 00:43:38,560 --> 00:43:45,360 the absolute value of a number. And so over here, I'm just going to make this variable negative five. 467 00:43:46,000 --> 00:43:51,360 And basically, I can just say ABS, and I can make an open and closed parentheses over here. 468 00:43:51,360 --> 00:43:57,120 And this is going to give me the absolute value of this number up here. So when I click the play 469 00:43:57,120 --> 00:44:03,440 button, you'll see it's just giving us five, because five is the absolute value of negative five. 470 00:44:03,440 --> 00:44:09,920 I can use a few other functions. There's another one which is called pow. It's so it's just POW. 471 00:44:09,920 --> 00:44:14,560 And I'm actually not going to use this variable. We just use a normal number. And this function is 472 00:44:14,560 --> 00:44:19,920 going to allow us to pass it two pieces of information. So I can give this function two pieces of 473 00:44:19,920 --> 00:44:25,360 information. The first is going to be like a number. And the second is going to be the power 474 00:44:25,360 --> 00:44:31,440 that I want to take that number to. So I could say like three comma two. And this is basically 475 00:44:31,440 --> 00:44:37,200 just going to be three raised to the power of two. So it's just going to be three squared. So we 476 00:44:37,200 --> 00:44:42,560 should get nine down here. You can see that we do. So I could pass in like, you know, some crazy 477 00:44:42,560 --> 00:44:48,000 number, like we could pass in a four and we could raise it to the power of six. And so we should 478 00:44:48,000 --> 00:44:54,400 get like a pretty big number here. And you can see we get 4096. So this pow function is really 479 00:44:54,400 --> 00:45:00,080 useful for taking numbers to specific powers. So in addition to using this pow function, we can 480 00:45:00,080 --> 00:45:06,480 also use another function which is called max. So I can say max. And what this is going to do is 481 00:45:06,480 --> 00:45:12,000 it's basically going to return the larger of the two numbers that we pass into it. So right now I'm 482 00:45:12,000 --> 00:45:17,680 giving this a four and a six. And this should tell us which number is higher. So over here, 483 00:45:17,680 --> 00:45:22,000 it's just printing out six because that's the bigger number. I can also use another function 484 00:45:22,000 --> 00:45:26,640 called min. And this is going to do the opposite. So now instead of printing out the max number, 485 00:45:26,640 --> 00:45:31,280 it's going to print out the smallest number. And it's going to print out four down here, as you can 486 00:45:31,280 --> 00:45:36,960 see, another cool function is called the round function. And this is going to allow us to round 487 00:45:36,960 --> 00:45:42,560 a number. So it's just going to follow like standard rounding rules. So if I said like 3.2 488 00:45:42,560 --> 00:45:48,880 inside of here, now it's going to round it down to just normal three. But if I said like 3.7, 489 00:45:49,760 --> 00:45:53,840 it's going to round it up to four. So that'll allow you to round a number. 490 00:45:53,840 --> 00:45:59,440 So there's a few other functions that I want to show you guys, but in order to get access to them, 491 00:45:59,440 --> 00:46:05,760 I'm actually going to have to do something called importing. And in Python, we can actually import 492 00:46:05,760 --> 00:46:12,400 external code into our files. And so if I want to access these specific math functions, 493 00:46:12,960 --> 00:46:20,000 I have to import something called Python math. So I can just say from up here, math, import, 494 00:46:20,000 --> 00:46:25,440 and this star. And basically, what this is going to do is it's going to go out and it's going to 495 00:46:25,440 --> 00:46:30,880 grab a bunch of different math functions that we can use. So I already showed you guys a few of 496 00:46:30,880 --> 00:46:35,200 these different math functions, but there's actually a bunch more. And in order to access them, 497 00:46:35,200 --> 00:46:39,920 we're going to need to include this line of code inside of our file. So once we have that, 498 00:46:39,920 --> 00:46:44,240 now I can access a few others. So there's another function which is called the floor method. And 499 00:46:44,240 --> 00:46:50,640 it's just FLOR. And what this will do is it'll basically just grab the lowest number. So it's 500 00:46:50,640 --> 00:46:55,760 essentially just going to chop off this decimal point. And you can see over here, now we get a 501 00:46:55,760 --> 00:47:00,960 three, because we're using that floor function, there's also another one called seal. And that's 502 00:47:00,960 --> 00:47:05,440 just going to do the exact opposite. So that's just going to round the number up no matter what. So 503 00:47:05,440 --> 00:47:10,560 no matter what, we'll always get four with we have a three point whatever here. And there's 504 00:47:10,560 --> 00:47:16,560 another one which is called square root. So it's just SQRT. And essentially, this is just going to 505 00:47:16,560 --> 00:47:24,080 return the square root of a number. So I could say like 36. And now we should get six back. So 506 00:47:24,080 --> 00:47:30,320 those are awesome, very interesting functions. And inside of this math module, so we would call 507 00:47:30,320 --> 00:47:34,960 this a module. And you don't have to worry too much about what that is right now. Just know that 508 00:47:34,960 --> 00:47:41,840 when we put this line of code into our program, it gives us access to a lot more math function. 509 00:47:41,840 --> 00:47:47,680 So it basically allows us to do a lot more things with math inside of our program. And there's a 510 00:47:47,680 --> 00:47:52,640 bunch of different math functions in Python. And if you want, you can basically just go online and 511 00:47:52,640 --> 00:47:57,360 search for different math functions. I showed you guys a few here. And I would say the ones that we 512 00:47:57,360 --> 00:48:03,440 looked at here are probably the most commonly used, but there's a bunch more. And like I said, 513 00:48:03,440 --> 00:48:07,680 you know, there's tons of documentation on all this stuff. So you can just look up, you know, 514 00:48:07,680 --> 00:48:12,880 math functions in Python. And there's going to be tons and tons like lists of these things 515 00:48:12,880 --> 00:48:17,920 that you can use inside of your programs to perform different math operations. But for now, 516 00:48:17,920 --> 00:48:21,040 that's just been a little bit about using numbers a little bit about doing, you know, 517 00:48:21,040 --> 00:48:25,280 certain math operations and using functions with those numbers. 518 00:48:25,280 --> 00:48:34,880 In this tutorial, I'm going to show you how to get input from a user. So we're basically going to 519 00:48:34,880 --> 00:48:40,720 allow a user to input information into our program. And I'm actually going to take the 520 00:48:40,720 --> 00:48:45,600 information that the user inputs, I'm going to store it inside of a variable. And then we're 521 00:48:45,600 --> 00:48:50,880 going to be able to do something with that variable. So I want to show you guys how we can actually 522 00:48:50,880 --> 00:48:57,440 go about getting input from someone. And down here in my Python file, all I have to do to get 523 00:48:57,440 --> 00:49:03,760 input from someone is I just have to type out input, and then an open and closed parentheses. 524 00:49:03,760 --> 00:49:08,240 And what this is basically going to do is it's going to tell Python like, hey, we want to get 525 00:49:08,240 --> 00:49:15,520 input from a user and Python will allow the user to type in some information. And inside of these 526 00:49:15,520 --> 00:49:21,200 parentheses, we can actually type a prompt. And so generally, when we want the user to enter in 527 00:49:21,200 --> 00:49:25,680 some information, we want to tell them what we want them to enter. And that's what we can put 528 00:49:25,680 --> 00:49:32,560 inside of here. So I'm just going to say, enter your name. So we're actually going to create a 529 00:49:32,560 --> 00:49:37,760 little Python app, which is going to allow the user to enter in their name. And then we'll store 530 00:49:37,760 --> 00:49:42,400 the name inside of a variable, and then we'll basically just say hi to the user. So we'll say 531 00:49:42,400 --> 00:49:48,080 like, hi, and then whatever the name they entered was. So what I want to do is I actually want to 532 00:49:48,080 --> 00:49:55,520 store whatever the user inputted into our program inside of a variable. So I can actually go over 533 00:49:55,520 --> 00:50:01,120 here, and I can create a variable, I could just call it name. And I could set this variable 534 00:50:01,120 --> 00:50:07,520 equal to input. So what I'm doing now is I'm taking the value that the user inputs, 535 00:50:07,520 --> 00:50:16,400 and I'm storing it inside of this variable container called name. And so now I have access to whatever 536 00:50:16,400 --> 00:50:23,040 they entered in. So what we can do is we can say print. And inside of the parentheses, I can just 537 00:50:23,040 --> 00:50:30,400 print out hello. And then I'm just going to add on to this name. And actually, why don't we put 538 00:50:30,400 --> 00:50:36,480 an exclamation point? So essentially, we're getting a name from the user, we're storing it inside of 539 00:50:36,480 --> 00:50:42,000 the name variable. And I'm printing it out onto the screen. So I'm going to go ahead and play this. 540 00:50:42,720 --> 00:50:48,080 And when we run the program, you'll notice down here, Python is giving us this little prompt. So 541 00:50:48,080 --> 00:50:53,760 it's saying enter your name. Now in here, I can actually type in a value. So I can type in Mike. 542 00:50:54,800 --> 00:51:01,600 And then I can just hit enter. And Python's going to go ahead store this value inside of that variable. 543 00:51:01,600 --> 00:51:06,560 And then it's going to print out hello, Mike. And so this is kind of cool. And actually, what we 544 00:51:06,560 --> 00:51:12,880 could do is we could prompt the user to enter in more than one piece of information. So I could 545 00:51:12,880 --> 00:51:18,800 actually copy this line of code. And I'm going to paste it below here. And why don't we have the 546 00:51:18,800 --> 00:51:24,880 user enter in another variable? So we could have them enter in, for example, their age. And over 547 00:51:24,880 --> 00:51:30,800 here, we just want to say enter your age. And so now we're able to get two pieces of information 548 00:51:30,800 --> 00:51:38,320 from the user. And I could say hello, name. And then I can say, you are, and I'll print out their 549 00:51:38,320 --> 00:51:44,560 age. So I'm just going to print age. So now I'm basically able to get both of these pieces of 550 00:51:44,560 --> 00:51:50,640 information from the user, I can get their name. And I can get their age. So when we run this program, 551 00:51:52,240 --> 00:51:57,280 I can type in both of those pieces of information. So to say, answer your name. So we could just 552 00:51:57,280 --> 00:52:03,440 make up a name like john, and then enter the age, let's say john is 25. And now it's going to say, 553 00:52:03,440 --> 00:52:09,520 Hello, john, you are 25. So the program is able to get input from the user. And then it's able to 554 00:52:09,520 --> 00:52:15,760 use that information in order to say hi to the user and tell them how old they are. And so 555 00:52:15,760 --> 00:52:21,360 getting input from the user is a great way to make your programs more interactive. And here, 556 00:52:21,360 --> 00:52:25,520 we're just scratching the surface, right? We're just basically getting to, you know, a piece of 557 00:52:25,520 --> 00:52:30,800 information from the user, but you can use these inputs to do awesome things in Python. And as we 558 00:52:30,800 --> 00:52:35,840 go through this course, you're going to see some really cool things that we can do with these inputs. 559 00:52:39,840 --> 00:52:45,280 In this tutorial, I'm going to show you how to build a very basic calculator in Python, 560 00:52:45,280 --> 00:52:49,440 we're basically going to build a calculator where we'll get two numbers from a user, 561 00:52:49,440 --> 00:52:54,160 and then we'll add those numbers together and print the answer out onto the screen. So let's 562 00:52:54,160 --> 00:52:59,520 look at some practice in getting input from users. And we'll also talk about getting numbers from 563 00:52:59,520 --> 00:53:05,440 users as opposed to just strings. So I'm really excited to get started on this calculator. The 564 00:53:05,440 --> 00:53:11,600 first thing we want to do is create two variables and inside of those variables, we want to store 565 00:53:11,600 --> 00:53:16,640 the two numbers that the user wants to add together. So down here, I'm just going to say, 566 00:53:17,920 --> 00:53:23,440 num one. And that's what we'll call our first variable is equal to. And now I need to get 567 00:53:23,440 --> 00:53:28,800 input from the user. So I'm going to say input. And inside these parentheses, I'm going to say, 568 00:53:28,800 --> 00:53:38,720 enter a number. And now I basically want to do the same exact thing, but for the second number. 569 00:53:38,720 --> 00:53:45,760 So we'll paste this guy down here. And I'm just going to say num two. And then we'll just say, 570 00:53:45,760 --> 00:53:52,640 enter another number. So now we're able to store the user's input inside of these two variables. 571 00:53:52,640 --> 00:53:58,800 So now all I want to do is just print out the result. So I'm actually going to create a variable 572 00:53:58,800 --> 00:54:03,680 called result. So I'm just going to say result is equal to, and basically, we just want to add 573 00:54:03,680 --> 00:54:09,840 these two numbers together. So I'm going to say num one plus num two. And now all we have to do 574 00:54:09,840 --> 00:54:16,720 is just print out the result. So I'm just going to print result. So we basically have our entire 575 00:54:16,720 --> 00:54:21,040 program written out here. And this will allow us to enter in two numbers, and then we'll add them 576 00:54:21,040 --> 00:54:29,120 together and print out the answer. So I'm going to click play. And down here, we can enter our number. 577 00:54:29,120 --> 00:54:34,080 So I'm going to enter the first number, we'll say five. And I'm going to enter my second number. 578 00:54:34,080 --> 00:54:44,080 So let's say 8.0, or let's say 8.3. So now we should print out the answer. But if you look over here, 579 00:54:44,080 --> 00:54:53,040 that's not quite the answer we were looking for, right, five plus 8.3 is definitely not 58.3. And 580 00:54:53,040 --> 00:55:01,120 here's what happened. When we get input from a user by default, Python is just going to convert 581 00:55:01,120 --> 00:55:06,080 it into a string. So it doesn't matter what we put down there when we're entering the numbers, 582 00:55:06,080 --> 00:55:12,320 Python's always just going to be like, okay, this is a string, that's it. Sounds good, right? It's just 583 00:55:12,320 --> 00:55:18,000 going to make it a string. In order for us to make these num one and num two variables numbers, 584 00:55:18,000 --> 00:55:24,640 we're actually going to have to convert the strings that we get from the user into numbers. And so 585 00:55:24,640 --> 00:55:30,240 the way that we can do that is by basically just coming over here. And when we add the two numbers 586 00:55:30,240 --> 00:55:37,440 together, I can use a special Python function in order to convert those strings into numbers. 587 00:55:37,440 --> 00:55:43,360 And there's actually two Python functions that we could use for this. The first is called int. 588 00:55:43,360 --> 00:55:50,080 So I could say int, I can make an open parentheses and I can make a closed parentheses. And basically 589 00:55:50,080 --> 00:55:54,960 what this is going to do is it's going to convert whatever is inside of these open and closed parentheses 590 00:55:54,960 --> 00:56:02,000 into an integer number. And an integer number is basically a whole number. So it's like one, 591 00:56:02,000 --> 00:56:07,040 two, three, four, five. So it's a, it's a whole number. In other words, it can't have a decimal 592 00:56:07,040 --> 00:56:15,200 point. And so what I could do is I could say, int, and I could put these inside of parentheses. 593 00:56:15,200 --> 00:56:19,680 And now what this is going to do is it's going to convert both of these numbers into 594 00:56:19,680 --> 00:56:25,200 integers. In other words, into whole numbers that don't have decimals. And it's going to add 595 00:56:25,200 --> 00:56:29,200 them together and we'll print out the results. So why don't we do that? And we'll just see if 596 00:56:29,200 --> 00:56:34,000 this works. So I'm going to click the play button. And down here, we'll enter our first number. So 597 00:56:34,000 --> 00:56:38,720 I'm just going to enter in a four. And then I'll enter in a five. And now you can see it's 598 00:56:38,720 --> 00:56:45,120 printing out the correct answer four plus five. And so that's essentially our basic calculator. 599 00:56:45,120 --> 00:56:49,440 But we do have one problem with this calculator. So I'm going to run it again. And I'll show you 600 00:56:49,440 --> 00:56:56,800 guys with that problem is if I wanted to add in a decimal number, so we'll add 4.3 and 5.5. 601 00:56:56,800 --> 00:57:03,840 You'll see that we're getting this error. And that's because I'm using this int function 602 00:57:03,840 --> 00:57:09,840 up here. And this int function is looking for a whole number. So if I put decimal numbers 603 00:57:09,840 --> 00:57:14,400 inside of there, it's going to break the program. There's another function we can use 604 00:57:14,400 --> 00:57:19,760 instead of an int, we can say float. And a float is basically just a number that has 605 00:57:19,760 --> 00:57:24,880 decimal. So it's a decimal number. So I can convert both of these guys into floats. 606 00:57:24,880 --> 00:57:30,480 And now I'll be able to use decimal numbers. So I can click play. And I can just enter in some 607 00:57:30,480 --> 00:57:36,640 numbers. So I could say like four. And I could also say like 5.5. And now it's going to be able 608 00:57:36,640 --> 00:57:42,160 to add both of those numbers, even if one of them is a decimal. And that's the beauty of using that 609 00:57:42,160 --> 00:57:47,600 little float function. So in certain circumstances, you might want the user to only be able to enter 610 00:57:47,600 --> 00:57:52,880 in an integer to a whole number. But in a lot of cases, like in this calculator, we want them to 611 00:57:52,880 --> 00:57:56,960 be able to enter any number that they want. So we're going to use this float 612 00:57:56,960 --> 00:58:01,520 function. So I'm just going to recap what we did one more time, we created two variables, 613 00:58:01,520 --> 00:58:07,440 num one and num two. And we basically stored the two numbers that the users input into those 614 00:58:07,440 --> 00:58:14,080 variables. And then we converted them into numbers. And we added them together and we printed out 615 00:58:14,080 --> 00:58:18,160 the results. So this is a pretty cool calculator. I'm happy with it. Hopefully you guys learned 616 00:58:18,160 --> 00:58:23,200 something. And as we go forward in this Python course, you're going to learn all sorts of ways 617 00:58:23,200 --> 00:58:31,600 that we can make this calculator more complex. In this tutorial, I want to show you guys how to 618 00:58:31,600 --> 00:58:38,960 build a madlibs game in Python. So a madlibs game is basically just a game where you can 619 00:58:38,960 --> 00:58:46,400 enter in a bunch of random words. So like, you know, verbs, nouns, names, colors. And you basically 620 00:58:46,400 --> 00:58:52,880 take all of those words and put them into a story randomly. So over here, my web browser, 621 00:58:52,880 --> 00:58:57,680 I just have an example of a madlibs. So, you know, down here, we're basically just taking random 622 00:58:57,680 --> 00:59:02,960 words, like an adjective or a noun or a plural noun, and entering them in side of a story. So 623 00:59:02,960 --> 00:59:06,960 like you read through the story and then like some random word pops up and madlibs can be pretty 624 00:59:06,960 --> 00:59:11,040 fun. And I'm sure you guys have, you know, at least heard of madlibs before. But if not, 625 00:59:11,040 --> 00:59:15,680 that was just a little introduction. So I want to create a madlibs in Python. And this is going 626 00:59:15,680 --> 00:59:20,720 to be really cool. So down here, I have this like little poem, which just says, roses are red, 627 00:59:20,720 --> 00:59:26,160 violets are blue, I love you. And so, you know, this is kind of like a classic poem. But I think 628 00:59:26,160 --> 00:59:33,520 we should mix it up a little bit and create a madlib for this poem. And so instead of having this red 629 00:59:33,520 --> 00:59:40,560 color over here, I want to have the user enter in their own color. Instead of having violets, 630 00:59:40,560 --> 00:59:47,040 why don't we have them enter in a plural noun? And instead of saying I love you, why don't we have 631 00:59:47,040 --> 00:59:55,200 them say I love and then some celebrity? So celebrity. So this is the basic structure for our little 632 00:59:55,200 --> 01:00:00,960 madlib, right? I want to basically have the user enter in a color, enter in a plural noun, 633 01:00:00,960 --> 01:00:05,920 and enter in a celebrity. And then I'll take all of those words and put them inside of our 634 01:00:05,920 --> 01:00:10,960 madlib. And hopefully we end up with something that's pretty funny. So how can we actually go 635 01:00:10,960 --> 01:00:15,600 about building this in Python? Well, the first thing we're going to have to do is we're going to have 636 01:00:15,600 --> 01:00:20,880 to be able to get these words from the user, right? That's pretty obvious. So I think what we should 637 01:00:20,880 --> 01:00:27,120 do is we should create three variables called color plural noun and celebrity. And we'll store the 638 01:00:27,120 --> 01:00:33,440 user's inputs inside of those variables. So up here, let's create those variables. The first 639 01:00:33,440 --> 01:00:38,400 variable I want to create is going to be called color. So say color. And I want to set this equal 640 01:00:38,400 --> 01:00:43,520 to the input. So I'm going to say input. And inside of here, I'm just going to give them a prompt. So 641 01:00:43,520 --> 01:00:50,960 I'll say enter a color. And we can basically just copy this line of code. And I'm going to paste 642 01:00:50,960 --> 01:00:56,640 it down below. And we'll paste one more time. So now we're going to create our plural noun 643 01:00:56,640 --> 01:01:06,640 variable. So I'll say PL, URL, plural noun. And we'll just say enter a plural noun. And actually, 644 01:01:06,640 --> 01:01:12,640 over here, I want to make a colon as well. So then finally, we'll create our celebrity variable 645 01:01:12,640 --> 01:01:19,760 and we'll say enter a celebrity. And so now basically, we're creating three variables called 646 01:01:19,760 --> 01:01:26,000 color plural noun and celebrity. And inside of them, we're storing the color that the user enters, 647 01:01:26,000 --> 01:01:32,080 the plural noun, the user enters and the celebrity, the user enters. So last thing we have to do is 648 01:01:32,080 --> 01:01:39,200 replace these guys with those variables. So I'm basically just going to add in the color over here. 649 01:01:40,400 --> 01:01:45,120 And we're going to add in the plural noun. So I can just go like this. 650 01:01:45,120 --> 01:01:59,120 And finally, we'll just add in that celebrity. So now we have a pretty cool mad lib. And it should 651 01:01:59,120 --> 01:02:04,880 be functional. Everything should work. It looks like it's good to go. So let's come over here and 652 01:02:04,880 --> 01:02:11,200 we'll run our program. And then down here in the console, we can play our little mad libs game. 653 01:02:11,200 --> 01:02:18,000 So let's say enter a color, I'm just going to say magenta, enter a plural noun. Why don't we say 654 01:02:19,600 --> 01:02:27,120 microwaves and enter a celebrity, let's say Tom Hanks. So when I click enter, what should happen 655 01:02:27,120 --> 01:02:32,400 is we should get our mad libs printed out. So I'm going to click enter. And now we have our mad 656 01:02:32,400 --> 01:02:38,800 libs. So it says roses or magenta, microwaves are blue. I love Tom Hanks. So it's a pretty good mad 657 01:02:38,800 --> 01:02:44,080 lib. And obviously, you know, the more the more print statements we put over here, you know, 658 01:02:44,080 --> 01:02:48,640 the better the mad lib could could be. And we could store as many, you know, variables up here as we 659 01:02:48,640 --> 01:02:54,080 wanted. This was just a very simple mad lib. We only took in three inputs, but you know, you can 660 01:02:54,080 --> 01:02:59,440 see how you could take something like this and create a little mad libs game. So your homework 661 01:02:59,440 --> 01:03:03,920 for this lesson is to go off and make your own mad libs game. Hopefully one that's a little bit 662 01:03:03,920 --> 01:03:09,200 funnier than mine. But that's the basics of creating a mad libs game in Python. 663 01:03:13,200 --> 01:03:18,480 In this tutorial, I want to talk to you guys about working with lists in Python. A lot of 664 01:03:18,480 --> 01:03:23,280 times when you're programming in Python, you're going to be dealing with large amounts of data. 665 01:03:23,280 --> 01:03:26,800 And when you're dealing with large amounts of data, you want to make sure that you can 666 01:03:26,800 --> 01:03:32,800 manage it and organize it properly. And the list is essentially just a structure that we can use 667 01:03:32,800 --> 01:03:38,880 inside of Python to store lists of information. So we can take a bunch of different data values, 668 01:03:38,880 --> 01:03:43,920 we can put them inside a list, and it allows us to organize them and keep track of them a lot 669 01:03:43,920 --> 01:03:49,920 easier. So generally, you would create a Python list, and you would put a bunch of related values 670 01:03:49,920 --> 01:03:54,000 inside of that list, and then you can use it throughout your program. So I'm going to show you 671 01:03:54,000 --> 01:03:58,480 guys just the basics of using lists, we're going to look at some of the common use cases and 672 01:03:58,480 --> 01:04:03,280 really just get you up to speed with what lists are and why they're useful. So over here in my 673 01:04:03,280 --> 01:04:08,800 program, I'm actually going to create a list. And we create a list a lot like we create a 674 01:04:08,800 --> 01:04:13,520 Python, just like a normal Python variable. So the first thing we want to do when we're making a 675 01:04:13,520 --> 01:04:18,960 list is give it a name. So we want to give it a descriptive name, something that's going to 676 01:04:18,960 --> 01:04:24,800 basically describe what's inside the list. In my case, for this example, let's make a list of 677 01:04:24,800 --> 01:04:29,440 like friends. So we could say like here's a list of all my friends. So I can say friends. 678 01:04:30,000 --> 01:04:35,840 And I'm going to set this equal to the list of values that represent my friends. And we want 679 01:04:35,840 --> 01:04:41,920 to create a list, we can use these open and closed square brackets. So whenever you use these open 680 01:04:41,920 --> 01:04:47,680 and closed square brackets, Python basically knows, okay, they want to store a bunch of values inside 681 01:04:47,680 --> 01:04:52,640 of this list. So why don't we go ahead and start adding some values? So we can say like my friends 682 01:04:52,640 --> 01:05:05,840 are like Kevin, Karen, and let's say Jim. So here I have three values inside of my friends array. 683 01:05:05,840 --> 01:05:11,680 So when we're creating a normal variable in Python, generally, we just give it one value, 684 01:05:11,680 --> 01:05:16,160 right? So I would create a variable, I would give it one value, maybe a string or a number or a 685 01:05:16,160 --> 01:05:23,680 boolean. But when we make lists, we're able to store multiple values inside of the same like little 686 01:05:23,680 --> 01:05:29,600 item inside of the same object, right? And then what I can do is I can access these individual items 687 01:05:29,600 --> 01:05:36,240 inside of my program. So inside of this friends list, we're basically storing all of these different 688 01:05:36,240 --> 01:05:41,120 values, and that can be really useful. So now that we've stored some values, let's talk about what 689 01:05:41,120 --> 01:05:46,560 you can put inside of a list. So you can really put any piece of information in Python, we could 690 01:05:46,560 --> 01:05:51,600 put things like strings, numbers, or booleans. So if I wanted, instead of just putting strings 691 01:05:51,600 --> 01:05:56,640 here, I could change like one of these to a number, and I could change one of these to a boolean, 692 01:05:56,640 --> 01:06:01,120 and it wouldn't matter, Python is going to be fine with that. But let's change these back to 693 01:06:01,120 --> 01:06:07,840 strings just for the sake of the example. So now that we've created our list, how can we access 694 01:06:07,840 --> 01:06:12,640 individual elements inside of this list? It's a good question. I'm gonna go ahead and print 695 01:06:12,640 --> 01:06:16,880 something out to the screen. The first thing we can do is we can just print out the actual list. 696 01:06:16,880 --> 01:06:22,720 So I can just say print friends. And then when I run this program, if we look down here in the 697 01:06:22,720 --> 01:06:27,280 console, you can see it's just printing out the actual list of friends, right? But let's say that 698 01:06:27,280 --> 01:06:33,760 I wanted to print out a specific element inside of this list. Well, I can actually refer to elements 699 01:06:33,760 --> 01:06:40,960 by their index. So each one of the elements inside of this list has a particular index. And the index 700 01:06:40,960 --> 01:06:46,880 is actually start at zero. So we would say that this first element in the list, Kevin has an index 701 01:06:46,880 --> 01:06:53,200 of zero. We would say that this other element Karen has an index of one. And this third element 702 01:06:53,200 --> 01:06:59,760 Jim would have an index of two. So even though Jim is technically the third element inside of the 703 01:06:59,760 --> 01:07:07,200 list, it has an index of two. And that's because we start indexing index position one. And so that's 704 01:07:07,200 --> 01:07:13,600 just kind of how lists work. And that's how list indexes work. So if I wanted to access a specific 705 01:07:13,600 --> 01:07:19,440 element inside of my friends list, I can just refer to it by its index. So over here after we 706 01:07:19,440 --> 01:07:24,480 type out friends, I'm just gonna type an open and closed square bracket. And inside of this open 707 01:07:24,480 --> 01:07:29,840 and closed square bracket, I want to put the index of the element inside the list that I want to 708 01:07:29,840 --> 01:07:35,760 access. So if I wanted to access this guy right here, this element, I can just put a zero. So I can 709 01:07:35,760 --> 01:07:41,280 say friends zero. And now when I run my program, it's just gonna print out that value, it's just 710 01:07:41,280 --> 01:07:47,520 gonna print out Kevin. If I wanted to access this Jim value over here, I can put a two. And that 711 01:07:47,520 --> 01:07:53,600 will give us that Jim value, you can see right there. So we can access elements based off of 712 01:07:53,600 --> 01:08:00,400 their index, we can also access elements based off of their index from the back of the list. So 713 01:08:00,400 --> 01:08:06,160 for example, if I said negative one inside of here, this is gonna give me access to this value 714 01:08:06,160 --> 01:08:11,120 right here for Jim. And you can see that sort of does so when we use negatives, it starts indexing 715 01:08:11,120 --> 01:08:16,560 from the back of the list. So if I said negative two, this is gonna give us Karen. And so you can 716 01:08:16,560 --> 01:08:21,440 use the negatives to access items at the back of the list. Just keep in mind though, and this is kind 717 01:08:21,440 --> 01:08:29,760 of interesting. We would say this first element here is index position zero, right? But we would 718 01:08:29,760 --> 01:08:34,960 say the first element at the back of the list is index position negative one. So just keep that 719 01:08:34,960 --> 01:08:39,920 in mind. So there's actually other ways that we can access like just portions of this list. So for 720 01:08:39,920 --> 01:08:44,800 example, let's say I just wanted to select the last two elements in this list. And I didn't want 721 01:08:44,800 --> 01:08:50,560 to select the first one. Well, in here, I can just say one with a colon. And this will grab the 722 01:08:50,560 --> 01:08:55,840 element at index position one and all of the elements after that. So now we should just grab 723 01:08:55,840 --> 01:09:02,480 Jim and Karen or Karen and Jim in that order. I could also specify a range. So actually, 724 01:09:02,480 --> 01:09:08,160 let me add in a couple other elements just so we can illustrate this better. So I'll add in two 725 01:09:08,160 --> 01:09:13,760 more names. So if I wanted to, I could select like the element at index position one up to the 726 01:09:13,760 --> 01:09:20,800 element at index position three. So it's going to grab this one. So that's one, two. And it's actually 727 01:09:20,800 --> 01:09:25,200 not going to grab the third one. So it's it's going to grab all the elements up to but not 728 01:09:25,200 --> 01:09:30,960 including three. So it'll grab Karen and Jim. So let's go ahead and run this. And you can see 729 01:09:30,960 --> 01:09:36,000 it grabs Karen and Jim. So you can either just put like one with a colon after it, and then it'll 730 01:09:36,000 --> 01:09:41,040 grab that index position and everything after it, or you can specify like a range that you want to 731 01:09:41,040 --> 01:09:47,600 grab from. So that's how we can access elements inside of this list. But we can also modify elements. 732 01:09:47,600 --> 01:09:53,440 So for example, I could come down here, let's say I want to modify this value right here, Karen. 733 01:09:53,440 --> 01:09:59,440 Well, all I have to do is just access that element. So I could say friends and Karen is at index 734 01:09:59,440 --> 01:10:04,640 position one. And I can change this to whatever I want. So I could say friends index position one 735 01:10:04,640 --> 01:10:11,360 is equal to Mike. And now when we print out friends index position one, that value is going to be 736 01:10:11,360 --> 01:10:16,800 updated. So it's just going to say Mike instead of saying Karen. So it can be really useful to 737 01:10:16,800 --> 01:10:23,200 modify values inside of arrays. So it can be really useful to modify values inside of Python lists. 738 01:10:23,200 --> 01:10:27,840 So those are kind of the basics of working with lists in Python. And actually in the next tutorial, 739 01:10:27,840 --> 01:10:32,160 I'm going to show you guys even more ways that we can use these lists, we're going to learn how 740 01:10:32,160 --> 01:10:37,520 to like add elements to a list, delete elements from a list, copy lists, and a bunch of cool stuff 741 01:10:38,240 --> 01:10:43,360 with these lists. But for now, this has just been sort of like the basics of working with lists. 742 01:10:47,280 --> 01:10:53,200 In this tutorial, I want to talk to you guys about using functions with lists in Python. 743 01:10:53,200 --> 01:10:58,960 And Python, a list is one of the most important structures where we can store our information. 744 01:10:58,960 --> 01:11:03,760 A list basically allows you to take a bunch of different values and organize them and store them 745 01:11:03,760 --> 01:11:10,160 inside of their own little list structure. So down here in my program, you can see I have two lists. 746 01:11:10,160 --> 01:11:14,480 I have a list of lucky numbers. And this is just a bunch of different numbers. And then I have a 747 01:11:14,480 --> 01:11:18,880 list of friends. So this could be like a list of somebody's friends. And it's basically just a 748 01:11:18,880 --> 01:11:25,120 bunch of strings with names in them. So these two lists are storing a bunch of different values. 749 01:11:25,120 --> 01:11:30,000 And what makes lists awesome is not only could I store like this many values, I mean, I could store 750 01:11:30,000 --> 01:11:35,040 hundreds or thousands or millions of values. And when you're working with a lot of data and a lot 751 01:11:35,040 --> 01:11:41,040 of information in Python, these lists are invaluable. So because lists are so important, there's actually 752 01:11:41,040 --> 01:11:46,320 a bunch of different functions that we can use with lists. And those functions allow us to do 753 01:11:46,320 --> 01:11:52,720 things like modify the lists and get information about the list. So in this tutorial, I just want 754 01:11:52,720 --> 01:11:56,320 to show you a bunch of those functions, we're going to talk about some of the most common and 755 01:11:56,320 --> 01:12:01,680 the most popular functions that you would use on lists to make them more powerful and make them 756 01:12:01,680 --> 01:12:07,440 more easy to use. So the first thing we can do is obviously just print out a list. So down here, 757 01:12:07,440 --> 01:12:13,520 I'm printing out this friends list. And when I want to run my program, you see down here, 758 01:12:13,520 --> 01:12:18,160 we're just printing out all the elements in the list. So it's always good to be able to print out 759 01:12:18,160 --> 01:12:22,480 all the elements inside of a list. But there's a lot of other functions that we can use. So 760 01:12:22,480 --> 01:12:27,280 one function is called the extend function. And the extend function will basically allow 761 01:12:27,280 --> 01:12:34,800 you to take a list and append another list onto the end of it. So I could say like friends dot 762 01:12:34,800 --> 01:12:40,720 extend. And inside of these parentheses, I can pass in the list that I want to add on to the 763 01:12:40,720 --> 01:12:47,200 friends list. So I could say like lucky numbers. And now when I print out this friends list, 764 01:12:47,200 --> 01:12:50,800 not only is it going to have all the elements in the friends list, it's also going to have all 765 01:12:50,800 --> 01:12:55,760 the elements inside the lucky numbers list. You can see down here, we have all of our friends. 766 01:12:55,760 --> 01:13:00,720 And then we also have the numbers over here. So that's a really easy way to essentially just like, 767 01:13:00,720 --> 01:13:07,360 add two lists together. We can also just add individual elements onto a list. So I could come 768 01:13:07,360 --> 01:13:14,960 down here and I could say friends dot append. And this will actually allow me to append another item 769 01:13:14,960 --> 01:13:20,720 onto the end of this list. So over here, I could just add another name. So we could say creed, for 770 01:13:20,720 --> 01:13:25,680 example, and now I'm like adding another friend onto the end of this list. So when I run the program, 771 01:13:26,240 --> 01:13:31,760 in addition to having the original items that were in the list up here, we also have this 772 01:13:31,760 --> 01:13:36,960 additional item at the end, and that append function is always going to add the item onto the end of 773 01:13:36,960 --> 01:13:41,680 the list. But let's say that you wanted to add an item into the middle of the list. So like right 774 01:13:41,680 --> 01:13:49,360 here or right here, there's another list function called insert. And insert is going to take two 775 01:13:49,360 --> 01:13:54,560 parameters. The first parameter is going to be the index where you want to insert the item. So 776 01:13:54,560 --> 01:14:00,000 why don't we insert it here at index position one? I'm just going to say one. And then I'm going to 777 01:14:00,000 --> 01:14:06,160 type in the name of the element that I want to add. So we can just say like Kelly. And now at index 778 01:14:06,160 --> 01:14:11,600 position one, we're going to have the value Kelly, and all of these other values are going to get 779 01:14:11,600 --> 01:14:18,240 pushed up, they're going to get pushed to the right one index position. So now I run the program, 780 01:14:18,240 --> 01:14:23,600 you can see we have Kevin, this new element Kelly, and then all the other elements have been pushed 781 01:14:23,600 --> 01:14:29,360 off to the right, we can also remove elements. So for example, let's say I wanted to remove this 782 01:14:29,360 --> 01:14:36,480 gym value, all I have to do is just say friends dot remove. And I can just type in whatever element 783 01:14:36,480 --> 01:14:42,080 I want to remove. So I want to remove this gym element. And so now when we run the program, 784 01:14:42,080 --> 01:14:48,320 Jim is going to be gone from the list, and we just have these four guys left, you can also remove 785 01:14:48,320 --> 01:14:53,600 all of the elements from the list. So if I just wanted to completely like reset the list and get 786 01:14:53,600 --> 01:14:59,040 rid of everything, I could just say friends dot clear. And this is going to give us an empty list. 787 01:14:59,040 --> 01:15:04,400 So it'll get rid of every single element inside the list. There's also another really cool method, 788 01:15:04,400 --> 01:15:09,280 which is called pop. And you can just type out pop like that. And basically what pops going to do 789 01:15:09,280 --> 01:15:14,880 is it's going to pop an item off of this list. So when I click the play button, you'll see here 790 01:15:14,880 --> 01:15:22,640 that we're left with Kevin, Karen, Jim, and Oscar. So we basically got rid of the last element inside 791 01:15:22,640 --> 01:15:27,680 the list. And that's what pop does, it pops an element off of the list. So it basically removes 792 01:15:27,680 --> 01:15:33,440 the last element in the list. So we have our list, but let's say that we wanted to figure out if a 793 01:15:33,440 --> 01:15:38,080 certain element was in this list, right, if a certain value is in here, so let's say I wanted 794 01:15:38,080 --> 01:15:45,520 to check to see if my friend Kevin was in this list, well, I could say friends dot index. And in 795 01:15:45,520 --> 01:15:53,520 here, I can just pass a value. So I could say like, Kevin, and this is going to tell me the index 796 01:15:53,520 --> 01:15:58,960 of Kevin. So it'll tell me if Kevin's in the list. So now when I click play, you can see down here, 797 01:15:58,960 --> 01:16:05,920 it says zero. If I was to say like Oscar in here, for example, now it'll give me the index of Oscar. 798 01:16:05,920 --> 01:16:12,560 So that's it three. But if I put a name that wasn't in here, so like, if I put Mike, that's not in the 799 01:16:12,560 --> 01:16:18,560 list. And so now we're going to get back in air, because it says Mike is not in the list. So that's 800 01:16:18,560 --> 01:16:23,920 an easy way to figure out if a specific element is inside of the list. And if it is, then you know 801 01:16:23,920 --> 01:16:29,840 what index it's at, you can also count the number of similar elements in the list. So I'm actually 802 01:16:29,840 --> 01:16:36,160 going to come down here, copy this gym element, and we'll add another gym element into here. So I 803 01:16:36,160 --> 01:16:43,440 could come down here and I could say friends dot count. And we'll pass in gym. And this will tell 804 01:16:43,440 --> 01:16:50,640 me how many times the value gym shows up inside of this list. So it should say two, which it does 805 01:16:50,640 --> 01:16:57,280 down there, because we have two values with the text gym. And so we have all these values, we could 806 01:16:57,280 --> 01:17:03,920 also sort this list. So I could come over here and I could say friends dot sort. And what this 807 01:17:03,920 --> 01:17:10,320 will do is it'll sort the list in ascending order. So we have a bunch of text here, and you'll see 808 01:17:10,320 --> 01:17:14,800 what it does. Basically, what it's going to do is it's going to put it in alphabetical order. So 809 01:17:14,800 --> 01:17:20,880 you can see we have J K O and T. So the list went from being just random order, like whatever we 810 01:17:20,880 --> 01:17:26,640 had up here to now it's in alphabetical order. And that's also going to work for lucky numbers. So I 811 01:17:26,640 --> 01:17:34,320 could say like lucky numbers dot sort, and then we'll print out lucky numbers. And these should 812 01:17:34,320 --> 01:17:40,080 now be in ascending order, which I'm realizing they already are up here. But if I was to take this 813 01:17:40,080 --> 01:17:48,560 42 and put it down here, now it will figure everything out and put it in ascending order. Anyway, we 814 01:17:48,560 --> 01:17:54,000 can also reverse a list. So if I wanted, I could reverse this lucky numbers list, I could say 815 01:17:54,000 --> 01:18:00,640 lucky numbers dot reverse. And now when we print it out, instead of being 4815 1623, 816 01:18:00,640 --> 01:18:07,680 it's going to be 2316 15 842. So it's basically just reverse the order of the list. And finally, 817 01:18:07,680 --> 01:18:14,000 there's one more list function I want to show you guys which is copy. So I could actually create 818 01:18:14,000 --> 01:18:21,920 another list, and I could make it as a copy. So I could say like, friends to, and we could set this 819 01:18:21,920 --> 01:18:29,680 equal to friends dot copy. And now friends to is going to have all the same attributes as friends. 820 01:18:30,720 --> 01:18:36,080 Because it's basically just like copied it basically took a copy of that friends list. So 821 01:18:36,080 --> 01:18:40,320 you can see it's the same exact thing and copying lists can come in handy in a lot of 822 01:18:40,320 --> 01:18:44,800 different situations. So those are just some basic list functions and just to kind of like, 823 01:18:44,800 --> 01:18:49,040 you know, get your feet wet and working with lists. Obviously, like the more you work with them, 824 01:18:49,040 --> 01:18:52,880 the more you practice using these different functions, the more comfortable you're going to get. 825 01:18:52,880 --> 01:18:55,920 But that's just kind of like a basic overview of what you can do. 826 01:18:55,920 --> 01:19:07,840 In this tutorial, I want to talk to you guys about tuples in Python. Now a tuple is a type of data 827 01:19:07,840 --> 01:19:14,240 structure, which basically means it's a container where we can store different values. Now if you're 828 01:19:14,240 --> 01:19:21,520 familiar with lists in Python, a tuple is actually very similar to a list. It's basically a structure 829 01:19:21,520 --> 01:19:29,040 where we can store multiple pieces of information. But a tuple has a few key differences from lists 830 01:19:29,040 --> 01:19:33,760 that we're going to talk about in this tutorial. First thing I want to do is I want to show you 831 01:19:33,760 --> 01:19:39,600 the how to create a tuple. So I can come down here and I can create a tuple. And one of the most 832 01:19:40,640 --> 01:19:46,640 common examples of tuples is coordinates. So let's say I had a series of like XY coordinates, I 833 01:19:46,640 --> 01:19:53,280 could create a variable called coordinates. And I'm just going to set this equal to an open and 834 01:19:53,280 --> 01:19:58,880 closed parentheses. And whenever we want to create a tuple inside of Python, we're going to use 835 01:19:58,880 --> 01:20:03,680 these open and closed parentheses. And inside of here inside of these parentheses, we're going to 836 01:20:03,680 --> 01:20:10,800 put the values that we want to store. So I could, for example, put a list of numbers. So this could 837 01:20:10,800 --> 01:20:17,680 be like my coordinates, I could have like four and five. So this would be my tuple with coordinates 838 01:20:17,680 --> 01:20:24,240 inside of it. And one of the things about tuples that makes them unique is a tuple is immutable. 839 01:20:24,240 --> 01:20:28,720 And you'll hear that word a lot when we're talking about tuples immutable, it basically means that 840 01:20:28,720 --> 01:20:35,600 the tuple can't be changed or modified. Once we create our tuple up here, you cannot modify it, 841 01:20:35,600 --> 01:20:40,960 you cannot change it, you can't add elements to it, you can't erase elements from it, you can't 842 01:20:40,960 --> 01:20:46,000 change any of the elements inside the tuple. What you see is what you get, once you create the 843 01:20:46,000 --> 01:20:51,520 tuple, it's, it's as it is, it can't change. So I could come down here and I can print out 844 01:20:52,080 --> 01:20:59,680 some of the attributes inside the tuple. And the way I can access them is just by using an open 845 01:20:59,680 --> 01:21:07,200 enclosed square bracket. And in here, I can pass in a index. So tuples are indexed starting at zero. 846 01:21:07,200 --> 01:21:13,920 So technically, we would say that four is at index position one and sorry, four is at index 847 01:21:13,920 --> 01:21:20,320 position zero and five is at index position one. So if I said coordinate zero, now it should print 848 01:21:20,320 --> 01:21:27,440 out four. And down here, we're printing out four. So if I wanted to print out that five, 849 01:21:27,440 --> 01:21:32,160 we can just put a one inside of those square brackets. And now we'll print out that five. So I can 850 01:21:32,160 --> 01:21:39,680 access the elements inside of a tuple, I can easily access each, each entry. But if I was to come 851 01:21:39,680 --> 01:21:46,560 over here and try to change one of the elements, so if I said coordinates, and coordinates one, 852 01:21:46,560 --> 01:21:52,640 and I tried to set it equal to something else like 10, Python's actually going to give us an 853 01:21:52,640 --> 01:21:56,800 error when we run this program. So I'm going to run the program, you see we get this error, it says, 854 01:21:57,600 --> 01:22:05,440 type error tuple object does not support item assignment, because it's immutable, because we can't 855 01:22:05,440 --> 01:22:12,000 change it. So that's basically what tuples are how we can create them. And you know, a tuple, 856 01:22:12,000 --> 01:22:16,800 like I said, it's very simple, you just create it, and then you have it. And now I want to talk to 857 01:22:16,800 --> 01:22:23,520 you guys about a common topic in Python, which is the difference between tuples and lists. So 858 01:22:23,520 --> 01:22:28,000 remember, a list is basically doing the same thing a list. If we wanted to create a list, we would 859 01:22:28,000 --> 01:22:34,160 just use square brackets instead of those parentheses. And if I was using a list, I could assign 860 01:22:34,160 --> 01:22:39,680 different values to it, I could I could basically mutate any of the elements I could add delete, 861 01:22:40,640 --> 01:22:46,480 modify change, whatever I wanted with the list. But with a tuple, we can't do that. And that's 862 01:22:46,480 --> 01:22:51,920 sort of the basic difference. And really, Python doesn't say anything more than that. It basically 863 01:22:51,920 --> 01:22:57,840 just says like one of them's immutable, one of them's not. But in practical use cases, generally, 864 01:22:57,840 --> 01:23:04,080 people will use tuples for data that's never going to change. So you'll you'll store data inside of 865 01:23:04,080 --> 01:23:09,680 a tuple, when that data doesn't need to be mutated. That's why something like coordinates 866 01:23:09,680 --> 01:23:14,400 is such a good candidate for a tuple, because once you have coordinates, like, we're probably 867 01:23:14,400 --> 01:23:19,440 not going to modify them, right, we might add other coordinates. So for example, if I wanted, 868 01:23:19,440 --> 01:23:27,840 I could create a list of tuples. So I can say like 4567, maybe we have some other coordinates, 869 01:23:27,840 --> 01:23:35,440 like 8034. So here I have, you know, I have a list and inside of it, we have these tuples. 870 01:23:36,800 --> 01:23:42,080 But remember, these tuples can't be modified, the values can't be changed. So really, it's just a 871 01:23:42,080 --> 01:23:47,840 case by case basis, I would say by a huge majority, you're going to be using lists most of the time. 872 01:23:48,800 --> 01:23:55,280 Tuples are more of a niche, you know, they're using more special situations. But there are 873 01:23:55,280 --> 01:23:59,040 plenty situations where you want to use tuples. So if you're in a situation where you want to store 874 01:23:59,040 --> 01:24:04,960 data that can't be changed or mutated, then tuples are the way to go. So yeah, that's just some basics 875 01:24:04,960 --> 01:24:10,480 about tuples, some basics about the difference between lists and tuples. And so a tuple is a 876 01:24:10,480 --> 01:24:20,560 great thing to add into your Python repertoire. In this tutorial, I want to talk to you guys about 877 01:24:20,560 --> 01:24:27,120 using functions in Python. Now a function is basically just a collection of code, 878 01:24:27,120 --> 01:24:33,120 which performs a specific task. So I can take a bunch of lines of code that are basically doing 879 01:24:33,120 --> 01:24:39,200 one thing, I can put them inside of a function. And then when I wanted to do that task or do that 880 01:24:39,200 --> 01:24:45,200 one thing that the function was doing, I can just call the function. And so functions really 881 01:24:45,200 --> 01:24:50,080 help you to organize your code a lot better, they allow you to kind of break up your code into 882 01:24:50,080 --> 01:24:54,560 different, you know, little chunks that are doing different things. And they're really just awesome. 883 01:24:54,560 --> 01:25:00,400 So functions are like a very core concept when we're talking about programming in Python. So I'm 884 01:25:00,400 --> 01:25:05,040 going to show you guys how to create an awesome function today. Let's say for the purposes of 885 01:25:05,040 --> 01:25:11,440 this tutorial, we want to create a function that says hi to the user. So the one task that our 886 01:25:11,440 --> 01:25:17,040 function performs is basically just saying hi to whoever is writing the program. So over here, 887 01:25:17,920 --> 01:25:22,800 if I want to write a function, the first thing I have to use is a keyword in Python, 888 01:25:22,800 --> 01:25:29,680 it's called death. So D E F. And basically, when Python sees this keyword, it's going to say, 889 01:25:29,680 --> 01:25:36,640 okay, this person wants to use a function. So after we type out death, we need to give this 890 01:25:36,640 --> 01:25:43,200 function a name. So just like when we are creating variables, we give them descriptive names, 891 01:25:43,200 --> 01:25:49,200 we also want to do the same thing with functions. So if I'm creating a function, I can just give 892 01:25:49,200 --> 01:25:53,760 it a specific name, which basically says like what it's doing. So we're going to create a function 893 01:25:53,760 --> 01:26:00,720 that says hi. So I'm just going to call this function, say hi, just like that. And once we type 894 01:26:00,720 --> 01:26:06,720 out the name of the function, and so also I could type out say hi with no underscore or I can type 895 01:26:06,720 --> 01:26:13,120 out say hi with an underscore, both are considered like good practices in Python. A lot of times, 896 01:26:13,120 --> 01:26:17,600 if you just have a simple like two word function like this, you don't need to put an underscore, 897 01:26:17,600 --> 01:26:22,080 but we could put an underscore there. If we wanted, after we type out the name of the function, 898 01:26:22,080 --> 01:26:28,160 I'm just going to type an open and close parentheses. And then I'm going to type a colon. And basically, 899 01:26:28,160 --> 01:26:34,640 what this is doing is it's saying to Python, like, all right, all the code that comes after this line 900 01:26:34,640 --> 01:26:41,280 is going to be inside of our function. And in order to write code that's going to end up being 901 01:26:41,280 --> 01:26:47,120 inside the function, we actually have to indent it. So over here, you'll notice that when I clicked 902 01:26:47,120 --> 01:26:54,240 enter, my text editor automatically indented the text. So it's automatically like, using this indent 903 01:26:54,240 --> 01:26:59,920 here. And that's kind of like one of the rules in Python is like, the code that goes inside of 904 01:26:59,920 --> 01:27:06,640 this function needs to be indented. So if I was to write some code, like out here, this is no longer 905 01:27:06,640 --> 01:27:11,040 going to be considered inside the function. So you can see, as I type out code, and obviously, 906 01:27:11,040 --> 01:27:17,120 this isn't real code, but as I type text over here, that's indented, this little like marker over 907 01:27:17,120 --> 01:27:22,560 here is basically saying like, Oh yeah, that's inside the function. But then when I write code 908 01:27:22,560 --> 01:27:28,560 over here, that is like not at the same indentation level as this stuff, it's no longer considering 909 01:27:28,560 --> 01:27:33,600 it inside the function. So that's just a little thing. Any code inside this function needs to be 910 01:27:33,600 --> 01:27:39,360 indented. Alright, so our function is just going to say hi to the user. So I'm just going to have it 911 01:27:39,360 --> 01:27:47,440 print out some text, it's going to say hello, user. So this is a very simple function. Obviously, 912 01:27:47,440 --> 01:27:51,600 we just have one line of code and inside of a function, you could have, you know, as many lines 913 01:27:51,600 --> 01:27:56,880 of code as you want. But for our purposes, we only need one line in order to perform our function. 914 01:27:57,440 --> 01:28:03,680 So now all we have to do is call this function. So if I want to execute the code inside of this 915 01:28:03,680 --> 01:28:09,440 function, I have to do something called calling it. So if I was to just run my program as it is 916 01:28:09,440 --> 01:28:15,760 right now, I'm just going to run it, you'll see that nothing happens over here, right? Even though 917 01:28:15,760 --> 01:28:21,200 this function is printing out hello user, when I run the program, it's not doing it. And that's 918 01:28:21,200 --> 01:28:27,200 because the code inside of a function isn't going to get executed by default. The code inside of 919 01:28:27,200 --> 01:28:32,640 a function is only going to get executed when we specify that we want to execute it. And in order 920 01:28:32,640 --> 01:28:36,640 to do that, we're going to have to do something called calling the function. So in order to call 921 01:28:36,640 --> 01:28:41,760 the function, you basically just type out the functions name and those open and close parentheses. 922 01:28:41,760 --> 01:28:48,560 So I'm just going to type out say hi, and open and close parentheses. And now when we run this 923 01:28:48,560 --> 01:28:54,720 program again, you'll see that it prints out hello user. So we're executing the code inside of the 924 01:28:54,720 --> 01:28:59,760 function. And I want to show you guys just one more thing here, just talking to you guys about 925 01:28:59,760 --> 01:29:05,680 how these functions actually work. So up here, I'm going to print top. And then down here, I'm 926 01:29:05,680 --> 01:29:12,320 going to print bottom. So I want to show you guys the flow of these functions inside the program. So 927 01:29:12,320 --> 01:29:20,160 when I run this program, you'll see we print out top hello user, and then bottom. So essentially, 928 01:29:20,160 --> 01:29:25,920 what's happening is when Python goes through and executes this program, it goes over here to 929 01:29:25,920 --> 01:29:31,360 this first line, it says, okay, we want to print out the word top. And then it goes down here and 930 01:29:31,360 --> 01:29:38,240 it says, okay, we want to execute the say hi function. So Python actually jumps up, and it goes 931 01:29:38,240 --> 01:29:44,240 over to this say hi function, and it's going to execute all of the code inside of this function. 932 01:29:44,240 --> 01:29:49,440 So it's going to go through execute all this code. And then once it's done executing all the code 933 01:29:49,440 --> 01:29:53,840 in the function, it's going to jump back down here, and it's going to move on to the next line, 934 01:29:53,840 --> 01:29:58,800 which is bottom. So that's kind of like the flow of functions. Again, with functions generally, 935 01:29:58,800 --> 01:30:06,000 when we're naming these functions, you want them to be named in all lowercase. And usually, 936 01:30:06,000 --> 01:30:11,200 when we're naming stuff in Python, if there's two or more words, we're going to use an underspace 937 01:30:11,200 --> 01:30:17,280 or an underscore in between them. So I could write this out as say underscore high. But in a lot 938 01:30:17,280 --> 01:30:21,760 of situations, though, if I have a function like this where the name is really short, I might just 939 01:30:21,760 --> 01:30:25,280 be easier to leave it without an underscore, but why don't we just put an underscore in there just 940 01:30:25,280 --> 01:30:32,960 to be a super Python official. All right, so now we can actually make these functions a little bit 941 01:30:32,960 --> 01:30:38,560 more powerful. And what we can do is we can give them information. So a lot of times when we write 942 01:30:38,560 --> 01:30:44,320 a function, we're going to want to have additional information that gets passed in. And these are 943 01:30:44,320 --> 01:30:52,080 called parameters. So a parameter is a piece of information that we give to the function. So over 944 01:30:52,080 --> 01:30:57,600 here, I can actually specify that this function needs to receive some parameters. So I can basically 945 01:30:57,600 --> 01:31:02,240 say like, Hey, if you're going to call this function, you need to give us some information, 946 01:31:02,240 --> 01:31:06,800 you need to give us some parameters. And all I have to do to do that is just type out the name 947 01:31:06,800 --> 01:31:13,600 of the parameter that I want to receive. So why don't we allow the code calling this function to 948 01:31:13,600 --> 01:31:19,680 tell it what name to say hi to up here, I can just say name. And basically, what this means is it 949 01:31:19,680 --> 01:31:26,000 means whenever I call this say hi function, we have to give it a name. So down here, if I was to call 950 01:31:26,000 --> 01:31:31,920 this, I have to include a name in here. So I can say like, Mike, and what we can do now is we can 951 01:31:31,920 --> 01:31:38,720 actually access this parameter or this variable inside of our function. So I could come over here 952 01:31:38,720 --> 01:31:45,200 and instead of saying hello user, I could say hello name. And basically, what this is going to do is 953 01:31:45,200 --> 01:31:51,120 it's just going to say hello to whatever name got passed in here. So I'm actually going to copy this 954 01:31:51,120 --> 01:31:59,760 and we will do this twice. So I'll say hello, Mike hello, Steve. And now when I run this program, 955 01:31:59,760 --> 01:32:06,080 you'll see that instead of just saying hello user, it's saying hello to whichever name I passed 956 01:32:06,080 --> 01:32:10,640 into the function. So that's why this can be really useful, right? We can give the function 957 01:32:10,640 --> 01:32:15,440 information, and depending on the information we give it, it'll perform its task a little bit 958 01:32:15,440 --> 01:32:20,800 differently. I could also include more than one parameter. So you can, I mean, technically, you 959 01:32:20,800 --> 01:32:25,360 could have like as many parameters as you want. So I can put another one in here, we can say age. 960 01:32:25,360 --> 01:32:31,840 And now I'm going to have to pass in an age along with these. So I'm just going to pass in age and 961 01:32:31,840 --> 01:32:40,400 pass in an age for down here. And I'm just going to pass in strings. So we can say hello name. 962 01:32:43,280 --> 01:32:50,560 You are age. So I'm passing in two pieces of information. And now when we run this program, 963 01:32:50,560 --> 01:32:54,960 it's going to call the function. And it's going to use both of those pieces of information. So I 964 01:32:54,960 --> 01:33:02,640 say hello, Mike, you are 35. Hello, Steve, you are 70. So essentially, we're writing out this one line 965 01:33:02,640 --> 01:33:08,720 of code, which just prints out like hello to someone. And we're allowing this function to receive 966 01:33:08,720 --> 01:33:13,920 two parameters. So the name and the age, and depending on the name and the age, the function's 967 01:33:13,920 --> 01:33:19,600 going to print out hello a little bit differently. And that's kind of the beauty of using function. 968 01:33:19,600 --> 01:33:26,080 So you could pass anything you want into a function. So for example, I could pass in a integer instead 969 01:33:26,080 --> 01:33:31,200 of a string for the age. So like I could pass in the number, the only difference is over here, 970 01:33:31,200 --> 01:33:37,760 we're going to have to convert this into a string. But it's going to work just the same. So you 971 01:33:37,760 --> 01:33:46,080 could pass in strings, numbers, bullions, arrays, you can really pass any type of data into a 972 01:33:46,080 --> 01:33:50,800 function. And it's going to work. So you can see here, we get the same result. So that's the basics 973 01:33:50,800 --> 01:33:56,480 of functions. And as you go through with Python, you're going to be using functions more and more. 974 01:33:56,480 --> 01:34:01,680 And generally, it's a good idea to break your code up into different functions. So whenever you 975 01:34:01,680 --> 01:34:07,600 have like a grouping of code that's designed to perform a specific task, that's usually a good 976 01:34:07,600 --> 01:34:16,000 candidate to be put inside of a function. In this tutorial, I want to talk to you guys about 977 01:34:16,000 --> 01:34:23,200 using the return statement in Python functions. Now Python function is basically just a collection 978 01:34:23,200 --> 01:34:29,920 of Python code that performs a specific task. And when we want to perform that task in our Python 979 01:34:29,920 --> 01:34:36,080 programs, we can call a Python function. And a lot of times when we're calling a Python function, 980 01:34:36,080 --> 01:34:41,120 we just kind of call it and it does its thing. And then we move on with the program. But sometimes 981 01:34:41,120 --> 01:34:47,280 when we call a function, we're actually going to want to get information back from that function. 982 01:34:47,280 --> 01:34:52,160 So when I call the function, I want it to basically execute its task, I want it to execute all the 983 01:34:52,160 --> 01:34:57,040 code. And then I want it to like give me some information back. So that could be like a particular 984 01:34:57,040 --> 01:35:02,560 value, it could be something like telling me how the task went. Basically, the function can just 985 01:35:02,560 --> 01:35:07,840 communicate back to us like, Hey, here's some information. And that's what the return keyword 986 01:35:07,840 --> 01:35:14,000 can allow us to do the return keyword can basically allow Python to return information 987 01:35:14,000 --> 01:35:19,760 from a function. So I'm going to show you guys how we can do this, I want to create a very simple 988 01:35:19,760 --> 01:35:25,680 function. And it's just going to cube a number. So when we cube a number, we take it to the power 989 01:35:25,680 --> 01:35:31,600 of three. So if I was going to cube like two, it'd be like two raised to the power of three. 990 01:35:31,600 --> 01:35:36,640 Right, it's just pretty basic math. So we're going to make a function that will cube a number. 991 01:35:36,640 --> 01:35:41,520 And I'll show you guys how we can use that return statement. So first thing we want to do is make 992 01:35:41,520 --> 01:35:48,000 a function, I'm just going to say death cube, we'll call the function cube. And over here, 993 01:35:48,000 --> 01:35:54,240 we can specify any parameters that this function will take. So basically, any values that we want 994 01:35:54,240 --> 01:35:58,560 to give this function. So we're going to give it one function, which is just going to be a number. 995 01:35:58,560 --> 01:36:03,360 So I'm just going to call it num. And then over here, I'm going to type out a colon. 996 01:36:03,360 --> 01:36:09,600 And then down here, we can put any of the code that we want to be included in this function. 997 01:36:09,600 --> 01:36:15,040 So basically, this function is just going to cube this number. So we could just type out num, 998 01:36:15,040 --> 01:36:22,960 times num, times num. Right, that's going to give us num cubed. So that's all well and good. And 999 01:36:22,960 --> 01:36:28,480 if I come down here, I can execute this function. So I can basically just call it, I'll say cube. 1000 01:36:28,480 --> 01:36:34,000 And why don't we pass it a three? So I'm going to go ahead and pass it to three. And I'm going 1001 01:36:34,000 --> 01:36:43,760 to save this. And now let's go ahead and run this code. So we're on the code. And you'll see down 1002 01:36:43,760 --> 01:36:50,800 here, nothing happened, right? So nothing got printed out in our program. In fact, when we call 1003 01:36:50,800 --> 01:36:56,160 this cube function, it didn't really do anything. So let me try to print this out. So maybe if we 1004 01:36:56,160 --> 01:37:01,920 print this function out, it'll give us back the information that we want. So type out print, 1005 01:37:01,920 --> 01:37:06,880 and we'll print out the value of the function, I'm just going to click play. You look down here, 1006 01:37:06,880 --> 01:37:14,320 it says none. So when I printed out the value of this function, it told me none. But really, 1007 01:37:14,320 --> 01:37:19,840 this function is supposed to be cubing the number. And here's how we can use the return statement 1008 01:37:19,840 --> 01:37:26,240 in Python. I can come over here and I can just say return. And now Python is going to return 1009 01:37:26,240 --> 01:37:32,160 whatever value we put over here to the right. So when I call this cube function, and I give it a 1010 01:37:32,160 --> 01:37:37,600 three, Python is going to go off, it's going to execute this function. When it sees this return 1011 01:37:37,600 --> 01:37:43,600 statement, it's going to say, okay, I want to give a value back to whatever call of this function. 1012 01:37:43,600 --> 01:37:50,960 And it's going to return this value. Now, when I print out cube three, it should print out the 1013 01:37:50,960 --> 01:37:58,080 answer because we get an answer back. So when I play this, you'll see down here, it says 27. So 1014 01:37:58,080 --> 01:38:02,560 we got the correct answer. So I could do this with anything, why don't we cube like four or 1015 01:38:02,560 --> 01:38:09,040 something? So now we're on this. And you can see we're constantly getting the correct answer back. 1016 01:38:09,040 --> 01:38:15,120 So that's the basics of using this return statement, it allows us to return a value back to the 1017 01:38:15,120 --> 01:38:20,000 caller. In other words, back to whatever's calling the function. So I just want to illustrate this 1018 01:38:20,000 --> 01:38:23,920 point a little bit further, I'm going to come up here, I'm actually going to create a variable. 1019 01:38:23,920 --> 01:38:30,400 So I'm going to create a variable called result. And I'm just going to set it equal to cube four. 1020 01:38:30,400 --> 01:38:37,760 This variable result is going to store the value that gets returned from the cube function. So it's 1021 01:38:37,760 --> 01:38:43,040 not going to store like cube four, it's going to store the value that gets returned from executing 1022 01:38:43,040 --> 01:38:47,840 that function. So now if I came down here and printed out result, it's actually just going to 1023 01:38:47,840 --> 01:38:53,120 print out 64 again, because that's the value that it's storing. So this return statement can be 1024 01:38:53,120 --> 01:38:58,320 really useful for like getting information back from a function, we can use something like a 1025 01:38:58,320 --> 01:39:03,920 parameter, like this four as a parameter, and we can give information to a function. And then we 1026 01:39:03,920 --> 01:39:09,920 can use the return statement to get information back from a function. And I do just want to point 1027 01:39:09,920 --> 01:39:16,160 out one more thing, I'm actually not able to put any code after this return statement. So if I 1028 01:39:16,160 --> 01:39:21,760 came down here and tried to put like a print statement, this actually isn't going to work. So this is 1029 01:39:21,760 --> 01:39:27,680 never going to get printed out. So if I just typed out like code right here, when we run this program, 1030 01:39:27,680 --> 01:39:35,280 it's not going to type out code. That's because when I use this return keyword, it breaks us out 1031 01:39:35,280 --> 01:39:40,080 of the function. So Python's going to go through, it's going to execute the code in the function, 1032 01:39:40,080 --> 01:39:44,800 whenever it sees this return keyword, it's just going to break back out and we're going to be 1033 01:39:44,800 --> 01:39:50,960 done. So we'll never be able to reach this line of code. So that's the basics of using return 1034 01:39:50,960 --> 01:39:56,320 statements. And you can return any data type you want, we could easily return like a string or a 1035 01:39:56,320 --> 01:40:01,840 Boolean or an array, it doesn't really matter, you can return whatever you want. And a lot of times 1036 01:40:01,840 --> 01:40:05,440 in a lot of situations, you're going to want to get a value back from a function. 1037 01:40:09,440 --> 01:40:15,360 In this tutorial, I want to talk to you guys about using if statements in Python. Now if statements 1038 01:40:15,360 --> 01:40:22,400 are a special structure in Python, where we can actually help our programs to make decisions. So 1039 01:40:22,400 --> 01:40:28,080 by using an if statement, I could execute certain code when certain conditions are true. And I can 1040 01:40:28,080 --> 01:40:34,320 execute other code when other conditions are true. So basically if statements allow our programs to 1041 01:40:34,320 --> 01:40:40,800 respond to the input that they're given. So depending on the data that we're using in the program, 1042 01:40:40,800 --> 01:40:46,080 our programs will be able to respond. So when certain data is certain values, we can do certain 1043 01:40:46,080 --> 01:40:51,280 things. And when other data is other values, we can do other things. So you know, your programs 1044 01:40:51,280 --> 01:40:57,680 are essentially becoming a lot smarter. And actually, if statements are things that we as human beings 1045 01:40:57,680 --> 01:41:02,640 deal with every day. So you know, as you go throughout your day, generally, you're dealing with if 1046 01:41:02,640 --> 01:41:07,120 statements all the time. So I want to kind of give you guys a brief introduction into if statements, 1047 01:41:07,120 --> 01:41:11,040 and we'll talk about some of the common if statements that you'd see in everyday life. 1048 01:41:11,040 --> 01:41:15,760 And then we'll go ahead and do a really awesome example to kind of like show you guys what if 1049 01:41:15,760 --> 01:41:21,280 statements can do for us. So over here in my text editor, I just have this little text file. 1050 01:41:21,280 --> 01:41:27,040 And in here, I wrote out a bunch of if statements that a lot of people will encounter on a daily 1051 01:41:27,040 --> 01:41:34,560 basis. So here's the first one, it says I wake up, if I'm hungry, I eat breakfast. So let's break 1052 01:41:34,560 --> 01:41:40,560 this down a little bit up here, it says if I'm hungry. And this is a condition, right? This is 1053 01:41:40,560 --> 01:41:46,960 either going to be true, or it's going to be false, right? If it's true, I'm going to go ahead and 1054 01:41:46,960 --> 01:41:53,440 eat breakfast, right? If it's false, I'm just going to skip whatever was in here and move on, 1055 01:41:53,440 --> 01:41:58,080 right? So that's the basics of an if statement, right? We have a condition, in this case, if I'm 1056 01:41:58,080 --> 01:42:04,240 hungry, if the condition is true, then we do the action. So we eat breakfast. Otherwise, 1057 01:42:04,240 --> 01:42:11,280 we just skip it. So down here, we have another one, it says I leave my house. If it's cloudy, 1058 01:42:11,920 --> 01:42:18,160 I bring an umbrella. Otherwise, I bring sunglasses. And so this one is a little bit more complex than 1059 01:42:18,160 --> 01:42:22,480 the one we just looked at. You'll see we have our condition, right? It says if it's cloudy, 1060 01:42:23,200 --> 01:42:28,720 if that condition's true, we're going to bring the umbrella. But in this case, if the condition's 1061 01:42:28,720 --> 01:42:34,160 false, there's something else that we want to do. So in the case up here, if this condition was 1062 01:42:34,160 --> 01:42:39,680 false, we just moved on. But now if this condition's false, then we're going to go ahead and bring 1063 01:42:39,680 --> 01:42:44,960 sunglasses. So that's another example of an if statement, it's a little bit more complex. 1064 01:42:44,960 --> 01:42:49,360 Finally, I have one more down here, and this one's even more complex, it says I'm at a restaurant. 1065 01:42:50,000 --> 01:42:56,720 If I want meat, I order a steak. So if this condition's true, we order the steak. But if this 1066 01:42:56,720 --> 01:43:02,000 condition is false, then we're going to move on, and we're actually going to check another condition. 1067 01:43:02,000 --> 01:43:08,080 So I'm going to say otherwise, if I want pasta, I order spaghetti and meatballs. This is another 1068 01:43:08,080 --> 01:43:14,160 condition. If this condition's true, we get the spaghetti. If it's not true, then finally, 1069 01:43:14,160 --> 01:43:20,160 we're going to move on down here, and we'll just default to ordering the salad. So all three of 1070 01:43:20,160 --> 01:43:25,840 these if statements are valid if statements, it's just they get more and more complex as we go down 1071 01:43:25,840 --> 01:43:30,320 the list. So hopefully that's, you know, it can kind of wrap your head around what if statements 1072 01:43:30,320 --> 01:43:35,120 are we're basically specifying conditions. If those conditions are true, we're going to do certain 1073 01:43:35,120 --> 01:43:40,960 things. And if those conditions are false, we can do other things, or we can check other conditions. 1074 01:43:40,960 --> 01:43:45,040 All right, so let's head over to our Python file, and we'll start writing some actual Python. 1075 01:43:45,920 --> 01:43:51,520 So in here in my app dot Python file, I want to create an if statement. And I'm going to show 1076 01:43:51,520 --> 01:43:56,000 you guys a very basic if statement in this tutorial. And then in the next tutorial, we're going to 1077 01:43:56,000 --> 01:44:02,720 look at some more advanced if statements. And so what I want to do is create a Boolean variable. 1078 01:44:02,720 --> 01:44:07,760 So I'm going to create a Boolean variable that's going to store whether or not the user is a mail. 1079 01:44:07,760 --> 01:44:13,520 So it's just going to be called is underscore mail. And I'm going to set this equal to true, 1080 01:44:13,520 --> 01:44:19,120 because I'm a male. So we have this Boolean variable, right? And right now we have it set 1081 01:44:19,120 --> 01:44:25,920 equal to true. I can actually use an if statement to check to see what the value of this variable 1082 01:44:25,920 --> 01:44:31,120 is. And if it's true, I can do something. So if I want to use an if statement, all I have to do 1083 01:44:31,120 --> 01:44:38,400 is just type out if, and then I just need to type out a condition. So basically, I would have to type 1084 01:44:38,400 --> 01:44:44,480 out like something that's going to be true or false. Like, for example, in the other page, we 1085 01:44:44,480 --> 01:44:50,240 were specifying like, if I'm hungry, or if it's cloudy, here, we're just going to check to see if 1086 01:44:50,240 --> 01:44:57,680 is mail. So in other words, if the person is male. And so remember, this has to be a, like, it has to 1087 01:44:57,680 --> 01:45:03,680 be able to be reducible to a true or a false value. So in our case, we're just using a Boolean, 1088 01:45:03,680 --> 01:45:10,320 because that's like the easiest thing we can do. So I said if I specified my true or false condition, 1089 01:45:11,120 --> 01:45:16,800 and now I want to just type a colon. And what I want to do now is make a new line. And so 1090 01:45:16,800 --> 01:45:24,320 anything that I put below this with an indentation is actually going to be executed when that 1091 01:45:24,320 --> 01:45:30,480 conditions true. So I could put print. And down here, I could just put you are a male. 1092 01:45:32,000 --> 01:45:36,000 And now you'll see what happens when I run this program, it's going to go down here, 1093 01:45:36,000 --> 01:45:42,560 and it's going to say you are a male. But if I changed is mail to false. So instead of this 1094 01:45:42,560 --> 01:45:47,760 being true, I just change it to false. Now all of a sudden it's not going to print anything. So 1095 01:45:47,760 --> 01:45:53,760 it's just going to be blank down here. That's because this condition wasn't true. So anything 1096 01:45:53,760 --> 01:46:01,120 that I put below this if declaration and that has an indentation like that is going to be executed 1097 01:46:01,120 --> 01:46:08,080 when the if statement is true. We can also use another keyword in Python, which is called else. 1098 01:46:08,080 --> 01:46:14,480 And else is basically going to be like otherwise. So you remember before we said, if it's cloudy, 1099 01:46:14,480 --> 01:46:19,760 I'll bring an umbrella. Otherwise, I'll bring sunglasses. Well, this is going to be like our 1100 01:46:19,760 --> 01:46:26,480 otherwise. So I can say down here else. And now anything that I put below else is going to get 1101 01:46:26,480 --> 01:46:31,520 printed out when that condition isn't true. So why don't we just put you are not a male. 1102 01:46:32,480 --> 01:46:37,680 So now we're able to cover both of the situations for this variable. If it's false, 1103 01:46:37,680 --> 01:46:42,560 we can cover that situation. So it says you are not a male. If it's true, 1104 01:46:43,760 --> 01:46:48,720 we can cover that situation as well. So it'll say you are a male. So that's pretty cool. 1105 01:46:49,600 --> 01:46:54,800 But we can also make these more complex. And actually before I talk about that, 1106 01:46:54,800 --> 01:46:58,880 you can put as much code as you want inside of an if statement. So I could have like, 1107 01:46:58,880 --> 01:47:03,920 you know, dozens of lines of code if I wanted. I'm just giving you guys a very simple example. 1108 01:47:03,920 --> 01:47:09,760 So why don't we make this more complex? And I'm going to add another variable into the mix. So 1109 01:47:09,760 --> 01:47:14,640 I'm going to add another, you know, Boolean attribute into this mix. I'm going to make another variable 1110 01:47:14,640 --> 01:47:22,080 called is tall. And we're going to set this equal to true as well. So now I have two variables in 1111 01:47:22,080 --> 01:47:28,240 the mix. Let's say I wanted to write an if statement that will check both of those variables. So I 1112 01:47:28,240 --> 01:47:32,960 could write an if statement that's like, if they're male and they're tall, or if they're male and 1113 01:47:32,960 --> 01:47:37,680 they're not tall, or if they're not male and they're tall, like I could write out a bunch of if 1114 01:47:37,680 --> 01:47:44,560 statements to check the combination of these variables. And so the way I can do that is by using 1115 01:47:44,560 --> 01:47:51,360 one of two keywords. So the first keyword I want to show you guys is the or keyword. So I can come 1116 01:47:51,360 --> 01:47:59,360 over here and I can say if is male, or is underscore tall. And what this is basically going to do is 1117 01:47:59,360 --> 01:48:07,280 it's going to say if the person is either male, or if they're tall, then we want to do something. So 1118 01:48:08,240 --> 01:48:14,400 this is only going to be true when one of these values is true, when one or both of those values 1119 01:48:14,400 --> 01:48:21,200 is true. So down here, we could actually put instead of you are a male, we could put you are a male 1120 01:48:21,200 --> 01:48:27,680 or tall or both, that's going to be more appropriate. And then down here, we would say you are 1121 01:48:29,920 --> 01:48:36,480 neither male nor tall. And that's because if we execute the code inside of this else, 1122 01:48:36,480 --> 01:48:42,320 that means that both of these guys are false. Right, so I'm saying if they're male or they're tall, 1123 01:48:42,320 --> 01:48:48,320 we're going to execute whatever is in here. Otherwise, that means they're neither male nor tall. We're 1124 01:48:48,320 --> 01:48:53,320 just going to execute this down here. So let me show you guys how this works. I'll click play. And 1125 01:48:53,320 --> 01:48:59,320 these are both true. So it's going to say you are a male or tall or both. If I was to set one of 1126 01:48:59,320 --> 01:49:06,320 these to false. So let's set is male equal to false. Now, it's still going to say you are a male 1127 01:49:06,320 --> 01:49:13,320 or tall or both. But if I set them both equal to false, now it's going to say you're neither 1128 01:49:13,320 --> 01:49:21,320 male nor tall. So that's kind of the basics of using the or operator. And there's another operator 1129 01:49:21,320 --> 01:49:27,320 we can use which is called and, and basically it's similar to or except both of these conditions 1130 01:49:27,320 --> 01:49:34,320 have to be true. So this is basically saying if the person is male and they're also tall, 1131 01:49:34,320 --> 01:49:39,320 then we're going to execute whatever is in here. So we could actually change this text to say you 1132 01:49:39,320 --> 01:49:46,320 are a tall male, because we know for a fact that if this is getting executed, they're both male 1133 01:49:46,320 --> 01:49:54,320 and tall. We can also change this print statement down here. This could say you are either not male 1134 01:49:54,320 --> 01:50:02,320 or not tall or both. So you can see it's basically just saying that you're either not one of them 1135 01:50:02,320 --> 01:50:08,320 or you're not both of them. So we click the play button and you'll see because they're both false, 1136 01:50:08,320 --> 01:50:14,320 it's going to say you are either not male or not tall or both. But if I make them both true, 1137 01:50:14,320 --> 01:50:21,320 now it's going to say you are a tall male. If I was going to make one of these false, 1138 01:50:21,320 --> 01:50:27,320 though, so let's make is tall false. Now it's not going to execute this first print statement. 1139 01:50:27,320 --> 01:50:32,320 It's going to execute the one inside of the else. So to say you are either not male or not tall 1140 01:50:32,320 --> 01:50:39,320 or both. So that's the basics of and an or and you'll be using those a lot as you use your if 1141 01:50:39,320 --> 01:50:45,320 statements. But there's actually one more thing we can do. We can add in some more conditions. 1142 01:50:45,320 --> 01:50:51,320 So up here I'm checking if they're male and they're tall. But what if I wanted to check if they're 1143 01:50:51,320 --> 01:50:58,320 and they're not tall? So if they're like a short male, I can use another keyword in Python called 1144 01:50:58,320 --> 01:51:05,320 else if or really it's just typed out E L I F and it stands for else if and after this we can put 1145 01:51:05,320 --> 01:51:11,320 another condition. So over here I want to check to see if they're male and they're not tall. 1146 01:51:11,320 --> 01:51:19,320 So I'm going to say else if male is male and now I can type in not tall and the way I can do that 1147 01:51:19,320 --> 01:51:25,320 is by saying not an open and close parentheses and saying is tall. And basically when I use this 1148 01:51:25,320 --> 01:51:32,320 not function it's going to negate whatever is inside of here. So if this was true it's going to make 1149 01:51:32,320 --> 01:51:37,320 it false and if this was false it's going to make it true. So basically that'll tell us whether or 1150 01:51:37,320 --> 01:51:44,320 not they're tall. And then I'm going to use a colon and now down here inside of here we're basically 1151 01:51:44,320 --> 01:51:50,320 just going to type out any of the code that we want to happen when this condition's true. So down 1152 01:51:50,320 --> 01:51:58,320 here we can just print out you are a short male and there's actually one more condition that we want 1153 01:51:58,320 --> 01:52:05,320 to check for which would be if they're not male and they're tall. So we can make one more else if 1154 01:52:05,320 --> 01:52:13,320 and I'm actually just going to copy this guy and we'll paste it down here. And so I want to say if 1155 01:52:13,320 --> 01:52:23,320 not is male and is tall. So now we're checking to see if they're male and or if they're not male 1156 01:52:23,320 --> 01:52:33,320 and they're tall. So here we could say you are a you are not a male but are tall. So essentially 1157 01:52:33,320 --> 01:52:39,320 here we have an if statement that's going to cover every single condition for these two variables. So 1158 01:52:39,320 --> 01:52:44,320 in every single situation of the two values of these variables we're going to be able to print 1159 01:52:44,320 --> 01:52:50,320 something out for it. So if I made both of these true it's basically going to tell us you are a 1160 01:52:50,320 --> 01:52:59,320 tall male. If I make is male false it's going to tell us you are not a male but are tall. If I made is 1161 01:52:59,320 --> 01:53:08,320 male true and is tall false then it's going to tell us you are a short male. And if I made both of 1162 01:53:08,320 --> 01:53:14,320 these false it's going to tell us you are either not male or not tall or both. And actually we need to 1163 01:53:14,320 --> 01:53:22,320 change this. So this should say you are not a male and not tall. So that would be a better print out 1164 01:53:22,320 --> 01:53:31,320 for that situation. So you can basically see how we can use if else ifs and else in order to help our 1165 01:53:31,320 --> 01:53:37,320 program to make decisions and really to respond to the different data in our programs. So I'm able 1166 01:53:37,320 --> 01:53:44,320 to respond to every combination of the values for these two variables. And that's really cool. So in 1167 01:53:44,320 --> 01:53:50,320 this example we were just using simple Boolean variables but in the next tutorial I'm going to show you 1168 01:53:50,320 --> 01:53:55,320 guys how you can actually use something called comparison operators. So we could compare like two 1169 01:53:55,320 --> 01:54:01,320 numbers or two strings and we could use those as our conditions. But for now I think this is a good 1170 01:54:01,320 --> 01:54:11,320 introduction into if statements. So I'll see you guys in the next tutorial. In this tutorial I want 1171 01:54:11,320 --> 01:54:16,320 to talk to you some more about if statements in Python. More specifically we're going to be looking 1172 01:54:16,320 --> 01:54:22,320 at using comparisons inside of our if statements. So if you're following along with the course in 1173 01:54:22,320 --> 01:54:27,320 the last tutorial I talked to you guys about just the basics of if statements and we used a couple 1174 01:54:27,320 --> 01:54:32,320 different Boolean variables. And depending on the values of those bullions we were able to do certain 1175 01:54:32,320 --> 01:54:37,320 things. In this tutorial we're going to look at another way to use if statements which is with 1176 01:54:37,320 --> 01:54:44,320 comparisons. So instead of just using Boolean values we can actually compare different values. So I 1177 01:54:44,320 --> 01:54:49,320 could compare a couple numbers or I could compare a couple strings. And depending on the results of 1178 01:54:49,320 --> 01:54:55,320 those comparisons we can do certain things. So this is going to be pretty cool. And this is really 1179 01:54:55,320 --> 01:55:01,320 going to give you guys a full understanding of what you can do with if statements. So for this 1180 01:55:01,320 --> 01:55:07,320 tutorial I actually want to create a Python function. And this function is going to give us the 1181 01:55:07,320 --> 01:55:14,320 maximum number that we pass into it. So this function is going to take three parameters as input and 1182 01:55:14,320 --> 01:55:20,320 it's going to print out the biggest number that we give it. So let's create this function. I'm 1183 01:55:20,320 --> 01:55:28,320 just going to say death. And I'm just going to call it max num. And inside of these parentheses I'm 1184 01:55:28,320 --> 01:55:34,320 going to specify that I want three parameters as input. So I'm going to say num one, num two and 1185 01:55:34,320 --> 01:55:42,320 num three. So basically we're passing it three numbers and this function will return the largest 1186 01:55:42,320 --> 01:55:48,320 of the three. So in order to figure out which of these numbers is the largest we're going to have to 1187 01:55:48,320 --> 01:55:55,320 use an if statement. So I can say if and over here I need to specify a condition right so I need to 1188 01:55:55,320 --> 01:56:01,320 put like a true or a false value. And so what we can actually do is we can compare these different 1189 01:56:01,320 --> 01:56:12,320 numbers. So I could say if num one is greater than or equal to num two. And num one is greater 1190 01:56:12,320 --> 01:56:18,320 than or equal to num three. So basically what I'm doing here is I'm comparing these different 1191 01:56:18,320 --> 01:56:23,320 numbers. So I'm saying if the first number is greater than or equal to the second number. And 1192 01:56:23,320 --> 01:56:29,320 this is a comparison. So when you think about it, when we compare these two numbers, we're going to 1193 01:56:29,320 --> 01:56:35,320 end up with a true or a false value. Right, num one is either greater than or equal to num two or 1194 01:56:35,320 --> 01:56:42,320 it's not. It's actually a Boolean value right it's either true or it's false. Same thing over here. 1195 01:56:42,320 --> 01:56:48,320 The result of this comparison is either true or it's false. So technically I'm putting a true or 1196 01:56:48,320 --> 01:56:55,320 false value in there. I'm just getting that true or false value by using a comparison. And you'll 1197 01:56:55,320 --> 01:57:00,320 notice over here we're using something called a comparison operator. And basically this is just 1198 01:57:00,320 --> 01:57:05,320 like how we want to compare them. So we're saying like greater than or equal to in this case. 1199 01:57:05,320 --> 01:57:12,320 So down here, if this whole condition is true, in other words, if the results of these two 1200 01:57:12,320 --> 01:57:18,320 comparisons end up being true, then we know that num one is the biggest. So I can just return 1201 01:57:18,320 --> 01:57:24,320 num one, I can say return num one. But we also want to check some other conditions. So I can 1202 01:57:24,320 --> 01:57:32,320 say L if, and now I want to do some other comparison. So I'm going to say else if num two is greater 1203 01:57:32,320 --> 01:57:39,320 than or equal to num one. And num two is greater than or equal to num three. So I'm doing 1204 01:57:39,320 --> 01:57:44,320 essentially what I did up here, except for num two. So again, I'm comparing these different 1205 01:57:44,320 --> 01:57:50,320 numbers. And this is going to end up being a true or a false value, depending on the result of 1206 01:57:50,320 --> 01:57:57,320 the comparison. So down here, again, we can just return num two, because if all of this is true, 1207 01:57:57,320 --> 01:58:03,320 that means we're going to end up returning num two. And then finally, we can just say else. 1208 01:58:03,320 --> 01:58:09,320 So if num one isn't the biggest and num two isn't the biggest, then we can pretty much 1209 01:58:09,320 --> 01:58:17,320 assume that num three is the biggest. So now we have our function. So down here, I'm actually 1210 01:58:17,320 --> 01:58:21,320 just going to call this function. So I'm just going to say, actually, I'll print out the 1211 01:58:21,320 --> 01:58:28,320 result. So I'll say max num, and we'll pass in like three, four, five. So we're basically 1212 01:58:28,320 --> 01:58:35,320 calling that new function that we just made. And I'm going to go ahead and run this program. 1213 01:58:35,320 --> 01:58:40,320 So you can see down here in the output, we print it out five, because that was the biggest. 1214 01:58:40,320 --> 01:58:45,320 If I was to change this middle one to the biggest, so we make the middle 140, now it'll be able 1215 01:58:45,320 --> 01:58:51,320 to take on that situation. And finally, I can make this like 300. And it'll be able to handle 1216 01:58:51,320 --> 01:58:56,320 that. So no matter which number num one num two or num three ends up being the biggest, 1217 01:58:56,320 --> 01:59:03,320 it's able to tell us without a problem. So these comparisons are a really, really, really 1218 01:59:03,320 --> 01:59:08,320 common way to make if statements. And a lot of times, you're going to want to be comparing 1219 01:59:08,320 --> 01:59:14,320 different values inside of Python. So over here, we're comparing numbers, but we could also 1220 01:59:14,320 --> 01:59:19,320 compare strings. So I could easily make these both like, you know, I could make this like 1221 01:59:19,320 --> 01:59:25,320 dog, and I can make this dog. And I could say a different operator. So I could say like 1222 01:59:25,320 --> 01:59:30,320 this double equal sign would mean equal. So I could basically say like, if this string 1223 01:59:30,320 --> 01:59:35,320 is equal to that string, so you can basically like compare all different data types, you 1224 01:59:35,320 --> 01:59:41,320 know, I could compare numbers, strings, you could also compare bullions. So once again, 1225 01:59:41,320 --> 01:59:46,320 these are comparison operators. And this one is greater than or equal to, but there's a 1226 01:59:46,320 --> 01:59:51,320 bunch of other comparison operators we can use. So the most basic is just going to be 1227 01:59:51,320 --> 01:59:56,320 a double equal sign. And that basically checks to see if the two values are equal. So it'll 1228 01:59:56,320 --> 02:00:03,320 say like, if number one is equal to number two, we can also say not equals. And this 1229 02:00:03,320 --> 02:00:11,320 and this exclamation point will basically mean not equal. So it's if num one is not equal 1230 02:00:11,320 --> 02:00:18,320 to num two, we also have greater than less than greater than or equal to less than or equal 1231 02:00:18,320 --> 02:00:23,320 to. And those are kind of all of the operators are all the, you know, the basic comparison 1232 02:00:23,320 --> 02:00:28,320 operators that you're going to be using in Python. So using comparisons is really awesome. 1233 02:00:28,320 --> 02:00:32,320 And there's going to be tons and tons of situations inside of Python where you're going 1234 02:00:32,320 --> 02:00:40,320 to want to compare different values. So these can be extremely useful. In this tutorial, 1235 02:00:40,320 --> 02:00:45,320 I'm going to talk to you guys about building a calculator in Python. Now, if you've been 1236 02:00:45,320 --> 02:00:49,320 following along with this course, you'll know that in the beginning of the course, we actually 1237 02:00:49,320 --> 02:00:55,320 created a very basic calculator. Basically, the user could just enter in two numbers and 1238 02:00:55,320 --> 02:01:00,320 our calculator would add the numbers together and spit out the answer. In this tutorial, 1239 02:01:00,320 --> 02:01:05,320 I want to show you guys how to build a more advanced calculator. So this calculator is 1240 02:01:05,320 --> 02:01:10,320 going to be able to perform all of the basic arithmetic operations. So we can add, subtract, 1241 02:01:10,320 --> 02:01:15,320 multiply and divide. And we'll actually allow the user to specify which one of those they 1242 02:01:15,320 --> 02:01:21,320 want to do. So the user will enter in a number, they'll enter in whatever operator they want 1243 02:01:21,320 --> 02:01:26,320 to use. So plus minus divide, multiply. And then they'll enter in the second number and 1244 02:01:26,320 --> 02:01:30,320 we'll perform the operation for them. So this can be pretty cool. And it's going to show 1245 02:01:30,320 --> 02:01:36,320 you how we can use if statements in like a practical application. So let's get started. 1246 02:01:36,320 --> 02:01:41,320 The first thing we want to do is get input from the user. So I'm going to create three 1247 02:01:41,320 --> 02:01:46,320 variables, one for the first number, one for the second number, and one for the operator. 1248 02:01:46,320 --> 02:01:51,320 And we'll basically store the values that the user inputs into those variables. So the first 1249 02:01:51,320 --> 02:01:57,320 one will just be num one. And I'm going to set this equal to input. And we're going to 1250 02:01:57,320 --> 02:02:04,320 give this a prompt. So I'll say enter first number. And here's actually the thing is 1251 02:02:04,320 --> 02:02:10,320 whatever the user enters, we're going to want to convert it into a number. So generally in 1252 02:02:10,320 --> 02:02:15,320 Python, when we get input from the user, no matter what they put in, it just gets converted 1253 02:02:15,320 --> 02:02:20,320 into a string. But we don't want a string, because we're going to have to be adding or 1254 02:02:20,320 --> 02:02:25,320 subtracting or multiplying these numbers together. So whenever they enter in the number, 1255 02:02:25,320 --> 02:02:30,320 I just want to immediately convert it to an actual like number data type instead of a 1256 02:02:30,320 --> 02:02:36,320 string. So what I can do is I can actually say float, and I can surround this entire 1257 02:02:36,320 --> 02:02:42,320 input tag with parentheses. And now what this is going to do is it's going to immediately 1258 02:02:42,320 --> 02:02:48,320 convert whatever the user inputs into a float. Now that's going to mean that they're going 1259 02:02:48,320 --> 02:02:52,320 to have to insert a number, otherwise we'll get an error. But for the most part, this should 1260 02:02:52,320 --> 02:02:58,320 work out for us. So I'm actually just going to copy this guy. And we'll go ahead and paste 1261 02:02:58,320 --> 02:03:03,320 it over here. And we'll paste it one more time. So we're basically just trying to get three 1262 02:03:03,320 --> 02:03:08,320 pieces of input from the user. So the second thing we want to get is the operator. So I'm 1263 02:03:08,320 --> 02:03:15,320 just going to say op for operator. And I'll actually get rid of this float, because we 1264 02:03:15,320 --> 02:03:21,320 want to keep this as a string. And I'll just say enter operator. So that's going to be 1265 02:03:21,320 --> 02:03:27,320 like plus minus multiplication, whatever. And then finally down here, we'll get num two. 1266 02:03:27,320 --> 02:03:33,320 And again, we're going to want to convert this to a float. So I'll say enter second number. 1267 02:03:33,320 --> 02:03:37,320 So now basically what we're doing is we're getting the first number, we're getting the 1268 02:03:37,320 --> 02:03:43,320 operator and we're getting the second number from the user. So what we need to do now is 1269 02:03:43,320 --> 02:03:49,320 we need to figure out what is inside of this op. In other words, we need to figure out 1270 02:03:49,320 --> 02:03:54,320 what operator the user was trying to do. So are they trying to do addition or they trying 1271 02:03:54,320 --> 02:04:00,320 to do subtraction like we want to figure that out. So we can use an if statement in order 1272 02:04:00,320 --> 02:04:05,320 to figure that out. So I'm going to come down here and I'm basically just going to create 1273 02:04:05,320 --> 02:04:10,320 an if statement. So I'm going to say if, and we want to check a condition. So I'm basically 1274 02:04:10,320 --> 02:04:18,320 going to check to see if this operator is equal to a plus sign. So I can say if op equals 1275 02:04:18,320 --> 02:04:25,320 plus sign. And now if the operator is equal to plus sign, I'm just going to print out 1276 02:04:25,320 --> 02:04:30,320 num one plus num two. But let's say that instead of a plus sign they entered in something 1277 02:04:30,320 --> 02:04:37,320 else. So we can say out if op is equal to, and now we'll check if it's a minus sign. 1278 02:04:37,320 --> 02:04:44,320 And so here we'll just print num one minus num two. And actually forgot to put a colon 1279 02:04:44,320 --> 02:04:53,320 here. So we can just do these for everything. So now we can say L if op is equal to division 1280 02:04:53,320 --> 02:05:00,320 and here we'll just divide the two numbers so I can print out num one divided by num two. 1281 02:05:00,320 --> 02:05:07,320 And finally, we'll do the same thing for multiplication. So I'll say l if op is equal 1282 02:05:07,320 --> 02:05:16,320 to asterisk. And we'll just print out num one times num two. Alright, so basically over 1283 02:05:16,320 --> 02:05:22,320 here we have every, you know, condition. But I also want to put in an else statement. So 1284 02:05:22,320 --> 02:05:28,320 in the case that the user enters in an operation, that's not like one of the four that we 1285 02:05:28,320 --> 02:05:32,320 specified up here, I just want to throw an error. So I'm just going to say op or actually 1286 02:05:32,320 --> 02:05:40,820 we just say else. And here we'll just print out like invalid operator. Alright, so this 1287 02:05:40,820 --> 02:05:46,320 if statement should give us everything we want. Basically, I'm just checking to see if it's 1288 02:05:46,320 --> 02:05:51,320 equal to a plus sign a minus sign a division sign or a multiplication sign. Depending on 1289 02:05:51,320 --> 02:05:55,320 which one it's equal to, we'll do a different operation. So let's go ahead and run this 1290 02:05:55,320 --> 02:06:01,320 program and see how we do. So I'm going to run this program and it says enter first number. 1291 02:06:01,320 --> 02:06:08,320 Let's enter five, enter an operator. So why don't we just do addition and enter a second 1292 02:06:08,320 --> 02:06:15,320 number. Let's do 23. So now it prints out the answer 28. So a calculator actually works. 1293 02:06:15,320 --> 02:06:22,320 Let's try another operator just to make sure. So we'll do six and why don't we multiply 1294 02:06:22,320 --> 02:06:29,320 it by, I don't know, 5.23. So now we go off and we do the multiplication and looks like 1295 02:06:29,320 --> 02:06:34,320 we got our answer. So our calculator seems to be working. And so basically all we did was 1296 02:06:34,320 --> 02:06:40,320 we got the input from the user and then we used if statements to figure out what they 1297 02:06:40,320 --> 02:06:45,320 wanted to do. So we can check to see if they wanted addition. We can check to see if they 1298 02:06:45,320 --> 02:06:49,320 wanted subtraction or division or multiplication. And we can also check to see if they entered 1299 02:06:49,320 --> 02:06:56,320 in an invalid operator. So actually why don't we try that too. So I'll enter in like five 1300 02:06:56,320 --> 02:07:02,320 and now we'll just enter in like an open and close parentheses and a four. So you can 1301 02:07:02,320 --> 02:07:07,320 see here it's able to tell like the program's smart enough to know that that's not a 1302 02:07:07,320 --> 02:07:11,320 valid operator. So these if statements can be really powerful and we can use them in 1303 02:07:11,320 --> 02:07:20,320 combination with getting the user input to create a little calculator. In this tutorial 1304 02:07:20,320 --> 02:07:26,320 I want to talk to you guys about using dictionaries in Python. Now a dictionary is a special 1305 02:07:26,320 --> 02:07:32,320 structure in Python which allows us to store information in what are called key value 1306 02:07:32,320 --> 02:07:38,320 pairs. So essentially I can just create a bunch of these different key value pairs and 1307 02:07:38,320 --> 02:07:43,320 then when I want to access a specific piece of information inside of the dictionary I 1308 02:07:43,320 --> 02:07:49,320 can just refer to it by its key. So the word dictionary is actually pretty appropriate 1309 02:07:49,320 --> 02:07:53,320 here. If you guys are familiar with a normal dictionary you know in a normal dictionary 1310 02:07:53,320 --> 02:07:59,320 you'd have a word and then you'd have a definition associated to that word. So in this 1311 02:07:59,320 --> 02:08:04,320 situation the word would be the key. In other words the word is what like uniquely 1312 02:08:04,320 --> 02:08:10,320 identifies it inside of the dictionary and then the value would be the actual definition. 1313 02:08:10,320 --> 02:08:15,320 So this is going to make more sense once we start working with this but I just wanted 1314 02:08:15,320 --> 02:08:19,320 to give you guys a quick introduction. So in this tutorial I want to create a little 1315 02:08:19,320 --> 02:08:26,320 program that will allow us to convert a three digit month name into the full month name. 1316 02:08:26,320 --> 02:08:36,320 So I could convert for example like JAN to January or I could convert M A R into 1317 02:08:36,320 --> 02:08:41,320 March. Like I want to be able to have a program that can do that for us and that's actually 1318 02:08:41,320 --> 02:08:45,320 a pretty good use case for a dictionary. So we're actually going to create a 1319 02:08:45,320 --> 02:08:49,320 dictionary and I'm going to show you guys how we can do that. So the first thing we 1320 02:08:49,320 --> 02:08:54,320 need to do in creating a dictionary is just give it a name. So I want to give this a 1321 02:08:54,320 --> 02:09:01,320 specific name. So I'm just going to call this month conversions. And now I just want to 1322 02:09:01,320 --> 02:09:06,320 set this equal to an open and closed curly bracket. So whenever we create a dictionary 1323 02:09:06,320 --> 02:09:12,320 in Python we're always going to want to create it inside of these open and closed curly brackets. 1324 02:09:12,320 --> 02:09:18,320 So inside of the dictionary now we can start defining what are called key value 1325 02:09:18,320 --> 02:09:24,320 pairs. So I can define a key and then I can give it a corresponding value. And the 1326 02:09:24,320 --> 02:09:27,320 way that we can do that is just by typing out the key and then typing out the value. 1327 02:09:27,320 --> 02:09:32,320 So in our case we're converting three digit month names into the full month 1328 02:09:32,320 --> 02:09:38,320 names. So I would want to type out for example JAN and then I can type a colon 1329 02:09:38,320 --> 02:09:43,320 and I want to type out the value I want to associate with it so we could say January. 1330 02:09:43,320 --> 02:09:50,320 I can basically do this for every combination. So I could say like FEB and this would be 1331 02:09:50,320 --> 02:09:55,320 February. And actually after each one of these we're going to need to put a comma. 1332 02:09:55,320 --> 02:10:01,320 So I could do the same for March, say M.A.R. 1333 02:10:01,320 --> 02:10:08,320 So essentially what I can do is I can specify an entry inside of this dictionary for each 1334 02:10:08,320 --> 02:10:14,320 one of these. So this over here would be the key and this over here would be the 1335 02:10:14,320 --> 02:10:20,320 value. And here's one thing is all of these keys have to be unique. So if I was to 1336 02:10:20,320 --> 02:10:24,320 come down here and change this to JAN, this is actually going to give us a little 1337 02:10:24,320 --> 02:10:29,320 warning. So you can see down here it says dictionary contains duplicate keys. 1338 02:10:29,320 --> 02:10:33,320 That's a no no when we're creating dictionaries. So you always want to make sure 1339 02:10:33,320 --> 02:10:37,320 that the keys are unique. All right. So I'm going to go ahead and create one of 1340 02:10:37,320 --> 02:10:41,320 these for each one of the months and then we'll come back and we'll talk about it. 1341 02:10:41,320 --> 02:10:47,320 All right. So I've gone ahead and created an entry for all 12 of the months. So now 1342 02:10:47,320 --> 02:10:52,320 we have a bunch of unique keys and each one of those keys is associated with a 1343 02:10:52,320 --> 02:10:57,320 different value. So this is the basics of creating a dictionary is we have a bunch 1344 02:10:57,320 --> 02:11:02,320 of these different key value pairs. And now what we can actually do is we can 1345 02:11:02,320 --> 02:11:07,320 access them from inside of this dictionary. So if I wanted to access a 1346 02:11:07,320 --> 02:11:12,320 specific key or a specific value, all I have to do is come over here and I'm 1347 02:11:12,320 --> 02:11:16,320 actually just going to print it out onto the screen so we can see it. All I have 1348 02:11:16,320 --> 02:11:21,320 to do is refer to the dictionary by name. So it's just called month conversions. 1349 02:11:21,320 --> 02:11:27,320 And there's actually a bunch of different ways that I can access these month names. 1350 02:11:27,320 --> 02:11:33,320 So I could access different entries inside of this dictionary. The first way is just 1351 02:11:33,320 --> 02:11:39,320 by making a open and close square bracket and inside here, I can just type in one 1352 02:11:39,320 --> 02:11:44,320 of the keys. So for example, inside here, I could type in NOV. And what this should 1353 02:11:44,320 --> 02:11:49,320 do is it should give us back the full name for November. So when I print this 1354 02:11:49,320 --> 02:11:55,320 out, you'll see that we get back down here November. So I could do the same thing 1355 02:11:55,320 --> 02:12:00,320 for, you know, any of these, we could say like M A R. And now we should get 1356 02:12:00,320 --> 02:12:05,320 March back. So I'm able to refer to the key. And it's going to go into the 1357 02:12:05,320 --> 02:12:09,320 dictionary and it's going to give me the value that's associated to that key. 1358 02:12:09,320 --> 02:12:14,320 There's actually another way we could do this too. So I could say month conversions 1359 02:12:14,320 --> 02:12:20,320 dot get an inside of here. I could also pass in the name of a key. So I could pass 1360 02:12:20,320 --> 02:12:28,320 in like D E C. And now we'll be printing out December. And what's cool about using 1361 02:12:28,320 --> 02:12:33,320 this get function is I can actually specify a default value that I want to 1362 02:12:33,320 --> 02:12:38,320 use if this key is not found. So there's going to be certain cases when we're 1363 02:12:38,320 --> 02:12:41,320 dealing with dictionaries where you're going to put in a key that might not 1364 02:12:41,320 --> 02:12:47,320 necessarily map to a value inside of the dictionary. So you put in an invalid key. 1365 02:12:47,320 --> 02:12:52,320 So let's just try to do that. So I'll just put like L U V. That's not a month 1366 02:12:52,320 --> 02:12:58,320 abbreviation. So when I try to run this, you'll see we get this none down here. 1367 02:12:58,320 --> 02:13:03,320 And in a lot of cases, you're going to want to create some sort of a default 1368 02:13:03,320 --> 02:13:08,320 value. So if I'm in this situation where I have a key that's not mapable to any 1369 02:13:08,320 --> 02:13:14,320 values inside of this dictionary, I can actually pass it a default value. So 1370 02:13:14,320 --> 02:13:19,320 inside this dot get function, I can make a comma. And now I can pass in like a 1371 02:13:19,320 --> 02:13:25,320 default value to get printed out. So I can say like not a valid key. And now when I 1372 02:13:25,320 --> 02:13:30,320 put in the invalid key, instead of saying none, it's going to go ahead and say not 1373 02:13:30,320 --> 02:13:35,320 a valid key. So this is going to give us like a default value that we can fall 1374 02:13:35,320 --> 02:13:41,320 back on. So these dictionaries are really awesome. And the keys over here don't 1375 02:13:41,320 --> 02:13:45,320 have to be string. So you could also use numbers. So for example, I could say 1376 02:13:45,320 --> 02:13:52,320 like zero, one, 10, like I can put any numbers I want over here as keys as long 1377 02:13:52,320 --> 02:13:56,320 as they're unique. And then I could access those keys, just like I normally 1378 02:13:56,320 --> 02:14:01,320 would down here with this get function or with those open and close square brackets. 1379 02:14:01,320 --> 02:14:05,320 So that's sort of the basics of using dictionaries. It can be really useful to 1380 02:14:05,320 --> 02:14:10,320 store key value pairs. And honestly, you're going to be using them a lot in Python to 1381 02:14:10,320 --> 02:14:18,320 store different types of data. In this tutorial, I want to talk to you guys about 1382 02:14:18,320 --> 02:14:24,320 while loops in Python. Now a while loop is basically a structure in Python, which 1383 02:14:24,320 --> 02:14:31,320 allows us to loop through and execute a block of code multiple times. So I can 1384 02:14:31,320 --> 02:14:36,320 specify like a few different lines of code, and then I can put that code inside of a 1385 02:14:36,320 --> 02:14:41,320 while loop. And it would basically loop through that code, executing it repeatedly 1386 02:14:41,320 --> 02:14:46,320 until a certain condition was false. So while loops can be awesome. And there's 1387 02:14:46,320 --> 02:14:50,320 a lot of situations in Python where we're going to want to loop through specific 1388 02:14:50,320 --> 02:14:54,320 lines of code. So in this tutorial, I just want to show you guys the like bare 1389 02:14:54,320 --> 02:14:58,320 basics of how while loops work. And then in future tutorials, we're going to use 1390 02:14:58,320 --> 02:15:02,320 while loops to create little games and stuff like that. So let's talk about 1391 02:15:02,320 --> 02:15:08,320 while loops. When I create my while loop, the first thing I want to do actually is 1392 02:15:08,320 --> 02:15:13,320 create an integer. I'm basically creating a variable that's a number and you don't 1393 02:15:13,320 --> 02:15:17,320 have to do this for a while loop, but just for our demonstration I'm going to. So 1394 02:15:17,320 --> 02:15:22,320 I'm just going to call this I and I'm going to set it equal to one. And now what I 1395 02:15:22,320 --> 02:15:26,320 want to do is I want to create a while loop. So I'm basically just going to say 1396 02:15:26,320 --> 02:15:34,320 while. And over here I want to specify a condition. And this is what's called our 1397 02:15:34,320 --> 02:15:39,320 loop condition or you can also refer to it as a loop guard. And basically we are 1398 02:15:39,320 --> 02:15:44,320 going to keep looping through the code inside of the while loop as long as this 1399 02:15:44,320 --> 02:15:50,320 condition is true. So whatever condition I put in here, as long as it's true, we're 1400 02:15:50,320 --> 02:15:53,320 going to keep looping through the code inside the while loop. So I'm just going to say 1401 02:15:53,320 --> 02:16:02,320 while I is less than or equal to 10. And now I'm going to put a colon and I'm going 1402 02:16:02,320 --> 02:16:07,320 to make a new line. And so anything that's below this while loop declaration and 1403 02:16:07,320 --> 02:16:13,320 that's indented like this is going to be considered code that's inside the while 1404 02:16:13,320 --> 02:16:18,320 loop. And so that code is going to get repeatedly executed while this condition 1405 02:16:18,320 --> 02:16:23,320 up here is true. So I'm just going to put a very simple line of code here. We're 1406 02:16:23,320 --> 02:16:27,320 just going to print out the value of I. And then down here I'm going to actually 1407 02:16:27,320 --> 02:16:34,320 increment I so I'm going to add one to I'm going to say I is equal to I plus one. 1408 02:16:34,320 --> 02:16:39,320 So basically I'm taking this I value and I'm adding one to it. There's actually a 1409 02:16:39,320 --> 02:16:44,320 shorthand that we can use in Python to do something like this though I could just say 1410 02:16:44,320 --> 02:16:51,320 I plus equals one and that's automatically going to add one to I. So this is like 1411 02:16:51,320 --> 02:16:56,320 a little shorthand and you'll see people in Python using that a lot. So over here 1412 02:16:56,320 --> 02:17:02,320 we basically have our while loop. So I have the outline for a while loop. So we're 1413 02:17:02,320 --> 02:17:06,320 defining this variable up here and we're going to keep looping through the code 1414 02:17:06,320 --> 02:17:12,320 inside the while loop as long as this condition up here is true. So if this 1415 02:17:12,320 --> 02:17:17,320 condition is false for example if I is equal to eleven then we're not going to 1416 02:17:17,320 --> 02:17:21,320 loop through this code anymore and we're just going to move on. So down here I'm 1417 02:17:21,320 --> 02:17:27,320 actually just going to print. Done with loop. So this will kind of illustrate 1418 02:17:27,320 --> 02:17:32,320 this for us. Alright so let's go ahead and run this program. I'm just going to run 1419 02:17:32,320 --> 02:17:38,320 it. And you'll see down here we get some output. So we're actually going to get 1420 02:17:38,320 --> 02:17:43,320 numbers printed out one all the way down through ten and then we're printing out 1421 02:17:43,320 --> 02:17:48,320 done with loop. So basically what's happening is we're printing out every 1422 02:17:48,320 --> 02:17:53,320 time we go through this loop. So initially I is equal to one right. The first thing 1423 02:17:53,320 --> 02:17:56,320 that Python is going to do when it gets to this while loop is it's going to check 1424 02:17:56,320 --> 02:18:00,320 this condition. So before it does anything else it's going to check this 1425 02:18:00,320 --> 02:18:05,320 condition and it's going to say okay is one less than or equal to ten and that's 1426 02:18:05,320 --> 02:18:10,320 true. So as long as this guy is true we're going to go through and loop through 1427 02:18:10,320 --> 02:18:14,320 all the code inside of here. So we're going to print out i which has the 1428 02:18:14,320 --> 02:18:19,320 value of one and then we're going to add one to i. So now i is going to go from 1429 02:18:19,320 --> 02:18:24,320 one to two. Now what we're going to do is we're going to go all the way back up 1430 02:18:24,320 --> 02:18:29,320 here to this while loop declaration and we're going to check the condition again. 1431 02:18:29,320 --> 02:18:33,320 So remember I checked the condition the first time and then I loop through all 1432 02:18:33,320 --> 02:18:38,320 the code then I go back up to the top and I check it again. So I'm going to say 1433 02:18:38,320 --> 02:18:44,320 okay is i less than or equal to ten. At this point i is equal to two. So i is 1434 02:18:44,320 --> 02:18:48,320 less than or equal to ten. So now we're going to go through and we'll execute 1435 02:18:48,320 --> 02:18:52,320 all this code again. So I'm going to print out i so it's going to be two and I'm 1436 02:18:52,320 --> 02:18:57,320 going to add one to i so now we have three. Then Python is going to go all the way 1437 02:18:57,320 --> 02:19:02,320 back up to the top and it's going to check the condition again. So on every 1438 02:19:02,320 --> 02:19:06,320 iteration through the loop before Python does what's inside of this loop it's 1439 02:19:06,320 --> 02:19:11,320 going to check that condition. And as long as the condition remains true we're 1440 02:19:11,320 --> 02:19:15,320 going to keep executing the code inside the loop. Eventually though in this 1441 02:19:15,320 --> 02:19:20,320 program i is going to end up being eleven. So on the last iteration it's going to 1442 02:19:20,320 --> 02:19:24,320 end up being eleven. We're going to come back up here and we're going to say hmm 1443 02:19:24,320 --> 02:19:29,320 is eleven less than or equal to ten? Nope. And so since it's nope we're going 1444 02:19:29,320 --> 02:19:33,320 to come down here and we'll print out this done with loop line. So that's the 1445 02:19:33,320 --> 02:19:38,320 basics of how these while loops work. We're specifying a loop condition as long 1446 02:19:38,320 --> 02:19:42,320 as that condition is true we'll keep executing the code inside of the loop. 1447 02:19:42,320 --> 02:19:46,320 And after every execution of the loop we're going to come back up and check the 1448 02:19:46,320 --> 02:19:52,320 condition again. So that's how we end up with this printing out down here. So 1449 02:19:52,320 --> 02:19:57,320 that's the basics of a while loop. And we can use these while loops to do all 1450 02:19:57,320 --> 02:20:05,320 sorts of things. Like while loops are very very powerful. In this tutorial I'm 1451 02:20:05,320 --> 02:20:10,320 going to show you guys how to build a basic guessing game in Python. And in this 1452 02:20:10,320 --> 02:20:14,320 guessing game we're actually going to be using all sorts of cool programming 1453 02:20:14,320 --> 02:20:17,320 structures that we've learned up to this point in the course. So we're going to 1454 02:20:17,320 --> 02:20:21,320 be using things like if statements and while loops and variables and all these 1455 02:20:21,320 --> 02:20:26,320 cool things in order to build this game. So the basic idea is we will specify 1456 02:20:26,320 --> 02:20:31,320 like a secret word. So we'll have a secret word that we store inside of our program. 1457 02:20:31,320 --> 02:20:36,320 And then the user can interact with the program and try to guess the secret word. 1458 02:20:36,320 --> 02:20:40,320 And so what we want to be able to happen is we want the user to be able to keep 1459 02:20:40,320 --> 02:20:44,320 guessing what the secret word is and keep typing in different responses until they 1460 02:20:44,320 --> 02:20:50,320 get the secret word right. So that's the basics of what this game is going to be. 1461 02:20:50,320 --> 02:20:54,320 And now let's go ahead and create it. So the first thing we want to do is create a 1462 02:20:54,320 --> 02:21:01,320 variable to store our secret word. So I could say secret word and we're just 1463 02:21:01,320 --> 02:21:04,320 going to set this equal to a secret word. So why don't we make it draft. That's 1464 02:21:04,320 --> 02:21:08,320 a pretty good secret word. And now what we want to do is we want to create a 1465 02:21:08,320 --> 02:21:13,320 variable that will store the user's response. So I want a variable that will 1466 02:21:13,320 --> 02:21:18,320 store like all the guesses that the user makes. So I'm just going to call this 1467 02:21:18,320 --> 02:21:23,320 guess. And I'm just going to set it equal to an empty string right now. So now we 1468 02:21:23,320 --> 02:21:28,320 have the secret word and we have a variable to store the user's guess. And 1469 02:21:28,320 --> 02:21:33,320 what we need to do is we need to be able to prompt the user to input the secret 1470 02:21:33,320 --> 02:21:38,320 word. But here's the catch. What we want to happen is we want them to enter the 1471 02:21:38,320 --> 02:21:42,320 secret word. And if they don't guess it correctly, we want to prompt them to 1472 02:21:42,320 --> 02:21:48,320 enter it again. So we can't just use a single input statement. We actually have 1473 02:21:48,320 --> 02:21:53,320 to use something called a while loop. And we can use a while loop in order to 1474 02:21:53,320 --> 02:21:59,320 continually ask the person to guess the word until they guess it correctly. So 1475 02:21:59,320 --> 02:22:03,320 let's go ahead and create our while loop. So I'm going to say while. And after I 1476 02:22:03,320 --> 02:22:09,320 say while, I need to specify a looping condition or a looping guard. Basically, 1477 02:22:09,320 --> 02:22:13,320 this is something that as long as it's true, we're going to keep looping through 1478 02:22:13,320 --> 02:22:18,320 this loop. So basically, I want to say I want to keep looping as long as the 1479 02:22:18,320 --> 02:22:25,320 user's guess is not equal to the secret word. So as long as they haven't guessed 1480 02:22:25,320 --> 02:22:31,320 the secret word, I'm going to keep going through this loop. And inside of this 1481 02:22:31,320 --> 02:22:35,320 loop, what we're going to do is we're going to ask them to input the secret word. So 1482 02:22:35,320 --> 02:22:42,320 I can take this guess variable and I can set it equal to input. And I'm just going 1483 02:22:42,320 --> 02:22:49,320 to say enter guess. And so over here, we're basically telling the user to enter in 1484 02:22:49,320 --> 02:22:56,320 their guess. I'm storing whatever they enter inside of this guess variable. And 1485 02:22:56,320 --> 02:22:59,320 then what's going to happen is we're going to come back up here. We're going to check 1486 02:22:59,320 --> 02:23:04,320 to see if the guess is equal to the secret word. If the guess isn't equal to the 1487 02:23:04,320 --> 02:23:08,320 secret word, then we're going to do it again. But if the guess is equal to the 1488 02:23:08,320 --> 02:23:13,320 secret word, then we're just going to break out of this loop. And so we can come down 1489 02:23:13,320 --> 02:23:20,320 here. And we can just print out a success message. So I could say you win because 1490 02:23:20,320 --> 02:23:25,320 they got the secret word. So this is a very, very simple program. But this is 1491 02:23:25,320 --> 02:23:30,320 essentially all of the code that we need to be able to build a game like this. So 1492 02:23:30,320 --> 02:23:35,320 let's go ahead and play our game. We can run it and see how we did. So I'm going to 1493 02:23:35,320 --> 02:23:40,320 click play. And down here, you can see it's prompting us to enter a guess. So I'm 1494 02:23:40,320 --> 02:23:44,320 just going to type in some, you know, random text, we can type in whatever we 1495 02:23:44,320 --> 02:23:48,320 want. And as long as we're not entering that secret word, it's going to keep 1496 02:23:48,320 --> 02:23:54,320 prompting us to enter different information. But if I enter in the secret word, so if 1497 02:23:54,320 --> 02:23:58,320 I enter in draft, now all of a sudden the program is going to terminate and it's 1498 02:23:58,320 --> 02:24:04,320 going to say, Hey, you win, because we were able to guess the secret word. So that's 1499 02:24:04,320 --> 02:24:08,320 like a really cool way for us to be able to do this. And we actually have a fully 1500 02:24:08,320 --> 02:24:13,320 functional guessing game. But I think this guessing game could actually be 1501 02:24:13,320 --> 02:24:19,320 improved quite a bit. A lot of times when we're making a guessing game, we want to 1502 02:24:19,320 --> 02:24:24,320 set a limit. So in other words, I want to set a limit for the number of times that 1503 02:24:24,320 --> 02:24:28,320 the user can try to guess the word. So let's say that the user has three tries, 1504 02:24:28,320 --> 02:24:32,320 right, they have three guesses in order to guess the word. And if they can't guess 1505 02:24:32,320 --> 02:24:36,320 the word after three tries, then they're going to lose the game. But if they can 1506 02:24:36,320 --> 02:24:40,320 guess the word inside of three tries, then they'll win the game, right? I think 1507 02:24:40,320 --> 02:24:44,320 that would be a little bit more of a fun game. So why don't we try to program 1508 02:24:44,320 --> 02:24:49,320 that game? Basically, we're going to set a limit on the number of guesses that the 1509 02:24:49,320 --> 02:24:54,320 user can have. In order to do this, we're going to have to create a couple more 1510 02:24:54,320 --> 02:24:58,320 variables down here. In other words, we're going to have to store a couple more 1511 02:24:58,320 --> 02:25:03,320 pieces of information. The first piece of information I want to keep track of is 1512 02:25:03,320 --> 02:25:08,320 how many times the user has guessed, right? So we can just make a variable called 1513 02:25:08,320 --> 02:25:13,320 guess count. And we'll just set this equal to zero because initially, the user won't 1514 02:25:13,320 --> 02:25:19,320 have guessed down in this while loop. Every time we go through the loop, I want to 1515 02:25:19,320 --> 02:25:24,320 increment that guess count, right? So every time we've gone through this while loop, 1516 02:25:24,320 --> 02:25:29,320 I want to increment that count because that means the user will have guessed. 1517 02:25:29,320 --> 02:25:36,320 So down here, I'm going to say guess count plus equals one. And this is just going 1518 02:25:36,320 --> 02:25:42,320 to add one to the guess count, actually whoops. Alright, so after each 1519 02:25:42,320 --> 02:25:46,320 iteration of this loop, we're going to go ahead and add one to the guess count. 1520 02:25:46,320 --> 02:25:49,320 Alright, so that's the first variable that we're going to need. We're also going to 1521 02:25:49,320 --> 02:25:54,320 want to store another variable. And this is going to be called guess limit. And 1522 02:25:54,320 --> 02:25:59,320 basically, this is going to tell us how many times the user can guess the word. 1523 02:25:59,320 --> 02:26:04,320 So I'm going to it's going to be guess limit. And we'll basically just say three. 1524 02:26:04,320 --> 02:26:08,320 So let's say that the user has three tries to guess the word three strikes and 1525 02:26:08,320 --> 02:26:13,320 you're out. So the user, if they can't get it in three tries, then we're going to 1526 02:26:13,320 --> 02:26:17,320 basically say that they lose the game. And I'm also going to need one more 1527 02:26:17,320 --> 02:26:25,320 variable here, which we're going to call out of guesses. And I'm just going to set 1528 02:26:25,320 --> 02:26:30,320 this equal to false initially. So this out of guesses variable is going to be a 1529 02:26:30,320 --> 02:26:35,320 Boolean. And it's going to tell us whether or not the user is out of guesses. So if 1530 02:26:35,320 --> 02:26:39,320 out of guesses is true, that means they have no more guesses, right? They basically 1531 02:26:39,320 --> 02:26:44,320 lost the game. And if out of guesses is false, that means that they still have 1532 02:26:44,320 --> 02:26:48,320 some guesses left so they can keep playing. All right, so let's use these 1533 02:26:48,320 --> 02:26:53,320 different variables in order to make our program more functional. So the first 1534 02:26:53,320 --> 02:26:58,320 thing I want to do is when I go through this loop, I want to check to make sure 1535 02:26:58,320 --> 02:27:04,320 that the user has more guesses, right? In other words, before I let the user 1536 02:27:04,320 --> 02:27:08,320 enter a guess, I want to check to see that they haven't already used up all 1537 02:27:08,320 --> 02:27:13,320 their guesses. So down here, I can make an if statement. I can say if and inside 1538 02:27:13,320 --> 02:27:21,320 of the condition, I want to check to see that guess count is less than guess limit. 1539 02:27:21,320 --> 02:27:27,320 If guess count is less than guess limit, that means that they haven't guessed the 1540 02:27:27,320 --> 02:27:32,320 total number of guesses that they have. So they have some guesses left. And if that's 1541 02:27:32,320 --> 02:27:38,320 the case and if that's true, then I'm going to go ahead and give them a guess and 1542 02:27:38,320 --> 02:27:43,320 then we'll increment the guess count. If this isn't true, in other words, if they 1543 02:27:43,320 --> 02:27:50,320 have reached their guess limit, then I'm going to want to set out of guesses 1544 02:27:50,320 --> 02:27:54,320 equal to true, because they're out of guesses, right? They have no more guesses 1545 02:27:54,320 --> 02:27:58,320 because the guess count wasn't less than the guess limit. And so that means that 1546 02:27:58,320 --> 02:28:02,320 they're out of guesses. So they have no more guesses. So there's actually one more 1547 02:28:02,320 --> 02:28:07,320 thing we need to do inside of this while loop, we need to add another condition 1548 02:28:07,320 --> 02:28:11,320 on to this loop guard. So right now, we're going to keep looping as long as the 1549 02:28:11,320 --> 02:28:17,320 guess is not equal to the secret word. But remember, if the user's out of guesses, 1550 02:28:17,320 --> 02:28:21,320 in other words, if they've reached their guess limit, we don't want them to guess 1551 02:28:21,320 --> 02:28:25,320 anymore, right? So if they ran out of guesses, then we want to basically break out of 1552 02:28:25,320 --> 02:28:29,320 this loop and not give them any more guesses. So I'm going to go ahead and add 1553 02:28:29,320 --> 02:28:33,320 another condition onto here. And I'm going to say, while they haven't guessed the 1554 02:28:33,320 --> 02:28:41,320 secret word, and they're not out of guesses. So it's going to be not out of 1555 02:28:41,320 --> 02:28:47,320 guesses. Then we're going to keep looping. So as long as they haven't guess the word, 1556 02:28:47,320 --> 02:28:51,320 and as long as they still have some guesses left, we're going to keep looping. But 1557 02:28:51,320 --> 02:28:56,320 otherwise, we're going to break out of the loop. And so down here, we're printing out 1558 02:28:56,320 --> 02:29:01,320 you win. But actually, when we break out of this loop, there's going to be two 1559 02:29:01,320 --> 02:29:07,320 possible scenarios. So again, there's two possible ways that this loop could end, 1560 02:29:07,320 --> 02:29:11,320 right? Either the guesses equal to the secret word. So either the user guessed 1561 02:29:11,320 --> 02:29:17,320 the word correctly, or the user ran out of guesses. And so there's two situations 1562 02:29:17,320 --> 02:29:21,320 down here that we need to account for. And so I'm going to use an if statement to 1563 02:29:21,320 --> 02:29:27,320 figure out which is which. So I'm going to say if, and we're just going to type out 1564 02:29:27,320 --> 02:29:35,320 of guesses. And if the user is out of guesses, then I want to print out of 1565 02:29:35,320 --> 02:29:41,320 you lose. So we're basically going to be like, you lost the game. Otherwise, though, 1566 02:29:41,320 --> 02:29:45,320 if they're not out of guesses, that means that they guess the word correctly. So 1567 02:29:45,320 --> 02:29:50,320 we're just going to print out you win. Alright, so now we have all the logic for 1568 02:29:50,320 --> 02:29:54,320 this little guessing game setup. And let's see if we can run it and play through 1569 02:29:54,320 --> 02:29:59,320 it. So I'm just going to run this program. And now it's going to tell us to 1570 02:29:59,320 --> 02:30:03,320 enter a guess. So why don't we enter more guesses than we have? So we're going to 1571 02:30:03,320 --> 02:30:07,320 try to lose the game. So I'm just going to enter one guess, two guesses. And now I'm 1572 02:30:07,320 --> 02:30:12,320 on my final guess. So if I don't get it here, we should actually lose the game. And 1573 02:30:12,320 --> 02:30:17,320 you can see it says out of guesses, you lose. So we weren't able to guess it in the 1574 02:30:17,320 --> 02:30:22,320 number of tries that we had. Let's run the program again, and we'll try to win the 1575 02:30:22,320 --> 02:30:27,320 game. So we'll get a couple wrong. Let's say we're on our last guess. And I'm like, 1576 02:30:27,320 --> 02:30:34,320 Okay, I can do this. So I type in the word and bam, we won the game. So that's how we 1577 02:30:34,320 --> 02:30:39,320 can basically create a game where we have a guess limit. So this is a lot of code. 1578 02:30:39,320 --> 02:30:42,320 Let me walk you guys through this one more time so you can just get a full 1579 02:30:42,320 --> 02:30:47,320 understanding of what we're doing. Up here, I created a few different variables. We 1580 02:30:47,320 --> 02:30:52,320 created this secret word variable, and we created this guess variable. And then we 1581 02:30:52,320 --> 02:30:57,320 also created some more variables. So we created this guess count variable. And this 1582 02:30:57,320 --> 02:31:03,320 just keeps track of how many times the user has guessed the word. And you can see 1583 02:31:03,320 --> 02:31:10,320 down here, every time we give them a guess, we're incrementing the guess count. We also 1584 02:31:10,320 --> 02:31:15,320 have guess limit. And guess limit is telling the program how many times the user can 1585 02:31:15,320 --> 02:31:20,320 guess. So before I go through this while loop, the first thing I'm doing is I'm 1586 02:31:20,320 --> 02:31:25,320 checking to see that the guess count is less than the guess limit. In other words, 1587 02:31:25,320 --> 02:31:30,320 do they still have some guesses left? If they do, then we're going to get the 1588 02:31:30,320 --> 02:31:36,320 input from the user. Otherwise, we have this other variable up here called out of 1589 02:31:36,320 --> 02:31:40,320 guesses. And out of guesses is going to tell us whether or not they have some guesses 1590 02:31:40,320 --> 02:31:45,320 left. So if this is equal to false, that means they have some guesses. If it's 1591 02:31:45,320 --> 02:31:50,320 equal to true, however, it means no more dice, no more guesses, they are done. So 1592 02:31:50,320 --> 02:31:54,320 they lost the game. The last thing we needed to do was specify an additional 1593 02:31:54,320 --> 02:32:00,320 condition up here. So we are going to keep looping through the code inside of this 1594 02:32:00,320 --> 02:32:06,320 loop, as long as the conditions up here are true. So as long as they haven't 1595 02:32:06,320 --> 02:32:13,320 guessed the word, and as long as they're not out of guesses, we are going to keep 1596 02:32:13,320 --> 02:32:18,320 looping through. And so when eventually the user does break out of this loop, 1597 02:32:18,320 --> 02:32:22,320 there's going to be two possible situations. The first situation is that they ran 1598 02:32:22,320 --> 02:32:26,320 out of guesses. And so we want to check to see if that's the situation. I want to 1599 02:32:26,320 --> 02:32:30,320 check to see if they're out of guesses. If they are, we'll print out a lose 1600 02:32:30,320 --> 02:32:34,320 message. If they're not, then they must have guessed it correctly. So they win. And so 1601 02:32:34,320 --> 02:32:38,320 that is how we can use while loops and if statements and also variables in 1602 02:32:38,320 --> 02:32:43,320 combination with each other to build a pretty awesome guessing game. 1603 02:32:47,320 --> 02:32:52,320 In this tutorial, I want to talk to you guys about using for loops in Python. Now 1604 02:32:52,320 --> 02:32:57,320 a for loop is a special type of loop in Python, which allows us to loop over 1605 02:32:57,320 --> 02:33:03,320 different collections of items. So a lot of times we'll use for loops in Python to 1606 02:33:03,320 --> 02:33:08,320 like loop through different arrays, or we can loop over like the letters inside 1607 02:33:08,320 --> 02:33:13,320 of a string, or we could just loop through like a series of numbers. So for loops 1608 02:33:13,320 --> 02:33:18,320 provide a very specific purpose. And the easiest way to kind of wrap your head 1609 02:33:18,320 --> 02:33:21,320 around why for loops are useful is just for me to show you guys a bunch of 1610 02:33:21,320 --> 02:33:25,320 different examples. So that's exactly what I'm going to do. We're going to look at 1611 02:33:25,320 --> 02:33:31,320 why for loops are awesome. So down here in my text file and my Python file, I'm 1612 02:33:31,320 --> 02:33:34,320 going to write out a couple of different for loops. So the way that we create a 1613 02:33:34,320 --> 02:33:41,320 for loop is just by saying for. And now what I want to do is specify a variable. 1614 02:33:41,320 --> 02:33:48,320 And this variable is going to essentially represent a different value every time we 1615 02:33:48,320 --> 02:33:53,320 go through this for loop. And you guys will see how that works in a second. But 1616 02:33:53,320 --> 02:33:58,320 just know that this variable is going to be used on every iteration of our for 1617 02:33:58,320 --> 02:34:04,320 loop. And each time it will most likely have a different value. So in our case, 1618 02:34:04,320 --> 02:34:10,320 I'm just going to call this letter. And I'm going to say for letter in. And now 1619 02:34:10,320 --> 02:34:15,320 what I want to do is I want to specify a collection that I want to loop over. 1620 02:34:15,320 --> 02:34:19,320 One example of this would be like a string. So I could put a string in here. 1621 02:34:19,320 --> 02:34:24,320 I could just put like draft Academy. And now I can just put a colon. And so 1622 02:34:24,320 --> 02:34:29,320 basically what this is going to say is it's going to say for every letter inside 1623 02:34:29,320 --> 02:34:35,320 of draft Academy, I want to do something. And so down here inside of this for loop 1624 02:34:35,320 --> 02:34:40,320 and again, we need to indent this. We can put what we want to do with each letter. 1625 02:34:40,320 --> 02:34:44,320 So let me just show you guys like basically what this is going to do. So I can print 1626 02:34:44,320 --> 02:34:49,320 out a letter so I can print out this letter variable. And it's actually going to 1627 02:34:49,320 --> 02:34:54,320 print out a different letter inside of this draft Academy string on every 1628 02:34:54,320 --> 02:35:01,320 single iteration of this loop. So I'm going to run this program. And down here 1629 02:35:01,320 --> 02:35:06,320 inside of my console, you'll see that I'm basically printing out draft Academy. 1630 02:35:06,320 --> 02:35:11,320 So on the first iteration of the loop, I printed out the first letter in draft 1631 02:35:11,320 --> 02:35:16,320 Academy, which was G. On the second iteration of the loop, I printed out I 1632 02:35:16,320 --> 02:35:22,320 said, third was R, fourth was A. So I'm essentially just looping through all of 1633 02:35:22,320 --> 02:35:28,320 the letters inside of draft Academy. So I'm saying for each letter in draft 1634 02:35:28,320 --> 02:35:33,320 Academy, I want to print out that letter. And so this is kind of like how four 1635 02:35:33,320 --> 02:35:39,320 loops can be used. We can define a variable. And that variable will change on each 1636 02:35:39,320 --> 02:35:43,320 iteration of the loop, right? So on the first iteration of the loop, this letter 1637 02:35:43,320 --> 02:35:47,320 variable represented a G. On the second iteration of the loop, the letter 1638 02:35:47,320 --> 02:35:52,320 variable stored at the value I, etc. Right. So we went through this entire string, 1639 02:35:52,320 --> 02:35:57,320 and we were able to print out each letter. So in addition to using this with 1640 02:35:57,320 --> 02:36:01,320 strings, we can also use this with other collections, for example, like an 1641 02:36:01,320 --> 02:36:06,320 array. So if I created an array up here, let's just call it friends, I'm going to 1642 02:36:06,320 --> 02:36:10,320 set this equal to a bunch of different values. So we can put like a list of our 1643 02:36:10,320 --> 02:36:16,320 friends in here, we can say like Jim, Karen, and Kevin, instead of saying the 1644 02:36:16,320 --> 02:36:25,320 letter, why don't we call this friend? And I can say for friend in friends. And now 1645 02:36:25,320 --> 02:36:31,320 we'll print out the friend. So over here, I'm saying for each friend inside of 1646 02:36:31,320 --> 02:36:36,320 this friends array, I want to print out the friend. So now we'll actually be able 1647 02:36:36,320 --> 02:36:40,320 to print out each element inside of that array. So down here, you'll see we're 1648 02:36:40,320 --> 02:36:44,320 printing out Jim, and then on the second iteration, we're printing out Karen, 1649 02:36:44,320 --> 02:36:48,320 and on the third, we're printing out Kevin. So I'm able to loop through all of the 1650 02:36:48,320 --> 02:36:53,320 values inside of the array. And just so you guys know, like, you can name this 1651 02:36:53,320 --> 02:36:57,320 variable, whatever you want. So I could name, I could give this like any random 1652 02:36:57,320 --> 02:37:03,320 name. And if I want to access it, though, I have to access it using that same name. 1653 02:37:03,320 --> 02:37:07,320 So we can loop through something like an array, we could also just loop through 1654 02:37:07,320 --> 02:37:13,320 a series of numbers. So I could say index over here. And again, this can be anything. 1655 02:37:13,320 --> 02:37:18,320 I'm just going to call it index. So we could say for index in range, and then in 1656 02:37:18,320 --> 02:37:24,320 here, I can pass in a number. So I could pass in like 10, for example. And down here, 1657 02:37:24,320 --> 02:37:28,320 I'm just going to print out the index. When I run this program, you guys will see that 1658 02:37:28,320 --> 02:37:33,320 it's basically going to print out every number in the range from zero to 10, 1659 02:37:33,320 --> 02:37:38,320 not including 10. So starting with zero, it's going to print out zero, one, two, 1660 02:37:38,320 --> 02:37:43,320 three, four, five, six, seven, eight, nine. But notice that it didn't print out 10. 1661 02:37:43,320 --> 02:37:48,320 So it's essentially just printing out all the numbers between zero and 10, 1662 02:37:48,320 --> 02:37:54,320 not including 10. You can also specify a range of numbers. So I could say, for 1663 02:37:54,320 --> 02:37:59,320 example, like three and 10. And now this will print out all the numbers between 1664 02:37:59,320 --> 02:38:06,320 three and 10, not including 10. So we're on this program. And you can see we print 1665 02:38:06,320 --> 02:38:11,320 out three, four, five, six, seven, eight, nine, and not 10. So whatever value that 1666 02:38:11,320 --> 02:38:17,320 you put here in this second position is not going to get included in the range. 1667 02:38:17,320 --> 02:38:22,320 And ranges can be really useful. So for example, I could use a range to loop 1668 02:38:22,320 --> 02:38:27,320 through an array, just like we did before. So if I wanted, I could say something 1669 02:38:27,320 --> 02:38:34,320 like four index in range. And now inside of this range, I can pass in the length 1670 02:38:34,320 --> 02:38:40,320 of the array. So just so you guys know, if I wanted to get the length of this 1671 02:38:40,320 --> 02:38:44,320 array, in other words, if I wanted to figure out how many elements were inside 1672 02:38:44,320 --> 02:38:49,320 of it, I could just type out LEN and then inside parentheses, the name of the 1673 02:38:49,320 --> 02:38:54,320 array. And so this is going to spit out three, because there's three elements 1674 02:38:54,320 --> 02:38:59,320 inside of here, right? Kind of makes sense. So what I can do is I can say down here 1675 02:38:59,320 --> 02:39:06,320 inside of this range function, I can just type in LEN and then friends. And so what 1676 02:39:06,320 --> 02:39:11,320 this is going to do is it's going to essentially give me a range between zero 1677 02:39:11,320 --> 02:39:16,320 and the number of friends inside of this list. So down here, I could actually type 1678 02:39:16,320 --> 02:39:23,320 out friends index. And this will allow me to access each individual friend inside 1679 02:39:23,320 --> 02:39:28,320 of this list, just like we did before. But now I'm doing it with a range. So you 1680 02:39:28,320 --> 02:39:34,320 can see I'm typing out Jim, Karen and Kevin. So actually, for each iteration 1681 02:39:34,320 --> 02:39:41,320 through this loop, it's basically going to be printing out friends zero, friends one, 1682 02:39:41,320 --> 02:39:46,320 and then friends two, because in here, we're passing in a three. And remember, 1683 02:39:46,320 --> 02:39:50,320 whenever I pass in there, it's going to range from zero all the way up to that 1684 02:39:50,320 --> 02:39:54,320 number, but not including that number. So that's another way that we could print 1685 02:39:54,320 --> 02:39:59,320 out all the elements in the array. And looping through something like an array 1686 02:39:59,320 --> 02:40:04,320 is actually a very, very common use case for for loops. But like I said, we can 1687 02:40:04,320 --> 02:40:08,320 use for loops to loop through essentially any collection that we have. So we could 1688 02:40:08,320 --> 02:40:12,320 loop through like a string, we could also loop through, you know, something like an 1689 02:40:12,320 --> 02:40:18,320 array. So this can be really useful. And that's one of the basics of for loops in 1690 02:40:18,320 --> 02:40:22,320 Python. And I also want to show you guys one more example, just while we're here. 1691 02:40:22,320 --> 02:40:28,320 So why don't we go ahead and print out a range up to five, right? So this will just 1692 02:40:28,320 --> 02:40:33,320 be a simple program. You can use all sorts of logic inside of these for loops. So 1693 02:40:33,320 --> 02:40:37,320 let's say that I wanted to do something special on the first iteration of the 1694 02:40:37,320 --> 02:40:44,320 loop. So I could say, if index is equal to zero. And if the index is zero, then we 1695 02:40:44,320 --> 02:40:47,320 know it's the first iteration of the loop. So I could do something special, I could 1696 02:40:47,320 --> 02:40:55,320 like print out first iteration. And then otherwise, we could just print like not 1697 02:40:55,320 --> 02:40:59,320 first. So this would be an example of like, maybe you want to do something on the 1698 02:40:59,320 --> 02:41:04,320 first iteration of the loop and do something else on subsequent iteration. So if I play 1699 02:41:04,320 --> 02:41:08,320 this, you'll see only on the first iteration is it printing this out. And 1700 02:41:08,320 --> 02:41:12,320 otherwise, it's printing out other stuff. So, you know, don't be afraid to put some 1701 02:41:12,320 --> 02:41:17,320 complex logic inside of these for loops, because it can really make your program 1702 02:41:17,320 --> 02:41:24,320 is more powerful. In this tutorial, I'm going to show you guys how to build an 1703 02:41:24,320 --> 02:41:29,320 exponent function. Now an exponent function is basically going to allow us to take a 1704 02:41:29,320 --> 02:41:34,320 certain number and raise it to a specific power. So in Python, there's actually a 1705 02:41:34,320 --> 02:41:39,320 really easy way to do this. I can come down here and I can print, we can say like 1706 02:41:39,320 --> 02:41:45,320 two, and we can just do two multiplication signs. And I can say three. And this is 1707 02:41:45,320 --> 02:41:51,320 basically just going to be two raised to the third power. So you can see here, I can 1708 02:41:51,320 --> 02:41:55,320 just print this out and we get eight, because that's too cute. So it's really 1709 02:41:55,320 --> 02:42:00,320 easy to do exponents in Python. But I want to show you guys how we can use 1710 02:42:00,320 --> 02:42:05,320 something like a for loop in order to create a function like this of our own. 1711 02:42:05,320 --> 02:42:10,320 So we'll actually create an exponent function that will basically do this and 1712 02:42:10,320 --> 02:42:15,320 we'll use for loops to do it. So let's create a function. So I'm just going to say 1713 02:42:15,320 --> 02:42:19,320 def. And now we need to give this function a name. So why don't we call it 1714 02:42:19,320 --> 02:42:25,320 raise to power. And inside of here, I'm going to accept two parameters. The first 1715 02:42:25,320 --> 02:42:30,320 parameter is going to be the base number. And then the second parameter is going to 1716 02:42:30,320 --> 02:42:35,320 be the power number. So we're going to take the base number and raise it to the 1717 02:42:35,320 --> 02:42:42,320 power number. So over here, we can just make a colon. Now, inside of this function, 1718 02:42:42,320 --> 02:42:46,320 we need to start writing some code. Here's the thing. Inside the function, we don't 1719 02:42:46,320 --> 02:42:52,320 necessarily know the value of this pound. Right. For example, if we knew that we 1720 02:42:52,320 --> 02:42:57,320 were just trying to like square this number or cube this number, I could just 1721 02:42:57,320 --> 02:43:04,320 return like base num times base num, right? That would square the number or I could 1722 02:43:04,320 --> 02:43:08,320 cube the number base num. So right. So this would be like the number cubed. The 1723 02:43:08,320 --> 02:43:12,320 problem is though, we don't know like right off the bat what this value is going to 1724 02:43:12,320 --> 02:43:17,320 be. Right. This could change like the user can decide that. So in order to write this 1725 02:43:17,320 --> 02:43:21,320 function, we're actually going to need to use a for loop. And I'm going to show you 1726 02:43:21,320 --> 02:43:25,320 guys how we can use a for loop to basically figure this out. So the first thing I 1727 02:43:25,320 --> 02:43:29,320 want to do is create a variable. And I'm just going to call it result. And we're 1728 02:43:29,320 --> 02:43:34,320 just going to set result equal to one. So we're going to start this off at one. Now 1729 02:43:34,320 --> 02:43:41,320 I want to create a for loop. So I'm basically just going to say four. And over here, 1730 02:43:41,320 --> 02:43:47,320 I basically want to specify like a index. So I'm going to say index in, and now I 1731 02:43:47,320 --> 02:43:52,320 want to specify a range and a range will basically range us through a collection 1732 02:43:52,320 --> 02:43:58,320 of numbers. So basically, I want to multiply the base num by itself as many 1733 02:43:58,320 --> 02:44:05,320 times as the power num specifies. Right. So over here, I can just make this a range 1734 02:44:05,320 --> 02:44:12,320 of power num. Right. And so we'll basically loop through this for loop as many times 1735 02:44:12,320 --> 02:44:17,320 as pound them. So if pound them is two, we'll loop through it twice. If pound them 1736 02:44:17,320 --> 02:44:20,320 is four, we'll loop through it four times. And every time through the loop will 1737 02:44:20,320 --> 02:44:24,320 multiply base num by itself. Or actually, you'll see we're going to multiply it by 1738 02:44:24,320 --> 02:44:30,320 this result. So down here inside of this for loop, I can basically say result is 1739 02:44:30,320 --> 02:44:37,320 equal to result times base num. This should actually give us everything we need to 1740 02:44:37,320 --> 02:44:43,320 take this number to the specific power. So the actual like result, you know, of 1741 02:44:43,320 --> 02:44:48,320 doing the math is going to get stored inside of this result variable. So the 1742 02:44:48,320 --> 02:44:53,320 first time through the loop, we're just multiplying base num by one. Right. And 1743 02:44:53,320 --> 02:44:58,320 so now result is going to become the same value as base num. The second time 1744 02:44:58,320 --> 02:45:02,320 through the loop. So this would be if we were squaring the number, we're basically 1745 02:45:02,320 --> 02:45:06,320 multiplying result by base num again. So it's essentially just base num times 1746 02:45:06,320 --> 02:45:10,320 base num. The third time through the loop, we're multiplying result times base num. 1747 02:45:10,320 --> 02:45:14,320 So we're essentially just like cubing the number. So that's essentially what this 1748 02:45:14,320 --> 02:45:19,320 for loop is doing for us. So the last thing I want to do is right here below 1749 02:45:19,320 --> 02:45:24,320 this for loop, I'm just going to return the result. So we'll be able to return 1750 02:45:24,320 --> 02:45:28,320 whatever the result of raising the number to the power was. All right. So let's 1751 02:45:28,320 --> 02:45:32,320 come down after this function. And we're just going to call it. So I'm just going 1752 02:45:32,320 --> 02:45:37,320 to say, actually, we'll print out the answer. So I'm going to say raise to power. 1753 02:45:37,320 --> 02:45:41,320 And inside of these parentheses, remember, we can pass two numbers. So let's 1754 02:45:41,320 --> 02:45:46,320 raise three to the second power. So we're just going to square three. And we'll see 1755 02:45:46,320 --> 02:45:51,320 how this works. So throw on the program. And you can see down here in the console 1756 02:45:51,320 --> 02:45:56,320 we're getting nine. Let's try something a little bit crazier. Let's try three to 1757 02:45:56,320 --> 02:46:01,320 the fourth power. And we'll run this and get 81. So that's three to the fourth 1758 02:46:01,320 --> 02:46:06,320 power. Let's try two to the third power. And we're getting eight. Yeah, so that makes 1759 02:46:06,320 --> 02:46:12,320 sense. So our raise the power function is working just as expected. And I'll just 1760 02:46:12,320 --> 02:46:16,320 walk you guys through it one more time. So inside of this function, we're taking 1761 02:46:16,320 --> 02:46:20,320 in two pieces of input, we're taking in a base number, and we're taking in a 1762 02:46:20,320 --> 02:46:24,320 power number. So that's like, the base number is going to be taken to the power 1763 02:46:24,320 --> 02:46:29,320 number. I'm defining a variable here called result. And result is where we're going 1764 02:46:29,320 --> 02:46:35,320 to store the actual result of doing the math. Now we specify this for loop. And 1765 02:46:35,320 --> 02:46:40,320 I'm basically saying that I want to loop through this range of numbers. When I put 1766 02:46:40,320 --> 02:46:44,320 pound on here, it's basically going to range from zero all the way up to but not 1767 02:46:44,320 --> 02:46:49,320 including the power number. So we'll basically loop through power number of 1768 02:46:49,320 --> 02:46:52,320 times, if that makes sense. And then every time through the loop, we're just 1769 02:46:52,320 --> 02:46:57,320 multiplying result by base num. And then we're just returning results. So that's 1770 02:46:57,320 --> 02:47:01,320 the basics of building a power function. And obviously, like if you're just trying 1771 02:47:01,320 --> 02:47:05,320 to take a number to a power and Python, it's really easy. But this kind of shows you 1772 02:47:05,320 --> 02:47:09,320 like the internal workings, right? For a function like that to work, we would have 1773 02:47:09,320 --> 02:47:17,320 to actually type something out just like this. And this tutorial, I want to talk 1774 02:47:17,320 --> 02:47:21,320 to you guys about two different concepts in Python. It's going to be a pretty cool 1775 02:47:21,320 --> 02:47:26,320 lesson. First thing I want to show you guys though, are two dimensional lists. So 1776 02:47:26,320 --> 02:47:32,320 down here in our Python file, I just want to create a basic list. So I'm just going 1777 02:47:32,320 --> 02:47:37,320 to call this number grid. And I'm just going to set this equal to a normal list 1778 02:47:37,320 --> 02:47:43,320 right just like this. Now normally if we were creating a list, I could just put in a 1779 02:47:43,320 --> 02:47:47,320 bunch of attributes and bunch of elements, right? And now all of these elements are 1780 02:47:47,320 --> 02:47:53,320 inside of this number grid list. Everybody's happy. But another thing I can do is I 1781 02:47:53,320 --> 02:48:00,320 can make all of the elements inside of this number grid list lists. So the first item 1782 02:48:00,320 --> 02:48:04,320 in the number grid list is going to be a list. The second item in the number grid 1783 02:48:04,320 --> 02:48:08,320 list is going to be a list, etc. So I'm going to show you guys how to do that. I 1784 02:48:08,320 --> 02:48:12,320 can basically just come down here and where I would normally put the first element 1785 02:48:12,320 --> 02:48:16,320 in the list, I can just create another array. So I'm just going to make an open 1786 02:48:16,320 --> 02:48:23,320 and closed square bracket. And now I'm just going to say 123. And I can create the 1787 02:48:23,320 --> 02:48:30,320 next item in my list. I can say 456. And let's make another item in this list. I 1788 02:48:30,320 --> 02:48:36,320 can say 789. And finally, we're going to put one more list inside of the number 1789 02:48:36,320 --> 02:48:41,320 grid list. And it's just going to have zero in it. So inside of this number 1790 02:48:41,320 --> 02:48:48,320 grid list, right, this high level overall list, we have four elements. And all of 1791 02:48:48,320 --> 02:48:53,320 those elements are themselves lists. So essentially what we're doing is we're 1792 02:48:53,320 --> 02:49:03,320 creating a grid, right? This grid has 1234 rows, and 123 columns, right, four 1793 02:49:03,320 --> 02:49:07,320 rows, three columns. And that's kind of like why these can be useful is we can 1794 02:49:07,320 --> 02:49:13,320 create a grid like structure inside of Python using two dimensional lists. So 1795 02:49:13,320 --> 02:49:18,320 this is a pretty cool list structure. And I want to show you guys how we can 1796 02:49:18,320 --> 02:49:24,320 access individual elements inside of this list structure. So, for example, 1797 02:49:24,320 --> 02:49:27,320 let's say I wanted to print out one of these values. Let's say I wanted to print 1798 02:49:27,320 --> 02:49:34,320 out this one right here. The way I can access it is by saying number grid. And 1799 02:49:34,320 --> 02:49:38,320 the first thing I want to do is make an open and close square brackets. And in 1800 02:49:38,320 --> 02:49:44,320 here, I want to put the index of the row that I want to access. So this would be 1801 02:49:44,320 --> 02:49:49,320 like row zero, because that's the zero element in the array. This would be row one. 1802 02:49:49,320 --> 02:49:54,320 It's at index position one. This would be row two, row three, etc. So I could put 1803 02:49:54,320 --> 02:49:59,320 row zero. And now right next to this, I'm going to make another square bracket. 1804 02:49:59,320 --> 02:50:05,320 And I want to put the index of the column. So this would be like column one, column 1805 02:50:05,320 --> 02:50:09,320 two, column three. So I'm going to put another zero, because this is at the 1806 02:50:09,320 --> 02:50:15,320 zero index of this first array. So I just put zero zero here. Now we can 1807 02:50:15,320 --> 02:50:21,320 print this guy out to the screen. And you'll see down here that we get that one. So I 1808 02:50:21,320 --> 02:50:25,320 could also do this for like for this eight, for example. So this is going to be zero 1809 02:50:25,320 --> 02:50:31,320 one two. It's going to be row two and column one. And now we should be able to 1810 02:50:31,320 --> 02:50:37,320 print out that eight. So that's basically how we can access elements inside of this 1811 02:50:37,320 --> 02:50:43,320 2D list. The next thing I want to show you guys is a nested for loop. And a nested 1812 02:50:43,320 --> 02:50:48,320 for loop is a situation, like I said, where we have a for loop inside of a for loop. 1813 02:50:48,320 --> 02:50:53,320 And I'll show you how we can use this nested for loop in order to print out all 1814 02:50:53,320 --> 02:50:57,320 the elements inside of this array. So I basically show you guys how to parse 1815 02:50:57,320 --> 02:51:02,320 through a two dimensional list or a two dimensional array. Alright, so basically 1816 02:51:02,320 --> 02:51:06,320 we're just going to create a normal for loop. So I'm going to say four, and I'm just 1817 02:51:06,320 --> 02:51:12,320 going to say row in number grid. And I'm using the word row here because I'm kind 1818 02:51:12,320 --> 02:51:18,320 of seeing these guys as rows, right? These are sort of horizontal rows in our little 1819 02:51:18,320 --> 02:51:24,320 grid. So for each of those rows, for each of the elements inside of number grid, I 1820 02:51:24,320 --> 02:51:28,320 want to loop through. So I could actually just print out each row right now. And it's 1821 02:51:28,320 --> 02:51:32,320 just going to print out all the rows inside of this list. You can see down here, 1822 02:51:32,320 --> 02:51:36,320 we basically just get exactly what we have up there. But I want to be able to 1823 02:51:36,320 --> 02:51:42,320 access each of these individual attributes inside of each of these array elements. 1824 02:51:42,320 --> 02:51:47,320 So we can actually create another for loop. And I'm going to put that for loop inside 1825 02:51:47,320 --> 02:51:55,320 of here. So now I'm going to say for column in row. And basically what this is going 1826 02:51:55,320 --> 02:52:01,320 to give us is each individual column or each individual element inside of these 1827 02:52:01,320 --> 02:52:06,320 arrays. So for example, inside of the arrays that are the elements of the number grid. 1828 02:52:06,320 --> 02:52:12,320 Hopefully that makes sense. So down here, now I can just print out column, and this 1829 02:52:12,320 --> 02:52:17,320 should actually print out every single value inside of this two dimensional array. 1830 02:52:17,320 --> 02:52:21,320 So let's go ahead and run this program. And down here, you'll see that we're 1831 02:52:21,320 --> 02:52:26,320 basically printing out one all the way down to zero. So we're printing out all 1832 02:52:26,320 --> 02:52:32,320 of the elements inside of all of the arrays inside of the number grid. So that's 1833 02:52:32,320 --> 02:52:36,320 how we can use two dimensional lists and nested four loops together. And both of 1834 02:52:36,320 --> 02:52:44,320 those things are actually pretty handy in Python. In this tutorial, I'm going to show 1835 02:52:44,320 --> 02:52:50,320 you how to build a basic translator in Python. So essentially, we can take in a 1836 02:52:50,320 --> 02:52:56,320 string so we can take in like a phrase or a word, and we'll be able to translate it 1837 02:52:56,320 --> 02:53:01,320 into a different language. So over here, I have this little file, and it basically 1838 02:53:01,320 --> 02:53:05,320 specifies this language that I made up. So I'm calling it the draft language. 1839 02:53:05,320 --> 02:53:12,320 And in the draft language, all vowels become G. So if I was going to translate an 1840 02:53:12,320 --> 02:53:18,320 English word into a draft word, for example, the word dog, I would look through the 1841 02:53:18,320 --> 02:53:23,320 word dog in any instances where there was a vowel. So in our case, let's just say 1842 02:53:23,320 --> 02:53:31,320 A E I O U, I would convert that vowel into a G. So dog would map to D G G, cat would 1843 02:53:31,320 --> 02:53:36,320 get mapped into C G T. So those are the basic translation rules. Obviously, this is 1844 02:53:36,320 --> 02:53:41,320 a pretty simple language, but you get the point any vowel is going to become a G, 1845 02:53:41,320 --> 02:53:46,320 and then we'll get the draft language. So we're going to build a draft translator. 1846 02:53:46,320 --> 02:53:51,320 Over here in our Python file, let's start making this translator. The first thing 1847 02:53:51,320 --> 02:53:56,320 I'm going to do is just define a translate function. So I'm going to make a function 1848 02:53:56,320 --> 02:54:01,320 here, and I'm just going to call it translate. And inside of these parentheses, 1849 02:54:01,320 --> 02:54:06,320 we want this to take one piece of information, which is going to be the phrase 1850 02:54:06,320 --> 02:54:11,320 that we want to translate. So I'm just going to say phrase. And now inside of this 1851 02:54:11,320 --> 02:54:17,320 function, we need to figure out like, okay, how can we translate English into our 1852 02:54:17,320 --> 02:54:23,320 draft language? Well, remember, the rules is basically just any vowels become G's. 1853 02:54:23,320 --> 02:54:27,320 So the first thing I want to do is create a variable. And this variable is going to 1854 02:54:27,320 --> 02:54:31,320 be the translation. So this will be like the final result that we're going to return 1855 02:54:31,320 --> 02:54:37,320 to the user. So I can say translation. For now, I'm just going to set it equal to 1856 02:54:37,320 --> 02:54:41,320 the empty string. So this is just going to be like a completely empty string. It's not 1857 02:54:41,320 --> 02:54:47,320 going to be doing anything. And now what we want to do is we basically want to 1858 02:54:47,320 --> 02:54:52,320 loop through every letter inside of this phrase. And if it's a vowel, we want to 1859 02:54:52,320 --> 02:54:56,320 change it to G. And if it's not a vowel, we want to leave it alone. And we're 1860 02:54:56,320 --> 02:55:00,320 essentially going to be looping through phrase. And we're going to be adding the 1861 02:55:00,320 --> 02:55:05,320 letters onto this translation one by one. So let me show you guys how we can do this. 1862 02:55:05,320 --> 02:55:09,320 I'm going to make a for loop. So I'm going to say four. And I'm just going to say 1863 02:55:09,320 --> 02:55:17,320 letter in phrase. And so now, when I loop through here, I can access each 1864 02:55:17,320 --> 02:55:23,320 individual letter inside of the phrase that they passed in. So what I want to 1865 02:55:23,320 --> 02:55:28,320 do is I want to check to see if the letter is a vowel or not, right? If it's a 1866 02:55:28,320 --> 02:55:34,320 vowel, then we can add a G onto translation. If it's not a vowel, then we can just add 1867 02:55:34,320 --> 02:55:39,320 on whatever letter it was onto translation anyway. So I'm going to use this if 1868 02:55:39,320 --> 02:55:44,320 statement. I'm going to say if. And there's actually this special thing we can do in 1869 02:55:44,320 --> 02:55:49,320 Python, we can check to see if something is in something else. So I can say if 1870 02:55:49,320 --> 02:55:55,320 letter in, and over here, I'm just going to type out all the vowels. So we'll say 1871 02:55:55,320 --> 02:56:02,320 A E I O U and A E I O U. So I'm basically checking to see if the letter is inside 1872 02:56:02,320 --> 02:56:08,320 of this string. And if the letter is inside of here, then we know that it's a vowel. 1873 02:56:08,320 --> 02:56:15,320 So down here, we can handle that case. So I can say, translation is equal to 1874 02:56:15,320 --> 02:56:24,320 translation plus G. Because if this letter is a vowel, we just want to convert it 1875 02:56:24,320 --> 02:56:32,320 into a G. Otherwise, though, I can basically just say, translation is equal to 1876 02:56:32,320 --> 02:56:39,320 translation plus letter. So in the case where we have a vowel, I'm adding a G onto 1877 02:56:39,320 --> 02:56:43,320 translation. In the case where we don't have a vowel, I'm just adding on whatever 1878 02:56:43,320 --> 02:56:49,320 letter it was onto translation. So finally, down below this for loop, we just want 1879 02:56:49,320 --> 02:56:56,320 to return the translation. So I'm just going to say return translation. And now we 1880 02:56:56,320 --> 02:57:01,320 have a awesome translate function, and it should probably work. So let's come down 1881 02:57:01,320 --> 02:57:05,320 here and we'll test it out. So why don't we allow a user to input some 1882 02:57:05,320 --> 02:57:09,320 information. So I'm basically going to call this function. Actually, we're going to 1883 02:57:09,320 --> 02:57:13,320 print it out. So we'll print this out. And I'm going to call this function 1884 02:57:13,320 --> 02:57:20,320 translate. And inside of here, I'm actually just going to pass whatever the user 1885 02:57:20,320 --> 02:57:26,320 inputs. So I can just say input. And inside of these parentheses, I can say the 1886 02:57:26,320 --> 02:57:32,320 prompt. So enter a phrase. And so now I'm basically combining all these statements 1887 02:57:32,320 --> 02:57:38,320 together. I'm saying I want to print out the translation of whatever the user 1888 02:57:38,320 --> 02:57:44,320 enters in. So let's go ahead and run this program, and we'll see how we do. So I'm 1889 02:57:44,320 --> 02:57:49,320 going to run the program. It says enter a phrase. Actually, let me format this a 1890 02:57:49,320 --> 02:57:55,320 little bit better. Okay. All right. So it says enter a 1891 02:57:55,320 --> 02:58:00,320 phrase. So let's go ahead and do that. So let's just type in the example we used 1892 02:58:00,320 --> 02:58:06,320 before. I'm going to say dog. And so now out pops the answer, which is d g g. So 1893 02:58:06,320 --> 02:58:10,320 that's kind of cool. And we can just keep running this as many times as we want. 1894 02:58:10,320 --> 02:58:15,320 So I could type in like to be or not to be. So this is like a little bit longer of 1895 02:58:15,320 --> 02:58:20,320 a string. And you can see it translates it into our draft language. So that's 1896 02:58:20,320 --> 02:58:25,320 basically how we can create our little translator app. And that's pretty cool. 1897 02:58:25,320 --> 02:58:30,320 And so you can see we're using a for loop in combination with an if loop. And 1898 02:58:30,320 --> 02:58:34,320 that's a really powerful structure. And actually, there's one place over here 1899 02:58:34,320 --> 02:58:39,320 where we can make this a little bit more efficient. So instead of saying if letter 1900 02:58:39,320 --> 02:58:44,320 in this string, you'll notice here, I'm checking to see if it's in all the lower 1901 02:58:44,320 --> 02:58:51,320 case and the uppercase. I could actually just say if letter dot lower in. And now 1902 02:58:51,320 --> 02:58:55,320 I only have to type out the lower case letters. And it's still going to do the 1903 02:58:55,320 --> 02:58:59,320 same exact thing. So that's a little like hack or whatever that we could use. And 1904 02:58:59,320 --> 02:59:03,320 there's also one more problem with this program. So you'll notice down here, 1905 02:59:03,320 --> 02:59:08,320 we're setting the translation equal to the translation plus g. But the problem is 1906 02:59:08,320 --> 02:59:14,320 if I was to start my word inside of the phrase with a capital vowel. So in other 1907 02:59:14,320 --> 02:59:19,320 words, if I said like on, you'll notice that when this gets printed out, it gets 1908 02:59:19,320 --> 02:59:25,320 printed out as lowercase g and so it's not keeping our uppercase syntax. And that's 1909 02:59:25,320 --> 02:59:29,320 kind of a problem. So we can actually use another if statement in here. And we 1910 02:59:29,320 --> 02:59:40,320 can check to see if letter dot is upper. And if the letter is uppercase, then we 1911 02:59:40,320 --> 02:59:48,320 can just set the translation equal to the translation plus a capital G. Otherwise, 1912 02:59:48,320 --> 02:59:54,320 we can just set it equal to the lowercase g. And so now we're able to control both 1913 02:59:54,320 --> 03:00:00,320 of those situations. So let's just test this and see if it works. Enter a phrase, 1914 03:00:00,320 --> 03:00:05,320 say on. And now we're keeping that capital letter. So those are just a couple 1915 03:00:05,320 --> 03:00:09,320 different ways that we could make this program a little bit better. But the 1916 03:00:09,320 --> 03:00:12,320 basic concept is we have a for loop and then we have some if loops inside of it. 1917 03:00:12,320 --> 03:00:17,320 And we're calling this awesome function that can translate a word for us. 1918 03:00:17,320 --> 03:00:24,320 In this tutorial, I want to talk to you guys about comments in Python. This is 1919 03:00:24,320 --> 03:00:28,320 going to be a pretty short tutorial. I just want to give you guys an overview of 1920 03:00:28,320 --> 03:00:33,320 what comments are and how we can use them in our Python programs. So over here in 1921 03:00:33,320 --> 03:00:38,320 my Python program, I just have this line here. It says print comments are fun. So 1922 03:00:38,320 --> 03:00:43,320 if I run my program down here inside of the console, it's going to print out 1923 03:00:43,320 --> 03:00:48,320 that comments are fun, right? So any code that I put over here is going to get 1924 03:00:48,320 --> 03:00:54,320 executed by Python. But there's actually a special line of code that we can write 1925 03:00:54,320 --> 03:00:59,320 and it's called a comment. And a comment is basically a line inside of our Python 1926 03:00:59,320 --> 03:01:03,320 file that's just not going to get rendered by Python. So when Python sees it, 1927 03:01:03,320 --> 03:01:06,320 it's just going to ignore it. It's not going to execute it. It's not going to 1928 03:01:06,320 --> 03:01:10,320 print anything out. It's just going to kind of ignore it. And comments are 1929 03:01:10,320 --> 03:01:15,320 going to be just used for us humans. So a comment is used for like me or another 1930 03:01:15,320 --> 03:01:20,320 developer to write a little comment, a little like plain text, you know, comment 1931 03:01:20,320 --> 03:01:25,320 inside of a file. In order to create a comment, I could use this hashtag symbol. 1932 03:01:25,320 --> 03:01:30,320 So I can click hashtag and now I am writing a comment. So I can write whatever I 1933 03:01:30,320 --> 03:01:37,320 wanted in here. I could say like this program is cool. And you'll notice when I 1934 03:01:37,320 --> 03:01:41,320 put on the program, that doesn't show up anywhere, right? It doesn't matter what 1935 03:01:41,320 --> 03:01:46,320 I put after this hashtag. This is just like ignored by Python. It's just for me 1936 03:01:46,320 --> 03:01:50,320 or another developer to come in here and use. So a lot of times if you want to 1937 03:01:50,320 --> 03:01:54,320 write like a little note inside of your Python files, or maybe you can write 1938 03:01:54,320 --> 03:01:59,320 yourself a little reminder, or if you want to write a line to like explain a line of 1939 03:01:59,320 --> 03:02:03,320 code. So for example, I could write a comment that would explain this print 1940 03:02:03,320 --> 03:02:08,320 statement. So I could say like this prints out a string, right? Now anybody looking 1941 03:02:08,320 --> 03:02:12,320 at my file would be able to read this and they'd be like, Oh, that's what this 1942 03:02:12,320 --> 03:02:18,320 does. Okay, cool. So comments are useful for leaving little comments in a Python 1943 03:02:18,320 --> 03:02:22,320 file. If you want to make comments on multiple lines, you can just create a new 1944 03:02:22,320 --> 03:02:26,320 line and use another hashtag. And so then I could type out whatever my next comment 1945 03:02:26,320 --> 03:02:30,320 was. And generally, if you're going to put comments on multiple lines, you want to 1946 03:02:30,320 --> 03:02:35,320 just use multiple hashtags. But there's actually another way that you can write 1947 03:02:35,320 --> 03:02:40,320 out comments. So you can use a triple quotation mark. So I'm typing out the 1948 03:02:40,320 --> 03:02:45,320 single quote. And I'm typing it out three times. So you can see here up here, we 1949 03:02:45,320 --> 03:02:50,320 have this three times, we have this three times. So in here, I can, you know, 1950 03:02:50,320 --> 03:02:54,320 write any comment that I wanted. So I could put, you know, as much text up here as I 1951 03:02:54,320 --> 03:02:59,320 would want. And same thing, when I run this program, it's just not going to get 1952 03:02:59,320 --> 03:03:05,320 rendered. So this is another way to make multiple line comments. But the, like, 1953 03:03:05,320 --> 03:03:10,320 official Python, like style guides or whatever, just recommends that if you're 1954 03:03:10,320 --> 03:03:14,320 going to put comments on multiple lines, you use these hashtags. So, you know, 1955 03:03:14,320 --> 03:03:17,320 you can really do whatever you want. But if you want to be like super Python 1956 03:03:17,320 --> 03:03:23,320 official, then use these hashtags. Comments can also be useful for doing something 1957 03:03:23,320 --> 03:03:27,320 called commenting out a line of code. A lot of times when you're writing a 1958 03:03:27,320 --> 03:03:32,320 program, you might have a line of code in there that is maybe like potentially 1959 03:03:32,320 --> 03:03:36,320 messing stuff up. And so what you can do is you can actually comment out that 1960 03:03:36,320 --> 03:03:41,320 line of code. So I could put a comment in front of this line of code. And now when 1961 03:03:41,320 --> 03:03:46,320 I run my program, the program is going to run, but it's not going to use this line. 1962 03:03:46,320 --> 03:03:50,320 So sometimes if you're trying to see if like a line of code is causing you trouble 1963 03:03:50,320 --> 03:03:54,320 or if you want to see what your program would be like without a line of code, or 1964 03:03:54,320 --> 03:03:58,320 even like a block of code, you can just comment it out. And that way you're not 1965 03:03:58,320 --> 03:04:03,320 actually deleting the code from your file. You're just commenting it out. So Python 1966 03:04:03,320 --> 03:04:07,320 is going to ignore it. And those are really the two uses for comments is to write 1967 03:04:07,320 --> 03:04:12,320 little notes, write little comments about different things or to comment out code. 1968 03:04:12,320 --> 03:04:17,320 And as you go forward in Python, I'm sure you'll find uses for comments. 1969 03:04:17,320 --> 03:04:25,320 In this tutorial, I want to talk to you guys about catching errors in Python. 1970 03:04:25,320 --> 03:04:29,320 Now, a lot of times when we're running Python programs, you'll encounter different 1971 03:04:29,320 --> 03:04:33,320 errors. So different situations could come up and your program might throw an error 1972 03:04:33,320 --> 03:04:37,320 or it might throw an exception. And a lot of times when these situations happen, 1973 03:04:37,320 --> 03:04:42,320 they'll completely stop your program from running. And what we can actually do is 1974 03:04:42,320 --> 03:04:47,320 we can watch out for, you know, certain specific errors that are going to pop up 1975 03:04:47,320 --> 03:04:51,320 in our program and we can actually handle them. So instead of our program just 1976 03:04:51,320 --> 03:04:55,320 breaking and stop executing, we can actually handle those errors and do things 1977 03:04:55,320 --> 03:04:59,320 when they occur. So I want to show you guys an example of using this down here. 1978 03:04:59,320 --> 03:05:04,320 I'm saying number is equal to int input enter a number. Basically what's happening 1979 03:05:04,320 --> 03:05:09,320 is I'm prompting the user to enter in a number using this input command. And then 1980 03:05:09,320 --> 03:05:14,320 I'm converting whatever they entered into an integer. So as long as they enter in 1981 03:05:14,320 --> 03:05:18,320 like a valid integer, I can convert it into an integer. So I can convert it into 1982 03:05:18,320 --> 03:05:22,320 like a whole number and I can store it inside of this number variable that I'm 1983 03:05:22,320 --> 03:05:25,320 just printing it out. So let's try to execute this program. I'm going to run 1984 03:05:25,320 --> 03:05:30,320 the program and you'll see down here it says enter a number. So we can enter in 1985 03:05:30,320 --> 03:05:34,320 like four, for example. And when I enter in four, everything's going to work and 1986 03:05:34,320 --> 03:05:38,320 it's just going to print out four and everything is right in the world. And 1987 03:05:38,320 --> 03:05:42,320 actually, I'm going to modify this up here. So it's a little easier to see. But 1988 03:05:42,320 --> 03:05:46,320 let's say we run the program again. So it says, okay, enter your number. Let's say 1989 03:05:46,320 --> 03:05:50,320 that I break the rules and I don't enter a number. So I just ignore what the 1990 03:05:50,320 --> 03:05:56,320 prompt says and I put, you know, some random text in here. Now when I enter this, 1991 03:05:56,320 --> 03:06:00,320 the program is going to throw an error. You can see down here, it says value error 1992 03:06:00,320 --> 03:06:06,320 invalid literal for int base with base 10. Basically, hey, you didn't enter in an 1993 03:06:06,320 --> 03:06:10,320 integer like you said you would, right? We told them to enter in a number. They 1994 03:06:10,320 --> 03:06:16,320 entered in text. So it broke the program. And this is a situation that happens a 1995 03:06:16,320 --> 03:06:19,320 lot. So there's a lot of different situations where something can just go 1996 03:06:19,320 --> 03:06:23,320 wrong in our program. And I'm until now we've just kind of accepted it as a 1997 03:06:23,320 --> 03:06:28,320 reality. But if you're writing a real live Python program, you don't want something 1998 03:06:28,320 --> 03:06:31,320 like this to trip up your program. You don't want the entire program to break when 1999 03:06:31,320 --> 03:06:35,320 somebody just forgets to enter in a number, right? You want to be able to handle 2000 03:06:35,320 --> 03:06:39,320 all these different situations. And in order to handle them, we can use something 2001 03:06:39,320 --> 03:06:44,320 called a try except block. And a try except block will basically allow your 2002 03:06:44,320 --> 03:06:49,320 program to try out a piece of code. And if everything goes well, then we're great. 2003 03:06:49,320 --> 03:06:53,320 Like we could try out entering in the number. And if the user enters in the 2004 03:06:53,320 --> 03:06:57,320 correct number, great. But if they don't enter in the correct number, then we can 2005 03:06:57,320 --> 03:07:00,320 basically like account for that. So they entered in something like a string when 2006 03:07:00,320 --> 03:07:04,320 they're supposed to enter in a number, we could tell them like, Hey, you entered 2007 03:07:04,320 --> 03:07:08,320 in the wrong thing. So I'm going to show you guys how we can use these try except 2008 03:07:08,320 --> 03:07:12,320 blocks. I'm going to come up here, and I'm just going to say try. I'm going to make 2009 03:07:12,320 --> 03:07:17,320 it colon. And I want to indent both of these things. So both of these things are 2010 03:07:17,320 --> 03:07:21,320 going to be inside of this try block. Now I'm going to make a new line. I'm going 2011 03:07:21,320 --> 03:07:25,320 to come down here and I'm just going to type out. Except, and I'm also going to 2012 03:07:25,320 --> 03:07:29,320 put a colon here. And down here, I'm just going to print something out. So I'll just 2013 03:07:29,320 --> 03:07:36,320 say like invalid input. So now, since I put this code inside of this try block, 2014 03:07:36,320 --> 03:07:40,320 if the user enters in something wrong, it's going to be able to catch it. So it'll 2015 03:07:40,320 --> 03:07:45,320 be able to catch what the user does wrong and print out invalid input onto the 2016 03:07:45,320 --> 03:07:49,320 screen. So let's go ahead and run our program. And it's just going to say enter 2017 03:07:49,320 --> 03:07:53,320 a number. So we're going to ignore that. We'll enter this in. And now instead of 2018 03:07:53,320 --> 03:07:58,320 yelling at us and breaking the program, it just says, Hey, invalid input. So it was 2019 03:07:58,320 --> 03:08:03,320 able to handle the error they got called. It was able to handle the invalid input 2020 03:08:03,320 --> 03:08:09,320 error. Basically just told us, Hey, no more input. So this is a really powerful 2021 03:08:09,320 --> 03:08:13,320 thing that we can do in our Python programs. Basically just to protect our 2022 03:08:13,320 --> 03:08:17,320 programs, right? If I want to have my program running for long period of time, 2023 03:08:17,320 --> 03:08:21,320 I can't have something as simple as an invalid input, trip it up. So I can 2024 03:08:21,320 --> 03:08:25,320 catch that invalid input using these try accept blocks. But here's the thing. 2025 03:08:25,320 --> 03:08:29,320 You'll notice that this little guy down here is getting highlighted. And let's 2026 03:08:29,320 --> 03:08:34,320 just see what it has to say. So it's basically saying to broad exception clause. 2027 03:08:34,320 --> 03:08:39,320 And this is a message that we're getting because when I just say accept, this 2028 03:08:39,320 --> 03:08:43,320 will catch any error under the sun. So anything that can go wrong on my program 2029 03:08:43,320 --> 03:08:47,320 for the most part, this is going to accept it. So let me give you guys an 2030 03:08:47,320 --> 03:08:52,320 example. Let's say I had in my program, I was creating a variable, right? So I 2031 03:08:52,320 --> 03:08:57,320 wanted to create a variable called value. And I set it equal to 10 divided by 2032 03:08:57,320 --> 03:09:00,320 zero. Now, if you know anything about math, you'll know that you can't divide a 2033 03:09:00,320 --> 03:09:05,320 number by zero. It's not possible. So the program isn't going to be able to 2034 03:09:05,320 --> 03:09:08,320 handle this. And you'll see that this is going to throw an error for us. So when I 2035 03:09:08,320 --> 03:09:13,320 run the program, it basically says zero division error division by zero. We're 2036 03:09:13,320 --> 03:09:18,320 not allowed to do that, right? If I took this and I put this down inside of this 2037 03:09:18,320 --> 03:09:23,320 dry block, and I just pasted it in here, it's going to get caught by this 2038 03:09:23,320 --> 03:09:27,320 exception that we wrote down here. So it's going to get caught by that 2039 03:09:27,320 --> 03:09:32,320 accept. So when I run the program, you'll see it says invalid input. But the 2040 03:09:32,320 --> 03:09:37,320 problem is that we didn't input something that was invalid, right? We just divided 2041 03:09:37,320 --> 03:09:42,320 by zero, but it wasn't able to handle that. Right. So it could not divide by zero 2042 03:09:42,320 --> 03:09:46,320 it through an error. So even though we didn't get an invalid input, even though 2043 03:09:46,320 --> 03:09:50,320 it was because we divided by zero, it's still printed this out. And this brings 2044 03:09:50,320 --> 03:09:55,320 me up to another point is we can actually catch, or we can accept specific 2045 03:09:55,320 --> 03:10:00,320 types of errors. So for example, in this case, we got a division by zero error. In 2046 03:10:00,320 --> 03:10:05,320 this case, we got an invalid input error, right? We put something in that was wrong. 2047 03:10:05,320 --> 03:10:09,320 You couldn't convert the value. So I can create two different accept blocks to 2048 03:10:09,320 --> 03:10:14,320 catch two different types of errors. So down here, I can just make a space and I 2049 03:10:14,320 --> 03:10:19,320 can specify the type of error that I want to catch. So when we divided by zero, it 2050 03:10:19,320 --> 03:10:25,320 was zero division error. So if I just put zero division error in here, now it's 2051 03:10:25,320 --> 03:10:31,320 going to catch that zero division error. So I can say divided by zero. I could 2052 03:10:31,320 --> 03:10:36,320 also make another accept. So I could say accept. And we can catch the other 2053 03:10:36,320 --> 03:10:41,320 exception that we got. So I think it was a value error, right? It was, we put in 2054 03:10:41,320 --> 03:10:47,320 the type of value inside of that input. So now we can print out invalid input. So 2055 03:10:47,320 --> 03:10:52,320 with these two accept blocks, we're basically able to handle both of these 2056 03:10:52,320 --> 03:10:57,320 situations. So for example, if this ends up breaking the program, this division by 2057 03:10:57,320 --> 03:11:01,320 zero, then it'll be able to catch that. So down here, it'll tell us, Hey, you 2058 03:11:01,320 --> 03:11:07,320 divided by zero. But if I was to get rid of this line of code, and I just entered 2059 03:11:07,320 --> 03:11:11,320 in the number wrong. So I came down here and I entered in a string instead of a 2060 03:11:11,320 --> 03:11:15,320 number. Now it's going to tell us invalid input. So we're going to be able to 2061 03:11:15,320 --> 03:11:20,320 specify what happens when certain things break. So when we get a valid, when we 2062 03:11:20,320 --> 03:11:24,320 get a value error, we can say invalid input, when we get a zero division error, we 2063 03:11:24,320 --> 03:11:29,320 can say divided by zero. So depending on what breaks, depending on what happens, 2064 03:11:29,320 --> 03:11:34,320 we can actually do different things. And I want to show you guys one more thing 2065 03:11:34,320 --> 03:11:40,320 we can do, which is we can actually store this error as a variable. So I can say 2066 03:11:40,320 --> 03:11:45,320 accept, I can say as, and I can type in a name here. So a lot of times people will 2067 03:11:45,320 --> 03:11:51,320 just say like, e r r. And down here, I can print out the actual error. So I can just 2068 03:11:51,320 --> 03:11:58,320 print e r r. And so if we end up dividing by zero. So if I say answers equal to 2069 03:11:58,320 --> 03:12:03,320 10 divided by zero, we're going to throw this error, and it's going to print out 2070 03:12:03,320 --> 03:12:08,320 what went wrong. So now I can run this program. And it just says division by zero. 2071 03:12:08,320 --> 03:12:13,320 So it just printed out the error that we got. So that's another useful thing you 2072 03:12:13,320 --> 03:12:17,320 can do is you can actually like print out the specific error that got thrown. Now, 2073 03:12:17,320 --> 03:12:22,320 a best practice in Python is to use these specific errors. So you always want to 2074 03:12:22,320 --> 03:12:28,320 accept and accept for a specific error. What you don't want to do is just say 2075 03:12:28,320 --> 03:12:35,320 accept, and then just accept anything under the sun. It's just too broad. And it's 2076 03:12:35,320 --> 03:12:39,320 not a good practice in Python. So you always want to be catching specific errors like 2077 03:12:39,320 --> 03:12:47,320 this. In this tutorial, I want to talk to you guys about reading from external 2078 03:12:47,320 --> 03:12:52,320 files in Python. Now a lot of times in Python, you're going to want to read from 2079 03:12:52,320 --> 03:12:57,320 files that are outside of your Python file. So you might want to read information 2080 03:12:57,320 --> 03:13:03,320 from like a text file or a CSV file or like an HTML file. And you can actually use 2081 03:13:03,320 --> 03:13:09,320 something called the Python read command. And it will allow you to read a file that 2082 03:13:09,320 --> 03:13:14,320 is stored outside of your Python file. So you can use these files to get information 2083 03:13:14,320 --> 03:13:18,320 or you can parse through different files and do different things. So I'm going to 2084 03:13:18,320 --> 03:13:23,320 show you guys the basics of reading files, opening files, closing files, doing all 2085 03:13:23,320 --> 03:13:30,320 that stuff. Over here, I have this file called employees dot text. And it basically 2086 03:13:30,320 --> 03:13:34,320 just lists out a bunch of different employees, like these could be employees in 2087 03:13:34,320 --> 03:13:39,320 an office or whatever. So it's just listing out all of this information. So let's say 2088 03:13:39,320 --> 03:13:45,320 that inside of my app dot Python file, I wanted to read the employees inside of 2089 03:13:45,320 --> 03:13:50,320 that file. The first thing I have to do is actually open that file from inside 2090 03:13:50,320 --> 03:13:56,320 of Python. So I can use a special command called open. So I can say open. And then 2091 03:13:56,320 --> 03:14:00,320 in here, I can type in the name of the file that I want to open. So this is either 2092 03:14:00,320 --> 03:14:05,320 going to be a relative path to the file, an absolute path to the file, or just the 2093 03:14:05,320 --> 03:14:11,320 files name if both files are in the same directory. So in my case, app dot Python 2094 03:14:11,320 --> 03:14:16,320 and employees dot text are in the same folder, like they're in the same directory. 2095 03:14:16,320 --> 03:14:21,320 So I can just type out the name of the file. I can just say employees dot text. 2096 03:14:21,320 --> 03:14:26,320 And then I want to put one more thing inside of this open function. And it's 2097 03:14:26,320 --> 03:14:30,320 going to be the mode that I want to open the file in. So you can actually open 2098 03:14:30,320 --> 03:14:35,320 files in a couple of different modes. And the first mode is called read. So I can 2099 03:14:35,320 --> 03:14:39,320 just put an R here and that's going to stand for read. And this basically means 2100 03:14:39,320 --> 03:14:43,320 that I only want to read the information inside the file. I don't want to modify 2101 03:14:43,320 --> 03:14:47,320 it. I don't want to change it. I just want to read it. I just want to see what's in 2102 03:14:47,320 --> 03:14:52,320 the file and do some stuff with that information. Another mode is called 2103 03:14:52,320 --> 03:14:56,320 write. So I can type in this w and writing basically means that you can change the 2104 03:14:56,320 --> 03:15:01,320 file, right? You can write new information. You can change existing information. 2105 03:15:01,320 --> 03:15:06,320 There's another one called a and a stands for append. And this basically means 2106 03:15:06,320 --> 03:15:11,320 that you can append information onto the end of the file. So you can't modify 2107 03:15:11,320 --> 03:15:15,320 any of the information in the file. You can't change any of the information, but 2108 03:15:15,320 --> 03:15:20,320 you can add new information. And there's one more, which is R plus. And this 2109 03:15:20,320 --> 03:15:25,320 basically means read and write. So this will give you all the power of reading 2110 03:15:25,320 --> 03:15:30,320 and writing. So in our case, we're just going to be working with regular R. So 2111 03:15:30,320 --> 03:15:35,320 we're going to be reading from the file. Now this open function will essentially 2112 03:15:35,320 --> 03:15:40,320 just open the file. So it's going to like go over to that file inside of our file 2113 03:15:40,320 --> 03:15:44,320 system, open it up, and it'll allow us to read through it. But generally, we're 2114 03:15:44,320 --> 03:15:50,320 going to want to store this opened file inside of a variable. So I can create a 2115 03:15:50,320 --> 03:15:55,320 variable and we can just call it employee file. And I'm just going to set it equal 2116 03:15:55,320 --> 03:16:01,320 to this open function. So now the open employees dot text file and all the 2117 03:16:01,320 --> 03:16:07,320 content inside of it is stored inside of this employee file variable. Now, whenever 2118 03:16:07,320 --> 03:16:12,320 you open a file, you always want to make sure that you close the file as well. So 2119 03:16:12,320 --> 03:16:18,320 just like we have this open command, we also have an close function. So I can come 2120 03:16:18,320 --> 03:16:24,320 down here and say employee file dot closed. And this is essentially just going to 2121 03:16:24,320 --> 03:16:29,320 close the file. So we're no longer going to be able to access it. And generally, 2122 03:16:29,320 --> 03:16:33,320 it's a good idea whenever you're opening up a file, you want to also make sure that 2123 03:16:33,320 --> 03:16:37,320 you're closing the file at some point. So generally, once you're done reading it, 2124 03:16:37,320 --> 03:16:41,320 you can just close it. So that's kind of like how we can open and close a file. 2125 03:16:41,320 --> 03:16:45,320 Now, let's talk about how we can get information from the file, right? There's no 2126 03:16:45,320 --> 03:16:48,320 point in having the file that we can't figure out what's in it. So there's actually 2127 03:16:48,320 --> 03:16:53,320 a few different functions that we can use on this employee file to figure out what's 2128 03:16:53,320 --> 03:16:56,320 inside of it. And I'm going to show you guys some of those. So I'm just going to 2129 03:16:56,320 --> 03:17:01,320 make a print statement. And inside this print statement will basically just print out 2130 03:17:01,320 --> 03:17:07,320 some information about the employee file. So the most basic thing we can print out 2131 03:17:07,320 --> 03:17:11,320 is just the entire contents of the file. But before I do that, I want to show you 2132 03:17:11,320 --> 03:17:16,320 guys how you can check to make sure that a file is readable. So before we do anything 2133 03:17:16,320 --> 03:17:21,320 else, generally, it's a good idea to make sure that it's possible to read this file. 2134 03:17:21,320 --> 03:17:26,320 And there's a function inside of Python, we can use called readable. So I'm just 2135 03:17:26,320 --> 03:17:34,320 going to type out employee file dot readable. And this is going to return a 2136 03:17:34,320 --> 03:17:38,320 Boolean value. And it's going to tell us whether or not we can read from this file. 2137 03:17:38,320 --> 03:17:43,320 So I'm going to run my program. And you'll see down here, we get a value of true. And 2138 03:17:43,320 --> 03:17:48,320 that's because we set the file with a read mode. So it's in read mode, we can read 2139 03:17:48,320 --> 03:17:54,320 from it. If I was to put a double you here. So if I put like, right, now readable is 2140 03:17:54,320 --> 03:17:58,320 going to be false, because we can no longer read the file, we can only write to the 2141 03:17:58,320 --> 03:18:04,320 file. So I'm going to change this back to our so we can just read it. So once you 2142 03:18:04,320 --> 03:18:09,320 figure out whether or not the file can be read from, let's actually read it. So 2143 03:18:09,320 --> 03:18:13,320 there's another function called employee file dot read. And this is basically just 2144 03:18:13,320 --> 03:18:17,320 going to spit out all the information in the files. So when I run the program, it's 2145 03:18:17,320 --> 03:18:21,320 just going to spit out all of this information, right, all the information that 2146 03:18:21,320 --> 03:18:26,320 was in that file. I can also come down here and we can read an individual line 2147 03:18:26,320 --> 03:18:31,320 inside this file. So I can say employee file dot read line. And what this is going 2148 03:18:31,320 --> 03:18:36,320 to do is it's going to read an individual line inside of this file. So now when I 2149 03:18:36,320 --> 03:18:41,320 run this program, you'll see it's just reading that first line in the file. And 2150 03:18:41,320 --> 03:18:45,320 this read line function is actually just reading the first line. And then it's 2151 03:18:45,320 --> 03:18:50,320 basically like moving a little cursor onto the next line. So if I was to copy this 2152 03:18:50,320 --> 03:18:56,320 code and then print it again down here, I'm saying employee file dot read line. So 2153 03:18:56,320 --> 03:19:00,320 it's going to read the first line. And then when I say it again, it's going to read 2154 03:19:00,320 --> 03:19:03,320 the line after that. So this is actually going to end up printing out the first 2155 03:19:03,320 --> 03:19:08,320 two lines in the file. So when I run this program, you'll see we print out Jim 2156 03:19:08,320 --> 03:19:13,320 salesman and Dwight salesman. So if I was to do this multiple times, like I could 2157 03:19:13,320 --> 03:19:19,320 technically print out every line inside of this file. And you can see we can do 2158 03:19:19,320 --> 03:19:23,320 that. And so that can be pretty useful for reading multiple lines in a file. But 2159 03:19:23,320 --> 03:19:27,320 there's actually another function that is better at doing that. And we can say 2160 03:19:27,320 --> 03:19:32,320 instead of employee file dot read line, we can say dot read lines. And what this is 2161 03:19:32,320 --> 03:19:36,320 going to do is it's going to take all of the lines inside of our file and put them 2162 03:19:36,320 --> 03:19:40,320 inside of an array. And so now when I print this out, you'll see we have this 2163 03:19:40,320 --> 03:19:45,320 array down here. It says Jim salesman, that's the first item in the array, Dwight 2164 03:19:45,320 --> 03:19:50,320 salesman, the second item in the array, it's basically taking each line and putting 2165 03:19:50,320 --> 03:19:54,320 it inside of an array. So if I wanted to access a specific line, I can just 2166 03:19:54,320 --> 03:19:59,320 refer to it by its index in the array. So if I said one, now this is going to give 2167 03:19:59,320 --> 03:20:04,320 us that Dwight salesman line, because that is at index position one inside of the 2168 03:20:04,320 --> 03:20:10,320 array. You can also use this read lines function with a for loop. So I can come up 2169 03:20:10,320 --> 03:20:16,320 here and create a for loop. I'm just going to say for, and we'll say employee in 2170 03:20:16,320 --> 03:20:22,320 employee file. And then for each employee, we just want to print them out. So and 2171 03:20:22,320 --> 03:20:28,320 actually started, we have to put employee file dot read lines up here. And so now this 2172 03:20:28,320 --> 03:20:32,320 will loop through all of the employees in this employee file that read lines array. 2173 03:20:32,320 --> 03:20:38,320 So we can actually just print out the individual employee. And now this will print 2174 03:20:38,320 --> 03:20:42,320 out all the employees inside of that file. So it's basically printing out each 2175 03:20:42,320 --> 03:20:47,320 line in the file. And that can be pretty useful. So you can use all of these 2176 03:20:47,320 --> 03:20:52,320 different functions like read, read line, read lines, readable, you know, there's a 2177 03:20:52,320 --> 03:20:56,320 bunch of these different things that we can do to get information from a file. And 2178 03:20:56,320 --> 03:20:59,320 so there's a lot of cases where you're going to want to be able to parse through 2179 03:20:59,320 --> 03:21:03,320 information in a file. And this is a awesome way to do it. So just to recap, 2180 03:21:03,320 --> 03:21:07,320 whenever you want to open a file and read from it, you can just use this open 2181 03:21:07,320 --> 03:21:11,320 function, type in the name of the file and then the mode, which in our case is going 2182 03:21:11,320 --> 03:21:15,320 to be our, then you can do all sorts of stuff with it. And you always want to make 2183 03:21:15,320 --> 03:21:19,320 sure that you close it when you're done. That's just good practice. So that's the 2184 03:21:19,320 --> 03:21:24,320 basics of reading from files. And hopefully you guys can use this in some way, shape 2185 03:21:24,320 --> 03:21:32,320 or form in the future. In this tutorial, I want to talk to you about writing and 2186 03:21:32,320 --> 03:21:38,320 appending to files in Python. So one of the cool things about Python is it allows you 2187 03:21:38,320 --> 03:21:43,320 to work with external files. So I could have like an external text file and I could 2188 03:21:43,320 --> 03:21:47,320 actually like completely read all of the information in it. I could parse through 2189 03:21:47,320 --> 03:21:52,320 it, use that information to do certain things. But in addition to reading a file, I could 2190 03:21:52,320 --> 03:21:56,320 also write a file. And that's what I want to talk to you guys about today is writing 2191 03:21:56,320 --> 03:22:01,320 new files and appending onto existing files. Over here, I basically just have some 2192 03:22:01,320 --> 03:22:06,320 written out and this essentially just reads information from this employees.text file. 2193 03:22:06,320 --> 03:22:11,320 So you can see over here, I'm specifying the mode, which is R and that stands for 2194 03:22:11,320 --> 03:22:15,320 read. And then down here, I'm just reading all of the contents of the file and 2195 03:22:15,320 --> 03:22:19,320 spitting it out on the screen. So I'm going to click the play button over here and 2196 03:22:19,320 --> 03:22:25,320 you'll see that this executes. So it's printing out all of the lines of code in 2197 03:22:25,320 --> 03:22:30,320 our text file. So over here, I'm in this employees.text file and it just has all 2198 03:22:30,320 --> 03:22:34,320 this information, like employees in an office. But let's say that I wanted to 2199 03:22:34,320 --> 03:22:39,320 add another employee onto here, right? Let's say that a new employee joined our 2200 03:22:39,320 --> 03:22:43,320 company. So we wanted to add them onto this list. Well, I can come over here to 2201 03:22:43,320 --> 03:22:49,320 my app.python file. And instead of reading from the file, I want to append to the 2202 03:22:49,320 --> 03:22:54,320 file. So I want to say a and appending to the file basically means that you're 2203 03:22:54,320 --> 03:22:59,320 adding some text at the end of the file. So wherever the file ends, you're just 2204 03:22:59,320 --> 03:23:03,320 going to add some text onto there. So what we can do is we can actually add 2205 03:23:03,320 --> 03:23:08,320 another employee into the file. So instead of printing something out, I'm 2206 03:23:08,320 --> 03:23:15,320 actually just going to say employee file dot right. And when I say employee file 2207 03:23:15,320 --> 03:23:20,320 dot right, I'm going to be able to write something to the end of the file. So I 2208 03:23:20,320 --> 03:23:25,320 can basically just write whatever I want. So why don't we add in another employee 2209 03:23:25,320 --> 03:23:29,320 into our employees dot text file. So we can add in another employee. Why don't we 2210 03:23:29,320 --> 03:23:38,320 say like Toby, and he's going to be in human resources. So now, when I run this 2211 03:23:38,320 --> 03:23:43,320 program, it's going to add Toby human resources onto the end of the file. So I'm 2212 03:23:43,320 --> 03:23:47,320 going to run my program. And you'll see that nothing shows up down here in the 2213 03:23:47,320 --> 03:23:53,320 console. But if I go over to my employees dot text file, all of a sudden we have a 2214 03:23:53,320 --> 03:23:58,320 new entry over here, it's Toby from human resources, right? So I was able to 2215 03:23:58,320 --> 03:24:03,320 append a line onto the end of this file. But here's the thing, you need to be 2216 03:24:03,320 --> 03:24:07,320 careful when you're writing to files, because you can actually mess up a file 2217 03:24:07,320 --> 03:24:12,320 very easily. For example, I already added Toby here into my file. But if I was to 2218 03:24:12,320 --> 03:24:17,320 run this program again, you'll see that over here in this employees dot text 2219 03:24:17,320 --> 03:24:22,320 file, it went ahead and added Toby again, right? So it added this employee here 2220 03:24:22,320 --> 03:24:27,320 again. And also, you'll notice that in this case, this employee didn't go on to 2221 03:24:27,320 --> 03:24:32,320 the next line. I accidentally ran my file again, and all of a sudden it messed up 2222 03:24:32,320 --> 03:24:37,320 this file over here. And so appending, you really need to be careful because if you 2223 03:24:37,320 --> 03:24:41,320 actually run your file again, or if you, you know, append something on something 2224 03:24:41,320 --> 03:24:46,320 wrong to the file, it's permanent, like it's getting saved inside of the file. So 2225 03:24:46,320 --> 03:24:50,320 I want to talk to you guys a little bit more about, you know, appending. Another 2226 03:24:50,320 --> 03:24:54,320 thing we could do, let's say we wanted to add another employee. And you'll notice 2227 03:24:54,320 --> 03:24:59,320 over here in this employees dot text file. When I appended it on again, it got 2228 03:24:59,320 --> 03:25:04,320 appended to the end of the existing line, right? So the first time I had a new 2229 03:25:04,320 --> 03:25:08,320 line there, but if you don't have a new line at the end of your file, and you all 2230 03:25:08,320 --> 03:25:11,320 and you want to add a new line, you're going to have to add some special 2231 03:25:11,320 --> 03:25:16,320 characters. So let's add another employee, and we'll call her Kelly, and let's just 2232 03:25:16,320 --> 03:25:25,320 say Kelly is in customer service. So Kelly is going to be in customer service. 2233 03:25:25,320 --> 03:25:30,320 And if I want to add this employee onto the end of the file in a new line, I'm 2234 03:25:30,320 --> 03:25:35,320 going to have to put a new line character in front of it. So I can say backslash n, 2235 03:25:35,320 --> 03:25:40,320 and this will append this entry into the file with a new line. So on a new line. 2236 03:25:40,320 --> 03:25:45,320 So now when I run this, you'll see we get Kelly from customer service on her own 2237 03:25:45,320 --> 03:25:49,320 line. So you want to make sure that you're aware of like these special 2238 03:25:49,320 --> 03:25:53,320 characters that you can use, they call them escape characters. And you know, 2239 03:25:53,320 --> 03:25:56,320 anytime you're adding onto a file, you want to make sure that you're adding on 2240 03:25:56,320 --> 03:26:01,320 exactly where you want to add on. So in addition to appending to a file, I could 2241 03:26:01,320 --> 03:26:07,320 also just like overwrite a file or I could write an entirely new file. So since we 2242 03:26:07,320 --> 03:26:11,320 already have this open, instead of appending to the file, why don't we just 2243 03:26:11,320 --> 03:26:18,320 write a file? So I'm going to use this W. And now if I say employee file dot right, 2244 03:26:18,320 --> 03:26:23,320 because I'm using W and I'm not using a, it's actually going to override the 2245 03:26:23,320 --> 03:26:28,320 entire file. And it's only going to put this inside the file. So when I run this, 2246 03:26:28,320 --> 03:26:32,320 and we go over to this employees dot text file, you'll see we only have one line 2247 03:26:32,320 --> 03:26:37,320 inside of this file. Now it's just Kelly customer service. That's because I was 2248 03:26:37,320 --> 03:26:42,320 using W not a when you use W, it's just overriding everything that's in that 2249 03:26:42,320 --> 03:26:48,320 existing file. You can also use W to create a new file. So over here, I could say 2250 03:26:48,320 --> 03:26:54,320 employee file is equal to employees one dot text. And now what's going to happen 2251 03:26:54,320 --> 03:26:58,320 is when I run this, it's going to create another file for me. So I'm going to run 2252 03:26:58,320 --> 03:27:03,320 this. And you'll see over here in my file browser, we have this new file 2253 03:27:03,320 --> 03:27:08,320 employees one dot text. So if I open this up, it has exactly the same stuff as in 2254 03:27:08,320 --> 03:27:13,320 this employees file. But it basically created a new file for us. And so a lot of 2255 03:27:13,320 --> 03:27:17,320 times you're going to want to create a new file. And you can use different 2256 03:27:17,320 --> 03:27:23,320 extensions too. So if I wanted to create like a web page, I could say index dot 2257 03:27:23,320 --> 03:27:28,320 HTML. And I could also add in some HTML code in here. So if you don't understand 2258 03:27:28,320 --> 03:27:33,320 HTML, don't worry about it. But if you do, I could put like a paragraph in here, 2259 03:27:33,320 --> 03:27:40,320 another paragraph, like this is HTML, basically HTML is like a web page. And 2260 03:27:40,320 --> 03:27:44,320 the point I'm trying to make is that you could write out a web page inside of Python 2261 03:27:44,320 --> 03:27:49,320 by doing something like this. So now when I play this, we get this index dot HTML 2262 03:27:49,320 --> 03:27:55,320 file. And it has some HTML inside of it. So that's one way that writing to files 2263 03:27:55,320 --> 03:27:59,320 can be really useful. You can overwrite an existing file, you can write a new 2264 03:27:59,320 --> 03:28:05,320 file and create it, or you can append onto the end of a file. And there's tons of 2265 03:28:05,320 --> 03:28:09,320 applications for writing to files and Python's a great language for working with 2266 03:28:09,320 --> 03:28:17,320 reading, writing, and doing all that stuff with files. In this tutorial, I want to 2267 03:28:17,320 --> 03:28:22,320 talk to you guys about using modules in Python. Now, a module is essentially just 2268 03:28:22,320 --> 03:28:28,320 a Python file that we can import into our current Python file. So for example, if I 2269 03:28:28,320 --> 03:28:33,320 wrote a Python file that had a bunch of like useful functions or useful variables 2270 03:28:33,320 --> 03:28:38,320 or, you know, other things like that, I could take that file, I could import it 2271 03:28:38,320 --> 03:28:42,320 into the file I'm currently working on, and I could actually access all of those 2272 03:28:42,320 --> 03:28:47,320 functions, all of those variables, all the stuff from that external file inside the 2273 03:28:47,320 --> 03:28:51,320 file that I'm currently working on. So I'm going to show you guys how we can use 2274 03:28:51,320 --> 03:28:55,320 modules, and then we'll talk about like, you know, where you can find awesome 2275 03:28:55,320 --> 03:29:01,320 modules and really why modules make Python an awesome language. So over here in my 2276 03:29:01,320 --> 03:29:07,320 text editor, I actually created this file and it's called useful tools dot Python. 2277 03:29:07,320 --> 03:29:12,320 And basically this file just has a bunch of sort of like useful tools that I might 2278 03:29:12,320 --> 03:29:16,320 want to use in one of my programs. So you'll see over here, I have some 2279 03:29:16,320 --> 03:29:20,320 variables. This one is telling me how many feet are in a mile. This one's telling me 2280 03:29:20,320 --> 03:29:24,320 how many meters are in a kilometer. And then we have this list here, which lists 2281 03:29:24,320 --> 03:29:29,320 out all of the members of the Beatles. I also have a couple different functions 2282 03:29:29,320 --> 03:29:34,320 down here. So I have this get file extension function. And this basically just will, 2283 03:29:34,320 --> 03:29:37,320 you know, you give it a file name, it'll tell you what the extension is. And then we 2284 03:29:37,320 --> 03:29:41,320 have this other function down here, which simulates rolling a dice. So you pass 2285 03:29:41,320 --> 03:29:45,320 it a number. If I pass it like a six, it would roll a six sided dice. If I pass 2286 03:29:45,320 --> 03:29:49,320 it a nine, it roll a nine sided dice, etc. So this is like a Python file that I 2287 03:29:49,320 --> 03:29:53,320 wrote. And it has some useful stuff in it. And honestly, there's a lot of stuff 2288 03:29:53,320 --> 03:29:58,320 in here that I might want to use in the other Python files that I work with. So 2289 03:29:58,320 --> 03:30:03,320 let's say I'm over here at this app dot Python file. And I'm thinking to myself, 2290 03:30:03,320 --> 03:30:08,320 huh, I think I could use one of those functions that was inside of that useful 2291 03:30:08,320 --> 03:30:13,320 tools file. Actually, yeah, I need to simulate rolling a dice in my program. 2292 03:30:13,320 --> 03:30:18,320 Well, instead of having to go over here, copy this function and then paste it 2293 03:30:18,320 --> 03:30:23,320 over here into my program, I can actually just import this useful tools file. And 2294 03:30:23,320 --> 03:30:28,320 I'll be able to import all of these functions and all of these variables 2295 03:30:28,320 --> 03:30:32,320 and attributes. So the way I can do that is just by coming up here. And I'm just 2296 03:30:32,320 --> 03:30:36,320 going to go right at the top of my file. And I'm just going to say import. And 2297 03:30:36,320 --> 03:30:40,320 then I want to type in the name of the file that I want to import. So I'm just 2298 03:30:40,320 --> 03:30:45,320 going to say useful underscore tools. And Python is going to be smart enough to 2299 03:30:45,320 --> 03:30:50,320 know that it should go off and grab all the stuff from this useful tools dot 2300 03:30:50,320 --> 03:30:55,320 pi file. So once I've used that import statement, I can actually use all of 2301 03:30:55,320 --> 03:31:00,320 the functions inside of that useful tools file. So for example, I could simulate 2302 03:31:00,320 --> 03:31:08,320 rolling a dice, I could say useful tools dot. And now I'm actually able to access 2303 03:31:08,320 --> 03:31:12,320 all of the attributes from inside that file. So when I say useful tools dot, 2304 03:31:12,320 --> 03:31:16,320 you'll see down here in my little suggestions menu, it's telling me like 2305 03:31:16,320 --> 03:31:21,320 Beatles feet and miles, get file extension meters and kilometers roll dice. So it's 2306 03:31:21,320 --> 03:31:26,320 giving the access to all of the stuff that was in that file. So over here, I could 2307 03:31:26,320 --> 03:31:31,320 just say like roll dice and we can pass this like a 10 will roll a 10 sided 2308 03:31:31,320 --> 03:31:36,320 dice. And now this should actually be able to run that function. So we'll simulate 2309 03:31:36,320 --> 03:31:42,320 rolling a 10 sided dice. You can see we got four. And this is a really core 2310 03:31:42,320 --> 03:31:49,320 concept in Python, which is importing functionality from external Python files. 2311 03:31:49,320 --> 03:31:53,320 And this is like huge in Python. And this is seriously going to change the way that 2312 03:31:53,320 --> 03:31:59,320 you create your Python files. So you'll notice like I didn't have to copy any 2313 03:31:59,320 --> 03:32:05,320 functions or any variables or anything over into this file. And yet I was able to 2314 03:32:05,320 --> 03:32:10,320 use all the stuff that was inside this useful tools file. So that is huge. And 2315 03:32:10,320 --> 03:32:14,320 honestly, it's going to save you a lot of time because you can write something 2316 03:32:14,320 --> 03:32:18,320 once and then you can import it into your other files. So that's really the 2317 03:32:18,320 --> 03:32:22,320 basics of using modules. I mean, modules are very simple. A module is just any 2318 03:32:22,320 --> 03:32:27,320 external Python file that you want to use some stuff inside of it. I want to 2319 03:32:27,320 --> 03:32:33,320 show you guys a place where you can go to find a huge list of modules. So I'm 2320 03:32:33,320 --> 03:32:38,320 going to go over here to my web browser. And I'm on this website. It's actually 2321 03:32:38,320 --> 03:32:43,320 like the official Python docs. And basically all I did to get here was just type in 2322 03:32:43,320 --> 03:32:47,320 list of Python modules and Google. And depending on the version of Python you're 2323 03:32:47,320 --> 03:32:50,320 using, you're going to want to make sure that you click the right one. I clicked 2324 03:32:50,320 --> 03:32:54,320 on the version 31. And over here on this page, you can see there's just a huge 2325 03:32:54,320 --> 03:32:59,320 list of Python modules. And these are basically modules inside of Python that 2326 03:32:59,320 --> 03:33:04,320 you can just access. So essentially, there's like all of this awesome code 2327 03:33:04,320 --> 03:33:08,320 that's already been written for you. And so there's some sort of functionality 2328 03:33:08,320 --> 03:33:12,320 that you want to have inside of your Python program. Chances are there's a 2329 03:33:12,320 --> 03:33:17,320 module in here that has that functionality. So there's all sorts of stuff you can do. 2330 03:33:17,320 --> 03:33:21,320 I mean, if you just look through this list, like, you'll see a huge collection of 2331 03:33:21,320 --> 03:33:25,320 basically just, you know, a bunch of either Python variables or Python 2332 03:33:25,320 --> 03:33:29,320 functions, just, you know, things that you can use to make your programs better. 2333 03:33:29,320 --> 03:33:34,320 So here's what I would recommend, like, head over to this page and just sort of 2334 03:33:34,320 --> 03:33:37,320 look through a lot of these different modules, you know, see what you can see. 2335 03:33:37,320 --> 03:33:41,320 And honestly, like, if you click on one of them, it'll bring you to a page that 2336 03:33:41,320 --> 03:33:45,320 talks about like how to use it. It'll tell you like how to import it and just, 2337 03:33:45,320 --> 03:33:50,320 you know, some basics about it. And this is sort of like the list that's on the 2338 03:33:50,320 --> 03:33:56,320 official Python docs. But here's the other thing. The Python community is huge. 2339 03:33:56,320 --> 03:34:01,320 There's tons of developers who use Python. And you can actually find a lot of third 2340 03:34:01,320 --> 03:34:05,320 party modules. So if you just go online and, you know, look up, like, Python 2341 03:34:05,320 --> 03:34:10,320 module for doing X or Python module for doing Y, chances are somebody out there 2342 03:34:10,320 --> 03:34:15,320 has already written a Python module to do what you're trying to do. So if you get 2343 03:34:15,320 --> 03:34:18,320 good at using modules and you get good at, you know, looking for them and finding 2344 03:34:18,320 --> 03:34:22,320 them, you can actually save yourself a bunch of time because chances are that 2345 03:34:22,320 --> 03:34:26,320 somebody's already written a module to do, you know, what you're trying to do or 2346 03:34:26,320 --> 03:34:30,320 like part of what you're trying to do. So now that we've taken a look at all these 2347 03:34:30,320 --> 03:34:34,320 different Python modules, I want to just kind of dive in a little bit deeper into 2348 03:34:34,320 --> 03:34:39,320 how we can actually use these things. So you'll notice here, there's a lot of 2349 03:34:39,320 --> 03:34:44,320 files, right? There's a lot of different modules that we apparently have access to 2350 03:34:44,320 --> 03:34:49,320 Python. But the question is, where are all these files stored, right? When I was 2351 03:34:49,320 --> 03:34:54,320 over here in my program, for example, I was using this useful tools.py file. 2352 03:34:54,320 --> 03:34:59,320 Like, I knew where that was. I created that file. I was directly involved in 2353 03:34:59,320 --> 03:35:04,320 making it. And I just imported over here. It was pretty easy, right? But what about 2354 03:35:04,320 --> 03:35:09,320 all of these files, right? What about all these modules over here? Like, where are 2355 03:35:09,320 --> 03:35:13,320 all of these stored? And there's basically two types of modules here. There's built-in 2356 03:35:13,320 --> 03:35:17,320 modules, which means they're just built into the Python language. So we kind of 2357 03:35:17,320 --> 03:35:23,320 just automatically have access to them. And there's external modules. And a lot 2358 03:35:23,320 --> 03:35:28,320 of these external modules are just stored in basically the same folder that we 2359 03:35:28,320 --> 03:35:32,320 installed Python on our computer. So for example, let's look at a couple of these, 2360 03:35:32,320 --> 03:35:40,320 right? We have, like, base 64, BDB, bin ASCII, like, if I come over here and 2361 03:35:40,320 --> 03:35:45,320 you'll see, I'm over here in my little file explorer. I have my Python project, 2362 03:35:45,320 --> 03:35:49,320 which is draft. There's also this other folder over here called external 2363 03:35:49,320 --> 03:35:54,320 libraries. If I was to click down on this and I come down here, you can see it's 2364 03:35:54,320 --> 03:35:59,320 just the version of Python that I'm using. There's a folder here called lib. 2365 03:35:59,320 --> 03:36:03,320 And this is a very important folder. This is basically storing all of those 2366 03:36:03,320 --> 03:36:08,320 external modules. So you can see if I scroll down here, we have all of these 2367 03:36:08,320 --> 03:36:14,320 different, like, modules, right? So here's that base 64. Here's BDB, right? A lot 2368 03:36:14,320 --> 03:36:19,320 of these external modules are stored inside of this lib folder. And like I 2369 03:36:19,320 --> 03:36:23,320 said, there's external modules. There's also a few modules that are just like built 2370 03:36:23,320 --> 03:36:27,320 in modules. They're not going to be stored inside of here. And you can actually 2371 03:36:27,320 --> 03:36:32,320 tell. So for example, if I was to come over here and click on base 64, it tells 2372 03:36:32,320 --> 03:36:38,320 me where the source code is. So the source codes in lib forward slash base 64. 2373 03:36:38,320 --> 03:36:42,320 We were able to see that, right? I was, I saw that inside of my lib folder. 2374 03:36:42,320 --> 03:36:47,320 This bin ASCII, though, for example, if I click on this, you'll notice that this 2375 03:36:47,320 --> 03:36:51,320 isn't giving me a folder because this is basically just like built into Python. 2376 03:36:51,320 --> 03:36:56,320 So we don't have to worry about, you know, locating that file. It's just kind of 2377 03:36:56,320 --> 03:36:59,320 like, it just kind of works. So some of these are external. Some of them are 2378 03:36:59,320 --> 03:37:04,320 built in. And I want to show you guys, in addition to using these modules, 2379 03:37:04,320 --> 03:37:07,320 right? So there's a lot of good stuff here. And honestly, you could spend, you 2380 03:37:07,320 --> 03:37:11,320 know, years and years just learning about all these different modules. But a lot 2381 03:37:11,320 --> 03:37:15,320 of times you're going to want to use modules that other people have written. 2382 03:37:15,320 --> 03:37:21,320 And you know, there's a lot of developers who work on Python and who write different 2383 03:37:21,320 --> 03:37:25,320 modules. So there's a lot of useful modules out there that aren't included in this 2384 03:37:25,320 --> 03:37:30,320 list, right? They're not going to be inside of this lib folder by default. 2385 03:37:30,320 --> 03:37:35,320 And what we can actually do is we can install those external modules, those 2386 03:37:35,320 --> 03:37:40,320 third party modules, that don't just come pre installed with Python. And I'm 2387 03:37:40,320 --> 03:37:43,320 going to show you guys how to do that really quick. So the first thing you need 2388 03:37:43,320 --> 03:37:48,320 to do, obviously, is find a Python module that you want to install that you want 2389 03:37:48,320 --> 03:37:54,320 to download. And I have actually used one of the past Python docs. It's an 2390 03:37:54,320 --> 03:37:58,320 external module that you can use to basically use Python to create Word 2391 03:37:58,320 --> 03:38:02,320 documents, which is pretty cool. So you can like format Word documents inside 2392 03:38:02,320 --> 03:38:06,320 of it. So I'm just going to look that up here in Google. I'm just going to type 2393 03:38:06,320 --> 03:38:11,320 in Python docs. And look, here's the thing, there's tons of these external modules. 2394 03:38:11,320 --> 03:38:16,320 You don't have to use Python docs. I'm just giving you guys an example. But really, 2395 03:38:16,320 --> 03:38:20,320 if you just look up like useful third party Python modules, there's going to be lists 2396 03:38:20,320 --> 03:38:25,320 of these things online. In my case, though, Python docs has a website, and it 2397 03:38:25,320 --> 03:38:29,320 basically just has like some installation instructions. I'm going to come over here. 2398 03:38:29,320 --> 03:38:35,320 And it tells me that I can install Python docs using the command pip install Python 2399 03:38:35,320 --> 03:38:39,320 docs. So this brings us to something I want to talk to you guys about, which is 2400 03:38:39,320 --> 03:38:45,320 pip. And pip is essentially a program. And actually, if you have a newer version 2401 03:38:45,320 --> 03:38:50,320 of Python three, it comes pre installed with Python three. And you can use pip 2402 03:38:50,320 --> 03:38:56,320 to install Python modules. It's referred to as a package manager. And a package 2403 03:38:56,320 --> 03:39:02,320 manager basically just allows you to like install, manage, update, and uninstall 2404 03:39:02,320 --> 03:39:08,320 like different Python modules. So pip is extremely useful. And in order for us to 2405 03:39:08,320 --> 03:39:12,320 install Python docs, we're going to have to use pip. So I'm going to show you guys 2406 03:39:12,320 --> 03:39:17,320 how we can do that. What I want to do is open up the command prompt or the 2407 03:39:17,320 --> 03:39:21,320 terminal on my computer. If you're on a Mac, you want to open up your terminal. 2408 03:39:21,320 --> 03:39:24,320 If you're on a Windows computer, you want to open up your command prompts. I'm 2409 03:39:24,320 --> 03:39:28,320 using a Windows right now. So I'm just going to come down to the search bar and 2410 03:39:28,320 --> 03:39:32,320 type in CMD. And this command prompt should come up. So I'm going to click this 2411 03:39:32,320 --> 03:39:36,320 inside of the command prompt, we can actually use pip. The first thing we want to do 2412 03:39:36,320 --> 03:39:40,320 is just check to make sure that pip's installed. And like I said, if you have a 2413 03:39:40,320 --> 03:39:45,320 version of Python three, pip should come just pre installed. And it should just work 2414 03:39:45,320 --> 03:39:49,320 in here. If you don't have a new version of Python three, though, you might have to 2415 03:39:49,320 --> 03:39:54,320 install pip separately. And there's tons of instructions online on how to install 2416 03:39:54,320 --> 03:39:57,320 pip. So I'm just going to check to make sure that I have it, I'm going to type in 2417 03:39:57,320 --> 03:40:02,320 pip hyphen hyphen version. And this should spit out the version of pip that I 2418 03:40:02,320 --> 03:40:07,320 currently have. So as long as we have pip, then we're ready to install an external 2419 03:40:07,320 --> 03:40:13,320 or third party Python module. All I have to do is just say pick install. And now I 2420 03:40:13,320 --> 03:40:17,320 just want to type in the name of the Python module. So in the case of Python 2421 03:40:17,320 --> 03:40:23,320 docs, it was just Python docs like that. Now again, you don't have to install Python 2422 03:40:23,320 --> 03:40:27,320 docs. I'm just doing this for this tutorial. Just kind of show you guys how this is 2423 03:40:27,320 --> 03:40:32,320 going to work. But you know, what you want to do is go online and look up some third 2424 03:40:32,320 --> 03:40:37,320 party external Python modules. And generally, like I'd say 90 to 95% of the 2425 03:40:37,320 --> 03:40:41,320 time, you're going to be able to just install them using pip. In the off case 2426 03:40:41,320 --> 03:40:45,320 that you can't install them using pip, chances are there'll be like some detailed 2427 03:40:45,320 --> 03:40:49,320 installation instructions. But I would say for the most part, any like legitimate 2428 03:40:49,320 --> 03:40:54,320 Python module is going to be able to install using pip. So over here, I'm just 2429 03:40:54,320 --> 03:41:00,320 going to say pip install and then the module name. So Python hyphen docs. And 2430 03:41:00,320 --> 03:41:04,320 when I click enter, this is going to go off and install Python docs for us. So I'm 2431 03:41:04,320 --> 03:41:08,320 just going to enter. And it's going to go off and install everything we need for 2432 03:41:08,320 --> 03:41:16,320 Python docs. So you can see we successfully installed Python docs 0.8.6. So I'm going 2433 03:41:16,320 --> 03:41:21,320 to show you guys where exactly this got placed. So normally, when we install a 2434 03:41:21,320 --> 03:41:26,320 external third party module, it's going to get put inside of this lib folder. But 2435 03:41:26,320 --> 03:41:31,320 it's going to get put inside of a special folder in here called site packages. So 2436 03:41:31,320 --> 03:41:36,320 site packages is a special folder. And if I open this folder, you'll see now we have 2437 03:41:36,320 --> 03:41:44,320 this docs folder and we also have this Python docs 0.8.6 pi 3.6.ag info folder. 2438 03:41:44,320 --> 03:41:50,320 So these two folders are basically included in that module installation. So you can 2439 03:41:50,320 --> 03:41:55,320 see this docs folder has a bunch of different Python files and has a bunch of 2440 03:41:55,320 --> 03:41:59,320 stuff in here that we can use. So if I wanted to use this inside of one of my 2441 03:41:59,320 --> 03:42:03,320 programs, I'm just going to refer to the name of the module. So in our case, 2442 03:42:03,320 --> 03:42:08,320 it's just going to be docs. So I used Python docs in order to install it. But we're 2443 03:42:08,320 --> 03:42:13,320 going to use docs if we want to import it. So I could come up here and I could say 2444 03:42:13,320 --> 03:42:19,320 import DOCX. And now I can actually use it. So I can just say like docs, 2445 03:42:19,320 --> 03:42:22,320 doc, whatever. And you can see there's a bunch of different stuff down here. There's 2446 03:42:22,320 --> 03:42:26,320 like a document document part image part. There's a bunch of stuff that we can use 2447 03:42:26,320 --> 03:42:30,320 with this. And obviously, depending on the Python module you install, there's going 2448 03:42:30,320 --> 03:42:34,320 to be different instructions. But you can see it got stored down here in this 2449 03:42:34,320 --> 03:42:39,320 site packages folder. If I wanted to remove this, I could use PIP to do it. So I could 2450 03:42:39,320 --> 03:42:48,320 just say PIP uninstall. And we could just again say the module name. So Python docs. 2451 03:42:48,320 --> 03:42:54,320 And PIP will now uninstall this on our computer. So how is to go back over to 2452 03:42:54,320 --> 03:43:00,320 this folder? You'll see that those two, that docs folder and then that other 2453 03:43:00,320 --> 03:43:05,320 folder disappeared. So they're no longer here. And I'm actually not going to be able 2454 03:43:05,320 --> 03:43:10,320 to use this anymore. So that's sort of the ins and outs of using modules. Now again, 2455 03:43:10,320 --> 03:43:15,320 there's tons of these modules and I can make dozens and dozens of Python courses 2456 03:43:15,320 --> 03:43:20,320 covering each one of these modules. The built-in modules, the modules that are 2457 03:43:20,320 --> 03:43:25,320 included by default and external modules, there's tons of these things out there. 2458 03:43:25,320 --> 03:43:29,320 And really, as a Python programmer now, what you can do is you can go out and play 2459 03:43:29,320 --> 03:43:33,320 around with these different modules. I showed you the ins and outs of installing 2460 03:43:33,320 --> 03:43:37,320 them and you can use PIP to install all these different modules. And you can make 2461 03:43:37,320 --> 03:43:41,320 sure that you have them by checking the site packages folder or the libs folder. 2462 03:43:41,320 --> 03:43:46,320 But really now it's on you to just kind of go out and use these modules and don't 2463 03:43:46,320 --> 03:43:51,320 shy away from this because modules are a huge part of Python and you're definitely 2464 03:43:51,320 --> 03:43:56,320 going to want to include them in your Python stack. 2465 03:43:58,320 --> 03:44:03,320 In this tutorial, I want to talk to you guys about classes and objects in Python. 2466 03:44:03,320 --> 03:44:09,320 Now classes and objects are extremely useful in Python programming and they can 2467 03:44:09,320 --> 03:44:13,320 help you to make your programs more organized and more powerful. 2468 03:44:13,320 --> 03:44:17,320 So when we're in Python, we're dealing with all types of data, right? 2469 03:44:17,320 --> 03:44:21,320 And a lot of times when we're writing programs, we're going to have to work with 2470 03:44:21,320 --> 03:44:25,320 different types of data. And there's essentially like a few basic types of data 2471 03:44:25,320 --> 03:44:30,320 we can do with usually things like strings, so like plain text, numbers, and 2472 03:44:30,320 --> 03:44:34,320 Boolean values. Those three are kind of like the main types of data that you're 2473 03:44:34,320 --> 03:44:38,320 going to be working with in Python. And we have all sorts of structures we can 2474 03:44:38,320 --> 03:44:42,320 use to store that data, you know, things like lists or dictionaries. 2475 03:44:42,320 --> 03:44:47,320 But here's the problem is that not all information, not all data and not all 2476 03:44:47,320 --> 03:44:53,320 things can be represented using strings, numbers, or Booleans, right? 2477 03:44:53,320 --> 03:44:57,320 There's a lot of things in the real world that we can't represent in something like 2478 03:44:57,320 --> 03:45:01,320 a string or just a number, right? In other words, something like think of like 2479 03:45:01,320 --> 03:45:05,320 something like a phone or a computer or a person, right? You can't really 2480 03:45:05,320 --> 03:45:10,320 represent those things in like a string or a number. You know, in other words, 2481 03:45:10,320 --> 03:45:15,320 like the data types that we have available to us in Python can't cover that. 2482 03:45:15,320 --> 03:45:20,320 And so what we can do with classes and objects is we can essentially create our 2483 03:45:20,320 --> 03:45:25,320 own data types. So I can create my own data type for anything I want in Python. 2484 03:45:25,320 --> 03:45:29,320 So I could create like a phone data type and it could represent a phone. 2485 03:45:29,320 --> 03:45:33,320 So I could store all the information I would ever want to know about my phone 2486 03:45:33,320 --> 03:45:38,320 inside of that data type. And in Python, we could create a class for it. 2487 03:45:38,320 --> 03:45:42,320 And essentially what a class is is it's just saying, hey, here's another data type 2488 03:45:42,320 --> 03:45:47,320 that we want to use in Python. So with a class, you can essentially define your 2489 03:45:47,320 --> 03:45:52,320 own data type. And it's super awesome. And classes are extremely useful. 2490 03:45:52,320 --> 03:45:56,320 Classes are used in almost every single major programming language out there. 2491 03:45:56,320 --> 03:46:00,320 So in this tutorial, I just want to give you guys a basic introduction of classes 2492 03:46:00,320 --> 03:46:05,320 and using them inside of Python. So let's say that I'm writing a program 2493 03:46:05,320 --> 03:46:09,320 and I want to represent a student inside of this program. 2494 03:46:09,320 --> 03:46:13,320 So maybe I'm writing a program for like a college or a university. 2495 03:46:13,320 --> 03:46:17,320 Let's say that in this program, I want to model a student. 2496 03:46:17,320 --> 03:46:21,320 Like I want to model a real world object and I want it to be a student. 2497 03:46:21,320 --> 03:46:25,320 Right, we don't have a student data type and I can't really represent a student 2498 03:46:25,320 --> 03:46:28,320 in just a single string or a number. 2499 03:46:28,320 --> 03:46:33,320 So what I can actually do is I can create a class for a student. 2500 03:46:33,320 --> 03:46:36,320 And I'm basically creating like a student data type. 2501 03:46:36,320 --> 03:46:40,320 So I'm going to show you guys how we can do that and create our student class. 2502 03:46:40,320 --> 03:46:43,320 So over here, I'm just going to make a new file. 2503 03:46:43,320 --> 03:46:48,320 We'll just make it a new Python file. So I'm just going to call it student.py. 2504 03:46:48,320 --> 03:46:54,320 And inside of this student.py file, I want to create a student class. 2505 03:46:54,320 --> 03:46:58,320 So the way that I can do that is just by typing class. 2506 03:46:58,320 --> 03:47:02,320 And now I want to type the name of the class that I want to create. 2507 03:47:02,320 --> 03:47:04,320 So in our case, we're going to create a class called student. 2508 03:47:04,320 --> 03:47:08,320 So I'm just going to say student and now we're going to make a colon. 2509 03:47:08,320 --> 03:47:13,320 Now everything that goes inside of here is going to be inside of our student class. 2510 03:47:13,320 --> 03:47:19,320 So everything that's indented like this will be considered a part of our student class. 2511 03:47:19,320 --> 03:47:23,320 And basically what we can do inside of this student class is we can define a bunch of attributes 2512 03:47:23,320 --> 03:47:25,320 about a student. 2513 03:47:25,320 --> 03:47:28,320 So essentially what I'm doing is I'm like modeling a student. 2514 03:47:28,320 --> 03:47:33,320 I'm creating our student data type. And I can use things like strings, 2515 03:47:33,320 --> 03:47:40,320 integers, and booleans in order to map out what a student should be and what a student should have. 2516 03:47:40,320 --> 03:47:43,320 So I want to create something called an initialize function. 2517 03:47:43,320 --> 03:47:47,320 And the way I do that is just by typing out def. 2518 03:47:47,320 --> 03:47:50,320 And I'm just going to type two underscores. 2519 03:47:50,320 --> 03:47:55,320 And then the word I N I T and then two more underscores. 2520 03:47:55,320 --> 03:47:58,320 And you also want to type out open and close parentheses. 2521 03:47:58,320 --> 03:48:02,320 And inside of those parentheses, we want to type out self. 2522 03:48:02,320 --> 03:48:04,320 And then we want to colon after that. 2523 03:48:04,320 --> 03:48:11,320 What I can do inside of this initialize function is I can basically map out what attributes a student should have. 2524 03:48:11,320 --> 03:48:17,320 So we can essentially define like, hey, here's the student data type in Python. 2525 03:48:17,320 --> 03:48:21,320 So what I want to do is add in certain attributes after this self. 2526 03:48:21,320 --> 03:48:23,320 So I'm just going to say self a comma. 2527 03:48:23,320 --> 03:48:30,320 And now we can start thinking about, you know, what values will represent a student inside of our Python program. 2528 03:48:30,320 --> 03:48:34,320 So if I'm thinking like, I'm thinking something like name, right? 2529 03:48:34,320 --> 03:48:37,320 So every student in our program should have a name. 2530 03:48:37,320 --> 03:48:41,320 They also might want to have a major because they're in college. 2531 03:48:41,320 --> 03:48:48,320 They probably also will have a GPA. So they're a grade point average, like how they're doing in school. 2532 03:48:48,320 --> 03:48:50,320 And let's define one more thing. 2533 03:48:50,320 --> 03:48:53,320 Let me think. Why don't we make a Boolean? 2534 03:48:53,320 --> 03:48:57,320 And it's going to be called is on probation. 2535 03:48:57,320 --> 03:49:02,320 And this will basically tell whether or not the student is on probation. 2536 03:49:02,320 --> 03:49:10,320 So essentially what I'm doing inside of this initialize method inside of this init function is I'm defining what 2537 03:49:10,320 --> 03:49:18,320 a student is in our program. And so in this program, a student has a name, it has a major, it has a GPA, 2538 03:49:18,320 --> 03:49:21,320 and it has a value that determines whether or not it's on probation. 2539 03:49:21,320 --> 03:49:24,320 This right here is the student data type. 2540 03:49:24,320 --> 03:49:31,320 So if I'm representing a student inside of my program now, it has all of these attributes associated to it. 2541 03:49:31,320 --> 03:49:33,320 And that's basically what I'm doing up here. 2542 03:49:33,320 --> 03:49:42,320 Now inside of this initialize function, we actually have to do something. And basically what I want to do is assign some values. 2543 03:49:42,320 --> 03:49:47,320 So I'm going to be writing out some stuff and this might not make total sense right now. 2544 03:49:47,320 --> 03:49:52,320 But this is going to make sense in a second after we create our first student object. 2545 03:49:52,320 --> 03:49:57,320 So just stick with me right now and just basically know that we have to do what I'm doing over here. 2546 03:49:57,320 --> 03:50:05,320 So I'm just going to say self dot name is equal to name. And then I'm going to say self dot major is equal to major. 2547 03:50:05,320 --> 03:50:16,320 Self dot GPA is equal to GPA. And I'm going to say self dot is on probation is equal to is on probation. 2548 03:50:16,320 --> 03:50:23,320 So again, this might not make a whole lot of sense right now, but in a little bit, this is going to make total sense. 2549 03:50:23,320 --> 03:50:30,320 So now that I have this student class defined, I can actually use this class inside of my other file. 2550 03:50:30,320 --> 03:50:36,320 So I'm going to come over here into this app dot Python file. And this is just my main file. 2551 03:50:36,320 --> 03:50:43,320 So I actually want to create a student right so in that student class, we defined the student data type. 2552 03:50:43,320 --> 03:50:49,320 And we basically said like hey, a student has a name, a major GPA and it says whether or not they're on probation. 2553 03:50:49,320 --> 03:50:55,320 That's like the template for what a student is. But we can actually create a student. 2554 03:50:55,320 --> 03:51:01,320 So we can create an actual student and give it some information. And that's called an object. 2555 03:51:01,320 --> 03:51:07,320 So this student class over here is basically defining what a student is. 2556 03:51:07,320 --> 03:51:12,320 So a class is just like an overview of what the student data type is. 2557 03:51:12,320 --> 03:51:19,320 An object is an actual student. So it's an actual student with a name, a major and a GPA. 2558 03:51:19,320 --> 03:51:25,320 It's not just this template anymore. It's actually like a student that we're representing inside of our program. 2559 03:51:25,320 --> 03:51:32,320 So in order to use that student class and create a student object, I actually need to import that. 2560 03:51:32,320 --> 03:51:40,320 So all I have to do over here is just say from student, and this is referring to this student file. 2561 03:51:40,320 --> 03:51:44,320 I can say import student. 2562 03:51:44,320 --> 03:51:51,320 And basically what this is saying is from the student file, I want to import the student class. 2563 03:51:51,320 --> 03:51:54,320 So even though these are both student, they're referring to different things. 2564 03:51:54,320 --> 03:51:59,320 So this is referring to the file and this is referring to the actual student class. 2565 03:51:59,320 --> 03:52:06,320 So now that we did this, we can create a student. So you can create an object of a class a lot like you would a normal variable. 2566 03:52:06,320 --> 03:52:10,320 So I can just give it a name. I'm going to call this student one. 2567 03:52:10,320 --> 03:52:14,320 And I'm just going to set it equal to student. 2568 03:52:14,320 --> 03:52:16,320 And I'm going to make an open and close parentheses. 2569 03:52:16,320 --> 03:52:27,320 And now inside of this parentheses, I want to give this student a name, a major, a GPA, and an is on probation value. 2570 03:52:27,320 --> 03:52:32,320 So I'm going to say, we'll just create a fake student. So I'll just say his name's Jim. 2571 03:52:32,320 --> 03:52:36,320 And he's studying business. 2572 03:52:36,320 --> 03:52:41,320 And we want to give him a GPA. So maybe he has like a 3.1. 2573 03:52:41,320 --> 03:52:46,320 And finally, we want to say whether or not this student is on probation. So why don't we say false? 2574 03:52:46,320 --> 03:52:51,320 So basically what I'm doing is I'm saying that I want to create a student. 2575 03:52:51,320 --> 03:52:58,320 So I want to actually create a student object. And remember, an object is just an instance of a class. 2576 03:52:58,320 --> 03:53:02,320 So the class is like an overall template. It defines what a student is. 2577 03:53:02,320 --> 03:53:06,320 But an object is an actual student with actual information. 2578 03:53:06,320 --> 03:53:11,320 So we could call student one now. This is a student object. 2579 03:53:11,320 --> 03:53:16,320 So I just want to show you guys what this student object actually is and what we can do with it. 2580 03:53:16,320 --> 03:53:20,320 And then I'm going to talk some more about that in it function from before. 2581 03:53:20,320 --> 03:53:23,320 So over here, I'm just going to make a print statement. 2582 03:53:23,320 --> 03:53:27,320 And inside of here, I just want to print out student. 2583 03:53:27,320 --> 03:53:35,320 And what's cool about this student one object is I can actually access each of the attributes from inside of this object. 2584 03:53:35,320 --> 03:53:39,320 So if I wanted to get the name of the student, I could say student one dot name. 2585 03:53:39,320 --> 03:53:43,320 And now this is actually going to print out the student's name. 2586 03:53:43,320 --> 03:53:47,320 So over here, you'll see it prints out Jim. 2587 03:53:47,320 --> 03:53:54,320 If I wanted, I could print out the student's GPA. And it's going to print out the student's GPA 3.1. 2588 03:53:54,320 --> 03:54:01,320 So now that I created this student object, I can actually access the information about the student. 2589 03:54:01,320 --> 03:54:04,320 So essentially, I've just created a student data type. 2590 03:54:04,320 --> 03:54:11,320 So I could create as many of these students as I wanted. If I wanted, I could create another student we could call it student two. 2591 03:54:11,320 --> 03:54:15,320 And essentially, we do the same thing. We just give it different information. 2592 03:54:15,320 --> 03:54:26,320 So we could say like Pam and her major is art. And she has like a 2.5. And let's say that she is on probation. 2593 03:54:26,320 --> 03:54:32,320 So now I have another student, student two. So if I wanted, I could access information about that student. 2594 03:54:32,320 --> 03:54:38,320 We could say like student two dot GPA. And now this is giving me the GPA of student two. 2595 03:54:38,320 --> 03:54:44,320 So basically, what I did was I created a student data type and I created student objects. 2596 03:54:44,320 --> 03:54:48,320 And now I'm able to represent a student inside of my program. 2597 03:54:48,320 --> 03:54:53,320 So let's talk real quick about all this stuff over here because I didn't really explain it. 2598 03:54:53,320 --> 03:55:01,320 Basically, what's happening is when I come over here and I say student and I'm passing in all of these different values, 2599 03:55:01,320 --> 03:55:05,320 those are getting passed into this init function. 2600 03:55:05,320 --> 03:55:09,320 Remember, I passed in a name, I passed in a major, I passed in a GPA. 2601 03:55:09,320 --> 03:55:14,320 When we create that student, we're actually calling this function. 2602 03:55:14,320 --> 03:55:21,320 And when I pass in the name, the major and the GPA, those values are actually getting stored over here in this name, 2603 03:55:21,320 --> 03:55:29,320 this major, this GPA, et cetera. So I'm giving this student object all of that information. 2604 03:55:29,320 --> 03:55:35,320 And down here, what I'm doing is I'm saying self dot name is equal to name. 2605 03:55:35,320 --> 03:55:42,320 And basically what this means is the actual object's name is going to be equal to the name that they passed in. 2606 03:55:42,320 --> 03:55:46,320 So self dot name is an attribute of student. 2607 03:55:46,320 --> 03:55:51,320 So the student is storing a name, the student is storing a major, the student is storing a GPA. 2608 03:55:51,320 --> 03:55:56,320 But that's different from this name, this major, and this GPA. 2609 03:55:56,320 --> 03:56:01,320 Remember, these are all just values that I passed in, they're just parameters. 2610 03:56:01,320 --> 03:56:08,320 And so I need to take the values that I passed in and I need to assign them to the actual attributes of the object. 2611 03:56:08,320 --> 03:56:14,320 So I need to say, okay, the name of the student is going to be equal to the name that we passed in. 2612 03:56:14,320 --> 03:56:21,320 One more time, the name of the student object is going to be equal to the name that we passed in. 2613 03:56:21,320 --> 03:56:26,320 The name of the student's major is going to be equal to the major that we passed in. 2614 03:56:26,320 --> 03:56:31,320 The student's GPA is going to be equal to the GPA that we passed in. 2615 03:56:31,320 --> 03:56:37,320 That's basically what's happening here. So when I say self, it's referring to the actual object. 2616 03:56:37,320 --> 03:56:43,320 So over here, when I'm creating this student object, I'm giving it all of this information. 2617 03:56:43,320 --> 03:56:48,320 It's taking that information and storing it as attributes for the object. 2618 03:56:48,320 --> 03:56:52,320 So hopefully that makes sense. I think that's about as clear as I can make it. 2619 03:56:52,320 --> 03:56:56,320 But the point is that now we have a student data type. 2620 03:56:56,320 --> 03:57:01,320 So now I can represent a student inside of my Python program. 2621 03:57:01,320 --> 03:57:06,320 And that's like super powerful. And what's cool about classes and objects is you can do this with anything. 2622 03:57:06,320 --> 03:57:12,320 So in this example, we created a student class and then we created student objects off of that. 2623 03:57:12,320 --> 03:57:16,320 But I can model any real world entity into this program. 2624 03:57:16,320 --> 03:57:23,320 I can model something like a phone or I can model like a water bottle or I can model, you know, a keyboard, right? 2625 03:57:23,320 --> 03:57:27,320 I can model anything that I wanted inside of my program. 2626 03:57:27,320 --> 03:57:29,320 I could just give it certain attributes, right? 2627 03:57:29,320 --> 03:57:36,320 And so that's what's cool about classes is we can model real world objects and we can create our own data types. 2628 03:57:40,320 --> 03:57:45,320 In this tutorial, I'm going to show you guys how to build a multiple choice quiz in Python. 2629 03:57:45,320 --> 03:57:50,320 So basically, we're going to set up a little multiple choice quiz and we'll have the user take the quiz. 2630 03:57:50,320 --> 03:57:55,320 And as they take the quiz, we'll keep track of their score and then at the end we'll tell them how they did. 2631 03:57:55,320 --> 03:57:59,320 So this is going to be a pretty cool video. I'm excited to show you guys how to do this. 2632 03:57:59,320 --> 03:58:05,320 And we'll get to use things like classes and if statements and loops in order to build this program. 2633 03:58:05,320 --> 03:58:08,320 So we're going to use a lot of cool stuff in Python. 2634 03:58:08,320 --> 03:58:15,320 The first thing I want to show you guys, I actually did this before I started the video. It's this little array. It's called question prompts. 2635 03:58:15,320 --> 03:58:20,320 And I basically just wrote out all the questions that are going to be or I shouldn't say the questions. 2636 03:58:20,320 --> 03:58:25,320 I wrote out all the question prompts that are going to be inside of my multiple choice quiz. 2637 03:58:25,320 --> 03:58:32,320 So the first one just says what color are apples and the answers are red, purple or orange. 2638 03:58:32,320 --> 03:58:38,320 The next one is what color bananas the options are teal, magenta and yellow. 2639 03:58:38,320 --> 03:58:43,320 And the third question is what color are strawberries and the options are yellow, red and blue. 2640 03:58:43,320 --> 03:58:46,320 So this is obviously a very, very easy multiple choice quiz. 2641 03:58:46,320 --> 03:58:51,320 But I just kind of wrote out the questions beforehand so I didn't have to spend time doing that. 2642 03:58:51,320 --> 03:58:56,320 So let's talk about how we can build a multiple choice test. 2643 03:58:56,320 --> 03:59:00,320 Well, the first thing I want to think about is how can I represent the questions in the test. 2644 03:59:00,320 --> 03:59:07,320 Over here, we have the question prompts, but there's actually two parts to a question when you think about it. 2645 03:59:07,320 --> 03:59:11,320 There's the prompts. In other words, the actual question itself. 2646 03:59:11,320 --> 03:59:17,320 And then there's the answer to the question and both of those attributes need to be kept track of. 2647 03:59:17,320 --> 03:59:21,320 I need to keep track of what we want to ask and I need to keep track of what the answer is. 2648 03:59:21,320 --> 03:59:25,320 So what I'm actually going to do is create a question class. 2649 03:59:25,320 --> 03:59:30,320 So I'm going to create a little data type for questions and inside of that question class, 2650 03:59:30,320 --> 03:59:36,320 we'll be able to store the question prompts and we'll also be able to store the questions answer. 2651 03:59:36,320 --> 03:59:45,320 So I'm just going to come over here to my folder and I'm going to make a new Python file and we're just going to call this question.py. 2652 03:59:45,320 --> 03:59:51,320 So we'll call it question.py and inside of this question.py file, I'm going to make a class. 2653 03:59:51,320 --> 03:59:56,320 So I'm just going to say class question and I'm going to put a colon here. 2654 03:59:56,320 --> 04:00:01,320 So inside of this question class, we want to define it in initialize function. 2655 04:00:01,320 --> 04:00:11,320 So I'm going to say def two underscores in it, two underscores, and then we're going to type out an open and close parentheses self and then a colon. 2656 04:00:11,320 --> 04:00:19,320 So after this self, we want to define the different attributes that will describe or that will be included in a question. 2657 04:00:19,320 --> 04:00:24,320 So we're going to include a prompt and we're going to include an answer. 2658 04:00:24,320 --> 04:00:28,320 So every question will have a question prompt and a question answer. 2659 04:00:28,320 --> 04:00:34,320 Now down here, I want to take those values and assign them to the actual class object. 2660 04:00:34,320 --> 04:00:43,320 So I'm going to say self dot prompt is equal to prompt and I'm going to say self dot answer is equal to answer. 2661 04:00:43,320 --> 04:00:50,320 Now we have a question class set up and we can store all the information we need about a question. 2662 04:00:50,320 --> 04:00:54,320 So what I want to do now is go back over to my app dot Python file. 2663 04:00:54,320 --> 04:00:57,320 And we're actually going to create an array of questions. 2664 04:00:57,320 --> 04:01:02,320 So I have over here these three prompts and I'm actually just going to create another array. 2665 04:01:02,320 --> 04:01:12,320 So we'll just call it questions and I'm just going to set it equal to to open and close square brackets and we're going to start creating question objects. 2666 04:01:12,320 --> 04:01:27,320 Let's say the first element in this questions array will be a question and we want to pass question prompts zero and the answer to the first question was a red green. 2667 04:01:27,320 --> 04:01:28,320 That's the color of Apple. 2668 04:01:28,320 --> 04:01:36,320 So I'm going to pass that first question and I'm also going to pass the answer, which is a and you'll see down here, I'm getting an error. 2669 04:01:36,320 --> 04:01:45,320 So saying unresolved reference question. So actually what we need to do before we can create these questions is we need to import this question class. 2670 04:01:45,320 --> 04:01:52,320 So I'm going to come up in here, come up here and say from question import question. 2671 04:01:52,320 --> 04:01:56,320 So now down here in this questions array, we're not getting that error anymore. 2672 04:01:56,320 --> 04:02:02,320 So we created one question object and I'm just going to create a couple more for these other ones. 2673 04:02:02,320 --> 04:02:10,320 So I'm just going to copy this one and we will paste this down here. 2674 04:02:10,320 --> 04:02:21,320 Alright, so now we're creating two other questions and these ones are going to be one and two and the answer to the second question is C and the answer to the third question. 2675 04:02:21,320 --> 04:02:25,320 I think it's B. Let me look. 2676 04:02:25,320 --> 04:02:35,320 Yeah, so it's C and B. Alright, so essentially what we're doing is we're creating three questions. Each one is getting a different question prompt and they're each getting different answers. 2677 04:02:35,320 --> 04:02:39,320 So now we have an array of questions that we want to ask on our test. 2678 04:02:39,320 --> 04:02:44,320 So the next step is to actually write a function that will run the test, right? 2679 04:02:44,320 --> 04:02:49,320 It has to ask the user the questions and it has to check to see if they got the answer right. 2680 04:02:49,320 --> 04:02:57,320 Down here, lower in the file, let's create a function. So I'm just going to say death and we'll call this run test. 2681 04:02:57,320 --> 04:03:02,320 And inside of this parentheses, we're going to take one parameter into this function. 2682 04:03:02,320 --> 04:03:07,320 We're going to take a list of questions. So I'm just going to say questions. 2683 04:03:07,320 --> 04:03:12,320 So this is basically going to be a list of question objects that we want to ask the user. 2684 04:03:12,320 --> 04:03:16,320 So once I'm in here, all I want to do is loop through all the questions. 2685 04:03:16,320 --> 04:03:20,320 So I want to loop through each question. I want to ask it to the user. 2686 04:03:20,320 --> 04:03:24,320 I want to get the user's answer and I want to check to see if it's right. 2687 04:03:24,320 --> 04:03:28,320 And we need to be able to keep track of how the user does through the test. 2688 04:03:28,320 --> 04:03:33,320 So I want to create a variable called score and I'm just going to set it equal to zero. 2689 04:03:33,320 --> 04:03:38,320 So every time the user answers a question right, we'll increment this score variable. 2690 04:03:38,320 --> 04:03:42,320 Alright, so what I want to do is, like I said, loop through all the questions in that questions array. 2691 04:03:42,320 --> 04:03:49,320 So I'm going to create a for loop. I'm going to say for question in questions. 2692 04:03:49,320 --> 04:03:55,320 So in other words, for each question object inside of this questions array, I want to do something. 2693 04:03:55,320 --> 04:04:00,320 And basically, the first thing I want to do is ask the user the question. 2694 04:04:00,320 --> 04:04:03,320 And I want to store their response inside of a variable. 2695 04:04:03,320 --> 04:04:07,320 So I'm going to say, I'm going to make a variable down here called answer. 2696 04:04:07,320 --> 04:04:12,320 And this is going to represent the user's answer to the question. 2697 04:04:12,320 --> 04:04:17,320 And I'm going to set it equal to input and I need to give this a prompt. 2698 04:04:17,320 --> 04:04:20,320 So I'm actually going to give this the prompt for the question. 2699 04:04:20,320 --> 04:04:24,320 So I can just say question dot prompt. 2700 04:04:24,320 --> 04:04:29,320 So now I have the answer that the user entered in, which is stored inside this variable. 2701 04:04:29,320 --> 04:04:32,320 So we want to check to see if they got the question right. 2702 04:04:32,320 --> 04:04:34,320 And I can do that using an if statement. 2703 04:04:34,320 --> 04:04:42,320 Let's say if answer, and this refers to the student's answer, is equal to question dot answer. 2704 04:04:42,320 --> 04:04:49,320 So I'm checking to see if the answer that the student gave is equal to the answer of the current question that we're asking. 2705 04:04:49,320 --> 04:04:53,320 And if this is true, then I just want to increment score. 2706 04:04:53,320 --> 04:04:56,320 So I'm going to say score plus equals one. 2707 04:04:56,320 --> 04:04:59,320 And this basically just means we're adding one to the score. 2708 04:04:59,320 --> 04:05:04,320 So the last thing I want to do inside of this function is just print out the result. 2709 04:05:04,320 --> 04:05:06,320 So I want to print out how the user did. 2710 04:05:06,320 --> 04:05:10,320 So down here, I'm just going to say print and I'm basically just going to print out like, 2711 04:05:10,320 --> 04:05:14,320 Hey, you got like two out of three questions or you got five out of six questions, right? 2712 04:05:14,320 --> 04:05:16,320 Or you got zero out of 10 questions, right? 2713 04:05:16,320 --> 04:05:18,320 We'll just print out how they did. 2714 04:05:18,320 --> 04:05:20,320 So I'll just say you got. 2715 04:05:20,320 --> 04:05:24,320 And now I want to print out how many questions they got right. 2716 04:05:24,320 --> 04:05:26,320 So this is just going to be the score. 2717 04:05:26,320 --> 04:05:33,320 And in order to print a number alongside of a string like this, I'm going to have to say STR and then type in the number. 2718 04:05:33,320 --> 04:05:35,320 So we'll say STR score. 2719 04:05:35,320 --> 04:05:39,320 So we'll basically say you got however many they got right out of. 2720 04:05:39,320 --> 04:05:41,320 So we'll use this little forward slash. 2721 04:05:41,320 --> 04:05:45,320 And now we'll just say how many questions there were. 2722 04:05:45,320 --> 04:05:49,320 So we can say, and again, we're going to have to convert this to a string because it's going to be a number. 2723 04:05:49,320 --> 04:05:56,320 So we can say, string, and then we want to figure out how many questions were in that questions array. So I can say LEN. 2724 04:05:56,320 --> 04:05:58,320 And inside of here, we can just say questions. 2725 04:05:58,320 --> 04:06:02,320 So this is kind of like this long statement just to print out how many questions there were. 2726 04:06:02,320 --> 04:06:05,320 So then over here, I'll just say, correct. 2727 04:06:05,320 --> 04:06:06,320 All right. 2728 04:06:06,320 --> 04:06:08,320 So now we have this print statement. 2729 04:06:08,320 --> 04:06:12,320 And it's just going to be printing out how many questions they got right. 2730 04:06:12,320 --> 04:06:14,320 So let's see if this works. 2731 04:06:14,320 --> 04:06:19,320 The last thing I need to do is just call this run test function. 2732 04:06:19,320 --> 04:06:24,320 So I'll say run test, and I'm just going to pass it that questions array that we created before. 2733 04:06:24,320 --> 04:06:29,320 So we created this questions array with all of these question objects inside of it. 2734 04:06:29,320 --> 04:06:33,320 I'm basically just going to pass that into this run test function. 2735 04:06:33,320 --> 04:06:36,320 So now let's run our program and see how we did. 2736 04:06:36,320 --> 04:06:38,320 Hopefully we don't get any errors. 2737 04:06:38,320 --> 04:06:42,320 There's a lot of code there, so it's possible that we might. 2738 04:06:42,320 --> 04:06:44,320 Okay, so it says what color are apples? 2739 04:06:44,320 --> 04:06:45,320 Huh, this is a tough one. 2740 04:06:45,320 --> 04:06:48,320 All right, let's put A. So we'll get the first question right. 2741 04:06:48,320 --> 04:06:49,320 What color are bananas? 2742 04:06:49,320 --> 04:06:51,320 Let's get the second question wrong. 2743 04:06:51,320 --> 04:06:54,320 So I'm going to say B, bananas are magenta. 2744 04:06:54,320 --> 04:06:56,320 And we'll get the third question right. 2745 04:06:56,320 --> 04:06:58,320 So it says what color are strawberries? 2746 04:06:58,320 --> 04:07:01,320 I'll just put B because that's the right answer. 2747 04:07:01,320 --> 04:07:04,320 So now you'll see that our program prints out. 2748 04:07:04,320 --> 04:07:06,320 You got two out of three correct. 2749 04:07:06,320 --> 04:07:10,320 So not only did it ask us all the questions and get our input for each question, 2750 04:07:10,320 --> 04:07:14,320 but it also kept track of our score and it printed it out. 2751 04:07:14,320 --> 04:07:20,320 So why don't we run through the test one more time and we'll just like get too wrong this time. 2752 04:07:20,320 --> 04:07:21,320 So we'll get this one wrong. 2753 04:07:21,320 --> 04:07:23,320 I'll get this one wrong and I'll get this one right. 2754 04:07:23,320 --> 04:07:26,320 So now it says you got one out of three correct. 2755 04:07:26,320 --> 04:07:28,320 So it's able to actually grade our test. 2756 04:07:28,320 --> 04:07:32,320 And what's cool about the program we just wrote is if I wanted, 2757 04:07:32,320 --> 04:07:37,320 I could add another question into here and it would automatically be able to ask it. 2758 04:07:37,320 --> 04:07:40,320 So it doesn't matter how many questions we put in here. 2759 04:07:40,320 --> 04:07:42,320 It's just able to ask it. 2760 04:07:42,320 --> 04:07:48,320 So really the point I was trying to make with this whole video was just how we can use something like a class 2761 04:07:48,320 --> 04:07:51,320 in order to model a real world entity. 2762 04:07:51,320 --> 04:07:59,320 So for example, we created this question class and we were able to store all the information about a question that we could ever need. 2763 04:07:59,320 --> 04:08:02,320 So we were able to sort of like the prompts and the answer. 2764 04:08:02,320 --> 04:08:08,320 So we created our own question data type and then we were able to create a bunch of different questions, 2765 04:08:08,320 --> 04:08:14,320 pass it into this run test method and it was able to grade the test and just run the test really easily. 2766 04:08:14,320 --> 04:08:16,320 So that's kind of the point. 2767 04:08:16,320 --> 04:08:18,320 So hopefully this makes sense. 2768 04:08:18,320 --> 04:08:19,320 You know, this was a lot of code. 2769 04:08:19,320 --> 04:08:23,320 So if you don't fully understand what I did, go back, watch the video, you know, 2770 04:08:23,320 --> 04:08:28,320 try it a couple of times on your own and see if you can create your own multiple choice test. 2771 04:08:28,320 --> 04:08:36,320 In this tutorial, I want to talk to you guys about class functions in Python. 2772 04:08:36,320 --> 04:08:45,320 So a class function is essentially a function that we can use inside of a class and it can either modify the objects of that class 2773 04:08:45,320 --> 04:08:48,320 or it can give us specific information about those objects. 2774 04:08:48,320 --> 04:08:55,320 Over here, I have this student.py file and inside of here, I created a student class. 2775 04:08:55,320 --> 04:08:59,320 So I just had a class student and I gave this student a couple of different attributes. 2776 04:08:59,320 --> 04:09:03,320 I gave it a name, a major and a GPA. 2777 04:09:03,320 --> 04:09:06,320 So I can give this student all of this information. 2778 04:09:06,320 --> 04:09:10,320 So when I create a student object, it'll have all of that information in it. 2779 04:09:10,320 --> 04:09:15,320 Now over here on this app.py file, it actually created two students. 2780 04:09:15,320 --> 04:09:21,320 So student one is Oscar who is studying accounting and he has a GPA of 3.1. 2781 04:09:21,320 --> 04:09:26,320 Student two is Phyllis who's studying business and she has a 3.8. 2782 04:09:26,320 --> 04:09:32,320 So what we can actually do is we can use functions inside of these class files. 2783 04:09:32,320 --> 04:09:40,320 So I could actually define a function inside of this student class and then all of my student objects could access it. 2784 04:09:40,320 --> 04:09:50,320 So for example, I can come down here and create a function and let's say we wanted to create a function that told us whether or not this particular student had honors. 2785 04:09:50,320 --> 04:09:53,320 So whether or not they were on the honor roll, right? 2786 04:09:53,320 --> 04:09:56,320 Basically like, are they like a really good student? 2787 04:09:56,320 --> 04:10:01,320 So I could define a function called on honor roll. 2788 04:10:01,320 --> 04:10:09,320 And basically what it's going to do, it's going to tell me whether or not this particular student is on the honor roll. 2789 04:10:09,320 --> 04:10:16,320 And let's basically just say the rules for being on honor roll is you have to have a GPA of 3.5 or above. 2790 04:10:16,320 --> 04:10:20,320 So here GPA is 3.5 or above, that means you're on the honor roll. 2791 04:10:20,320 --> 04:10:23,320 Well, over here I could actually write the code for that. 2792 04:10:23,320 --> 04:10:33,320 So I could say if self.gpa, and when I say self over here, this is referring to like the actual student's GPA. 2793 04:10:33,320 --> 04:10:39,320 And you'll notice over here I have to pass that in as a parameter and that always has to be the first parameter. 2794 04:10:39,320 --> 04:10:52,320 So I could say if self.gpa is greater than or equal to 3.5, then we can return true because this student is on the honor roll. 2795 04:10:52,320 --> 04:11:03,320 Otherwise else we can just return false because if their GPA isn't above a 3.5 then they're not on the honor roll. 2796 04:11:03,320 --> 04:11:12,320 So that's essentially our little on honor roll function. And you can see it's a very small function, but it provides a service to the objects of this class. 2797 04:11:12,320 --> 04:11:18,320 It allows the objects of this class to figure out whether or not that current student is on the honor roll. 2798 04:11:18,320 --> 04:11:24,320 So over in this app.pa file I could just print out that value. 2799 04:11:24,320 --> 04:11:32,320 So I could say print and I'm going to see if student one is on the honor roll. 2800 04:11:32,320 --> 04:11:37,320 So now when I run this you'll see it should print out false. 2801 04:11:37,320 --> 04:11:43,320 And it's putting out false down here because student one only has a GPA of 3.1. 2802 04:11:43,320 --> 04:11:51,320 If I was to do the same thing on student two, student two has a GPA of 3.8 so we should return true. 2803 04:11:51,320 --> 04:11:53,320 And that's exactly what happened. 2804 04:11:53,320 --> 04:11:59,320 So essentially a class function is just a little function that can be used by the objects of the class. 2805 04:11:59,320 --> 04:12:04,320 And I would say this is a pretty good example of what a class function should be doing. 2806 04:12:04,320 --> 04:12:10,320 It should be like giving us information about the class or it can also be like modifying information about the class. 2807 04:12:10,320 --> 04:12:12,320 So that's the basics of using functions in classes. 2808 04:12:12,320 --> 04:12:21,320 I mean this is a pretty simple tutorial, but I really just wanted to introduce you guys to the concept of putting functions inside of classes. 2809 04:12:21,320 --> 04:12:22,320 This is going to be really useful. 2810 04:12:22,320 --> 04:12:40,320 And if you're making a class you always want to think about what functions can I put inside of here that will essentially help the user to either figure out information about the object or modify different values in the object. 2811 04:12:40,320 --> 04:12:44,320 In this tutorial I want to talk to you guys about inheritance in Python. 2812 04:12:44,320 --> 04:12:51,320 Now inheritance is basically where we can define a bunch of attributes and functions and things inside of a class. 2813 04:12:51,320 --> 04:12:56,320 And then we can create another class and we can inherit all of those attributes. 2814 04:12:56,320 --> 04:13:06,320 So I can basically have one class that has all the functionality of another class without having to physically write out any of the same methods or attributes. 2815 04:13:06,320 --> 04:13:11,320 So if that's confusing don't worry I'm going to show you guys exactly what this is in this video. 2816 04:13:11,320 --> 04:13:15,320 So let me show you about some stuff I have set up over here. 2817 04:13:15,320 --> 04:13:21,320 I created this class called chef. And over here we just have this class chef. 2818 04:13:21,320 --> 04:13:24,320 And inside of this chef class we have three functions. 2819 04:13:24,320 --> 04:13:26,320 So this chef can do three things. 2820 04:13:26,320 --> 04:13:28,320 The chef can make chicken. 2821 04:13:28,320 --> 04:13:33,320 The chef can make salad and the chef can make a special dish. 2822 04:13:33,320 --> 04:13:44,320 And whenever the chef makes a dish he basically just says the chef makes a chicken or the chef makes a salad or the chef makes barbecue ribs for this special dish. 2823 04:13:44,320 --> 04:13:48,320 It's basically just saying whatever the chef is doing. 2824 04:13:48,320 --> 04:13:54,320 So if I was to come over here to my app.python file you'll notice that I'm importing the chef. 2825 04:13:54,320 --> 04:13:58,320 So I'm basically allowed to use this chef class now. 2826 04:13:58,320 --> 04:14:01,320 And I can say my chef is equal to chef. 2827 04:14:01,320 --> 04:14:02,320 So I'm creating a new chef. 2828 04:14:02,320 --> 04:14:09,320 So now if I came down here and I said my chef dot make chicken. 2829 04:14:09,320 --> 04:14:14,320 When I run my program now it's going to say the chef makes a chicken. 2830 04:14:14,320 --> 04:14:26,320 If I said make special dish and I run the program now it's going to say the chef makes barbecue ribs because that's the chef's special dish. 2831 04:14:26,320 --> 04:14:29,320 So I have this really awesome chef class and it works really well. 2832 04:14:29,320 --> 04:14:35,320 But let's say that I wanted to create another class to model another type of chef. 2833 04:14:35,320 --> 04:14:39,320 So this chef class is just modeling like some generic chef. 2834 04:14:39,320 --> 04:14:42,320 We're representing some generic chef in our program. 2835 04:14:42,320 --> 04:14:47,320 But let's say that I wanted to create a class that modeled a different type of chef. 2836 04:14:47,320 --> 04:14:51,320 For example let's say we wanted to create a class that modeled a Chinese chef. 2837 04:14:51,320 --> 04:14:55,320 So instead of just a normal chef this is a Chinese chef. 2838 04:14:55,320 --> 04:14:59,320 Well I actually created a file over here called Chinese chef dot python. 2839 04:14:59,320 --> 04:15:06,320 So we could actually use this Chinese chef dot python file to create our Chinese chef class. 2840 04:15:06,320 --> 04:15:11,320 So I'm just going to say class and over here I'm just going to say Chinese chef. 2841 04:15:11,320 --> 04:15:17,320 And basically I just can define everything that the Chinese chef can do. 2842 04:15:17,320 --> 04:15:23,320 But let's say that our Chinese chef can do everything that our generic chef can do. 2843 04:15:23,320 --> 04:15:29,320 So the Chinese chef is like a special chef right it's a very specific type of chef. 2844 04:15:29,320 --> 04:15:33,320 But let's say that this Chinese chef can do everything that the normal chef can do. 2845 04:15:33,320 --> 04:15:38,320 So the Chinese chef can make chicken, can make salad and can also make a special dish. 2846 04:15:38,320 --> 04:15:49,320 Well if I wanted to give this Chinese chef all of that functionality I could just come over here and I could copy all of these functions and I could paste them in here. 2847 04:15:49,320 --> 04:15:54,320 So now the Chinese chef can do everything that the other chef can do. 2848 04:15:54,320 --> 04:16:01,320 But let's say that in addition to all of these things the Chinese chef can also make a special dish called fried rice. 2849 04:16:01,320 --> 04:16:06,320 So I could say deaf and we could say make fried rice. 2850 04:16:06,320 --> 04:16:16,320 So the Chinese chef has this extra function which allows it to make fried rice and over here we can just say the chef makes fried rice. 2851 04:16:16,320 --> 04:16:20,320 And let's also say that the Chinese chef has a different special dish. 2852 04:16:20,320 --> 04:16:25,320 So instead of making barbecue ribs the Chinese chef makes orange chicken. 2853 04:16:25,320 --> 04:16:33,320 Alright so now we have our Chinese chef class set up and you'll notice that it can do everything that the normal chef can do. 2854 04:16:33,320 --> 04:16:39,320 So let's go back over to our app.python file and I want to create a Chinese chef object. 2855 04:16:39,320 --> 04:16:49,320 So instead of importing from chef we're going to just import it from Chinese chef so I'll say Chinese chef and we're going to import Chinese chef. 2856 04:16:49,320 --> 04:16:53,320 So let's go ahead and make a Chinese chef object. 2857 04:16:53,320 --> 04:17:02,320 So down here I'm going to make one and I'll just say my and then my Chinese chef we can print out this special dish. 2858 04:17:02,320 --> 04:17:06,320 So remember the Chinese chef can do everything that the normal chef could do. 2859 04:17:06,320 --> 04:17:18,320 So if I say my Chinese chef make special dish when I run this program you'll see the regular chef is making barbecue ribs and the Chinese chef is making orange chicken. 2860 04:17:18,320 --> 04:17:28,320 So everything works out and this Chinese chef also has an extra method called make fried rice and you can also make fried rice. 2861 04:17:28,320 --> 04:17:38,320 But here's the problem right when I go over here to this Chinese chef dot pie when I wanted to use all of the functionality that was inside this chef class. 2862 04:17:38,320 --> 04:17:46,320 I actually had to copy and physically paste all of these functions down into this file right and that's kind of a drag. 2863 04:17:46,320 --> 04:17:52,320 I mean especially like imagine if this chef had like 20 or 30 of these different functions in it. 2864 04:17:52,320 --> 04:17:59,320 Well I have to copy and I have to paste all of those inside of this Chinese chef and this is where we can use something called inheritance. 2865 04:17:59,320 --> 04:18:07,320 So instead of having to copy and paste all of these functions I could just inherit these functions from that chef class. 2866 04:18:07,320 --> 04:18:16,320 And in order to do that I can actually just get rid of all of these and I can come over here and right next to where I'm saying class Chinese chef. 2867 04:18:16,320 --> 04:18:22,320 Inside of parentheses I can say the name of the class that I want to inherit from so I can just say chef. 2868 04:18:22,320 --> 04:18:32,320 And I'm also going to have to import this so I'm going to come over here and we'll say from chef import chef. 2869 04:18:32,320 --> 04:18:42,320 So basically what this is saying is inside of this Chinese chef I want to be able to use all of the functions that are contained inside of the chef class. 2870 04:18:42,320 --> 04:18:50,320 So I'll say that one more time. Inside of this Chinese chef class I want to be able to use all of the functions that are inside of this chef class. 2871 04:18:50,320 --> 04:19:02,320 So by using inheritance here I'm actually able to come over here to this app.py file and now I'm still able to execute for example like the make chicken function. 2872 04:19:02,320 --> 04:19:15,320 Even though I didn't specify the make chicken function in here I didn't write it out. I can still run this program and the Chinese chef will still be able to make chicken. 2873 04:19:15,320 --> 04:19:20,320 So when I run this program you'll see down here it says the chef makes chicken. 2874 04:19:20,320 --> 04:19:25,320 And that's because I inherited the make chicken method from the chef class. 2875 04:19:25,320 --> 04:19:32,320 But here's one thing that got messed up. If I come over here and I say make special dish. 2876 04:19:32,320 --> 04:19:36,320 Remember the Chinese chef special dish was supposed to be orange chicken. 2877 04:19:36,320 --> 04:19:47,320 But now the Chinese chef is going to be making barbecue ribs because I inherited this make special dish function from this chef class. 2878 04:19:47,320 --> 04:19:58,320 And so what I can actually do is I could come in here and I could override that make special dish class. So I could say death make special dish. 2879 04:19:58,320 --> 04:20:06,320 And down here I can just override it. So I could say the chef makes orange chicken. 2880 04:20:06,320 --> 04:20:12,320 And now when I go over here and I run this make special dish function. 2881 04:20:12,320 --> 04:20:18,320 Now the chef's going to be back to making orange chicken. So the Chinese chef will make his special dish. 2882 04:20:18,320 --> 04:20:26,320 So that's basically what inheritance does. It allows me to inherit functionality from an existing class into the new class. 2883 04:20:26,320 --> 04:20:31,320 And I can actually just inherit all of it without having to write it out. 2884 04:20:31,320 --> 04:20:36,320 So I didn't have to write out make chicken and make salad inside the Chinese class. 2885 04:20:36,320 --> 04:20:46,320 But the Chinese chef can still make chicken and salad because it inherited that functionality from the chef class. 2886 04:20:46,320 --> 04:20:50,320 In this tutorial I want to talk to you guys about the Python interpreter. 2887 04:20:50,320 --> 04:20:58,320 is basically a little environment that we can use to execute Python commands. 2888 04:20:58,320 --> 04:21:08,320 So it's kind of like this little sandbox environment where we can test out and try out different Python commands or different Python functions in a very safe and neutral environment. 2889 04:21:08,320 --> 04:21:13,320 So it's kind of like a quick and dirty way to write Python and try different things out. 2890 04:21:13,320 --> 04:21:18,320 And the way we use the Python interpreter is by opening up our command prompt. 2891 04:21:18,320 --> 04:21:22,320 Now if you're on Windows there's a program called command prompt. 2892 04:21:22,320 --> 04:21:30,320 If you're on Mac there's a program called terminal. And basically you need to open that program in order to use the Python interpreter. 2893 04:21:30,320 --> 04:21:37,320 So I'm here on a Mac but if you're on a Windows you can essentially do exactly what I'm doing just inside of the command prompt. 2894 04:21:37,320 --> 04:21:41,320 So on Mac here I'm just going to search for my terminal. 2895 04:21:41,320 --> 04:21:46,320 If you're on Windows you can just search for like CMD or command prompt and it should pop up. 2896 04:21:46,320 --> 04:21:57,320 So the terminal is basically an environment on our computer where we can interact with the computer and do different things without a graphical user interface. 2897 04:21:57,320 --> 04:22:05,320 So normally when we interact with the computer we're using things like buttons or windows or we can move stuff around. 2898 04:22:05,320 --> 04:22:16,320 We can basically just like interact with the computer with graphics but the terminal or the command prompt is a place where we can interact with the computer using text commands. 2899 04:22:16,320 --> 04:22:21,320 So you know I'm not going to get too much into what the terminal and the command prompt are but that's essentially what it is. 2900 04:22:21,320 --> 04:22:26,320 So inside of here we can use the Python interpreter. 2901 04:22:26,320 --> 04:22:33,320 And the Python interpreter is basically just a little program that like I said we can write you know sort of Python in. 2902 04:22:33,320 --> 04:22:40,320 So generally all you have to do is go over to your terminal or your command prompt and type in Python 3. 2903 04:22:40,320 --> 04:22:49,320 And since we're using Python 3 I'm going to type Python 3 but you can also just type normal Python and I'm pretty sure that gives you Python 2. 2904 04:22:49,320 --> 04:23:02,320 Now here's a quick disclaimer on Windows you may run into a problem where you can't use this Python 3 command and it's possible that if you're running into that problem Python. 2905 04:23:02,320 --> 04:23:07,320 Like Python 3 hasn't been added to your windows path variable. 2906 04:23:07,320 --> 04:23:13,320 And basically what that means is your computer doesn't recognize the command Python 3. 2907 04:23:13,320 --> 04:23:19,320 If that's the case all you have to do is just go and Google how to add Python 3 to your windows path variable. 2908 04:23:19,320 --> 04:23:28,320 It's really easy I'm not going to get into it in this video just because it's kind of outside the scope but if you're running into that problem that's probably it's not on your windows path variable. 2909 04:23:28,320 --> 04:23:32,320 So just Google around for that and you should be able to find an answer. 2910 04:23:32,320 --> 04:23:37,320 But I'm going to type Python 3 and click enter and it's going to open up this cool little thing down here. 2911 04:23:37,320 --> 04:23:41,320 So you can see it basically just has like here's the version of Python. 2912 04:23:41,320 --> 04:23:46,320 Now I'm just going to enter so we can have some like new lines here. 2913 04:23:46,320 --> 04:23:52,320 But down here this is the Python interpreter so we can write Python code in here and it's going to work. 2914 04:23:52,320 --> 04:23:58,320 So I can for example I could write like print and inside of here I could print like hello world. 2915 04:23:58,320 --> 04:24:02,320 And when I click enter you'll see that it prints out hello world. 2916 04:24:02,320 --> 04:24:07,320 I could create a variable so for example I could say like num one is equal to ten. 2917 04:24:07,320 --> 04:24:16,320 I could say num two is equal to ninety and then I could print out num one plus num two and it's going to be able to do that for me. 2918 04:24:16,320 --> 04:24:22,320 I could also use some like a function so I could define a function in here. 2919 04:24:22,320 --> 04:24:27,320 We could make a function that's called say hi and it'll take a name parameter. 2920 04:24:27,320 --> 04:24:36,320 And you'll see here when it saw I wanted to create a function it put these three little dots and I can indent and we can write the code for our function. 2921 04:24:36,320 --> 04:24:44,320 So I can say like print hello plus name. 2922 04:24:44,320 --> 04:24:57,320 And now I have a function called say hi so I can break out of this by just entering again and I can call the function so I could say say hi and we'll say hi to me. 2923 04:24:57,320 --> 04:25:08,320 Hi Mike so now it's going to say hello Mike so I can use a function I could use something like an if statement you know I could say I could use something like a for loop we could use while loops. 2924 04:25:08,320 --> 04:25:20,320 I mean we can basically use all of the basic like Python commands and you know inside of this Python interpreter and like I said before it's essentially just an environment where you can test out Python code. 2925 04:25:20,320 --> 04:25:25,320 Now this is not a place where you want to write like any serious Python scripts. 2926 04:25:25,320 --> 04:25:31,320 So if you're like designing and writing a Python program I would not recommend doing it inside of this interpreter. 2927 04:25:31,320 --> 04:25:42,320 It is it's not a very intuitive environment it's pretty much just set up for some quick and dirty tests like we need to test something out great this is a perfect place for it because you don't have to set up a file. 2928 04:25:42,320 --> 04:25:48,320 You don't have to use like some ID you don't have to execute a file you just go in here it's quick it's dirty you get it done. 2929 04:25:48,320 --> 04:25:55,320 But if you're writing like an actual program you definitely definitely definitely want to use a text editor. 2930 04:25:55,320 --> 04:26:06,320 The text editor it's just going to be way more organized everything is going to be a lot cleaner and you're not going to get confused with like where stuff is and what you have so that's the basics of the Python interpreter. 2931 04:26:06,320 --> 04:26:20,320 You know in the course I haven't really been using it at all just because I think it's a lot easier to teach when we're inside of a file than inside of here but you know don't count this guy out the Python interpreter is awesome a lot of people use it and it's great if you just 2932 04:26:20,320 --> 04:26:31,320 need to test you know little bits of code out without having to set up some huge environment so Python interpreter definitely awesome I would play around with it you know have some fun and take advantage of it. 2933 04:26:31,320 --> 04:26:37,320 Hey thanks for watching if you enjoyed the video please leave a like and subscribe to draft Academy to be the first to know when we release new content. 2934 04:26:37,320 --> 04:26:44,320 Also we're always looking to improve so if you have any constructive criticism or questions or anything leave a comment below. 2935 04:26:44,320 --> 04:26:52,320 Finally if you're enjoying draft Academy and you want to help us grow head over to draft Academy.com forward slash contribute and invest in our future. 363916

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.