TRANSCRIPTEnglish

Learn Python - Full Course for Beginners [Tutorial]

4h 26m 53s50,862 words2,935 segmentsEnglish

FULL TRANSCRIPT

0:00

In this course, I'm going to teach you everything you need to know to get started programming

0:04

in Python. Now, Python is one of the most popular programming languages out there. And it's by far

0:10

one of the most sought after for jobs. And so if you're trying to get a job or you're trying to,

0:15

you know, automate your life, or you're trying to write awesome scripts to do a bunch of different

0:19

things, then Python's for you. Honestly, more and more developers every day are moving their

0:24

projects over to Python because it's such a powerful and it's such an easy to use language. A lot

0:30

of programming languages out there just aren't very beginner friendly. There's a lot of syntax.

0:34

There's a lot of like little things that if you get wrong, the program will yell at you. Python

0:38

is the complete opposite of that. You basically just type out what you want to do and Python does

0:43

it. It's that simple. There's not a whole lot of syntax to learn. The learning curve is literally

0:49

zero. You jump in, you can start writing your first program in seconds. In this course, I'm

0:54

going to teach you guys everything you need to know to get started in Python. I designed this

0:58

course, especially for Python. And each lesson has been specially designed with examples that

1:03

will help you along the way with so many people starting to learn Python. The question isn't,

1:07

why should you learn Python? The question is, why shouldn't you? And I think for a lot of people,

1:12

the reason they might not want to learn Python is because they're intimidated or they're afraid

1:16

that it's going to be too hard. 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. We're going to look at everything you

1:25

need to know to start programming in Python and start being confident and start writing scripts

1:30

and start writing programs that are awesome and doing cool things in your life. Anyway,

1:34

I'm super pumped to be teaching you guys Python. I can't wait to get started in this course. And

1:38

I hope you guys stick around and follow along with the course and learn this amazing programming

1:43

language. In this tutorial, I'm going to show you guys how to install Python onto your computer.

1:52

And we're also going to install a text editor 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. So what we want

2:04

to do is head over to our web browser. And you want to go over here to this page, it's just

2:09

www.python.org forward slash downloads. 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. And at least that's the version that I have right

2:23

now or download Python 2.7.1.4. So here's the thing about Python, there's actually two major versions

2:30

of Python that are in use currently. There's Python two, and there's Python three. So you can see over

2:37

here, this is like 2.7.1.4. Basically, any Python two version we would just refer to as like Python

2:45

two. And here's like the latest Python three version. And so there's these two versions that you can

2:51

download. And the first thing we have to decide is which version do we want to download. And here's

2:55

the basic difference, Python two is a legacy version of Python, which basically means that

3:02

it's like an old version, and it's not being like actively maintained or supported like officially

3:08

by Python anymore. Python three is like the future of Python. It's like the newest version. It's the

3:14

one that's getting actively maintained and supported. And so there's pros and cons to picking either

3:19

one. A lot of people will say like Python two has been around longer. And so there's more like

3:25

libraries, there's basically just more Python code written in Python two. So therefore, like

3:31

there's a few more things that you can do in Python two, then you can do in Python three. But

3:35

then again, Python two is not supported. And you know, five or 10 years from now, probably no one's

3:41

going to be using it. Python three, like I said, is like the future of Python. It's the newest

3:45

version. It's the one that, you know, is sort of being maintained going forward. So for the

3:50

purposes of this tutorial, I'm going to be teaching you guys Python three. So we're going to want

3:55

to download Python three. Now here's the thing, like, if you learn Python three, or you learn Python

4:01

two, the differences aren't very big. There are a few differences in like some syntax and just some,

4:06

you know, little things here and there. But if you learn Python three, you'll be able to jump right

4:11

in and code in Python two as well. Don't let anyone, you know, tell you that you have to learn one

4:16

or the other, we're going to learn Python three just because it's I think probably the best for a

4:20

beginner to learn. And so that's the one that we want to download. So I'm just going to click

4:24

download Python three. And we're going to go ahead and download this version. And when that's done

4:29

downloading, I'm just going to head down to my downloads folder. And you'll see here, we just

4:34

have this file, I'm just going to double click. And we get this like Python installer. So I'm just

4:40

going to click through here. And we'll be able to install Python three on our computer. All right,

4:45

when that's done installing, we can just close out of this and we should be good to go. So now we

4:49

have Python three installed on our computer. The next step is we need to choose a text editor. So

4:57

we're going to be writing a bunch of code. So we're going to need some, you know,

5:01

environment or some program that we can write all that code in. And really, you can write Python

5:06

in any text editor, you could write it in like notepad or text edit, it doesn't really matter.

5:11

But there are special text editors that are designed just for writing Python code. And these are called

5:18

IDEs. And that stands for integrated development environment. It's basically just a special

5:23

environment where we can, you know, run and execute our Python code. And it'll, you know,

5:28

basically, like tell us how we're doing. So if we're, you know, if we write something that's wrong,

5:32

or we have errors, it'll kind of point us in the right direction of what we need to do to fix that

5:36

stuff. So in this course, we're going to be using an IDE. And one of my favorites, and one of the

5:41

most popular IDEs for Python is called pie charm. So I'm over here on this website, it's called

5:47

jetbrains.com forward slash pie charm. And this is the IDE that we're going to be using in this

5:54

course. So I'm just going to come over here and click this big download now button. And we'll go

5:58

ahead and download this pie charm program. Alright, so I'm getting this page over here. You can see

6:04

there's two versions of pie charm. One is a professional version, and that one's like paid,

6:08

so you have to pay for it. But then there's this community version down here, and that's

6:12

free and open source. So I'm just going to download this one. And this should have everything we need

6:16

to get started using Python. Alright, when that finishes downloading, I'm just going to pull that

6:22

up in my downloads folder. And again, we can just sort of run this guy. And I'm on a Mac. So I'm

6:28

going to have to drag it over to my applications folder. Alright, so we now have pie charm. And we

6:33

have Python three installed on our computer. So we're ready to start programming in Python.

6:39

And this tutorial, we're going to create our first Python program. And we're going to be able to run

6:48

it and see how everything works. The first thing I want to do is open up pie charm. So pie charm was

6:54

the IDE, the integrated development environment that we downloaded in the last tutorial. So I'm

7:00

just going to go ahead and search for it on my computer. And I can just open it up. And when we

7:05

first open up pie charm, we should basically just get a window prompting us to create a project. So

7:10

you can see that down here. Actually, the first thing I want to do though is come over here to

7:14

configure. And I'm just going to click on preferences. And I actually want to change the appearance. So

7:23

I'm going to go ahead and give this a theme. So I just went over here to appearance and behavior,

7:28

appearance. And I'm going to change the theme to dark. Just because I think it's a little bit

7:32

easier to use and look at. But you don't have to do that if you don't want to. And so down here,

7:38

I'm just going to click create new project. And it's going to give us this little window right here.

7:43

So I can just name my project. And I'm just going to name this draft. And then there's this other

7:49

option down here where it says interpreter. 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. So I'm on a Mac

8:02

computer. And by default, Mac has Python version two installed on it. If you're on a Windows machine,

8:09

I'm not sure if it does. But in this tutorial, we're going to be using Python three. So you want

8:14

to make sure that you have Python three selected as the interpreter. Otherwise, you might not be

8:20

able to fully follow along with what we're doing in this video. So now I'm just going to come down

8:24

here and click create. And this is going to go ahead and create our project for us. All right,

8:29

so now we have our Python project up and running. And I want to show you guys how we can create our

8:35

first Python program. So over here in this draft folder, and this is just that project that we

8:41

created, I'm just going to right click and I'm going to say new. And I'm going to come down and

8:46

click Python file. 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. So why don't we just call this app? And I'm just going to click

8:56

okay. All right, so now we have our first Python file open. And we can just start typing in some

9:02

Python. So I want to show you guys just a very basic Python program. And this is just going to be

9:08

a hello world program. So we're just going to print something out onto the screen. So what I

9:12

want to do is I want to type out PRINT print. And I want to type in open and closed parentheses.

9:18

Now inside of this parentheses, we can make some quotation marks. And inside of the quotation

9:24

marks, I'm just going to type out hello world. So this is like a very basic Python program. And

9:30

what this is going to do is it's just going to print out hello world onto the screen. And so now

9:35

what I can do is I can actually 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. And so now in order to run our Python

9:45

program, I'm just going to come up here to the top. And I'm just going to click run. And we'll

9:49

click this run button right here. And now we'll see what happens. So it's asking me what I want

9:54

to run. I can just click app. And down here at the bottom, you'll notice that we have this text

10:00

that just got printed out hello world. And so whatever I print, or whatever I put inside of

10:06

this print statement is going to get printed out down below in the console. So as long as you're

10:10

getting that print statement to work, then you've actually written your first Python program. And

10:15

as we go through forward in the course, we're going to be writing all sorts of awesome Python

10:19

programs. 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. So we're going to

10:30

write just a very simple Python program. We're going to look at how we can go ahead and write

10:35

our programs, 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. So this is going to be pretty

10:44

cool. I'm excited to talk to you guys about just the bare basics of Python, and really just sort of

10:50

give you an introduction. So over here, we have our basic text editor. And so any of the Python code

10:57

that I write inside of this file is actually going to get executed by Python. So this Python file

11:04

has a bunch of Python code inside of it. And basically what we're doing when we're programming

11:10

in Python is we're just giving the computer a set of instructions. Right, that's really what

11:14

programming is, it's just like we're specifying a set of instructions. And the computer is going

11:19

to go off and execute those instructions that we give it. And so the more complex the instructions

11:24

we give to the computer, the more complex the tasks the computer can carry out. So I want to

11:30

show you guys how we can use some Python instructions or some Python code in order to draw out a shape

11:36

onto the screen. So I'm actually going to draw out a little triangle. So over here, we can actually

11:42

print something out onto the screen and Python using something called print statements. So I can

11:47

just type out print. And inside of these parentheses, I can just type some quotation marks and inside

11:54

the quotation marks, we can put whatever we want to print out onto the screen. So I'm going to

11:58

copy this and I'm actually going to make a few of these little print statements. And we're going

12:03

to actually draw a shape. So we're going to draw like a triangle type shape. So down here, I'm just

12:08

going to draw a forward slash. 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. And over here, we're going to make

12:19

three spaces and a forward slash. 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. That'd be kind of cool.

12:30

All right, so now we have our triangle looking shape and you can see it looks kind of like a

12:36

right triangle. So I can save my file. And I can either come up here to run and click run app.

12:43

Or if you're using PyCharm, 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. So down here,

12:52

we have this little window and this is called the console. And basically, the console is just a

12:58

place where Python is going to output some information. So when we use this print statement,

13:03

we're basically telling Python like, Hey, can you print something out onto the console? So there's a

13:08

lot of situations where we're going to want to see what's going on in our code. And we can use

13:13

that print statement and print things out to the console. And we can basically like have a little

13:18

window into what our program is doing. In our case, we're just going to be printing out this

13:22

triangle onto the console. So I'm going to come over here and click this play button. And you'll

13:27

see down here, we're actually printing out that triangle. So mission accomplished, we were able to

13:33

print out our shape. So I could really draw any shape I wanted as long as I was able to specify

13:38

it inside of these print statements. So essentially, what Python's doing when I click that play button,

13:44

is it's going into this file and it's looking at all of these instructions in order. So the first

13:50

thing it does is it goes to this instruction, and it says, okay, they want me to draw out these

13:54

like little forward slash in this vertical bar onto the screen. Once it does that, it goes down

13:59

to this instruction, it says, okay, they want me to print something out again. When it prints that

14:03

out, it goes down to this instruction, etc. So Python is actually going to execute the lines of

14:10

code that we write in order. So the order in which I write the instructions matters a lot. So for

14:16

example, if I was to take this last line here, and put it up here at the top. Now when I save the file,

14:23

instead of just drawing out that triangle, it's going to draw out this little upside down bottom

14:28

thing at the top, and then the rest of the triangle down here. So the order of the instructions

14:34

actually matters a lot. So this is basically how your Python programs are going to go. As we go

14:39

through this course, we're going to learn more and more of these little lines. So here, we're

14:44

just using this little print function, basically, it just prints something out onto the screen. There's

14:48

a bunch of little things like this that we can learn as we go through, we'll learn more and more

14:53

instructions that we can give to the computer. But in this lesson, I just want to kind of show you

14:57

guys the basics, right? We can essentially just define these little instructions for the computer,

15:04

and the computer will execute them in order. In this tutorial, I want to talk to you guys about

15:12

using variables in Python. Now in Python, you're going to be dealing with a lot of data. So a lot

15:19

of times in our program, 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. 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. And when we use a

15:38

variable, when we put those data values inside containers, it makes it a lot easier for us to

15:44

work with and manage all of the different data inside of our programs. So variables are extremely

15:50

helpful. And it's sort of like a core topic in Python that you're really going to want to know

15:55

in master. So in this tutorial, I'm going to teach you guys everything you need to know to get started

16:00

with variables, we're going to look at what they are, why they're useful, and we'll look at the

16:04

different types of variables 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. And it's basically just

16:15

printing some text out onto the screen. And so this is actually a little story that I wrote. It

16:20

says there once was a man named George, he was 70 years old. He really liked the name George,

16:26

but didn't like being 70. So this is a valid Python program. I could go ahead and run this program.

16:33

And you'll see down here in the console, everything prints out and it looks great. So

16:38

we have an awesome little Python program here. But let's say that inside of our story, I wanted

16:43

to change the character's name. So instead of naming the character George, let's say I wanted

16:48

to name the character John. Well, I'm going to have to go through and I'm going to have to manually

16:54

change the name George to the name John at every 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, okay, John, I'm going to have to manually

17:03

change that. And then, okay, down here, we're gonna have to again, manually type in John and

17:08

change it to John. So in order to change the character's name, I had to manually go in and

17:13

change it in both places. And now let's say, okay, maybe we want to change the character's name age

17:20

also. So in addition to changing the name, we also want to change the age, let's make

17:24

them a little bit younger. So why don't we say John is going to be 35. And so you'll see here,

17:30

I had to come here and manually change the age and then come down here and manually change it

17:36

again. Right. And now that we changed it, like it's going to work, it's going to be updated in

17:40

our program. But you'll notice that in order to make that change, like I had to look through the

17:45

entire program, find it where that value was and change it. And this was only with four lines of

17:52

the story. Imagine if I had a story that was like, thousands of lines long. And we mentioned the

17:58

character's name and age, like hundreds of times. I mean, I'd have to look through each one of those

18:02

lines and manually change the character's name. 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, right, the character's

18:14

name and the age. And so we can actually use a variable in order to store the character's name

18:20

and the character's age. And when we use that variable, it'll make it a lot easier for us to

18:25

put the character's name and age inside of our little program here. So I'm going to show you guys

18:30

how we can create a variable for the character's name and age. And I'm just going to go up here

18:35

above this print statement. And up here, I want to create a variable. So when we create a variable

18:42

in Python, we actually need to give Python a couple pieces of information. The first piece of

18:48

information we need to give Python is the name of the variable. So I need to actually assign a name

18:54

to this container where we're going to be storing information. And so all I have to do is just type

18:59

out the name of the variable that I want to create. So I'm going to create a variable called

19:03

character name. So I'm just going to say character. And I'm going to say underscore name. And so

19:10

generally, when you're creating a name for a variable in Python, you want to separate different

19:16

words with an underscore. 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. So basically,

19:27

what I can do is I can just say equals, and over here, I can type a value. So I'm just going to type

19:33

out the character's name inside of quotation marks. So we're just going to type out john.

19:38

So now we have a variable for the character's name. And below this character name variable,

19:44

I want to create another variable called character age. So we're just going to type out character

19:50

underscore age. And I'm going to set this equal to the character's age, which is 35.

19:58

So we now have two variables, one representing the character's name and one representing the

20:02

character's age. So what I can do now is I can replace the character's name inside of the story

20:08

with this variable. And I'm going to show you guys how to do that. So if we want to put a variable

20:13

inside of this print statement, I'm actually going to have to do a couple of things. The first thing

20:18

I'm going to have to do is end off this text in here. So I'm going to have to put a quotation marks

20:25

here at the end. And you can see I'm basically wrapping this whole thing into a single like quoted

20:31

line. And now I also have this text over here at the end. So I'm going to have to put a quotation

20:36

mark here. So now I have a bunch of text here in quotation marks. And then I also have text over

20:42

here in quotation marks. Now inside of here, instead of saying the character's name, I want to refer

20:48

to that character name variable. So I'm just going to say plus character underscore name.

20:57

And I want to say another plus sign. 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. And what this will

21:10

do is actually print out the character's name. We print this. So I'm going to go ahead and run

21:15

this program. And you'll see down here, and actually let me put a space over here. And we'll run it

21:21

again. So you can see down here, 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, it's coming over here and it's

21:32

seeing, okay, they want to print out the value inside of character name. So it Python is going

21:38

to go up here, it's going to get the value. And it's going to insert it inside of there. So I can

21:44

basically do this for every instance of the character name in my file. So I can, you know,

21:49

I'll just copy this guy right here. And I can just paste it down here where the character's name is.

21:55

So I'll paste this. 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. So I'm going to come down here and I'll paste in what

22:06

we just paste it in. And I'm going to change this instead of character name, we'll change it to character

22:12

age. And I'm going to do this in one more spot. So we're going to do this over here where we're

22:18

mentioning the age. And again, I'll just change this to character age. And so now when I run this

22:24

program, it's going to print out the same story that we were printing out before. But now I don't

22:30

actually have to type in the character's name and the character's age. I can just refer to this

22:36

variable. And the cool thing about variables is if I wanted to change the character's name,

22:41

now all I have to do is change it up here. So I could change the character's name to like Tom

22:46

or something. And we could also change their age. So I could change the age to be like 50.

22:51

And now that's going to automatically update throughout our entire story. So when I click this

22:56

play button, you'll see now we're using the name Tom. And he's 50 years old. So that's where variables

23:03

can come in handy. And this can be a really awesome way to control and manage the data that's inside

23:10

of our programs. With variables, you can also modify their values. So for example, let's say that

23:16

halfway through the story, I wanted to change the character's name. Well, I can just make some new

23:20

lines over here. 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. 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. So let's say halfway

23:38

through the story, we want to change the character's name to Mike. Well, now when I run on my program,

23:44

you'll see here in the first part of the story, it says there once was a man named Tom. And in

23:49

the second part of the story, it's referring to the name as Mike. So I actually updated the

23:55

variable inside of my program over here. And that's a really awesome way. You know, a lot of times

24:01

in these programs, you're going to want to be modifying the values of variables. So you can do

24:06

it just like that. So finally, I want to talk to you guys about the different types of data that we

24:11

can store inside of these variables. So over here, I have the character name and the character age.

24:18

And I'm actually storing what are called strings. So a string is basically just plain text,

24:25

right? So the name Tom, that's just plain text. Down here, these guys are strings as well. So

24:31

there's a data type in Python called a string, and it's just basically going to store plain text. So,

24:39

you know, any text that you would have like in a story, or you know, someone's name, you can store

24:44

inside of a string. There's also another type of data that we can store inside of Python,

24:49

which is numbers. So in addition to storing text, we can also store numbers. So if I wanted to store

24:55

a number, for example, 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. So I could just type out 50. And when we're storing a number,

25:07

we don't need these quotation marks, you only need that quotation mark when you're storing a

25:12

string. So if I want to store a number, I can just write it out like this. And in addition to

25:17

using whole numbers, I could also use decimal numbers. So I could say like 50.5678213. And

25:24

Python's going to be able to store that number as well. So you can store all different types of

25:29

numbers. And we can also store what's called a Boolean value. And a Boolean value is essentially

25:36

a true or a false value. And there's a lot of instances in programming where we're going to

25:42

want to represent true or false data. For example, I could have a variable called like is mail. And

25:49

this would tell me whether or not someone was a male. And in my case, it would be true because

25:54

I'm a guy, right? So I can actually this needs to be capital. So I could sort of value of either

26:01

true or false. And actually, if we were naming this in Python, we want to use an underscore. So I

26:08

would say is underscore mail. And this can be either true or false. So true or false values is

26:14

maybe not something that you're used to dealing with like in the real world. But in programming,

26:19

we're going to be using true or false values all the time. They're super important. So like I

26:25

said, there's three basic types of data that we can work with in Python. There's strings,

26:30

which is just like plain text numbers, which would be like either decimal numbers or whole

26:35

numbers like this. And there's true or false values. And these are the basic types of data. I mean,

26:41

there's a bunch of other types of data that we can use. But I would say 99% of the time as a new

26:46

user and a new programmer to Python, 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. And you're going to be using

26:57

variables all the time in Python. So you definitely want to practice up and get comfortable using them.

27:06

In this tutorial, I want to talk to you guys about working with strings in Python. Now one of

27:12

the most common types of data that we're going to be working with in Python is going to be strings.

27:17

And strings are basically just plain text. So any text that we want to have inside of our program,

27:23

we can store inside of a string. So I want to talk to you guys about all the cool things we can do

27:28

with strings. And we're basically just going to get a full introduction into why strings are

27:32

awesome. So over here, I'm just going to actually print out a string. So I'm just going to say print

27:39

and inside of these parentheses, I can type out a string. In order to create a string, I need to

27:45

use quotation marks. So I can make an open and close quotation marks just like that. Now inside

27:50

of the quotation mark, I can put whatever text I want the string to have. So we could say like,

27:56

draft Academy. And so now we have a string with the text draft Academy inside of it. So if I run

28:03

my program, now down here, we're going to print out draft Academy, as you can see. And when we have

28:08

these strings, there's actually a bunch of cool things that we can do with them. So one thing I

28:12

could do would be to create a new line inside of the string. So if I want it, I can come over here,

28:18

and I could say backslash n. And you can see it got highlighted in a different color. And what this

28:24

is going to do is it's actually going to insert a new line into the string. So now it's going to say

28:28

draft on one line and Academy on another line. So I'm going to click play. And you'll see down

28:35

here, we just get draft Academy. In addition to the backslash n, I could also use a backslash

28:42

quotation mark. So if I wanted to put a quotation mark inside of my string, I can't just put a

28:48

quotation mark like that, because Python is going to think that I'm trying to end this string. So if

28:53

I want to include a quotation mark, I can just use this special backslash character. And that's

28:58

called the escape character. And it basically just tells Python that whatever character comes

29:03

after it, we want to render literally. So when I say backslash quotation mark, it basically means

29:10

like, Hey, Python, I want to print out a quotation mark. And so now we'll be able to print out a

29:15

quotation mark right there. I can also use this to print out a backslash. So if I needed to print

29:19

out a backslash, I could say backslash. And it'll just print out a normal backslash now. So you can

29:26

see just like that. So if you want, you can use that backslash to make new lines or print out

29:30

quotation marks, or you can just use it as a normal backslash. In addition to just typing out a string

29:37

here, I could also create a string variable. So I could come down here up here, and we can create

29:42

a variable and we'll just call it phrase. And I'm going to set it equal to draft academy. So I can

29:49

store this string value inside of a variable called phrase. And then when I want to print out that

29:55

variable, or I want to access that string variable, I can just type the name of the variable. And

30:02

you'll see that it's going to print out the value that was stored inside of it. I can also use

30:07

something called concatenation. And concatenation is basically the process of taking a string and

30:13

appending another string onto it. So I could come over here and I could say phrase, and I can say

30:18

plus. And now I can add in another string. So I could say like, is cool. And now this is going to say

30:25

draft academy is cool. So I'm basically appending another string onto another one, they call that

30:32

concatenation. And so in addition to doing all that stuff, we can also use special things called

30:38

functions. And a function is basically just a little block of code that we can run. And it will

30:44

perform a specific operation for us. So we can use functions to modify our strings. And we can

30:51

also use functions to get information about our strings. So I'm going to show you guys a couple

30:57

common functions we can use with these strings. And they're actually going to do awesome stuff. So

31:02

in order to access one of these functions, I can just say phrase, and then I can say dot. And I'm

31:07

going to show you guys a function that we can use to convert this string entirely into lower case.

31:12

So I could just say phrase dot lower. And I'm going to want to type an open and close parentheses.

31:17

And this will take this phrase, it'll take my string and it'll convert it to lowercase. So you

31:22

can see now we just have draft academy down here, but it's entirely in lowercase. I could also do

31:27

the same thing for uppercase. So I could say phrase dot upper. And this will convert the entire string

31:34

into uppercase. You can see now it's entirely uppercase. So in addition to converting the string

31:39

into uppercase and lowercase, I could also check to see if a string is entirely uppercase or

31:45

entirely lowercase. So for example, I could say phrase dot is upper. And this is going to give me

31:51

back a true or a false value. So it's going to be true. If the string is entirely uppercase,

31:56

or false, if it's not. And you can see here, we're getting a false value because this is an

32:02

uppercase. I can also use these functions in combination with each other. So for example,

32:08

I could say phrase dot upper, and then parentheses. 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, it's going to convert it

32:22

into uppercase. And then it's going to run this is upper function right after that. And you'll see

32:27

now we're going to get a true value back because it will have converted the whole thing into uppercase.

32:33

So you can see now we're getting a true value. So you can use these functions one after another,

32:38

it can be really useful. And there's a few other ones that I want to show you. So we can also

32:43

figure out the length of this string. So if I wanted to figure out how many characters were

32:49

inside of this string, I could just say LEN. And I'm actually going to make an open parentheses.

32:54

And I'm going to make a closed parentheses. So I'm essentially saying LEN. And this is another

33:00

function, it's the length function. And inside of this LEN function, I'm actually passing this

33:06

phrase variable, I'm basically giving the length function 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. And so you can see here we get 15

33:16

because there's 15 characters inside of draft academy. So that's how we can get the length of

33:22

a string. And that's going to be really useful as we go forward and Python, we can also get

33:26

individual characters inside of a string. So imagine if I wanted to just grab one of these

33:32

characters, like imagine if I wanted to figure out what the first character in this string is,

33:37

we can actually use an open and closed square bracket, just like that. And in here, I can

33:43

specify the index of the character that I want to grab. So if I wanted to grab the first character

33:49

in this string, I can put in a zero. So if I say phrase square bracket zero, this is going to give

33:55

me that capital G. And you can see down here, that's what gets printed out. Now I want to point

34:01

something out in Python, 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. And that's because

34:14

in Python, when we use indexes on a string, we start with zero. So if Python is counting the

34:20

characters, or it's indexing the characters in a string, it's going to start with zero. So it's

34:25

going to say G is zero, i is one, r is two, a is three. So we would say that G is at position zero

34:33

in the string, i is at position one, r is at position two, a is at position three, etc. So we start

34:39

counting at zero. So if I wanted to access the first character in the string, this G, I have to say

34:48

phrase zero, and that's just a special thing in Python, and actually most programming languages

34:53

do that. So they'll start with zero. So for example, if I wanted to access this a, I'm going to have

34:59

to put zero 123. So if I put three inside of here, now we'll be able to access that first a,

35:09

as you can see, we get an a right here. So this is actually really useful being able to grab a

35:14

specific character inside of a string, and you're going to be using that a lot as we go forward

35:19

in Python, there's also another really awesome function that we can use and it's called the index

35:24

function. And basically what the index function will do is it'll tell us where a specific character

35:31

or string is located inside of our string. So I could say phrase dot index. And I can actually

35:38

give this a value. So sometimes when we use these functions in Python, we can actually give them

35:44

information. So I can give this a value. And we would call this passing a parameter. So I would

35:50

call a value that I would give to a function a parameter, and you'll hear that word a lot. So for

35:56

example, I could say inside of here capital G. And now this is going to return the index of the

36:03

capital G inside of our string. 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. So for example, if I put an a

36:16

inside of here, like a lowercase a, it should give me 0123, because that's where the first a is inside

36:23

of this string. So I can click the play button. And you'll see it's giving us a three, you can also

36:29

put actual words in here. So for example, I could put like Academy in here, or I could even just put

36:35

like a C A D. And this is going to tell me where this starts inside of my string. So when I click

36:42

play, it's going to give me an eight 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, if I put a Z in here,

36:55

that's not in here, it's actually going to throw an error. So when I play this, you'll see that we

37:00

get this error down here because Z is not found inside of our program. So that index function,

37:05

again, can be really useful. And we'll be using that a lot. There's also one more that I want to

37:10

show you, which is called replace. So we can say phrase dot replace. And in here, I can actually

37:16

give this to parameters. So I can give this replace function to values that it can use. And the first

37:22

thing I want to you to give it is what I want to replace. So for example, I could say draft.

37:28

And then I'm going to put a comma in here. And I want to put in here what I want to replace

37:33

draft with. So I could just say like elephant. And now instead of saying draft Academy, this is

37:38

going to print out elephant Academy. So you can see down here, it just says elephant Academy. So

37:44

this replace function can be really awesome because we can replace certain words or even certain

37:50

letters inside of our strings with other ones. So those are just some basic ways that we can work

37:55

with strings inside of Python. And there's a lot of these different functions that we can use with

38:01

strings. These are some of the most common the ones that I just showed you right now. But if you

38:05

just do a Google search, you can find all sorts of Python functions that you can try out and use and

38:10

you know, see what they do. 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. In this tutorial, I want to talk to you about

38:23

working with numbers in Python. Numbers are one of the most common data types in Python. And any

38:31

Python program you write is most likely going to be dealing with numbers at some point. So I want

38:36

to talk to you guys about just the basics of using numbers, we're going to talk about the

38:40

different types of numbers that we can represent in Python. And I also want to show you guys some

38:44

awesome functions that we can use with numbers. So we can do certain like mathematical operations,

38:50

we can do some awesome things with numbers. I'm going to show you guys all of that in this video,

38:55

it's going to be awesome. 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. I basically just write the number.

39:04

So I could say like print two. 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. I could also use a decimal number. So I could say like

39:15

2.0987. And Python is going to be just fine with that. We'll be able to print that out under the

39:21

screen as well. I can also make numbers negative. So I could make this 2.0987 a negative number. And

39:28

Python will be able to handle that just fine too. And Python, we can also use basic arithmetic. So

39:36

I could do things like addition subtraction multiplication. So I could say like three plus

39:41

four. And now this is going to print this out. Or why don't we say like three plus 4.5. And this

39:47

will be able to print out the answer. So we get 7.5. And we can do addition, we can do subtraction,

39:53

we can do division, or we could do like multiplication as well. So you can pretty much do whatever you

39:58

want inside of Python. And if you want to make more complex mathematical equations, we can also

40:04

use things to specify order of operations. So I could use parentheses. So for example, if I said

40:11

three times four plus five, what Python's going to do is it's going to multiply three and four

40:17

together. So it's going to be 12. And then we'll add five to that. So we should get 17. But let's

40:24

say that we wanted to change up the order, right? I could actually put a parentheses around four

40:28

and five. And now it's going to add them first. So it'll add four and five. So we'll get nine,

40:34

and we'll multiply that times three. So we should end up with 27. So you can see we can use those

40:39

parentheses to specify order of operations. So I could make like a very complex, a little

40:45

equation here just by using numbers and parentheses. We can also use one other operation, which is

40:52

called the modulus operator. So I could say like 10, and use this percent sign, three. And this is

40:59

actually read 10 mod three. And basically what this is going to do is it's going to take the first

41:04

number divided by the second number, and it's going to spit out the remainder. So it's basically

41:10

going to say, okay, 10 divided by three, that's going to be three with a remainder of one. And so

41:15

this is going to give us the remainder. And you'll see down here, we get one, because that's the

41:21

remainder of 10 divided by three. And that modulus operator can come in handy quite a bit. We can

41:26

also store these numbers inside of variables. So we can store them inside of these variable

41:32

containers. So I could come over here, 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. And so I could come down here,

41:44

and I can just print out my underscore num. And we'll be able to print that number out to the

41:49

screen just fine. So in addition to doing things like a basic arithmetic, we can also do some other

41:56

stuff. So one thing I could do is I could actually convert this number into a string. So if I wanted

42:01

to convert this number into a string, I can just say str, and I can put the number that I want to

42:07

convert inside of these parentheses. And so what's going to happen now is this number is going to

42:12

be converted into a string. 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. And this is going to come in handy when

42:24

you want to print out numbers alongside strings. So I can come over here and I can say like, my

42:32

favorite number, and we'll be able to print this number along with this string. So I can

42:38

click play. And it says five my favorite number. If I was to get rid of this string right here,

42:44

and I wasn't converting the number into a string, now Python is going to have a problem with this.

42:49

It's not going to allow us to do that. You can see we get an error. So anytime you want to print

42:54

out a number next to a string, 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. And a lot of times in Python,

43:04

you're going to want to be using specific, like more advanced math operators. So there's a bunch

43:10

of different math functions that we can use on our numbers. And a function is basically just like

43:16

a little collection of code that does something. So a function could perform an operation like

43:21

a mathematical operation on our number. It could also give us information about our number. So

43:27

I'm going to show you guys some of the most common functions that you'll be using in Python

43:31

related to numbers. The first one is called ABS, and it stands for absolute value. So we can get

43:38

the absolute value of a number. And so over here, I'm just going to make this variable negative five.

43:46

And basically, I can just say ABS, 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. So when I click the play

43:57

button, you'll see it's just giving us five, because five is the absolute value of negative five.

44:03

I can use a few other functions. There's another one which is called pow. It's so it's just POW.

44:09

And I'm actually not going to use this variable. We just use a normal number. And this function is

44:14

going to allow us to pass it two pieces of information. So I can give this function two pieces of

44:19

information. The first is going to be like a number. And the second is going to be the power

44:25

that I want to take that number to. So I could say like three comma two. And this is basically

44:31

just going to be three raised to the power of two. So it's just going to be three squared. So we

44:37

should get nine down here. You can see that we do. So I could pass in like, you know, some crazy

44:42

number, like we could pass in a four and we could raise it to the power of six. And so we should

44:48

get like a pretty big number here. And you can see we get 4096. So this pow function is really

44:54

useful for taking numbers to specific powers. So in addition to using this pow function, we can

45:00

also use another function which is called max. So I can say max. And what this is going to do is

45:06

it's basically going to return the larger of the two numbers that we pass into it. So right now I'm

45:12

giving this a four and a six. And this should tell us which number is higher. So over here,

45:17

it's just printing out six because that's the bigger number. I can also use another function

45:22

called min. And this is going to do the opposite. So now instead of printing out the max number,

45:26

it's going to print out the smallest number. And it's going to print out four down here, as you can

45:31

see, another cool function is called the round function. And this is going to allow us to round

45:36

a number. So it's just going to follow like standard rounding rules. So if I said like 3.2

45:42

inside of here, now it's going to round it down to just normal three. But if I said like 3.7,

45:49

it's going to round it up to four. 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, but in order to get access to them,

45:59

I'm actually going to have to do something called importing. And in Python, we can actually import

46:05

external code into our files. And so if I want to access these specific math functions,

46:12

I have to import something called Python math. So I can just say from up here, math, import,

46:20

and this star. And basically, what this is going to do is it's going to go out and it's going to

46:25

grab a bunch of different math functions that we can use. So I already showed you guys a few of

46:30

these different math functions, but there's actually a bunch more. And in order to access them,

46:35

we're going to need to include this line of code inside of our file. So once we have that,

46:39

now I can access a few others. So there's another function which is called the floor method. And

46:44

it's just FLOR. And what this will do is it'll basically just grab the lowest number. So it's

46:50

essentially just going to chop off this decimal point. And you can see over here, now we get a

46:55

three, because we're using that floor function, there's also another one called seal. And that's

47:00

just going to do the exact opposite. So that's just going to round the number up no matter what. So

47:05

no matter what, we'll always get four with we have a three point whatever here. And there's

47:10

another one which is called square root. So it's just SQRT. And essentially, this is just going to

47:16

return the square root of a number. So I could say like 36. And now we should get six back. So

47:24

those are awesome, very interesting functions. And inside of this math module, so we would call

47:30

this a module. And you don't have to worry too much about what that is right now. Just know that

47:34

when we put this line of code into our program, it gives us access to a lot more math function.

47:41

So it basically allows us to do a lot more things with math inside of our program. And there's a

47:47

bunch of different math functions in Python. And if you want, you can basically just go online and

47:52

search for different math functions. I showed you guys a few here. 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. And like I said,

48:03

you know, there's tons of documentation on all this stuff. So you can just look up, you know,

48:07

math functions in Python. 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. But for now,

48:17

that's just been a little bit about using numbers a little bit about doing, you know,

48:21

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. So we're basically going to

48:34

allow a user to input information into our program. And I'm actually going to take the

48:40

information that the user inputs, I'm going to store it inside of a variable. And then we're

48:45

going to be able to do something with that variable. So I want to show you guys how we can actually

48:50

go about getting input from someone. And down here in my Python file, all I have to do to get

48:57

input from someone is I just have to type out input, 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, we want to get

49:08

input from a user and Python will allow the user to type in some information. And inside of these

49:15

parentheses, we can actually type a prompt. And so generally, when we want the user to enter in

49:21

some information, we want to tell them what we want them to enter. And that's what we can put

49:25

inside of here. So I'm just going to say, enter your name. So we're actually going to create a

49:32

little Python app, which is going to allow the user to enter in their name. And then we'll store

49:37

the name inside of a variable, and then we'll basically just say hi to the user. So we'll say

49:42

like, hi, and then whatever the name they entered was. So what I want to do is I actually want to

49:48

store whatever the user inputted into our program inside of a variable. So I can actually go over

49:55

here, and I can create a variable, I could just call it name. And I could set this variable

50:01

equal to input. 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. And so now I have access to whatever

50:16

they entered in. So what we can do is we can say print. And inside of the parentheses, I can just

50:23

print out hello. And then I'm just going to add on to this name. And actually, why don't we put

50:30

an exclamation point? So essentially, we're getting a name from the user, we're storing it inside of

50:36

the name variable. And I'm printing it out onto the screen. So I'm going to go ahead and play this.

50:42

And when we run the program, you'll notice down here, Python is giving us this little prompt. So

50:48

it's saying enter your name. Now in here, I can actually type in a value. So I can type in Mike.

50:54

And then I can just hit enter. 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. And so this is kind of cool. And actually, what we

51:06

could do is we could prompt the user to enter in more than one piece of information. So I could

51:12

actually copy this line of code. And I'm going to paste it below here. And why don't we have the

51:18

user enter in another variable? So we could have them enter in, for example, their age. And over

51:24

here, we just want to say enter your age. And so now we're able to get two pieces of information

51:30

from the user. And I could say hello, name. And then I can say, you are, and I'll print out their

51:38

age. So I'm just going to print age. So now I'm basically able to get both of these pieces of

51:44

information from the user, I can get their name. And I can get their age. So when we run this program,

51:52

I can type in both of those pieces of information. So to say, answer your name. So we could just

51:57

make up a name like john, and then enter the age, let's say john is 25. And now it's going to say,

52:03

Hello, john, you are 25. So the program is able to get input from the user. And then it's able to

52:09

use that information in order to say hi to the user and tell them how old they are. And so

52:15

getting input from the user is a great way to make your programs more interactive. And here,

52:21

we're just scratching the surface, right? We're just basically getting to, you know, a piece of

52:25

information from the user, but you can use these inputs to do awesome things in Python. And as we

52:30

go through this course, 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 how to build a very basic calculator in Python,

52:45

we're basically going to build a calculator where we'll get two numbers from a user,

52:49

and then we'll add those numbers together and print the answer out onto the screen. So let's

52:54

look at some practice in getting input from users. And we'll also talk about getting numbers from

52:59

users as opposed to just strings. So I'm really excited to get started on this calculator. The

53:05

first thing we want to do is create two variables and inside of those variables, we want to store

53:11

the two numbers that the user wants to add together. So down here, I'm just going to say,

53:17

num one. And that's what we'll call our first variable is equal to. And now I need to get

53:23

input from the user. So I'm going to say input. And inside these parentheses, I'm going to say,

53:28

enter a number. 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. And I'm just going to say num two. And then we'll just say,

53:45

enter another number. 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. So I'm actually going to create a variable

53:58

called result. So I'm just going to say result is equal to, and basically, we just want to add

54:03

these two numbers together. So I'm going to say num one plus num two. And now all we have to do

54:09

is just print out the result. So I'm just going to print result. So we basically have our entire

54:16

program written out here. And this will allow us to enter in two numbers, and then we'll add them

54:21

together and print out the answer. So I'm going to click play. And down here, we can enter our number.

54:29

So I'm going to enter the first number, we'll say five. And I'm going to enter my second number.

54:34

So let's say 8.0, or let's say 8.3. So now we should print out the answer. But if you look over here,

54:44

that's not quite the answer we were looking for, right, five plus 8.3 is definitely not 58.3. And

54:53

here's what happened. When we get input from a user by default, Python is just going to convert

55:01

it into a string. 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. Sounds good, right? It's just

55:12

going to make it a string. 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 that we get from the user into numbers. And so

55:24

the way that we can do that is by basically just coming over here. And when we add the two numbers

55:30

together, I can use a special Python function in order to convert those strings into numbers.

55:37

And there's actually two Python functions that we could use for this. The first is called int.

55:43

So I could say int, I can make an open parentheses and I can make a closed parentheses. And basically

55:50

what this is going to do is it's going to convert whatever is inside of these open and closed parentheses

55:54

into an integer number. And an integer number is basically a whole number. So it's like one,

56:02

two, three, four, five. So it's a, it's a whole number. In other words, it can't have a decimal

56:07

point. And so what I could do is I could say, int, and I could put these inside of parentheses.

56:15

And now what this is going to do is it's going to convert both of these numbers into

56:19

integers. In other words, into whole numbers that don't have decimals. And it's going to add

56:25

them together and we'll print out the results. So why don't we do that? And we'll just see if

56:29

this works. So I'm going to click the play button. And down here, we'll enter our first number. So

56:34

I'm just going to enter in a four. And then I'll enter in a five. And now you can see it's

56:38

printing out the correct answer four plus five. And so that's essentially our basic calculator.

56:45

But we do have one problem with this calculator. So I'm going to run it again. And I'll show you

56:49

guys with that problem is 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. And that's because I'm using this int function

57:03

up here. And this int function is looking for a whole number. So if I put decimal numbers

57:09

inside of there, it's going to break the program. There's another function we can use

57:14

instead of an int, we can say float. And a float is basically just a number that has

57:19

decimal. So it's a decimal number. So I can convert both of these guys into floats.

57:24

And now I'll be able to use decimal numbers. So I can click play. And I can just enter in some

57:30

numbers. So I could say like four. And I could also say like 5.5. And now it's going to be able

57:36

to add both of those numbers, even if one of them is a decimal. And that's the beauty of using that

57:42

little float function. So in certain circumstances, you might want the user to only be able to enter

57:47

in an integer to a whole number. But in a lot of cases, like in this calculator, we want them to

57:52

we want them to be able to enter any number that they want. So we're going to use this float

57:56

function. So I'm just going to recap what we did one more time, we created two variables,

58:01

num one and num two. And we basically stored the two numbers that the users input into those

58:07

variables. And then we converted them into numbers. And we added them together and we printed out

58:14

the results. So this is a pretty cool calculator. I'm happy with it. Hopefully you guys learned

58:18

something. And as we go forward in this Python course, you're going to learn all sorts of ways

58:23

that we can make this calculator more complex. In this tutorial, I want to show you guys how to

58:31

build a madlibs game in Python. So a madlibs game is basically just a game where you can

58:38

enter in a bunch of random words. So like, you know, verbs, nouns, names, colors. And you basically

58:46

take all of those words and put them into a story randomly. So over here, my web browser,

58:52

I just have an example of a madlibs. So, you know, down here, we're basically just taking random

58:57

words, like an adjective or a noun or a plural noun, and entering them in side of a story. So

59:02

like you read through the story and then like some random word pops up and madlibs can be pretty

59:06

fun. And I'm sure you guys have, you know, at least heard of madlibs before. But if not,

59:11

that was just a little introduction. So I want to create a madlibs in Python. And this is going

59:15

to be really cool. So down here, I have this like little poem, which just says, roses are red,

59:20

violets are blue, I love you. And so, you know, this is kind of like a classic poem. But I think

59:26

we should mix it up a little bit and create a madlib for this poem. And so instead of having this red

59:33

color over here, I want to have the user enter in their own color. Instead of having violets,

59:40

why don't we have them enter in a plural noun? And instead of saying I love you, why don't we have

59:47

them say I love and then some celebrity? So celebrity. So this is the basic structure for our little

59:55

madlib, right? I want to basically have the user enter in a color, enter in a plural noun,

60:00

and enter in a celebrity. And then I'll take all of those words and put them inside of our

60:05

madlib. And hopefully we end up with something that's pretty funny. So how can we actually go

60:10

about building this in Python? Well, the first thing we're going to have to do is we're going to have

60:15

to be able to get these words from the user, right? That's pretty obvious. So I think what we should

60:20

do is we should create three variables called color plural noun and celebrity. And we'll store the

60:27

user's inputs inside of those variables. So up here, let's create those variables. The first

60:33

variable I want to create is going to be called color. So say color. And I want to set this equal

60:38

to the input. So I'm going to say input. And inside of here, I'm just going to give them a prompt. So

60:43

I'll say enter a color. And we can basically just copy this line of code. And I'm going to paste

60:50

it down below. And we'll paste one more time. So now we're going to create our plural noun

60:56

variable. So I'll say PL, URL, plural noun. And we'll just say enter a plural noun. And actually,

61:06

over here, I want to make a colon as well. So then finally, we'll create our celebrity variable

61:12

and we'll say enter a celebrity. And so now basically, we're creating three variables called

61:19

color plural noun and celebrity. And inside of them, we're storing the color that the user enters,

61:26

the plural noun, the user enters and the celebrity, the user enters. So last thing we have to do is

61:32

replace these guys with those variables. So I'm basically just going to add in the color over here.

61:40

And we're going to add in the plural noun. So I can just go like this.

61:45

And finally, we'll just add in that celebrity. So now we have a pretty cool mad lib. And it should

61:59

be functional. Everything should work. It looks like it's good to go. So let's come over here and

62:04

we'll run our program. And then down here in the console, we can play our little mad libs game.

62:11

So let's say enter a color, I'm just going to say magenta, enter a plural noun. Why don't we say

62:19

microwaves and enter a celebrity, let's say Tom Hanks. So when I click enter, what should happen

62:27

is we should get our mad libs printed out. So I'm going to click enter. And now we have our mad

62:32

libs. So it says roses or magenta, microwaves are blue. I love Tom Hanks. So it's a pretty good mad

62:38

lib. And obviously, you know, the more the more print statements we put over here, you know,

62:44

the better the mad lib could could be. And we could store as many, you know, variables up here as we

62:48

wanted. This was just a very simple mad lib. We only took in three inputs, but you know, you can

62:54

see how you could take something like this and create a little mad libs game. So your homework

62:59

for this lesson is to go off and make your own mad libs game. Hopefully one that's a little bit

63:03

funnier than mine. But that's the basics of creating a mad libs game in Python.

63:13

In this tutorial, I want to talk to you guys about working with lists in Python. A lot of

63:18

times when you're programming in Python, you're going to be dealing with large amounts of data.

63:23

And when you're dealing with large amounts of data, you want to make sure that you can

63:26

manage it and organize it properly. And the list is essentially just a structure that we can use

63:32

inside of Python to store lists of information. So we can take a bunch of different data values,

63:38

we can put them inside a list, and it allows us to organize them and keep track of them a lot

63:43

easier. So generally, you would create a Python list, and you would put a bunch of related values

63:49

inside of that list, and then you can use it throughout your program. So I'm going to show you

63:54

guys just the basics of using lists, we're going to look at some of the common use cases and

63:58

really just get you up to speed with what lists are and why they're useful. So over here in my

64:03

program, I'm actually going to create a list. And we create a list a lot like we create a

64:08

Python, just like a normal Python variable. So the first thing we want to do when we're making a

64:13

list is give it a name. So we want to give it a descriptive name, something that's going to

64:18

basically describe what's inside the list. In my case, for this example, let's make a list of

64:24

like friends. So we could say like here's a list of all my friends. So I can say friends.

64:30

And I'm going to set this equal to the list of values that represent my friends. And we want

64:35

to create a list, we can use these open and closed square brackets. So whenever you use these open

64:41

and closed square brackets, Python basically knows, okay, they want to store a bunch of values inside

64:47

of this list. So why don't we go ahead and start adding some values? So we can say like my friends

64:52

are like Kevin, Karen, and let's say Jim. So here I have three values inside of my friends array.

65:05

So when we're creating a normal variable in Python, generally, we just give it one value,

65:11

right? So I would create a variable, I would give it one value, maybe a string or a number or a

65:16

boolean. But when we make lists, we're able to store multiple values inside of the same like little

65:23

item inside of the same object, right? And then what I can do is I can access these individual items

65:29

inside of my program. So inside of this friends list, we're basically storing all of these different

65:36

values, and that can be really useful. So now that we've stored some values, let's talk about what

65:41

you can put inside of a list. So you can really put any piece of information in Python, we could

65:46

put things like strings, numbers, or booleans. So if I wanted, instead of just putting strings

65:51

here, I could change like one of these to a number, and I could change one of these to a boolean,

65:56

and it wouldn't matter, Python is going to be fine with that. But let's change these back to

66:01

strings just for the sake of the example. So now that we've created our list, how can we access

66:07

individual elements inside of this list? It's a good question. I'm gonna go ahead and print

66:12

something out to the screen. The first thing we can do is we can just print out the actual list.

66:16

So I can just say print friends. And then when I run this program, if we look down here in the

66:22

console, you can see it's just printing out the actual list of friends, right? But let's say that

66:27

I wanted to print out a specific element inside of this list. Well, I can actually refer to elements

66:33

by their index. So each one of the elements inside of this list has a particular index. And the index

66:40

is actually start at zero. So we would say that this first element in the list, Kevin has an index

66:46

of zero. We would say that this other element Karen has an index of one. And this third element

66:53

Jim would have an index of two. So even though Jim is technically the third element inside of the

66:59

list, it has an index of two. And that's because we start indexing index position one. And so that's

67:07

just kind of how lists work. And that's how list indexes work. So if I wanted to access a specific

67:13

element inside of my friends list, I can just refer to it by its index. So over here after we

67:19

type out friends, I'm just gonna type an open and closed square bracket. And inside of this open

67:24

and closed square bracket, I want to put the index of the element inside the list that I want to

67:29

access. So if I wanted to access this guy right here, this element, I can just put a zero. So I can

67:35

say friends zero. And now when I run my program, it's just gonna print out that value, it's just

67:41

gonna print out Kevin. If I wanted to access this Jim value over here, I can put a two. And that

67:47

will give us that Jim value, you can see right there. So we can access elements based off of

67:53

their index, we can also access elements based off of their index from the back of the list. So

68:00

for example, if I said negative one inside of here, this is gonna give me access to this value

68:06

right here for Jim. And you can see that sort of does so when we use negatives, it starts indexing

68:11

from the back of the list. So if I said negative two, this is gonna give us Karen. And so you can

68:16

use the negatives to access items at the back of the list. Just keep in mind though, and this is kind

68:21

of interesting. We would say this first element here is index position zero, right? But we would

68:29

say the first element at the back of the list is index position negative one. So just keep that

68:34

in mind. So there's actually other ways that we can access like just portions of this list. So for

68:39

example, let's say I just wanted to select the last two elements in this list. And I didn't want

68:44

to select the first one. Well, in here, I can just say one with a colon. And this will grab the

68:50

element at index position one and all of the elements after that. So now we should just grab

68:55

Jim and Karen or Karen and Jim in that order. I could also specify a range. So actually,

69:02

let me add in a couple other elements just so we can illustrate this better. So I'll add in two

69:08

more names. So if I wanted to, I could select like the element at index position one up to the

69:13

element at index position three. So it's going to grab this one. So that's one, two. And it's actually

69:20

not going to grab the third one. So it's it's going to grab all the elements up to but not

69:25

including three. So it'll grab Karen and Jim. So let's go ahead and run this. And you can see

69:30

it grabs Karen and Jim. So you can either just put like one with a colon after it, and then it'll

69:36

grab that index position and everything after it, or you can specify like a range that you want to

69:41

grab from. So that's how we can access elements inside of this list. But we can also modify elements.

69:47

So for example, I could come down here, let's say I want to modify this value right here, Karen.

69:53

Well, all I have to do is just access that element. So I could say friends and Karen is at index

69:59

position one. And I can change this to whatever I want. So I could say friends index position one

70:04

is equal to Mike. And now when we print out friends index position one, that value is going to be

70:11

updated. So it's just going to say Mike instead of saying Karen. So it can be really useful to

70:16

modify values inside of arrays. So it can be really useful to modify values inside of Python lists.

70:23

So those are kind of the basics of working with lists in Python. And actually in the next tutorial,

70:27

I'm going to show you guys even more ways that we can use these lists, we're going to learn how

70:32

to like add elements to a list, delete elements from a list, copy lists, and a bunch of cool stuff

70:38

with these lists. But for now, this has just been sort of like the basics of working with lists.

70:47

In this tutorial, I want to talk to you guys about using functions with lists in Python.

70:53

And Python, a list is one of the most important structures where we can store our information.

70:58

A list basically allows you to take a bunch of different values and organize them and store them

71:03

inside of their own little list structure. So down here in my program, you can see I have two lists.

71:10

I have a list of lucky numbers. And this is just a bunch of different numbers. And then I have a

71:14

list of friends. So this could be like a list of somebody's friends. And it's basically just a

71:18

bunch of strings with names in them. So these two lists are storing a bunch of different values.

71:25

And what makes lists awesome is not only could I store like this many values, I mean, I could store

71:30

hundreds or thousands or millions of values. And when you're working with a lot of data and a lot

71:35

of information in Python, these lists are invaluable. So because lists are so important, there's actually

71:41

a bunch of different functions that we can use with lists. And those functions allow us to do

71:46

things like modify the lists and get information about the list. So in this tutorial, I just want

71:52

to show you a bunch of those functions, we're going to talk about some of the most common and

71:56

the most popular functions that you would use on lists to make them more powerful and make them

72:01

more easy to use. So the first thing we can do is obviously just print out a list. So down here,

72:07

I'm printing out this friends list. And when I want to run my program, you see down here,

72:13

we're just printing out all the elements in the list. So it's always good to be able to print out

72:18

all the elements inside of a list. But there's a lot of other functions that we can use. So

72:22

one function is called the extend function. And the extend function will basically allow

72:27

you to take a list and append another list onto the end of it. So I could say like friends dot

72:34

extend. And inside of these parentheses, I can pass in the list that I want to add on to the

72:40

friends list. So I could say like lucky numbers. And now when I print out this friends list,

72:47

not only is it going to have all the elements in the friends list, it's also going to have all

72:50

the elements inside the lucky numbers list. You can see down here, we have all of our friends.

72:55

And then we also have the numbers over here. So that's a really easy way to essentially just like,

73:00

add two lists together. We can also just add individual elements onto a list. So I could come

73:07

down here and I could say friends dot append. And this will actually allow me to append another item

73:14

onto the end of this list. So over here, I could just add another name. So we could say creed, for

73:20

example, and now I'm like adding another friend onto the end of this list. So when I run the program,

73:26

in addition to having the original items that were in the list up here, we also have this

73:31

additional item at the end, and that append function is always going to add the item onto the end of

73:36

the list. But let's say that you wanted to add an item into the middle of the list. So like right

73:41

here or right here, there's another list function called insert. And insert is going to take two

73:49

parameters. The first parameter is going to be the index where you want to insert the item. So

73:54

why don't we insert it here at index position one? I'm just going to say one. And then I'm going to

74:00

type in the name of the element that I want to add. So we can just say like Kelly. And now at index

74:06

position one, we're going to have the value Kelly, and all of these other values are going to get

74:11

pushed up, they're going to get pushed to the right one index position. So now I run the program,

74:18

you can see we have Kevin, this new element Kelly, and then all the other elements have been pushed

74:23

off to the right, we can also remove elements. So for example, let's say I wanted to remove this

74:29

gym value, all I have to do is just say friends dot remove. And I can just type in whatever element

74:36

I want to remove. So I want to remove this gym element. And so now when we run the program,

74:42

Jim is going to be gone from the list, and we just have these four guys left, you can also remove

74:48

all of the elements from the list. So if I just wanted to completely like reset the list and get

74:53

rid of everything, I could just say friends dot clear. And this is going to give us an empty list.

74:59

So it'll get rid of every single element inside the list. There's also another really cool method,

75:04

which is called pop. And you can just type out pop like that. And basically what pops going to do

75:09

is it's going to pop an item off of this list. So when I click the play button, you'll see here

75:14

that we're left with Kevin, Karen, Jim, and Oscar. So we basically got rid of the last element inside

75:22

the list. And that's what pop does, it pops an element off of the list. So it basically removes

75:27

the last element in the list. So we have our list, but let's say that we wanted to figure out if a

75:33

certain element was in this list, right, if a certain value is in here, so let's say I wanted

75:38

to check to see if my friend Kevin was in this list, well, I could say friends dot index. And in

75:45

here, I can just pass a value. So I could say like, Kevin, and this is going to tell me the index

75:53

of Kevin. So it'll tell me if Kevin's in the list. So now when I click play, you can see down here,

75:58

it says zero. If I was to say like Oscar in here, for example, now it'll give me the index of Oscar.

76:05

So that's it three. But if I put a name that wasn't in here, so like, if I put Mike, that's not in the

76:12

list. And so now we're going to get back in air, because it says Mike is not in the list. So that's

76:18

an easy way to figure out if a specific element is inside of the list. And if it is, then you know

76:23

what index it's at, you can also count the number of similar elements in the list. So I'm actually

76:29

going to come down here, copy this gym element, and we'll add another gym element into here. So I

76:36

could come down here and I could say friends dot count. And we'll pass in gym. And this will tell

76:43

me how many times the value gym shows up inside of this list. So it should say two, which it does

76:50

down there, because we have two values with the text gym. And so we have all these values, we could

76:57

also sort this list. So I could come over here and I could say friends dot sort. And what this

77:03

will do is it'll sort the list in ascending order. So we have a bunch of text here, and you'll see

77:10

what it does. Basically, what it's going to do is it's going to put it in alphabetical order. So

77:14

you can see we have J K O and T. So the list went from being just random order, like whatever we

77:20

had up here to now it's in alphabetical order. And that's also going to work for lucky numbers. So I

77:26

could say like lucky numbers dot sort, and then we'll print out lucky numbers. And these should

77:34

now be in ascending order, which I'm realizing they already are up here. But if I was to take this

77:40

42 and put it down here, now it will figure everything out and put it in ascending order. Anyway, we

77:48

can also reverse a list. So if I wanted, I could reverse this lucky numbers list, I could say

77:54

lucky numbers dot reverse. And now when we print it out, instead of being 4815 1623,

78:00

it's going to be 2316 15 842. So it's basically just reverse the order of the list. And finally,

78:07

there's one more list function I want to show you guys which is copy. So I could actually create

78:14

another list, and I could make it as a copy. So I could say like, friends to, and we could set this

78:21

equal to friends dot copy. And now friends to is going to have all the same attributes as friends.

78:30

Because it's basically just like copied it basically took a copy of that friends list. So

78:36

you can see it's the same exact thing and copying lists can come in handy in a lot of

78:40

different situations. So those are just some basic list functions and just to kind of like,

78:44

you know, get your feet wet and working with lists. Obviously, like the more you work with them,

78:49

the more you practice using these different functions, the more comfortable you're going to get.

78:52

But that's just kind of like a basic overview of what you can do.

78:55

In this tutorial, I want to talk to you guys about tuples in Python. Now a tuple is a type of data

79:07

structure, which basically means it's a container where we can store different values. Now if you're

79:14

familiar with lists in Python, a tuple is actually very similar to a list. It's basically a structure

79:21

where we can store multiple pieces of information. But a tuple has a few key differences from lists

79:29

that we're going to talk about in this tutorial. First thing I want to do is I want to show you

79:33

the how to create a tuple. So I can come down here and I can create a tuple. And one of the most

79:40

common examples of tuples is coordinates. So let's say I had a series of like XY coordinates, I

79:46

could create a variable called coordinates. And I'm just going to set this equal to an open and

79:53

closed parentheses. And whenever we want to create a tuple inside of Python, we're going to use

79:58

these open and closed parentheses. And inside of here inside of these parentheses, we're going to

80:03

put the values that we want to store. So I could, for example, put a list of numbers. So this could

80:10

be like my coordinates, I could have like four and five. So this would be my tuple with coordinates

80:17

inside of it. And one of the things about tuples that makes them unique is a tuple is immutable.

80:24

And you'll hear that word a lot when we're talking about tuples immutable, it basically means that

80:28

the tuple can't be changed or modified. Once we create our tuple up here, you cannot modify it,

80:35

you cannot change it, you can't add elements to it, you can't erase elements from it, you can't

80:40

change any of the elements inside the tuple. What you see is what you get, once you create the

80:46

tuple, it's, it's as it is, it can't change. So I could come down here and I can print out

80:52

some of the attributes inside the tuple. And the way I can access them is just by using an open

80:59

enclosed square bracket. And in here, I can pass in a index. So tuples are indexed starting at zero.

81:07

So technically, we would say that four is at index position one and sorry, four is at index

81:13

position zero and five is at index position one. So if I said coordinate zero, now it should print

81:20

out four. And down here, we're printing out four. So if I wanted to print out that five,

81:27

we can just put a one inside of those square brackets. And now we'll print out that five. So I can

81:32

access the elements inside of a tuple, I can easily access each, each entry. But if I was to come

81:39

over here and try to change one of the elements, so if I said coordinates, and coordinates one,

81:46

and I tried to set it equal to something else like 10, Python's actually going to give us an

81:52

error when we run this program. So I'm going to run the program, you see we get this error, it says,

81:57

type error tuple object does not support item assignment, because it's immutable, because we can't

82:05

change it. So that's basically what tuples are how we can create them. And you know, a tuple,

82:12

like I said, it's very simple, you just create it, and then you have it. And now I want to talk to

82:16

you guys about a common topic in Python, which is the difference between tuples and lists. So

82:23

remember, a list is basically doing the same thing a list. If we wanted to create a list, we would

82:28

just use square brackets instead of those parentheses. And if I was using a list, I could assign

82:34

different values to it, I could I could basically mutate any of the elements I could add delete,

82:40

modify change, whatever I wanted with the list. But with a tuple, we can't do that. And that's

82:46

sort of the basic difference. And really, Python doesn't say anything more than that. It basically

82:51

just says like one of them's immutable, one of them's not. But in practical use cases, generally,

82:57

people will use tuples for data that's never going to change. So you'll you'll store data inside of

83:04

a tuple, when that data doesn't need to be mutated. That's why something like coordinates

83:09

is such a good candidate for a tuple, because once you have coordinates, like, we're probably

83:14

not going to modify them, right, we might add other coordinates. So for example, if I wanted,

83:19

I could create a list of tuples. So I can say like 4567, maybe we have some other coordinates,

83:27

like 8034. So here I have, you know, I have a list and inside of it, we have these tuples.

83:36

But remember, these tuples can't be modified, the values can't be changed. So really, it's just a

83:42

case by case basis, I would say by a huge majority, you're going to be using lists most of the time.

83:48

Tuples are more of a niche, you know, they're using more special situations. But there are

83:55

plenty situations where you want to use tuples. So if you're in a situation where you want to store

83:59

data that can't be changed or mutated, then tuples are the way to go. So yeah, that's just some basics

84:04

about tuples, some basics about the difference between lists and tuples. And so a tuple is a

84:10

great thing to add into your Python repertoire. In this tutorial, I want to talk to you guys about

84:20

using functions in Python. Now a function is basically just a collection of code,

84:27

which performs a specific task. So I can take a bunch of lines of code that are basically doing

84:33

one thing, I can put them inside of a function. And then when I wanted to do that task or do that

84:39

one thing that the function was doing, I can just call the function. And so functions really

84:45

help you to organize your code a lot better, they allow you to kind of break up your code into

84:50

different, you know, little chunks that are doing different things. And they're really just awesome.

84:54

So functions are like a very core concept when we're talking about programming in Python. So I'm

85:00

going to show you guys how to create an awesome function today. Let's say for the purposes of

85:05

this tutorial, we want to create a function that says hi to the user. So the one task that our

85:11

function performs is basically just saying hi to whoever is writing the program. So over here,

85:17

if I want to write a function, the first thing I have to use is a keyword in Python,

85:22

it's called death. So D E F. And basically, when Python sees this keyword, it's going to say,

85:29

okay, this person wants to use a function. So after we type out death, we need to give this

85:36

function a name. So just like when we are creating variables, we give them descriptive names,

85:43

we also want to do the same thing with functions. So if I'm creating a function, I can just give

85:49

it a specific name, which basically says like what it's doing. So we're going to create a function

85:53

that says hi. So I'm just going to call this function, say hi, just like that. And once we type

86:00

out the name of the function, and so also I could type out say hi with no underscore or I can type

86:06

out say hi with an underscore, both are considered like good practices in Python. A lot of times,

86:13

if you just have a simple like two word function like this, you don't need to put an underscore,

86:17

but we could put an underscore there. If we wanted, after we type out the name of the function,

86:22

I'm just going to type an open and close parentheses. And then I'm going to type a colon. And basically,

86:28

what this is doing is it's saying to Python, like, all right, all the code that comes after this line

86:34

is going to be inside of our function. And in order to write code that's going to end up being

86:41

inside the function, we actually have to indent it. So over here, you'll notice that when I clicked

86:47

enter, my text editor automatically indented the text. So it's automatically like, using this indent

86:54

here. And that's kind of like one of the rules in Python is like, the code that goes inside of

86:59

this function needs to be indented. So if I was to write some code, like out here, this is no longer

87:06

going to be considered inside the function. So you can see, as I type out code, and obviously,

87:11

this isn't real code, but as I type text over here, that's indented, this little like marker over

87:17

here is basically saying like, Oh yeah, that's inside the function. But then when I write code

87:22

over here, that is like not at the same indentation level as this stuff, it's no longer considering

87:28

it inside the function. So that's just a little thing. Any code inside this function needs to be

87:33

indented. Alright, so our function is just going to say hi to the user. So I'm just going to have it

87:39

print out some text, it's going to say hello, user. So this is a very simple function. Obviously,

87:47

we just have one line of code and inside of a function, you could have, you know, as many lines

87:51

of code as you want. But for our purposes, we only need one line in order to perform our function.

87:57

So now all we have to do is call this function. So if I want to execute the code inside of this

88:03

function, I have to do something called calling it. So if I was to just run my program as it is

88:09

right now, I'm just going to run it, you'll see that nothing happens over here, right? Even though

88:15

this function is printing out hello user, when I run the program, it's not doing it. And that's

88:21

because the code inside of a function isn't going to get executed by default. The code inside of

88:27

a function is only going to get executed when we specify that we want to execute it. And in order

88:32

to do that, we're going to have to do something called calling the function. So in order to call

88:36

the function, you basically just type out the functions name and those open and close parentheses.

88:41

So I'm just going to type out say hi, and open and close parentheses. And now when we run this

88:48

program again, you'll see that it prints out hello user. So we're executing the code inside of the

88:54

function. And I want to show you guys just one more thing here, just talking to you guys about

88:59

how these functions actually work. So up here, I'm going to print top. And then down here, I'm

89:05

going to print bottom. So I want to show you guys the flow of these functions inside the program. So

89:12

when I run this program, you'll see we print out top hello user, and then bottom. So essentially,

89:20

what's happening is when Python goes through and executes this program, it goes over here to

89:25

this first line, it says, okay, we want to print out the word top. And then it goes down here and

89:31

it says, okay, we want to execute the say hi function. So Python actually jumps up, and it goes

89:38

over to this say hi function, and it's going to execute all of the code inside of this function.

89:44

So it's going to go through execute all this code. And then once it's done executing all the code

89:49

in the function, it's going to jump back down here, and it's going to move on to the next line,

89:53

which is bottom. So that's kind of like the flow of functions. Again, with functions generally,

89:58

when we're naming these functions, you want them to be named in all lowercase. And usually,

90:06

when we're naming stuff in Python, if there's two or more words, we're going to use an underspace

90:11

or an underscore in between them. So I could write this out as say underscore high. But in a lot

90:17

of situations, though, if I have a function like this where the name is really short, I might just

90:21

be easier to leave it without an underscore, but why don't we just put an underscore in there just

90:25

to be a super Python official. All right, so now we can actually make these functions a little bit

90:32

more powerful. And what we can do is we can give them information. So a lot of times when we write

90:38

a function, we're going to want to have additional information that gets passed in. And these are

90:44

called parameters. So a parameter is a piece of information that we give to the function. So over

90:52

here, I can actually specify that this function needs to receive some parameters. So I can basically

90:57

say like, Hey, if you're going to call this function, you need to give us some information,

91:02

you need to give us some parameters. And all I have to do to do that is just type out the name

91:06

of the parameter that I want to receive. So why don't we allow the code calling this function to

91:13

tell it what name to say hi to up here, I can just say name. And basically, what this means is it

91:19

means whenever I call this say hi function, we have to give it a name. So down here, if I was to call

91:26

this, I have to include a name in here. So I can say like, Mike, and what we can do now is we can

91:31

actually access this parameter or this variable inside of our function. So I could come over here

91:38

and instead of saying hello user, I could say hello name. And basically, what this is going to do is

91:45

it's just going to say hello to whatever name got passed in here. So I'm actually going to copy this

91:51

and we will do this twice. So I'll say hello, Mike hello, Steve. And now when I run this program,

91:59

you'll see that instead of just saying hello user, it's saying hello to whichever name I passed

92:06

into the function. So that's why this can be really useful, right? We can give the function

92:10

information, and depending on the information we give it, it'll perform its task a little bit

92:15

differently. I could also include more than one parameter. So you can, I mean, technically, you

92:20

could have like as many parameters as you want. So I can put another one in here, we can say age.

92:25

And now I'm going to have to pass in an age along with these. So I'm just going to pass in age and

92:31

pass in an age for down here. And I'm just going to pass in strings. So we can say hello name.

92:43

You are age. So I'm passing in two pieces of information. And now when we run this program,

92:50

it's going to call the function. And it's going to use both of those pieces of information. So I

92:54

say hello, Mike, you are 35. Hello, Steve, you are 70. So essentially, we're writing out this one line

93:02

of code, which just prints out like hello to someone. And we're allowing this function to receive

93:08

two parameters. So the name and the age, and depending on the name and the age, the function's

93:13

going to print out hello a little bit differently. And that's kind of the beauty of using function.

93:19

So you could pass anything you want into a function. So for example, I could pass in a integer instead

93:26

of a string for the age. So like I could pass in the number, the only difference is over here,

93:31

we're going to have to convert this into a string. But it's going to work just the same. So you

93:37

could pass in strings, numbers, bullions, arrays, you can really pass any type of data into a

93:46

function. And it's going to work. So you can see here, we get the same result. So that's the basics

93:50

of functions. And as you go through with Python, you're going to be using functions more and more.

93:56

And generally, it's a good idea to break your code up into different functions. So whenever you

94:01

have like a grouping of code that's designed to perform a specific task, that's usually a good

94:07

candidate to be put inside of a function. In this tutorial, I want to talk to you guys about

94:16

using the return statement in Python functions. Now Python function is basically just a collection

94:23

of Python code that performs a specific task. And when we want to perform that task in our Python

94:29

programs, we can call a Python function. And a lot of times when we're calling a Python function,

94:36

we just kind of call it and it does its thing. And then we move on with the program. But sometimes

94:41

when we call a function, we're actually going to want to get information back from that function.

94:47

So when I call the function, I want it to basically execute its task, I want it to execute all the

94:52

code. And then I want it to like give me some information back. So that could be like a particular

94:57

value, it could be something like telling me how the task went. Basically, the function can just

95:02

communicate back to us like, Hey, here's some information. And that's what the return keyword

95:07

can allow us to do the return keyword can basically allow Python to return information

95:14

from a function. So I'm going to show you guys how we can do this, I want to create a very simple

95:19

function. And it's just going to cube a number. So when we cube a number, we take it to the power

95:25

of three. So if I was going to cube like two, it'd be like two raised to the power of three.

95:31

Right, it's just pretty basic math. So we're going to make a function that will cube a number.

95:36

And I'll show you guys how we can use that return statement. So first thing we want to do is make

95:41

a function, I'm just going to say death cube, we'll call the function cube. And over here,

95:48

we can specify any parameters that this function will take. So basically, any values that we want

95:54

to give this function. So we're going to give it one function, which is just going to be a number.

95:58

So I'm just going to call it num. And then over here, I'm going to type out a colon.

96:03

And then down here, we can put any of the code that we want to be included in this function.

96:09

So basically, this function is just going to cube this number. So we could just type out num,

96:15

times num, times num. Right, that's going to give us num cubed. So that's all well and good. And

96:22

if I come down here, I can execute this function. So I can basically just call it, I'll say cube.

96:28

And why don't we pass it a three? So I'm going to go ahead and pass it to three. And I'm going

96:34

to save this. And now let's go ahead and run this code. So we're on the code. And you'll see down

96:43

here, nothing happened, right? So nothing got printed out in our program. In fact, when we call

96:50

this cube function, it didn't really do anything. So let me try to print this out. So maybe if we

96:56

print this function out, it'll give us back the information that we want. So type out print,

97:01

and we'll print out the value of the function, I'm just going to click play. You look down here,

97:06

it says none. So when I printed out the value of this function, it told me none. But really,

97:14

this function is supposed to be cubing the number. And here's how we can use the return statement

97:19

in Python. I can come over here and I can just say return. And now Python is going to return

97:26

whatever value we put over here to the right. So when I call this cube function, and I give it a

97:32

three, Python is going to go off, it's going to execute this function. When it sees this return

97:37

statement, it's going to say, okay, I want to give a value back to whatever call of this function.

97:43

And it's going to return this value. Now, when I print out cube three, it should print out the

97:50

answer because we get an answer back. So when I play this, you'll see down here, it says 27. So

97:58

we got the correct answer. So I could do this with anything, why don't we cube like four or

98:02

something? So now we're on this. And you can see we're constantly getting the correct answer back.

98:09

So that's the basics of using this return statement, it allows us to return a value back to the

98:15

caller. In other words, back to whatever's calling the function. So I just want to illustrate this

98:20

point a little bit further, I'm going to come up here, I'm actually going to create a variable.

98:23

So I'm going to create a variable called result. And I'm just going to set it equal to cube four.

98:30

This variable result is going to store the value that gets returned from the cube function. So it's

98:37

not going to store like cube four, it's going to store the value that gets returned from executing

98:43

that function. So now if I came down here and printed out result, it's actually just going to

98:47

print out 64 again, because that's the value that it's storing. So this return statement can be

98:53

really useful for like getting information back from a function, we can use something like a

98:58

parameter, like this four as a parameter, and we can give information to a function. And then we

99:03

can use the return statement to get information back from a function. And I do just want to point

99:09

out one more thing, I'm actually not able to put any code after this return statement. So if I

99:16

came down here and tried to put like a print statement, this actually isn't going to work. So this is

99:21

never going to get printed out. So if I just typed out like code right here, when we run this program,

99:27

it's not going to type out code. That's because when I use this return keyword, it breaks us out

99:35

of the function. So Python's going to go through, it's going to execute the code in the function,

99:40

whenever it sees this return keyword, it's just going to break back out and we're going to be

99:44

done. So we'll never be able to reach this line of code. So that's the basics of using return

99:50

statements. And you can return any data type you want, we could easily return like a string or a

99:56

Boolean or an array, it doesn't really matter, you can return whatever you want. And a lot of times

100:01

in a lot of situations, you're going to want to get a value back from a function.

100:09

In this tutorial, I want to talk to you guys about using if statements in Python. Now if statements

100:15

are a special structure in Python, where we can actually help our programs to make decisions. So

100:22

by using an if statement, I could execute certain code when certain conditions are true. And I can

100:28

execute other code when other conditions are true. So basically if statements allow our programs to

100:34

respond to the input that they're given. So depending on the data that we're using in the program,

100:40

our programs will be able to respond. So when certain data is certain values, we can do certain

100:46

things. And when other data is other values, we can do other things. So you know, your programs

100:51

are essentially becoming a lot smarter. And actually, if statements are things that we as human beings

100:57

deal with every day. So you know, as you go throughout your day, generally, you're dealing with if

101:02

statements all the time. So I want to kind of give you guys a brief introduction into if statements,

101:07

and we'll talk about some of the common if statements that you'd see in everyday life.

101:11

And then we'll go ahead and do a really awesome example to kind of like show you guys what if

101:15

statements can do for us. So over here in my text editor, I just have this little text file.

101:21

And in here, I wrote out a bunch of if statements that a lot of people will encounter on a daily

101:27

basis. So here's the first one, it says I wake up, if I'm hungry, I eat breakfast. So let's break

101:34

this down a little bit up here, it says if I'm hungry. And this is a condition, right? This is

101:40

either going to be true, or it's going to be false, right? If it's true, I'm going to go ahead and

101:46

eat breakfast, right? If it's false, I'm just going to skip whatever was in here and move on,

101:53

right? So that's the basics of an if statement, right? We have a condition, in this case, if I'm

101:58

hungry, if the condition is true, then we do the action. So we eat breakfast. Otherwise,

102:04

we just skip it. So down here, we have another one, it says I leave my house. If it's cloudy,

102:11

I bring an umbrella. Otherwise, I bring sunglasses. And so this one is a little bit more complex than

102:18

the one we just looked at. You'll see we have our condition, right? It says if it's cloudy,

102:23

if that condition's true, we're going to bring the umbrella. But in this case, if the condition's

102:28

false, there's something else that we want to do. So in the case up here, if this condition was

102:34

false, we just moved on. But now if this condition's false, then we're going to go ahead and bring

102:39

sunglasses. So that's another example of an if statement, it's a little bit more complex.

102:44

Finally, I have one more down here, and this one's even more complex, it says I'm at a restaurant.

102:50

If I want meat, I order a steak. So if this condition's true, we order the steak. But if this

102:56

condition is false, then we're going to move on, and we're actually going to check another condition.

103:02

So I'm going to say otherwise, if I want pasta, I order spaghetti and meatballs. This is another

103:08

condition. If this condition's true, we get the spaghetti. If it's not true, then finally,

103:14

we're going to move on down here, and we'll just default to ordering the salad. So all three of

103:20

these if statements are valid if statements, it's just they get more and more complex as we go down

103:25

the list. So hopefully that's, you know, it can kind of wrap your head around what if statements

103:30

are we're basically specifying conditions. If those conditions are true, we're going to do certain

103:35

things. And if those conditions are false, we can do other things, or we can check other conditions.

103:40

All right, so let's head over to our Python file, and we'll start writing some actual Python.

103:45

So in here in my app dot Python file, I want to create an if statement. And I'm going to show

103:51

you guys a very basic if statement in this tutorial. And then in the next tutorial, we're going to

103:56

look at some more advanced if statements. And so what I want to do is create a Boolean variable.

104:02

So I'm going to create a Boolean variable that's going to store whether or not the user is a mail.

104:07

So it's just going to be called is underscore mail. And I'm going to set this equal to true,

104:13

because I'm a male. So we have this Boolean variable, right? And right now we have it set

104:19

equal to true. I can actually use an if statement to check to see what the value of this variable

104:25

is. And if it's true, I can do something. So if I want to use an if statement, all I have to do

104:31

is just type out if, and then I just need to type out a condition. So basically, I would have to type

104:38

out like something that's going to be true or false. Like, for example, in the other page, we

104:44

were specifying like, if I'm hungry, or if it's cloudy, here, we're just going to check to see if

104:50

is mail. So in other words, if the person is male. And so remember, this has to be a, like, it has to

104:57

be able to be reducible to a true or a false value. So in our case, we're just using a Boolean,

105:03

because that's like the easiest thing we can do. So I said if I specified my true or false condition,

105:11

and now I want to just type a colon. And what I want to do now is make a new line. And so

105:16

anything that I put below this with an indentation is actually going to be executed when that

105:24

conditions true. So I could put print. And down here, I could just put you are a male.

105:32

And now you'll see what happens when I run this program, it's going to go down here,

105:36

and it's going to say you are a male. But if I changed is mail to false. So instead of this

105:42

being true, I just change it to false. Now all of a sudden it's not going to print anything. So

105:47

it's just going to be blank down here. That's because this condition wasn't true. So anything

105:53

that I put below this if declaration and that has an indentation like that is going to be executed

106:01

when the if statement is true. We can also use another keyword in Python, which is called else.

106:08

And else is basically going to be like otherwise. So you remember before we said, if it's cloudy,

106:14

I'll bring an umbrella. Otherwise, I'll bring sunglasses. Well, this is going to be like our

106:19

otherwise. So I can say down here else. And now anything that I put below else is going to get

106:26

printed out when that condition isn't true. So why don't we just put you are not a male.

106:32

So now we're able to cover both of the situations for this variable. If it's false,

106:37

we can cover that situation. So it says you are not a male. If it's true,

106:43

we can cover that situation as well. So it'll say you are a male. So that's pretty cool.

106:49

But we can also make these more complex. And actually before I talk about that,

106:54

you can put as much code as you want inside of an if statement. So I could have like,

106:58

you know, dozens of lines of code if I wanted. I'm just giving you guys a very simple example.

107:03

So why don't we make this more complex? And I'm going to add another variable into the mix. So

107:09

I'm going to add another, you know, Boolean attribute into this mix. I'm going to make another variable

107:14

called is tall. And we're going to set this equal to true as well. So now I have two variables in

107:22

the mix. Let's say I wanted to write an if statement that will check both of those variables. So I

107:28

could write an if statement that's like, if they're male and they're tall, or if they're male and

107:32

they're not tall, or if they're not male and they're tall, like I could write out a bunch of if

107:37

statements to check the combination of these variables. And so the way I can do that is by using

107:44

one of two keywords. So the first keyword I want to show you guys is the or keyword. So I can come

107:51

over here and I can say if is male, or is underscore tall. And what this is basically going to do is

107:59

it's going to say if the person is either male, or if they're tall, then we want to do something. So

108:08

this is only going to be true when one of these values is true, when one or both of those values

108:14

is true. So down here, we could actually put instead of you are a male, we could put you are a male

108:21

or tall or both, that's going to be more appropriate. And then down here, we would say you are

108:29

neither male nor tall. And that's because if we execute the code inside of this else,

108:36

that means that both of these guys are false. Right, so I'm saying if they're male or they're tall,

108:42

we're going to execute whatever is in here. Otherwise, that means they're neither male nor tall. We're

108:48

just going to execute this down here. So let me show you guys how this works. I'll click play. And

108:53

these are both true. So it's going to say you are a male or tall or both. If I was to set one of

108:59

these to false. So let's set is male equal to false. Now, it's still going to say you are a male

109:06

or tall or both. But if I set them both equal to false, now it's going to say you're neither

109:13

male nor tall. So that's kind of the basics of using the or operator. And there's another operator

109:21

we can use which is called and, and basically it's similar to or except both of these conditions

109:27

have to be true. So this is basically saying if the person is male and they're also tall,

109:34

then we're going to execute whatever is in here. So we could actually change this text to say you

109:39

are a tall male, because we know for a fact that if this is getting executed, they're both male

109:46

and tall. We can also change this print statement down here. This could say you are either not male

109:54

or not tall or both. So you can see it's basically just saying that you're either not one of them

110:02

or you're not both of them. So we click the play button and you'll see because they're both false,

110:08

it's going to say you are either not male or not tall or both. But if I make them both true,

110:14

now it's going to say you are a tall male. If I was going to make one of these false,

110:21

though, so let's make is tall false. Now it's not going to execute this first print statement.

110:27

It's going to execute the one inside of the else. So to say you are either not male or not tall

110:32

or both. So that's the basics of and an or and you'll be using those a lot as you use your if

110:39

statements. But there's actually one more thing we can do. We can add in some more conditions.

110:45

So up here I'm checking if they're male and they're tall. But what if I wanted to check if they're

110:51

and they're not tall? So if they're like a short male, I can use another keyword in Python called

110:58

else if or really it's just typed out E L I F and it stands for else if and after this we can put

111:05

another condition. So over here I want to check to see if they're male and they're not tall.

111:11

So I'm going to say else if male is male and now I can type in not tall and the way I can do that

111:19

is by saying not an open and close parentheses and saying is tall. And basically when I use this

111:25

not function it's going to negate whatever is inside of here. So if this was true it's going to make

111:32

it false and if this was false it's going to make it true. So basically that'll tell us whether or

111:37

not they're tall. And then I'm going to use a colon and now down here inside of here we're basically

111:44

just going to type out any of the code that we want to happen when this condition's true. So down

111:50

here we can just print out you are a short male and there's actually one more condition that we want

111:58

to check for which would be if they're not male and they're tall. So we can make one more else if

112:05

and I'm actually just going to copy this guy and we'll paste it down here. And so I want to say if

112:13

not is male and is tall. So now we're checking to see if they're male and or if they're not male

112:23

and they're tall. So here we could say you are a you are not a male but are tall. So essentially

112:33

here we have an if statement that's going to cover every single condition for these two variables. So

112:39

in every single situation of the two values of these variables we're going to be able to print

112:44

something out for it. So if I made both of these true it's basically going to tell us you are a

112:50

tall male. If I make is male false it's going to tell us you are not a male but are tall. If I made is

112:59

male true and is tall false then it's going to tell us you are a short male. And if I made both of

113:08

these false it's going to tell us you are either not male or not tall or both. And actually we need to

113:14

change this. So this should say you are not a male and not tall. So that would be a better print out

113:22

for that situation. So you can basically see how we can use if else ifs and else in order to help our

113:31

program to make decisions and really to respond to the different data in our programs. So I'm able

113:37

to respond to every combination of the values for these two variables. And that's really cool. So in

113:44

this example we were just using simple Boolean variables but in the next tutorial I'm going to show you

113:50

guys how you can actually use something called comparison operators. So we could compare like two

113:55

numbers or two strings and we could use those as our conditions. But for now I think this is a good

114:01

introduction into if statements. So I'll see you guys in the next tutorial. In this tutorial I want

114:11

to talk to you some more about if statements in Python. More specifically we're going to be looking

114:16

at using comparisons inside of our if statements. So if you're following along with the course in

114:22

the last tutorial I talked to you guys about just the basics of if statements and we used a couple

114:27

different Boolean variables. And depending on the values of those bullions we were able to do certain

114:32

things. In this tutorial we're going to look at another way to use if statements which is with

114:37

comparisons. So instead of just using Boolean values we can actually compare different values. So I

114:44

could compare a couple numbers or I could compare a couple strings. And depending on the results of

114:49

those comparisons we can do certain things. So this is going to be pretty cool. And this is really

114:55

going to give you guys a full understanding of what you can do with if statements. So for this

115:01

tutorial I actually want to create a Python function. And this function is going to give us the

115:07

maximum number that we pass into it. So this function is going to take three parameters as input and

115:14

it's going to print out the biggest number that we give it. So let's create this function. I'm

115:20

just going to say death. And I'm just going to call it max num. And inside of these parentheses I'm

115:28

going to specify that I want three parameters as input. So I'm going to say num one, num two and

115:34

num three. So basically we're passing it three numbers and this function will return the largest

115:42

of the three. So in order to figure out which of these numbers is the largest we're going to have to

115:48

use an if statement. So I can say if and over here I need to specify a condition right so I need to

115:55

put like a true or a false value. And so what we can actually do is we can compare these different

116:01

numbers. So I could say if num one is greater than or equal to num two. And num one is greater

116:12

than or equal to num three. So basically what I'm doing here is I'm comparing these different

116:18

numbers. So I'm saying if the first number is greater than or equal to the second number. And

116:23

this is a comparison. So when you think about it, when we compare these two numbers, we're going to

116:29

end up with a true or a false value. Right, num one is either greater than or equal to num two or

116:35

it's not. It's actually a Boolean value right it's either true or it's false. Same thing over here.

116:42

The result of this comparison is either true or it's false. So technically I'm putting a true or

116:48

false value in there. I'm just getting that true or false value by using a comparison. And you'll

116:55

notice over here we're using something called a comparison operator. And basically this is just

117:00

like how we want to compare them. So we're saying like greater than or equal to in this case.

117:05

So down here, if this whole condition is true, in other words, if the results of these two

117:12

comparisons end up being true, then we know that num one is the biggest. So I can just return

117:18

num one, I can say return num one. But we also want to check some other conditions. So I can

117:24

say L if, and now I want to do some other comparison. So I'm going to say else if num two is greater

117:32

than or equal to num one. And num two is greater than or equal to num three. So I'm doing

117:39

essentially what I did up here, except for num two. So again, I'm comparing these different

117:44

numbers. And this is going to end up being a true or a false value, depending on the result of

117:50

the comparison. So down here, again, we can just return num two, because if all of this is true,

117:57

that means we're going to end up returning num two. And then finally, we can just say else.

118:03

So if num one isn't the biggest and num two isn't the biggest, then we can pretty much

118:09

assume that num three is the biggest. So now we have our function. So down here, I'm actually

118:17

just going to call this function. So I'm just going to say, actually, I'll print out the

118:21

result. So I'll say max num, and we'll pass in like three, four, five. So we're basically

118:28

calling that new function that we just made. And I'm going to go ahead and run this program.

118:35

So you can see down here in the output, we print it out five, because that was the biggest.

118:40

If I was to change this middle one to the biggest, so we make the middle 140, now it'll be able

118:45

to take on that situation. And finally, I can make this like 300. And it'll be able to handle

118:51

that. So no matter which number num one num two or num three ends up being the biggest,

118:56

it's able to tell us without a problem. So these comparisons are a really, really, really

119:03

common way to make if statements. And a lot of times, you're going to want to be comparing

119:08

different values inside of Python. So over here, we're comparing numbers, but we could also

119:14

compare strings. So I could easily make these both like, you know, I could make this like

119:19

dog, and I can make this dog. And I could say a different operator. So I could say like

119:25

this double equal sign would mean equal. So I could basically say like, if this string

119:30

is equal to that string, so you can basically like compare all different data types, you

119:35

know, I could compare numbers, strings, you could also compare bullions. So once again,

119:41

these are comparison operators. And this one is greater than or equal to, but there's a

119:46

bunch of other comparison operators we can use. So the most basic is just going to be

119:51

a double equal sign. And that basically checks to see if the two values are equal. So it'll

119:56

say like, if number one is equal to number two, we can also say not equals. And this

120:03

and this exclamation point will basically mean not equal. So it's if num one is not equal

120:11

to num two, we also have greater than less than greater than or equal to less than or equal

120:18

to. And those are kind of all of the operators are all the, you know, the basic comparison

120:23

operators that you're going to be using in Python. So using comparisons is really awesome.

120:28

And there's going to be tons and tons of situations inside of Python where you're going

120:32

to want to compare different values. So these can be extremely useful. In this tutorial,

120:40

I'm going to talk to you guys about building a calculator in Python. Now, if you've been

120:45

following along with this course, you'll know that in the beginning of the course, we actually

120:49

created a very basic calculator. Basically, the user could just enter in two numbers and

120:55

our calculator would add the numbers together and spit out the answer. In this tutorial,

121:00

I want to show you guys how to build a more advanced calculator. So this calculator is

121:05

going to be able to perform all of the basic arithmetic operations. So we can add, subtract,

121:10

multiply and divide. And we'll actually allow the user to specify which one of those they

121:15

want to do. So the user will enter in a number, they'll enter in whatever operator they want

121:21

to use. So plus minus divide, multiply. And then they'll enter in the second number and

121:26

we'll perform the operation for them. So this can be pretty cool. And it's going to show

121:30

you how we can use if statements in like a practical application. So let's get started.

121:36

The first thing we want to do is get input from the user. So I'm going to create three

121:41

variables, one for the first number, one for the second number, and one for the operator.

121:46

And we'll basically store the values that the user inputs into those variables. So the first

121:51

one will just be num one. And I'm going to set this equal to input. And we're going to

121:57

give this a prompt. So I'll say enter first number. And here's actually the thing is

122:04

whatever the user enters, we're going to want to convert it into a number. So generally in

122:10

Python, when we get input from the user, no matter what they put in, it just gets converted

122:15

into a string. But we don't want a string, because we're going to have to be adding or

122:20

subtracting or multiplying these numbers together. So whenever they enter in the number,

122:25

I just want to immediately convert it to an actual like number data type instead of a

122:30

string. So what I can do is I can actually say float, and I can surround this entire

122:36

input tag with parentheses. And now what this is going to do is it's going to immediately

122:42

convert whatever the user inputs into a float. Now that's going to mean that they're going

122:48

to have to insert a number, otherwise we'll get an error. But for the most part, this should

122:52

work out for us. So I'm actually just going to copy this guy. And we'll go ahead and paste

122:58

it over here. And we'll paste it one more time. So we're basically just trying to get three

123:03

pieces of input from the user. So the second thing we want to get is the operator. So I'm

123:08

just going to say op for operator. And I'll actually get rid of this float, because we

123:15

want to keep this as a string. And I'll just say enter operator. So that's going to be

123:21

like plus minus multiplication, whatever. And then finally down here, we'll get num two.

123:27

And again, we're going to want to convert this to a float. So I'll say enter second number.

123:33

So now basically what we're doing is we're getting the first number, we're getting the

123:37

operator and we're getting the second number from the user. So what we need to do now is

123:43

we need to figure out what is inside of this op. In other words, we need to figure out

123:49

what operator the user was trying to do. So are they trying to do addition or they trying

123:54

to do subtraction like we want to figure that out. So we can use an if statement in order

124:00

to figure that out. So I'm going to come down here and I'm basically just going to create

124:05

an if statement. So I'm going to say if, and we want to check a condition. So I'm basically

124:10

going to check to see if this operator is equal to a plus sign. So I can say if op equals

124:18

plus sign. And now if the operator is equal to plus sign, I'm just going to print out

124:25

num one plus num two. But let's say that instead of a plus sign they entered in something

124:30

else. So we can say out if op is equal to, and now we'll check if it's a minus sign.

124:37

And so here we'll just print num one minus num two. And actually forgot to put a colon

124:44

here. So we can just do these for everything. So now we can say L if op is equal to division

124:53

and here we'll just divide the two numbers so I can print out num one divided by num two.

125:00

And finally, we'll do the same thing for multiplication. So I'll say l if op is equal

125:07

to asterisk. And we'll just print out num one times num two. Alright, so basically over

125:16

here we have every, you know, condition. But I also want to put in an else statement. So

125:22

in the case that the user enters in an operation, that's not like one of the four that we

125:28

specified up here, I just want to throw an error. So I'm just going to say op or actually

125:32

we just say else. And here we'll just print out like invalid operator. Alright, so this

125:40

if statement should give us everything we want. Basically, I'm just checking to see if it's

125:46

equal to a plus sign a minus sign a division sign or a multiplication sign. Depending on

125:51

which one it's equal to, we'll do a different operation. So let's go ahead and run this

125:55

program and see how we do. So I'm going to run this program and it says enter first number.

126:01

Let's enter five, enter an operator. So why don't we just do addition and enter a second

126:08

number. Let's do 23. So now it prints out the answer 28. So a calculator actually works.

126:15

Let's try another operator just to make sure. So we'll do six and why don't we multiply

126:22

it by, I don't know, 5.23. So now we go off and we do the multiplication and looks like

126:29

we got our answer. So our calculator seems to be working. And so basically all we did was

126:34

we got the input from the user and then we used if statements to figure out what they

126:40

wanted to do. So we can check to see if they wanted addition. We can check to see if they

126:45

wanted subtraction or division or multiplication. And we can also check to see if they entered

126:49

in an invalid operator. So actually why don't we try that too. So I'll enter in like five

126:56

and now we'll just enter in like an open and close parentheses and a four. So you can

127:02

see here it's able to tell like the program's smart enough to know that that's not a

127:07

valid operator. So these if statements can be really powerful and we can use them in

127:11

combination with getting the user input to create a little calculator. In this tutorial

127:20

I want to talk to you guys about using dictionaries in Python. Now a dictionary is a special

127:26

structure in Python which allows us to store information in what are called key value

127:32

pairs. So essentially I can just create a bunch of these different key value pairs and

127:38

then when I want to access a specific piece of information inside of the dictionary I

127:43

can just refer to it by its key. So the word dictionary is actually pretty appropriate

127:49

here. If you guys are familiar with a normal dictionary you know in a normal dictionary

127:53

you'd have a word and then you'd have a definition associated to that word. So in this

127:59

situation the word would be the key. In other words the word is what like uniquely

128:04

identifies it inside of the dictionary and then the value would be the actual definition.

128:10

So this is going to make more sense once we start working with this but I just wanted

128:15

to give you guys a quick introduction. So in this tutorial I want to create a little

128:19

program that will allow us to convert a three digit month name into the full month name.

128:26

So I could convert for example like JAN to January or I could convert M A R into

128:36

March. Like I want to be able to have a program that can do that for us and that's actually

128:41

a pretty good use case for a dictionary. So we're actually going to create a

128:45

dictionary and I'm going to show you guys how we can do that. So the first thing we

128:49

need to do in creating a dictionary is just give it a name. So I want to give this a

128:54

specific name. So I'm just going to call this month conversions. And now I just want to

129:01

set this equal to an open and closed curly bracket. So whenever we create a dictionary

129:06

in Python we're always going to want to create it inside of these open and closed curly brackets.

129:12

So inside of the dictionary now we can start defining what are called key value

129:18

pairs. So I can define a key and then I can give it a corresponding value. And the

129:24

way that we can do that is just by typing out the key and then typing out the value.

129:27

So in our case we're converting three digit month names into the full month

129:32

names. So I would want to type out for example JAN and then I can type a colon

129:38

and I want to type out the value I want to associate with it so we could say January.

129:43

I can basically do this for every combination. So I could say like FEB and this would be

129:50

February. And actually after each one of these we're going to need to put a comma.

129:55

So I could do the same for March, say M.A.R.

130:01

So essentially what I can do is I can specify an entry inside of this dictionary for each

130:08

one of these. So this over here would be the key and this over here would be the

130:14

value. And here's one thing is all of these keys have to be unique. So if I was to

130:20

come down here and change this to JAN, this is actually going to give us a little

130:24

warning. So you can see down here it says dictionary contains duplicate keys.

130:29

That's a no no when we're creating dictionaries. So you always want to make sure

130:33

that the keys are unique. All right. So I'm going to go ahead and create one of

130:37

these for each one of the months and then we'll come back and we'll talk about it.

130:41

All right. So I've gone ahead and created an entry for all 12 of the months. So now

130:47

we have a bunch of unique keys and each one of those keys is associated with a

130:52

different value. So this is the basics of creating a dictionary is we have a bunch

130:57

of these different key value pairs. And now what we can actually do is we can

131:02

access them from inside of this dictionary. So if I wanted to access a

131:07

specific key or a specific value, all I have to do is come over here and I'm

131:12

actually just going to print it out onto the screen so we can see it. All I have

131:16

to do is refer to the dictionary by name. So it's just called month conversions.

131:21

And there's actually a bunch of different ways that I can access these month names.

131:27

So I could access different entries inside of this dictionary. The first way is just

131:33

by making a open and close square bracket and inside here, I can just type in one

131:39

of the keys. So for example, inside here, I could type in NOV. And what this should

131:44

do is it should give us back the full name for November. So when I print this

131:49

out, you'll see that we get back down here November. So I could do the same thing

131:55

for, you know, any of these, we could say like M A R. And now we should get

132:00

March back. So I'm able to refer to the key. And it's going to go into the

132:05

dictionary and it's going to give me the value that's associated to that key.

132:09

There's actually another way we could do this too. So I could say month conversions

132:14

dot get an inside of here. I could also pass in the name of a key. So I could pass

132:20

in like D E C. And now we'll be printing out December. And what's cool about using

132:28

this get function is I can actually specify a default value that I want to

132:33

use if this key is not found. So there's going to be certain cases when we're

132:38

dealing with dictionaries where you're going to put in a key that might not

132:41

necessarily map to a value inside of the dictionary. So you put in an invalid key.

132:47

So let's just try to do that. So I'll just put like L U V. That's not a month

132:52

abbreviation. So when I try to run this, you'll see we get this none down here.

132:58

And in a lot of cases, you're going to want to create some sort of a default

133:03

value. So if I'm in this situation where I have a key that's not mapable to any

133:08

values inside of this dictionary, I can actually pass it a default value. So

133:14

inside this dot get function, I can make a comma. And now I can pass in like a

133:19

default value to get printed out. So I can say like not a valid key. And now when I

133:25

put in the invalid key, instead of saying none, it's going to go ahead and say not

133:30

a valid key. So this is going to give us like a default value that we can fall

133:35

back on. So these dictionaries are really awesome. And the keys over here don't

133:41

have to be string. So you could also use numbers. So for example, I could say

133:45

like zero, one, 10, like I can put any numbers I want over here as keys as long

133:52

as they're unique. And then I could access those keys, just like I normally

133:56

would down here with this get function or with those open and close square brackets.

134:01

So that's sort of the basics of using dictionaries. It can be really useful to

134:05

store key value pairs. And honestly, you're going to be using them a lot in Python to

134:10

store different types of data. In this tutorial, I want to talk to you guys about

134:18

while loops in Python. Now a while loop is basically a structure in Python, which

134:24

allows us to loop through and execute a block of code multiple times. So I can

134:31

specify like a few different lines of code, and then I can put that code inside of a

134:36

while loop. And it would basically loop through that code, executing it repeatedly

134:41

until a certain condition was false. So while loops can be awesome. And there's

134:46

a lot of situations in Python where we're going to want to loop through specific

134:50

lines of code. So in this tutorial, I just want to show you guys the like bare

134:54

basics of how while loops work. And then in future tutorials, we're going to use

134:58

while loops to create little games and stuff like that. So let's talk about

135:02

while loops. When I create my while loop, the first thing I want to do actually is

135:08

create an integer. I'm basically creating a variable that's a number and you don't

135:13

have to do this for a while loop, but just for our demonstration I'm going to. So

135:17

I'm just going to call this I and I'm going to set it equal to one. And now what I

135:22

want to do is I want to create a while loop. So I'm basically just going to say

135:26

while. And over here I want to specify a condition. And this is what's called our

135:34

loop condition or you can also refer to it as a loop guard. And basically we are

135:39

going to keep looping through the code inside of the while loop as long as this

135:44

condition is true. So whatever condition I put in here, as long as it's true, we're

135:50

going to keep looping through the code inside the while loop. So I'm just going to say

135:53

while I is less than or equal to 10. And now I'm going to put a colon and I'm going

136:02

to make a new line. And so anything that's below this while loop declaration and

136:07

that's indented like this is going to be considered code that's inside the while

136:13

loop. And so that code is going to get repeatedly executed while this condition

136:18

up here is true. So I'm just going to put a very simple line of code here. We're

136:23

just going to print out the value of I. And then down here I'm going to actually

136:27

increment I so I'm going to add one to I'm going to say I is equal to I plus one.

136:34

So basically I'm taking this I value and I'm adding one to it. There's actually a

136:39

shorthand that we can use in Python to do something like this though I could just say

136:44

I plus equals one and that's automatically going to add one to I. So this is like

136:51

a little shorthand and you'll see people in Python using that a lot. So over here

136:56

we basically have our while loop. So I have the outline for a while loop. So we're

137:02

defining this variable up here and we're going to keep looping through the code

137:06

inside the while loop as long as this condition up here is true. So if this

137:12

condition is false for example if I is equal to eleven then we're not going to

137:17

loop through this code anymore and we're just going to move on. So down here I'm

137:21

actually just going to print. Done with loop. So this will kind of illustrate

137:27

this for us. Alright so let's go ahead and run this program. I'm just going to run

137:32

it. And you'll see down here we get some output. So we're actually going to get

137:38

numbers printed out one all the way down through ten and then we're printing out

137:43

done with loop. So basically what's happening is we're printing out every

137:48

time we go through this loop. So initially I is equal to one right. The first thing

137:53

that Python is going to do when it gets to this while loop is it's going to check

137:56

this condition. So before it does anything else it's going to check this

138:00

condition and it's going to say okay is one less than or equal to ten and that's

138:05

true. So as long as this guy is true we're going to go through and loop through

138:10

all the code inside of here. So we're going to print out i which has the

138:14

value of one and then we're going to add one to i. So now i is going to go from

138:19

one to two. Now what we're going to do is we're going to go all the way back up

138:24

here to this while loop declaration and we're going to check the condition again.

138:29

So remember I checked the condition the first time and then I loop through all

138:33

the code then I go back up to the top and I check it again. So I'm going to say

138:38

okay is i less than or equal to ten. At this point i is equal to two. So i is

138:44

less than or equal to ten. So now we're going to go through and we'll execute

138:48

all this code again. So I'm going to print out i so it's going to be two and I'm

138:52

going to add one to i so now we have three. Then Python is going to go all the way

138:57

back up to the top and it's going to check the condition again. So on every

139:02

iteration through the loop before Python does what's inside of this loop it's

139:06

going to check that condition. And as long as the condition remains true we're

139:11

going to keep executing the code inside the loop. Eventually though in this

139:15

program i is going to end up being eleven. So on the last iteration it's going to

139:20

end up being eleven. We're going to come back up here and we're going to say hmm

139:24

is eleven less than or equal to ten? Nope. And so since it's nope we're going

139:29

to come down here and we'll print out this done with loop line. So that's the

139:33

basics of how these while loops work. We're specifying a loop condition as long

139:38

as that condition is true we'll keep executing the code inside of the loop.

139:42

And after every execution of the loop we're going to come back up and check the

139:46

condition again. So that's how we end up with this printing out down here. So

139:52

that's the basics of a while loop. And we can use these while loops to do all

139:57

sorts of things. Like while loops are very very powerful. In this tutorial I'm

140:05

going to show you guys how to build a basic guessing game in Python. And in this

140:10

guessing game we're actually going to be using all sorts of cool programming

140:14

structures that we've learned up to this point in the course. So we're going to

140:17

be using things like if statements and while loops and variables and all these

140:21

cool things in order to build this game. So the basic idea is we will specify

140:26

like a secret word. So we'll have a secret word that we store inside of our program.

140:31

And then the user can interact with the program and try to guess the secret word.

140:36

And so what we want to be able to happen is we want the user to be able to keep

140:40

guessing what the secret word is and keep typing in different responses until they

140:44

get the secret word right. So that's the basics of what this game is going to be.

140:50

And now let's go ahead and create it. So the first thing we want to do is create a

140:54

variable to store our secret word. So I could say secret word and we're just

141:01

going to set this equal to a secret word. So why don't we make it draft. That's

141:04

a pretty good secret word. And now what we want to do is we want to create a

141:08

variable that will store the user's response. So I want a variable that will

141:13

store like all the guesses that the user makes. So I'm just going to call this

141:18

guess. And I'm just going to set it equal to an empty string right now. So now we

141:23

have the secret word and we have a variable to store the user's guess. And

141:28

what we need to do is we need to be able to prompt the user to input the secret

141:33

word. But here's the catch. What we want to happen is we want them to enter the

141:38

secret word. And if they don't guess it correctly, we want to prompt them to

141:42

enter it again. So we can't just use a single input statement. We actually have

141:48

to use something called a while loop. And we can use a while loop in order to

141:53

continually ask the person to guess the word until they guess it correctly. So

141:59

let's go ahead and create our while loop. So I'm going to say while. And after I

142:03

say while, I need to specify a looping condition or a looping guard. Basically,

142:09

this is something that as long as it's true, we're going to keep looping through

142:13

this loop. So basically, I want to say I want to keep looping as long as the

142:18

user's guess is not equal to the secret word. So as long as they haven't guessed

142:25

the secret word, I'm going to keep going through this loop. And inside of this

142:31

loop, what we're going to do is we're going to ask them to input the secret word. So

142:35

I can take this guess variable and I can set it equal to input. And I'm just going

142:42

to say enter guess. And so over here, we're basically telling the user to enter in

142:49

their guess. I'm storing whatever they enter inside of this guess variable. And

142:56

then what's going to happen is we're going to come back up here. We're going to check

142:59

to see if the guess is equal to the secret word. If the guess isn't equal to the

143:04

secret word, then we're going to do it again. But if the guess is equal to the

143:08

secret word, then we're just going to break out of this loop. And so we can come down

143:13

here. And we can just print out a success message. So I could say you win because

143:20

they got the secret word. So this is a very, very simple program. But this is

143:25

essentially all of the code that we need to be able to build a game like this. So

143:30

let's go ahead and play our game. We can run it and see how we did. So I'm going to

143:35

click play. And down here, you can see it's prompting us to enter a guess. So I'm

143:40

just going to type in some, you know, random text, we can type in whatever we

143:44

want. And as long as we're not entering that secret word, it's going to keep

143:48

prompting us to enter different information. But if I enter in the secret word, so if

143:54

I enter in draft, now all of a sudden the program is going to terminate and it's

143:58

going to say, Hey, you win, because we were able to guess the secret word. So that's

144:04

like a really cool way for us to be able to do this. And we actually have a fully

144:08

functional guessing game. But I think this guessing game could actually be

144:13

improved quite a bit. A lot of times when we're making a guessing game, we want to

144:19

set a limit. So in other words, I want to set a limit for the number of times that

144:24

the user can try to guess the word. So let's say that the user has three tries,

144:28

right, they have three guesses in order to guess the word. And if they can't guess

144:32

the word after three tries, then they're going to lose the game. But if they can

144:36

guess the word inside of three tries, then they'll win the game, right? I think

144:40

that would be a little bit more of a fun game. So why don't we try to program

144:44

that game? Basically, we're going to set a limit on the number of guesses that the

144:49

user can have. In order to do this, we're going to have to create a couple more

144:54

variables down here. In other words, we're going to have to store a couple more

144:58

pieces of information. The first piece of information I want to keep track of is

145:03

how many times the user has guessed, right? So we can just make a variable called

145:08

guess count. And we'll just set this equal to zero because initially, the user won't

145:13

have guessed down in this while loop. Every time we go through the loop, I want to

145:19

increment that guess count, right? So every time we've gone through this while loop,

145:24

I want to increment that count because that means the user will have guessed.

145:29

So down here, I'm going to say guess count plus equals one. And this is just going

145:36

to add one to the guess count, actually whoops. Alright, so after each

145:42

iteration of this loop, we're going to go ahead and add one to the guess count.

145:46

Alright, so that's the first variable that we're going to need. We're also going to

145:49

want to store another variable. And this is going to be called guess limit. And

145:54

basically, this is going to tell us how many times the user can guess the word.

145:59

So I'm going to it's going to be guess limit. And we'll basically just say three.

146:04

So let's say that the user has three tries to guess the word three strikes and

146:08

you're out. So the user, if they can't get it in three tries, then we're going to

146:13

basically say that they lose the game. And I'm also going to need one more

146:17

variable here, which we're going to call out of guesses. And I'm just going to set

146:25

this equal to false initially. So this out of guesses variable is going to be a

146:30

Boolean. And it's going to tell us whether or not the user is out of guesses. So if

146:35

out of guesses is true, that means they have no more guesses, right? They basically

146:39

lost the game. And if out of guesses is false, that means that they still have

146:44

some guesses left so they can keep playing. All right, so let's use these

146:48

different variables in order to make our program more functional. So the first

146:53

thing I want to do is when I go through this loop, I want to check to make sure

146:58

that the user has more guesses, right? In other words, before I let the user

147:04

enter a guess, I want to check to see that they haven't already used up all

147:08

their guesses. So down here, I can make an if statement. I can say if and inside

147:13

of the condition, I want to check to see that guess count is less than guess limit.

147:21

If guess count is less than guess limit, that means that they haven't guessed the

147:27

total number of guesses that they have. So they have some guesses left. And if that's

147:32

the case and if that's true, then I'm going to go ahead and give them a guess and

147:38

then we'll increment the guess count. If this isn't true, in other words, if they

147:43

have reached their guess limit, then I'm going to want to set out of guesses

147:50

equal to true, because they're out of guesses, right? They have no more guesses

147:54

because the guess count wasn't less than the guess limit. And so that means that

147:58

they're out of guesses. So they have no more guesses. So there's actually one more

148:02

thing we need to do inside of this while loop, we need to add another condition

148:07

on to this loop guard. So right now, we're going to keep looping as long as the

148:11

guess is not equal to the secret word. But remember, if the user's out of guesses,

148:17

in other words, if they've reached their guess limit, we don't want them to guess

148:21

anymore, right? So if they ran out of guesses, then we want to basically break out of

148:25

this loop and not give them any more guesses. So I'm going to go ahead and add

148:29

another condition onto here. And I'm going to say, while they haven't guessed the

148:33

secret word, and they're not out of guesses. So it's going to be not out of

148:41

guesses. Then we're going to keep looping. So as long as they haven't guess the word,

148:47

and as long as they still have some guesses left, we're going to keep looping. But

148:51

otherwise, we're going to break out of the loop. And so down here, we're printing out

148:56

you win. But actually, when we break out of this loop, there's going to be two

149:01

possible scenarios. So again, there's two possible ways that this loop could end,

149:07

right? Either the guesses equal to the secret word. So either the user guessed

149:11

the word correctly, or the user ran out of guesses. And so there's two situations

149:17

down here that we need to account for. And so I'm going to use an if statement to

149:21

figure out which is which. So I'm going to say if, and we're just going to type out

149:27

of guesses. And if the user is out of guesses, then I want to print out of

149:35

you lose. So we're basically going to be like, you lost the game. Otherwise, though,

149:41

if they're not out of guesses, that means that they guess the word correctly. So

149:45

we're just going to print out you win. Alright, so now we have all the logic for

149:50

this little guessing game setup. And let's see if we can run it and play through

149:54

it. So I'm just going to run this program. And now it's going to tell us to

149:59

enter a guess. So why don't we enter more guesses than we have? So we're going to

150:03

try to lose the game. So I'm just going to enter one guess, two guesses. And now I'm

150:07

on my final guess. So if I don't get it here, we should actually lose the game. And

150:12

you can see it says out of guesses, you lose. So we weren't able to guess it in the

150:17

number of tries that we had. Let's run the program again, and we'll try to win the

150:22

game. So we'll get a couple wrong. Let's say we're on our last guess. And I'm like,

150:27

Okay, I can do this. So I type in the word and bam, we won the game. So that's how we

150:34

can basically create a game where we have a guess limit. So this is a lot of code.

150:39

Let me walk you guys through this one more time so you can just get a full

150:42

understanding of what we're doing. Up here, I created a few different variables. We

150:47

created this secret word variable, and we created this guess variable. And then we

150:52

also created some more variables. So we created this guess count variable. And this

150:57

just keeps track of how many times the user has guessed the word. And you can see

151:03

down here, every time we give them a guess, we're incrementing the guess count. We also

151:10

have guess limit. And guess limit is telling the program how many times the user can

151:15

guess. So before I go through this while loop, the first thing I'm doing is I'm

151:20

checking to see that the guess count is less than the guess limit. In other words,

151:25

do they still have some guesses left? If they do, then we're going to get the

151:30

input from the user. Otherwise, we have this other variable up here called out of

151:36

guesses. And out of guesses is going to tell us whether or not they have some guesses

151:40

left. So if this is equal to false, that means they have some guesses. If it's

151:45

equal to true, however, it means no more dice, no more guesses, they are done. So

151:50

they lost the game. The last thing we needed to do was specify an additional

151:54

condition up here. So we are going to keep looping through the code inside of this

152:00

loop, as long as the conditions up here are true. So as long as they haven't

152:06

guessed the word, and as long as they're not out of guesses, we are going to keep

152:13

looping through. And so when eventually the user does break out of this loop,

152:18

there's going to be two possible situations. The first situation is that they ran

152:22

out of guesses. And so we want to check to see if that's the situation. I want to

152:26

check to see if they're out of guesses. If they are, we'll print out a lose

152:30

message. If they're not, then they must have guessed it correctly. So they win. And so

152:34

that is how we can use while loops and if statements and also variables in

152:38

combination with each other to build a pretty awesome guessing game.

152:47

In this tutorial, I want to talk to you guys about using for loops in Python. Now

152:52

a for loop is a special type of loop in Python, which allows us to loop over

152:57

different collections of items. So a lot of times we'll use for loops in Python to

153:03

like loop through different arrays, or we can loop over like the letters inside

153:08

of a string, or we could just loop through like a series of numbers. So for loops

153:13

provide a very specific purpose. And the easiest way to kind of wrap your head

153:18

around why for loops are useful is just for me to show you guys a bunch of

153:21

different examples. So that's exactly what I'm going to do. We're going to look at

153:25

why for loops are awesome. So down here in my text file and my Python file, I'm

153:31

going to write out a couple of different for loops. So the way that we create a

153:34

for loop is just by saying for. And now what I want to do is specify a variable.

153:41

And this variable is going to essentially represent a different value every time we

153:48

go through this for loop. And you guys will see how that works in a second. But

153:53

just know that this variable is going to be used on every iteration of our for

153:58

loop. And each time it will most likely have a different value. So in our case,

154:04

I'm just going to call this letter. And I'm going to say for letter in. And now

154:10

what I want to do is I want to specify a collection that I want to loop over.

154:15

One example of this would be like a string. So I could put a string in here.

154:19

I could just put like draft Academy. And now I can just put a colon. And so

154:24

basically what this is going to say is it's going to say for every letter inside

154:29

of draft Academy, I want to do something. And so down here inside of this for loop

154:35

and again, we need to indent this. We can put what we want to do with each letter.

154:40

So let me just show you guys like basically what this is going to do. So I can print

154:44

out a letter so I can print out this letter variable. And it's actually going to

154:49

print out a different letter inside of this draft Academy string on every

154:54

single iteration of this loop. So I'm going to run this program. And down here

155:01

inside of my console, you'll see that I'm basically printing out draft Academy.

155:06

So on the first iteration of the loop, I printed out the first letter in draft

155:11

Academy, which was G. On the second iteration of the loop, I printed out I

155:16

said, third was R, fourth was A. So I'm essentially just looping through all of

155:22

the letters inside of draft Academy. So I'm saying for each letter in draft

155:28

Academy, I want to print out that letter. And so this is kind of like how four

155:33

loops can be used. We can define a variable. And that variable will change on each

155:39

iteration of the loop, right? So on the first iteration of the loop, this letter

155:43

variable represented a G. On the second iteration of the loop, the letter

155:47

variable stored at the value I, etc. Right. So we went through this entire string,

155:52

and we were able to print out each letter. So in addition to using this with

155:57

strings, we can also use this with other collections, for example, like an

156:01

array. So if I created an array up here, let's just call it friends, I'm going to

156:06

set this equal to a bunch of different values. So we can put like a list of our

156:10

friends in here, we can say like Jim, Karen, and Kevin, instead of saying the

156:16

letter, why don't we call this friend? And I can say for friend in friends. And now

156:25

we'll print out the friend. So over here, I'm saying for each friend inside of

156:31

this friends array, I want to print out the friend. So now we'll actually be able

156:36

to print out each element inside of that array. So down here, you'll see we're

156:40

printing out Jim, and then on the second iteration, we're printing out Karen,

156:44

and on the third, we're printing out Kevin. So I'm able to loop through all of the

156:48

values inside of the array. And just so you guys know, like, you can name this

156:53

variable, whatever you want. So I could name, I could give this like any random

156:57

name. And if I want to access it, though, I have to access it using that same name.

157:03

So we can loop through something like an array, we could also just loop through

157:07

a series of numbers. So I could say index over here. And again, this can be anything.

157:13

I'm just going to call it index. So we could say for index in range, and then in

157:18

here, I can pass in a number. So I could pass in like 10, for example. And down here,

157:24

I'm just going to print out the index. When I run this program, you guys will see that

157:28

it's basically going to print out every number in the range from zero to 10,

157:33

not including 10. So starting with zero, it's going to print out zero, one, two,

157:38

three, four, five, six, seven, eight, nine. But notice that it didn't print out 10.

157:43

So it's essentially just printing out all the numbers between zero and 10,

157:48

not including 10. You can also specify a range of numbers. So I could say, for

157:54

example, like three and 10. And now this will print out all the numbers between

157:59

three and 10, not including 10. So we're on this program. And you can see we print

158:06

out three, four, five, six, seven, eight, nine, and not 10. So whatever value that

158:11

you put here in this second position is not going to get included in the range.

158:17

And ranges can be really useful. So for example, I could use a range to loop

158:22

through an array, just like we did before. So if I wanted, I could say something

158:27

like four index in range. And now inside of this range, I can pass in the length

158:34

of the array. So just so you guys know, if I wanted to get the length of this

158:40

array, in other words, if I wanted to figure out how many elements were inside

158:44

of it, I could just type out LEN and then inside parentheses, the name of the

158:49

array. And so this is going to spit out three, because there's three elements

158:54

inside of here, right? Kind of makes sense. So what I can do is I can say down here

158:59

inside of this range function, I can just type in LEN and then friends. And so what

159:06

this is going to do is it's going to essentially give me a range between zero

159:11

and the number of friends inside of this list. So down here, I could actually type

159:16

out friends index. And this will allow me to access each individual friend inside

159:23

of this list, just like we did before. But now I'm doing it with a range. So you

159:28

can see I'm typing out Jim, Karen and Kevin. So actually, for each iteration

159:34

through this loop, it's basically going to be printing out friends zero, friends one,

159:41

and then friends two, because in here, we're passing in a three. And remember,

159:46

whenever I pass in there, it's going to range from zero all the way up to that

159:50

number, but not including that number. So that's another way that we could print

159:54

out all the elements in the array. And looping through something like an array

159:59

is actually a very, very common use case for for loops. But like I said, we can

160:04

use for loops to loop through essentially any collection that we have. So we could

160:08

loop through like a string, we could also loop through, you know, something like an

160:12

array. So this can be really useful. And that's one of the basics of for loops in

160:18

Python. And I also want to show you guys one more example, just while we're here.

160:22

So why don't we go ahead and print out a range up to five, right? So this will just

160:28

be a simple program. You can use all sorts of logic inside of these for loops. So

160:33

let's say that I wanted to do something special on the first iteration of the

160:37

loop. So I could say, if index is equal to zero. And if the index is zero, then we

160:44

know it's the first iteration of the loop. So I could do something special, I could

160:47

like print out first iteration. And then otherwise, we could just print like not

160:55

first. So this would be an example of like, maybe you want to do something on the

160:59

first iteration of the loop and do something else on subsequent iteration. So if I play

161:04

this, you'll see only on the first iteration is it printing this out. And

161:08

otherwise, it's printing out other stuff. So, you know, don't be afraid to put some

161:12

complex logic inside of these for loops, because it can really make your program

161:17

is more powerful. In this tutorial, I'm going to show you guys how to build an

161:24

exponent function. Now an exponent function is basically going to allow us to take a

161:29

certain number and raise it to a specific power. So in Python, there's actually a

161:34

really easy way to do this. I can come down here and I can print, we can say like

161:39

two, and we can just do two multiplication signs. And I can say three. And this is

161:45

basically just going to be two raised to the third power. So you can see here, I can

161:51

just print this out and we get eight, because that's too cute. So it's really

161:55

easy to do exponents in Python. But I want to show you guys how we can use

162:00

something like a for loop in order to create a function like this of our own.

162:05

So we'll actually create an exponent function that will basically do this and

162:10

we'll use for loops to do it. So let's create a function. So I'm just going to say

162:15

def. And now we need to give this function a name. So why don't we call it

162:19

raise to power. And inside of here, I'm going to accept two parameters. The first

162:25

parameter is going to be the base number. And then the second parameter is going to

162:30

be the power number. So we're going to take the base number and raise it to the

162:35

power number. So over here, we can just make a colon. Now, inside of this function,

162:42

we need to start writing some code. Here's the thing. Inside the function, we don't

162:46

necessarily know the value of this pound. Right. For example, if we knew that we

162:52

were just trying to like square this number or cube this number, I could just

162:57

return like base num times base num, right? That would square the number or I could

163:04

cube the number base num. So right. So this would be like the number cubed. The

163:08

problem is though, we don't know like right off the bat what this value is going to

163:12

be. Right. This could change like the user can decide that. So in order to write this

163:17

function, we're actually going to need to use a for loop. And I'm going to show you

163:21

guys how we can use a for loop to basically figure this out. So the first thing I

163:25

want to do is create a variable. And I'm just going to call it result. And we're

163:29

just going to set result equal to one. So we're going to start this off at one. Now

163:34

I want to create a for loop. So I'm basically just going to say four. And over here,

163:41

I basically want to specify like a index. So I'm going to say index in, and now I

163:47

want to specify a range and a range will basically range us through a collection

163:52

of numbers. So basically, I want to multiply the base num by itself as many

163:58

times as the power num specifies. Right. So over here, I can just make this a range

164:05

of power num. Right. And so we'll basically loop through this for loop as many times

164:12

as pound them. So if pound them is two, we'll loop through it twice. If pound them

164:17

is four, we'll loop through it four times. And every time through the loop will

164:20

multiply base num by itself. Or actually, you'll see we're going to multiply it by

164:24

this result. So down here inside of this for loop, I can basically say result is

164:30

equal to result times base num. This should actually give us everything we need to

164:37

take this number to the specific power. So the actual like result, you know, of

164:43

doing the math is going to get stored inside of this result variable. So the

164:48

first time through the loop, we're just multiplying base num by one. Right. And

164:53

so now result is going to become the same value as base num. The second time

164:58

through the loop. So this would be if we were squaring the number, we're basically

165:02

multiplying result by base num again. So it's essentially just base num times

165:06

base num. The third time through the loop, we're multiplying result times base num.

165:10

So we're essentially just like cubing the number. So that's essentially what this

165:14

for loop is doing for us. So the last thing I want to do is right here below

165:19

this for loop, I'm just going to return the result. So we'll be able to return

165:24

whatever the result of raising the number to the power was. All right. So let's

165:28

come down after this function. And we're just going to call it. So I'm just going

165:32

to say, actually, we'll print out the answer. So I'm going to say raise to power.

165:37

And inside of these parentheses, remember, we can pass two numbers. So let's

165:41

raise three to the second power. So we're just going to square three. And we'll see

165:46

how this works. So throw on the program. And you can see down here in the console

165:51

we're getting nine. Let's try something a little bit crazier. Let's try three to

165:56

the fourth power. And we'll run this and get 81. So that's three to the fourth

166:01

power. Let's try two to the third power. And we're getting eight. Yeah, so that makes

166:06

sense. So our raise the power function is working just as expected. And I'll just

166:12

walk you guys through it one more time. So inside of this function, we're taking

166:16

in two pieces of input, we're taking in a base number, and we're taking in a

166:20

power number. So that's like, the base number is going to be taken to the power

166:24

number. I'm defining a variable here called result. And result is where we're going

166:29

to store the actual result of doing the math. Now we specify this for loop. And

166:35

I'm basically saying that I want to loop through this range of numbers. When I put

166:40

pound on here, it's basically going to range from zero all the way up to but not

166:44

including the power number. So we'll basically loop through power number of

166:49

times, if that makes sense. And then every time through the loop, we're just

166:52

multiplying result by base num. And then we're just returning results. So that's

166:57

the basics of building a power function. And obviously, like if you're just trying

167:01

to take a number to a power and Python, it's really easy. But this kind of shows you

167:05

like the internal workings, right? For a function like that to work, we would have

167:09

to actually type something out just like this. And this tutorial, I want to talk

167:17

to you guys about two different concepts in Python. It's going to be a pretty cool

167:21

lesson. First thing I want to show you guys though, are two dimensional lists. So

167:26

down here in our Python file, I just want to create a basic list. So I'm just going

167:32

to call this number grid. And I'm just going to set this equal to a normal list

167:37

right just like this. Now normally if we were creating a list, I could just put in a

167:43

bunch of attributes and bunch of elements, right? And now all of these elements are

167:47

inside of this number grid list. Everybody's happy. But another thing I can do is I

167:53

can make all of the elements inside of this number grid list lists. So the first item

168:00

in the number grid list is going to be a list. The second item in the number grid

168:04

list is going to be a list, etc. So I'm going to show you guys how to do that. I

168:08

can basically just come down here and where I would normally put the first element

168:12

in the list, I can just create another array. So I'm just going to make an open

168:16

and closed square bracket. And now I'm just going to say 123. And I can create the

168:23

next item in my list. I can say 456. And let's make another item in this list. I

168:30

can say 789. And finally, we're going to put one more list inside of the number

168:36

grid list. And it's just going to have zero in it. So inside of this number

168:41

grid list, right, this high level overall list, we have four elements. And all of

168:48

those elements are themselves lists. So essentially what we're doing is we're

168:53

creating a grid, right? This grid has 1234 rows, and 123 columns, right, four

169:03

rows, three columns. And that's kind of like why these can be useful is we can

169:07

create a grid like structure inside of Python using two dimensional lists. So

169:13

this is a pretty cool list structure. And I want to show you guys how we can

169:18

access individual elements inside of this list structure. So, for example,

169:24

let's say I wanted to print out one of these values. Let's say I wanted to print

169:27

out this one right here. The way I can access it is by saying number grid. And

169:34

the first thing I want to do is make an open and close square brackets. And in

169:38

here, I want to put the index of the row that I want to access. So this would be

169:44

like row zero, because that's the zero element in the array. This would be row one.

169:49

It's at index position one. This would be row two, row three, etc. So I could put

169:54

row zero. And now right next to this, I'm going to make another square bracket.

169:59

And I want to put the index of the column. So this would be like column one, column

170:05

two, column three. So I'm going to put another zero, because this is at the

170:09

zero index of this first array. So I just put zero zero here. Now we can

170:15

print this guy out to the screen. And you'll see down here that we get that one. So I

170:21

could also do this for like for this eight, for example. So this is going to be zero

170:25

one two. It's going to be row two and column one. And now we should be able to

170:31

print out that eight. So that's basically how we can access elements inside of this

170:37

2D list. The next thing I want to show you guys is a nested for loop. And a nested

170:43

for loop is a situation, like I said, where we have a for loop inside of a for loop.

170:48

And I'll show you how we can use this nested for loop in order to print out all

170:53

the elements inside of this array. So I basically show you guys how to parse

170:57

through a two dimensional list or a two dimensional array. Alright, so basically

171:02

we're just going to create a normal for loop. So I'm going to say four, and I'm just

171:06

going to say row in number grid. And I'm using the word row here because I'm kind

171:12

of seeing these guys as rows, right? These are sort of horizontal rows in our little

171:18

grid. So for each of those rows, for each of the elements inside of number grid, I

171:24

want to loop through. So I could actually just print out each row right now. And it's

171:28

just going to print out all the rows inside of this list. You can see down here,

171:32

we basically just get exactly what we have up there. But I want to be able to

171:36

access each of these individual attributes inside of each of these array elements.

171:42

So we can actually create another for loop. And I'm going to put that for loop inside

171:47

of here. So now I'm going to say for column in row. And basically what this is going

171:55

to give us is each individual column or each individual element inside of these

172:01

arrays. So for example, inside of the arrays that are the elements of the number grid.

172:06

Hopefully that makes sense. So down here, now I can just print out column, and this

172:12

should actually print out every single value inside of this two dimensional array.

172:17

So let's go ahead and run this program. And down here, you'll see that we're

172:21

basically printing out one all the way down to zero. So we're printing out all

172:26

of the elements inside of all of the arrays inside of the number grid. So that's

172:32

how we can use two dimensional lists and nested four loops together. And both of

172:36

those things are actually pretty handy in Python. In this tutorial, I'm going to show

172:44

you how to build a basic translator in Python. So essentially, we can take in a

172:50

string so we can take in like a phrase or a word, and we'll be able to translate it

172:56

into a different language. So over here, I have this little file, and it basically

173:01

specifies this language that I made up. So I'm calling it the draft language.

173:05

And in the draft language, all vowels become G. So if I was going to translate an

173:12

English word into a draft word, for example, the word dog, I would look through the

173:18

word dog in any instances where there was a vowel. So in our case, let's just say

173:23

A E I O U, I would convert that vowel into a G. So dog would map to D G G, cat would

173:31

get mapped into C G T. So those are the basic translation rules. Obviously, this is

173:36

a pretty simple language, but you get the point any vowel is going to become a G,

173:41

and then we'll get the draft language. So we're going to build a draft translator.

173:46

Over here in our Python file, let's start making this translator. The first thing

173:51

I'm going to do is just define a translate function. So I'm going to make a function

173:56

here, and I'm just going to call it translate. And inside of these parentheses,

174:01

we want this to take one piece of information, which is going to be the phrase

174:06

that we want to translate. So I'm just going to say phrase. And now inside of this

174:11

function, we need to figure out like, okay, how can we translate English into our

174:17

draft language? Well, remember, the rules is basically just any vowels become G's.

174:23

So the first thing I want to do is create a variable. And this variable is going to

174:27

be the translation. So this will be like the final result that we're going to return

174:31

to the user. So I can say translation. For now, I'm just going to set it equal to

174:37

the empty string. So this is just going to be like a completely empty string. It's not

174:41

going to be doing anything. And now what we want to do is we basically want to

174:47

loop through every letter inside of this phrase. And if it's a vowel, we want to

174:52

change it to G. And if it's not a vowel, we want to leave it alone. And we're

174:56

essentially going to be looping through phrase. And we're going to be adding the

175:00

letters onto this translation one by one. So let me show you guys how we can do this.

175:05

I'm going to make a for loop. So I'm going to say four. And I'm just going to say

175:09

letter in phrase. And so now, when I loop through here, I can access each

175:17

individual letter inside of the phrase that they passed in. So what I want to

175:23

do is I want to check to see if the letter is a vowel or not, right? If it's a

175:28

vowel, then we can add a G onto translation. If it's not a vowel, then we can just add

175:34

on whatever letter it was onto translation anyway. So I'm going to use this if

175:39

statement. I'm going to say if. And there's actually this special thing we can do in

175:44

Python, we can check to see if something is in something else. So I can say if

175:49

letter in, and over here, I'm just going to type out all the vowels. So we'll say

175:55

A E I O U and A E I O U. So I'm basically checking to see if the letter is inside

176:02

of this string. And if the letter is inside of here, then we know that it's a vowel.

176:08

So down here, we can handle that case. So I can say, translation is equal to

176:15

translation plus G. Because if this letter is a vowel, we just want to convert it

176:24

into a G. Otherwise, though, I can basically just say, translation is equal to

176:32

translation plus letter. So in the case where we have a vowel, I'm adding a G onto

176:39

translation. In the case where we don't have a vowel, I'm just adding on whatever

176:43

letter it was onto translation. So finally, down below this for loop, we just want

176:49

to return the translation. So I'm just going to say return translation. And now we

176:56

have a awesome translate function, and it should probably work. So let's come down

177:01

here and we'll test it out. So why don't we allow a user to input some

177:05

information. So I'm basically going to call this function. Actually, we're going to

177:09

print it out. So we'll print this out. And I'm going to call this function

177:13

translate. And inside of here, I'm actually just going to pass whatever the user

177:20

inputs. So I can just say input. And inside of these parentheses, I can say the

177:26

prompt. So enter a phrase. And so now I'm basically combining all these statements

177:32

together. I'm saying I want to print out the translation of whatever the user

177:38

enters in. So let's go ahead and run this program, and we'll see how we do. So I'm

177:44

going to run the program. It says enter a phrase. Actually, let me format this a

177:49

little bit better. Okay. All right. So it says enter a

177:55

phrase. So let's go ahead and do that. So let's just type in the example we used

178:00

before. I'm going to say dog. And so now out pops the answer, which is d g g. So

178:06

that's kind of cool. And we can just keep running this as many times as we want.

178:10

So I could type in like to be or not to be. So this is like a little bit longer of

178:15

a string. And you can see it translates it into our draft language. So that's

178:20

basically how we can create our little translator app. And that's pretty cool.

178:25

And so you can see we're using a for loop in combination with an if loop. And

178:30

that's a really powerful structure. And actually, there's one place over here

178:34

where we can make this a little bit more efficient. So instead of saying if letter

178:39

in this string, you'll notice here, I'm checking to see if it's in all the lower

178:44

case and the uppercase. I could actually just say if letter dot lower in. And now

178:51

I only have to type out the lower case letters. And it's still going to do the

178:55

same exact thing. So that's a little like hack or whatever that we could use. And

178:59

there's also one more problem with this program. So you'll notice down here,

179:03

we're setting the translation equal to the translation plus g. But the problem is

179:08

if I was to start my word inside of the phrase with a capital vowel. So in other

179:14

words, if I said like on, you'll notice that when this gets printed out, it gets

179:19

printed out as lowercase g and so it's not keeping our uppercase syntax. And that's

179:25

kind of a problem. So we can actually use another if statement in here. And we

179:29

can check to see if letter dot is upper. And if the letter is uppercase, then we

179:40

can just set the translation equal to the translation plus a capital G. Otherwise,

179:48

we can just set it equal to the lowercase g. And so now we're able to control both

179:54

of those situations. So let's just test this and see if it works. Enter a phrase,

180:00

say on. And now we're keeping that capital letter. So those are just a couple

180:05

different ways that we could make this program a little bit better. But the

180:09

basic concept is we have a for loop and then we have some if loops inside of it.

180:12

And we're calling this awesome function that can translate a word for us.

180:17

In this tutorial, I want to talk to you guys about comments in Python. This is

180:24

going to be a pretty short tutorial. I just want to give you guys an overview of

180:28

what comments are and how we can use them in our Python programs. So over here in

180:33

my Python program, I just have this line here. It says print comments are fun. So

180:38

if I run my program down here inside of the console, it's going to print out

180:43

that comments are fun, right? So any code that I put over here is going to get

180:48

executed by Python. But there's actually a special line of code that we can write

180:54

and it's called a comment. And a comment is basically a line inside of our Python

180:59

file that's just not going to get rendered by Python. So when Python sees it,

181:03

it's just going to ignore it. It's not going to execute it. It's not going to

181:06

print anything out. It's just going to kind of ignore it. And comments are

181:10

going to be just used for us humans. So a comment is used for like me or another

181:15

developer to write a little comment, a little like plain text, you know, comment

181:20

inside of a file. In order to create a comment, I could use this hashtag symbol.

181:25

So I can click hashtag and now I am writing a comment. So I can write whatever I

181:30

wanted in here. I could say like this program is cool. And you'll notice when I

181:37

put on the program, that doesn't show up anywhere, right? It doesn't matter what

181:41

I put after this hashtag. This is just like ignored by Python. It's just for me

181:46

or another developer to come in here and use. So a lot of times if you want to

181:50

write like a little note inside of your Python files, or maybe you can write

181:54

yourself a little reminder, or if you want to write a line to like explain a line of

181:59

code. So for example, I could write a comment that would explain this print

182:03

statement. So I could say like this prints out a string, right? Now anybody looking

182:08

at my file would be able to read this and they'd be like, Oh, that's what this

182:12

does. Okay, cool. So comments are useful for leaving little comments in a Python

182:18

file. If you want to make comments on multiple lines, you can just create a new

182:22

line and use another hashtag. And so then I could type out whatever my next comment

182:26

was. And generally, if you're going to put comments on multiple lines, you want to

182:30

just use multiple hashtags. But there's actually another way that you can write

182:35

out comments. So you can use a triple quotation mark. So I'm typing out the

182:40

single quote. And I'm typing it out three times. So you can see here up here, we

182:45

have this three times, we have this three times. So in here, I can, you know,

182:50

write any comment that I wanted. So I could put, you know, as much text up here as I

182:54

would want. And same thing, when I run this program, it's just not going to get

182:59

rendered. So this is another way to make multiple line comments. But the, like,

183:05

official Python, like style guides or whatever, just recommends that if you're

183:10

going to put comments on multiple lines, you use these hashtags. So, you know,

183:14

you can really do whatever you want. But if you want to be like super Python

183:17

official, then use these hashtags. Comments can also be useful for doing something

183:23

called commenting out a line of code. A lot of times when you're writing a

183:27

program, you might have a line of code in there that is maybe like potentially

183:32

messing stuff up. And so what you can do is you can actually comment out that

183:36

line of code. So I could put a comment in front of this line of code. And now when

183:41

I run my program, the program is going to run, but it's not going to use this line.

183:46

So sometimes if you're trying to see if like a line of code is causing you trouble

183:50

or if you want to see what your program would be like without a line of code, or

183:54

even like a block of code, you can just comment it out. And that way you're not

183:58

actually deleting the code from your file. You're just commenting it out. So Python

184:03

is going to ignore it. And those are really the two uses for comments is to write

184:07

little notes, write little comments about different things or to comment out code.

184:12

And as you go forward in Python, I'm sure you'll find uses for comments.

184:17

In this tutorial, I want to talk to you guys about catching errors in Python.

184:25

Now, a lot of times when we're running Python programs, you'll encounter different

184:29

errors. So different situations could come up and your program might throw an error

184:33

or it might throw an exception. And a lot of times when these situations happen,

184:37

they'll completely stop your program from running. And what we can actually do is

184:42

we can watch out for, you know, certain specific errors that are going to pop up

184:47

in our program and we can actually handle them. So instead of our program just

184:51

breaking and stop executing, we can actually handle those errors and do things

184:55

when they occur. So I want to show you guys an example of using this down here.

184:59

I'm saying number is equal to int input enter a number. Basically what's happening

185:04

is I'm prompting the user to enter in a number using this input command. And then

185:09

I'm converting whatever they entered into an integer. So as long as they enter in

185:14

like a valid integer, I can convert it into an integer. So I can convert it into

185:18

like a whole number and I can store it inside of this number variable that I'm

185:22

just printing it out. So let's try to execute this program. I'm going to run

185:25

the program and you'll see down here it says enter a number. So we can enter in

185:30

like four, for example. And when I enter in four, everything's going to work and

185:34

it's just going to print out four and everything is right in the world. And

185:38

actually, I'm going to modify this up here. So it's a little easier to see. But

185:42

let's say we run the program again. So it says, okay, enter your number. Let's say

185:46

that I break the rules and I don't enter a number. So I just ignore what the

185:50

prompt says and I put, you know, some random text in here. Now when I enter this,

185:56

the program is going to throw an error. You can see down here, it says value error

186:00

invalid literal for int base with base 10. Basically, hey, you didn't enter in an

186:06

integer like you said you would, right? We told them to enter in a number. They

186:10

entered in text. So it broke the program. And this is a situation that happens a

186:16

lot. So there's a lot of different situations where something can just go

186:19

wrong in our program. And I'm until now we've just kind of accepted it as a

186:23

reality. But if you're writing a real live Python program, you don't want something

186:28

like this to trip up your program. You don't want the entire program to break when

186:31

somebody just forgets to enter in a number, right? You want to be able to handle

186:35

all these different situations. And in order to handle them, we can use something

186:39

called a try except block. And a try except block will basically allow your

186:44

program to try out a piece of code. And if everything goes well, then we're great.

186:49

Like we could try out entering in the number. And if the user enters in the

186:53

correct number, great. But if they don't enter in the correct number, then we can

186:57

basically like account for that. So they entered in something like a string when

187:00

they're supposed to enter in a number, we could tell them like, Hey, you entered

187:04

in the wrong thing. So I'm going to show you guys how we can use these try except

187:08

blocks. I'm going to come up here, and I'm just going to say try. I'm going to make

187:12

it colon. And I want to indent both of these things. So both of these things are

187:17

going to be inside of this try block. Now I'm going to make a new line. I'm going

187:21

to come down here and I'm just going to type out. Except, and I'm also going to

187:25

put a colon here. And down here, I'm just going to print something out. So I'll just

187:29

say like invalid input. So now, since I put this code inside of this try block,

187:36

if the user enters in something wrong, it's going to be able to catch it. So it'll

187:40

be able to catch what the user does wrong and print out invalid input onto the

187:45

screen. So let's go ahead and run our program. And it's just going to say enter

187:49

a number. So we're going to ignore that. We'll enter this in. And now instead of

187:53

yelling at us and breaking the program, it just says, Hey, invalid input. So it was

187:58

able to handle the error they got called. It was able to handle the invalid input

188:03

error. Basically just told us, Hey, no more input. So this is a really powerful

188:09

thing that we can do in our Python programs. Basically just to protect our

188:13

programs, right? If I want to have my program running for long period of time,

188:17

I can't have something as simple as an invalid input, trip it up. So I can

188:21

catch that invalid input using these try accept blocks. But here's the thing.

188:25

You'll notice that this little guy down here is getting highlighted. And let's

188:29

just see what it has to say. So it's basically saying to broad exception clause.

188:34

And this is a message that we're getting because when I just say accept, this

188:39

will catch any error under the sun. So anything that can go wrong on my program

188:43

for the most part, this is going to accept it. So let me give you guys an

188:47

example. Let's say I had in my program, I was creating a variable, right? So I

188:52

wanted to create a variable called value. And I set it equal to 10 divided by

188:57

zero. Now, if you know anything about math, you'll know that you can't divide a

189:00

number by zero. It's not possible. So the program isn't going to be able to

189:05

handle this. And you'll see that this is going to throw an error for us. So when I

189:08

run the program, it basically says zero division error division by zero. We're

189:13

not allowed to do that, right? If I took this and I put this down inside of this

189:18

dry block, and I just pasted it in here, it's going to get caught by this

189:23

exception that we wrote down here. So it's going to get caught by that

189:27

accept. So when I run the program, you'll see it says invalid input. But the

189:32

problem is that we didn't input something that was invalid, right? We just divided

189:37

by zero, but it wasn't able to handle that. Right. So it could not divide by zero

189:42

it through an error. So even though we didn't get an invalid input, even though

189:46

it was because we divided by zero, it's still printed this out. And this brings

189:50

me up to another point is we can actually catch, or we can accept specific

189:55

types of errors. So for example, in this case, we got a division by zero error. In

190:00

this case, we got an invalid input error, right? We put something in that was wrong.

190:05

You couldn't convert the value. So I can create two different accept blocks to

190:09

catch two different types of errors. So down here, I can just make a space and I

190:14

can specify the type of error that I want to catch. So when we divided by zero, it

190:19

was zero division error. So if I just put zero division error in here, now it's

190:25

going to catch that zero division error. So I can say divided by zero. I could

190:31

also make another accept. So I could say accept. And we can catch the other

190:36

exception that we got. So I think it was a value error, right? It was, we put in

190:41

the type of value inside of that input. So now we can print out invalid input. So

190:47

with these two accept blocks, we're basically able to handle both of these

190:52

situations. So for example, if this ends up breaking the program, this division by

190:57

zero, then it'll be able to catch that. So down here, it'll tell us, Hey, you

191:01

divided by zero. But if I was to get rid of this line of code, and I just entered

191:07

in the number wrong. So I came down here and I entered in a string instead of a

191:11

number. Now it's going to tell us invalid input. So we're going to be able to

191:15

specify what happens when certain things break. So when we get a valid, when we

191:20

get a value error, we can say invalid input, when we get a zero division error, we

191:24

can say divided by zero. So depending on what breaks, depending on what happens,

191:29

we can actually do different things. And I want to show you guys one more thing

191:34

we can do, which is we can actually store this error as a variable. So I can say

191:40

accept, I can say as, and I can type in a name here. So a lot of times people will

191:45

just say like, e r r. And down here, I can print out the actual error. So I can just

191:51

print e r r. And so if we end up dividing by zero. So if I say answers equal to

191:58

10 divided by zero, we're going to throw this error, and it's going to print out

192:03

what went wrong. So now I can run this program. And it just says division by zero.

192:08

So it just printed out the error that we got. So that's another useful thing you

192:13

can do is you can actually like print out the specific error that got thrown. Now,

192:17

a best practice in Python is to use these specific errors. So you always want to

192:22

accept and accept for a specific error. What you don't want to do is just say

192:28

accept, and then just accept anything under the sun. It's just too broad. And it's

192:35

not a good practice in Python. So you always want to be catching specific errors like

192:39

this. In this tutorial, I want to talk to you guys about reading from external

192:47

files in Python. Now a lot of times in Python, you're going to want to read from

192:52

files that are outside of your Python file. So you might want to read information

192:57

from like a text file or a CSV file or like an HTML file. And you can actually use

193:03

something called the Python read command. And it will allow you to read a file that

193:09

is stored outside of your Python file. So you can use these files to get information

193:14

or you can parse through different files and do different things. So I'm going to

193:18

show you guys the basics of reading files, opening files, closing files, doing all

193:23

that stuff. Over here, I have this file called employees dot text. And it basically

193:30

just lists out a bunch of different employees, like these could be employees in

193:34

an office or whatever. So it's just listing out all of this information. So let's say

193:39

that inside of my app dot Python file, I wanted to read the employees inside of

193:45

that file. The first thing I have to do is actually open that file from inside

193:50

of Python. So I can use a special command called open. So I can say open. And then

193:56

in here, I can type in the name of the file that I want to open. So this is either

194:00

going to be a relative path to the file, an absolute path to the file, or just the

194:05

files name if both files are in the same directory. So in my case, app dot Python

194:11

and employees dot text are in the same folder, like they're in the same directory.

194:16

So I can just type out the name of the file. I can just say employees dot text.

194:21

And then I want to put one more thing inside of this open function. And it's

194:26

going to be the mode that I want to open the file in. So you can actually open

194:30

files in a couple of different modes. And the first mode is called read. So I can

194:35

just put an R here and that's going to stand for read. And this basically means

194:39

that I only want to read the information inside the file. I don't want to modify

194:43

it. I don't want to change it. I just want to read it. I just want to see what's in

194:47

the file and do some stuff with that information. Another mode is called

194:52

write. So I can type in this w and writing basically means that you can change the

194:56

file, right? You can write new information. You can change existing information.

195:01

There's another one called a and a stands for append. And this basically means

195:06

that you can append information onto the end of the file. So you can't modify

195:11

any of the information in the file. You can't change any of the information, but

195:15

you can add new information. And there's one more, which is R plus. And this

195:20

basically means read and write. So this will give you all the power of reading

195:25

and writing. So in our case, we're just going to be working with regular R. So

195:30

we're going to be reading from the file. Now this open function will essentially

195:35

just open the file. So it's going to like go over to that file inside of our file

195:40

system, open it up, and it'll allow us to read through it. But generally, we're

195:44

going to want to store this opened file inside of a variable. So I can create a

195:50

variable and we can just call it employee file. And I'm just going to set it equal

195:55

to this open function. So now the open employees dot text file and all the

196:01

content inside of it is stored inside of this employee file variable. Now, whenever

196:07

you open a file, you always want to make sure that you close the file as well. So

196:12

just like we have this open command, we also have an close function. So I can come

196:18

down here and say employee file dot closed. And this is essentially just going to

196:24

close the file. So we're no longer going to be able to access it. And generally,

196:29

it's a good idea whenever you're opening up a file, you want to also make sure that

196:33

you're closing the file at some point. So generally, once you're done reading it,

196:37

you can just close it. So that's kind of like how we can open and close a file.

196:41

Now, let's talk about how we can get information from the file, right? There's no

196:45

point in having the file that we can't figure out what's in it. So there's actually

196:48

a few different functions that we can use on this employee file to figure out what's

196:53

inside of it. And I'm going to show you guys some of those. So I'm just going to

196:56

make a print statement. And inside this print statement will basically just print out

197:01

some information about the employee file. So the most basic thing we can print out

197:07

is just the entire contents of the file. But before I do that, I want to show you

197:11

guys how you can check to make sure that a file is readable. So before we do anything

197:16

else, generally, it's a good idea to make sure that it's possible to read this file.

197:21

And there's a function inside of Python, we can use called readable. So I'm just

197:26

going to type out employee file dot readable. And this is going to return a

197:34

Boolean value. And it's going to tell us whether or not we can read from this file.

197:38

So I'm going to run my program. And you'll see down here, we get a value of true. And

197:43

that's because we set the file with a read mode. So it's in read mode, we can read

197:48

from it. If I was to put a double you here. So if I put like, right, now readable is

197:54

going to be false, because we can no longer read the file, we can only write to the

197:58

file. So I'm going to change this back to our so we can just read it. So once you

198:04

figure out whether or not the file can be read from, let's actually read it. So

198:09

there's another function called employee file dot read. And this is basically just

198:13

going to spit out all the information in the files. So when I run the program, it's

198:17

just going to spit out all of this information, right, all the information that

198:21

was in that file. I can also come down here and we can read an individual line

198:26

inside this file. So I can say employee file dot read line. And what this is going

198:31

to do is it's going to read an individual line inside of this file. So now when I

198:36

run this program, you'll see it's just reading that first line in the file. And

198:41

this read line function is actually just reading the first line. And then it's

198:45

basically like moving a little cursor onto the next line. So if I was to copy this

198:50

code and then print it again down here, I'm saying employee file dot read line. So

198:56

it's going to read the first line. And then when I say it again, it's going to read

199:00

the line after that. So this is actually going to end up printing out the first

199:03

two lines in the file. So when I run this program, you'll see we print out Jim

199:08

salesman and Dwight salesman. So if I was to do this multiple times, like I could

199:13

technically print out every line inside of this file. And you can see we can do

199:19

that. And so that can be pretty useful for reading multiple lines in a file. But

199:23

there's actually another function that is better at doing that. And we can say

199:27

instead of employee file dot read line, we can say dot read lines. And what this is

199:32

going to do is it's going to take all of the lines inside of our file and put them

199:36

inside of an array. And so now when I print this out, you'll see we have this

199:40

array down here. It says Jim salesman, that's the first item in the array, Dwight

199:45

salesman, the second item in the array, it's basically taking each line and putting

199:50

it inside of an array. So if I wanted to access a specific line, I can just

199:54

refer to it by its index in the array. So if I said one, now this is going to give

199:59

us that Dwight salesman line, because that is at index position one inside of the

200:04

array. You can also use this read lines function with a for loop. So I can come up

200:10

here and create a for loop. I'm just going to say for, and we'll say employee in

200:16

employee file. And then for each employee, we just want to print them out. So and

200:22

actually started, we have to put employee file dot read lines up here. And so now this

200:28

will loop through all of the employees in this employee file that read lines array.

200:32

So we can actually just print out the individual employee. And now this will print

200:38

out all the employees inside of that file. So it's basically printing out each

200:42

line in the file. And that can be pretty useful. So you can use all of these

200:47

different functions like read, read line, read lines, readable, you know, there's a

200:52

bunch of these different things that we can do to get information from a file. And

200:56

so there's a lot of cases where you're going to want to be able to parse through

200:59

information in a file. And this is a awesome way to do it. So just to recap,

201:03

whenever you want to open a file and read from it, you can just use this open

201:07

function, type in the name of the file and then the mode, which in our case is going

201:11

to be our, then you can do all sorts of stuff with it. And you always want to make

201:15

sure that you close it when you're done. That's just good practice. So that's the

201:19

basics of reading from files. And hopefully you guys can use this in some way, shape

201:24

or form in the future. In this tutorial, I want to talk to you about writing and

201:32

appending to files in Python. So one of the cool things about Python is it allows you

201:38

to work with external files. So I could have like an external text file and I could

201:43

actually like completely read all of the information in it. I could parse through

201:47

it, use that information to do certain things. But in addition to reading a file, I could

201:52

also write a file. And that's what I want to talk to you guys about today is writing

201:56

new files and appending onto existing files. Over here, I basically just have some

202:01

written out and this essentially just reads information from this employees.text file.

202:06

So you can see over here, I'm specifying the mode, which is R and that stands for

202:11

read. And then down here, I'm just reading all of the contents of the file and

202:15

spitting it out on the screen. So I'm going to click the play button over here and

202:19

you'll see that this executes. So it's printing out all of the lines of code in

202:25

our text file. So over here, I'm in this employees.text file and it just has all

202:30

this information, like employees in an office. But let's say that I wanted to

202:34

add another employee onto here, right? Let's say that a new employee joined our

202:39

company. So we wanted to add them onto this list. Well, I can come over here to

202:43

my app.python file. And instead of reading from the file, I want to append to the

202:49

file. So I want to say a and appending to the file basically means that you're

202:54

adding some text at the end of the file. So wherever the file ends, you're just

202:59

going to add some text onto there. So what we can do is we can actually add

203:03

another employee into the file. So instead of printing something out, I'm

203:08

actually just going to say employee file dot right. And when I say employee file

203:15

dot right, I'm going to be able to write something to the end of the file. So I

203:20

can basically just write whatever I want. So why don't we add in another employee

203:25

into our employees dot text file. So we can add in another employee. Why don't we

203:29

say like Toby, and he's going to be in human resources. So now, when I run this

203:38

program, it's going to add Toby human resources onto the end of the file. So I'm

203:43

going to run my program. And you'll see that nothing shows up down here in the

203:47

console. But if I go over to my employees dot text file, all of a sudden we have a

203:53

new entry over here, it's Toby from human resources, right? So I was able to

203:58

append a line onto the end of this file. But here's the thing, you need to be

204:03

careful when you're writing to files, because you can actually mess up a file

204:07

very easily. For example, I already added Toby here into my file. But if I was to

204:12

run this program again, you'll see that over here in this employees dot text

204:17

file, it went ahead and added Toby again, right? So it added this employee here

204:22

again. And also, you'll notice that in this case, this employee didn't go on to

204:27

the next line. I accidentally ran my file again, and all of a sudden it messed up

204:32

this file over here. And so appending, you really need to be careful because if you

204:37

actually run your file again, or if you, you know, append something on something

204:41

wrong to the file, it's permanent, like it's getting saved inside of the file. So

204:46

I want to talk to you guys a little bit more about, you know, appending. Another

204:50

thing we could do, let's say we wanted to add another employee. And you'll notice

204:54

over here in this employees dot text file. When I appended it on again, it got

204:59

appended to the end of the existing line, right? So the first time I had a new

205:04

line there, but if you don't have a new line at the end of your file, and you all

205:08

and you want to add a new line, you're going to have to add some special

205:11

characters. So let's add another employee, and we'll call her Kelly, and let's just

205:16

say Kelly is in customer service. So Kelly is going to be in customer service.

205:25

And if I want to add this employee onto the end of the file in a new line, I'm

205:30

going to have to put a new line character in front of it. So I can say backslash n,

205:35

and this will append this entry into the file with a new line. So on a new line.

205:40

So now when I run this, you'll see we get Kelly from customer service on her own

205:45

line. So you want to make sure that you're aware of like these special

205:49

characters that you can use, they call them escape characters. And you know,

205:53

anytime you're adding onto a file, you want to make sure that you're adding on

205:56

exactly where you want to add on. So in addition to appending to a file, I could

206:01

also just like overwrite a file or I could write an entirely new file. So since we

206:07

already have this open, instead of appending to the file, why don't we just

206:11

write a file? So I'm going to use this W. And now if I say employee file dot right,

206:18

because I'm using W and I'm not using a, it's actually going to override the

206:23

entire file. And it's only going to put this inside the file. So when I run this,

206:28

and we go over to this employees dot text file, you'll see we only have one line

206:32

inside of this file. Now it's just Kelly customer service. That's because I was

206:37

using W not a when you use W, it's just overriding everything that's in that

206:42

existing file. You can also use W to create a new file. So over here, I could say

206:48

employee file is equal to employees one dot text. And now what's going to happen

206:54

is when I run this, it's going to create another file for me. So I'm going to run

206:58

this. And you'll see over here in my file browser, we have this new file

207:03

employees one dot text. So if I open this up, it has exactly the same stuff as in

207:08

this employees file. But it basically created a new file for us. And so a lot of

207:13

times you're going to want to create a new file. And you can use different

207:17

extensions too. So if I wanted to create like a web page, I could say index dot

207:23

HTML. And I could also add in some HTML code in here. So if you don't understand

207:28

HTML, don't worry about it. But if you do, I could put like a paragraph in here,

207:33

another paragraph, like this is HTML, basically HTML is like a web page. And

207:40

the point I'm trying to make is that you could write out a web page inside of Python

207:44

by doing something like this. So now when I play this, we get this index dot HTML

207:49

file. And it has some HTML inside of it. So that's one way that writing to files

207:55

can be really useful. You can overwrite an existing file, you can write a new

207:59

file and create it, or you can append onto the end of a file. And there's tons of

208:05

applications for writing to files and Python's a great language for working with

208:09

reading, writing, and doing all that stuff with files. In this tutorial, I want to

208:17

talk to you guys about using modules in Python. Now, a module is essentially just

208:22

a Python file that we can import into our current Python file. So for example, if I

208:28

wrote a Python file that had a bunch of like useful functions or useful variables

208:33

or, you know, other things like that, I could take that file, I could import it

208:38

into the file I'm currently working on, and I could actually access all of those

208:42

functions, all of those variables, all the stuff from that external file inside the

208:47

file that I'm currently working on. So I'm going to show you guys how we can use

208:51

modules, and then we'll talk about like, you know, where you can find awesome

208:55

modules and really why modules make Python an awesome language. So over here in my

209:01

text editor, I actually created this file and it's called useful tools dot Python.

209:07

And basically this file just has a bunch of sort of like useful tools that I might

209:12

want to use in one of my programs. So you'll see over here, I have some

209:16

variables. This one is telling me how many feet are in a mile. This one's telling me

209:20

how many meters are in a kilometer. And then we have this list here, which lists

209:24

out all of the members of the Beatles. I also have a couple different functions

209:29

down here. So I have this get file extension function. And this basically just will,

209:34

you know, you give it a file name, it'll tell you what the extension is. And then we

209:37

have this other function down here, which simulates rolling a dice. So you pass

209:41

it a number. If I pass it like a six, it would roll a six sided dice. If I pass

209:45

it a nine, it roll a nine sided dice, etc. So this is like a Python file that I

209:49

wrote. And it has some useful stuff in it. And honestly, there's a lot of stuff

209:53

in here that I might want to use in the other Python files that I work with. So

209:58

let's say I'm over here at this app dot Python file. And I'm thinking to myself,

210:03

huh, I think I could use one of those functions that was inside of that useful

210:08

tools file. Actually, yeah, I need to simulate rolling a dice in my program.

210:13

Well, instead of having to go over here, copy this function and then paste it

210:18

over here into my program, I can actually just import this useful tools file. And

210:23

I'll be able to import all of these functions and all of these variables

210:28

and attributes. So the way I can do that is just by coming up here. And I'm just

210:32

going to go right at the top of my file. And I'm just going to say import. And

210:36

then I want to type in the name of the file that I want to import. So I'm just

210:40

going to say useful underscore tools. And Python is going to be smart enough to

210:45

know that it should go off and grab all the stuff from this useful tools dot

210:50

pi file. So once I've used that import statement, I can actually use all of

210:55

the functions inside of that useful tools file. So for example, I could simulate

211:00

rolling a dice, I could say useful tools dot. And now I'm actually able to access

211:08

all of the attributes from inside that file. So when I say useful tools dot,

211:12

you'll see down here in my little suggestions menu, it's telling me like

211:16

Beatles feet and miles, get file extension meters and kilometers roll dice. So it's

211:21

giving the access to all of the stuff that was in that file. So over here, I could

211:26

just say like roll dice and we can pass this like a 10 will roll a 10 sided

211:31

dice. And now this should actually be able to run that function. So we'll simulate

211:36

rolling a 10 sided dice. You can see we got four. And this is a really core

211:42

concept in Python, which is importing functionality from external Python files.

211:49

And this is like huge in Python. And this is seriously going to change the way that

211:53

you create your Python files. So you'll notice like I didn't have to copy any

211:59

functions or any variables or anything over into this file. And yet I was able to

212:05

use all the stuff that was inside this useful tools file. So that is huge. And

212:10

honestly, it's going to save you a lot of time because you can write something

212:14

once and then you can import it into your other files. So that's really the

212:18

basics of using modules. I mean, modules are very simple. A module is just any

212:22

external Python file that you want to use some stuff inside of it. I want to

212:27

show you guys a place where you can go to find a huge list of modules. So I'm

212:33

going to go over here to my web browser. And I'm on this website. It's actually

212:38

like the official Python docs. And basically all I did to get here was just type in

212:43

list of Python modules and Google. And depending on the version of Python you're

212:47

using, you're going to want to make sure that you click the right one. I clicked

212:50

on the version 31. And over here on this page, you can see there's just a huge

212:54

list of Python modules. And these are basically modules inside of Python that

212:59

you can just access. So essentially, there's like all of this awesome code

213:04

that's already been written for you. And so there's some sort of functionality

213:08

that you want to have inside of your Python program. Chances are there's a

213:12

module in here that has that functionality. So there's all sorts of stuff you can do.

213:17

I mean, if you just look through this list, like, you'll see a huge collection of

213:21

basically just, you know, a bunch of either Python variables or Python

213:25

functions, just, you know, things that you can use to make your programs better.

213:29

So here's what I would recommend, like, head over to this page and just sort of

213:34

look through a lot of these different modules, you know, see what you can see.

213:37

And honestly, like, if you click on one of them, it'll bring you to a page that

213:41

talks about like how to use it. It'll tell you like how to import it and just,

213:45

you know, some basics about it. And this is sort of like the list that's on the

213:50

official Python docs. But here's the other thing. The Python community is huge.

213:56

There's tons of developers who use Python. And you can actually find a lot of third

214:01

party modules. So if you just go online and, you know, look up, like, Python

214:05

module for doing X or Python module for doing Y, chances are somebody out there

214:10

has already written a Python module to do what you're trying to do. So if you get

214:15

good at using modules and you get good at, you know, looking for them and finding

214:18

them, you can actually save yourself a bunch of time because chances are that

214:22

somebody's already written a module to do, you know, what you're trying to do or

214:26

like part of what you're trying to do. So now that we've taken a look at all these

214:30

different Python modules, I want to just kind of dive in a little bit deeper into

214:34

how we can actually use these things. So you'll notice here, there's a lot of

214:39

files, right? There's a lot of different modules that we apparently have access to

214:44

Python. But the question is, where are all these files stored, right? When I was

214:49

over here in my program, for example, I was using this useful tools.py file.

214:54

Like, I knew where that was. I created that file. I was directly involved in

214:59

making it. And I just imported over here. It was pretty easy, right? But what about

215:04

all of these files, right? What about all these modules over here? Like, where are

215:09

all of these stored? And there's basically two types of modules here. There's built-in

215:13

modules, which means they're just built into the Python language. So we kind of

215:17

just automatically have access to them. And there's external modules. And a lot

215:23

of these external modules are just stored in basically the same folder that we

215:28

installed Python on our computer. So for example, let's look at a couple of these,

215:32

right? We have, like, base 64, BDB, bin ASCII, like, if I come over here and

215:40

you'll see, I'm over here in my little file explorer. I have my Python project,

215:45

which is draft. There's also this other folder over here called external

215:49

libraries. If I was to click down on this and I come down here, you can see it's

215:54

just the version of Python that I'm using. There's a folder here called lib.

215:59

And this is a very important folder. This is basically storing all of those

216:03

external modules. So you can see if I scroll down here, we have all of these

216:08

different, like, modules, right? So here's that base 64. Here's BDB, right? A lot

216:14

of these external modules are stored inside of this lib folder. And like I

216:19

said, there's external modules. There's also a few modules that are just like built

216:23

in modules. They're not going to be stored inside of here. And you can actually

216:27

tell. So for example, if I was to come over here and click on base 64, it tells

216:32

me where the source code is. So the source codes in lib forward slash base 64.

216:38

We were able to see that, right? I was, I saw that inside of my lib folder.

216:42

This bin ASCII, though, for example, if I click on this, you'll notice that this

216:47

isn't giving me a folder because this is basically just like built into Python.

216:51

So we don't have to worry about, you know, locating that file. It's just kind of

216:56

like, it just kind of works. So some of these are external. Some of them are

216:59

built in. And I want to show you guys, in addition to using these modules,

217:04

right? So there's a lot of good stuff here. And honestly, you could spend, you

217:07

know, years and years just learning about all these different modules. But a lot

217:11

of times you're going to want to use modules that other people have written.

217:15

And you know, there's a lot of developers who work on Python and who write different

217:21

modules. So there's a lot of useful modules out there that aren't included in this

217:25

list, right? They're not going to be inside of this lib folder by default.

217:30

And what we can actually do is we can install those external modules, those

217:35

third party modules, that don't just come pre installed with Python. And I'm

217:40

going to show you guys how to do that really quick. So the first thing you need

217:43

to do, obviously, is find a Python module that you want to install that you want

217:48

to download. And I have actually used one of the past Python docs. It's an

217:54

external module that you can use to basically use Python to create Word

217:58

documents, which is pretty cool. So you can like format Word documents inside

218:02

of it. So I'm just going to look that up here in Google. I'm just going to type

218:06

in Python docs. And look, here's the thing, there's tons of these external modules.

218:11

You don't have to use Python docs. I'm just giving you guys an example. But really,

218:16

if you just look up like useful third party Python modules, there's going to be lists

218:20

of these things online. In my case, though, Python docs has a website, and it

218:25

basically just has like some installation instructions. I'm going to come over here.

218:29

And it tells me that I can install Python docs using the command pip install Python

218:35

docs. So this brings us to something I want to talk to you guys about, which is

218:39

pip. And pip is essentially a program. And actually, if you have a newer version

218:45

of Python three, it comes pre installed with Python three. And you can use pip

218:50

to install Python modules. It's referred to as a package manager. And a package

218:56

manager basically just allows you to like install, manage, update, and uninstall

219:02

like different Python modules. So pip is extremely useful. And in order for us to

219:08

install Python docs, we're going to have to use pip. So I'm going to show you guys

219:12

how we can do that. What I want to do is open up the command prompt or the

219:17

terminal on my computer. If you're on a Mac, you want to open up your terminal.

219:21

If you're on a Windows computer, you want to open up your command prompts. I'm

219:24

using a Windows right now. So I'm just going to come down to the search bar and

219:28

type in CMD. And this command prompt should come up. So I'm going to click this

219:32

inside of the command prompt, we can actually use pip. The first thing we want to do

219:36

is just check to make sure that pip's installed. And like I said, if you have a

219:40

version of Python three, pip should come just pre installed. And it should just work

219:45

in here. If you don't have a new version of Python three, though, you might have to

219:49

install pip separately. And there's tons of instructions online on how to install

219:54

pip. So I'm just going to check to make sure that I have it, I'm going to type in

219:57

pip hyphen hyphen version. And this should spit out the version of pip that I

220:02

currently have. So as long as we have pip, then we're ready to install an external

220:07

or third party Python module. All I have to do is just say pick install. And now I

220:13

just want to type in the name of the Python module. So in the case of Python

220:17

docs, it was just Python docs like that. Now again, you don't have to install Python

220:23

docs. I'm just doing this for this tutorial. Just kind of show you guys how this is

220:27

going to work. But you know, what you want to do is go online and look up some third

220:32

party external Python modules. And generally, like I'd say 90 to 95% of the

220:37

time, you're going to be able to just install them using pip. In the off case

220:41

that you can't install them using pip, chances are there'll be like some detailed

220:45

installation instructions. But I would say for the most part, any like legitimate

220:49

Python module is going to be able to install using pip. So over here, I'm just

220:54

going to say pip install and then the module name. So Python hyphen docs. And

221:00

when I click enter, this is going to go off and install Python docs for us. So I'm

221:04

just going to enter. And it's going to go off and install everything we need for

221:08

Python docs. So you can see we successfully installed Python docs 0.8.6. So I'm going

221:16

to show you guys where exactly this got placed. So normally, when we install a

221:21

external third party module, it's going to get put inside of this lib folder. But

221:26

it's going to get put inside of a special folder in here called site packages. So

221:31

site packages is a special folder. And if I open this folder, you'll see now we have

221:36

this docs folder and we also have this Python docs 0.8.6 pi 3.6.ag info folder.

221:44

So these two folders are basically included in that module installation. So you can

221:50

see this docs folder has a bunch of different Python files and has a bunch of

221:55

stuff in here that we can use. So if I wanted to use this inside of one of my

221:59

programs, I'm just going to refer to the name of the module. So in our case,

222:03

it's just going to be docs. So I used Python docs in order to install it. But we're

222:08

going to use docs if we want to import it. So I could come up here and I could say

222:13

import DOCX. And now I can actually use it. So I can just say like docs,

222:19

doc, whatever. And you can see there's a bunch of different stuff down here. There's

222:22

like a document document part image part. There's a bunch of stuff that we can use

222:26

with this. And obviously, depending on the Python module you install, there's going

222:30

to be different instructions. But you can see it got stored down here in this

222:34

site packages folder. If I wanted to remove this, I could use PIP to do it. So I could

222:39

just say PIP uninstall. And we could just again say the module name. So Python docs.

222:48

And PIP will now uninstall this on our computer. So how is to go back over to

222:54

this folder? You'll see that those two, that docs folder and then that other

223:00

folder disappeared. So they're no longer here. And I'm actually not going to be able

223:05

to use this anymore. So that's sort of the ins and outs of using modules. Now again,

223:10

there's tons of these modules and I can make dozens and dozens of Python courses

223:15

covering each one of these modules. The built-in modules, the modules that are

223:20

included by default and external modules, there's tons of these things out there.

223:25

And really, as a Python programmer now, what you can do is you can go out and play

223:29

around with these different modules. I showed you the ins and outs of installing

223:33

them and you can use PIP to install all these different modules. And you can make

223:37

sure that you have them by checking the site packages folder or the libs folder.

223:41

But really now it's on you to just kind of go out and use these modules and don't

223:46

shy away from this because modules are a huge part of Python and you're definitely

223:51

going to want to include them in your Python stack.

223:58

In this tutorial, I want to talk to you guys about classes and objects in Python.

224:03

Now classes and objects are extremely useful in Python programming and they can

224:09

help you to make your programs more organized and more powerful.

224:13

So when we're in Python, we're dealing with all types of data, right?

224:17

And a lot of times when we're writing programs, we're going to have to work with

224:21

different types of data. And there's essentially like a few basic types of data

224:25

we can do with usually things like strings, so like plain text, numbers, and

224:30

Boolean values. Those three are kind of like the main types of data that you're

224:34

going to be working with in Python. And we have all sorts of structures we can

224:38

use to store that data, you know, things like lists or dictionaries.

224:42

But here's the problem is that not all information, not all data and not all

224:47

things can be represented using strings, numbers, or Booleans, right?

224:53

There's a lot of things in the real world that we can't represent in something like

224:57

a string or just a number, right? In other words, something like think of like

225:01

something like a phone or a computer or a person, right? You can't really

225:05

represent those things in like a string or a number. You know, in other words,

225:10

like the data types that we have available to us in Python can't cover that.

225:15

And so what we can do with classes and objects is we can essentially create our

225:20

own data types. So I can create my own data type for anything I want in Python.

225:25

So I could create like a phone data type and it could represent a phone.

225:29

So I could store all the information I would ever want to know about my phone

225:33

inside of that data type. And in Python, we could create a class for it.

225:38

And essentially what a class is is it's just saying, hey, here's another data type

225:42

that we want to use in Python. So with a class, you can essentially define your

225:47

own data type. And it's super awesome. And classes are extremely useful.

225:52

Classes are used in almost every single major programming language out there.

225:56

So in this tutorial, I just want to give you guys a basic introduction of classes

226:00

and using them inside of Python. So let's say that I'm writing a program

226:05

and I want to represent a student inside of this program.

226:09

So maybe I'm writing a program for like a college or a university.

226:13

Let's say that in this program, I want to model a student.

226:17

Like I want to model a real world object and I want it to be a student.

226:21

Right, we don't have a student data type and I can't really represent a student

226:25

in just a single string or a number.

226:28

So what I can actually do is I can create a class for a student.

226:33

And I'm basically creating like a student data type.

226:36

So I'm going to show you guys how we can do that and create our student class.

226:40

So over here, I'm just going to make a new file.

226:43

We'll just make it a new Python file. So I'm just going to call it student.py.

226:48

And inside of this student.py file, I want to create a student class.

226:54

So the way that I can do that is just by typing class.

226:58

And now I want to type the name of the class that I want to create.

227:02

So in our case, we're going to create a class called student.

227:04

So I'm just going to say student and now we're going to make a colon.

227:08

Now everything that goes inside of here is going to be inside of our student class.

227:13

So everything that's indented like this will be considered a part of our student class.

227:19

And basically what we can do inside of this student class is we can define a bunch of attributes

227:23

about a student.

227:25

So essentially what I'm doing is I'm like modeling a student.

227:28

I'm creating our student data type. And I can use things like strings,

227:33

integers, and booleans in order to map out what a student should be and what a student should have.

227:40

So I want to create something called an initialize function.

227:43

And the way I do that is just by typing out def.

227:47

And I'm just going to type two underscores.

227:50

And then the word I N I T and then two more underscores.

227:55

And you also want to type out open and close parentheses.

227:58

And inside of those parentheses, we want to type out self.

228:02

And then we want to colon after that.

228:04

What I can do inside of this initialize function is I can basically map out what attributes a student should have.

228:11

So we can essentially define like, hey, here's the student data type in Python.

228:17

So what I want to do is add in certain attributes after this self.

228:21

So I'm just going to say self a comma.

228:23

And now we can start thinking about, you know, what values will represent a student inside of our Python program.

228:30

So if I'm thinking like, I'm thinking something like name, right?

228:34

So every student in our program should have a name.

228:37

They also might want to have a major because they're in college.

228:41

They probably also will have a GPA. So they're a grade point average, like how they're doing in school.

228:48

And let's define one more thing.

228:50

Let me think. Why don't we make a Boolean?

228:53

And it's going to be called is on probation.

228:57

And this will basically tell whether or not the student is on probation.

229:02

So essentially what I'm doing inside of this initialize method inside of this init function is I'm defining what

229:10

a student is in our program. And so in this program, a student has a name, it has a major, it has a GPA,

229:18

and it has a value that determines whether or not it's on probation.

229:21

This right here is the student data type.

229:24

So if I'm representing a student inside of my program now, it has all of these attributes associated to it.

229:31

And that's basically what I'm doing up here.

229:33

Now inside of this initialize function, we actually have to do something. And basically what I want to do is assign some values.

229:42

So I'm going to be writing out some stuff and this might not make total sense right now.

229:47

But this is going to make sense in a second after we create our first student object.

229:52

So just stick with me right now and just basically know that we have to do what I'm doing over here.

229:57

So I'm just going to say self dot name is equal to name. And then I'm going to say self dot major is equal to major.

230:05

Self dot GPA is equal to GPA. And I'm going to say self dot is on probation is equal to is on probation.

230:16

So again, this might not make a whole lot of sense right now, but in a little bit, this is going to make total sense.

230:23

So now that I have this student class defined, I can actually use this class inside of my other file.

230:30

So I'm going to come over here into this app dot Python file. And this is just my main file.

230:36

So I actually want to create a student right so in that student class, we defined the student data type.

230:43

And we basically said like hey, a student has a name, a major GPA and it says whether or not they're on probation.

230:49

That's like the template for what a student is. But we can actually create a student.

230:55

So we can create an actual student and give it some information. And that's called an object.

231:01

So this student class over here is basically defining what a student is.

231:07

So a class is just like an overview of what the student data type is.

231:12

An object is an actual student. So it's an actual student with a name, a major and a GPA.

231:19

It's not just this template anymore. It's actually like a student that we're representing inside of our program.

231:25

So in order to use that student class and create a student object, I actually need to import that.

231:32

So all I have to do over here is just say from student, and this is referring to this student file.

231:40

I can say import student.

231:44

And basically what this is saying is from the student file, I want to import the student class.

231:51

So even though these are both student, they're referring to different things.

231:54

So this is referring to the file and this is referring to the actual student class.

231:59

So now that we did this, we can create a student. So you can create an object of a class a lot like you would a normal variable.

232:06

So I can just give it a name. I'm going to call this student one.

232:10

And I'm just going to set it equal to student.

232:14

And I'm going to make an open and close parentheses.

232:16

And now inside of this parentheses, I want to give this student a name, a major, a GPA, and an is on probation value.

232:27

So I'm going to say, we'll just create a fake student. So I'll just say his name's Jim.

232:32

And he's studying business.

232:36

And we want to give him a GPA. So maybe he has like a 3.1.

232:41

And finally, we want to say whether or not this student is on probation. So why don't we say false?

232:46

So basically what I'm doing is I'm saying that I want to create a student.

232:51

So I want to actually create a student object. And remember, an object is just an instance of a class.

232:58

So the class is like an overall template. It defines what a student is.

233:02

But an object is an actual student with actual information.

233:06

So we could call student one now. This is a student object.

233:11

So I just want to show you guys what this student object actually is and what we can do with it.

233:16

And then I'm going to talk some more about that in it function from before.

233:20

So over here, I'm just going to make a print statement.

233:23

And inside of here, I just want to print out student.

233:27

And what's cool about this student one object is I can actually access each of the attributes from inside of this object.

233:35

So if I wanted to get the name of the student, I could say student one dot name.

233:39

And now this is actually going to print out the student's name.

233:43

So over here, you'll see it prints out Jim.

233:47

If I wanted, I could print out the student's GPA. And it's going to print out the student's GPA 3.1.

233:54

So now that I created this student object, I can actually access the information about the student.

234:01

So essentially, I've just created a student data type.

234:04

So I could create as many of these students as I wanted. If I wanted, I could create another student we could call it student two.

234:11

And essentially, we do the same thing. We just give it different information.

234:15

So we could say like Pam and her major is art. And she has like a 2.5. And let's say that she is on probation.

234:26

So now I have another student, student two. So if I wanted, I could access information about that student.

234:32

We could say like student two dot GPA. And now this is giving me the GPA of student two.

234:38

So basically, what I did was I created a student data type and I created student objects.

234:44

And now I'm able to represent a student inside of my program.

234:48

So let's talk real quick about all this stuff over here because I didn't really explain it.

234:53

Basically, what's happening is when I come over here and I say student and I'm passing in all of these different values,

235:01

those are getting passed into this init function.

235:05

Remember, I passed in a name, I passed in a major, I passed in a GPA.

235:09

When we create that student, we're actually calling this function.

235:14

And when I pass in the name, the major and the GPA, those values are actually getting stored over here in this name,

235:21

this major, this GPA, et cetera. So I'm giving this student object all of that information.

235:29

And down here, what I'm doing is I'm saying self dot name is equal to name.

235:35

And basically what this means is the actual object's name is going to be equal to the name that they passed in.

235:42

So self dot name is an attribute of student.

235:46

So the student is storing a name, the student is storing a major, the student is storing a GPA.

235:51

But that's different from this name, this major, and this GPA.

235:56

Remember, these are all just values that I passed in, they're just parameters.

236:01

And so I need to take the values that I passed in and I need to assign them to the actual attributes of the object.

236:08

So I need to say, okay, the name of the student is going to be equal to the name that we passed in.

236:14

One more time, the name of the student object is going to be equal to the name that we passed in.

236:21

The name of the student's major is going to be equal to the major that we passed in.

236:26

The student's GPA is going to be equal to the GPA that we passed in.

236:31

That's basically what's happening here. So when I say self, it's referring to the actual object.

236:37

So over here, when I'm creating this student object, I'm giving it all of this information.

236:43

It's taking that information and storing it as attributes for the object.

236:48

So hopefully that makes sense. I think that's about as clear as I can make it.

236:52

But the point is that now we have a student data type.

236:56

So now I can represent a student inside of my Python program.

237:01

And that's like super powerful. And what's cool about classes and objects is you can do this with anything.

237:06

So in this example, we created a student class and then we created student objects off of that.

237:12

But I can model any real world entity into this program.

237:16

I can model something like a phone or I can model like a water bottle or I can model, you know, a keyboard, right?

237:23

I can model anything that I wanted inside of my program.

237:27

I could just give it certain attributes, right?

237:29

And so that's what's cool about classes is we can model real world objects and we can create our own data types.

237:40

In this tutorial, I'm going to show you guys how to build a multiple choice quiz in Python.

237:45

So basically, we're going to set up a little multiple choice quiz and we'll have the user take the quiz.

237:50

And as they take the quiz, we'll keep track of their score and then at the end we'll tell them how they did.

237:55

So this is going to be a pretty cool video. I'm excited to show you guys how to do this.

237:59

And we'll get to use things like classes and if statements and loops in order to build this program.

238:05

So we're going to use a lot of cool stuff in Python.

238:08

The first thing I want to show you guys, I actually did this before I started the video. It's this little array. It's called question prompts.

238:15

And I basically just wrote out all the questions that are going to be or I shouldn't say the questions.

238:20

I wrote out all the question prompts that are going to be inside of my multiple choice quiz.

238:25

So the first one just says what color are apples and the answers are red, purple or orange.

238:32

The next one is what color bananas the options are teal, magenta and yellow.

238:38

And the third question is what color are strawberries and the options are yellow, red and blue.

238:43

So this is obviously a very, very easy multiple choice quiz.

238:46

But I just kind of wrote out the questions beforehand so I didn't have to spend time doing that.

238:51

So let's talk about how we can build a multiple choice test.

238:56

Well, the first thing I want to think about is how can I represent the questions in the test.

239:00

Over here, we have the question prompts, but there's actually two parts to a question when you think about it.

239:07

There's the prompts. In other words, the actual question itself.

239:11

And then there's the answer to the question and both of those attributes need to be kept track of.

239:17

I need to keep track of what we want to ask and I need to keep track of what the answer is.

239:21

So what I'm actually going to do is create a question class.

239:25

So I'm going to create a little data type for questions and inside of that question class,

239:30

we'll be able to store the question prompts and we'll also be able to store the questions answer.

239:36

So I'm just going to come over here to my folder and I'm going to make a new Python file and we're just going to call this question.py.

239:45

So we'll call it question.py and inside of this question.py file, I'm going to make a class.

239:51

So I'm just going to say class question and I'm going to put a colon here.

239:56

So inside of this question class, we want to define it in initialize function.

240:01

So I'm going to say def two underscores in it, two underscores, and then we're going to type out an open and close parentheses self and then a colon.

240:11

So after this self, we want to define the different attributes that will describe or that will be included in a question.

240:19

So we're going to include a prompt and we're going to include an answer.

240:24

So every question will have a question prompt and a question answer.

240:28

Now down here, I want to take those values and assign them to the actual class object.

240:34

So I'm going to say self dot prompt is equal to prompt and I'm going to say self dot answer is equal to answer.

240:43

Now we have a question class set up and we can store all the information we need about a question.

240:50

So what I want to do now is go back over to my app dot Python file.

240:54

And we're actually going to create an array of questions.

240:57

So I have over here these three prompts and I'm actually just going to create another array.

241:02

So we'll just call it questions and I'm just going to set it equal to to open and close square brackets and we're going to start creating question objects.

241:12

Let's say the first element in this questions array will be a question and we want to pass question prompts zero and the answer to the first question was a red green.

241:27

That's the color of Apple.

241:28

So I'm going to pass that first question and I'm also going to pass the answer, which is a and you'll see down here, I'm getting an error.

241:36

So saying unresolved reference question. So actually what we need to do before we can create these questions is we need to import this question class.

241:45

So I'm going to come up in here, come up here and say from question import question.

241:52

So now down here in this questions array, we're not getting that error anymore.

241:56

So we created one question object and I'm just going to create a couple more for these other ones.

242:02

So I'm just going to copy this one and we will paste this down here.

242:10

Alright, so now we're creating two other questions and these ones are going to be one and two and the answer to the second question is C and the answer to the third question.

242:21

I think it's B. Let me look.

242:25

Yeah, so it's C and B. Alright, so essentially what we're doing is we're creating three questions. Each one is getting a different question prompt and they're each getting different answers.

242:35

So now we have an array of questions that we want to ask on our test.

242:39

So the next step is to actually write a function that will run the test, right?

242:44

It has to ask the user the questions and it has to check to see if they got the answer right.

242:49

Down here, lower in the file, let's create a function. So I'm just going to say death and we'll call this run test.

242:57

And inside of this parentheses, we're going to take one parameter into this function.

243:02

We're going to take a list of questions. So I'm just going to say questions.

243:07

So this is basically going to be a list of question objects that we want to ask the user.

243:12

So once I'm in here, all I want to do is loop through all the questions.

243:16

So I want to loop through each question. I want to ask it to the user.

243:20

I want to get the user's answer and I want to check to see if it's right.

243:24

And we need to be able to keep track of how the user does through the test.

243:28

So I want to create a variable called score and I'm just going to set it equal to zero.

243:33

So every time the user answers a question right, we'll increment this score variable.

243:38

Alright, so what I want to do is, like I said, loop through all the questions in that questions array.

243:42

So I'm going to create a for loop. I'm going to say for question in questions.

243:49

So in other words, for each question object inside of this questions array, I want to do something.

243:55

And basically, the first thing I want to do is ask the user the question.

244:00

And I want to store their response inside of a variable.

244:03

So I'm going to say, I'm going to make a variable down here called answer.

244:07

And this is going to represent the user's answer to the question.

244:12

And I'm going to set it equal to input and I need to give this a prompt.

244:17

So I'm actually going to give this the prompt for the question.

244:20

So I can just say question dot prompt.

244:24

So now I have the answer that the user entered in, which is stored inside this variable.

244:29

So we want to check to see if they got the question right.

244:32

And I can do that using an if statement.

244:34

Let's say if answer, and this refers to the student's answer, is equal to question dot answer.

244:42

So I'm checking to see if the answer that the student gave is equal to the answer of the current question that we're asking.

244:49

And if this is true, then I just want to increment score.

244:53

So I'm going to say score plus equals one.

244:56

And this basically just means we're adding one to the score.

244:59

So the last thing I want to do inside of this function is just print out the result.

245:04

So I want to print out how the user did.

245:06

So down here, I'm just going to say print and I'm basically just going to print out like,

245:10

Hey, you got like two out of three questions or you got five out of six questions, right?

245:14

Or you got zero out of 10 questions, right?

245:16

We'll just print out how they did.

245:18

So I'll just say you got.

245:20

And now I want to print out how many questions they got right.

245:24

So this is just going to be the score.

245:26

And in order to print a number alongside of a string like this, I'm going to have to say STR and then type in the number.

245:33

So we'll say STR score.

245:35

So we'll basically say you got however many they got right out of.

245:39

So we'll use this little forward slash.

245:41

And now we'll just say how many questions there were.

245:45

So we can say, and again, we're going to have to convert this to a string because it's going to be a number.

245:49

So we can say, string, and then we want to figure out how many questions were in that questions array. So I can say LEN.

245:56

And inside of here, we can just say questions.

245:58

So this is kind of like this long statement just to print out how many questions there were.

246:02

So then over here, I'll just say, correct.

246:05

All right.

246:06

So now we have this print statement.

246:08

And it's just going to be printing out how many questions they got right.

246:12

So let's see if this works.

246:14

The last thing I need to do is just call this run test function.

246:19

So I'll say run test, and I'm just going to pass it that questions array that we created before.

246:24

So we created this questions array with all of these question objects inside of it.

246:29

I'm basically just going to pass that into this run test function.

246:33

So now let's run our program and see how we did.

246:36

Hopefully we don't get any errors.

246:38

There's a lot of code there, so it's possible that we might.

246:42

Okay, so it says what color are apples?

246:44

Huh, this is a tough one.

246:45

All right, let's put A. So we'll get the first question right.

246:48

What color are bananas?

246:49

Let's get the second question wrong.

246:51

So I'm going to say B, bananas are magenta.

246:54

And we'll get the third question right.

246:56

So it says what color are strawberries?

246:58

I'll just put B because that's the right answer.

247:01

So now you'll see that our program prints out.

247:04

You got two out of three correct.

247:06

So not only did it ask us all the questions and get our input for each question,

247:10

but it also kept track of our score and it printed it out.

247:14

So why don't we run through the test one more time and we'll just like get too wrong this time.

247:20

So we'll get this one wrong.

247:21

I'll get this one wrong and I'll get this one right.

247:23

So now it says you got one out of three correct.

247:26

So it's able to actually grade our test.

247:28

And what's cool about the program we just wrote is if I wanted,

247:32

I could add another question into here and it would automatically be able to ask it.

247:37

So it doesn't matter how many questions we put in here.

247:40

It's just able to ask it.

247:42

So really the point I was trying to make with this whole video was just how we can use something like a class

247:48

in order to model a real world entity.

247:51

So for example, we created this question class and we were able to store all the information about a question that we could ever need.

247:59

So we were able to sort of like the prompts and the answer.

248:02

So we created our own question data type and then we were able to create a bunch of different questions,

248:08

pass it into this run test method and it was able to grade the test and just run the test really easily.

248:14

So that's kind of the point.

248:16

So hopefully this makes sense.

248:18

You know, this was a lot of code.

248:19

So if you don't fully understand what I did, go back, watch the video, you know,

248:23

try it a couple of times on your own and see if you can create your own multiple choice test.

248:28

In this tutorial, I want to talk to you guys about class functions in Python.

248:36

So a class function is essentially a function that we can use inside of a class and it can either modify the objects of that class

248:45

or it can give us specific information about those objects.

248:48

Over here, I have this student.py file and inside of here, I created a student class.

248:55

So I just had a class student and I gave this student a couple of different attributes.

248:59

I gave it a name, a major and a GPA.

249:03

So I can give this student all of this information.

249:06

So when I create a student object, it'll have all of that information in it.

249:10

Now over here on this app.py file, it actually created two students.

249:15

So student one is Oscar who is studying accounting and he has a GPA of 3.1.

249:21

Student two is Phyllis who's studying business and she has a 3.8.

249:26

So what we can actually do is we can use functions inside of these class files.

249:32

So I could actually define a function inside of this student class and then all of my student objects could access it.

249:40

So for example, I can come down here and create a function and let's say we wanted to create a function that told us whether or not this particular student had honors.

249:50

So whether or not they were on the honor roll, right?

249:53

Basically like, are they like a really good student?

249:56

So I could define a function called on honor roll.

250:01

And basically what it's going to do, it's going to tell me whether or not this particular student is on the honor roll.

250:09

And let's basically just say the rules for being on honor roll is you have to have a GPA of 3.5 or above.

250:16

So here GPA is 3.5 or above, that means you're on the honor roll.

250:20

Well, over here I could actually write the code for that.

250:23

So I could say if self.gpa, and when I say self over here, this is referring to like the actual student's GPA.

250:33

And you'll notice over here I have to pass that in as a parameter and that always has to be the first parameter.

250:39

So I could say if self.gpa is greater than or equal to 3.5, then we can return true because this student is on the honor roll.

250:52

Otherwise else we can just return false because if their GPA isn't above a 3.5 then they're not on the honor roll.

251:03

So that's essentially our little on honor roll function. And you can see it's a very small function, but it provides a service to the objects of this class.

251:12

It allows the objects of this class to figure out whether or not that current student is on the honor roll.

251:18

So over in this app.pa file I could just print out that value.

251:24

So I could say print and I'm going to see if student one is on the honor roll.

251:32

So now when I run this you'll see it should print out false.

251:37

And it's putting out false down here because student one only has a GPA of 3.1.

251:43

If I was to do the same thing on student two, student two has a GPA of 3.8 so we should return true.

251:51

And that's exactly what happened.

251:53

So essentially a class function is just a little function that can be used by the objects of the class.

251:59

And I would say this is a pretty good example of what a class function should be doing.

252:04

It should be like giving us information about the class or it can also be like modifying information about the class.

252:10

So that's the basics of using functions in classes.

252:12

I mean this is a pretty simple tutorial, but I really just wanted to introduce you guys to the concept of putting functions inside of classes.

252:21

This is going to be really useful.

252:22

And if you're making a class you always want to think about what functions can I put inside of here that will essentially help the user to either figure out information about the object or modify different values in the object.

252:40

In this tutorial I want to talk to you guys about inheritance in Python.

252:44

Now inheritance is basically where we can define a bunch of attributes and functions and things inside of a class.

252:51

And then we can create another class and we can inherit all of those attributes.

252:56

So I can basically have one class that has all the functionality of another class without having to physically write out any of the same methods or attributes.

253:06

So if that's confusing don't worry I'm going to show you guys exactly what this is in this video.

253:11

So let me show you about some stuff I have set up over here.

253:15

I created this class called chef. And over here we just have this class chef.

253:21

And inside of this chef class we have three functions.

253:24

So this chef can do three things.

253:26

The chef can make chicken.

253:28

The chef can make salad and the chef can make a special dish.

253:33

And whenever the chef makes a dish he basically just says the chef makes a chicken or the chef makes a salad or the chef makes barbecue ribs for this special dish.

253:44

It's basically just saying whatever the chef is doing.

253:48

So if I was to come over here to my app.python file you'll notice that I'm importing the chef.

253:54

So I'm basically allowed to use this chef class now.

253:58

And I can say my chef is equal to chef.

254:01

So I'm creating a new chef.

254:02

So now if I came down here and I said my chef dot make chicken.

254:09

When I run my program now it's going to say the chef makes a chicken.

254:14

If I said make special dish and I run the program now it's going to say the chef makes barbecue ribs because that's the chef's special dish.

254:26

So I have this really awesome chef class and it works really well.

254:29

But let's say that I wanted to create another class to model another type of chef.

254:35

So this chef class is just modeling like some generic chef.

254:39

We're representing some generic chef in our program.

254:42

But let's say that I wanted to create a class that modeled a different type of chef.

254:47

For example let's say we wanted to create a class that modeled a Chinese chef.

254:51

So instead of just a normal chef this is a Chinese chef.

254:55

Well I actually created a file over here called Chinese chef dot python.

254:59

So we could actually use this Chinese chef dot python file to create our Chinese chef class.

255:06

So I'm just going to say class and over here I'm just going to say Chinese chef.

255:11

And basically I just can define everything that the Chinese chef can do.

255:17

But let's say that our Chinese chef can do everything that our generic chef can do.

255:23

So the Chinese chef is like a special chef right it's a very specific type of chef.

255:29

But let's say that this Chinese chef can do everything that the normal chef can do.

255:33

So the Chinese chef can make chicken, can make salad and can also make a special dish.

255:38

Well if I wanted to give this Chinese chef all of that functionality I could just come over here and I could copy all of these functions and I could paste them in here.

255:49

So now the Chinese chef can do everything that the other chef can do.

255:54

But let's say that in addition to all of these things the Chinese chef can also make a special dish called fried rice.

256:01

So I could say deaf and we could say make fried rice.

256:06

So the Chinese chef has this extra function which allows it to make fried rice and over here we can just say the chef makes fried rice.

256:16

And let's also say that the Chinese chef has a different special dish.

256:20

So instead of making barbecue ribs the Chinese chef makes orange chicken.

256:25

Alright so now we have our Chinese chef class set up and you'll notice that it can do everything that the normal chef can do.

256:33

So let's go back over to our app.python file and I want to create a Chinese chef object.

256:39

So instead of importing from chef we're going to just import it from Chinese chef so I'll say Chinese chef and we're going to import Chinese chef.

256:49

So let's go ahead and make a Chinese chef object.

256:53

So down here I'm going to make one and I'll just say my and then my Chinese chef we can print out this special dish.

257:02

So remember the Chinese chef can do everything that the normal chef could do.

257:06

So if I say my Chinese chef make special dish when I run this program you'll see the regular chef is making barbecue ribs and the Chinese chef is making orange chicken.

257:18

So everything works out and this Chinese chef also has an extra method called make fried rice and you can also make fried rice.

257:28

But here's the problem right when I go over here to this Chinese chef dot pie when I wanted to use all of the functionality that was inside this chef class.

257:38

I actually had to copy and physically paste all of these functions down into this file right and that's kind of a drag.

257:46

I mean especially like imagine if this chef had like 20 or 30 of these different functions in it.

257:52

Well I have to copy and I have to paste all of those inside of this Chinese chef and this is where we can use something called inheritance.

257:59

So instead of having to copy and paste all of these functions I could just inherit these functions from that chef class.

258:07

And in order to do that I can actually just get rid of all of these and I can come over here and right next to where I'm saying class Chinese chef.

258:16

Inside of parentheses I can say the name of the class that I want to inherit from so I can just say chef.

258:22

And I'm also going to have to import this so I'm going to come over here and we'll say from chef import chef.

258:32

So basically what this is saying is inside of this Chinese chef I want to be able to use all of the functions that are contained inside of the chef class.

258:42

So I'll say that one more time. Inside of this Chinese chef class I want to be able to use all of the functions that are inside of this chef class.

258:50

So by using inheritance here I'm actually able to come over here to this app.py file and now I'm still able to execute for example like the make chicken function.

259:02

Even though I didn't specify the make chicken function in here I didn't write it out. I can still run this program and the Chinese chef will still be able to make chicken.

259:15

So when I run this program you'll see down here it says the chef makes chicken.

259:20

And that's because I inherited the make chicken method from the chef class.

259:25

But here's one thing that got messed up. If I come over here and I say make special dish.

259:32

Remember the Chinese chef special dish was supposed to be orange chicken.

259:36

But now the Chinese chef is going to be making barbecue ribs because I inherited this make special dish function from this chef class.

259:47

And so what I can actually do is I could come in here and I could override that make special dish class. So I could say death make special dish.

259:58

And down here I can just override it. So I could say the chef makes orange chicken.

260:06

And now when I go over here and I run this make special dish function.

260:12

Now the chef's going to be back to making orange chicken. So the Chinese chef will make his special dish.

260:18

So that's basically what inheritance does. It allows me to inherit functionality from an existing class into the new class.

260:26

And I can actually just inherit all of it without having to write it out.

260:31

So I didn't have to write out make chicken and make salad inside the Chinese class.

260:36

But the Chinese chef can still make chicken and salad because it inherited that functionality from the chef class.

260:46

In this tutorial I want to talk to you guys about the Python interpreter.

260:50

The Python interpreter is basically a little environment that we can use to execute Python commands.

260:58

So it's kind of like this little sandbox environment where we can test out and try out different Python commands or different Python functions in a very safe and neutral environment.

261:08

So it's kind of like a quick and dirty way to write Python and try different things out.

261:13

And the way we use the Python interpreter is by opening up our command prompt.

261:18

Now if you're on Windows there's a program called command prompt.

261:22

If you're on Mac there's a program called terminal. And basically you need to open that program in order to use the Python interpreter.

261:30

So I'm here on a Mac but if you're on a Windows you can essentially do exactly what I'm doing just inside of the command prompt.

261:37

So on Mac here I'm just going to search for my terminal.

261:41

If you're on Windows you can just search for like CMD or command prompt and it should pop up.

261:46

So the terminal is basically an environment on our computer where we can interact with the computer and do different things without a graphical user interface.

261:57

So normally when we interact with the computer we're using things like buttons or windows or we can move stuff around.

262:05

We can basically just like interact with the computer with graphics but the terminal or the command prompt is a place where we can interact with the computer using text commands.

262:16

So you know I'm not going to get too much into what the terminal and the command prompt are but that's essentially what it is.

262:21

So inside of here we can use the Python interpreter.

262:26

And the Python interpreter is basically just a little program that like I said we can write you know sort of Python in.

262:33

So generally all you have to do is go over to your terminal or your command prompt and type in Python 3.

262:40

And since we're using Python 3 I'm going to type Python 3 but you can also just type normal Python and I'm pretty sure that gives you Python 2.

262:49

Now here's a quick disclaimer on Windows you may run into a problem where you can't use this Python 3 command and it's possible that if you're running into that problem Python.

263:02

Like Python 3 hasn't been added to your windows path variable.

263:07

And basically what that means is your computer doesn't recognize the command Python 3.

263:13

If that's the case all you have to do is just go and Google how to add Python 3 to your windows path variable.

263:19

It's really easy I'm not going to get into it in this video just because it's kind of outside the scope but if you're running into that problem that's probably it's not on your windows path variable.

263:28

So just Google around for that and you should be able to find an answer.

263:32

But I'm going to type Python 3 and click enter and it's going to open up this cool little thing down here.

263:37

So you can see it basically just has like here's the version of Python.

263:41

Now I'm just going to enter so we can have some like new lines here.

263:46

But down here this is the Python interpreter so we can write Python code in here and it's going to work.

263:52

So I can for example I could write like print and inside of here I could print like hello world.

263:58

And when I click enter you'll see that it prints out hello world.

264:02

I could create a variable so for example I could say like num one is equal to ten.

264:07

I could say num two is equal to ninety and then I could print out num one plus num two and it's going to be able to do that for me.

264:16

I could also use some like a function so I could define a function in here.

264:22

We could make a function that's called say hi and it'll take a name parameter.

264:27

And you'll see here when it saw I wanted to create a function it put these three little dots and I can indent and we can write the code for our function.

264:36

So I can say like print hello plus name.

264:44

And now I have a function called say hi so I can break out of this by just entering again and I can call the function so I could say say hi and we'll say hi to me.

264:57

Hi Mike so now it's going to say hello Mike so I can use a function I could use something like an if statement you know I could say I could use something like a for loop we could use while loops.

265:08

I mean we can basically use all of the basic like Python commands and you know inside of this Python interpreter and like I said before it's essentially just an environment where you can test out Python code.

265:20

Now this is not a place where you want to write like any serious Python scripts.

265:25

So if you're like designing and writing a Python program I would not recommend doing it inside of this interpreter.

265:31

It is it's not a very intuitive environment it's pretty much just set up for some quick and dirty tests like we need to test something out great this is a perfect place for it because you don't have to set up a file.

265:42

You don't have to use like some ID you don't have to execute a file you just go in here it's quick it's dirty you get it done.

265:48

But if you're writing like an actual program you definitely definitely definitely want to use a text editor.

265:55

The text editor it's just going to be way more organized everything is going to be a lot cleaner and you're not going to get confused with like where stuff is and what you have so that's the basics of the Python interpreter.

266:06

You know in the course I haven't really been using it at all just because I think it's a lot easier to teach when we're inside of a file than inside of here but you know don't count this guy out the Python interpreter is awesome a lot of people use it and it's great if you just

266:20

need to test you know little bits of code out without having to set up some huge environment so Python interpreter definitely awesome I would play around with it you know have some fun and take advantage of it.

266:31

Hey thanks for watching if you enjoyed the video please leave a like and subscribe to draft Academy to be the first to know when we release new content.

266:37

Also we're always looking to improve so if you have any constructive criticism or questions or anything leave a comment below.

266:44

Finally if you're enjoying draft Academy and you want to help us grow head over to draft Academy.com forward slash contribute and invest in our future.

UNLOCK MORE

Sign up free to access premium features

INTERACTIVE VIEWER

Watch the video with synced subtitles, adjustable overlay, and full playback control.

SIGN UP FREE TO UNLOCK

AI SUMMARY

Get an instant AI-generated summary of the video content, key points, and takeaways.

SIGN UP FREE TO UNLOCK

TRANSLATE

Translate the transcript to 100+ languages with one click. Download in any format.

SIGN UP FREE TO UNLOCK

MIND MAP

Visualize the transcript as an interactive mind map. Understand structure at a glance.

SIGN UP FREE TO UNLOCK

CHAT WITH TRANSCRIPT

Ask questions about the video content. Get answers powered by AI directly from the transcript.

SIGN UP FREE TO UNLOCK

GET MORE FROM YOUR TRANSCRIPTS

Sign up for free and unlock interactive viewer, AI summaries, translations, mind maps, and more. No credit card required.