Learn Python - Full Course for Beginners [Tutorial]
freeCodeCamp.org
0:00 In this course, I'm going to teach you everything
0:02 you need to know to get started programming in Python.
0:04 Now, Python is one of the most popular programming languages out there.
0:09 And it's by far one of the most sought after for jobs.
0:12 And so if you're trying to get a job
0:14 or you're trying to, you know, automate your life,
0:16 or you're trying to write awesome scripts to do a bunch of different things,
0:20 then Python's for you.
0:21 Honestly, more and more developers every day
0:23 are moving their projects over to Python
0:25 because it's such a powerful and it's such an easy to use language.
0:29 A lot of programming languages out there just aren't very beginner friendly.
0:33 There's a lot of syntax.
0:34 There's a lot of like little things that if you get wrong,
0:37 the program will yell at you.
0:38 Python is the complete opposite of that.
0:40 You basically just type out what you want to do and Python does it.
0:44 It's that simple.
0:44 There's not a whole lot of syntax to learn.
0:47 The learning curve is literally zero.
0:49 You jump in, you can start writing your first program in seconds.
0:52 In this course, I'm going to teach you guys
0:55 everything you need to know to get started in Python.
0:57 I designed this course, especially for Python.
1:00 And each lesson has been specially designed with examples that will help
1:04 you along the way with so many people starting to learn Python.
1:07 The question isn't, why should you learn Python?
1:09 The question is, why shouldn't you?
1:10 And I think for a lot of people,
1:12 the reason they might not want to learn Python is because
1:15 they're intimidated or they're afraid that it's going to be too hard.
1:18 Trust me, I am going to hold your hand through this entire course.
1:21 We're going to talk about all the core concepts in Python.
1:23 We're going to look at everything you need
1:25 to know to start programming in Python and start being
1:28 confident and start writing scripts and start writing programs
1:31 that are awesome and doing cool things in your life.
1:34 Anyway, I'm super pumped to be teaching you guys Python.
1:36 I can't wait to get started in this course.
1:38 And I hope you guys stick around and follow
1:41 along with the course and learn this amazing programming language.
1:43 In this tutorial, I'm going to show you
1:48 guys how to install Python onto your computer.
1:52 And we're also going to install a text editor
1:55 that we can use to write our Python programs in.
1:58 So the first order of business is to actually install Python on your computer.
2:02 So what we want to do is head over to our web browser.
2:06 And you want to go over here to this page,
2:08 it's just www.python.org forward slash downloads.
2:11 And on this page, there's going to be two buttons down here.
2:17 And it's just going to say download Python 3.6.3.
2:20 And at least that's the version that I
2:22 have right now or download Python 2.7.1.4.
2:25 So here's the thing about Python,
2:28 there's actually two major versions of Python that are in use currently.
2:33 There's Python two, and there's Python three.
2:35 So you can see over here, this is like 2.7.1.4.
2:39 Basically, any Python two version we would just refer to as like Python two.
2:46 And here's like the latest Python three version.
2:48 And so there's these two versions that you can download.
2:51 And the first thing we have to decide is which version do we want to download.
2:55 And here's the basic difference, Python two is a legacy version of Python,
3:00 which basically means that it's like an old version,
3:03 and it's not being like actively maintained
3:06 or supported like officially by Python anymore.
3:09 Python three is like the future of Python.
3:11 It's like the newest version.
3:13 It's the one that's getting actively maintained and supported.
3:16 And so there's pros and cons to picking either one.
3:19 A lot of people will say like Python two has been around longer.
3:23 And so there's more like libraries,
3:26 there's basically just more Python code written in Python two.
3:30 So therefore, like there's a few more things that you can do in Python two,
3:33 then you can do in Python three.
3:35 But then again, Python two is not supported.
3:37 And you know, five or 10 years from now, probably no one's going to be using it.
3:42 Python three, like I said, is like the future of Python.
3:45 It's the newest version.
3:46 It's the one that, you know, is sort of being maintained going forward.
3:49 So for the purposes of this tutorial,
3:51 I'm going to be teaching you guys Python three.
3:54 So we're going to want to download Python three.
3:56 Now here's the thing, like, if you learn Python three,
3:59 or you learn Python two, the differences aren't very big.
4:03 There are a few differences in like some syntax and just some,
4:06 you know, little things here and there.
4:08 But if you learn Python three,
4:09 you'll be able to jump right in and code in Python two as well.
4:13 Don't let anyone, you know, tell you that you have to learn one or the other,
4:17 we're going to learn Python three just because it's
4:19 I think probably the best for a beginner to learn.
4:21 And so that's the one that we want to download.
4:23 So I'm just going to click download Python three.
4:25 And we're going to go ahead and download this version.
4:28 And when that's done downloading,
4:30 I'm just going to head down to my downloads folder.
4:33 And you'll see here, we just have this file, I'm just going to double click.
4:37 And we get this like Python installer.
4:39 So I'm just going to click through here.
4:41 And we'll be able to install Python three on our computer.
4:45 All right, when that's done installing,
4:46 we can just close out of this and we should be good to go.
4:49 So now we have Python three installed on our computer.
4:52 The next step is we need to choose a text editor.
4:56 So we're going to be writing a bunch of code.
4:59 So we're going to need some, you know,
5:01 environment or some program that we can write all that code in.
5:04 And really, you can write Python in any text editor,
5:07 you could write it in like notepad or text edit, it doesn't really matter.
5:11 But there are special text editors
5:13 that are designed just for writing Python code.
5:16 And these are called IDEs.
5:18 And that stands for integrated development environment.
5:21 It's basically just a special environment where we can,
5:24 you know, run and execute our Python code.
5:27 And it'll, you know, basically, like tell us how we're doing.
5:29 So if we're, you know, if we write something that's wrong, or we have errors,
5:33 it'll kind of point us in the right direction
5:35 of what we need to do to fix that stuff.
5:37 So in this course, we're going to be using an IDE.
5:39 And one of my favorites,
5:40 and one of the most popular IDEs for Python is called pie charm.
5:44 So I'm over here on this website,
5:46 it's called jetbrains.com forward slash pie charm.
5:49 And this is the IDE that we're going to be using in this course.
5:54 So I'm just going to come over here and click this big download now button.
5:58 And we'll go ahead and download this pie charm program.
6:01 Alright, so I'm getting this page over here.
6:03 You can see there's two versions of pie charm.
6:06 One is a professional version, and that one's like paid,
6:08 so you have to pay for it.
6:10 But then there's this community version down here,
6:12 and that's free and open source.
6:13 So I'm just going to download this one.
6:15 And this should have everything we need to get started using Python.
6:18 Alright, when that finishes downloading,
6:20 I'm just going to pull that up in my downloads folder.
6:23 And again, we can just sort of run this guy.
6:26 And I'm on a Mac.
6:27 So I'm going to have to drag it over to my applications folder.
6:30 Alright, so we now have pie charm.
6:32 And we have Python three installed on our computer.
6:36 So we're ready to start programming in Python.
6:39 And this tutorial, we're going to create our first Python program.
6:44 And we're going to be able to run it and see how everything works.
6:50 The first thing I want to do is open up pie charm.
6:53 So pie charm was the IDE,
6:55 the integrated development environment that we downloaded in the last tutorial.
6:59 So I'm just going to go ahead and search for it on my computer.
7:03 And I can just open it up.
7:04 And when we first open up pie charm,
7:06 we should basically just get a window prompting us to create a project.
7:10 So you can see that down here.
7:12 Actually, the first thing I want to do though is come over here to configure.
7:15 And I'm just going to click on preferences.
7:18 And I actually want to change the appearance.
7:22 So I'm going to go ahead and give this a theme.
7:25 So I just went over here to appearance and behavior, appearance.
7:28 And I'm going to change the theme to dark.
7:30 Just because I think it's a little bit easier to use and look at.
7:34 But you don't have to do that if you don't want to.
7:37 And so down here, I'm just going to click create new project.
7:40 And it's going to give us this little window right here.
7:43 So I can just name my project.
7:45 And I'm just going to name this draft.
7:47 And then there's this other option down here where it says interpreter.
7:52 Now, what we want to select is Python version three.
7:57 So if I click down here, you'll notice that there's a couple different options.
8:00 So I'm on a Mac computer.
8:02 And by default, Mac has Python version two installed on it.
8:07 If you're on a Windows machine, I'm not sure if it does.
8:11 But in this tutorial, we're going to be using Python three.
8:14 So you want to make sure that you have Python three selected as the interpreter.
8:18 Otherwise, you might not be able to fully
8:21 follow along with what we're doing in this video.
8:22 So now I'm just going to come down here and click create.
8:25 And this is going to go ahead and create our project for us.
8:29 All right, so now we have our Python project up and running.
8:32 And I want to show you guys how we can create our first Python program.
8:36 So over here in this draft folder,
8:38 and this is just that project that we created,
8:41 I'm just going to right click and I'm going to say new.
8:44 And I'm going to come down and click Python file.
8:47 So we want to create a new Python file, this will be our first Python program.
8:52 And I can just give this a name.
8:53 So why don't we just call this app?
8:55 And I'm just going to click okay.
8:56 All right, so now we have our first Python file open.
9:00 And we can just start typing in some Python.
9:03 So I want to show you guys just a very basic Python program.
9:06 And this is just going to be a hello world program.
9:08 So we're just going to print something out onto the screen.
9:11 So what I want to do is I want to type out PRINT print.
9:15 And I want to type in open and closed parentheses.
9:18 Now inside of this parentheses, we can make some quotation marks.
9:22 And inside of the quotation marks, I'm just going to type out hello world.
9:26 So this is like a very basic Python program.
9:29 And what this is going to do is it's
9:32 just going to print out hello world onto the screen.
9:34 And so now what I can do is I can actually
9:37 run this file and we're actually getting a little error here.
9:39 It wants us to create a new line at the end of the file.
9:42 And so now in order to run our Python program,
9:45 I'm just going to come up here to the top.
9:47 And I'm just going to click run.
9:49 And we'll click this run button right here.
9:51 And now we'll see what happens.
9:52 So it's asking me what I want to run.
9:55 I can just click app.
9:56 And down here at the bottom,
9:58 you'll notice that we have this text that just got printed out hello world.
10:02 And so whatever I print, or whatever I put inside of this print statement is
10:07 going to get printed out down below in the console.
10:09 So as long as you're getting that print statement to work,
10:12 then you've actually written your first Python program.
10:15 And as we go through forward in the course,
10:17 we're going to be writing all sorts of awesome Python programs.
10:19 And we're going to be doing all sorts of cool stuff.
10:26 In this tutorial, I want to talk to you about writing a basic Python program.
10:29 So we're going to write just a very simple Python program.
10:32 We're going to look at how we can go ahead and write our programs,
10:36 we're going to talk about how our programs are getting executed by Python.
10:40 And we're also going to draw out a little shape onto the screen.
10:43 So this is going to be pretty cool.
10:45 I'm excited to talk to you guys about just the bare basics of Python,
10:48 and really just sort of give you an introduction.
10:51 So over here, we have our basic text editor.
10:54 And so any of the Python code that I write inside
10:59 of this file is actually going to get executed by Python.
11:03 So this Python file has a bunch of Python code inside of it.
11:07 And basically what we're doing when we're programming in Python
11:10 is we're just giving the computer a set of instructions.
11:13 Right, that's really what programming is,
11:15 it's just like we're specifying a set of instructions.
11:17 And the computer is going to go off
11:20 and execute those instructions that we give it.
11:22 And so the more complex the instructions we give to the computer,
11:26 the more complex the tasks the computer can carry out.
11:29 So I want to show you guys how we can use some Python instructions
11:33 or some Python code in order to draw out a shape onto the screen.
11:37 So I'm actually going to draw out a little triangle.
11:40 So over here, we can actually print something out
11:43 onto the screen and Python using something called print statements.
11:46 So I can just type out print.
11:48 And inside of these parentheses,
11:50 I can just type some quotation marks and inside the quotation marks,
11:54 we can put whatever we want to print out onto the screen.
11:57 So I'm going to copy this and I'm actually
12:00 going to make a few of these little print statements.
12:02 And we're going to actually draw a shape.
12:04 So we're going to draw like a triangle type shape.
12:07 So down here, I'm just going to draw a forward slash.
12:10 And then up here, I'll make a space and we'll do another forward slash.
12:14 Here, we'll make two spaces, we'll do another forward slash.
12:17 And over here, we're going to make three spaces and a forward slash.
12:20 And now I'm going to draw like vertical bars going all the way down.
12:27 And actually, we can do some underscores here as well.
12:29 That'd be kind of cool.
12:30 All right, so now we have our triangle looking shape
12:33 and you can see it looks kind of like a right triangle.
12:36 So I can save my file.
12:39 And I can either come up here to run and click run app.
12:43 Or if you're using PyCharm,
12:44 you can also just come over here and click this play button.
12:47 And when I click the play button, I want you to notice what happens down here.
12:52 So down here, we have this little window and this is called the console.
12:56 And basically, the console is just a place
12:58 where Python is going to output some information.
13:01 So when we use this print statement, we're basically telling Python like,
13:05 Hey, can you print something out onto the console?
13:07 So there's a lot of situations where we're going
13:10 to want to see what's going on in our code.
13:12 And we can use that print statement and print things out to the console.
13:16 And we can basically like have a little window into what our program is doing.
13:20 In our case, we're just going to be printing out this triangle onto the console.
13:23 So I'm going to come over here and click this play button.
13:27 And you'll see down here, we're actually printing out that triangle.
13:30 So mission accomplished, we were able to print out our shape.
13:34 So I could really draw any shape I wanted as long
13:37 as I was able to specify it inside of these print statements.
13:41 So essentially, what Python's doing when I click that play button,
13:44 is it's going into this file and it's
13:47 looking at all of these instructions in order.
13:49 So the first thing it does is it goes to this instruction, and it says, okay,
13:53 they want me to draw out these like little
13:55 forward slash in this vertical bar onto the screen.
13:57 Once it does that, it goes down to this instruction,
14:00 it says, okay, they want me to print something out again.
14:02 When it prints that out, it goes down to this instruction, etc.
14:06 So Python is actually going to execute the lines of code that we write in order.
14:11 So the order in which I write the instructions matters a lot.
14:15 So for example, if I was to take this last line here,
14:19 and put it up here at the top.
14:21 Now when I save the file, instead of just drawing out that triangle,
14:25 it's going to draw out this little upside down bottom thing at the top,
14:30 and then the rest of the triangle down here.
14:32 So the order of the instructions actually matters a lot.
14:35 So this is basically how your Python programs are going to go.
14:38 As we go through this course,
14:40 we're going to learn more and more of these little lines.
14:43 So here, we're just using this little print function,
14:45 basically, it just prints something out onto the screen.
14:48 There's a bunch of little things like this that we can learn as we go through,
14:51 we'll learn more and more instructions that we can give to the computer.
14:54 But in this lesson, I just want to kind of show you guys the basics, right?
14:59 We can essentially just define these little instructions for the computer,
15:04 and the computer will execute them in order.
15:07 In this tutorial, I want to talk to you guys about using variables in Python.
15:14 Now in Python, you're going to be dealing with a lot of data.
15:18 So a lot of times in our program,
15:21 we're going to be working with all types of information and data and values.
15:25 And sometimes that data can be difficult to manage.
15:28 So in Python, we have this thing called a variable.
15:32 And it's basically just a container, where we can store certain data values.
15:36 And when we use a variable, when we put those data values inside containers,
15:41 it makes it a lot easier for us to work
15:44 with and manage all of the different data inside of our programs.
15:49 So variables are extremely helpful.
15:50 And it's sort of like a core topic in Python
15:53 that you're really going to want to know in master.
15:55 So in this tutorial, I'm going to teach you guys everything you
15:58 need to know to get started with variables,
16:00 we're going to look at what they are,
16:02 why they're useful, and we'll look at the different types of variables
16:05 and the different types of data that we can store inside of variables.
16:09 But over here in my Python file, I just have a very simple program.
16:13 And it's basically just printing some text out onto the screen.
16:16 And so this is actually a little story that I wrote.
16:19 It says there once was a man named George, he was 70 years old.
16:24 He really liked the name George, but didn't like being 70.
16:28 So this is a valid Python program.
16:30 I could go ahead and run this program.
16:33 And you'll see down here in the console,
16:35 everything prints out and it looks great.
16:37 So we have an awesome little Python program here.
16:40 But let's say that inside of our story, I wanted to change the character's name.
16:45 So instead of naming the character George,
16:47 let's say I wanted to name the character John.
16:49 Well, I'm going to have to go through and I'm going to have to manually change
16:54 the name George to the name John at every
16:57 place inside of this story where it's mentioned.
16:59 So I'm going to have to go over here and I'll say,
17:02 okay, John, I'm going to have to manually change that.
17:04 And then, okay, down here, we're gonna have to again,
17:07 manually type in John and change it to John.
17:09 So in order to change the character's name,
17:11 I had to manually go in and change it in both places.
17:15 And now let's say, okay, maybe we want to change the character's name age also.
17:20 So in addition to changing the name,
17:22 we also want to change the age, let's make them a little bit younger.
17:25 So why don't we say John is going to be 35.
17:28 And so you'll see here, I had to come here and manually change the age
17:33 and then come down here and manually change it again.
17:36 Right.
17:36 And now that we changed it, like it's going to work,
17:39 it's going to be updated in our program.
17:41 But you'll notice that in order to make that change,
17:44 like I had to look through the entire program,
17:46 find it where that value was and change it.
17:49 And this was only with four lines of the story.
17:53 Imagine if I had a story that was like, thousands of lines long.
17:56 And we mentioned the character's name and age, like hundreds of times.
18:00 I mean, I'd have to look through each one
18:02 of those lines and manually change the character's name.
18:05 And that is not a very good way for us to do this.
18:09 And it's really not a good way for us to manage the data in our program,
18:13 right, the character's name and the age.
18:15 And so we can actually use a variable in order
18:19 to store the character's name and the character's age.
18:21 And when we use that variable, it'll make it a lot easier for us to put
18:26 the character's name and age inside of our little program here.
18:29 So I'm going to show you guys how we
18:31 can create a variable for the character's name and age.
18:33 And I'm just going to go up here above this print statement.
18:36 And up here, I want to create a variable.
18:39 So when we create a variable in Python,
18:42 we actually need to give Python a couple pieces of information.
18:46 The first piece of information we need
18:49 to give Python is the name of the variable.
18:51 So I need to actually assign a name
18:54 to this container where we're going to be storing information.
18:56 And so all I have to do is just type
18:59 out the name of the variable that I want to create.
19:01 So I'm going to create a variable called character name.
19:04 So I'm just going to say character.
19:07 And I'm going to say underscore name.
19:10 And so generally, when you're creating a name for a variable in Python,
19:14 you want to separate different words with an underscore.
19:17 So I have two words here, and I'm separating them with an underscore.
19:22 And now what I need to do is I need to put a value inside of this variable.
19:27 So basically, what I can do is I can just say equals,
19:29 and over here, I can type a value.
19:31 So I'm just going to type out the character's name inside of quotation marks.
19:35 So we're just going to type out john.
19:38 So now we have a variable for the character's name.
19:41 And below this character name variable,
19:44 I want to create another variable called character age.
19:47 So we're just going to type out character underscore age.
19:51 And I'm going to set this equal to the character's age, which is 35.
19:58 So we now have two variables,
19:59 one representing the character's name and one representing the character's age.
20:03 So what I can do now is I can replace
20:06 the character's name inside of the story with this variable.
20:09 And I'm going to show you guys how to do that.
20:11 So if we want to put a variable inside of this print statement,
20:14 I'm actually going to have to do a couple of things.
20:17 The first thing I'm going to have to do is end off this text in here.
20:22 So I'm going to have to put a quotation marks here at the end.
20:26 And you can see I'm basically wrapping
20:28 this whole thing into a single like quoted line.
20:31 And now I also have this text over here at the end.
20:34 So I'm going to have to put a quotation mark here.
20:36 So now I have a bunch of text here in quotation marks.
20:40 And then I also have text over here in quotation marks.
20:43 Now inside of here, instead of saying the character's name,
20:47 I want to refer to that character name variable.
20:51 So I'm just going to say plus character underscore name.
20:57 And I want to say another plus sign.
20:59 So I basically am saying I want to print out all of this text,
21:03 plus the value that's stored inside of character name, plus all of this text.
21:09 And what this will do is actually print out the character's name.
21:12 We print this.
21:13 So I'm going to go ahead and run this program.
21:15 And you'll see down here, and actually let me put a space over here.
21:19 And we'll run it again.
21:21 So you can see down here,
21:23 we still are printing out there once was a man named John.
21:27 And so what Python is doing is when it executes this line of code,
21:31 it's coming over here and it's seeing, okay,
21:33 they want to print out the value inside of character name.
21:37 So it Python is going to go up here, it's going to get the value.
21:41 And it's going to insert it inside of there.
21:43 So I can basically do this for every instance of the character name in my file.
21:47 So I can, you know, I'll just copy this guy right here.
21:51 And I can just paste it down here where the character's name is.
21:55 So I'll paste this.
21:56 And you can see now we're doing exactly the same thing that we did up there.
22:01 I can also do this for the character's age.
22:03 So I'm going to come down here and I'll paste in what we just paste it in.
22:08 And I'm going to change this instead of character name,
22:10 we'll change it to character age.
22:12 And I'm going to do this in one more spot.
22:15 So we're going to do this over here where we're mentioning the age.
22:19 And again, I'll just change this to character age.
22:22 And so now when I run this program,
22:25 it's going to print out the same story that we were printing out before.
22:29 But now I don't actually have to type
22:32 in the character's name and the character's age.
22:34 I can just refer to this variable.
22:36 And the cool thing about variables is
22:38 if I wanted to change the character's name,
22:41 now all I have to do is change it up here.
22:44 So I could change the character's name to like Tom or something.
22:47 And we could also change their age.
22:49 So I could change the age to be like 50.
22:51 And now that's going to automatically update throughout our entire story.
22:55 So when I click this play button, you'll see now we're using the name Tom.
23:00 And he's 50 years old.
23:02 So that's where variables can come in handy.
23:05 And this can be a really awesome way to control
23:08 and manage the data that's inside of our programs.
23:11 With variables, you can also modify their values.
23:14 So for example, let's say that halfway through the story,
23:17 I wanted to change the character's name.
23:19 Well, I can just make some new lines over here.
23:22 And down here, I can actually assign a new value to one of these variables.
23:27 So I could say like character underscore name.
23:30 And in order to give it a different value,
23:33 all I have to do is just say equals, and we'll give it a different value.
23:37 So let's say halfway through the story,
23:39 we want to change the character's name to Mike.
23:41 Well, now when I run on my program,
23:44 you'll see here in the first part of the story,
23:46 it says there once was a man named Tom.
23:49 And in the second part of the story, it's referring to the name as Mike.
23:54 So I actually updated the variable inside of my program over here.
23:57 And that's a really awesome way.
23:59 You know, a lot of times in these programs,
24:02 you're going to want to be modifying the values of variables.
24:05 So you can do it just like that.
24:07 So finally, I want to talk to you guys about the different
24:10 types of data that we can store inside of these variables.
24:13 So over here, I have the character name and the character age.
24:18 And I'm actually storing what are called strings.
24:21 So a string is basically just plain text, right?
24:25 So the name Tom, that's just plain text.
24:28 Down here, these guys are strings as well.
24:31 So there's a data type in Python called a string,
24:35 and it's just basically going to store plain text.
24:39 So, you know, any text that you would have like in a story,
24:42 or you know, someone's name, you can store inside of a string.
24:45 There's also another type of data that we
24:48 can store inside of Python, which is numbers.
24:50 So in addition to storing text, we can also store numbers.
24:54 So if I wanted to store a number, for example,
24:56 let's say we want to store the character age instead of inside of a string,
25:00 we could store it inside of a number.
25:03 So I could just type out 50.
25:05 And when we're storing a number, we don't need these quotation marks,
25:09 you only need that quotation mark when you're storing a string.
25:13 So if I want to store a number, I can just write it out like this.
25:16 And in addition to using whole numbers, I could also use decimal numbers.
25:21 So I could say like 50.5678213.
25:24 And Python's going to be able to store that number as well.
25:27 So you can store all different types of numbers.
25:29 And we can also store what's called a Boolean value.
25:33 And a Boolean value is essentially a true or a false value.
25:38 And there's a lot of instances in programming where
25:41 we're going to want to represent true or false data.
25:44 For example, I could have a variable called like is mail.
25:48 And this would tell me whether or not someone was a male.
25:52 And in my case, it would be true because I'm a guy, right?
25:55 So I can actually this needs to be capital.
25:58 So I could sort of value of either true or false.
26:02 And actually, if we were naming this in Python, we want to use an underscore.
26:07 So I would say is underscore mail.
26:10 And this can be either true or false.
26:12 So true or false values is maybe not something
26:15 that you're used to dealing with like in the real world.
26:18 But in programming, we're going to be using true or false values all the time.
26:23 They're super important.
26:24 So like I said, there's three basic types
26:27 of data that we can work with in Python.
26:29 There's strings, which is just like plain text numbers,
26:32 which would be like either decimal numbers or whole numbers like this.
26:36 And there's true or false values.
26:38 And these are the basic types of data.
26:40 I mean, there's a bunch of other types of data that we can use.
26:44 But I would say 99% of the time as a new user and a new programmer to Python,
26:49 you're just going to be dealing with these three types of data.
26:52 So those are the basics of the data and also with variables.
26:56 And you're going to be using variables all the time in Python.
27:00 So you definitely want to practice up and get comfortable using them.
27:06 In this tutorial, I want to talk
27:08 to you guys about working with strings in Python.
27:11 Now one of the most common types of data that we're
27:14 going to be working with in Python is going to be strings.
27:17 And strings are basically just plain text.
27:19 So any text that we want to have inside of our program,
27:23 we can store inside of a string.
27:25 So I want to talk to you guys about all the cool things we can do with strings.
27:28 And we're basically just going to get
27:30 a full introduction into why strings are awesome.
27:33 So over here, I'm just going to actually print out a string.
27:36 So I'm just going to say print and inside of these parentheses,
27:40 I can type out a string.
27:42 In order to create a string, I need to use quotation marks.
27:46 So I can make an open and close quotation marks just like that.
27:50 Now inside of the quotation mark,
27:51 I can put whatever text I want the string to have.
27:54 So we could say like, draft Academy.
27:56 And so now we have a string with the text draft Academy inside of it.
28:01 So if I run my program, now down here,
28:04 we're going to print out draft Academy, as you can see.
28:07 And when we have these strings,
28:09 there's actually a bunch of cool things that we can do with them.
28:12 So one thing I could do would be to create a new line inside of the string.
28:16 So if I want it, I can come over here, and I could say backslash n.
28:20 And you can see it got highlighted in a different color.
28:23 And what this is going to do is it's
28:25 actually going to insert a new line into the string.
28:27 So now it's going to say draft on one line and Academy on another line.
28:31 So I'm going to click play.
28:33 And you'll see down here, we just get draft Academy.
28:37 In addition to the backslash n, I could also use a backslash quotation mark.
28:42 So if I wanted to put a quotation mark inside of my string,
28:46 I can't just put a quotation mark like that, because Python
28:49 is going to think that I'm trying to end this string.
28:53 So if I want to include a quotation mark,
28:55 I can just use this special backslash character.
28:57 And that's called the escape character.
28:59 And it basically just tells Python that whatever character comes after it,
29:04 we want to render literally.
29:06 So when I say backslash quotation mark, it basically means like,
29:10 Hey, Python, I want to print out a quotation mark.
29:12 And so now we'll be able to print out a quotation mark right there.
29:16 I can also use this to print out a backslash.
29:18 So if I needed to print out a backslash, I could say backslash.
29:22 And it'll just print out a normal backslash now.
29:25 So you can see just like that.
29:27 So if you want, you can use that backslash
29:29 to make new lines or print out quotation marks,
29:31 or you can just use it as a normal backslash.
29:34 In addition to just typing out a string here,
29:37 I could also create a string variable.
29:39 So I could come down here up here,
29:41 and we can create a variable and we'll just call it phrase.
29:45 And I'm going to set it equal to draft academy.
29:48 So I can store this string value inside of a variable called phrase.
29:52 And then when I want to print out that variable,
29:56 or I want to access that string variable,
29:58 I can just type the name of the variable.
30:02 And you'll see that it's going to print
30:04 out the value that was stored inside of it.
30:06 I can also use something called concatenation.
30:08 And concatenation is basically the process of taking
30:12 a string and appending another string onto it.
30:14 So I could come over here and I could say phrase, and I can say plus.
30:19 And now I can add in another string.
30:21 So I could say like, is cool.
30:23 And now this is going to say draft academy is cool.
30:27 So I'm basically appending another string onto another one,
30:31 they call that concatenation.
30:32 And so in addition to doing all that stuff,
30:36 we can also use special things called functions.
30:39 And a function is basically just a little block of code that we can run.
30:43 And it will perform a specific operation for us.
30:46 So we can use functions to modify our strings.
30:50 And we can also use functions to get information about our strings.
30:54 So I'm going to show you guys a couple
30:57 common functions we can use with these strings.
30:59 And they're actually going to do awesome stuff.
31:02 So in order to access one of these functions,
31:04 I can just say phrase, and then I can say dot.
31:06 And I'm going to show you guys a function that we
31:09 can use to convert this string entirely into lower case.
31:12 So I could just say phrase dot lower.
31:14 And I'm going to want to type an open and close parentheses.
31:17 And this will take this phrase,
31:19 it'll take my string and it'll convert it to lowercase.
31:22 So you can see now we just have draft academy down here,
31:25 but it's entirely in lowercase.
31:26 I could also do the same thing for uppercase.
31:29 So I could say phrase dot upper.
31:31 And this will convert the entire string into uppercase.
31:34 You can see now it's entirely uppercase.
31:37 So in addition to converting the string into uppercase and lowercase,
31:41 I could also check to see if
31:43 a string is entirely uppercase or entirely lowercase.
31:45 So for example, I could say phrase dot is upper.
31:48 And this is going to give me back a true or a false value.
31:53 So it's going to be true.
31:55 If the string is entirely uppercase, or false, if it's not.
31:58 And you can see here, we're getting a false value because this is an uppercase.
32:02 I can also use these functions in combination with each other.
32:07 So for example, I could say phrase dot upper, and then parentheses.
32:13 And then after this, I could say dot is upper.
32:17 And now what it's going to do is it's going to run this upper function,
32:21 it's going to convert it into uppercase.
32:22 And then it's going to run this is upper function right after that.
32:26 And you'll see now we're going to get a true value
32:30 back because it will have converted the whole thing into uppercase.
32:33 So you can see now we're getting a true value.
32:35 So you can use these functions one after another, it can be really useful.
32:39 And there's a few other ones that I want to show you.
32:42 So we can also figure out the length of this string.
32:45 So if I wanted to figure out how many characters were inside of this string,
32:50 I could just say LEN.
32:52 And I'm actually going to make an open parentheses.
32:54 And I'm going to make a closed parentheses.
32:57 So I'm essentially saying LEN.
32:58 And this is another function, it's the length function.
33:01 And inside of this LEN function, I'm actually passing this phrase variable,
33:06 I'm basically giving the length function
33:08 this, and it's going to spit out a number.
33:11 So it'll tell me how many characters are inside of this string.
33:14 And so you can see here we get
33:16 15 because there's 15 characters inside of draft academy.
33:19 So that's how we can get the length of a string.
33:22 And that's going to be really useful as we go forward and Python,
33:25 we can also get individual characters inside of a string.
33:28 So imagine if I wanted to just grab one of these characters,
33:32 like imagine if I wanted to figure out
33:35 what the first character in this string is,
33:37 we can actually use an open and closed square bracket, just like that.
33:41 And in here, I can specify the index of the character that I want to grab.
33:46 So if I wanted to grab the first character in this string, I can put in a zero.
33:51 So if I say phrase square bracket zero, this is going to give me that capital G.
33:56 And you can see down here, that's what gets printed out.
33:59 Now I want to point something out in Python,
34:02 when we're working with strings, a string gets indexed starting with zero.
34:08 So you'll notice that in order to access this G, I had to put a zero in here.
34:13 And that's because in Python,
34:15 when we use indexes on a string, we start with zero.
34:18 So if Python is counting the characters,
34:21 or it's indexing the characters in a string, it's going to start with zero.
34:24 So it's going to say G is zero, i is one, r is two, a is three.
34:29 So we would say that G is at position zero in the string,
34:33 i is at position one, r is at position two, a is at position three, etc.
34:38 So we start counting at zero.
34:40 So if I wanted to access the first character in the string,
34:45 this G, I have to say phrase zero, and that's just a special thing in Python,
34:51 and actually most programming languages do that.
34:54 So they'll start with zero.
34:55 So for example, if I wanted to access this a, I'm going to have to put zero 123.
35:01 So if I put three inside of here,
35:04 now we'll be able to access that first a, as you can see,
35:10 we get an a right here.
35:11 So this is actually really useful being able
35:13 to grab a specific character inside of a string,
35:16 and you're going to be using that a lot as we go forward in Python,
35:19 there's also another really awesome function that we
35:22 can use and it's called the index function.
35:24 And basically what the index function will do is it'll tell us
35:29 where a specific character or string is located inside of our string.
35:34 So I could say phrase dot index.
35:37 And I can actually give this a value.
35:39 So sometimes when we use these functions in Python,
35:42 we can actually give them information.
35:44 So I can give this a value.
35:46 And we would call this passing a parameter.
35:49 So I would call a value that I would give to a function a parameter,
35:53 and you'll hear that word a lot.
35:55 So for example, I could say inside of here capital G.
35:59 And now this is going to return the index of the capital G inside of our string.
36:05 So it should give us back a zero because G is at the zero of index.
36:10 And you'll see down here that we get a zero because that's where G is.
36:14 So for example, if I put an a inside of here,
36:17 like a lowercase a, it should give me 0123,
36:20 because that's where the first a is inside of this string.
36:24 So I can click the play button.
36:26 And you'll see it's giving us a three, you can also put actual words in here.
36:31 So for example, I could put like Academy in here,
36:33 or I could even just put like a C A D.
36:36 And this is going to tell me where this starts inside of my string.
36:41 So when I click play, it's going to give me an eight
36:46 because that's where Academy starts right 012345678.
36:50 If I put something in here that wasn't in the string, so for example,
36:54 if I put a Z in here, that's not in here, it's actually going to throw an error.
36:58 So when I play this, you'll see that we get this error
37:01 down here because Z is not found inside of our program.
37:04 So that index function, again, can be really useful.
37:06 And we'll be using that a lot.
37:08 There's also one more that I want to show you, which is called replace.
37:12 So we can say phrase dot replace.
37:14 And in here, I can actually give this to parameters.
37:17 So I can give this replace function to values that it can use.
37:22 And the first thing I want to you to give it is what I want to replace.
37:26 So for example, I could say draft.
37:28 And then I'm going to put a comma in here.
37:30 And I want to put in here what I want to replace draft with.
37:33 So I could just say like elephant.
37:35 And now instead of saying draft Academy,
37:38 this is going to print out elephant Academy.
37:40 So you can see down here, it just says elephant Academy.
37:43 So this replace function can be really awesome because we can replace
37:48 certain words or even certain letters inside of our strings with other ones.
37:52 So those are just some basic ways
37:54 that we can work with strings inside of Python.
37:57 And there's a lot of these different functions that we can use with strings.
38:01 These are some of the most common the ones that I just showed you right now.
38:05 But if you just do a Google search,
38:06 you can find all sorts of Python functions that you can
38:09 try out and use and you know, see what they do.
38:12 But you definitely want to get comfortable working with strings in Python,
38:15 because you're going to be working with them a lot.
38:19 In this tutorial, I want to talk to you about working with numbers in Python.
38:25 Numbers are one of the most common data types in Python.
38:30 And any Python program you write is most likely
38:33 going to be dealing with numbers at some point.
38:35 So I want to talk to you guys about just the basics of using numbers,
38:38 we're going to talk about the different types
38:40 of numbers that we can represent in Python.
38:42 And I also want to show you guys
38:44 some awesome functions that we can use with numbers.
38:47 So we can do certain like mathematical operations,
38:50 we can do some awesome things with numbers.
38:52 I'm going to show you guys all of that in this video, it's going to be awesome.
38:56 So first thing we can do is we can actually just like print out a number.
39:00 So if I want to write a number in Python, it's really easy.
39:03 I basically just write the number.
39:04 So I could say like print two.
39:06 And this is just going to print the number two out onto the screen.
39:10 So I can use a whole number like this.
39:12 I could also use a decimal number.
39:14 So I could say like 2.0987.
39:15 And Python is going to be just fine with that.
39:18 We'll be able to print that out under the screen as well.
39:22 I can also make numbers negative.
39:24 So I could make this 2.0987 a negative number.
39:28 And Python will be able to handle that just fine too.
39:32 And Python, we can also use basic arithmetic.
39:35 So I could do things like addition subtraction multiplication.
39:38 So I could say like three plus four.
39:41 And now this is going to print this out.
39:44 Or why don't we say like three plus 4.5.
39:46 And this will be able to print out the answer.
39:49 So we get 7.5.
39:50 And we can do addition, we can do subtraction,
39:53 we can do division, or we could do like multiplication as well.
39:56 So you can pretty much do whatever you want inside of Python.
39:59 And if you want to make more complex mathematical equations,
40:03 we can also use things to specify order of operations.
40:07 So I could use parentheses.
40:09 So for example, if I said three times four plus five,
40:13 what Python's going to do is it's going to multiply three and four together.
40:17 So it's going to be 12.
40:19 And then we'll add five to that.
40:22 So we should get 17.
40:23 But let's say that we wanted to change up the order, right?
40:26 I could actually put a parentheses around four and five.
40:29 And now it's going to add them first.
40:31 So it'll add four and five.
40:33 So we'll get nine, and we'll multiply that times three.
40:36 So we should end up with 27.
40:38 So you can see we can use those parentheses to specify order of operations.
40:42 So I could make like a very complex,
40:45 a little equation here just by using numbers and parentheses.
40:48 We can also use one other operation, which is called the modulus operator.
40:53 So I could say like 10, and use this percent sign, three.
40:57 And this is actually read 10 mod three.
41:00 And basically what this is going to do is it's
41:03 going to take the first number divided by the second number,
41:06 and it's going to spit out the remainder.
41:09 So it's basically going to say, okay, 10 divided by three,
41:12 that's going to be three with a remainder of one.
41:15 And so this is going to give us the remainder.
41:18 And you'll see down here, we get one,
41:20 because that's the remainder of 10 divided by three.
41:22 And that modulus operator can come in handy quite a bit.
41:26 We can also store these numbers inside of variables.
41:29 So we can store them inside of these variable containers.
41:33 So I could come over here,
41:35 and I could create a variable called like my underscore num.
41:39 And we'll just store a value of let's say like five in here.
41:42 And so I could come down here, and I can just print out my underscore num.
41:46 And we'll be able to print that number out to the screen just fine.
41:50 So in addition to doing things like a basic arithmetic,
41:54 we can also do some other stuff.
41:56 So one thing I could do is I could actually convert this number into a string.
42:00 So if I wanted to convert this number into a string, I can just say str,
42:04 and I can put the number that I want to convert inside of these parentheses.
42:09 And so what's going to happen now is
42:11 this number is going to be converted into a string.
42:14 So when I click the run button, you'll see it's just printing out five,
42:18 but now instead of being a number, this is actually a string.
42:21 And this is going to come in handy
42:24 when you want to print out numbers alongside strings.
42:26 So I can come over here and I can say like, my favorite number,
42:32 and we'll be able to print this number along with this string.
42:37 So I can click play.
42:38 And it says five my favorite number.
42:41 If I was to get rid of this string right here,
42:44 and I wasn't converting the number into a string,
42:47 now Python is going to have a problem with this.
42:49 It's not going to allow us to do that.
42:51 You can see we get an error.
42:53 So anytime you want to print out a number next to a string,
42:56 you got to make sure that you use that little string function.
42:59 So there's a bunch of other stuff that we can do with numbers.
43:03 And a lot of times in Python,
43:04 you're going to want to be using specific, like more advanced math operators.
43:09 So there's a bunch of different math functions that we can use on our numbers.
43:14 And a function is basically just like
43:16 a little collection of code that does something.
43:19 So a function could perform an operation
43:21 like a mathematical operation on our number.
43:24 It could also give us information about our number.
43:27 So I'm going to show you guys some of the most
43:30 common functions that you'll be using in Python related to numbers.
43:32 The first one is called ABS, and it stands for absolute value.
43:37 So we can get the absolute value of a number.
43:40 And so over here, I'm just going to make this variable negative five.
43:46 And basically, I can just say ABS,
43:48 and I can make an open and closed parentheses over here.
43:51 And this is going to give me the absolute value of this number up here.
43:55 So when I click the play button, you'll see it's just giving us five,
44:00 because five is the absolute value of negative five.
44:03 I can use a few other functions.
44:05 There's another one which is called pow.
44:08 It's so it's just POW.
44:09 And I'm actually not going to use this variable.
44:12 We just use a normal number.
44:13 And this function is going to allow us to pass it two pieces of information.
44:17 So I can give this function two pieces of information.
44:20 The first is going to be like a number.
44:22 And the second is going to be the power that I want to take that number to.
44:27 So I could say like three comma two.
44:30 And this is basically just going to be three raised to the power of two.
44:34 So it's just going to be three squared.
44:36 So we should get nine down here.
44:38 You can see that we do.
44:40 So I could pass in like, you know, some crazy number,
44:42 like we could pass in a four and we could raise it to the power of six.
44:47 And so we should get like a pretty big number here.
44:50 And you can see we get 4096.
44:52 So this pow function is really useful for taking numbers to specific powers.
44:56 So in addition to using this pow function,
44:59 we can also use another function which is called max.
45:02 So I can say max.
45:04 And what this is going to do is it's basically going
45:07 to return the larger of the two numbers that we pass into it.
45:10 So right now I'm giving this a four and a six.
45:14 And this should tell us which number is higher.
45:16 So over here, it's just printing out six because that's the bigger number.
45:20 I can also use another function called min.
45:22 And this is going to do the opposite.
45:24 So now instead of printing out the max number,
45:26 it's going to print out the smallest number.
45:28 And it's going to print out four down here,
45:30 as you can see, another cool function is called the round function.
45:34 And this is going to allow us to round a number.
45:37 So it's just going to follow like standard rounding rules.
45:40 So if I said like 3.2 inside of here,
45:43 now it's going to round it down to just normal three.
45:46 But if I said like 3.7, it's going to round it up to four.
45:51 So that'll allow you to round a number.
45:53 So there's a few other functions that I want to show you guys,
45:57 but in order to get access to them,
45:59 I'm actually going to have to do something called importing.
46:03 And in Python, we can actually import external code into our files.
46:07 And so if I want to access these specific math functions,
46:12 I have to import something called Python math.
46:16 So I can just say from up here, math, import, and this star.
46:20 And basically, what this is going to do is it's going to go out
46:24 and it's going to grab a bunch of different math functions that we can use.
46:28 So I already showed you guys a few of these different math functions,
46:31 but there's actually a bunch more.
46:33 And in order to access them,
46:35 we're going to need to include this line of code inside of our file.
46:38 So once we have that, now I can access a few others.
46:41 So there's another function which is called the floor method.
46:44 And it's just FLOR.
46:45 And what this will do is it'll basically just grab the lowest number.
46:49 So it's essentially just going to chop off this decimal point.
46:53 And you can see over here, now we get a three,
46:56 because we're using that floor function, there's also another one called seal.
47:00 And that's just going to do the exact opposite.
47:02 So that's just going to round the number up no matter what.
47:05 So no matter what, we'll always get four
47:07 with we have a three point whatever here.
47:09 And there's another one which is called square root.
47:12 So it's just SQRT.
47:14 And essentially, this is just going to return the square root of a number.
47:19 So I could say like 36.
47:21 And now we should get six back.
47:23 So those are awesome, very interesting functions.
47:26 And inside of this math module, so we would call this a module.
47:31 And you don't have to worry too much about what that is right now.
47:34 Just know that when we put this line of code into our program,
47:38 it gives us access to a lot more math function.
47:41 So it basically allows us to do a lot
47:44 more things with math inside of our program.
47:46 And there's a bunch of different math functions in Python.
47:49 And if you want, you can basically just
47:51 go online and search for different math functions.
47:53 I showed you guys a few here.
47:55 And I would say the ones that we
47:57 looked at here are probably the most commonly used, but there's a bunch more.
48:02 And like I said, you know, there's tons of documentation on all this stuff.
48:05 So you can just look up, you know, math functions in Python.
48:08 And there's going to be tons and tons like lists of these things
48:12 that you can use inside of your programs to perform different math operations.
48:16 But for now, that's just been a little
48:18 bit about using numbers a little bit about doing,
48:20 you know, certain math operations and using functions with those numbers.
48:25 In this tutorial, I'm going to show you how to get input from a user.
48:32 So we're basically going to allow a user to input information into our program.
48:38 And I'm actually going to take the information that the user inputs,
48:42 I'm going to store it inside of a variable.
48:44 And then we're going to be able to do something with that variable.
48:48 So I want to show you guys how
48:50 we can actually go about getting input from someone.
48:52 And down here in my Python file,
48:55 all I have to do to get input from someone is I just have to type out input,
49:01 and then an open and closed parentheses.
49:03 And what this is basically going to do is it's going to tell Python like, hey,
49:07 we want to get input from a user and Python
49:10 will allow the user to type in some information.
49:13 And inside of these parentheses, we can actually type a prompt.
49:17 And so generally, when we want the user to enter in some information,
49:21 we want to tell them what we want them to enter.
49:24 And that's what we can put inside of here.
49:26 So I'm just going to say, enter your name.
49:30 So we're actually going to create a little Python app,
49:33 which is going to allow the user to enter in their name.
49:36 And then we'll store the name inside of a variable,
49:39 and then we'll basically just say hi to the user.
49:41 So we'll say like, hi, and then whatever the name they entered was.
49:45 So what I want to do is I actually want to store
49:48 whatever the user inputted into our program inside of a variable.
49:53 So I can actually go over here,
49:55 and I can create a variable, I could just call it name.
49:59 And I could set this variable equal to input.
50:02 So what I'm doing now is I'm taking the value that the user inputs,
50:07 and I'm storing it inside of this variable container called name.
50:12 And so now I have access to whatever they entered in.
50:17 So what we can do is we can say print.
50:20 And inside of the parentheses, I can just print out hello.
50:24 And then I'm just going to add on to this name.
50:28 And actually, why don't we put an exclamation point?
50:31 So essentially, we're getting a name from the user,
50:34 we're storing it inside of the name variable.
50:37 And I'm printing it out onto the screen.
50:39 So I'm going to go ahead and play this.
50:42 And when we run the program, you'll notice down here,
50:45 Python is giving us this little prompt.
50:47 So it's saying enter your name.
50:49 Now in here, I can actually type in a value.
50:52 So I can type in Mike.
50:54 And then I can just hit enter.
50:57 And Python's going to go ahead store this value inside of that variable.
51:01 And then it's going to print out hello, Mike.
51:03 And so this is kind of cool.
51:05 And actually, what we could do is we could prompt
51:08 the user to enter in more than one piece of information.
51:11 So I could actually copy this line of code.
51:14 And I'm going to paste it below here.
51:17 And why don't we have the user enter in another variable?
51:20 So we could have them enter in, for example, their age.
51:24 And over here, we just want to say enter your age.
51:27 And so now we're able to get two pieces of information from the user.
51:31 And I could say hello, name.
51:34 And then I can say, you are, and I'll print out their age.
51:38 So I'm just going to print age.
51:40 So now I'm basically able to get both
51:43 of these pieces of information from the user, I can get their name.
51:47 And I can get their age.
51:48 So when we run this program, I can type in both of those pieces of information.
51:54 So to say, answer your name.
51:56 So we could just make up a name like john,
51:58 and then enter the age, let's say john is 25.
52:01 And now it's going to say, Hello, john, you are 25.
52:04 So the program is able to get input from the user.
52:08 And then it's able to use that information in order to say
52:11 hi to the user and tell them how old they are.
52:15 And so getting input from the user is
52:17 a great way to make your programs more interactive.
52:20 And here, we're just scratching the surface, right?
52:22 We're just basically getting to, you know, a piece of information from the user,
52:26 but you can use these inputs to do awesome things in Python.
52:29 And as we go through this course,
52:31 you're going to see some really cool things that we can do with these inputs.
52:39 In this tutorial, I'm going to show you
52:42 how to build a very basic calculator in Python,
52:45 we're basically going to build a calculator
52:47 where we'll get two numbers from a user,
52:49 and then we'll add those numbers together
52:51 and print the answer out onto the screen.
52:53 So let's look at some practice in getting input from users.
52:56 And we'll also talk about getting numbers from users as opposed to just strings.
53:01 So I'm really excited to get started on this calculator.
53:05 The first thing we want to do is
53:07 create two variables and inside of those variables,
53:10 we want to store the two numbers that the user wants to add together.
53:14 So down here, I'm just going to say, num one.
53:18 And that's what we'll call our first variable is equal to.
53:21 And now I need to get input from the user.
53:24 So I'm going to say input.
53:26 And inside these parentheses, I'm going to say, enter a number.
53:30 And now I basically want to do the same exact thing, but for the second number.
53:38 So we'll paste this guy down here.
53:41 And I'm just going to say num two.
53:44 And then we'll just say, enter another number.
53:46 So now we're able to store the user's input inside of these two variables.
53:52 So now all I want to do is just print out the result.
53:56 So I'm actually going to create a variable called result.
53:59 So I'm just going to say result is equal to, and basically,
54:02 we just want to add these two numbers together.
54:04 So I'm going to say num one plus num two.
54:07 And now all we have to do is just print out the result.
54:12 So I'm just going to print result.
54:14 So we basically have our entire program written out here.
54:17 And this will allow us to enter in two numbers,
54:19 and then we'll add them together and print out the answer.
54:23 So I'm going to click play.
54:25 And down here, we can enter our number.
54:29 So I'm going to enter the first number, we'll say five.
54:31 And I'm going to enter my second number.
54:34 So let's say 8.0, or let's say 8.3.
54:37 So now we should print out the answer.
54:41 But if you look over here,
54:44 that's not quite the answer we were looking for, right,
54:49 five plus 8.3 is definitely not 58.3.
54:52 And here's what happened.
54:54 When we get input from a user by default,
54:58 Python is just going to convert it into a string.
55:02 So it doesn't matter what we put down there when we're entering the numbers,
55:06 Python's always just going to be like, okay, this is a string, that's it.
55:10 Sounds good, right?
55:11 It's just going to make it a string.
55:14 In order for us to make these num one and num two variables numbers,
55:18 we're actually going to have to convert the strings
55:21 that we get from the user into numbers.
55:23 And so the way that we can do that is by basically just coming over here.
55:28 And when we add the two numbers together, I can use a special Python function
55:33 in order to convert those strings into numbers.
55:37 And there's actually two Python functions that we could use for this.
55:41 The first is called int.
55:43 So I could say int, I can make
55:46 an open parentheses and I can make a closed parentheses.
55:49 And basically what this is going to do is it's going to convert
55:52 whatever is inside of these open and closed parentheses into an integer number.
55:56 And an integer number is basically a whole number.
56:00 So it's like one, two, three, four, five.
56:03 So it's a, it's a whole number.
56:04 In other words, it can't have a decimal point.
56:07 And so what I could do is I could say,
56:11 int, and I could put these inside of parentheses.
56:15 And now what this is going to do is
56:17 it's going to convert both of these numbers into integers.
56:20 In other words, into whole numbers that don't have decimals.
56:23 And it's going to add them together and we'll print out the results.
56:26 So why don't we do that?
56:28 And we'll just see if this works.
56:29 So I'm going to click the play button.
56:31 And down here, we'll enter our first number.
56:33 So I'm just going to enter in a four.
56:35 And then I'll enter in a five.
56:37 And now you can see it's printing out the correct answer four plus five.
56:42 And so that's essentially our basic calculator.
56:45 But we do have one problem with this calculator.
56:47 So I'm going to run it again.
56:48 And I'll show you guys with that problem is
56:51 if I wanted to add in a decimal number, so we'll add 4.3 and 5.5.
56:56 You'll see that we're getting this error.
57:00 And that's because I'm using this int function up here.
57:04 And this int function is looking for a whole number.
57:07 So if I put decimal numbers inside of there, it's going to break the program.
57:12 There's another function we can use instead of an int, we can say float.
57:16 And a float is basically just a number that has decimal.
57:20 So it's a decimal number.
57:21 So I can convert both of these guys into floats.
57:24 And now I'll be able to use decimal numbers.
57:27 So I can click play.
57:28 And I can just enter in some numbers.
57:30 So I could say like four.
57:32 And I could also say like 5.5.
57:34 And now it's going to be able to add both of those numbers,
57:38 even if one of them is a decimal.
57:40 And that's the beauty of using that little float function.
57:43 So in certain circumstances, you might want the user to only be able
57:46 to enter in an integer to a whole number.
57:49 But in a lot of cases, like in this calculator,
57:51 we want them to we want them to be able to enter any number that they want.
57:55 So we're going to use this float function.
57:57 So I'm just going to recap what we did one more time,
58:00 we created two variables, num one and num two.
58:03 And we basically stored the two numbers
58:05 that the users input into those variables.
58:07 And then we converted them into numbers.
58:10 And we added them together and we printed out the results.
58:14 So this is a pretty cool calculator.
58:16 I'm happy with it.
58:17 Hopefully you guys learned something.
58:18 And as we go forward in this Python course,
58:20 you're going to learn all sorts of ways
58:23 that we can make this calculator more complex.
58:26 In this tutorial, I want to show you guys how to build a madlibs game in Python.
58:34 So a madlibs game is basically just a game
58:37 where you can enter in a bunch of random words.
58:41 So like, you know, verbs, nouns, names, colors.
58:45 And you basically take all of those words and put them into a story randomly.
58:50 So over here, my web browser, I just have an example of a madlibs.
58:55 So, you know, down here, we're basically just taking random words,
58:57 like an adjective or a noun or a plural noun,
59:00 and entering them in side of a story.
59:02 So like you read through the story and then like
59:04 some random word pops up and madlibs can be pretty fun.
59:07 And I'm sure you guys have, you know, at least heard of madlibs before.
59:10 But if not, that was just a little introduction.
59:12 So I want to create a madlibs in Python.
59:14 And this is going to be really cool.
59:16 So down here, I have this like little poem,
59:19 which just says, roses are red, violets are blue, I love you.
59:22 And so, you know, this is kind of like a classic poem.
59:25 But I think we should mix it up a little bit and create a madlib for this poem.
59:31 And so instead of having this red color over here,
59:34 I want to have the user enter in their own color.
59:38 Instead of having violets, why don't we have them enter in a plural noun?
59:43 And instead of saying I love you,
59:45 why don't we have them say I love and then some celebrity?
59:50 So celebrity.
59:51 So this is the basic structure for our little madlib, right?
59:55 I want to basically have the user enter in a color,
59:59 enter in a plural noun, and enter in a celebrity.
1:00:02 And then I'll take all of those words and put them inside of our madlib.
1:00:06 And hopefully we end up with something that's pretty funny.
1:00:09 So how can we actually go about building this in Python?
1:00:12 Well, the first thing we're going to have to do is we're going
1:00:15 to have to be able to get these words from the user, right?
1:00:18 That's pretty obvious.
1:00:19 So I think what we should do is we
1:00:21 should create three variables called color plural noun and celebrity.
1:00:25 And we'll store the user's inputs inside of those variables.
1:00:29 So up here, let's create those variables.
1:00:32 The first variable I want to create is going to be called color.
1:00:36 So say color.
1:00:36 And I want to set this equal to the input.
1:00:39 So I'm going to say input.
1:00:40 And inside of here, I'm just going to give them a prompt.
1:00:43 So I'll say enter a color.
1:00:45 And we can basically just copy this line of code.
1:00:49 And I'm going to paste it down below.
1:00:51 And we'll paste one more time.
1:00:53 So now we're going to create our plural noun variable.
1:00:57 So I'll say PL, URL, plural noun.
1:01:01 And we'll just say enter a plural noun.
1:01:05 And actually, over here, I want to make a colon as well.
1:01:09 So then finally, we'll create our celebrity
1:01:12 variable and we'll say enter a celebrity.
1:01:15 And so now basically,
1:01:17 we're creating three variables called color plural noun and celebrity.
1:01:21 And inside of them, we're storing the color that the user enters,
1:01:26 the plural noun, the user enters and the celebrity, the user enters.
1:01:29 So last thing we have to do is replace these guys with those variables.
1:01:34 So I'm basically just going to add in the color over here.
1:01:40 And we're going to add in the plural noun.
1:01:43 So I can just go like this.
1:01:45 And finally, we'll just add in that celebrity.
1:01:50 So now we have a pretty cool mad lib.
1:01:57 And it should be functional.
1:01:59 Everything should work.
1:02:00 It looks like it's good to go.
1:02:02 So let's come over here and we'll run our program.
1:02:06 And then down here in the console, we can play our little mad libs game.
1:02:11 So let's say enter a color, I'm just going to say magenta, enter a plural noun.
1:02:16 Why don't we say microwaves and enter a celebrity, let's say Tom Hanks.
1:02:23 So when I click enter,
1:02:25 what should happen is we should get our mad libs printed out.
1:02:29 So I'm going to click enter.
1:02:30 And now we have our mad libs.
1:02:32 So it says roses or magenta, microwaves are blue.
1:02:35 I love Tom Hanks.
1:02:36 So it's a pretty good mad lib.
1:02:39 And obviously, you know, the more the more print statements we put over here,
1:02:43 you know, the better the mad lib could could be.
1:02:45 And we could store as many, you know, variables up here as we wanted.
1:02:48 This was just a very simple mad lib.
1:02:51 We only took in three inputs, but you know,
1:02:53 you can see how you could take something
1:02:55 like this and create a little mad libs game.
1:02:58 So your homework for this lesson is to go off and make your own mad libs game.
1:03:02 Hopefully one that's a little bit funnier than mine.
1:03:04 But that's the basics of creating a mad libs game in Python.
1:03:13 In this tutorial, I want to talk to you guys about working with lists in Python.
1:03:17 A lot of times when you're programming in Python,
1:03:20 you're going to be dealing with large amounts of data.
1:03:23 And when you're dealing with large amounts of data,
1:03:25 you want to make sure that you can manage it and organize it properly.
1:03:28 And the list is essentially just a structure that we
1:03:32 can use inside of Python to store lists of information.
1:03:35 So we can take a bunch of different data values, we can put them inside a list,
1:03:40 and it allows us to organize them and keep track of them a lot easier.
1:03:44 So generally, you would create a Python list,
1:03:46 and you would put a bunch of related values inside of that list,
1:03:50 and then you can use it throughout your program.
1:03:52 So I'm going to show you guys just the basics of using lists,
1:03:55 we're going to look at some of the common use cases and really
1:03:58 just get you up to speed with what lists are and why they're useful.
1:04:02 So over here in my program, I'm actually going to create a list.
1:04:05 And we create a list a lot like we create a Python,
1:04:09 just like a normal Python variable.
1:04:10 So the first thing we want to do when we're making a list is give it a name.
1:04:15 So we want to give it a descriptive name,
1:04:17 something that's going to basically describe what's inside the list.
1:04:21 In my case, for this example, let's make a list of like friends.
1:04:25 So we could say like here's a list of all my friends.
1:04:28 So I can say friends.
1:04:30 And I'm going to set this equal to the list of values that represent my friends.
1:04:34 And we want to create a list, we can use these open and closed square brackets.
1:04:40 So whenever you use these open and closed square brackets,
1:04:43 Python basically knows,
1:04:44 okay, they want to store a bunch of values inside of this list.
1:04:48 So why don't we go ahead and start adding some values?
1:04:50 So we can say like my friends are like Kevin, Karen, and let's say Jim.
1:04:58 So here I have three values inside of my friends array.
1:05:05 So when we're creating a normal variable in Python,
1:05:09 generally, we just give it one value, right?
1:05:11 So I would create a variable, I would give it one value,
1:05:14 maybe a string or a number or a boolean.
1:05:16 But when we make lists, we're able to store multiple values inside of the same
1:05:22 like little item inside of the same object, right?
1:05:25 And then what I can do is I
1:05:28 can access these individual items inside of my program.
1:05:31 So inside of this friends list,
1:05:33 we're basically storing all of these different values,
1:05:36 and that can be really useful.
1:05:38 So now that we've stored some values,
1:05:40 let's talk about what you can put inside of a list.
1:05:43 So you can really put any piece of information in Python,
1:05:46 we could put things like strings, numbers, or booleans.
1:05:48 So if I wanted, instead of just putting strings here,
1:05:51 I could change like one of these to a number,
1:05:54 and I could change one of these to a boolean,
1:05:56 and it wouldn't matter, Python is going to be fine with that.
1:05:59 But let's change these back to strings just for the sake of the example.
1:06:03 So now that we've created our list,
1:06:06 how can we access individual elements inside of this list?
1:06:09 It's a good question.
1:06:10 I'm gonna go ahead and print something out to the screen.
1:06:13 The first thing we can do is we can just print out the actual list.
1:06:16 So I can just say print friends.
1:06:18 And then when I run this program, if we look down here in the console,
1:06:22 you can see it's just printing out the actual list of friends, right?
1:06:26 But let's say that I wanted to print out a specific element inside of this list.
1:06:31 Well, I can actually refer to elements by their index.
1:06:34 So each one of the elements inside of this list has a particular index.
1:06:39 And the index is actually start at zero.
1:06:42 So we would say that this first element in the list, Kevin has an index of zero.
1:06:47 We would say that this other element Karen has an index of one.
1:06:51 And this third element Jim would have an index of two.
1:06:55 So even though Jim is technically the third element inside of the list,
1:07:00 it has an index of two.
1:07:02 And that's because we start indexing index position one.
1:07:06 And so that's just kind of how lists work.
1:07:09 And that's how list indexes work.
1:07:11 So if I wanted to access a specific element inside of my friends list,
1:07:15 I can just refer to it by its index.
1:07:17 So over here after we type out friends,
1:07:20 I'm just gonna type an open and closed square bracket.
1:07:23 And inside of this open and closed square bracket,
1:07:25 I want to put the index of the element inside the list that I want to access.
1:07:30 So if I wanted to access this guy right here,
1:07:32 this element, I can just put a zero.
1:07:34 So I can say friends zero.
1:07:36 And now when I run my program,
1:07:38 it's just gonna print out that value, it's just gonna print out Kevin.
1:07:42 If I wanted to access this Jim value over here, I can put a two.
1:07:46 And that will give us that Jim value, you can see right there.
1:07:51 So we can access elements based off of their index,
1:07:54 we can also access elements based off of their index from the back of the list.
1:08:00 So for example, if I said negative one inside of here,
1:08:03 this is gonna give me access to this value right here for Jim.
1:08:07 And you can see that sort of does so when we use negatives,
1:08:10 it starts indexing from the back of the list.
1:08:12 So if I said negative two, this is gonna give us Karen.
1:08:15 And so you can use the negatives to access items at the back of the list.
1:08:19 Just keep in mind though, and this is kind of interesting.
1:08:22 We would say this first element here is index position zero, right?
1:08:28 But we would say the first element at the back
1:08:31 of the list is index position negative one.
1:08:33 So just keep that in mind.
1:08:35 So there's actually other ways that we
1:08:37 can access like just portions of this list.
1:08:39 So for example, let's say I just wanted
1:08:41 to select the last two elements in this list.
1:08:43 And I didn't want to select the first one.
1:08:46 Well, in here, I can just say one with a colon.
1:08:49 And this will grab the element at index
1:08:51 position one and all of the elements after that.
1:08:54 So now we should just grab Jim and Karen or Karen and Jim in that order.
1:08:59 I could also specify a range.
1:09:01 So actually, let me add in a couple
1:09:04 other elements just so we can illustrate this better.
1:09:06 So I'll add in two more names.
1:09:08 So if I wanted to, I could select like the element
1:09:11 at index position one up to the element at index position three.
1:09:15 So it's going to grab this one.
1:09:18 So that's one, two.
1:09:19 And it's actually not going to grab the third one.
1:09:22 So it's it's going to grab all the elements up to but not including three.
1:09:25 So it'll grab Karen and Jim.
1:09:27 So let's go ahead and run this.
1:09:29 And you can see it grabs Karen and Jim.
1:09:32 So you can either just put like one with a colon after it,
1:09:35 and then it'll grab that index position and everything after it,
1:09:38 or you can specify like a range that you want to grab from.
1:09:41 So that's how we can access elements inside of this list.
1:09:45 But we can also modify elements.
1:09:47 So for example, I could come down here,
1:09:50 let's say I want to modify this value right here, Karen.
1:09:53 Well, all I have to do is just access that element.
1:09:56 So I could say friends and Karen is at index position one.
1:09:59 And I can change this to whatever I want.
1:10:02 So I could say friends index position one is equal to Mike.
1:10:05 And now when we print out friends index position one,
1:10:09 that value is going to be updated.
1:10:11 So it's just going to say Mike instead of saying Karen.
1:10:14 So it can be really useful to modify values inside of arrays.
1:10:18 So it can be really useful to modify values inside of Python lists.
1:10:23 So those are kind of the basics of working with lists in Python.
1:10:26 And actually in the next tutorial,
1:10:27 I'm going to show you guys even more ways that we can use these lists,
1:10:31 we're going to learn how to like add elements to a list,
1:10:34 delete elements from a list, copy lists,
1:10:35 and a bunch of cool stuff with these lists.
1:10:39 But for now, this has just been sort of like the basics of working with lists.
1:10:47 In this tutorial, I want to talk to you
1:10:50 guys about using functions with lists in Python.
1:10:53 And Python, a list is one of the most
1:10:56 important structures where we can store our information.
1:10:58 A list basically allows you to take a bunch of different values
1:11:02 and organize them and store them inside of their own little list structure.
1:11:06 So down here in my program, you can see I have two lists.
1:11:10 I have a list of lucky numbers.
1:11:11 And this is just a bunch of different numbers.
1:11:13 And then I have a list of friends.
1:11:15 So this could be like a list of somebody's friends.
1:11:17 And it's basically just a bunch of strings with names in them.
1:11:21 So these two lists are storing a bunch of different values.
1:11:25 And what makes lists awesome is not only could I store like this many values,
1:11:28 I mean, I could store hundreds or thousands or millions of values.
1:11:31 And when you're working with a lot of data and a lot of information in Python,
1:11:36 these lists are invaluable.
1:11:38 So because lists are so important,
1:11:40 there's actually a bunch of different functions that we can use with lists.
1:11:44 And those functions allow us to do things like
1:11:47 modify the lists and get information about the list.
1:11:50 So in this tutorial, I just want to show you a bunch of those functions,
1:11:54 we're going to talk about some of the most
1:11:55 common and the most popular functions that you
1:11:58 would use on lists to make them more powerful and make them more easy to use.
1:12:02 So the first thing we can do is obviously just print out a list.
1:12:06 So down here, I'm printing out this friends list.
1:12:09 And when I want to run my program, you see down here,
1:12:13 we're just printing out all the elements in the list.
1:12:15 So it's always good to be able to print out all the elements inside of a list.
1:12:19 But there's a lot of other functions that we can use.
1:12:22 So one function is called the extend function.
1:12:24 And the extend function will basically allow you to take
1:12:28 a list and append another list onto the end of it.
1:12:32 So I could say like friends dot extend.
1:12:35 And inside of these parentheses,
1:12:36 I can pass in the list that I want to add on to the friends list.
1:12:41 So I could say like lucky numbers.
1:12:43 And now when I print out this friends list,
1:12:47 not only is it going to have all the elements in the friends list,
1:12:49 it's also going to have all the elements inside the lucky numbers list.
1:12:52 You can see down here, we have all of our friends.
1:12:55 And then we also have the numbers over here.
1:12:58 So that's a really easy way to essentially just like, add two lists together.
1:13:02 We can also just add individual elements onto a list.
1:13:05 So I could come down here and I could say friends dot append.
1:13:10 And this will actually allow me to append
1:13:14 another item onto the end of this list.
1:13:16 So over here, I could just add another name.
1:13:19 So we could say creed, for example,
1:13:20 and now I'm like adding another friend onto the end of this list.
1:13:24 So when I run the program,
1:13:26 in addition to having the original items that were in the list up here,
1:13:30 we also have this additional item at the end,
1:13:33 and that append function is always going to add
1:13:35 the item onto the end of the list.
1:13:37 But let's say that you wanted to add an item into the middle of the list.
1:13:41 So like right here or right here, there's another list function called insert.
1:13:46 And insert is going to take two parameters.
1:13:49 The first parameter is going to be the index where you want to insert the item.
1:13:54 So why don't we insert it here at index position one?
1:13:57 I'm just going to say one.
1:13:58 And then I'm going to type in the name of the element that I want to add.
1:14:03 So we can just say like Kelly.
1:14:05 And now at index position one, we're going to have the value Kelly,
1:14:08 and all of these other values are going to get pushed up,
1:14:12 they're going to get pushed to the right one index position.
1:14:16 So now I run the program, you can see we have Kevin, this new element Kelly,
1:14:21 and then all the other elements have been pushed off to the right,
1:14:24 we can also remove elements.
1:14:26 So for example, let's say I wanted to remove this gym value,
1:14:30 all I have to do is just say friends dot remove.
1:14:33 And I can just type in whatever element I want to remove.
1:14:37 So I want to remove this gym element.
1:14:39 And so now when we run the program, Jim is going to be gone from the list,
1:14:44 and we just have these four guys left,
1:14:47 you can also remove all of the elements from the list.
1:14:50 So if I just wanted to completely like reset the list and get rid of everything,
1:14:54 I could just say friends dot clear.
1:14:56 And this is going to give us an empty list.
1:14:59 So it'll get rid of every single element inside the list.
1:15:02 There's also another really cool method, which is called pop.
1:15:05 And you can just type out pop like that.
1:15:07 And basically what pops going to do is it's
1:15:09 going to pop an item off of this list.
1:15:12 So when I click the play button,
1:15:14 you'll see here that we're left with Kevin, Karen, Jim, and Oscar.
1:15:18 So we basically got rid of the last element inside the list.
1:15:23 And that's what pop does, it pops an element off of the list.
1:15:26 So it basically removes the last element in the list.
1:15:29 So we have our list, but let's say that we wanted to figure
1:15:32 out if a certain element was in this list, right, if a certain value is in here,
1:15:36 so let's say I wanted to check to see if my friend Kevin was in this list,
1:15:42 well, I could say friends dot index.
1:15:44 And in here, I can just pass a value.
1:15:48 So I could say like, Kevin, and this is going to tell me the index of Kevin.
1:15:54 So it'll tell me if Kevin's in the list.
1:15:56 So now when I click play, you can see down here, it says zero.
1:15:59 If I was to say like Oscar in here,
1:16:02 for example, now it'll give me the index of Oscar.
1:16:05 So that's it three.
1:16:07 But if I put a name that wasn't in here,
1:16:09 so like, if I put Mike, that's not in the list.
1:16:12 And so now we're going to get back in air,
1:16:15 because it says Mike is not in the list.
1:16:18 So that's an easy way to figure out if a specific element is inside of the list.
1:16:22 And if it is, then you know what index it's at, you
1:16:25 can also count the number of similar elements in the list.
1:16:28 So I'm actually going to come down here,
1:16:31 copy this gym element, and we'll add another gym element into here.
1:16:35 So I could come down here and I could say friends dot count.
1:16:40 And we'll pass in gym.
1:16:41 And this will tell me how many times the value gym shows up inside of this list.
1:16:47 So it should say two, which it does down there,
1:16:51 because we have two values with the text gym.
1:16:54 And so we have all these values, we could also sort this list.
1:16:58 So I could come over here and I could say friends dot sort.
1:17:02 And what this will do is it'll sort the list in ascending order.
1:17:06 So we have a bunch of text here, and you'll see what it does.
1:17:11 Basically, what it's going to do is it's going to put it in alphabetical order.
1:17:14 So you can see we have J K O and T.
1:17:17 So the list went from being just random order,
1:17:20 like whatever we had up here to now it's in alphabetical order.
1:17:23 And that's also going to work for lucky numbers.
1:17:26 So I could say like lucky numbers dot sort,
1:17:29 and then we'll print out lucky numbers.
1:17:32 And these should now be in ascending order,
1:17:35 which I'm realizing they already are up here.
1:17:38 But if I was to take this 42 and put it down here,
1:17:42 now it will figure everything out and put it in ascending order.
1:17:47 Anyway, we can also reverse a list.
1:17:49 So if I wanted, I could reverse this lucky numbers list,
1:17:53 I could say lucky numbers dot reverse.
1:17:55 And now when we print it out,
1:17:58 instead of being 4815 1623, it's going to be 2316 15 842.
1:18:03 So it's basically just reverse the order of the list.
1:18:06 And finally, there's one more list function I
1:18:09 want to show you guys which is copy.
1:18:12 So I could actually create another list, and I could make it as a copy.
1:18:17 So I could say like,
1:18:19 friends to, and we could set this equal to friends dot copy.
1:18:23 And now friends to is going to have all the same attributes as friends.
1:18:30 Because it's basically just like copied it
1:18:33 basically took a copy of that friends list.
1:18:35 So you can see it's the same exact thing and copying
1:18:38 lists can come in handy in a lot of different situations.
1:18:40 So those are just some basic list functions and just to kind of like,
1:18:44 you know, get your feet wet and working with lists.
1:18:47 Obviously, like the more you work with them,
1:18:49 the more you practice using these different functions,
1:18:51 the more comfortable you're going to get.
1:18:52 But that's just kind of like a basic overview of what you can do.
1:18:55 In this tutorial, I want to talk to you guys about tuples in Python.
1:19:03 Now a tuple is a type of data structure,
1:19:08 which basically means it's a container where we can store different values.
1:19:13 Now if you're familiar with lists in Python,
1:19:16 a tuple is actually very similar to a list.
1:19:19 It's basically a structure where we can store multiple pieces of information.
1:19:24 But a tuple has a few key differences from lists
1:19:29 that we're going to talk about in this tutorial.
1:19:31 First thing I want to do is I want to show you the how to create a tuple.
1:19:35 So I can come down here and I can create a tuple.
1:19:38 And one of the most common examples of tuples is coordinates.
1:19:42 So let's say I had a series of like XY coordinates,
1:19:46 I could create a variable called coordinates.
1:19:48 And I'm just going to set this equal to an open and closed parentheses.
1:19:53 And whenever we want to create a tuple inside of Python,
1:19:57 we're going to use these open and closed parentheses.
1:20:00 And inside of here inside of these parentheses,
1:20:02 we're going to put the values that we want to store.
1:20:06 So I could, for example, put a list of numbers.
1:20:09 So this could be like my coordinates, I could have like four and five.
1:20:14 So this would be my tuple with coordinates inside of it.
1:20:18 And one of the things about tuples
1:20:21 that makes them unique is a tuple is immutable.
1:20:24 And you'll hear that word a lot when we're talking about tuples immutable,
1:20:27 it basically means that the tuple can't be changed or modified.
1:20:31 Once we create our tuple up here, you cannot modify it,
1:20:35 you cannot change it, you can't add elements to it,
1:20:38 you can't erase elements from it,
1:20:40 you can't change any of the elements inside the tuple.
1:20:43 What you see is what you get, once you create the tuple,
1:20:46 it's, it's as it is, it can't change.
1:20:48 So I could come down here and I can
1:20:50 print out some of the attributes inside the tuple.
1:20:54 And the way I can access them is just by using an open enclosed square bracket.
1:21:00 And in here, I can pass in a index.
1:21:04 So tuples are indexed starting at zero.
1:21:07 So technically, we would say that four is at index position one and sorry,
1:21:12 four is at index position zero and five is at index position one.
1:21:16 So if I said coordinate zero, now it should print out four.
1:21:21 And down here, we're printing out four.
1:21:23 So if I wanted to print out that five,
1:21:27 we can just put a one inside of those square brackets.
1:21:29 And now we'll print out that five.
1:21:31 So I can access the elements inside of a tuple,
1:21:34 I can easily access each, each entry.
1:21:37 But if I was to come over here and try to change one of the elements,
1:21:43 so if I said coordinates, and coordinates one,
1:21:46 and I tried to set it equal to something else like 10,
1:21:50 Python's actually going to give us an error when we run this program.
1:21:53 So I'm going to run the program, you see we get this error, it says,
1:21:57 type error tuple object does not support item assignment,
1:22:02 because it's immutable, because we can't change it.
1:22:06 So that's basically what tuples are how we can create them.
1:22:10 And you know, a tuple, like I said, it's very simple,
1:22:13 you just create it, and then you have it.
1:22:15 And now I want to talk to you guys about a common topic in Python,
1:22:19 which is the difference between tuples and lists.
1:22:23 So remember, a list is basically doing the same thing a list.
1:22:25 If we wanted to create a list,
1:22:27 we would just use square brackets instead of those parentheses.
1:22:30 And if I was using a list, I could assign different values to it,
1:22:35 I could I could basically mutate any of the elements I could add delete,
1:22:40 modify change, whatever I wanted with the list.
1:22:43 But with a tuple, we can't do that.
1:22:45 And that's sort of the basic difference.
1:22:48 And really, Python doesn't say anything more than that.
1:22:51 It basically just says like one of them's immutable, one of them's not.
1:22:55 But in practical use cases, generally,
1:22:57 people will use tuples for data that's never going to change.
1:23:01 So you'll you'll store data inside of a tuple,
1:23:04 when that data doesn't need to be mutated.
1:23:07 That's why something like coordinates is such a good candidate for a tuple,
1:23:12 because once you have coordinates,
1:23:13 like, we're probably not going to modify them,
1:23:15 right, we might add other coordinates.
1:23:17 So for example, if I wanted, I could create a list of tuples.
1:23:22 So I can say like 4567, maybe we have some other coordinates, like 8034.
1:23:28 So here I have, you know, I have a list and inside of it, we have these tuples.
1:23:36 But remember, these tuples can't be modified, the values can't be changed.
1:23:40 So really, it's just a case by case basis, I would say by a huge majority,
1:23:45 you're going to be using lists most of the time.
1:23:48 Tuples are more of a niche, you know, they're using more special situations.
1:23:54 But there are plenty situations where you want to use tuples.
1:23:56 So if you're in a situation where you want
1:23:58 to store data that can't be changed or mutated, then tuples are the way to go.
1:24:03 So yeah, that's just some basics about tuples,
1:24:05 some basics about the difference between lists and tuples.
1:24:08 And so a tuple is a great thing to add into your Python repertoire.
1:24:14 In this tutorial, I want to talk to you guys about using functions in Python.
1:24:22 Now a function is basically just a collection of code,
1:24:27 which performs a specific task.
1:24:28 So I can take a bunch of lines of code that are basically doing one thing,
1:24:33 I can put them inside of a function.
1:24:35 And then when I wanted to do that task
1:24:38 or do that one thing that the function was doing, I can just call the function.
1:24:43 And so functions really help you to organize your code a lot better,
1:24:47 they allow you to kind of break up your code into different,
1:24:50 you know, little chunks that are doing different things.
1:24:53 And they're really just awesome.
1:24:54 So functions are like a very core
1:24:56 concept when we're talking about programming in Python.
1:24:59 So I'm going to show you guys how to create an awesome function today.
1:25:03 Let's say for the purposes of this tutorial,
1:25:05 we want to create a function that says hi to the user.
1:25:09 So the one task that our function performs is
1:25:12 basically just saying hi to whoever is writing the program.
1:25:15 So over here, if I want to write a function,
1:25:19 the first thing I have to use is a keyword in Python, it's called death.
1:25:23 So D E F.
1:25:25 And basically, when Python sees this keyword, it's going to say,
1:25:29 okay, this person wants to use a function.
1:25:32 So after we type out death, we need to give this function a name.
1:25:37 So just like when we are creating variables, we give them descriptive names,
1:25:43 we also want to do the same thing with functions.
1:25:46 So if I'm creating a function, I can just give it a specific name,
1:25:50 which basically says like what it's doing.
1:25:51 So we're going to create a function that says hi.
1:25:54 So I'm just going to call this function, say hi, just like that.
1:25:59 And once we type out the name of the function,
1:26:02 and so also I could type out say hi with no
1:26:05 underscore or I can type out say hi with an underscore,
1:26:08 both are considered like good practices in Python.
1:26:11 A lot of times, if you just have a simple like
1:26:14 two word function like this, you don't need to put an underscore,
1:26:17 but we could put an underscore there.
1:26:19 If we wanted, after we type out the name of the function,
1:26:22 I'm just going to type an open and close parentheses.
1:26:25 And then I'm going to type a colon.
1:26:27 And basically, what this is doing is it's saying to Python, like, all right,
1:26:32 all the code that comes after this line is going to be inside of our function.
1:26:37 And in order to write code that's going to end up being inside the function,
1:26:42 we actually have to indent it.
1:26:44 So over here, you'll notice that when I clicked enter,
1:26:47 my text editor automatically indented the text.
1:26:50 So it's automatically like, using this indent here.
1:26:54 And that's kind of like one of the rules in Python is like,
1:26:58 the code that goes inside of this function needs to be indented.
1:27:01 So if I was to write some code, like out here,
1:27:05 this is no longer going to be considered inside the function.
1:27:08 So you can see, as I type out code, and obviously,
1:27:11 this isn't real code, but as I type text over here,
1:27:14 that's indented, this little like marker over here is basically saying like,
1:27:18 Oh yeah, that's inside the function.
1:27:20 But then when I write code over here,
1:27:23 that is like not at the same indentation level as this stuff,
1:27:27 it's no longer considering it inside the function.
1:27:29 So that's just a little thing.
1:27:31 Any code inside this function needs to be indented.
1:27:33 Alright, so our function is just going to say hi to the user.
1:27:37 So I'm just going to have it print out some text, it's going to say hello, user.
1:27:43 So this is a very simple function.
1:27:46 Obviously, we just have one line of code and inside of a function,
1:27:49 you could have, you know, as many lines of code as you want.
1:27:52 But for our purposes, we only need one line in order to perform our function.
1:27:57 So now all we have to do is call this function.
1:28:00 So if I want to execute the code inside of this function,
1:28:03 I have to do something called calling it.
1:28:06 So if I was to just run my program as it is right now,
1:28:10 I'm just going to run it, you'll see that nothing happens over here, right?
1:28:15 Even though this function is printing out hello user,
1:28:17 when I run the program, it's not doing it.
1:28:20 And that's because the code inside of a function
1:28:23 isn't going to get executed by default.
1:28:25 The code inside of a function is only going to get
1:28:29 executed when we specify that we want to execute it.
1:28:31 And in order to do that, we're going
1:28:33 to have to do something called calling the function.
1:28:35 So in order to call the function, you basically just type out the functions
1:28:39 name and those open and close parentheses.
1:28:41 So I'm just going to type out say hi, and open and close parentheses.
1:28:46 And now when we run this program again,
1:28:49 you'll see that it prints out hello user.
1:28:51 So we're executing the code inside of the function.
1:28:54 And I want to show you guys just one more thing here,
1:28:58 just talking to you guys about how these functions actually work.
1:29:01 So up here, I'm going to print top.
1:29:04 And then down here, I'm going to print bottom.
1:29:07 So I want to show you guys the flow of these functions inside the program.
1:29:11 So when I run this program,
1:29:14 you'll see we print out top hello user, and then bottom.
1:29:19 So essentially, what's happening is when
1:29:21 Python goes through and executes this program,
1:29:24 it goes over here to this first line, it says,
1:29:27 okay, we want to print out the word top.
1:29:29 And then it goes down here and it says,
1:29:32 okay, we want to execute the say hi function.
1:29:35 So Python actually jumps up, and it goes over to this say hi function,
1:29:40 and it's going to execute all of the code inside of this function.
1:29:44 So it's going to go through execute all this code.
1:29:46 And then once it's done executing all the code in the function,
1:29:50 it's going to jump back down here,
1:29:51 and it's going to move on to the next line, which is bottom.
1:29:54 So that's kind of like the flow of functions.
1:29:57 Again, with functions generally, when we're naming these functions,
1:30:01 you want them to be named in all lowercase.
1:30:05 And usually, when we're naming stuff in Python, if there's two or more words,
1:30:09 we're going to use an underspace or an underscore in between them.
1:30:13 So I could write this out as say underscore high.
1:30:16 But in a lot of situations, though,
1:30:17 if I have a function like this where the name is really short,
1:30:21 I might just be easier to leave it without an underscore,
1:30:23 but why don't we just put an underscore
1:30:24 in there just to be a super Python official.
1:30:27 All right, so now we can actually
1:30:30 make these functions a little bit more powerful.
1:30:33 And what we can do is we can give them information.
1:30:36 So a lot of times when we write a function,
1:30:39 we're going to want to have additional information that gets passed in.
1:30:43 And these are called parameters.
1:30:45 So a parameter is a piece of information that we give to the function.
1:30:51 So over here, I can actually specify
1:30:53 that this function needs to receive some parameters.
1:30:56 So I can basically say like, Hey, if you're going to call this function,
1:31:00 you need to give us some information, you need to give us some parameters.
1:31:03 And all I have to do to do that is just
1:31:05 type out the name of the parameter that I want to receive.
1:31:09 So why don't we allow the code calling this function
1:31:13 to tell it what name to say hi to up here, I can just say name.
1:31:17 And basically, what this means is it means whenever I call this say hi function,
1:31:21 we have to give it a name.
1:31:23 So down here, if I was to call this, I have to include a name in here.
1:31:28 So I can say like, Mike, and what we can do now is we can actually
1:31:32 access this parameter or this variable inside of our function.
1:31:36 So I could come over here and instead of saying hello user,
1:31:40 I could say hello name.
1:31:42 And basically, what this is going to do is it's just
1:31:45 going to say hello to whatever name got passed in here.
1:31:49 So I'm actually going to copy this and we will do this twice.
1:31:53 So I'll say hello, Mike hello, Steve.
1:31:56 And now when I run this program,
1:31:59 you'll see that instead of just saying hello user,
1:32:03 it's saying hello to whichever name I passed into the function.
1:32:06 So that's why this can be really useful, right?
1:32:09 We can give the function information,
1:32:10 and depending on the information we give it,
1:32:13 it'll perform its task a little bit differently.
1:32:15 I could also include more than one parameter.
1:32:18 So you can, I mean, technically,
1:32:20 you could have like as many parameters as you want.
1:32:22 So I can put another one in here, we can say age.
1:32:25 And now I'm going to have to pass in an age along with these.
1:32:29 So I'm just going to pass in age and pass in an age for down here.
1:32:34 And I'm just going to pass in strings.
1:32:37 So we can say hello name.
1:32:43 You are age.
1:32:44 So I'm passing in two pieces of information.
1:32:47 And now when we run this program, it's going to call the function.
1:32:51 And it's going to use both of those pieces of information.
1:32:54 So I say hello, Mike, you are 35.
1:32:57 Hello, Steve, you are 70.
1:32:59 So essentially, we're writing out this one line of code,
1:33:03 which just prints out like hello to someone.
1:33:06 And we're allowing this function to receive two parameters.
1:33:09 So the name and the age, and depending on the name and the age,
1:33:13 the function's going to print out hello a little bit differently.
1:33:16 And that's kind of the beauty of using function.
1:33:19 So you could pass anything you want into a function.
1:33:22 So for example, I could pass in a integer instead of a string for the age.
1:33:27 So like I could pass in the number, the only difference is over here,
1:33:31 we're going to have to convert this into a string.
1:33:34 But it's going to work just the same.
1:33:37 So you could pass in strings, numbers, bullions, arrays,
1:33:41 you can really pass any type of data into a function.
1:33:46 And it's going to work.
1:33:47 So you can see here, we get the same result.
1:33:49 So that's the basics of functions.
1:33:51 And as you go through with Python,
1:33:53 you're going to be using functions more and more.
1:33:56 And generally, it's a good idea to break your code up into different functions.
1:34:00 So whenever you have like a grouping
1:34:03 of code that's designed to perform a specific task,
1:34:06 that's usually a good candidate to be put inside of a function.
1:34:11 In this tutorial, I want to talk to you
1:34:15 guys about using the return statement in Python functions.
1:34:19 Now Python function is basically just a collection
1:34:23 of Python code that performs a specific task.
1:34:26 And when we want to perform that task in our Python programs,
1:34:30 we can call a Python function.
1:34:32 And a lot of times when we're calling a Python function,
1:34:36 we just kind of call it and it does its thing.
1:34:38 And then we move on with the program.
1:34:40 But sometimes when we call a function,
1:34:42 we're actually going to want to get information back from that function.
1:34:47 So when I call the function, I want it to basically execute its task,
1:34:50 I want it to execute all the code.
1:34:52 And then I want it to like give me some information back.
1:34:55 So that could be like a particular value,
1:34:57 it could be something like telling me how the task went.
1:35:00 Basically, the function can just communicate back to us like,
1:35:04 Hey, here's some information.
1:35:05 And that's what the return keyword can allow us to do
1:35:09 the return keyword can basically allow
1:35:12 Python to return information from a function.
1:35:14 So I'm going to show you guys how we can
1:35:17 do this, I want to create a very simple function.
1:35:20 And it's just going to cube a number.
1:35:22 So when we cube a number, we take it to the power of three.
1:35:26 So if I was going to cube like two,
1:35:28 it'd be like two raised to the power of three.
1:35:31 Right, it's just pretty basic math.
1:35:33 So we're going to make a function that will cube a number.
1:35:36 And I'll show you guys how we can use that return statement.
1:35:39 So first thing we want to do is make a function,
1:35:42 I'm just going to say death cube, we'll call the function cube.
1:35:46 And over here, we can specify any parameters that this function will take.
1:35:51 So basically, any values that we want to give this function.
1:35:55 So we're going to give it one function, which is just going to be a number.
1:35:58 So I'm just going to call it num.
1:36:00 And then over here, I'm going to type out a colon.
1:36:03 And then down here, we can put any of the code
1:36:06 that we want to be included in this function.
1:36:09 So basically, this function is just going to cube this number.
1:36:12 So we could just type out num, times num, times num.
1:36:16 Right, that's going to give us num cubed.
1:36:20 So that's all well and good.
1:36:22 And if I come down here, I can execute this function.
1:36:25 So I can basically just call it, I'll say cube.
1:36:28 And why don't we pass it a three?
1:36:30 So I'm going to go ahead and pass it to three.
1:36:33 And I'm going to save this.
1:36:35 And now let's go ahead and run this code.
1:36:39 So we're on the code.
1:36:41 And you'll see down here, nothing happened, right?
1:36:45 So nothing got printed out in our program.
1:36:48 In fact, when we call this cube function, it didn't really do anything.
1:36:52 So let me try to print this out.
1:36:55 So maybe if we print this function out,
1:36:57 it'll give us back the information that we want.
1:37:00 So type out print, and we'll print out the value of the function,
1:37:04 I'm just going to click play.
1:37:05 You look down here, it says none.
1:37:08 So when I printed out the value of this function, it told me none.
1:37:13 But really, this function is supposed to be cubing the number.
1:37:17 And here's how we can use the return statement in Python.
1:37:20 I can come over here and I can just say return.
1:37:24 And now Python is going to return whatever value we put over here to the right.
1:37:28 So when I call this cube function, and I give it a three,
1:37:32 Python is going to go off, it's going to execute this function.
1:37:36 When it sees this return statement, it's going to say, okay,
1:37:39 I want to give a value back to whatever call of this function.
1:37:43 And it's going to return this value.
1:37:46 Now, when I print out cube three,
1:37:49 it should print out the answer because we get an answer back.
1:37:53 So when I play this, you'll see down here, it says 27.
1:37:57 So we got the correct answer.
1:37:59 So I could do this with anything, why don't we cube like four or something?
1:38:02 So now we're on this.
1:38:04 And you can see we're constantly getting the correct answer back.
1:38:09 So that's the basics of using this return statement,
1:38:11 it allows us to return a value back to the caller.
1:38:15 In other words, back to whatever's calling the function.
1:38:17 So I just want to illustrate this point a little bit further,
1:38:21 I'm going to come up here, I'm actually going to create a variable.
1:38:23 So I'm going to create a variable called result.
1:38:26 And I'm just going to set it equal to cube four.
1:38:30 This variable result is going to store
1:38:33 the value that gets returned from the cube function.
1:38:36 So it's not going to store like cube four,
1:38:39 it's going to store the value that gets returned from executing that function.
1:38:43 So now if I came down here and printed out result,
1:38:46 it's actually just going to print out 64 again,
1:38:49 because that's the value that it's storing.
1:38:51 So this return statement can be really useful
1:38:53 for like getting information back from a function,
1:38:56 we can use something like a parameter, like this four as a parameter,
1:39:00 and we can give information to a function.
1:39:02 And then we can use the return
1:39:05 statement to get information back from a function.
1:39:07 And I do just want to point out one more thing,
1:39:11 I'm actually not able to put any code after this return statement.
1:39:15 So if I came down here and tried to put like a print statement,
1:39:19 this actually isn't going to work.
1:39:20 So this is never going to get printed out.
1:39:23 So if I just typed out like code right here,
1:39:26 when we run this program, it's not going to type out code.
1:39:30 That's because when I use this return keyword, it breaks us out of the function.
1:39:36 So Python's going to go through, it's going to execute the code in the function,
1:39:40 whenever it sees this return keyword,
1:39:41 it's just going to break back out and we're going to be done.
1:39:45 So we'll never be able to reach this line of code.
1:39:48 So that's the basics of using return statements.
1:39:51 And you can return any data type you want,
1:39:53 we could easily return like a string or a Boolean or an array,
1:39:57 it doesn't really matter, you can return whatever you want.
1:40:00 And a lot of times in a lot of situations,
1:40:02 you're going to want to get a value back from a function.
1:40:09 In this tutorial, I want to talk
1:40:11 to you guys about using if statements in Python.
1:40:14 Now if statements are a special structure in Python,
1:40:17 where we can actually help our programs to make decisions.
1:40:21 So by using an if statement,
1:40:23 I could execute certain code when certain conditions are true.
1:40:27 And I can execute other code when other conditions are true.
1:40:31 So basically if statements allow our programs
1:40:33 to respond to the input that they're given.
1:40:36 So depending on the data that we're using in the program,
1:40:40 our programs will be able to respond.
1:40:42 So when certain data is certain values, we can do certain things.
1:40:46 And when other data is other values, we can do other things.
1:40:49 So you know, your programs are essentially becoming a lot smarter.
1:40:53 And actually, if statements are things that we
1:40:56 as human beings deal with every day.
1:40:58 So you know, as you go throughout your day,
1:41:01 generally, you're dealing with if statements all the time.
1:41:03 So I want to kind of give you guys a brief introduction into if statements,
1:41:07 and we'll talk about some of the common
1:41:09 if statements that you'd see in everyday life.
1:41:11 And then we'll go ahead and do a really awesome example to kind
1:41:14 of like show you guys what if statements can do for us.
1:41:17 So over here in my text editor, I just have this little text file.
1:41:21 And in here, I wrote out a bunch of if statements
1:41:24 that a lot of people will encounter on a daily basis.
1:41:27 So here's the first one, it says I wake up, if I'm hungry, I eat breakfast.
1:41:33 So let's break this down a little bit up here, it says if I'm hungry.
1:41:38 And this is a condition, right?
1:41:39 This is either going to be true, or it's going to be false, right?
1:41:44 If it's true, I'm going to go ahead and eat breakfast, right?
1:41:48 If it's false, I'm just going to skip whatever was in here and move on, right?
1:41:53 So that's the basics of an if statement, right?
1:41:55 We have a condition, in this case, if I'm hungry,
1:41:58 if the condition is true, then we do the action.
1:42:02 So we eat breakfast.
1:42:03 Otherwise, we just skip it.
1:42:05 So down here, we have another one, it says I leave my house.
1:42:10 If it's cloudy, I bring an umbrella.
1:42:13 Otherwise, I bring sunglasses.
1:42:14 And so this one is a little bit more complex than the one we just looked at.
1:42:19 You'll see we have our condition, right?
1:42:21 It says if it's cloudy, if that condition's true,
1:42:24 we're going to bring the umbrella.
1:42:26 But in this case, if the condition's false,
1:42:29 there's something else that we want to do.
1:42:31 So in the case up here, if this condition was false, we just moved on.
1:42:35 But now if this condition's false,
1:42:37 then we're going to go ahead and bring sunglasses.
1:42:40 So that's another example of an if statement, it's a little bit more complex.
1:42:44 Finally, I have one more down here,
1:42:46 and this one's even more complex, it says I'm at a restaurant.
1:42:50 If I want meat, I order a steak.
1:42:52 So if this condition's true, we order the steak.
1:42:55 But if this condition is false, then we're going to move on, and we're
1:43:00 actually going to check another condition.
1:43:02 So I'm going to say otherwise, if I want pasta, I order spaghetti and meatballs.
1:43:07 This is another condition.
1:43:08 If this condition's true, we get the spaghetti.
1:43:11 If it's not true, then finally, we're going to move on down here,
1:43:16 and we'll just default to ordering the salad.
1:43:18 So all three of these if statements are valid if statements,
1:43:22 it's just they get more and more complex as we go down the list.
1:43:26 So hopefully that's, you know, it can kind of wrap your head around
1:43:29 what if statements are we're basically specifying conditions.
1:43:31 If those conditions are true, we're going to do certain things.
1:43:35 And if those conditions are false,
1:43:37 we can do other things, or we can check other conditions.
1:43:40 All right, so let's head over to our Python file,
1:43:43 and we'll start writing some actual Python.
1:43:45 So in here in my app dot Python file, I want to create an if statement.
1:43:50 And I'm going to show you guys a very basic if statement in this tutorial.
1:43:53 And then in the next tutorial,
1:43:55 we're going to look at some more advanced if statements.
1:43:58 And so what I want to do is create a Boolean variable.
1:44:02 So I'm going to create a Boolean variable that's going
1:44:05 to store whether or not the user is a mail.
1:44:07 So it's just going to be called is underscore mail.
1:44:10 And I'm going to set this equal to true, because I'm a male.
1:44:14 So we have this Boolean variable, right?
1:44:16 And right now we have it set equal to true.
1:44:20 I can actually use an if statement to check
1:44:23 to see what the value of this variable is.
1:44:26 And if it's true, I can do something.
1:44:27 So if I want to use an if statement, all I have to do is just type out if,
1:44:32 and then I just need to type out a condition.
1:44:36 So basically, I would have to type out
1:44:38 like something that's going to be true or false.
1:44:41 Like, for example, in the other page, we were specifying like, if I'm hungry,
1:44:46 or if it's cloudy, here, we're just going to check to see if is mail.
1:44:50 So in other words, if the person is male.
1:44:53 And so remember, this has to be a, like,
1:44:56 it has to be able to be reducible to a true or a false value.
1:45:01 So in our case, we're just using a Boolean,
1:45:03 because that's like the easiest thing we can do.
1:45:06 So I said if I specified my true or false condition,
1:45:11 and now I want to just type a colon.
1:45:13 And what I want to do now is make a new line.
1:45:16 And so anything that I put below this with an indentation
1:45:20 is actually going to be executed when that conditions true.
1:45:25 So I could put print.
1:45:26 And down here, I could just put you are a male.
1:45:32 And now you'll see what happens when I run this program,
1:45:34 it's going to go down here, and it's going to say you are a male.
1:45:38 But if I changed is mail to false.
1:45:41 So instead of this being true, I just change it to false.
1:45:44 Now all of a sudden it's not going to print anything.
1:45:47 So it's just going to be blank down here.
1:45:50 That's because this condition wasn't true.
1:45:53 So anything that I put below this if declaration and that has an indentation
1:45:58 like that is going to be executed when the if statement is true.
1:46:03 We can also use another keyword in Python, which is called else.
1:46:08 And else is basically going to be like otherwise.
1:46:11 So you remember before we said, if it's cloudy, I'll bring an umbrella.
1:46:15 Otherwise, I'll bring sunglasses.
1:46:17 Well, this is going to be like our otherwise.
1:46:20 So I can say down here else.
1:46:22 And now anything that I put below else is
1:46:25 going to get printed out when that condition isn't true.
1:46:28 So why don't we just put you are not a male.
1:46:32 So now we're able to cover both of the situations for this variable.
1:46:36 If it's false, we can cover that situation.
1:46:39 So it says you are not a male.
1:46:41 If it's true, we can cover that situation as well.
1:46:45 So it'll say you are a male.
1:46:47 So that's pretty cool.
1:46:49 But we can also make these more complex.
1:46:52 And actually before I talk about that, you can put
1:46:55 as much code as you want inside of an if statement.
1:46:57 So I could have like, you know, dozens of lines of code if I wanted.
1:47:01 I'm just giving you guys a very simple example.
1:47:03 So why don't we make this more complex?
1:47:06 And I'm going to add another variable into the mix.
1:47:09 So I'm going to add another, you know, Boolean attribute into this mix.
1:47:13 I'm going to make another variable called is tall.
1:47:15 And we're going to set this equal to true as well.
1:47:19 So now I have two variables in the mix.
1:47:22 Let's say I wanted to write an if
1:47:25 statement that will check both of those variables.
1:47:27 So I could write an if statement that's like,
1:47:30 if they're male and they're tall, or if they're male and they're not tall,
1:47:33 or if they're not male and they're tall, like I could write out a bunch of if
1:47:37 statements to check the combination of these variables.
1:47:40 And so the way I can do that is by using one of two keywords.
1:47:45 So the first keyword I want to show you guys is the or keyword.
1:47:50 So I can come over here and I can say if is male, or is underscore tall.
1:47:56 And what this is basically going to do is
1:47:59 it's going to say if the person is either male,
1:48:03 or if they're tall, then we want to do something.
1:48:06 So this is only going to be true when one of these values is true,
1:48:12 when one or both of those values is true.
1:48:15 So down here, we could actually put instead of you are a male,
1:48:19 we could put you are a male or tall or both,
1:48:22 that's going to be more appropriate.
1:48:24 And then down here, we would say you are neither male nor tall.
1:48:31 And that's because if we execute the code inside of this else,
1:48:36 that means that both of these guys are false.
1:48:39 Right, so I'm saying if they're male or they're tall,
1:48:42 we're going to execute whatever is in here.
1:48:45 Otherwise, that means they're neither male nor tall.
1:48:47 We're just going to execute this down here.
1:48:50 So let me show you guys how this works.
1:48:52 I'll click play.
1:48:53 And these are both true.
1:48:54 So it's going to say you are a male or tall or both.
1:48:57 If I was to set one of these to false.
1:49:00 So let's set is male equal to false.
1:49:02 Now, it's still going to say you are a male or tall or both.
1:49:07 But if I set them both equal to false,
1:49:10 now it's going to say you're neither male nor tall.
1:49:14 So that's kind of the basics of using the or operator.
1:49:19 And there's another operator we can use which is called and, and basically
1:49:24 it's similar to or except both of these conditions have to be true.
1:49:28 So this is basically saying if the person is male and they're also tall,
1:49:34 then we're going to execute whatever is in here.
1:49:36 So we could actually change this text to say you are a tall male,
1:49:40 because we know for a fact that if this is getting executed,
1:49:45 they're both male and tall.
1:49:47 We can also change this print statement down here.
1:49:50 This could say you are either not male or not tall or both.
1:49:56 So you can see it's basically just saying that you're
1:50:00 either not one of them or you're not both of them.
1:50:04 So we click the play button and you'll see because they're both false,
1:50:08 it's going to say you are either not male or not tall or both.
1:50:12 But if I make them both true, now it's going to say you are a tall male.
1:50:17 If I was going to make one of these false, though, so let's make is tall false.
1:50:23 Now it's not going to execute this first print statement.
1:50:27 It's going to execute the one inside of the else.
1:50:29 So to say you are either not male or not tall or both.
1:50:32 So that's the basics of and an or and you'll
1:50:36 be using those a lot as you use your if statements.
1:50:39 But there's actually one more thing we can do.
1:50:42 We can add in some more conditions.
1:50:45 So up here I'm checking if they're male and they're tall.
1:50:48 But what if I wanted to check if they're and they're not tall?
1:50:52 So if they're like a short male, I can use another keyword in Python called
1:50:58 else if or really it's just typed out E
1:51:00 L I F and it stands for else if and after this we can put another condition.
1:51:05 So over here I want to check to see if they're male and they're not tall.
1:51:11 So I'm going to say else if male is male and now I can type in not tall
1:51:17 and the way I can do that is by saying
1:51:20 not an open and close parentheses and saying is tall.
1:51:23 And basically when I use this not function
1:51:26 it's going to negate whatever is inside of here.
1:51:29 So if this was true it's going to make it false
1:51:32 and if this was false it's going to make it true.
1:51:35 So basically that'll tell us whether or not they're tall.
1:51:38 And then I'm going to use a colon
1:51:41 and now down here inside of here we're basically
1:51:44 just going to type out any of the code
1:51:47 that we want to happen when this condition's true.
1:51:49 So down here we can just print out you are a short male and there's actually one
1:51:56 more condition that we want to check for which
1:51:59 would be if they're not male and they're tall.
1:52:02 So we can make one more else if and I'm actually
1:52:06 just going to copy this guy and we'll paste it down here.
1:52:10 And so I want to say if not is male and is tall.
1:52:16 So now we're checking to see if they're male
1:52:20 and or if they're not male and they're tall.
1:52:24 So here we could say you are a you are not a male but are tall.
1:52:32 So essentially here we have an if statement that's
1:52:35 going to cover every single condition for these two variables.
1:52:38 So in every single situation of the two values of these variables
1:52:42 we're going to be able to print something out for it.
1:52:45 So if I made both of these true it's
1:52:48 basically going to tell us you are a tall male.
1:52:51 If I make is male false it's going to tell us you are not a male but are tall.
1:52:57 If I made is male true and is tall false
1:53:01 then it's going to tell us you are a short male.
1:53:05 And if I made both of these false it's going to tell
1:53:09 us you are either not male or not tall or both.
1:53:12 And actually we need to change this.
1:53:15 So this should say you are not a male and not tall.
1:53:19 So that would be a better print out for that situation.
1:53:23 So you can basically see how we can use if else ifs and else in order
1:53:30 to help our program to make decisions and really
1:53:33 to respond to the different data in our programs.
1:53:36 So I'm able to respond to every
1:53:38 combination of the values for these two variables.
1:53:41 And that's really cool.
1:53:43 So in this example we were just using simple Boolean variables but in the next
1:53:48 tutorial I'm going to show you guys how
1:53:50 you can actually use something called comparison operators.
1:53:53 So we could compare like two numbers or two
1:53:56 strings and we could use those as our conditions.
1:53:58 But for now I think this is a good introduction into if statements.
1:54:03 So I'll see you guys in the next tutorial.
1:54:08 In this tutorial I want to talk to you some more about if statements in Python.
1:54:14 More specifically we're going to be looking
1:54:16 at using comparisons inside of our if statements.
1:54:19 So if you're following along with the course
1:54:21 in the last tutorial I talked to you guys about just the basics of if statements
1:54:26 and we used a couple different Boolean variables.
1:54:28 And depending on the values of those bullions we were able to do certain things.
1:54:32 In this tutorial we're going to look at another
1:54:35 way to use if statements which is with comparisons.
1:54:37 So instead of just using Boolean
1:54:40 values we can actually compare different values.
1:54:43 So I could compare a couple numbers or I could compare a couple strings.
1:54:47 And depending on the results of those comparisons we can do certain things.
1:54:51 So this is going to be pretty cool.
1:54:54 And this is really going to give you guys
1:54:56 a full understanding of what you can do with if statements.
1:55:00 So for this tutorial I actually want to create a Python function.
1:55:04 And this function is going to give us the maximum number that we pass into it.
1:55:09 So this function is going to take three parameters as input
1:55:13 and it's going to print out the biggest number that we give it.
1:55:18 So let's create this function.
1:55:19 I'm just going to say death.
1:55:22 And I'm just going to call it max num.
1:55:25 And inside of these parentheses I'm going
1:55:28 to specify that I want three parameters as input.
1:55:31 So I'm going to say num one, num two and num three.
1:55:35 So basically we're passing it three numbers
1:55:38 and this function will return the largest of the three.
1:55:43 So in order to figure out which of these numbers is
1:55:46 the largest we're going to have to use an if statement.
1:55:49 So I can say if and over here I need to specify a condition
1:55:53 right so I need to put like a true or a false value.
1:55:57 And so what we can actually do is we can compare these different numbers.
1:56:01 So I could say if num one is greater than or equal to num two.
1:56:09 And num one is greater than or equal to num three.
1:56:14 So basically what I'm doing here is I'm comparing these different numbers.
1:56:18 So I'm saying if the first number is greater than or equal to the second number.
1:56:23 And this is a comparison.
1:56:24 So when you think about it, when we compare these two numbers,
1:56:28 we're going to end up with a true or a false value.
1:56:31 Right, num one is either greater than or equal to num two or it's not.
1:56:36 It's actually a Boolean value right it's either true or it's false.
1:56:40 Same thing over here.
1:56:42 The result of this comparison is either true or it's false.
1:56:45 So technically I'm putting a true or false value in there.
1:56:49 I'm just getting that true or false value by using a comparison.
1:56:54 And you'll notice over here we're using something called a comparison operator.
1:56:58 And basically this is just like how we want to compare them.
1:57:02 So we're saying like greater than or equal to in this case.
1:57:05 So down here, if this whole condition is true, in other words,
1:57:09 if the results of these two comparisons end up being true,
1:57:13 then we know that num one is the biggest.
1:57:16 So I can just return num one, I can say return num one.
1:57:20 But we also want to check some other conditions.
1:57:23 So I can say L if, and now I want to do some other comparison.
1:57:28 So I'm going to say else if num two is greater than or equal to num one.
1:57:34 And num two is greater than or equal to num three.
1:57:38 So I'm doing essentially what I did up here, except for num two.
1:57:42 So again, I'm comparing these different numbers.
1:57:44 And this is going to end up being a true or a false value,
1:57:48 depending on the result of the comparison.
1:57:51 So down here, again, we can just return num two, because if all of this is true,
1:57:57 that means we're going to end up returning num two.
1:58:00 And then finally, we can just say else.
1:58:03 So if num one isn't the biggest and num two isn't the biggest,
1:58:07 then we can pretty much assume that num three is the biggest.
1:58:12 So now we have our function.
1:58:15 So down here, I'm actually just going to call this function.
1:58:18 So I'm just going to say, actually, I'll print out the result.
1:58:21 So I'll say max num, and we'll pass in like three, four, five.
1:58:27 So we're basically calling that new function that we just made.
1:58:31 And I'm going to go ahead and run this program.
1:58:35 So you can see down here in the output,
1:58:37 we print it out five, because that was the biggest.
1:58:40 If I was to change this middle one to the biggest,
1:58:42 so we make the middle 140, now it'll be able to take on that situation.
1:58:46 And finally, I can make this like 300.
1:58:49 And it'll be able to handle that.
1:58:51 So no matter which number num one num
1:58:53 two or num three ends up being the biggest,
1:58:56 it's able to tell us without a problem.
1:58:59 So these comparisons are a really, really,
1:59:02 really common way to make if statements.
1:59:04 And a lot of times, you're going to want
1:59:07 to be comparing different values inside of Python.
1:59:10 So over here, we're comparing numbers, but we could also compare strings.
1:59:14 So I could easily make these both like, you know,
1:59:17 I could make this like dog, and I can make this dog.
1:59:21 And I could say a different operator.
1:59:23 So I could say like this double equal sign would mean equal.
1:59:27 So I could basically say like, if this string is equal to that string,
1:59:31 so you can basically like compare all different data types,
1:59:35 you know, I could compare numbers, strings, you could also compare bullions.
1:59:40 So once again, these are comparison operators.
1:59:42 And this one is greater than or equal to, but there's
1:59:46 a bunch of other comparison operators we can use.
1:59:48 So the most basic is just going to be a double equal sign.
1:59:52 And that basically checks to see if the two values are equal.
1:59:55 So it'll say like, if number one is equal to number two,
2:00:00 we can also say not equals.
2:00:02 And this and this exclamation point will basically mean not equal.
2:00:07 So it's if num one is not equal to num two,
2:00:12 we also have greater than less than greater
2:00:15 than or equal to less than or equal to.
2:00:18 And those are kind of all of the operators are all the, you know,
2:00:22 the basic comparison operators that you're going to be using in Python.
2:00:26 So using comparisons is really awesome.
2:00:28 And there's going to be tons and tons of situations inside
2:00:31 of Python where you're going to want to compare different values.
2:00:35 So these can be extremely useful.
2:00:38 In this tutorial, I'm going to talk
2:00:41 to you guys about building a calculator in Python.
2:00:44 Now, if you've been following along with this course,
2:00:46 you'll know that in the beginning of the course,
2:00:48 we actually created a very basic calculator.
2:00:51 Basically, the user could just enter in two numbers and our calculator
2:00:55 would add the numbers together and spit out the answer.
2:00:59 In this tutorial, I want to show you
2:01:01 guys how to build a more advanced calculator.
2:01:04 So this calculator is going to be able
2:01:06 to perform all of the basic arithmetic operations.
2:01:08 So we can add, subtract, multiply and divide.
2:01:11 And we'll actually allow the user to specify which one of those they want to do.
2:01:16 So the user will enter in a number,
2:01:18 they'll enter in whatever operator they want to use.
2:01:21 So plus minus divide, multiply.
2:01:23 And then they'll enter in the second
2:01:25 number and we'll perform the operation for them.
2:01:27 So this can be pretty cool.
2:01:29 And it's going to show you how we
2:01:31 can use if statements in like a practical application.
2:01:34 So let's get started.
2:01:36 The first thing we want to do is get input from the user.
2:01:39 So I'm going to create three variables, one for the first number,
2:01:43 one for the second number, and one for the operator.
2:01:46 And we'll basically store the values that the user inputs into those variables.
2:01:50 So the first one will just be num one.
2:01:53 And I'm going to set this equal to input.
2:01:56 And we're going to give this a prompt.
2:01:59 So I'll say enter first number.
2:02:01 And here's actually the thing is whatever the user enters,
2:02:05 we're going to want to convert it into a number.
2:02:09 So generally in Python, when we get input from the user,
2:02:12 no matter what they put in, it just gets converted into a string.
2:02:16 But we don't want a string, because we're going to have to be
2:02:19 adding or subtracting or multiplying these numbers together.
2:02:22 So whenever they enter in the number,
2:02:25 I just want to immediately convert it to an actual
2:02:28 like number data type instead of a string.
2:02:30 So what I can do is I can actually say float,
2:02:34 and I can surround this entire input tag with parentheses.
2:02:37 And now what this is going to do is it's
2:02:41 going to immediately convert whatever the user inputs into a float.
2:02:45 Now that's going to mean that they're going to have to insert a number,
2:02:49 otherwise we'll get an error.
2:02:50 But for the most part, this should work out for us.
2:02:53 So I'm actually just going to copy this guy.
2:02:56 And we'll go ahead and paste it over here.
2:02:59 And we'll paste it one more time.
2:03:01 So we're basically just trying to get three pieces of input from the user.
2:03:04 So the second thing we want to get is the operator.
2:03:07 So I'm just going to say op for operator.
2:03:11 And I'll actually get rid of this float,
2:03:14 because we want to keep this as a string.
2:03:17 And I'll just say enter operator.
2:03:19 So that's going to be like plus minus multiplication, whatever.
2:03:23 And then finally down here, we'll get num two.
2:03:27 And again, we're going to want to convert this to a float.
2:03:31 So I'll say enter second number.
2:03:33 So now basically what we're doing is we're getting the first number,
2:03:36 we're getting the operator and we're getting the second number from the user.
2:03:40 So what we need to do now is we need to figure out what is inside of this op.
2:03:46 In other words, we need to figure out what operator the user was trying to do.
2:03:51 So are they trying to do addition or they trying
2:03:54 to do subtraction like we want to figure that out.
2:03:57 So we can use an if statement in order to figure that out.
2:04:01 So I'm going to come down here and I'm
2:04:03 basically just going to create an if statement.
2:04:06 So I'm going to say if, and we want to check a condition.
2:04:09 So I'm basically going to check to see if this operator is equal to a plus sign.
2:04:15 So I can say if op equals plus sign.
2:04:19 And now if the operator is equal to plus sign,
2:04:22 I'm just going to print out num one plus num two.
2:04:26 But let's say that instead of a plus sign they entered in something else.
2:04:30 So we can say out if op is equal to, and now we'll check if it's a minus sign.
2:04:37 And so here we'll just print num one minus num two.
2:04:41 And actually forgot to put a colon here.
2:04:44 So we can just do these for everything.
2:04:48 So now we can say L if op is equal to division and here we'll
2:04:54 just divide the two numbers so I can print out num one divided by num two.
2:05:00 And finally, we'll do the same thing for multiplication.
2:05:04 So I'll say l if op is equal to asterisk.
2:05:08 And we'll just print out num one times num two.
2:05:14 Alright, so basically over here we have every, you know, condition.
2:05:18 But I also want to put in an else statement.
2:05:21 So in the case that the user enters in an operation,
2:05:25 that's not like one of the four that we specified up here,
2:05:28 I just want to throw an error.
2:05:30 So I'm just going to say op or actually we just say else.
2:05:34 And here we'll just print out like invalid operator.
2:05:39 Alright, so this if statement should give us everything we want.
2:05:43 Basically, I'm just checking to see if it's equal to a plus
2:05:47 sign a minus sign a division sign or a multiplication sign.
2:05:50 Depending on which one it's equal to, we'll do a different operation.
2:05:53 So let's go ahead and run this program and see how we do.
2:05:57 So I'm going to run this program and it says enter first number.
2:06:01 Let's enter five, enter an operator.
2:06:03 So why don't we just do addition and enter a second number.
2:06:08 Let's do 23.
2:06:09 So now it prints out the answer 28.
2:06:13 So a calculator actually works.
2:06:15 Let's try another operator just to make sure.
2:06:18 So we'll do six and why don't we multiply it by, I don't know, 5.23.
2:06:24 So now we go off and we do the multiplication and looks like we got our answer.
2:06:30 So our calculator seems to be working.
2:06:32 And so basically all we did was we got the input from the user
2:06:36 and then we used if statements to figure out what they wanted to do.
2:06:41 So we can check to see if they wanted addition.
2:06:43 We can check to see if they wanted subtraction or division or multiplication.
2:06:46 And we can also check to see if they entered in an invalid operator.
2:06:50 So actually why don't we try that too.
2:06:53 So I'll enter in like five and now we'll just
2:06:57 enter in like an open and close parentheses and a four.
2:07:01 So you can see here it's able to tell like
2:07:04 the program's smart enough to know that that's not a valid operator.
2:07:07 So these if statements can be really powerful and we can use
2:07:10 them in combination with getting the user input to create a little calculator.
2:07:18 In this tutorial I want to talk to you guys about using dictionaries in Python.
2:07:24 Now a dictionary is a special structure in Python which allows
2:07:28 us to store information in what are called key value pairs.
2:07:32 So essentially I can just create a bunch
2:07:35 of these different key value pairs and then when I want
2:07:39 to access a specific piece of information inside of the dictionary
2:07:43 I can just refer to it by its key.
2:07:46 So the word dictionary is actually pretty appropriate here.
2:07:49 If you guys are familiar with a normal dictionary you know in a normal
2:07:53 dictionary you'd have a word and then
2:07:55 you'd have a definition associated to that word.
2:07:58 So in this situation the word would be the key.
2:08:01 In other words the word is what like uniquely identifies it inside
2:08:05 of the dictionary and then the value would be the actual definition.
2:08:10 So this is going to make more sense once we start working
2:08:13 with this but I just wanted to give you guys a quick introduction.
2:08:16 So in this tutorial I want to create a little program that will
2:08:20 allow us to convert a three digit month name into the full month name.
2:08:26 So I could convert for example like JAN to January
2:08:31 or I could convert M A R into March.
2:08:36 Like I want to be able to have a program that can do
2:08:39 that for us and that's actually a pretty good use case for a dictionary.
2:08:43 So we're actually going to create a dictionary and I'm
2:08:45 going to show you guys how we can do that.
2:08:48 So the first thing we need to do
2:08:50 in creating a dictionary is just give it a name.
2:08:52 So I want to give this a specific name.
2:08:55 So I'm just going to call this month conversions.
2:08:58 And now I just want to set this equal to an open and closed curly bracket.
2:09:04 So whenever we create a dictionary in Python we're always going
2:09:07 to want to create it inside of these open and closed curly brackets.
2:09:12 So inside of the dictionary now we can
2:09:15 start defining what are called key value pairs.
2:09:18 So I can define a key and then I can give it a corresponding value.
2:09:23 And the way that we can do that is just
2:09:25 by typing out the key and then typing out the value.
2:09:27 So in our case we're converting three
2:09:29 digit month names into the full month names.
2:09:32 So I would want to type out for example JAN and then I can type a colon
2:09:38 and I want to type out the value I
2:09:40 want to associate with it so we could say January.
2:09:43 I can basically do this for every combination.
2:09:46 So I could say like FEB and this would be February.
2:09:50 And actually after each one of these we're going to need to put a comma.
2:09:55 So I could do the same for March, say M.A.R.
2:10:01 So essentially what I can do is I can specify
2:10:05 an entry inside of this dictionary for each one of these.
2:10:09 So this over here would be the key and this over here would be the value.
2:10:14 And here's one thing is all of these keys have to be unique.
2:10:18 So if I was to come down here and change this to JAN,
2:10:22 this is actually going to give us a little warning.
2:10:24 So you can see down here it says dictionary contains duplicate keys.
2:10:29 That's a no no when we're creating dictionaries.
2:10:31 So you always want to make sure that the keys are unique.
2:10:34 All right.
2:10:34 So I'm going to go ahead and create one of these for each
2:10:38 one of the months and then we'll come back and we'll talk about it.
2:10:41 All right.
2:10:41 So I've gone ahead and created an entry for all 12 of the months.
2:10:46 So now we have a bunch of unique keys and each
2:10:49 one of those keys is associated with a different value.
2:10:52 So this is the basics of creating a dictionary is
2:10:56 we have a bunch of these different key value pairs.
2:10:59 And now what we can actually do is
2:11:01 we can access them from inside of this dictionary.
2:11:04 So if I wanted to access a specific key or a specific value,
2:11:09 all I have to do is come over here and I'm actually just
2:11:12 going to print it out onto the screen so we can see it.
2:11:15 All I have to do is refer to the dictionary by name.
2:11:19 So it's just called month conversions.
2:11:21 And there's actually a bunch of different
2:11:24 ways that I can access these month names.
2:11:27 So I could access different entries inside of this dictionary.
2:11:31 The first way is just by making a open and close square bracket and inside here,
2:11:37 I can just type in one of the keys.
2:11:40 So for example, inside here, I could type in NOV.
2:11:43 And what this should do is it should give us back the full name for November.
2:11:47 So when I print this out, you'll see that we get back down here November.
2:11:52 So I could do the same thing for, you know,
2:11:56 any of these, we could say like M A R.
2:11:58 And now we should get March back.
2:12:00 So I'm able to refer to the key.
2:12:03 And it's going to go into the dictionary and it's
2:12:06 going to give me the value that's associated to that key.
2:12:09 There's actually another way we could do this too.
2:12:12 So I could say month conversions dot get an inside of here.
2:12:16 I could also pass in the name of a key.
2:12:19 So I could pass in like D E C.
2:12:22 And now we'll be printing out December.
2:12:25 And what's cool about using this get function is I can actually specify
2:12:30 a default value that I want to use if this key is not found.
2:12:35 So there's going to be certain cases
2:12:37 when we're dealing with dictionaries where you're going
2:12:39 to put in a key that might not
2:12:41 necessarily map to a value inside of the dictionary.
2:12:44 So you put in an invalid key.
2:12:47 So let's just try to do that.
2:12:49 So I'll just put like L U V.
2:12:51 That's not a month abbreviation.
2:12:52 So when I try to run this, you'll see we get this none down here.
2:12:58 And in a lot of cases,
2:13:00 you're going to want to create some sort of a default value.
2:13:03 So if I'm in this situation where I have a key
2:13:06 that's not mapable to any values inside of this dictionary,
2:13:10 I can actually pass it a default value.
2:13:13 So inside this dot get function, I can make a comma.
2:13:17 And now I can pass in like a default value to get printed out.
2:13:21 So I can say like not a valid key.
2:13:24 And now when I put in the invalid key, instead of saying none,
2:13:27 it's going to go ahead and say not a valid key.
2:13:31 So this is going to give us like a default value that we can fall back on.
2:13:36 So these dictionaries are really awesome.
2:13:38 And the keys over here don't have to be string.
2:13:42 So you could also use numbers.
2:13:43 So for example, I could say like zero, one, 10,
2:13:46 like I can put any numbers I want over here as keys as long as they're unique.
2:13:53 And then I could access those keys,
2:13:55 just like I normally would down here with this get
2:13:58 function or with those open and close square brackets.
2:14:01 So that's sort of the basics of using dictionaries.
2:14:03 It can be really useful to store key value pairs.
2:14:06 And honestly, you're going to be using them
2:14:08 a lot in Python to store different types of data.
2:14:12 In this tutorial, I want to talk to you guys about while loops in Python.
2:14:19 Now a while loop is basically a structure in Python,
2:14:23 which allows us to loop through and execute a block of code multiple times.
2:14:30 So I can specify like a few different lines of code,
2:14:33 and then I can put that code inside of a while loop.
2:14:37 And it would basically loop through that code,
2:14:40 executing it repeatedly until a certain condition was false.
2:14:43 So while loops can be awesome.
2:14:45 And there's a lot of situations in Python where we're
2:14:48 going to want to loop through specific lines of code.
2:14:51 So in this tutorial,
2:14:51 I just want to show you guys the like bare basics of how while loops work.
2:14:55 And then in future tutorials,
2:14:57 we're going to use while loops to create little games and stuff like that.
2:15:01 So let's talk about while loops.
2:15:03 When I create my while loop,
2:15:05 the first thing I want to do actually is create an integer.
2:15:09 I'm basically creating a variable that's a number and you
2:15:12 don't have to do this for a while loop,
2:15:15 but just for our demonstration I'm going to.
2:15:17 So I'm just going to call this I and I'm going to set it equal to one.
2:15:21 And now what I want to do is I want to create a while loop.
2:15:24 So I'm basically just going to say while.
2:15:26 And over here I want to specify a condition.
2:15:31 And this is what's called our loop condition or you
2:15:35 can also refer to it as a loop guard.
2:15:38 And basically we are going to keep looping through the code
2:15:41 inside of the while loop as long as this condition is true.
2:15:45 So whatever condition I put in here, as long as it's true,
2:15:49 we're going to keep looping through the code inside the while loop.
2:15:52 So I'm just going to say while I is less than or equal to 10.
2:15:57 And now I'm going to put a colon and I'm going to make a new line.
2:16:03 And so anything that's below this while loop declaration and that's indented
2:16:08 like this is going to be considered code that's inside the while loop.
2:16:13 And so that code is going to get
2:16:16 repeatedly executed while this condition up here is true.
2:16:19 So I'm just going to put a very simple line of code here.
2:16:23 We're just going to print out the value of I.
2:16:25 And then down here I'm going to actually increment I so I'm going
2:16:29 to add one to I'm going to say I is equal to I plus one.
2:16:34 So basically I'm taking this I value and I'm adding one to it.
2:16:38 There's actually a shorthand that we can use
2:16:40 in Python to do something like this though I could just say I plus equals one
2:16:45 and that's automatically going to add one to I.
2:16:49 So this is like a little shorthand and you'll
2:16:52 see people in Python using that a lot.
2:16:55 So over here we basically have our while loop.
2:16:58 So I have the outline for a while loop.
2:17:01 So we're defining this variable up here and we're going to keep looping through
2:17:05 the code inside the while loop as long as this condition up here is true.
2:17:11 So if this condition is false for example if I is equal to eleven then
2:17:16 we're not going to loop through this code
2:17:18 anymore and we're just going to move on.
2:17:20 So down here I'm actually just going to print.
2:17:23 Done with loop.
2:17:24 So this will kind of illustrate this for us.
2:17:28 Alright so let's go ahead and run this program.
2:17:30 I'm just going to run it.
2:17:32 And you'll see down here we get some output.
2:17:36 So we're actually going to get numbers printed out one all
2:17:39 the way down through ten and then we're printing out done with loop.
2:17:44 So basically what's happening is we're printing
2:17:47 out every time we go through this loop.
2:17:50 So initially I is equal to one right.
2:17:52 The first thing that Python is going to do when it
2:17:54 gets to this while loop is it's going to check this condition.
2:17:56 So before it does anything else it's going to check this condition and it's
2:18:01 going to say okay is one less than or equal to ten and that's true.
2:18:05 So as long as this guy is true we're going
2:18:08 to go through and loop through all the code inside of here.
2:18:11 So we're going to print out i which has the value
2:18:14 of one and then we're going to add one to i.
2:18:17 So now i is going to go from one to two.
2:18:20 Now what we're going to do is we're going to go all the way back
2:18:24 up here to this while loop declaration
2:18:26 and we're going to check the condition again.
2:18:29 So remember I checked the condition the first time and then I loop through all
2:18:33 the code then I go back up to the top and I check it again.
2:18:37 So I'm going to say okay is i less than or equal to ten.
2:18:41 At this point i is equal to two.
2:18:43 So i is less than or equal to ten.
2:18:45 So now we're going to go through and we'll execute all this code again.
2:18:49 So I'm going to print out i so it's going to be two
2:18:51 and I'm going to add one to i so now we have three.
2:18:55 Then Python is going to go all the way back up
2:18:57 to the top and it's going to check the condition again.
2:19:01 So on every iteration through the loop before Python does
2:19:04 what's inside of this loop it's going to check that condition.
2:19:08 And as long as the condition remains true we're
2:19:11 going to keep executing the code inside the loop.
2:19:14 Eventually though in this program i is going to end up being eleven.
2:19:17 So on the last iteration it's going to end up being eleven.
2:19:21 We're going to come back up here and we're going
2:19:23 to say hmm is eleven less than or equal to ten?
2:19:26 Nope.
2:19:27 And so since it's nope we're going to come down
2:19:30 here and we'll print out this done with loop line.
2:19:32 So that's the basics of how these while loops work.
2:19:35 We're specifying a loop condition as long as that condition
2:19:39 is true we'll keep executing the code inside of the loop.
2:19:42 And after every execution of the loop we're going
2:19:44 to come back up and check the condition again.
2:19:47 So that's how we end up with this printing out down here.
2:19:51 So that's the basics of a while loop.
2:19:54 And we can use these while loops to do all sorts of things.
2:19:59 Like while loops are very very powerful.
2:20:03 In this tutorial I'm going to show you guys
2:20:06 how to build a basic guessing game in Python.
2:20:09 And in this guessing game we're actually going to be using all sorts
2:20:13 of cool programming structures that we've learned
2:20:15 up to this point in the course.
2:20:16 So we're going to be using things like if statements and while loops
2:20:19 and variables and all these cool things in order to build this game.
2:20:23 So the basic idea is we will specify like a secret word.
2:20:27 So we'll have a secret word that we store inside of our program.
2:20:31 And then the user can interact with the program
2:20:34 and try to guess the secret word.
2:20:36 And so what we want to be able to happen is we want the user to be able to keep
2:20:40 guessing what the secret word is and keep typing
2:20:42 in different responses until they get the secret word right.
2:20:46 So that's the basics of what this game is going to be.
2:20:50 And now let's go ahead and create it.
2:20:52 So the first thing we want to do is create a variable to store our secret word.
2:20:57 So I could say secret word and we're just
2:21:01 going to set this equal to a secret word.
2:21:02 So why don't we make it draft.
2:21:04 That's a pretty good secret word.
2:21:05 And now what we want to do is we want
2:21:07 to create a variable that will store the user's response.
2:21:10 So I want a variable that will store like all the guesses that the user makes.
2:21:16 So I'm just going to call this guess.
2:21:18 And I'm just going to set it equal to an empty string right now.
2:21:22 So now we have the secret word and we have a variable to store the user's guess.
2:21:27 And what we need to do is we need to be
2:21:30 able to prompt the user to input the secret word.
2:21:33 But here's the catch.
2:21:34 What we want to happen is we want them to enter the secret word.
2:21:38 And if they don't guess it correctly, we want to prompt them to enter it again.
2:21:43 So we can't just use a single input statement.
2:21:47 We actually have to use something called a while loop.
2:21:50 And we can use a while loop in order to continually
2:21:53 ask the person to guess the word until they guess it correctly.
2:21:58 So let's go ahead and create our while loop.
2:22:01 So I'm going to say while.
2:22:02 And after I say while, I need to specify a looping condition or a looping guard.
2:22:08 Basically, this is something that as long as it's true,
2:22:11 we're going to keep looping through this loop.
2:22:13 So basically, I want to say I want to keep looping
2:22:17 as long as the user's guess is not equal to the secret word.
2:22:22 So as long as they haven't guessed the secret word,
2:22:26 I'm going to keep going through this loop.
2:22:29 And inside of this loop,
2:22:31 what we're going to do is we're going to ask them to input the secret word.
2:22:35 So I can take this guess variable and I can set it equal to input.
2:22:40 And I'm just going to say enter guess.
2:22:44 And so over here, we're basically telling the user to enter in their guess.
2:22:50 I'm storing whatever they enter inside of this guess variable.
2:22:55 And then what's going to happen is we're going to come back up here.
2:22:58 We're going to check to see if the guess is equal to the secret word.
2:23:02 If the guess isn't equal to the secret word, then we're going to do it again.
2:23:06 But if the guess is equal to the secret word,
2:23:08 then we're just going to break out of this loop.
2:23:11 And so we can come down here.
2:23:13 And we can just print out a success message.
2:23:17 So I could say you win because they got the secret word.
2:23:21 So this is a very, very simple program.
2:23:24 But this is essentially all of the code that we
2:23:27 need to be able to build a game like this.
2:23:30 So let's go ahead and play our game.
2:23:32 We can run it and see how we did.
2:23:34 So I'm going to click play.
2:23:35 And down here, you can see it's prompting us to enter a guess.
2:23:39 So I'm just going to type in some, you know,
2:23:42 random text, we can type in whatever we want.
2:23:44 And as long as we're not entering that secret word,
2:23:47 it's going to keep prompting us to enter different information.
2:23:50 But if I enter in the secret word, so if I enter in draft,
2:23:55 now all of a sudden the program is going to terminate and it's going to say,
2:23:59 Hey, you win, because we were able to guess the secret word.
2:24:03 So that's like a really cool way for us to be able to do this.
2:24:07 And we actually have a fully functional guessing game.
2:24:09 But I think this guessing game could actually be improved quite a bit.
2:24:14 A lot of times when we're making a guessing game, we want to set a limit.
2:24:20 So in other words, I want to set a limit for the number
2:24:23 of times that the user can try to guess the word.
2:24:26 So let's say that the user has three tries, right,
2:24:28 they have three guesses in order to guess the word.
2:24:31 And if they can't guess the word after three tries,
2:24:33 then they're going to lose the game.
2:24:35 But if they can guess the word inside of three tries,
2:24:38 then they'll win the game, right?
2:24:39 I think that would be a little bit more of a fun game.
2:24:42 So why don't we try to program that game?
2:24:44 Basically, we're going to set a limit
2:24:47 on the number of guesses that the user can have.
2:24:50 In order to do this, we're going to have
2:24:52 to create a couple more variables down here.
2:24:55 In other words, we're going to have
2:24:56 to store a couple more pieces of information.
2:24:59 The first piece of information I want to keep track
2:25:02 of is how many times the user has guessed, right?
2:25:05 So we can just make a variable called guess count.
2:25:08 And we'll just set this equal to zero because initially,
2:25:12 the user won't have guessed down in this while loop.
2:25:15 Every time we go through the loop, I want to increment that guess count, right?
2:25:21 So every time we've gone through this while loop,
2:25:24 I want to increment that count because that means the user will have guessed.
2:25:29 So down here, I'm going to say guess count plus equals one.
2:25:34 And this is just going to add one to the guess count, actually whoops.
2:25:40 Alright, so after each iteration of this loop,
2:25:43 we're going to go ahead and add one to the guess count.
2:25:46 Alright, so that's the first variable that we're going to need.
2:25:48 We're also going to want to store another variable.
2:25:50 And this is going to be called guess limit.
2:25:53 And basically, this is going to tell us
2:25:56 how many times the user can guess the word.
2:25:59 So I'm going to it's going to be guess limit.
2:26:02 And we'll basically just say three.
2:26:04 So let's say that the user has three tries
2:26:06 to guess the word three strikes and you're out.
2:26:08 So the user, if they can't get it in three tries,
2:26:12 then we're going to basically say that they lose the game.
2:26:15 And I'm also going to need one more variable here,
2:26:18 which we're going to call out of guesses.
2:26:22 And I'm just going to set this equal to false initially.
2:26:26 So this out of guesses variable is going to be a Boolean.
2:26:30 And it's going to tell us whether or not the user is out of guesses.
2:26:34 So if out of guesses is true, that means they have no more guesses, right?
2:26:38 They basically lost the game.
2:26:40 And if out of guesses is false,
2:26:42 that means that they still have some guesses left so they can keep playing.
2:26:46 All right, so let's use these different variables
2:26:49 in order to make our program more functional.
2:26:52 So the first thing I want to do is when I go through this loop,
2:26:56 I want to check to make sure that the user has more guesses, right?
2:27:01 In other words, before I let the user enter a guess,
2:27:05 I want to check to see that they haven't already used up all their guesses.
2:27:08 So down here, I can make an if statement.
2:27:11 I can say if and inside of the condition,
2:27:14 I want to check to see that guess count is less than guess limit.
2:27:21 If guess count is less than guess limit,
2:27:24 that means that they haven't guessed the total number of guesses that they have.
2:27:29 So they have some guesses left.
2:27:31 And if that's the case and if that's true,
2:27:34 then I'm going to go ahead and give them
2:27:37 a guess and then we'll increment the guess count.
2:27:40 If this isn't true, in other words, if they have reached their guess limit,
2:27:45 then I'm going to want to set out of guesses equal to true,
2:27:51 because they're out of guesses, right?
2:27:52 They have no more guesses because the guess
2:27:55 count wasn't less than the guess limit.
2:27:56 And so that means that they're out of guesses.
2:27:59 So they have no more guesses.
2:28:00 So there's actually one more thing we need to do inside of this while loop,
2:28:05 we need to add another condition on to this loop guard.
2:28:08 So right now, we're going to keep looping as long
2:28:10 as the guess is not equal to the secret word.
2:28:14 But remember, if the user's out of guesses, in other words,
2:28:18 if they've reached their guess limit,
2:28:19 we don't want them to guess anymore, right?
2:28:21 So if they ran out of guesses, then we want to basically break out
2:28:25 of this loop and not give them any more guesses.
2:28:27 So I'm going to go ahead and add another condition onto here.
2:28:30 And I'm going to say, while they haven't guessed the secret word,
2:28:34 and they're not out of guesses.
2:28:37 So it's going to be not out of guesses.
2:28:41 Then we're going to keep looping.
2:28:43 So as long as they haven't guess the word,
2:28:47 and as long as they still have some guesses left, we're going to keep looping.
2:28:51 But otherwise, we're going to break out of the loop.
2:28:54 And so down here, we're printing out you win.
2:28:56 But actually, when we break out of this loop,
2:28:59 there's going to be two possible scenarios.
2:29:02 So again, there's two possible ways that this loop could end, right?
2:29:07 Either the guesses equal to the secret word.
2:29:09 So either the user guessed the word correctly, or the user ran out of guesses.
2:29:15 And so there's two situations down here that we need to account for.
2:29:19 And so I'm going to use an if statement to figure out which is which.
2:29:22 So I'm going to say if, and we're just going to type out of guesses.
2:29:28 And if the user is out of guesses, then I want to print out of you lose.
2:29:36 So we're basically going to be like, you lost the game.
2:29:40 Otherwise, though, if they're not out of guesses,
2:29:42 that means that they guess the word correctly.
2:29:45 So we're just going to print out you win.
2:29:47 Alright, so now we have all the logic for this little guessing game setup.
2:29:51 And let's see if we can run it and play through it.
2:29:54 So I'm just going to run this program.
2:29:56 And now it's going to tell us to enter a guess.
2:30:00 So why don't we enter more guesses than we have?
2:30:02 So we're going to try to lose the game.
2:30:04 So I'm just going to enter one guess, two guesses.
2:30:06 And now I'm on my final guess.
2:30:08 So if I don't get it here, we should actually lose the game.
2:30:12 And you can see it says out of guesses, you lose.
2:30:14 So we weren't able to guess it in the number of tries that we had.
2:30:19 Let's run the program again, and we'll try to win the game.
2:30:22 So we'll get a couple wrong.
2:30:24 Let's say we're on our last guess.
2:30:26 And I'm like, Okay, I can do this.
2:30:28 So I type in the word and bam, we won the game.
2:30:32 So that's how we can basically create a game where we have a guess limit.
2:30:37 So this is a lot of code.
2:30:39 Let me walk you guys through this one more time so
2:30:41 you can just get a full understanding of what we're doing.
2:30:44 Up here, I created a few different variables.
2:30:46 We created this secret word variable, and we created this guess variable.
2:30:51 And then we also created some more variables.
2:30:54 So we created this guess count variable.
2:30:56 And this just keeps track of how many times the user has guessed the word.
2:31:01 And you can see down here,
2:31:04 every time we give them a guess, we're incrementing the guess count.
2:31:09 We also have guess limit.
2:31:11 And guess limit is telling the program how many times the user can guess.
2:31:15 So before I go through this while loop,
2:31:18 the first thing I'm doing is I'm checking to see
2:31:21 that the guess count is less than the guess limit.
2:31:24 In other words, do they still have some guesses left?
2:31:27 If they do, then we're going to get the input from the user.
2:31:31 Otherwise, we have this other variable up here called out of guesses.
2:31:36 And out of guesses is going to tell
2:31:38 us whether or not they have some guesses left.
2:31:40 So if this is equal to false, that means they have some guesses.
2:31:44 If it's equal to true, however,
2:31:46 it means no more dice, no more guesses, they are done.
2:31:50 So they lost the game.
2:31:51 The last thing we needed to do was specify an additional condition up here.
2:31:55 So we are going to keep looping through the code inside of this loop,
2:32:00 as long as the conditions up here are true.
2:32:04 So as long as they haven't guessed the word,
2:32:07 and as long as they're not out of guesses, we are going to keep looping through.
2:32:14 And so when eventually the user does break out of this loop,
2:32:18 there's going to be two possible situations.
2:32:20 The first situation is that they ran out of guesses.
2:32:22 And so we want to check to see if that's the situation.
2:32:25 I want to check to see if they're out of guesses.
2:32:28 If they are, we'll print out a lose message.
2:32:30 If they're not, then they must have guessed it correctly.
2:32:33 So they win.
2:32:33 And so that is how we can use while loops and if statements
2:32:37 and also variables in combination with each
2:32:39 other to build a pretty awesome guessing game.
2:32:47 In this tutorial, I want to talk to you guys about using for loops in Python.
2:32:52 Now a for loop is a special type of loop in Python,
2:32:55 which allows us to loop over different collections of items.
2:32:58 So a lot of times we'll use for loops
2:33:02 in Python to like loop through different arrays,
2:33:05 or we can loop over like the letters inside of a string,
2:33:09 or we could just loop through like a series of numbers.
2:33:12 So for loops provide a very specific purpose.
2:33:14 And the easiest way to kind of wrap your head around why for loops
2:33:19 are useful is just for me to show you guys a bunch of different examples.
2:33:21 So that's exactly what I'm going to do.
2:33:23 We're going to look at why for loops are awesome.
2:33:27 So down here in my text file and my Python file,
2:33:30 I'm going to write out a couple of different for loops.
2:33:33 So the way that we create a for loop is just by saying for.
2:33:37 And now what I want to do is specify a variable.
2:33:41 And this variable is going to essentially represent a different
2:33:46 value every time we go through this for loop.
2:33:49 And you guys will see how that works in a second.
2:33:53 But just know that this variable is going
2:33:55 to be used on every iteration of our for loop.
2:33:58 And each time it will most likely have a different value.
2:34:02 So in our case, I'm just going to call this letter.
2:34:06 And I'm going to say for letter in.
2:34:09 And now what I want to do is I want
2:34:12 to specify a collection that I want to loop over.
2:34:15 One example of this would be like a string.
2:34:17 So I could put a string in here.
2:34:19 I could just put like draft Academy.
2:34:21 And now I can just put a colon.
2:34:23 And so basically what this is going to say is
2:34:26 it's going to say for every letter inside of draft Academy,
2:34:30 I want to do something.
2:34:32 And so down here inside of this for loop and again, we need to indent this.
2:34:37 We can put what we want to do with each letter.
2:34:40 So let me just show you guys like basically what this is going to do.
2:34:43 So I can print out a letter so I can print out this letter variable.
2:34:47 And it's actually going to print out a different letter inside
2:34:51 of this draft Academy string on every single iteration of this loop.
2:34:56 So I'm going to run this program.
2:34:59 And down here inside of my console,
2:35:02 you'll see that I'm basically printing out draft Academy.
2:35:06 So on the first iteration of the loop,
2:35:08 I printed out the first letter in draft Academy, which was G.
2:35:12 On the second iteration of the loop,
2:35:14 I printed out I said, third was R, fourth was A.
2:35:19 So I'm essentially just looping through all
2:35:21 of the letters inside of draft Academy.
2:35:24 So I'm saying for each letter in draft Academy, I want to print out that letter.
2:35:30 And so this is kind of like how four loops can be used.
2:35:34 We can define a variable.
2:35:36 And that variable will change on each iteration of the loop, right?
2:35:40 So on the first iteration of the loop, this letter variable represented a G.
2:35:44 On the second iteration of the loop,
2:35:46 the letter variable stored at the value I, etc.
2:35:49 Right.
2:35:49 So we went through this entire string,
2:35:52 and we were able to print out each letter.
2:35:55 So in addition to using this with strings,
2:35:57 we can also use this with other collections, for example, like an array.
2:36:01 So if I created an array up here, let's just call it friends,
2:36:05 I'm going to set this equal to a bunch of different values.
2:36:08 So we can put like a list of our friends in here, we can say like Jim,
2:36:13 Karen, and Kevin, instead of saying the letter, why don't we call this friend?
2:36:20 And I can say for friend in friends.
2:36:24 And now we'll print out the friend.
2:36:27 So over here, I'm saying for each friend inside of this friends array,
2:36:32 I want to print out the friend.
2:36:34 So now we'll actually be able to print out each element inside of that array.
2:36:38 So down here, you'll see we're printing out Jim,
2:36:41 and then on the second iteration,
2:36:43 we're printing out Karen, and on the third, we're printing out Kevin.
2:36:46 So I'm able to loop through all of the values inside of the array.
2:36:49 And just so you guys know, like, you can name this variable, whatever you want.
2:36:54 So I could name, I could give this like any random name.
2:36:57 And if I want to access it, though, I have to access it using that same name.
2:37:03 So we can loop through something like an array,
2:37:05 we could also just loop through a series of numbers.
2:37:08 So I could say index over here.
2:37:11 And again, this can be anything.
2:37:13 I'm just going to call it index.
2:37:15 So we could say for index in range, and then in here, I can pass in a number.
2:37:20 So I could pass in like 10, for example.
2:37:23 And down here, I'm just going to print out the index.
2:37:26 When I run this program,
2:37:27 you guys will see that it's basically going to print out
2:37:30 every number in the range from zero to 10, not including 10.
2:37:34 So starting with zero, it's going to print out zero,
2:37:37 one, two, three, four, five, six, seven, eight, nine.
2:37:40 But notice that it didn't print out 10.
2:37:43 So it's essentially just printing out all
2:37:46 the numbers between zero and 10, not including 10.
2:37:49 You can also specify a range of numbers.
2:37:52 So I could say, for example, like three and 10.
2:37:55 And now this will print out all
2:37:58 the numbers between three and 10, not including 10.
2:38:01 So we're on this program.
2:38:03 And you can see we print out three,
2:38:06 four, five, six, seven, eight, nine, and not 10.
2:38:09 So whatever value that you put here in this second
2:38:13 position is not going to get included in the range.
2:38:17 And ranges can be really useful.
2:38:19 So for example, I could use a range to loop through an array,
2:38:23 just like we did before.
2:38:24 So if I wanted, I could say something like four index in range.
2:38:29 And now inside of this range, I can pass in the length of the array.
2:38:35 So just so you guys know,
2:38:37 if I wanted to get the length of this array, in other words,
2:38:41 if I wanted to figure out how many elements were inside of it,
2:38:44 I could just type out LEN and then inside parentheses, the name of the array.
2:38:49 And so this is going to spit out three,
2:38:52 because there's three elements inside of here, right?
2:38:55 Kind of makes sense.
2:38:56 So what I can do is I can say down here inside of this range function,
2:39:01 I can just type in LEN and then friends.
2:39:05 And so what this is going to do is it's going to essentially give
2:39:09 me a range between zero and the number of friends inside of this list.
2:39:14 So down here, I could actually type out friends index.
2:39:17 And this will allow me to access each individual friend inside of this list,
2:39:24 just like we did before.
2:39:25 But now I'm doing it with a range.
2:39:27 So you can see I'm typing out Jim, Karen and Kevin.
2:39:32 So actually, for each iteration through this loop,
2:39:35 it's basically going to be printing out friends zero,
2:39:40 friends one, and then friends two, because in here, we're passing in a three.
2:39:45 And remember, whenever I pass in there,
2:39:47 it's going to range from zero all the way up to that number,
2:39:50 but not including that number.
2:39:52 So that's another way that we could print out all the elements in the array.
2:39:56 And looping through something like an array is actually a very,
2:40:00 very common use case for for loops.
2:40:02 But like I said, we can use for loops
2:40:05 to loop through essentially any collection that we have.
2:40:07 So we could loop through like a string,
2:40:09 we could also loop through, you know, something like an array.
2:40:12 So this can be really useful.
2:40:14 And that's one of the basics of for loops in Python.
2:40:18 And I also want to show you guys one more example, just while we're here.
2:40:22 So why don't we go ahead and print out a range up to five, right?
2:40:27 So this will just be a simple program.
2:40:29 You can use all sorts of logic inside of these for loops.
2:40:33 So let's say that I wanted to do
2:40:35 something special on the first iteration of the loop.
2:40:37 So I could say, if index is equal to zero.
2:40:41 And if the index is zero, then we know it's the first iteration of the loop.
2:40:45 So I could do something special, I could like print out first iteration.
2:40:50 And then otherwise, we could just print like not first.
2:40:55 So this would be an example of like,
2:40:57 maybe you want to do something on the first iteration
2:40:59 of the loop and do something else on subsequent iteration.
2:41:03 So if I play this, you'll see only
2:41:05 on the first iteration is it printing this out.
2:41:08 And otherwise, it's printing out other stuff.
2:41:09 So, you know, don't be afraid to put
2:41:12 some complex logic inside of these for loops,
2:41:14 because it can really make your program is more powerful.
2:41:18 In this tutorial, I'm going to show you guys how to build an exponent function.
2:41:24 Now an exponent function is basically going to allow us
2:41:28 to take a certain number and raise it to a specific power.
2:41:32 So in Python, there's actually a really easy way to do this.
2:41:35 I can come down here and I can print,
2:41:38 we can say like two, and we can just do two multiplication signs.
2:41:42 And I can say three.
2:41:44 And this is basically just going to be two raised to the third power.
2:41:48 So you can see here,
2:41:50 I can just print this out and we get eight, because that's too cute.
2:41:54 So it's really easy to do exponents in Python.
2:41:57 But I want to show you guys how we can use something like
2:42:00 a for loop in order to create a function like this of our own.
2:42:05 So we'll actually create an exponent function that will basically
2:42:09 do this and we'll use for loops to do it.
2:42:12 So let's create a function.
2:42:13 So I'm just going to say def.
2:42:15 And now we need to give this function a name.
2:42:17 So why don't we call it raise to power.
2:42:20 And inside of here, I'm going to accept two parameters.
2:42:24 The first parameter is going to be the base number.
2:42:27 And then the second parameter is going to be the power number.
2:42:31 So we're going to take the base number and raise it to the power number.
2:42:36 So over here, we can just make a colon.
2:42:40 Now, inside of this function, we need to start writing some code.
2:42:44 Here's the thing.
2:42:44 Inside the function, we don't necessarily know the value of this pound.
2:42:49 Right.
2:42:49 For example, if we knew that we were just
2:42:52 trying to like square this number or cube this number,
2:42:56 I could just return like base num times base num, right?
2:43:00 That would square the number or I could cube the number base num.
2:43:05 So right.
2:43:06 So this would be like the number cubed.
2:43:08 The problem is though,
2:43:09 we don't know like right off the bat what this value is going to be.
2:43:12 Right.
2:43:12 This could change like the user can decide that.
2:43:15 So in order to write this function,
2:43:17 we're actually going to need to use a for loop.
2:43:19 And I'm going to show you guys how we
2:43:21 can use a for loop to basically figure this out.
2:43:24 So the first thing I want to do is create a variable.
2:43:26 And I'm just going to call it result.
2:43:28 And we're just going to set result equal to one.
2:43:31 So we're going to start this off at one.
2:43:34 Now I want to create a for loop.
2:43:37 So I'm basically just going to say four.
2:43:40 And over here, I basically want to specify like a index.
2:43:43 So I'm going to say index in, and now I want to specify
2:43:48 a range and a range will basically range us through a collection of numbers.
2:43:53 So basically, I want to multiply the base num
2:43:56 by itself as many times as the power num specifies.
2:44:00 Right.
2:44:01 So over here, I can just make this a range of power num.
2:44:06 Right.
2:44:07 And so we'll basically loop through this for loop as many times as pound them.
2:44:13 So if pound them is two, we'll loop through it twice.
2:44:16 If pound them is four, we'll loop through it four times.
2:44:18 And every time through the loop will multiply base num by itself.
2:44:21 Or actually, you'll see we're going to multiply it by this result.
2:44:25 So down here inside of this for loop,
2:44:28 I can basically say result is equal to result times base num.
2:44:33 This should actually give us everything we need
2:44:36 to take this number to the specific power.
2:44:40 So the actual like result, you know,
2:44:42 of doing the math is going to get stored inside of this result variable.
2:44:47 So the first time through the loop, we're just multiplying base num by one.
2:44:52 Right.
2:44:52 And so now result is going to become the same value as base num.
2:44:57 The second time through the loop.
2:44:59 So this would be if we were squaring the number,
2:45:01 we're basically multiplying result by base num again.
2:45:04 So it's essentially just base num times base num.
2:45:06 The third time through the loop, we're multiplying result times base num.
2:45:10 So we're essentially just like cubing the number.
2:45:12 So that's essentially what this for loop is doing for us.
2:45:15 So the last thing I want to do is right here below this for loop,
2:45:20 I'm just going to return the result.
2:45:22 So we'll be able to return whatever the result
2:45:25 of raising the number to the power was.
2:45:27 All right.
2:45:27 So let's come down after this function.
2:45:29 And we're just going to call it.
2:45:31 So I'm just going to say, actually, we'll print out the answer.
2:45:34 So I'm going to say raise to power.
2:45:37 And inside of these parentheses, remember, we can pass two numbers.
2:45:40 So let's raise three to the second power.
2:45:43 So we're just going to square three.
2:45:45 And we'll see how this works.
2:45:47 So throw on the program.
2:45:48 And you can see down here in the console we're getting nine.
2:45:52 Let's try something a little bit crazier.
2:45:54 Let's try three to the fourth power.
2:45:57 And we'll run this and get 81.
2:45:59 So that's three to the fourth power.
2:46:01 Let's try two to the third power.
2:46:03 And we're getting eight.
2:46:05 Yeah, so that makes sense.
2:46:06 So our raise the power function is working just as expected.
2:46:11 And I'll just walk you guys through it one more time.
2:46:14 So inside of this function, we're taking in two pieces of input,
2:46:17 we're taking in a base number, and we're taking in a power number.
2:46:20 So that's like, the base number is going to be taken to the power number.
2:46:24 I'm defining a variable here called result.
2:46:27 And result is where we're going to store the actual result of doing the math.
2:46:32 Now we specify this for loop.
2:46:34 And I'm basically saying that I want to loop through this range of numbers.
2:46:39 When I put pound on here, it's basically going to range from zero all
2:46:42 the way up to but not including the power number.
2:46:45 So we'll basically loop through power number of times, if that makes sense.
2:46:50 And then every time through the loop, we're just multiplying result by base num.
2:46:54 And then we're just returning results.
2:46:56 So that's the basics of building a power function.
2:46:59 And obviously, like if you're just trying to take
2:47:01 a number to a power and Python, it's really easy.
2:47:03 But this kind of shows you like the internal workings, right?
2:47:06 For a function like that to work,
2:47:08 we would have to actually type something out just like this.
2:47:13 And this tutorial, I want to talk
2:47:17 to you guys about two different concepts in Python.
2:47:19 It's going to be a pretty cool lesson.
2:47:21 First thing I want to show you guys though, are two dimensional lists.
2:47:25 So down here in our Python file, I just want to create a basic list.
2:47:30 So I'm just going to call this number grid.
2:47:33 And I'm just going to set this equal to a normal list right just like this.
2:47:38 Now normally if we were creating a list,
2:47:41 I could just put in a bunch of attributes and bunch of elements, right?
2:47:45 And now all of these elements are inside of this number grid list.
2:47:49 Everybody's happy.
2:47:50 But another thing I can do is I can make
2:47:54 all of the elements inside of this number grid list lists.
2:47:58 So the first item in the number grid list is going to be a list.
2:48:02 The second item in the number grid list is going to be a list, etc.
2:48:05 So I'm going to show you guys how to do that.
2:48:08 I can basically just come down here and where
2:48:10 I would normally put the first element in the list,
2:48:13 I can just create another array.
2:48:14 So I'm just going to make an open and closed square bracket.
2:48:17 And now I'm just going to say 123.
2:48:21 And I can create the next item in my list.
2:48:25 I can say 456.
2:48:26 And let's make another item in this list.
2:48:29 I can say 789.
2:48:31 And finally, we're going to put one more list inside of the number grid list.
2:48:36 And it's just going to have zero in it.
2:48:39 So inside of this number grid list, right,
2:48:42 this high level overall list, we have four elements.
2:48:46 And all of those elements are themselves lists.
2:48:50 So essentially what we're doing is we're creating a grid, right?
2:48:56 This grid has 1234 rows, and 123 columns, right, four rows, three columns.
2:49:04 And that's kind of like why these can be useful is we
2:49:07 can create a grid like structure inside of Python using two dimensional lists.
2:49:12 So this is a pretty cool list structure.
2:49:15 And I want to show you guys how we
2:49:18 can access individual elements inside of this list structure.
2:49:22 So, for example, let's say I wanted to print out one of these values.
2:49:26 Let's say I wanted to print out this one right here.
2:49:29 The way I can access it is by saying number grid.
2:49:33 And the first thing I want to do is make an open and close square brackets.
2:49:37 And in here, I want to put the index of the row that I want to access.
2:49:43 So this would be like row zero, because that's the zero element in the array.
2:49:47 This would be row one.
2:49:49 It's at index position one.
2:49:50 This would be row two, row three, etc.
2:49:53 So I could put row zero.
2:49:54 And now right next to this, I'm going to make another square bracket.
2:49:59 And I want to put the index of the column.
2:50:02 So this would be like column one, column two, column three.
2:50:06 So I'm going to put another zero,
2:50:07 because this is at the zero index of this first array.
2:50:11 So I just put zero zero here.
2:50:14 Now we can print this guy out to the screen.
2:50:17 And you'll see down here that we get that one.
2:50:20 So I could also do this for like for this eight, for example.
2:50:23 So this is going to be zero one two.
2:50:25 It's going to be row two and column one.
2:50:28 And now we should be able to print out that eight.
2:50:32 So that's basically how we can access elements inside of this 2D list.
2:50:37 The next thing I want to show you guys is a nested for loop.
2:50:42 And a nested for loop is a situation, like I said,
2:50:45 where we have a for loop inside of a for loop.
2:50:48 And I'll show you how we can use this nested for loop
2:50:51 in order to print out all the elements inside of this array.
2:50:54 So I basically show you guys how to parse
2:50:57 through a two dimensional list or a two dimensional array.
2:51:01 Alright, so basically we're just going to create a normal for loop.
2:51:04 So I'm going to say four, and I'm just going to say row in number grid.
2:51:08 And I'm using the word row here because
2:51:11 I'm kind of seeing these guys as rows, right?
2:51:14 These are sort of horizontal rows in our little grid.
2:51:18 So for each of those rows,
2:51:20 for each of the elements inside of number grid, I want to loop through.
2:51:25 So I could actually just print out each row right now.
2:51:27 And it's just going to print out all the rows inside of this list.
2:51:31 You can see down here, we basically just get exactly what we have up there.
2:51:34 But I want to be able to access each
2:51:37 of these individual attributes inside of each of these array elements.
2:51:42 So we can actually create another for loop.
2:51:44 And I'm going to put that for loop inside of here.
2:51:48 So now I'm going to say for column in row.
2:51:52 And basically what this is going to give us is
2:51:57 each individual column or each individual element inside of these arrays.
2:52:01 So for example, inside of the arrays that are the elements of the number grid.
2:52:06 Hopefully that makes sense.
2:52:07 So down here, now I can just print out column,
2:52:11 and this should actually print out every
2:52:14 single value inside of this two dimensional array.
2:52:17 So let's go ahead and run this program.
2:52:19 And down here, you'll see that we're basically
2:52:21 printing out one all the way down to zero.
2:52:24 So we're printing out all of the elements inside
2:52:27 of all of the arrays inside of the number grid.
2:52:31 So that's how we can use two dimensional lists and nested four loops together.
2:52:35 And both of those things are actually pretty handy in Python.
2:52:40 In this tutorial, I'm going to show you
2:52:44 how to build a basic translator in Python.
2:52:47 So essentially, we can take in a string so
2:52:50 we can take in like a phrase or a word,
2:52:54 and we'll be able to translate it into a different language.
2:52:57 So over here, I have this little file,
2:53:00 and it basically specifies this language that I made up.
2:53:03 So I'm calling it the draft language.
2:53:05 And in the draft language, all vowels become G.
2:53:09 So if I was going to translate an English word into a draft word, for example,
2:53:15 the word dog, I would look through the word
2:53:18 dog in any instances where there was a vowel.
2:53:21 So in our case, let's just say A E I O U, I would convert that vowel into a G.
2:53:27 So dog would map to D G G, cat would get mapped into C G T.
2:53:33 So those are the basic translation rules.
2:53:35 Obviously, this is a pretty simple language,
2:53:37 but you get the point any vowel is going to become a G,
2:53:41 and then we'll get the draft language.
2:53:43 So we're going to build a draft translator.
2:53:46 Over here in our Python file, let's start making this translator.
2:53:50 The first thing I'm going to do is just define a translate function.
2:53:54 So I'm going to make a function here, and I'm just going to call it translate.
2:53:59 And inside of these parentheses, we want this to take one piece of information,
2:54:04 which is going to be the phrase that we want to translate.
2:54:07 So I'm just going to say phrase.
2:54:09 And now inside of this function, we need to figure out like,
2:54:14 okay, how can we translate English into our draft language?
2:54:18 Well, remember, the rules is basically just any vowels become G's.
2:54:23 So the first thing I want to do is create a variable.
2:54:25 And this variable is going to be the translation.
2:54:28 So this will be like the final result that we're going to return to the user.
2:54:32 So I can say translation.
2:54:33 For now, I'm just going to set it equal to the empty string.
2:54:38 So this is just going to be like a completely empty string.
2:54:40 It's not going to be doing anything.
2:54:43 And now what we want to do is we basically
2:54:46 want to loop through every letter inside of this phrase.
2:54:49 And if it's a vowel, we want to change it to G.
2:54:53 And if it's not a vowel, we want to leave it alone.
2:54:55 And we're essentially going to be looping through phrase.
2:54:58 And we're going to be adding the letters onto this translation one by one.
2:55:02 So let me show you guys how we can do this.
2:55:05 I'm going to make a for loop.
2:55:06 So I'm going to say four.
2:55:08 And I'm just going to say letter in phrase.
2:55:10 And so now, when I loop through here,
2:55:15 I can access each individual letter inside of the phrase that they passed in.
2:55:21 So what I want to do is I want to check
2:55:24 to see if the letter is a vowel or not, right?
2:55:27 If it's a vowel, then we can add a G onto translation.
2:55:31 If it's not a vowel,
2:55:32 then we can just add on whatever letter it was onto translation anyway.
2:55:36 So I'm going to use this if statement.
2:55:39 I'm going to say if.
2:55:41 And there's actually this special thing we can do in Python,
2:55:44 we can check to see if something is in something else.
2:55:47 So I can say if letter in, and over here,
2:55:51 I'm just going to type out all the vowels.
2:55:54 So we'll say A E I O U and A E I O U.
2:55:58 So I'm basically checking to see if the letter is inside of this string.
2:56:03 And if the letter is inside of here, then we know that it's a vowel.
2:56:08 So down here, we can handle that case.
2:56:11 So I can say, translation is equal to translation plus G.
2:56:17 Because if this letter is a vowel, we just want to convert it into a G.
2:56:26 Otherwise, though, I can basically just say,
2:56:30 translation is equal to translation plus letter.
2:56:33 So in the case where we have a vowel, I'm adding a G onto translation.
2:56:39 In the case where we don't have a vowel,
2:56:41 I'm just adding on whatever letter it was onto translation.
2:56:45 So finally, down below this for loop, we just want to return the translation.
2:56:51 So I'm just going to say return translation.
2:56:54 And now we have a awesome translate function, and it should probably work.
2:56:59 So let's come down here and we'll test it out.
2:57:02 So why don't we allow a user to input some information.
2:57:05 So I'm basically going to call this function.
2:57:08 Actually, we're going to print it out.
2:57:10 So we'll print this out.
2:57:11 And I'm going to call this function translate.
2:57:13 And inside of here, I'm actually just going to pass whatever the user inputs.
2:57:20 So I can just say input.
2:57:22 And inside of these parentheses, I can say the prompt.
2:57:26 So enter a phrase.
2:57:28 And so now I'm basically combining all these statements together.
2:57:32 I'm saying I want to print out the translation of whatever the user enters in.
2:57:38 So let's go ahead and run this program, and we'll see how we do.
2:57:43 So I'm going to run the program.
2:57:45 It says enter a phrase.
2:57:47 Actually, let me format this a little bit better.
2:57:50 Okay.
2:57:51 All right.
2:57:52 So it says enter a phrase.
2:57:55 So let's go ahead and do that.
2:57:57 So let's just type in the example we used before.
2:58:00 I'm going to say dog.
2:58:02 And so now out pops the answer, which is d g g.
2:58:06 So that's kind of cool.
2:58:07 And we can just keep running this as many times as we want.
2:58:10 So I could type in like to be or not to be.
2:58:13 So this is like a little bit longer of a string.
2:58:15 And you can see it translates it into our draft language.
2:58:19 So that's basically how we can create our little translator app.
2:58:23 And that's pretty cool.
2:58:25 And so you can see we're using a for loop in combination with an if loop.
2:58:30 And that's a really powerful structure.
2:58:31 And actually, there's one place over here where
2:58:34 we can make this a little bit more efficient.
2:58:37 So instead of saying if letter in this string, you'll notice here,
2:58:41 I'm checking to see if it's in all the lower case and the uppercase.
2:58:46 I could actually just say if letter dot lower in.
2:58:50 And now I only have to type out the lower case letters.
2:58:53 And it's still going to do the same exact thing.
2:58:56 So that's a little like hack or whatever that we could use.
2:58:59 And there's also one more problem with this program.
2:59:01 So you'll notice down here,
2:59:03 we're setting the translation equal to the translation plus g.
2:59:06 But the problem is if I was to start
2:59:09 my word inside of the phrase with a capital vowel.
2:59:13 So in other words, if I said like
2:59:15 on, you'll notice that when this gets printed out,
2:59:18 it gets printed out as lowercase g and so it's not keeping our uppercase syntax.
2:59:24 And that's kind of a problem.
2:59:26 So we can actually use another if statement in here.
2:59:28 And we can check to see if letter dot is upper.
2:59:35 And if the letter is uppercase,
2:59:39 then we can just set the translation equal to the translation plus a capital G.
2:59:47 Otherwise, we can just set it equal to the lowercase g.
2:59:51 And so now we're able to control both of those situations.
2:59:55 So let's just test this and see if it works.
2:59:59 Enter a phrase, say on.
3:00:00 And now we're keeping that capital letter.
3:00:03 So those are just a couple different ways
3:00:05 that we could make this program a little bit better.
3:00:08 But the basic concept is we have a for loop
3:00:10 and then we have some if loops inside of it.
3:00:12 And we're calling this awesome function that can translate a word for us.
3:00:17 In this tutorial, I want to talk to you guys about comments in Python.
3:00:23 This is going to be a pretty short tutorial.
3:00:25 I just want to give you guys an overview of what
3:00:28 comments are and how we can use them in our Python programs.
3:00:32 So over here in my Python program, I just have this line here.
3:00:36 It says print comments are fun.
3:00:38 So if I run my program down here inside of the console,
3:00:41 it's going to print out that comments are fun, right?
3:00:44 So any code that I put over here is going to get executed by Python.
3:00:49 But there's actually a special line of code
3:00:52 that we can write and it's called a comment.
3:00:55 And a comment is basically a line inside of our Python
3:00:59 file that's just not going to get rendered by Python.
3:01:01 So when Python sees it, it's just going to ignore it.
3:01:04 It's not going to execute it.
3:01:05 It's not going to print anything out.
3:01:07 It's just going to kind of ignore it.
3:01:09 And comments are going to be just used for us humans.
3:01:12 So a comment is used for like me or another developer to write a little comment,
3:01:17 a little like plain text, you know, comment inside of a file.
3:01:21 In order to create a comment, I could use this hashtag symbol.
3:01:25 So I can click hashtag and now I am writing a comment.
3:01:28 So I can write whatever I wanted in here.
3:01:31 I could say like this program is cool.
3:01:35 And you'll notice when I put on the program,
3:01:38 that doesn't show up anywhere, right?
3:01:40 It doesn't matter what I put after this hashtag.
3:01:42 This is just like ignored by Python.
3:01:45 It's just for me or another developer to come in here and use.
3:01:48 So a lot of times if you want to write
3:01:50 like a little note inside of your Python files,
3:01:52 or maybe you can write yourself a little reminder,
3:01:55 or if you want to write a line to like explain a line of code.
3:01:59 So for example, I could write a comment that would explain this print statement.
3:02:03 So I could say like this prints out a string, right?
3:02:07 Now anybody looking at my file would be able to read this and they'd be like,
3:02:11 Oh, that's what this does.
3:02:12 Okay, cool.
3:02:13 So comments are useful for leaving little comments in a Python file.
3:02:18 If you want to make comments on multiple lines,
3:02:20 you can just create a new line and use another hashtag.
3:02:23 And so then I could type out whatever my next comment was.
3:02:26 And generally, if you're going to put comments on multiple lines,
3:02:29 you want to just use multiple hashtags.
3:02:31 But there's actually another way that you can write out comments.
3:02:35 So you can use a triple quotation mark.
3:02:38 So I'm typing out the single quote.
3:02:40 And I'm typing it out three times.
3:02:42 So you can see here up here, we have this three times, we have this three times.
3:02:48 So in here, I can, you know, write any comment that I wanted.
3:02:51 So I could put, you know, as much text up here as I would want.
3:02:54 And same thing, when I run this program, it's just not going to get rendered.
3:02:59 So this is another way to make multiple line comments.
3:03:04 But the, like, official Python, like style guides or whatever,
3:03:08 just recommends that if you're going to put comments on multiple lines,
3:03:12 you use these hashtags.
3:03:13 So, you know, you can really do whatever you want.
3:03:15 But if you want to be like super Python official, then use these hashtags.
3:03:19 Comments can also be useful for doing
3:03:22 something called commenting out a line of code.
3:03:25 A lot of times when you're writing a program,
3:03:27 you might have a line of code in there
3:03:30 that is maybe like potentially messing stuff up.
3:03:33 And so what you can do is you can actually comment out that line of code.
3:03:37 So I could put a comment in front of this line of code.
3:03:40 And now when I run my program, the program is going to run,
3:03:44 but it's not going to use this line.
3:03:46 So sometimes if you're trying to see if
3:03:48 like a line of code is causing you trouble
3:03:50 or if you want to see what your program would be like without a line of code,
3:03:54 or even like a block of code, you can just comment it out.
3:03:57 And that way you're not actually deleting the code from your file.
3:04:00 You're just commenting it out.
3:04:02 So Python is going to ignore it.
3:04:04 And those are really the two uses for comments is to write little notes,
3:04:08 write little comments about different things or to comment out code.
3:04:12 And as you go forward in Python, I'm sure you'll find uses for comments.
3:04:17 In this tutorial, I want to talk to you guys about catching errors in Python.
3:04:25 Now, a lot of times when we're running Python programs,
3:04:28 you'll encounter different errors.
3:04:29 So different situations could come up and your program
3:04:32 might throw an error or it might throw an exception.
3:04:34 And a lot of times when these situations happen,
3:04:37 they'll completely stop your program from running.
3:04:39 And what we can actually do is we can watch out for, you know,
3:04:44 certain specific errors that are going to pop up
3:04:47 in our program and we can actually handle them.
3:04:49 So instead of our program just breaking and stop executing,
3:04:52 we can actually handle those errors and do things when they occur.
3:04:56 So I want to show you guys an example of using this down here.
3:04:59 I'm saying number is equal to int input enter a number.
3:05:03 Basically what's happening is I'm prompting the user
3:05:05 to enter in a number using this input command.
3:05:08 And then I'm converting whatever they entered into an integer.
3:05:11 So as long as they enter in like a valid integer,
3:05:15 I can convert it into an integer.
3:05:16 So I can convert it into like a whole number and I can
3:05:20 store it inside of this number variable that I'm just printing it out.
3:05:23 So let's try to execute this program.
3:05:24 I'm going to run the program and you'll see down here it says enter a number.
3:05:28 So we can enter in like four, for example.
3:05:31 And when I enter in four,
3:05:32 everything's going to work and it's just going to print
3:05:35 out four and everything is right in the world.
3:05:38 And actually, I'm going to modify this up here.
3:05:40 So it's a little easier to see.
3:05:42 But let's say we run the program again.
3:05:44 So it says, okay, enter your number.
3:05:45 Let's say that I break the rules and I don't enter a number.
3:05:48 So I just ignore what the prompt says and I put,
3:05:52 you know, some random text in here.
3:05:54 Now when I enter this, the program is going to throw an error.
3:05:58 You can see down here,
3:05:59 it says value error invalid literal for int base with base 10.
3:06:03 Basically, hey, you didn't enter in an integer like you said you would, right?
3:06:08 We told them to enter in a number.
3:06:10 They entered in text.
3:06:11 So it broke the program.
3:06:13 And this is a situation that happens a lot.
3:06:16 So there's a lot of different situations where
3:06:18 something can just go wrong in our program.
3:06:20 And I'm until now we've just kind of accepted it as a reality.
3:06:23 But if you're writing a real live Python program,
3:06:26 you don't want something like this to trip up your program.
3:06:29 You don't want the entire program to break when
3:06:31 somebody just forgets to enter in a number, right?
3:06:33 You want to be able to handle all these different situations.
3:06:36 And in order to handle them, we can use something called a try except block.
3:06:41 And a try except block will basically allow
3:06:43 your program to try out a piece of code.
3:06:46 And if everything goes well, then we're great.
3:06:49 Like we could try out entering in the number.
3:06:51 And if the user enters in the correct number, great.
3:06:54 But if they don't enter in the correct number,
3:06:56 then we can basically like account for that.
3:06:58 So they entered in something like a string
3:07:00 when they're supposed to enter in a number,
3:07:02 we could tell them like, Hey, you entered in the wrong thing.
3:07:05 So I'm going to show you guys how we can use these try except blocks.
3:07:08 I'm going to come up here, and I'm just going to say try.
3:07:11 I'm going to make it colon.
3:07:12 And I want to indent both of these things.
3:07:15 So both of these things are going to be inside of this try block.
3:07:19 Now I'm going to make a new line.
3:07:20 I'm going to come down here and I'm just going to type out.
3:07:23 Except, and I'm also going to put a colon here.
3:07:26 And down here, I'm just going to print something out.
3:07:28 So I'll just say like invalid input.
3:07:31 So now, since I put this code inside of this try block,
3:07:36 if the user enters in something wrong, it's going to be able to catch it.
3:07:39 So it'll be able to catch what the user
3:07:42 does wrong and print out invalid input onto the screen.
3:07:45 So let's go ahead and run our program.
3:07:47 And it's just going to say enter a number.
3:07:49 So we're going to ignore that.
3:07:51 We'll enter this in.
3:07:52 And now instead of yelling at us and breaking the program,
3:07:55 it just says, Hey, invalid input.
3:07:57 So it was able to handle the error they got called.
3:08:00 It was able to handle the invalid input error.
3:08:03 Basically just told us, Hey, no more input.
3:08:06 So this is a really powerful thing that we can do in our Python programs.
3:08:11 Basically just to protect our programs, right?
3:08:13 If I want to have my program running for long period of time,
3:08:17 I can't have something as simple as an invalid input, trip it up.
3:08:20 So I can catch that invalid input using these try accept blocks.
3:08:24 But here's the thing.
3:08:25 You'll notice that this little guy down here is getting highlighted.
3:08:28 And let's just see what it has to say.
3:08:31 So it's basically saying to broad exception clause.
3:08:34 And this is a message that we're getting because when I just say accept,
3:08:38 this will catch any error under the sun.
3:08:41 So anything that can go wrong on my program for the most part,
3:08:44 this is going to accept it.
3:08:45 So let me give you guys an example.
3:08:47 Let's say I had in my program, I was creating a variable, right?
3:08:51 So I wanted to create a variable called value.
3:08:54 And I set it equal to 10 divided by zero.
3:08:57 Now, if you know anything about math,
3:08:58 you'll know that you can't divide a number by zero.
3:09:01 It's not possible.
3:09:02 So the program isn't going to be able to handle this.
3:09:05 And you'll see that this is going to throw an error for us.
3:09:07 So when I run the program,
3:09:09 it basically says zero division error division by zero.
3:09:12 We're not allowed to do that, right?
3:09:14 If I took this and I put this down inside of this dry block,
3:09:18 and I just pasted it in here,
3:09:21 it's going to get caught by this exception that we wrote down here.
3:09:25 So it's going to get caught by that accept.
3:09:27 So when I run the program, you'll see it says invalid input.
3:09:31 But the problem is that we didn't input something that was invalid, right?
3:09:36 We just divided by zero, but it wasn't able to handle that.
3:09:39 Right.
3:09:40 So it could not divide by zero it through an error.
3:09:43 So even though we didn't get an invalid input,
3:09:45 even though it was because we divided by zero, it's still printed this out.
3:09:49 And this brings me up to another point is we can actually catch,
3:09:53 or we can accept specific types of errors.
3:09:56 So for example, in this case, we got a division by zero error.
3:10:00 In this case, we got an invalid input error, right?
3:10:03 We put something in that was wrong.
3:10:05 You couldn't convert the value.
3:10:06 So I can create two different accept
3:10:08 blocks to catch two different types of errors.
3:10:11 So down here, I can just make a space and I
3:10:14 can specify the type of error that I want to catch.
3:10:17 So when we divided by zero, it was zero division error.
3:10:20 So if I just put zero division error in here,
3:10:24 now it's going to catch that zero division error.
3:10:27 So I can say divided by zero.
3:10:30 I could also make another accept.
3:10:32 So I could say accept.
3:10:34 And we can catch the other exception that we got.
3:10:37 So I think it was a value error, right?
3:10:39 It was, we put in the type of value inside of that input.
3:10:44 So now we can print out invalid input.
3:10:46 So with these two accept blocks,
3:10:49 we're basically able to handle both of these situations.
3:10:52 So for example, if this ends up breaking the program,
3:10:56 this division by zero, then it'll be able to catch that.
3:10:59 So down here, it'll tell us, Hey, you divided by zero.
3:11:02 But if I was to get rid of this line of code,
3:11:06 and I just entered in the number wrong.
3:11:08 So I came down here and I entered in a string instead of a number.
3:11:11 Now it's going to tell us invalid input.
3:11:13 So we're going to be able to specify what happens when certain things break.
3:11:17 So when we get a valid, when we get a value error, we can say invalid input,
3:11:22 when we get a zero division error, we can say divided by zero.
3:11:26 So depending on what breaks, depending on what happens,
3:11:29 we can actually do different things.
3:11:31 And I want to show you guys one more thing we can do,
3:11:35 which is we can actually store this error as a variable.
3:11:38 So I can say accept, I can say as, and I can type in a name here.
3:11:43 So a lot of times people will just say like, e r r.
3:11:47 And down here, I can print out the actual error.
3:11:50 So I can just print e r r.
3:11:52 And so if we end up dividing by zero.
3:11:55 So if I say answers equal to 10 divided by zero,
3:11:59 we're going to throw this error, and it's going to print out what went wrong.
3:12:04 So now I can run this program.
3:12:06 And it just says division by zero.
3:12:08 So it just printed out the error that we got.
3:12:11 So that's another useful thing you can do is you
3:12:14 can actually like print out the specific error that got thrown.
3:12:17 Now, a best practice in Python is to use these specific errors.
3:12:20 So you always want to accept and accept for a specific error.
3:12:24 What you don't want to do is just say accept,
3:12:28 and then just accept anything under the sun.
3:12:32 It's just too broad.
3:12:34 And it's not a good practice in Python.
3:12:36 So you always want to be catching specific errors like this.
3:12:39 In this tutorial, I want to talk to you
3:12:44 guys about reading from external files in Python.
3:12:48 Now a lot of times in Python,
3:12:50 you're going to want to read from files that are outside of your Python file.
3:12:54 So you might want to read information from like a text
3:12:58 file or a CSV file or like an HTML file.
3:13:01 And you can actually use something called the Python read command.
3:13:05 And it will allow you to read a file that is stored outside of your Python file.
3:13:11 So you can use these files to get information
3:13:14 or you can parse through different files and do different things.
3:13:17 So I'm going to show you guys the basics of reading files,
3:13:21 opening files, closing files, doing all that stuff.
3:13:24 Over here, I have this file called employees dot text.
3:13:28 And it basically just lists out a bunch of different employees,
3:13:32 like these could be employees in an office or whatever.
3:13:35 So it's just listing out all of this information.
3:13:38 So let's say that inside of my app dot Python file,
3:13:42 I wanted to read the employees inside of that file.
3:13:45 The first thing I have to do is actually open that file from inside of Python.
3:13:50 So I can use a special command called open.
3:13:53 So I can say open.
3:13:55 And then in here, I can type in the name of the file that I want to open.
3:13:59 So this is either going to be a relative path to the file,
3:14:02 an absolute path to the file,
3:14:04 or just the files name if both files are in the same directory.
3:14:08 So in my case, app dot Python and employees dot text are in the same folder,
3:14:14 like they're in the same directory.
3:14:16 So I can just type out the name of the file.
3:14:19 I can just say employees dot text.
3:14:21 And then I want to put one more thing inside of this open function.
3:14:25 And it's going to be the mode that I want to open the file in.
3:14:29 So you can actually open files in a couple of different modes.
3:14:32 And the first mode is called read.
3:14:34 So I can just put an R here and that's going to stand for read.
3:14:38 And this basically means that I only
3:14:40 want to read the information inside the file.
3:14:42 I don't want to modify it.
3:14:43 I don't want to change it.
3:14:44 I just want to read it.
3:14:45 I just want to see what's in the file and do some stuff with that information.
3:14:50 Another mode is called write.
3:14:52 So I can type in this w and writing
3:14:54 basically means that you can change the file, right?
3:14:57 You can write new information.
3:14:59 You can change existing information.
3:15:01 There's another one called a and a stands for append.
3:15:04 And this basically means that you can
3:15:07 append information onto the end of the file.
3:15:09 So you can't modify any of the information in the file.
3:15:13 You can't change any of the information, but you can add new information.
3:15:16 And there's one more, which is R plus.
3:15:19 And this basically means read and write.
3:15:21 So this will give you all the power of reading and writing.
3:15:25 So in our case, we're just going to be working with regular R.
3:15:30 So we're going to be reading from the file.
3:15:33 Now this open function will essentially just open the file.
3:15:36 So it's going to like go over to that file inside of our file system,
3:15:40 open it up, and it'll allow us to read through it.
3:15:43 But generally, we're going to want to store
3:15:46 this opened file inside of a variable.
3:15:48 So I can create a variable and we can just call it employee file.
3:15:52 And I'm just going to set it equal to this open function.
3:15:56 So now the open employees dot text file and all the content
3:16:01 inside of it is stored inside of this employee file variable.
3:16:06 Now, whenever you open a file,
3:16:08 you always want to make sure that you close the file as well.
3:16:12 So just like we have this open command, we also have an close function.
3:16:16 So I can come down here and say employee file dot closed.
3:16:21 And this is essentially just going to close the file.
3:16:25 So we're no longer going to be able to access it.
3:16:28 And generally, it's a good idea whenever you're opening up a file,
3:16:31 you want to also make sure that you're closing the file at some point.
3:16:35 So generally, once you're done reading it, you can just close it.
3:16:38 So that's kind of like how we can open and close a file.
3:16:41 Now, let's talk about how we can get information from the file, right?
3:16:44 There's no point in having the file that we can't figure out what's in it.
3:16:47 So there's actually a few different functions that we can use
3:16:50 on this employee file to figure out what's inside of it.
3:16:53 And I'm going to show you guys some of those.
3:16:55 So I'm just going to make a print statement.
3:16:57 And inside this print statement will basically just
3:17:00 print out some information about the employee file.
3:17:03 So the most basic thing we can print
3:17:06 out is just the entire contents of the file.
3:17:09 But before I do that, I want to show you guys
3:17:11 how you can check to make sure that a file is readable.
3:17:14 So before we do anything else, generally,
3:17:16 it's a good idea to make sure that it's possible to read this file.
3:17:21 And there's a function inside of Python, we can use called readable.
3:17:25 So I'm just going to type out employee file dot readable.
3:17:30 And this is going to return a Boolean value.
3:17:34 And it's going to tell us whether or not we can read from this file.
3:17:38 So I'm going to run my program.
3:17:40 And you'll see down here, we get a value of true.
3:17:43 And that's because we set the file with a read mode.
3:17:46 So it's in read mode, we can read from it.
3:17:48 If I was to put a double you here.
3:17:51 So if I put like, right, now readable is going to be false,
3:17:55 because we can no longer read the file, we can only write to the file.
3:17:58 So I'm going to change this back to our so we can just read it.
3:18:03 So once you figure out whether or not
3:18:05 the file can be read from, let's actually read it.
3:18:09 So there's another function called employee file dot read.
3:18:11 And this is basically just going to spit out all the information in the files.
3:18:15 So when I run the program, it's just going to spit out all of this information,
3:18:19 right, all the information that was in that file.
3:18:22 I can also come down here and we can read an individual line inside this file.
3:18:27 So I can say employee file dot read line.
3:18:29 And what this is going to do is it's
3:18:32 going to read an individual line inside of this file.
3:18:35 So now when I run this program,
3:18:37 you'll see it's just reading that first line in the file.
3:18:40 And this read line function is actually just reading the first line.
3:18:44 And then it's basically like moving a little cursor onto the next line.
3:18:48 So if I was to copy this code and then print it again down here,
3:18:53 I'm saying employee file dot read line.
3:18:55 So it's going to read the first line.
3:18:57 And then when I say it again, it's going to read the line after that.
3:19:01 So this is actually going to end up
3:19:02 printing out the first two lines in the file.
3:19:04 So when I run this program,
3:19:06 you'll see we print out Jim salesman and Dwight salesman.
3:19:09 So if I was to do this multiple times,
3:19:12 like I could technically print out every line inside of this file.
3:19:16 And you can see we can do that.
3:19:19 And so that can be pretty useful for reading multiple lines in a file.
3:19:23 But there's actually another function that is better at doing that.
3:19:26 And we can say instead of employee file dot read line,
3:19:29 we can say dot read lines.
3:19:31 And what this is going to do is it's going to take all
3:19:34 of the lines inside of our file and put them inside of an array.
3:19:37 And so now when I print this out, you'll see we have this array down here.
3:19:41 It says Jim salesman, that's the first item in the array, Dwight salesman,
3:19:45 the second item in the array,
3:19:47 it's basically taking each line and putting it inside of an array.
3:19:51 So if I wanted to access a specific line,
3:19:53 I can just refer to it by its index in the array.
3:19:56 So if I said one, now this is going to give us that Dwight salesman line,
3:20:00 because that is at index position one inside of the array.
3:20:04 You can also use this read lines function with a for loop.
3:20:08 So I can come up here and create a for loop.
3:20:12 I'm just going to say for, and we'll say employee in employee file.
3:20:17 And then for each employee, we just want to print them out.
3:20:21 So and actually started, we have to put employee file dot read lines up here.
3:20:26 And so now this will loop through all
3:20:29 of the employees in this employee file that read lines array.
3:20:32 So we can actually just print out the individual employee.
3:20:36 And now this will print out all the employees inside of that file.
3:20:40 So it's basically printing out each line in the file.
3:20:43 And that can be pretty useful.
3:20:45 So you can use all of these different functions like read,
3:20:48 read line, read lines, readable, you know,
3:20:51 there's a bunch of these different things that we
3:20:54 can do to get information from a file.
3:20:56 And so there's a lot of cases where you're going
3:20:57 to want to be able to parse through information in a file.
3:21:00 And this is a awesome way to do it.
3:21:02 So just to recap, whenever you want to open a file and read from it,
3:21:05 you can just use this open function,
3:21:07 type in the name of the file and then the mode,
3:21:09 which in our case is going to be our, then
3:21:12 you can do all sorts of stuff with it.
3:21:14 And you always want to make sure that you close it when you're done.
3:21:17 That's just good practice.
3:21:18 So that's the basics of reading from files.
3:21:20 And hopefully you guys can use this in some way, shape or form in the future.
3:21:26 In this tutorial, I want to talk to you
3:21:30 about writing and appending to files in Python.
3:21:34 So one of the cool things about Python
3:21:36 is it allows you to work with external files.
3:21:39 So I could have like an external text file and I
3:21:43 could actually like completely read all of the information in it.
3:21:46 I could parse through it, use that information to do certain things.
3:21:49 But in addition to reading a file, I could also write a file.
3:21:53 And that's what I want to talk to you guys
3:21:55 about today is writing new files and appending onto existing files.
3:21:58 Over here, I basically just have some written out
3:22:02 and this essentially just reads information from this employees.text file.
3:22:06 So you can see over here, I'm specifying the mode,
3:22:09 which is R and that stands for read.
3:22:11 And then down here, I'm just reading all of the contents
3:22:14 of the file and spitting it out on the screen.
3:22:16 So I'm going to click the play button
3:22:18 over here and you'll see that this executes.
3:22:21 So it's printing out all of the lines of code in our text file.
3:22:26 So over here, I'm in this employees.text
3:22:28 file and it just has all this information, like employees in an office.
3:22:32 But let's say that I wanted to add another employee onto here, right?
3:22:36 Let's say that a new employee joined our company.
3:22:39 So we wanted to add them onto this list.
3:22:41 Well, I can come over here to my app.python file.
3:22:44 And instead of reading from the file, I want to append to the file.
3:22:49 So I want to say a and appending to the file basically
3:22:53 means that you're adding some text at the end of the file.
3:22:57 So wherever the file ends, you're just going to add some text onto there.
3:23:00 So what we can do is we can actually add another employee into the file.
3:23:05 So instead of printing something out,
3:23:07 I'm actually just going to say employee file dot right.
3:23:12 And when I say employee file dot right,
3:23:15 I'm going to be able to write something to the end of the file.
3:23:19 So I can basically just write whatever I want.
3:23:22 So why don't we add in another employee into our employees dot text file.
3:23:26 So we can add in another employee.
3:23:28 Why don't we say like Toby, and he's going to be in human resources.
3:23:35 So now, when I run this program,
3:23:38 it's going to add Toby human resources onto the end of the file.
3:23:42 So I'm going to run my program.
3:23:44 And you'll see that nothing shows up down here in the console.
3:23:47 But if I go over to my employees dot text file,
3:23:51 all of a sudden we have a new entry over here,
3:23:54 it's Toby from human resources, right?
3:23:56 So I was able to append a line onto the end of this file.
3:24:00 But here's the thing, you need to be careful when you're writing to files,
3:24:05 because you can actually mess up a file very easily.
3:24:07 For example, I already added Toby here into my file.
3:24:10 But if I was to run this program again,
3:24:13 you'll see that over here in this employees dot text file,
3:24:17 it went ahead and added Toby again, right?
3:24:20 So it added this employee here again.
3:24:22 And also, you'll notice that in this case,
3:24:25 this employee didn't go on to the next line.
3:24:28 I accidentally ran my file again,
3:24:29 and all of a sudden it messed up this file over here.
3:24:33 And so appending, you really need to be careful because
3:24:36 if you actually run your file again, or if you,
3:24:39 you know, append something on something wrong to the file,
3:24:42 it's permanent, like it's getting saved inside of the file.
3:24:45 So I want to talk to you guys a little bit more about, you know, appending.
3:24:50 Another thing we could do, let's say we wanted to add another employee.
3:24:53 And you'll notice over here in this employees dot text file.
3:24:56 When I appended it on again,
3:24:58 it got appended to the end of the existing line, right?
3:25:01 So the first time I had a new line there,
3:25:04 but if you don't have a new line at the end of your file,
3:25:07 and you all and you want to add a new line,
3:25:09 you're going to have to add some special characters.
3:25:11 So let's add another employee, and we'll call her Kelly,
3:25:15 and let's just say Kelly is in customer service.
3:25:19 So Kelly is going to be in customer service.
3:25:25 And if I want to add this employee onto the end of the file in a new line,
3:25:30 I'm going to have to put a new line character in front of it.
3:25:33 So I can say backslash n,
3:25:35 and this will append this entry into the file with a new line.
3:25:38 So on a new line.
3:25:40 So now when I run this, you'll see we
3:25:42 get Kelly from customer service on her own line.
3:25:45 So you want to make sure that you're aware
3:25:48 of like these special characters that you can use,
3:25:50 they call them escape characters.
3:25:52 And you know, anytime you're adding onto a file,
3:25:54 you want to make sure that you're adding on exactly where you want to add on.
3:25:58 So in addition to appending to a file,
3:26:00 I could also just like overwrite a file or I could write an entirely new file.
3:26:06 So since we already have this open,
3:26:08 instead of appending to the file, why don't we just write a file?
3:26:12 So I'm going to use this W.
3:26:15 And now if I say employee file dot right, because I'm using W and I'm not using
3:26:20 a, it's actually going to override the entire file.
3:26:23 And it's only going to put this inside the file.
3:26:26 So when I run this, and we go over to this employees dot text file,
3:26:30 you'll see we only have one line inside of this file.
3:26:33 Now it's just Kelly customer service.
3:26:35 That's because I was using W not a when you use W,
3:26:39 it's just overriding everything that's in that existing file.
3:26:42 You can also use W to create a new file.
3:26:46 So over here, I could say employee file is equal to employees one dot text.
3:26:51 And now what's going to happen is when I
3:26:54 run this, it's going to create another file for me.
3:26:57 So I'm going to run this.
3:26:58 And you'll see over here in my file browser,
3:27:01 we have this new file employees one dot text.
3:27:04 So if I open this up, it has exactly the same stuff as in this employees file.
3:27:09 But it basically created a new file for us.
3:27:11 And so a lot of times you're going to want to create a new file.
3:27:15 And you can use different extensions too.
3:27:18 So if I wanted to create like a web page, I could say index dot HTML.
3:27:23 And I could also add in some HTML code in here.
3:27:26 So if you don't understand HTML, don't worry about it.
3:27:29 But if you do, I could put like a paragraph in here,
3:27:33 another paragraph, like this is HTML, basically HTML is like a web page.
3:27:39 And the point I'm trying to make is that you could write
3:27:42 out a web page inside of Python by doing something like this.
3:27:45 So now when I play this, we get this index dot HTML file.
3:27:49 And it has some HTML inside of it.
3:27:52 So that's one way that writing to files can be really useful.
3:27:56 You can overwrite an existing file, you can write a new file and create it,
3:28:00 or you can append onto the end of a file.
3:28:03 And there's tons of applications for writing to files
3:28:06 and Python's a great language for working with reading,
3:28:09 writing, and doing all that stuff with files.
3:28:14 In this tutorial, I want to talk to you guys about using modules in Python.
3:28:20 Now, a module is essentially just a Python file
3:28:23 that we can import into our current Python file.
3:28:26 So for example, if I wrote a Python file that had
3:28:30 a bunch of like useful functions or useful variables or, you know,
3:28:34 other things like that, I could take that file,
3:28:37 I could import it into the file I'm currently working
3:28:39 on, and I could actually access all of those functions, all of those variables,
3:28:44 all the stuff from that external file
3:28:46 inside the file that I'm currently working on.
3:28:48 So I'm going to show you guys how we can use modules,
3:28:51 and then we'll talk about like, you know, where you can find awesome modules
3:28:55 and really why modules make Python an awesome language.
3:28:59 So over here in my text editor,
3:29:02 I actually created this file and it's called useful tools dot Python.
3:29:07 And basically this file just has a bunch of sort of like
3:29:10 useful tools that I might want to use in one of my programs.
3:29:14 So you'll see over here, I have some variables.
3:29:16 This one is telling me how many feet are in a mile.
3:29:19 This one's telling me how many meters are in a kilometer.
3:29:22 And then we have this list here,
3:29:23 which lists out all of the members of the Beatles.
3:29:26 I also have a couple different functions down here.
3:29:29 So I have this get file extension function.
3:29:32 And this basically just will, you know,
3:29:34 you give it a file name, it'll tell you what the extension is.
3:29:36 And then we have this other function down here, which simulates rolling a dice.
3:29:40 So you pass it a number.
3:29:41 If I pass it like a six, it would roll a six sided dice.
3:29:44 If I pass it a nine, it roll a nine sided dice, etc.
3:29:47 So this is like a Python file that I wrote.
3:29:49 And it has some useful stuff in it.
3:29:51 And honestly, there's a lot of stuff in here that I might
3:29:54 want to use in the other Python files that I work with.
3:29:58 So let's say I'm over here at this app dot Python file.
3:30:01 And I'm thinking to myself, huh, I think I could use one of those functions
3:30:06 that was inside of that useful tools file.
3:30:09 Actually, yeah, I need to simulate rolling a dice in my program.
3:30:13 Well, instead of having to go over here,
3:30:15 copy this function and then paste it over here into my program,
3:30:19 I can actually just import this useful tools file.
3:30:22 And I'll be able to import all
3:30:25 of these functions and all of these variables and attributes.
3:30:28 So the way I can do that is just by coming up here.
3:30:31 And I'm just going to go right at the top of my file.
3:30:34 And I'm just going to say import.
3:30:36 And then I want to type in the name of the file that I want to import.
3:30:39 So I'm just going to say useful underscore tools.
3:30:42 And Python is going to be smart enough to know that it should
3:30:46 go off and grab all the stuff from this useful tools dot pi file.
3:30:50 So once I've used that import statement,
3:30:53 I can actually use all of the functions inside of that useful tools file.
3:30:58 So for example, I could simulate rolling a dice, I could say useful tools dot.
3:31:04 And now I'm actually able to access all of the attributes from inside that file.
3:31:10 So when I say useful tools dot,
3:31:12 you'll see down here in my little suggestions menu,
3:31:15 it's telling me like Beatles feet and miles,
3:31:17 get file extension meters and kilometers roll dice.
3:31:20 So it's giving the access to all of the stuff that was in that file.
3:31:24 So over here, I could just say like roll dice and we
3:31:28 can pass this like a 10 will roll a 10 sided dice.
3:31:31 And now this should actually be able to run that function.
3:31:35 So we'll simulate rolling a 10 sided dice.
3:31:38 You can see we got four.
3:31:40 And this is a really core concept in Python,
3:31:44 which is importing functionality from external Python files.
3:31:49 And this is like huge in Python.
3:31:50 And this is seriously going to change the way that you create your Python files.
3:31:55 So you'll notice like I didn't have to copy any
3:31:59 functions or any variables or anything over into this file.
3:32:03 And yet I was able to use all the stuff that was inside this useful tools file.
3:32:08 So that is huge.
3:32:10 And honestly, it's going to save you a lot of time because you
3:32:13 can write something once and then you can import it into your other files.
3:32:17 So that's really the basics of using modules.
3:32:19 I mean, modules are very simple.
3:32:20 A module is just any external Python file
3:32:23 that you want to use some stuff inside of it.
3:32:26 I want to show you guys a place where you can go to find a huge list of modules.
3:32:32 So I'm going to go over here to my web browser.
3:32:36 And I'm on this website.
3:32:37 It's actually like the official Python docs.
3:32:39 And basically all I did to get here was
3:32:42 just type in list of Python modules and Google.
3:32:45 And depending on the version of Python you're using,
3:32:47 you're going to want to make sure that you click the right one.
3:32:49 I clicked on the version 31.
3:32:51 And over here on this page,
3:32:52 you can see there's just a huge list of Python modules.
3:32:55 And these are basically modules inside of Python that you can just access.
3:33:00 So essentially, there's like all of this awesome
3:33:03 code that's already been written for you.
3:33:06 And so there's some sort of functionality that you
3:33:08 want to have inside of your Python program.
3:33:11 Chances are there's a module in here that has that functionality.
3:33:14 So there's all sorts of stuff you can do.
3:33:17 I mean, if you just look through this list, like,
3:33:19 you'll see a huge collection of basically just, you know,
3:33:22 a bunch of either Python variables or Python functions, just,
3:33:25 you know, things that you can use to make your programs better.
3:33:29 So here's what I would recommend, like, head over to this page and just sort
3:33:34 of look through a lot of these different modules,
3:33:35 you know, see what you can see.
3:33:37 And honestly, like, if you click on one of them,
3:33:39 it'll bring you to a page that talks about like how to use it.
3:33:42 It'll tell you like how to import it and just, you know, some basics about it.
3:33:47 And this is sort of like the list that's on the official Python docs.
3:33:51 But here's the other thing.
3:33:54 The Python community is huge.
3:33:56 There's tons of developers who use Python.
3:33:58 And you can actually find a lot of third party modules.
3:34:01 So if you just go online and, you know, look up,
3:34:04 like, Python module for doing X or Python module for doing Y,
3:34:08 chances are somebody out there has already written
3:34:11 a Python module to do what you're trying to do.
3:34:14 So if you get good at using modules and you get good at, you know,
3:34:17 looking for them and finding them,
3:34:18 you can actually save yourself a bunch of time because
3:34:21 chances are that somebody's already written a module to do,
3:34:24 you know, what you're trying to do or like part of what you're trying to do.
3:34:28 So now that we've taken a look at all these different Python modules,
3:34:31 I want to just kind of dive in a little
3:34:33 bit deeper into how we can actually use these things.
3:34:36 So you'll notice here, there's a lot of files, right?
3:34:40 There's a lot of different modules that we apparently have access to Python.
3:34:44 But the question is, where are all these files stored, right?
3:34:48 When I was over here in my program,
3:34:51 for example, I was using this useful tools.py file.
3:34:54 Like, I knew where that was.
3:34:56 I created that file.
3:34:57 I was directly involved in making it.
3:34:59 And I just imported over here.
3:35:01 It was pretty easy, right?
3:35:03 But what about all of these files, right?
3:35:05 What about all these modules over here?
3:35:08 Like, where are all of these stored?
3:35:10 And there's basically two types of modules here.
3:35:12 There's built-in modules,
3:35:13 which means they're just built into the Python language.
3:35:16 So we kind of just automatically have access to them.
3:35:20 And there's external modules.
3:35:21 And a lot of these external modules are just stored
3:35:25 in basically the same folder that we installed Python on our computer.
3:35:29 So for example, let's look at a couple of these, right?
3:35:32 We have, like, base 64, BDB, bin ASCII, like,
3:35:37 if I come over here and you'll see, I'm over here in my little file explorer.
3:35:43 I have my Python project, which is draft.
3:35:46 There's also this other folder over here called external libraries.
3:35:49 If I was to click down on this and I come down here,
3:35:53 you can see it's just the version of Python that I'm using.
3:35:57 There's a folder here called lib.
3:35:59 And this is a very important folder.
3:36:01 This is basically storing all of those external modules.
3:36:03 So you can see if I scroll down here,
3:36:06 we have all of these different, like, modules, right?
3:36:10 So here's that base 64.
3:36:12 Here's BDB, right?
3:36:13 A lot of these external modules are stored inside of this lib folder.
3:36:18 And like I said, there's external modules.
3:36:20 There's also a few modules that are just like built in modules.
3:36:23 They're not going to be stored inside of here.
3:36:26 And you can actually tell.
3:36:27 So for example, if I was to come over here and click on base 64,
3:36:31 it tells me where the source code is.
3:36:34 So the source codes in lib forward slash base 64.
3:36:38 We were able to see that, right?
3:36:39 I was, I saw that inside of my lib folder.
3:36:42 This bin ASCII, though, for example,
3:36:44 if I click on this, you'll notice that this isn't giving
3:36:47 me a folder because this is basically just like built into Python.
3:36:51 So we don't have to worry about, you know, locating that file.
3:36:55 It's just kind of like, it just kind of works.
3:36:57 So some of these are external.
3:36:58 Some of them are built in.
3:36:59 And I want to show you guys, in addition to using these modules, right?
3:37:04 So there's a lot of good stuff here.
3:37:06 And honestly, you could spend, you know,
3:37:07 years and years just learning about all these different modules.
3:37:10 But a lot of times you're going to want
3:37:13 to use modules that other people have written.
3:37:15 And you know, there's a lot of developers
3:37:18 who work on Python and who write different modules.
3:37:21 So there's a lot of useful modules out
3:37:23 there that aren't included in this list, right?
3:37:26 They're not going to be inside of this lib folder by default.
3:37:30 And what we can actually do is we can install those external modules,
3:37:34 those third party modules, that don't just come pre installed with Python.
3:37:39 And I'm going to show you guys how to do that really quick.
3:37:42 So the first thing you need to do, obviously,
3:37:44 is find a Python module that you want to install that you want to download.
3:37:49 And I have actually used one of the past Python docs.
3:37:53 It's an external module that you can use
3:37:56 to basically use Python to create Word documents, which is pretty cool.
3:37:59 So you can like format Word documents inside of it.
3:38:02 So I'm just going to look that up here in Google.
3:38:05 I'm just going to type in Python docs.
3:38:07 And look, here's the thing, there's tons of these external modules.
3:38:11 You don't have to use Python docs.
3:38:13 I'm just giving you guys an example.
3:38:15 But really, if you just look up like useful third party Python modules,
3:38:19 there's going to be lists of these things online.
3:38:21 In my case, though, Python docs has a website,
3:38:24 and it basically just has like some installation instructions.
3:38:27 I'm going to come over here.
3:38:29 And it tells me that I can install
3:38:32 Python docs using the command pip install Python docs.
3:38:35 So this brings us to something I want to talk to you guys about, which is pip.
3:38:39 And pip is essentially a program.
3:38:42 And actually, if you have a newer version of Python three,
3:38:46 it comes pre installed with Python three.
3:38:48 And you can use pip to install Python modules.
3:38:52 It's referred to as a package manager.
3:38:55 And a package manager basically just allows you to like install,
3:39:00 manage, update, and uninstall like different Python modules.
3:39:03 So pip is extremely useful.
3:39:05 And in order for us to install Python docs, we're going to have to use pip.
3:39:10 So I'm going to show you guys how we can do that.
3:39:13 What I want to do is open up the command prompt or the terminal on my computer.
3:39:18 If you're on a Mac, you want to open up your terminal.
3:39:21 If you're on a Windows computer, you want to open up your command prompts.
3:39:24 I'm using a Windows right now.
3:39:25 So I'm just going to come down to the search bar and type in CMD.
3:39:29 And this command prompt should come up.
3:39:30 So I'm going to click this inside of the command prompt,
3:39:33 we can actually use pip.
3:39:34 The first thing we want to do is just check to make sure that pip's installed.
3:39:38 And like I said, if you have a version of Python three,
3:39:41 pip should come just pre installed.
3:39:43 And it should just work in here.
3:39:45 If you don't have a new version of Python three,
3:39:48 though, you might have to install pip separately.
3:39:50 And there's tons of instructions online on how to install pip.
3:39:54 So I'm just going to check to make sure that I have it,
3:39:56 I'm going to type in pip hyphen hyphen version.
3:39:58 And this should spit out the version of pip that I currently have.
3:40:02 So as long as we have pip,
3:40:05 then we're ready to install an external or third party Python module.
3:40:08 All I have to do is just say pick install.
3:40:12 And now I just want to type in the name of the Python module.
3:40:15 So in the case of Python docs, it was just Python docs like that.
3:40:20 Now again, you don't have to install Python docs.
3:40:23 I'm just doing this for this tutorial.
3:40:25 Just kind of show you guys how this is going to work.
3:40:28 But you know, what you want to do is go
3:40:30 online and look up some third party external Python modules.
3:40:33 And generally, like I'd say 90 to 95% of the time,
3:40:37 you're going to be able to just install them using pip.
3:40:40 In the off case that you can't install them using pip,
3:40:43 chances are there'll be like some detailed installation instructions.
3:40:45 But I would say for the most part,
3:40:48 any like legitimate Python module is going to be able to install using pip.
3:40:52 So over here, I'm just going to say pip install and then the module name.
3:40:58 So Python hyphen docs.
3:40:59 And when I click enter, this is going to go off and install Python docs for us.
3:41:03 So I'm just going to enter.
3:41:05 And it's going to go off and install everything we need for Python docs.
3:41:09 So you can see we successfully installed Python docs 0.8.6.
3:41:14 So I'm going to show you guys where exactly this got placed.
3:41:19 So normally, when we install a external third party module,
3:41:22 it's going to get put inside of this lib folder.
3:41:25 But it's going to get put inside
3:41:28 of a special folder in here called site packages.
3:41:31 So site packages is a special folder.
3:41:33 And if I open this folder, you'll see now we have this docs folder and we
3:41:38 also have this Python docs 0.8.6 pi 3.6.ag info folder.
3:41:44 So these two folders are basically included in that module installation.
3:41:49 So you can see this docs folder has a bunch of different Python
3:41:53 files and has a bunch of stuff in here that we can use.
3:41:56 So if I wanted to use this inside of one of my programs,
3:41:59 I'm just going to refer to the name of the module.
3:42:02 So in our case, it's just going to be docs.
3:42:04 So I used Python docs in order to install it.
3:42:07 But we're going to use docs if we want to import it.
3:42:10 So I could come up here and I could say import DOCX.
3:42:14 And now I can actually use it.
3:42:16 So I can just say like docs, doc, whatever.
3:42:19 And you can see there's a bunch of different stuff down here.
3:42:22 There's like a document document part image part.
3:42:24 There's a bunch of stuff that we can use with this.
3:42:26 And obviously, depending on the Python module you install,
3:42:29 there's going to be different instructions.
3:42:31 But you can see it got stored down here in this site packages folder.
3:42:35 If I wanted to remove this, I could use PIP to do it.
3:42:38 So I could just say PIP uninstall.
3:42:41 And we could just again say the module name.
3:42:46 So Python docs.
3:42:48 And PIP will now uninstall this on our computer.
3:42:51 So how is to go back over to this folder?
3:42:55 You'll see that those two,
3:42:57 that docs folder and then that other folder disappeared.
3:43:00 So they're no longer here.
3:43:02 And I'm actually not going to be able to use this anymore.
3:43:06 So that's sort of the ins and outs of using modules.
3:43:09 Now again, there's tons of these modules and I can make
3:43:13 dozens and dozens of Python courses covering each one of these modules.
3:43:17 The built-in modules,
3:43:18 the modules that are included by default and external modules,
3:43:22 there's tons of these things out there.
3:43:25 And really, as a Python programmer now,
3:43:26 what you can do is you can go out and play around with these different modules.
3:43:30 I showed you the ins and outs of installing them
3:43:33 and you can use PIP to install all these different modules.
3:43:36 And you can make sure that you have them
3:43:38 by checking the site packages folder or the libs folder.
3:43:41 But really now it's on you to just kind
3:43:43 of go out and use these modules and don't shy away
3:43:46 from this because modules are a huge part of Python and you're
3:43:50 definitely going to want to include them in your Python stack.
3:43:58 In this tutorial, I want to talk
3:44:00 to you guys about classes and objects in Python.
3:44:03 Now classes and objects are extremely useful in Python programming and they
3:44:08 can help you to make your programs more organized and more powerful.
3:44:13 So when we're in Python, we're dealing with all types of data, right?
3:44:17 And a lot of times when we're writing programs,
3:44:19 we're going to have to work with different types of data.
3:44:22 And there's essentially like a few basic types
3:44:24 of data we can do with usually things like strings,
3:44:28 so like plain text, numbers, and Boolean values.
3:44:30 Those three are kind of like the main types
3:44:33 of data that you're going to be working with in Python.
3:44:36 And we have all sorts of structures we can use to store that data,
3:44:39 you know, things like lists or dictionaries.
3:44:42 But here's the problem is that not all information,
3:44:45 not all data and not all things can be represented using strings,
3:44:50 numbers, or Booleans, right?
3:44:53 There's a lot of things in the real world that we
3:44:56 can't represent in something like a string or just a number, right?
3:44:59 In other words, something like think of like something
3:45:01 like a phone or a computer or a person, right?
3:45:04 You can't really represent those things in like a string or a number.
3:45:08 You know, in other words,
3:45:10 like the data types that we have available to us in Python can't cover that.
3:45:15 And so what we can do with classes and objects
3:45:18 is we can essentially create our own data types.
3:45:21 So I can create my own data type for anything I want in Python.
3:45:25 So I could create like a phone data type and it could represent a phone.
3:45:29 So I could store all the information I would ever
3:45:31 want to know about my phone inside of that data type.
3:45:34 And in Python, we could create a class for it.
3:45:38 And essentially what a class is is it's just saying, hey,
3:45:41 here's another data type that we want to use in Python.
3:45:44 So with a class, you can essentially define your own data type.
3:45:48 And it's super awesome.
3:45:50 And classes are extremely useful.
3:45:52 Classes are used in almost every single major programming language out there.
3:45:56 So in this tutorial, I just want to give you guys a basic
3:45:59 introduction of classes and using them inside of Python.
3:46:02 So let's say that I'm writing a program and I
3:46:06 want to represent a student inside of this program.
3:46:09 So maybe I'm writing a program for like a college or a university.
3:46:13 Let's say that in this program, I want to model a student.
3:46:17 Like I want to model a real world object and I want it to be a student.
3:46:21 Right, we don't have a student data type and I can't
3:46:24 really represent a student in just a single string or a number.
3:46:28 So what I can actually do is I can create a class for a student.
3:46:33 And I'm basically creating like a student data type.
3:46:36 So I'm going to show you guys how we can do that and create our student class.
3:46:40 So over here, I'm just going to make a new file.
3:46:43 We'll just make it a new Python file.
3:46:45 So I'm just going to call it student.py.
3:46:48 And inside of this student.py file, I want to create a student class.
3:46:54 So the way that I can do that is just by typing class.
3:46:58 And now I want to type the name of the class that I want to create.
3:47:02 So in our case, we're going to create a class called student.
3:47:04 So I'm just going to say student and now we're going to make a colon.
3:47:08 Now everything that goes inside of here is
3:47:10 going to be inside of our student class.
3:47:13 So everything that's indented like this will
3:47:16 be considered a part of our student class.
3:47:19 And basically what we can do inside of this student class
3:47:21 is we can define a bunch of attributes about a student.
3:47:25 So essentially what I'm doing is I'm like modeling a student.
3:47:28 I'm creating our student data type.
3:47:30 And I can use things like strings, integers,
3:47:33 and booleans in order to map out what
3:47:36 a student should be and what a student should have.
3:47:40 So I want to create something called an initialize function.
3:47:43 And the way I do that is just by typing out def.
3:47:47 And I'm just going to type two underscores.
3:47:50 And then the word I N I T and then two more underscores.
3:47:55 And you also want to type out open and close parentheses.
3:47:58 And inside of those parentheses, we want to type out self.
3:48:02 And then we want to colon after that.
3:48:04 What I can do inside of this initialize function is
3:48:07 I can basically map out what attributes a student should have.
3:48:11 So we can essentially define like, hey, here's the student data type in Python.
3:48:17 So what I want to do is add in certain attributes after this self.
3:48:21 So I'm just going to say self a comma.
3:48:23 And now we can start thinking about, you know,
3:48:26 what values will represent a student inside of our Python program.
3:48:30 So if I'm thinking like, I'm thinking something like name, right?
3:48:34 So every student in our program should have a name.
3:48:37 They also might want to have a major because they're in college.
3:48:41 They probably also will have a GPA.
3:48:43 So they're a grade point average, like how they're doing in school.
3:48:48 And let's define one more thing.
3:48:50 Let me think.
3:48:51 Why don't we make a Boolean?
3:48:53 And it's going to be called is on probation.
3:48:57 And this will basically tell whether or not the student is on probation.
3:49:02 So essentially what I'm doing inside of this initialize method inside
3:49:06 of this init function is I'm defining what a student is in our program.
3:49:12 And so in this program, a student has a name, it has a major,
3:49:16 it has a GPA, and it has a value
3:49:19 that determines whether or not it's on probation.
3:49:21 This right here is the student data type.
3:49:24 So if I'm representing a student inside of my program now,
3:49:28 it has all of these attributes associated to it.
3:49:31 And that's basically what I'm doing up here.
3:49:33 Now inside of this initialize function, we actually have to do something.
3:49:38 And basically what I want to do is assign some values.
3:49:42 So I'm going to be writing out some stuff
3:49:44 and this might not make total sense right now.
3:49:47 But this is going to make sense
3:49:49 in a second after we create our first student object.
3:49:52 So just stick with me right now and just basically
3:49:54 know that we have to do what I'm doing over here.
3:49:57 So I'm just going to say self dot name is equal to name.
3:50:01 And then I'm going to say self dot major is equal to major.
3:50:05 Self dot GPA is equal to GPA.
3:50:08 And I'm going to say self dot is on probation is equal to is on probation.
3:50:16 So again, this might not make a whole lot of sense right now,
3:50:19 but in a little bit, this is going to make total sense.
3:50:23 So now that I have this student class defined,
3:50:26 I can actually use this class inside of my other file.
3:50:30 So I'm going to come over here into this app dot Python file.
3:50:34 And this is just my main file.
3:50:36 So I actually want to create a student right so in that student class,
3:50:41 we defined the student data type.
3:50:43 And we basically said like hey, a student has a name,
3:50:46 a major GPA and it says whether or not they're on probation.
3:50:49 That's like the template for what a student is.
3:50:52 But we can actually create a student.
3:50:55 So we can create an actual student and give it some information.
3:50:59 And that's called an object.
3:51:01 So this student class over here is basically defining what a student is.
3:51:07 So a class is just like an overview of what the student data type is.
3:51:12 An object is an actual student.
3:51:14 So it's an actual student with a name, a major and a GPA.
3:51:19 It's not just this template anymore.
3:51:21 It's actually like a student that we're representing inside of our program.
3:51:25 So in order to use that student class and create a student object,
3:51:30 I actually need to import that.
3:51:32 So all I have to do over here is just say from student,
3:51:37 and this is referring to this student file.
3:51:40 I can say import student.
3:51:44 And basically what this is saying is from the student file,
3:51:48 I want to import the student class.
3:51:51 So even though these are both student, they're referring to different things.
3:51:54 So this is referring to the file
3:51:56 and this is referring to the actual student class.
3:51:59 So now that we did this, we can create a student.
3:52:02 So you can create an object of a class a lot like you would a normal variable.
3:52:06 So I can just give it a name.
3:52:08 I'm going to call this student one.
3:52:10 And I'm just going to set it equal to student.
3:52:14 And I'm going to make an open and close parentheses.
3:52:16 And now inside of this parentheses, I want to give this student a name,
3:52:22 a major, a GPA, and an is on probation value.
3:52:27 So I'm going to say, we'll just create a fake student.
3:52:30 So I'll just say his name's Jim.
3:52:32 And he's studying business.
3:52:36 And we want to give him a GPA.
3:52:38 So maybe he has like a 3.1.
3:52:41 And finally, we want to say whether or not this student is on probation.
3:52:44 So why don't we say false?
3:52:46 So basically what I'm doing is I'm saying that I want to create a student.
3:52:51 So I want to actually create a student object.
3:52:54 And remember, an object is just an instance of a class.
3:52:58 So the class is like an overall template.
3:53:00 It defines what a student is.
3:53:02 But an object is an actual student with actual information.
3:53:06 So we could call student one now.
3:53:09 This is a student object.
3:53:11 So I just want to show you guys what this student
3:53:13 object actually is and what we can do with it.
3:53:16 And then I'm going to talk some more about that in it function from before.
3:53:20 So over here, I'm just going to make a print statement.
3:53:23 And inside of here, I just want to print out student.
3:53:27 And what's cool about this student one object is I can
3:53:31 actually access each of the attributes from inside of this object.
3:53:35 So if I wanted to get the name of the student, I could say student one dot name.
3:53:39 And now this is actually going to print out the student's name.
3:53:43 So over here, you'll see it prints out Jim.
3:53:47 If I wanted, I could print out the student's GPA.
3:53:50 And it's going to print out the student's GPA 3.1.
3:53:54 So now that I created this student object,
3:53:57 I can actually access the information about the student.
3:54:01 So essentially, I've just created a student data type.
3:54:04 So I could create as many of these students as I wanted.
3:54:07 If I wanted, I could create another student we could call it student two.
3:54:11 And essentially, we do the same thing.
3:54:13 We just give it different information.
3:54:15 So we could say like Pam and her major is art.
3:54:20 And she has like a 2.5.
3:54:22 And let's say that she is on probation.
3:54:26 So now I have another student, student two.
3:54:28 So if I wanted, I could access information about that student.
3:54:32 We could say like student two dot GPA.
3:54:34 And now this is giving me the GPA of student two.
3:54:38 So basically, what I did was I created
3:54:41 a student data type and I created student objects.
3:54:44 And now I'm able to represent a student inside of my program.
3:54:48 So let's talk real quick about all this stuff
3:54:50 over here because I didn't really explain it.
3:54:53 Basically, what's happening is when I come over here and I
3:54:57 say student and I'm passing in all of these different values,
3:55:01 those are getting passed into this init function.
3:55:05 Remember, I passed in a name, I passed in a major, I passed in a GPA.
3:55:09 When we create that student, we're actually calling this function.
3:55:14 And when I pass in the name, the major and the GPA,
3:55:17 those values are actually getting stored over here in this name,
3:55:21 this major, this GPA, et cetera.
3:55:24 So I'm giving this student object all of that information.
3:55:29 And down here, what I'm doing is I'm saying self dot name is equal to name.
3:55:35 And basically what this means is the actual object's name is
3:55:38 going to be equal to the name that they passed in.
3:55:42 So self dot name is an attribute of student.
3:55:46 So the student is storing a name,
3:55:48 the student is storing a major, the student is storing a GPA.
3:55:51 But that's different from this name, this major, and this GPA.
3:55:56 Remember, these are all just values that I passed in, they're just parameters.
3:56:01 And so I need to take the values that I passed
3:56:04 in and I need to assign them to the actual attributes of the object.
3:56:08 So I need to say, okay,
3:56:09 the name of the student is going to be equal to the name that we passed in.
3:56:14 One more time, the name of the student object is
3:56:17 going to be equal to the name that we passed in.
3:56:21 The name of the student's major is going
3:56:23 to be equal to the major that we passed in.
3:56:26 The student's GPA is going to be equal to the GPA that we passed in.
3:56:31 That's basically what's happening here.
3:56:33 So when I say self, it's referring to the actual object.
3:56:37 So over here, when I'm creating this student object,
3:56:40 I'm giving it all of this information.
3:56:43 It's taking that information and storing it as attributes for the object.
3:56:48 So hopefully that makes sense.
3:56:49 I think that's about as clear as I can make it.
3:56:52 But the point is that now we have a student data type.
3:56:56 So now I can represent a student inside of my Python program.
3:57:01 And that's like super powerful.
3:57:02 And what's cool about classes and objects is you can do this with anything.
3:57:06 So in this example, we created a student class
3:57:09 and then we created student objects off of that.
3:57:12 But I can model any real world entity into this program.
3:57:16 I can model something like a phone or I
3:57:18 can model like a water bottle or I can model, you know, a keyboard, right?
3:57:23 I can model anything that I wanted inside of my program.
3:57:27 I could just give it certain attributes, right?
3:57:29 And so that's what's cool about classes is we can model
3:57:32 real world objects and we can create our own data types.
3:57:40 In this tutorial, I'm going to show you guys
3:57:42 how to build a multiple choice quiz in Python.
3:57:45 So basically, we're going to set up a little multiple
3:57:47 choice quiz and we'll have the user take the quiz.
3:57:50 And as they take the quiz, we'll keep track of their score and then
3:57:53 at the end we'll tell them how they did.
3:57:55 So this is going to be a pretty cool video.
3:57:57 I'm excited to show you guys how to do this.
3:57:59 And we'll get to use things like classes and if
3:58:02 statements and loops in order to build this program.
3:58:05 So we're going to use a lot of cool stuff in Python.
3:58:08 The first thing I want to show you guys,
3:58:10 I actually did this before I started the video.
3:58:13 It's this little array.
3:58:14 It's called question prompts.
3:58:15 And I basically just wrote out all the questions
3:58:17 that are going to be or I shouldn't say the questions.
3:58:20 I wrote out all the question prompts that are
3:58:22 going to be inside of my multiple choice quiz.
3:58:25 So the first one just says what color are
3:58:28 apples and the answers are red, purple or orange.
3:58:32 The next one is what color bananas the options are teal, magenta and yellow.
3:58:38 And the third question is what color are
3:58:40 strawberries and the options are yellow, red and blue.
3:58:43 So this is obviously a very, very easy multiple choice quiz.
3:58:46 But I just kind of wrote out the questions
3:58:48 beforehand so I didn't have to spend time doing that.
3:58:51 So let's talk about how we can build a multiple choice test.
3:58:56 Well, the first thing I want to think about
3:58:58 is how can I represent the questions in the test.
3:59:00 Over here, we have the question prompts,
3:59:02 but there's actually two parts to a question when you think about it.
3:59:07 There's the prompts.
3:59:08 In other words, the actual question itself.
3:59:11 And then there's the answer to the question and both
3:59:14 of those attributes need to be kept track of.
3:59:17 I need to keep track of what we want to ask
3:59:19 and I need to keep track of what the answer is.
3:59:21 So what I'm actually going to do is create a question class.
3:59:25 So I'm going to create a little data
3:59:27 type for questions and inside of that question class,
3:59:30 we'll be able to store the question prompts
3:59:32 and we'll also be able to store the questions answer.
3:59:36 So I'm just going to come over here to my folder and I'm going
3:59:40 to make a new Python file and we're just going to call this question.py.
3:59:45 So we'll call it question.py and inside of this question.py file,
3:59:49 I'm going to make a class.
3:59:51 So I'm just going to say class question and I'm going to put a colon here.
3:59:56 So inside of this question class, we want to define it in initialize function.
4:00:01 So I'm going to say def two underscores in it, two underscores,
4:00:05 and then we're going to type out an open
4:00:08 and close parentheses self and then a colon.
4:00:11 So after this self, we want to define the different attributes
4:00:15 that will describe or that will be included in a question.
4:00:19 So we're going to include a prompt and we're going to include an answer.
4:00:24 So every question will have a question prompt and a question answer.
4:00:28 Now down here, I want to take those values
4:00:31 and assign them to the actual class object.
4:00:34 So I'm going to say self dot prompt is equal to prompt
4:00:38 and I'm going to say self dot answer is equal to answer.
4:00:43 Now we have a question class set up and we
4:00:46 can store all the information we need about a question.
4:00:50 So what I want to do now is go back over to my app dot Python file.
4:00:54 And we're actually going to create an array of questions.
4:00:57 So I have over here these three prompts
4:00:59 and I'm actually just going to create another array.
4:01:02 So we'll just call it questions and I'm just going to set it equal
4:01:07 to to open and close square brackets
4:01:09 and we're going to start creating question objects.
4:01:12 Let's say the first element in this questions
4:01:16 array will be a question and we want
4:01:19 to pass question prompts zero and the answer
4:01:23 to the first question was a red green.
4:01:27 That's the color of Apple.
4:01:28 So I'm going to pass that first question and I'm also going to pass the answer,
4:01:32 which is a and you'll see down here, I'm getting an error.
4:01:36 So saying unresolved reference question.
4:01:38 So actually what we need to do before we can
4:01:41 create these questions is we need to import this question class.
4:01:45 So I'm going to come up in here,
4:01:48 come up here and say from question import question.
4:01:52 So now down here in this questions array, we're not getting that error anymore.
4:01:56 So we created one question object and I'm just
4:01:59 going to create a couple more for these other ones.
4:02:02 So I'm just going to copy this one and we will paste this down here.
4:02:10 Alright, so now we're creating two other questions
4:02:12 and these ones are going to be one and two and the answer to the second question
4:02:18 is C and the answer to the third question.
4:02:21 I think it's B.
4:02:23 Let me look.
4:02:25 Yeah, so it's C and B.
4:02:27 Alright, so essentially what we're doing is we're creating three questions.
4:02:30 Each one is getting a different question
4:02:33 prompt and they're each getting different answers.
4:02:35 So now we have an array of questions that we want to ask on our test.
4:02:39 So the next step is to actually write a function that will run the test, right?
4:02:44 It has to ask the user the questions and it
4:02:46 has to check to see if they got the answer right.
4:02:49 Down here, lower in the file, let's create a function.
4:02:52 So I'm just going to say death and we'll call this run test.
4:02:57 And inside of this parentheses,
4:02:59 we're going to take one parameter into this function.
4:03:02 We're going to take a list of questions.
4:03:04 So I'm just going to say questions.
4:03:07 So this is basically going to be a list
4:03:09 of question objects that we want to ask the user.
4:03:12 So once I'm in here, all I want to do is loop through all the questions.
4:03:16 So I want to loop through each question.
4:03:18 I want to ask it to the user.
4:03:20 I want to get the user's answer and I want to check to see if it's right.
4:03:24 And we need to be able to keep track of how the user does through the test.
4:03:28 So I want to create a variable called score
4:03:30 and I'm just going to set it equal to zero.
4:03:33 So every time the user answers a question right,
4:03:36 we'll increment this score variable.
4:03:38 Alright, so what I want to do is, like I said,
4:03:40 loop through all the questions in that questions array.
4:03:42 So I'm going to create a for loop.
4:03:45 I'm going to say for question in questions.
4:03:49 So in other words, for each question object inside of this questions array,
4:03:53 I want to do something.
4:03:55 And basically, the first thing I want to do is ask the user the question.
4:04:00 And I want to store their response inside of a variable.
4:04:03 So I'm going to say, I'm going to make a variable down here called answer.
4:04:07 And this is going to represent the user's answer to the question.
4:04:12 And I'm going to set it equal to input and I need to give this a prompt.
4:04:17 So I'm actually going to give this the prompt for the question.
4:04:20 So I can just say question dot prompt.
4:04:24 So now I have the answer that the user
4:04:26 entered in, which is stored inside this variable.
4:04:29 So we want to check to see if they got the question right.
4:04:32 And I can do that using an if statement.
4:04:34 Let's say if answer, and this refers to the student's answer,
4:04:39 is equal to question dot answer.
4:04:42 So I'm checking to see if the answer that the student gave
4:04:45 is equal to the answer of the current question that we're asking.
4:04:49 And if this is true, then I just want to increment score.
4:04:53 So I'm going to say score plus equals one.
4:04:56 And this basically just means we're adding one to the score.
4:04:59 So the last thing I want to do inside
4:05:01 of this function is just print out the result.
4:05:04 So I want to print out how the user did.
4:05:06 So down here, I'm just going to say print
4:05:08 and I'm basically just going to print out like,
4:05:10 Hey, you got like two out of three questions
4:05:12 or you got five out of six questions, right?
4:05:14 Or you got zero out of 10 questions, right?
4:05:16 We'll just print out how they did.
4:05:18 So I'll just say you got.
4:05:20 And now I want to print out how many questions they got right.
4:05:24 So this is just going to be the score.
4:05:26 And in order to print a number alongside of a string like
4:05:29 this, I'm going to have to say STR and then type in the number.
4:05:33 So we'll say STR score.
4:05:35 So we'll basically say you got however many they got right out of.
4:05:39 So we'll use this little forward slash.
4:05:41 And now we'll just say how many questions there were.
4:05:45 So we can say, and again, we're going to have to convert this to a string
4:05:48 because it's going to be a number.
4:05:49 So we can say, string,
4:05:50 and then we want to figure out how many questions were in that questions array.
4:05:54 So I can say LEN.
4:05:56 And inside of here, we can just say questions.
4:05:58 So this is kind of like this long statement
4:06:00 just to print out how many questions there were.
4:06:02 So then over here, I'll just say, correct.
4:06:05 All right.
4:06:06 So now we have this print statement.
4:06:08 And it's just going to be printing out how many questions they got right.
4:06:12 So let's see if this works.
4:06:14 The last thing I need to do is just call this run test function.
4:06:19 So I'll say run test,
4:06:20 and I'm just going to pass it that questions array that we created before.
4:06:24 So we created this questions array with all
4:06:26 of these question objects inside of it.
4:06:29 I'm basically just going to pass that into this run test function.
4:06:33 So now let's run our program and see how we did.
4:06:36 Hopefully we don't get any errors.
4:06:38 There's a lot of code there, so it's possible that we might.
4:06:42 Okay, so it says what color are apples?
4:06:44 Huh, this is a tough one.
4:06:45 All right, let's put A.
4:06:46 So we'll get the first question right.
4:06:48 What color are bananas?
4:06:49 Let's get the second question wrong.
4:06:51 So I'm going to say B, bananas are magenta.
4:06:54 And we'll get the third question right.
4:06:56 So it says what color are strawberries?
4:06:58 I'll just put B because that's the right answer.
4:07:01 So now you'll see that our program prints out.
4:07:04 You got two out of three correct.
4:07:06 So not only did it ask us all the questions and get our input for each question,
4:07:10 but it also kept track of our score and it printed it out.
4:07:14 So why don't we run through the test one more
4:07:17 time and we'll just like get too wrong this time.
4:07:20 So we'll get this one wrong.
4:07:21 I'll get this one wrong and I'll get this one right.
4:07:23 So now it says you got one out of three correct.
4:07:26 So it's able to actually grade our test.
4:07:28 And what's cool about the program we just wrote is if I wanted,
4:07:32 I could add another question into here
4:07:34 and it would automatically be able to ask it.
4:07:37 So it doesn't matter how many questions we put in here.
4:07:40 It's just able to ask it.
4:07:42 So really the point I was trying to make with this whole video was just
4:07:46 how we can use something like a class in order to model a real world entity.
4:07:51 So for example, we created this question class and we were able
4:07:55 to store all the information about a question that we could ever need.
4:07:59 So we were able to sort of like the prompts and the answer.
4:08:02 So we created our own question data type and then
4:08:05 we were able to create a bunch of different questions,
4:08:08 pass it into this run test method and it was able
4:08:11 to grade the test and just run the test really easily.
4:08:14 So that's kind of the point.
4:08:16 So hopefully this makes sense.
4:08:18 You know, this was a lot of code.
4:08:19 So if you don't fully understand what I did, go back, watch the video, you know,
4:08:23 try it a couple of times on your own
4:08:25 and see if you can create your own multiple choice test.
4:08:28 In this tutorial, I want to talk to you guys about class functions in Python.
4:08:36 So a class function is essentially a function
4:08:39 that we can use inside of a class and it
4:08:42 can either modify the objects of that class
4:08:45 or it can give us specific information about those objects.
4:08:48 Over here, I have this student.py file and inside of here,
4:08:53 I created a student class.
4:08:55 So I just had a class student and I
4:08:57 gave this student a couple of different attributes.
4:08:59 I gave it a name, a major and a GPA.
4:09:03 So I can give this student all of this information.
4:09:06 So when I create a student object, it'll have all of that information in it.
4:09:10 Now over here on this app.py file, it actually created two students.
4:09:15 So student one is Oscar who is studying accounting and he has a GPA of 3.1.
4:09:21 Student two is Phyllis who's studying business and she has a 3.8.
4:09:26 So what we can actually do is we can use functions inside of these class files.
4:09:32 So I could actually define a function inside of this student
4:09:36 class and then all of my student objects could access it.
4:09:40 So for example, I can come down here and create a function and let's say we
4:09:45 wanted to create a function that told us
4:09:47 whether or not this particular student had honors.
4:09:50 So whether or not they were on the honor roll, right?
4:09:53 Basically like, are they like a really good student?
4:09:56 So I could define a function called on honor roll.
4:10:01 And basically what it's going to do, it's going to tell me whether or not
4:10:06 this particular student is on the honor roll.
4:10:09 And let's basically just say the rules for being on honor
4:10:12 roll is you have to have a GPA of 3.5 or above.
4:10:16 So here GPA is 3.5 or above, that means you're on the honor roll.
4:10:20 Well, over here I could actually write the code for that.
4:10:23 So I could say if self.gpa, and when I say self over here,
4:10:29 this is referring to like the actual student's GPA.
4:10:33 And you'll notice over here I have to pass
4:10:35 that in as a parameter and that always has to be the first parameter.
4:10:39 So I could say if self.gpa is greater than or equal to 3.5,
4:10:45 then we can return true because this student is on the honor roll.
4:10:52 Otherwise else we can just return false because if their GPA
4:10:57 isn't above a 3.5 then they're not on the honor roll.
4:11:03 So that's essentially our little on honor roll function.
4:11:06 And you can see it's a very small function,
4:11:08 but it provides a service to the objects of this class.
4:11:12 It allows the objects of this class to figure out
4:11:15 whether or not that current student is on the honor roll.
4:11:18 So over in this app.pa file I could just print out that value.
4:11:24 So I could say print and I'm going to see if student one is on the honor roll.
4:11:32 So now when I run this you'll see it should print out false.
4:11:37 And it's putting out false down here because student one only has a GPA of 3.1.
4:11:43 If I was to do the same thing on student two,
4:11:47 student two has a GPA of 3.8 so we should return true.
4:11:51 And that's exactly what happened.
4:11:53 So essentially a class function is just a little function
4:11:56 that can be used by the objects of the class.
4:11:59 And I would say this is a pretty good
4:12:01 example of what a class function should be doing.
4:12:04 It should be like giving us information about the class
4:12:07 or it can also be like modifying information about the class.
4:12:10 So that's the basics of using functions in classes.
4:12:12 I mean this is a pretty simple tutorial,
4:12:15 but I really just wanted to introduce you guys
4:12:18 to the concept of putting functions inside of classes.
4:12:21 This is going to be really useful.
4:12:22 And if you're making a class you always want
4:12:26 to think about what functions can I put inside of here
4:12:31 that will essentially help the user to either figure out
4:12:35 information about the object or modify different values in the object.
4:12:40 In this tutorial I want to talk to you guys about inheritance in Python.
4:12:44 Now inheritance is basically where we can define a bunch
4:12:47 of attributes and functions and things inside of a class.
4:12:51 And then we can create another class and we can inherit all of those attributes.
4:12:56 So I can basically have one class that has all the functionality of another
4:13:01 class without having to physically write out
4:13:03 any of the same methods or attributes.
4:13:06 So if that's confusing don't worry I'm going to show
4:13:08 you guys exactly what this is in this video.
4:13:11 So let me show you about some stuff I have set up over here.
4:13:15 I created this class called chef.
4:13:17 And over here we just have this class chef.
4:13:21 And inside of this chef class we have three functions.
4:13:24 So this chef can do three things.
4:13:26 The chef can make chicken.
4:13:28 The chef can make salad and the chef can make a special dish.
4:13:33 And whenever the chef makes a dish he
4:13:36 basically just says the chef makes a chicken
4:13:38 or the chef makes a salad or the chef makes barbecue ribs for this special dish.
4:13:44 It's basically just saying whatever the chef is doing.
4:13:48 So if I was to come over here
4:13:50 to my app.python file you'll notice that I'm importing the chef.
4:13:54 So I'm basically allowed to use this chef class now.
4:13:58 And I can say my chef is equal to chef.
4:14:01 So I'm creating a new chef.
4:14:02 So now if I came down here and I said my chef dot make chicken.
4:14:09 When I run my program now it's going to say the chef makes a chicken.
4:14:14 If I said make special dish and I run the program now it's
4:14:20 going to say the chef makes barbecue
4:14:23 ribs because that's the chef's special dish.
4:14:26 So I have this really awesome chef class and it works really well.
4:14:29 But let's say that I wanted to create
4:14:32 another class to model another type of chef.
4:14:35 So this chef class is just modeling like some generic chef.
4:14:39 We're representing some generic chef in our program.
4:14:42 But let's say that I wanted to create
4:14:44 a class that modeled a different type of chef.
4:14:47 For example let's say we wanted to create a class that modeled a Chinese chef.
4:14:51 So instead of just a normal chef this is a Chinese chef.
4:14:55 Well I actually created a file over here called Chinese chef dot python.
4:14:59 So we could actually use this Chinese chef
4:15:02 dot python file to create our Chinese chef class.
4:15:06 So I'm just going to say class and over here I'm just going to say Chinese chef.
4:15:11 And basically I just can define everything that the Chinese chef can do.
4:15:17 But let's say that our Chinese chef can
4:15:20 do everything that our generic chef can do.
4:15:23 So the Chinese chef is like a special
4:15:26 chef right it's a very specific type of chef.
4:15:29 But let's say that this Chinese chef can
4:15:31 do everything that the normal chef can do.
4:15:33 So the Chinese chef can make chicken,
4:15:35 can make salad and can also make a special dish.
4:15:38 Well if I wanted to give this Chinese
4:15:40 chef all of that functionality I could just come over here and I could copy all
4:15:46 of these functions and I could paste them in here.
4:15:49 So now the Chinese chef can do everything that the other chef can do.
4:15:54 But let's say that in addition to all of these things
4:15:57 the Chinese chef can also make a special dish called fried rice.
4:16:01 So I could say deaf and we could say make fried rice.
4:16:06 So the Chinese chef has this extra function which allows it to make
4:16:11 fried rice and over here we can just say the chef makes fried rice.
4:16:16 And let's also say that the Chinese chef has a different special dish.
4:16:20 So instead of making barbecue ribs the Chinese chef makes orange chicken.
4:16:25 Alright so now we have our Chinese chef class set up and you'll
4:16:29 notice that it can do everything that the normal chef can do.
4:16:33 So let's go back over to our app.python file
4:16:36 and I want to create a Chinese chef object.
4:16:39 So instead of importing from chef we're going to just import it from Chinese
4:16:44 chef so I'll say Chinese chef and we're going to import Chinese chef.
4:16:49 So let's go ahead and make a Chinese chef object.
4:16:53 So down here I'm going to make one and I'll just say
4:16:57 my and then my Chinese chef we can print out this special dish.
4:17:02 So remember the Chinese chef can do everything that the normal chef could do.
4:17:06 So if I say my Chinese chef make special dish when I run this program you'll
4:17:12 see the regular chef is making barbecue ribs
4:17:15 and the Chinese chef is making orange chicken.
4:17:18 So everything works out and this Chinese chef also has an extra
4:17:23 method called make fried rice and you can also make fried rice.
4:17:28 But here's the problem right when I go over here to this Chinese chef dot
4:17:33 pie when I wanted to use all
4:17:35 of the functionality that was inside this chef class.
4:17:38 I actually had to copy and physically paste all of these functions
4:17:42 down into this file right and that's kind of a drag.
4:17:46 I mean especially like imagine if this chef had
4:17:49 like 20 or 30 of these different functions in it.
4:17:52 Well I have to copy and I have to paste all of those inside
4:17:55 of this Chinese chef and this is where we can use something called inheritance.
4:17:59 So instead of having to copy and paste all of these functions
4:18:03 I could just inherit these functions from that chef class.
4:18:07 And in order to do that I can actually just get rid of all
4:18:11 of these and I can come over here
4:18:13 and right next to where I'm saying class Chinese chef.
4:18:16 Inside of parentheses I can say the name of the class
4:18:19 that I want to inherit from so I can just say chef.
4:18:22 And I'm also going to have to import this so I'm
4:18:27 going to come over here and we'll say from chef import chef.
4:18:32 So basically what this is saying is inside of this Chinese chef I want to be
4:18:37 able to use all of the functions that are contained inside of the chef class.
4:18:42 So I'll say that one more time.
4:18:44 Inside of this Chinese chef class I want to be able
4:18:46 to use all of the functions that are inside of this chef class.
4:18:50 So by using inheritance here I'm actually able to come over here to this app.py
4:18:56 file and now I'm still able to execute
4:18:59 for example like the make chicken function.
4:19:02 Even though I didn't specify the make chicken
4:19:05 function in here I didn't write it out.
4:19:08 I can still run this program and the Chinese
4:19:12 chef will still be able to make chicken.
4:19:15 So when I run this program you'll see down here it says the chef makes chicken.
4:19:20 And that's because I inherited the make chicken method from the chef class.
4:19:25 But here's one thing that got messed up.
4:19:28 If I come over here and I say make special dish.
4:19:32 Remember the Chinese chef special dish was supposed to be orange chicken.
4:19:36 But now the Chinese chef is going to be making barbecue ribs
4:19:41 because I inherited this make special dish function from this chef class.
4:19:47 And so what I can actually do is I could come
4:19:51 in here and I could override that make special dish class.
4:19:55 So I could say death make special dish.
4:19:58 And down here I can just override it.
4:20:02 So I could say the chef makes orange chicken.
4:20:06 And now when I go over here and I run this make special dish function.
4:20:12 Now the chef's going to be back to making orange chicken.
4:20:15 So the Chinese chef will make his special dish.
4:20:18 So that's basically what inheritance does.
4:20:20 It allows me to inherit functionality from an existing class into the new class.
4:20:26 And I can actually just inherit all of it without having to write it out.
4:20:31 So I didn't have to write out make
4:20:33 chicken and make salad inside the Chinese class.
4:20:36 But the Chinese chef can still make chicken and salad
4:20:41 because it inherited that functionality from the chef class.
4:20:46 In this tutorial I want to talk to you guys about the Python interpreter.
4:20:50 The Python interpreter is basically a little environment
4:20:54 that we can use to execute Python commands.
4:20:58 So it's kind of like this little sandbox
4:21:00 environment where we can test out and try
4:21:03 out different Python commands or different Python
4:21:05 functions in a very safe and neutral environment.
4:21:08 So it's kind of like a quick and dirty
4:21:10 way to write Python and try different things out.
4:21:13 And the way we use the Python interpreter is by opening up our command prompt.
4:21:18 Now if you're on Windows there's a program called command prompt.
4:21:22 If you're on Mac there's a program called terminal.
4:21:25 And basically you need to open that program
4:21:27 in order to use the Python interpreter.
4:21:30 So I'm here on a Mac but if you're on a Windows you
4:21:33 can essentially do exactly what I'm doing just inside of the command prompt.
4:21:37 So on Mac here I'm just going to search for my terminal.
4:21:41 If you're on Windows you can just search for like
4:21:43 CMD or command prompt and it should pop up.
4:21:46 So the terminal is basically an environment on our computer where we can
4:21:51 interact with the computer and do
4:21:54 different things without a graphical user interface.
4:21:57 So normally when we interact with the computer we're using
4:22:01 things like buttons or windows or we can move stuff around.
4:22:05 We can basically just like interact
4:22:07 with the computer with graphics but the terminal
4:22:10 or the command prompt is a place where
4:22:13 we can interact with the computer using text commands.
4:22:16 So you know I'm not going to get too much into what
4:22:18 the terminal and the command prompt are but that's essentially what it is.
4:22:21 So inside of here we can use the Python interpreter.
4:22:26 And the Python interpreter is basically just a little program that like
4:22:29 I said we can write you know sort of Python in.
4:22:33 So generally all you have to do is go over
4:22:36 to your terminal or your command prompt and type in Python 3.
4:22:40 And since we're using Python 3 I'm going to type Python 3 but you
4:22:44 can also just type normal Python and I'm pretty sure that gives you Python 2.
4:22:49 Now here's a quick disclaimer on Windows you
4:22:52 may run into a problem where you can't
4:22:55 use this Python 3 command and it's possible
4:22:59 that if you're running into that problem Python.
4:23:02 Like Python 3 hasn't been added to your windows path variable.
4:23:07 And basically what that means is
4:23:09 your computer doesn't recognize the command Python 3.
4:23:13 If that's the case all you have to do is just go
4:23:16 and Google how to add Python 3 to your windows path variable.
4:23:19 It's really easy I'm not going to get into it
4:23:21 in this video just because it's kind of outside
4:23:23 the scope but if you're running into that problem
4:23:26 that's probably it's not on your windows path variable.
4:23:28 So just Google around for that and you should be able to find an answer.
4:23:32 But I'm going to type Python 3 and click enter
4:23:34 and it's going to open up this cool little thing down here.
4:23:37 So you can see it basically just has like here's the version of Python.
4:23:41 Now I'm just going to enter so we can have some like new lines here.
4:23:46 But down here this is the Python interpreter so we
4:23:49 can write Python code in here and it's going to work.
4:23:52 So I can for example I could write like print
4:23:55 and inside of here I could print like hello world.
4:23:58 And when I click enter you'll see that it prints out hello world.
4:24:02 I could create a variable so for example I
4:24:04 could say like num one is equal to ten.
4:24:07 I could say num two is equal to ninety and then I could print out
4:24:11 num one plus num two and it's going to be able to do that for me.
4:24:16 I could also use some like a function so I could define a function in here.
4:24:22 We could make a function that's called say hi and it'll take a name parameter.
4:24:27 And you'll see here when it saw I wanted to create a function it put
4:24:31 these three little dots and I can indent
4:24:33 and we can write the code for our function.
4:24:36 So I can say like print hello plus name.
4:24:44 And now I have a function called say hi
4:24:47 so I can break out of this by just entering
4:24:50 again and I can call the function so I could say say hi and we'll say hi to me.
4:24:57 Hi Mike so now it's going to say hello Mike
4:24:59 so I can use a function I could use something like
4:25:02 an if statement you know I could say I could
4:25:05 use something like a for loop we could use while loops.
4:25:08 I mean we can basically use all of the basic
4:25:11 like Python commands and you know inside of this Python
4:25:14 interpreter and like I said before it's essentially just
4:25:17 an environment where you can test out Python code.
4:25:20 Now this is not a place where you want to write like any serious Python scripts.
4:25:25 So if you're like designing and writing a Python program
4:25:28 I would not recommend doing it inside of this interpreter.
4:25:31 It is it's not a very intuitive environment it's pretty much
4:25:34 just set up for some quick and dirty tests like we
4:25:36 need to test something out great this is a perfect place
4:25:39 for it because you don't have to set up a file.
4:25:42 You don't have to use like some ID you don't have to execute
4:25:45 a file you just go in here it's quick it's dirty you get it done.
4:25:48 But if you're writing like an actual program you
4:25:51 definitely definitely definitely want to use a text editor.
4:25:55 The text editor it's just going to be way more
4:25:57 organized everything is going to be a lot cleaner and you're
4:26:00 not going to get confused with like where stuff is
4:26:03 and what you have so that's the basics of the Python interpreter.
4:26:06 You know in the course I haven't really been using it
4:26:08 at all just because I think it's a lot easier to teach when
4:26:12 we're inside of a file than inside of here but you know
4:26:15 don't count this guy out the Python interpreter is awesome a lot
4:26:17 of people use it and it's great if you just need
4:26:20 to test you know little bits of code out without having to set
4:26:24 up some huge environment so Python interpreter definitely awesome I would play
4:26:27 around with it you know have some fun and take advantage of it.
4:26:31 Hey thanks for watching if you enjoyed
4:26:32 the video please leave a like and subscribe
4:26:34 to draft Academy to be the first to know when we release new content.
4:26:37 Also we're always looking to improve so if you have
4:26:40 any constructive criticism or questions or anything leave a comment below.
4:26:44 Finally if you're enjoying draft Academy and you want to help us grow
4:26:48 head over to draft Academy.com forward
4:26:50 slash contribute and invest in our future.