python教程修订版

9/23

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

Now, Python is one of the most popular programming languages out there在众多的……中.

And it's by far目前为止 one of the most sõught after受欢迎的 for jobs.

And so if you're trying to get a job or you're trying to ãutömāte自动化 your life or you're trying to write awesome scripts to do a bunch of一堆 different things, then Python's for you.

Honestly, more and more developers every day are moving their projects over to Python because it's such a powerful and it's such an easy to use language.

A lot of programming languages out there just aren't very beginner friendly.

There's a lot of sỳntax.

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

Python is the complete opposite of that.

You basically just type out what you want to do and Python does it.

It's that simple.

There's not a whole lot of大量的 syntax to learn.

The learning cûrve曲线 is litërälly字面意义的 zero.

You jump in, you can start writing your first program in seconds.

In this course, I'm going to teach you guys everything you need to know to get started in Python.

I designed this course, especially for Python.

And each lesson has been specially designed with examples that will help you along the way.

with so many people starting to learn Python.

The question isn't why should you learn Python? The question is, why shouldn't you?

And I think for a lot of people the reason they might not want to learn Python is because they're intimidāted害怕担心 or they're afraid that it's going to be too hard.

Trust me, I am going to hold your hand through this èntīre全部的 course.

We're going to talk about all the cõre concepts核心概念 in Python.

We're going to look at everything you need to know to start programming in Python and start being confïdënt自信的 and start writing scripts and start writing programs that are awesome and doing cool things in your life.

Anyway, I'm super pûmped to迫不及待 be teaching you guys Python.

I can't wait to get started in this course.

And I hope you guys stick around逗留 and follow along with the course and learn this amazing programming language.

In this tūtõríäl教程, I'm going to show you guys how to install Python onto your computer.

And we're also going to install a text editör文本编辑器 that we can use to write our Python programs in.

So the first order of business首要任务 is to actually install Python on your computer.

So what we want to do is head over前往 to our web browsër浏览器.

And you want to go over here to this page, it's just www.python.org forward slash正斜杠/ downloads.

And on this page, there's going to be two buttons down here.

And it's just going to say download Python 3.6.3.

And at least that's the version that I have right now.

or download Python 2.7.1.4.

So here's the thing about Python, there's actually two mājör主要的 versions of Python that are in use cûrrëntly目前.

There's Python two, and there's Python three.

So you can see over here, this is like 2.7.1.4.

Basically, any Python two version we would just refer to as like Python two.

And here's like the latest Python three version.

And so there's these two versions that you can download.

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

9/24

And here's the basic difference:  
Python two is a legäcy遗留 version of Python,  
which basically means that it's like an old version,  
and it's not being like actively积极 maintained维护 or süppõrted提供帮助 like öfficiälly官方的,正式的 by Python anymore.  
Python three is like the future of Python.  
It's like the newest version.  
It's the one that's getting actively maintained and supported.  
And so there's prōs优点 and cõns缺点 to picking either one.  
A lot of people will say like Python two has been around longer.  
And so there's more like libraries,  
there's basically just more Python code written in Python two.  
So therefore, like there's a few more things that you can do in Python two than you can do in Python three.  
But then again另一方面, Python two is not supported.  
And you know, five or 10 years from now, probably no one's going to be using it.  
Python three, like I said, is like the future of Python.  
It's the newest version.  
It's the one that, you know, is sort of有点算是 being maintained going forward今后.  
So for the pürpöses of 为了this tutorial, I'm going to be teaching you guys Python three.  
So we're going to want to download Python three.  
Now here's the thin·g, like, if you learn Python three or you learn Python two, the differences aren't very big.  
There are a few differences in like some syntax and just some, you know, little things here and there.  
But if you learn Python three, you'll be able to jump right in and code in Python two as well.  
Don't let anyone, you know, tell you that you have to learn one or the other;  
we're going to learn Python three just because it's I think probably the best for a beginner to learn.  
And so that's the one that we want to download.  
So I'm just going to click点击 download Python three.  
And we're going to go ahead and download this version.  
And when that's done downloading, I'm just going to head down to my downloads fōldër文件夹.  
And you'll see here, we just have this file,  
I'm just going to double click.  
And we get this like Python installer.  
So I'm just going to click through here.  
And we'll be able to install Python three on our computer.  
All right, when that's done installing, we can just close out of this,  
and we should be good to go准备好了.  
So now we have Python three installed on our computer.  
The next step is we need to choose a text editor.  
So we're going to be writing a bunch of code.  
So we're going to need some, you know, ènvīrönmënt环境 or some program that we can write all that code in.  
And really, you can write Python in any text editor;  
you could write it in like notepad or text edit, it doesn't really matter.  
But there are special text editors that are designed just for writing Python code.  
And these are called IDEs.  
And that stands for...的缩写 intègrāted集成的 development environment.集成开发环境  
It's basically just a special environment where we can, you know, run and exècúte执行 our Python code.  
And it'll, you know, basically like tell us how we're doing.  
So if we're, you know, if we write something that's wrong or we have errors,  
it'll kind of point us in the right dïrection方向、指引 of what we need to do to fix that stuff.  
So in this course, we're going to be using an IDE.  
And one of my favorites, and one of the most popular IDEs for Python, is called pychårm.  
So I'm over here on this website, it's called https://www.jetbrains.com/pycharm/.  
And this is the IDE that we're going to be using in this course.  
So I'm just going to come over here and click this big download now button.  
And we'll go ahead and download this pycharm program.  
Alright, so I'm getting this page over here.  
You can see there's two versions of pycharm.  
One is a pröfessiönäl专业 version, and that one's like paid, so you have to pay for it.  
But then there's this cömmúnity社区 version down here, and that's free and open sõurce资源.  
So I'm just going to download this one.  
And this should have everything we need to get started using Python.  
Alright, when that finishes downloading, I'm just going to pull that up 打开in my downloads folder.  
And again, we can just sort of run this guy.  
And I'm on a Mac.  
So I'm going to have to drag拖 it over to my applications folder.  
Alright, so we now have pycharm.  
And we have Python three installed on our computer.  
So we're ready to start programming in Python.  
And this tutorial, we're going to create our first Python program.  
And we're going to be able to run it and see how everything works.  
The first thing I want to do is open up pycharm.  
So pycharm was the IDE, the integrated development environment that we downloaded in the last tutorial.  
So I'm just going to go ahead and search for it on my computer.  
And I can just open it up.  
And when we first open up pycharm, we should basically just get a window prompting引导 us to create a project.  
So you can see that down here.  
Actually, the first thing I want to do though is come over here to cönfig[y]üre.  
And I'm just going to click on preferënces偏好.  
And I actually want to change the äppēaränce外观.  
So I'm going to go ahead and give this a thēme主题.  
So I just went over here to appearance and bèhāvíör行为, appearance.  
And I'm going to change the theme to dark.  
Just because I think it's a little bit easier to use and look at.  
But you don't have to do that if you don't want to.  
And so down here, I'm just going to click create new project.  
And it's going to give us this little window right here.  
So I can just name my project.  
And I'm just going to name this draft草稿.  
And then there's this other option选项 down here where it says intërprètër解释器.  
Now, what we want to select is Python version three.  
So if I click down here, you'll notice that there's a couple different options.  
So I'm on a Mac computer.  
And by dèfãult默认, Mac has Python version two installed on it.  
If you're on a Windows mächine机器, I'm not sure if it does.  
But in this tutorial, we're going to be using Python three.  
So you want to make sure that you have Python three selected as the interpreter.  
Other-wise否则, you might not be able to fully follow along with what we're doing in this video.  
So now I'm just going to come down here and click create.  
And this is going to go ahead and create our project for us.  
All right, so now we have our Python project up and running.  
And I want to show you guys how we can create our first Python program.  
So over here in this draft folder, and this is just that project that we created,  
I'm just going to right-click and I'm going to say new.  
And I'm going to come down and click Python file.  
So we want to create a new Python file; this will be our first Python program.  
And I can just give this a name.  
So why don't we just call this app?  
And I'm just going to click okay.  
All right, so now we have our first Python file open.  
And we can just start typing in some Python.  
So I want to show you guys just a very basic Python program.  
And this is just going to be a hello world program.  
So we're just going to print something out onto the scrēen屏幕.  
So what I want to do is I want to type out print.  
And I want to type in open and closed pärenthësēs园括号.  
Now inside of this parentheses, we can make some quōtātion引用 marks.  
And inside of the quotation marks, I'm just going to type out hello world.  
So this is like a very basic Python program.  
And what this is going to do is it's just going to print out hello world onto the screen.  
And so now what I can do is I can actually run this file,  
and we're actually getting a little error here.  
It wants us to create a new line at the end of the file.  
And so now, in order to为了 run our Python program,  
I'm just going to come up here to the top.  
And I'm just going to click run.  
And we'll click this run button right here.  
And now we'll see what happens.  
So it's asking me what I want to run.  
I can just click app.  
And down here at the bottom, you'll notice that...,  
that just got printed out, hello world.  
And so whatever任何 I print, or whatever I put inside of this print stātëmënt指令,  
is going to get printed out down below in the cönsole控制台.  
So as long as只要 you're getting that print statement to work,  
then you've actually written your first Python program.  
And as we go through forward 继续in the course,  we're going to be writing all sorts of awesome Python programs.  
And we're going to be doing all sorts of cool stuff. In this tutorial, I want to talk to you about writing a basic Python program.  
So we're going to write just a very simple Python program.  
We're going to look at how we can go ahead and write our programs,  
we're going to talk about how our programs are getting executed by Python.  
And we're also going to drãw画 out a little shape形状 onto the screen.  
So this is going to be pretty cool.  
I'm excited to talk to you guys about just the báre basics最基本 of Python,  
and really just sort of give you an intrödûction介绍.  
So over here, we have our basic text editor.  
And so any of the Python code that I write inside of this file  
is actually going to get executed by Python.  
So this Python file has a bunch of Python code inside of it.  
And basically what we're doing when we're programming in Python  
is we're just giving the computer a set of一组 instrûctions命令.  
Right, that's really what programming is;  
it's just like we're specifýing指定 a set of instructions.  
And the computer is going to go off运行 and execute those instructions that we give it.  
And so the more complex复杂 the instructions we give to the computer,  
the more complex the tasks任务 the computer can carry out.  
So I want to show you guys how we can use some Python instructions  
or some Python code in order to draw out a shape onto the screen.  
So I'm actually going to draw out a little trīangle三角形.  
So over here, we can actually print something out onto the screen in Python  
using something called print statements.  
So I can just type out print.  
And inside of these parentheses, I can just type some quotation marks,  
and inside the quotation marks, we can put whatever we want to print out onto the screen.  
So I'm going to copy this and I'm actually going to make a few of these little print statements.  
And we're going to actually draw a shape.  
So we're going to draw like a triangle type shape.  
So down here, I'm just going to draw a forward slash.  
And then up here, I'll make a space空格 and we'll do another forward slash.  
Here, we'll make two spaces, we'll do another forward slash.  
And over here, we're going to make three spaces and a forward slash.  
And now I'm going to draw like vërticäl垂直 bars going all the way down.

9/25

And actually we can do some under-scõres下划线here as well. That'd be kind of cool.

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

So I can save my file. And I can either come up here to run and click run app. Or if you're using PyCharm you can also just come over here and click this play button.

And when I click the play button I want you to notice what happens down here.

So down here we have this little window and this is called the console.

And basically the console is just a place where Python is going to output some information.

So when we use this print statement we're basically telling Python like, Hey, can you print something out onto the console?

So there's a lot of situations where we're going to want to see what's going on in our code.

And we can use that print statement and print things out to the console. And we can basically like have a little window into what our program is doing.

In our case, we're just going to be printing out this triangle onto the console.

So I'm going to come over here and click this play button. And you'll see down here, we're actually printing out that triangle.

So mission任务 äccömplished完成, we were able to print out our shape. So I could really draw any shape I wanted as long as I was able to specify it inside of these print statements.

So èssentiälly本质上, what Python's doing when I click that play button is it's going into this file and it's looking at all of these instructions in order.

So the first thing it does is it goes to this instruction, and it says okay, they want me to draw out these like little forward slash in this vertical bar onto the screen.

Once it does that, it goes down to this instruction, it says okay, they want me to print something out again.

When it prints that out, it goes down to this instruction, etc.

So Python is actually going to exècute执行the lines of code that we write in order.

So the order in which I write the instructions matters a lot.

So for example, if I was to take this last line here, and put it up here at the top.

Now when I save the file instead of just drawing out that triangle, it's going to draw out this little up-side down bottom thing at the top and then the rest of the triangle down here.

So the order of the instructions actually matters a lot. So this is basically how your Python programs are going to go.

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

So here, we're just using this little print function, basically, it just prints something out onto the screen.

There's a bunch of little things like this that we can learn as we go through, we'll learn more and more instructions that we can give to the computer.

But in this lesson, I just want to kind of show you guys the basics, right?

We can essentially just dèfine定义 these little instructions for the computer and the computer will execute them in order.

In this tutorial, I want to talk to you guys about using váríäbles变量 in Python. Now in Python, you're going to be dealing with a lot of data.

So a lot of times in our program, we're going to be working with all types of information and data and values. And sometimes that data can be difficült to manage.

So in Python, we have this thing called a variable. And it's basically just a container, where we can store cêrtäin肯定 data values. And when we use a variable, when we put those data values inside containers, it makes it a lot easier for us to work with and manage all of the different data inside of our programs.

So variables are extreēmely极其 helpful. And it's sort of like a cõre核心topic in Python that you're really going to want to know in master.

So in this tutorial, I'm going to teach you guys everything you need to know to get started with variables, we're going to look at what they are, why they're useful, and we'll look at the different types of variables and the different types of data that we can store inside of variables.

But over here in my Python file, I just have a very simple program.

And it's basically just printing some text out onto the screen. And so this is actually a little story that I wrote.

It says there once was a man named George, he was 70 years old. He really liked the name George but didn't like being 70.

So this is a valid有效Python program. I could go ahead and run this program. And you'll see down here in the console, everything prints out and it looks great.

So we have an awesome little Python program here. But let's say that inside of our story, I wanted to change the charäctër's角色name. So instead of naming the character George, let's say I wanted to name the character John.

Well, I'm going to have to go through and I'm going to have to manúälly手动的 change the name George to the name John at every place inside of this story where it's mentioned. So I'm going to have to go over here and I'll say okay, John, I'm going to have to manually change that.

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

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

And now let's say okay, maybe we want to change the character's name age also.

So in äddition加法 to 除此之外changing the name, we also want to change the age, let's make them a little bit younger.

So why don't we say John is going to be 35. And so you'll see here I had to come here and manually change the age and then come down here and manually change it again.

Right. And now that we changed it, like it's going to work, it's going to be updated in our program. But you'll notice that in order to make that change, like I had to look through the entire program, find it where that value was and change it.

And this was only with four lines of the story.

Imagine if I had a story that was like thousands of lines long. And we mentioned the character's name and age, like hundreds of times. I mean, I'd have to look through each one of those lines and manually change the character's name.

And that is not a very good way for us to do this. And it's really not a good way for us to manage the data in our program, right? The character's name and the age.

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

And when we use that variable, it'll make it a lot easier for us to put the character's name and age inside of our little program here.

So I'm going to show you guys how we can create a variable for the character's name and age. And I'm just going to go up here äbôve this print statement.

9/27

And up here, I want to create a variable.

So when we create a variable in Python, we actually need to give Python a couple pieces of information.

The first piece of information we need to give Python is the name of the variable.

So I need to actually ässīgn指定 a name to this container where we're going to be storing information.

And so all I have to do is just type out the name of the variable that I want to create.

So I'm going to create a variable called character name.

So I'm just going to say character.

And I'm going to say underscore name.

And so genërälly通常, when you're creating a name for a variable in Python, you want to separäte隔开 different words with an underscore.

So I have two words here, and I'm separating them with an underscore.

And now what I need to do is I need to put a value inside of this variable.

So basically what I can do is I can just say ēquäls等于 and over here, I can type a value.

So I'm just going to type out the character's name inside of quotation marks.

So we're just going to type out john.

So now we have a variable for the character's name.

And below this character name variable, I want to create another variable called character age.

So we're just going to type out character underscore age.

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

So we now have two variables, one reprèsenting代表 the character's name and one representing the character's age.

So what I can do now is I can rèplace替换 the character's name inside of the story with this variable.

And I'm going to show you guys how to do that.

So if we want to put a variable inside of this print statement, I'm actually going to have to do a couple of things.

The first thing 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 here at the end.

And you can see I'm basically wrapping包裹 this whole thing into a single单个 like quoted line.

And now I also have this text over here at the end.

So I'm going to have to put a quotation mark here.

So now I have a bunch of text here in quotation marks.

And then I also have text over here in quotation marks.

Now inside of here, instead of saying the character's name, I want to refer to引用 that character name variable.

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

And I want to say another plus sign.

So I basically am saying I want to print out all of this text plus the value that's stored inside of character name, plus all of this text.

And what this will do is actually print out the character's name.

We print this.

So I'm going to go ahead and run this program.

And you'll see down here, and actually let me put a space over here.

And we'll run it again.

So you can see down here, we still are printing out there once was a man named John.

And so what Python is doing is when it executes this line of code, it's coming over here and it's seeing, okay, they want to print out the value inside of character name.

So it Python is going to go up here, it's going to get the value.

And it's going to insêrt插入 it inside of there.

So I can basically do this for every instänce例子 of the character name in my file.

So I can, you know, I'll just copy this guy right here.

And I can just pāste粘贴 it down here where the character's name is.

So I'll paste this.

And you can see now we're doing exactly the same thing that we did up there.

I can also do this for the character's age.

So I'm going to come down here and I'll paste in what we just paste it in.

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

And I'm going to do this in one more spot地点.

So we're going to do this over here where we're mentioning the age.

And again, I'll just change this to character age.

And so now when I run this program, it's going to print out the same story that we were printing out before.

But now I don't actually have to type in the character's name and the character's age.

I can just refer to this variable.

And the cool thing about variables is if I wanted to change the character's name now, all I have to do is change it up here.

So I could change the character's name to like Tom or something.

And we could also change their age.

So I could change the age to be like 50.

And now that's going to automatically update throughout our entire story.

So when I click this play button, you'll see now we're using the name Tom.

And he's 50 years old.

So that's where variables can come in handy方便使用.

And this can be a really awesome way to cöntrōl and manage the data that's inside of our programs.

With variables, you can also modifý修改 their values.

So for example, let's say that halfway through the story, I wanted to change the character's name.

Well, I can just make some new lines over here.

And down here, I can actually assign a new value to one of these variables.

So I could say like character underscore name.

And in order to give it a different value, all I have to do is just say equals, and we'll give it a different value.

So let's say halfway through the story, we want to change the character's name to Mike.

Well, now when I run on my program, you'll see here in the first part of the story, it says there once was a man named Tom.

And in the second part of the story, it's referring to the name as Mike.

So I actually updated the variable inside of my program over here.

And that's a really awesome way.

You know, a lot of times in these programs, you're going to want to be modifying the values of variables.

So you can do it just like that.

So finally, I want to talk to you guys about the different types of data that we can store inside of these variables.

So over here, I have the character name and the character age.

And I'm actually storing what are called strings.

So a string is basically just plāin完全的 text, right?

So the name Tom, that's just plain text.

Down here, these guys are strings as well.

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

So you know, any text that you would have like in a story.

9/28

or you know someone's name you can store inside of a string.

There's also another type of data that we can store inside of Python which is numbers.

So in addition to storing text we can also store numbers.

So if I wanted to store a number for example let's say we want to store the character age instead of inside of a string

we could store it inside of a number.

So I could just type out 50.

And when we're storing a number we don't need these quotation marks you only need that quotation mark when you're storing a string.

So if I want to store a number I can just write it out like this.

And in addition to using whole numbers I could also use decimäl小数 numbers.

So I could say like 50.5678213.

And Python's going to be able to store that number as well.

So you can store all different types of numbers.

And we can also store what's called a Bóolēän布尔 value.

And a Boolean value is essentially必需 a true or a false value.

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

For example I could have a variable called like is māil邮件.

And this would tell me whether or not是否 someone was a male.

And in my case it would be true because I'm a guy right?

So I can actually this needs to be capitäl大写.

So I could sort of value of either true or false.

And actually if we were naming this in Python we want to use an underscore.

So I would say is underscore mail.

And this can be either true or false.

So true or false values is maybe not something that you're used to dealing with like in the real world.

But in programming we're going to be using true or false values all the time.

They're super important.

So like I said there's three basic types of data that we can work with in Python.

There's strings which is just like plain text numbers which would be like either decimal numbers or whole

numbers like this.

And there's true or false values.

And these are the basic types of data.

I mean there's a bunch of other types of data that we can use.

But I would say 99% of the time as a new user and a new programmer to Python you're just going to be dealing with these three types of data.

So those are the basics of the data and also with variables.

And you're going to be using variables all the time in Python.

So you definitely want to practice up and get comfortable using them.

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

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

And strings are basically just plain text.

So any text that we want to have inside of our program we can store inside of a string.

So I want to talk to you guys about all the cool things we can do with strings.

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

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

So I'm just going to say print and inside of these parentheses I can type out a string.

In order to create a string I need to use quotation marks.

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

Now inside of the quotation mark I can put whatever text I want the string to have.

So we could say like draft äcadëmy研究.

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

So if I run my program now down here we're going to print out draft Academy as you can see.

And when we have these strings there's actually a bunch of cool things that we can do with them.

So one thing I could do would be to create a new line inside of the string.

So if I want it I can come over here and I could say backslash反斜线 n.

And you can see it got hīghlīghted突出 in a different color.

And what this is going to do is it's actually going to insert a new line into the string.

So now it's going to say draft on one line and Academy on another line.

So I'm going to click play. And you'll see down here we just get draft Academy.

In addition to the backslash n I could also use a backslash quotation mark.

So if I wanted to put a quotation mark inside of my string I can't just put a quotation mark like that because Python is going to think that I'm trying to end this string.

So if I want to include a quotation mark I can just use this special backslash character.

And that's called the èscape character转义字符.

And it basically just tells Python that whatever character comes after it we want to rendër译成 literally.

So when I say backslash quotation mark it basically means like Hey Python I want to print out a quotation mark.

And so now we'll be able to print out a quotation mark right there.

I can also use this to print out a backslash.

So if I needed to print out a backslash I could say backslash.

And it'll just print out a normal backslash now.

So you can see just like that.

So if you want you can use that backslash to make new lines or print out quotation marks or you can just use it as a normal backslash.

In addition to just typing out a string here I could also create a string variable.

So I could come down here up here and we can create a variable and we'll just call it phrase.

And I'm going to set it equal to draft academy.

So I can store this string value inside of a variable called phrase.

And then when I want to print out that variable or I want to access取得 that string variable I can just type the name of the variable.

And you'll see that it's going to print out the value that was stored inside of it.

I can also use something called cöncatënātion串联.

And concatenation is basically the process过程 of taking a string and äppending添加 another string onto it.

So I could come over here and I could say phrase and I can say plus.

And now I can add in another string.

So I could say like is cool.

And now this is going to say draft academy is cool.

So I'm basically appending another string onto another one they call that concatenation.

And so in addition to doing all that stuff we can also use special things called fûnctions函数.

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

And it will përfõrm执行 a specific opërātion操作 for us.

So we can use functions to modify our strings.

And we can also use functions to get information about our strings.

So I'm going to show you guys a couple common functions we can use with these strings.

And they're actually going to do awesome stuff.

So in order to access one of these functions I can just say phrase and then I can say dot点.

And I'm going to show you guys a function that we can use to cönvêrt转变 this string entirely into lower-case小写.

So I could just say phrase dot lower.

And I'm going to want to type an open and close parentheses.

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

So you can see now we just have draft academy down here but it's entirely in lowercase.

I could also do the same thing for uppercase写.

So I could say phrase dot upper.

And this will convert the entire string into uppercase.

You can see now it's entirely uppercase.

So in addition to converting the string into uppercase and lowercase I could also check to see if a string is entirely uppercase or entirely lowercase.

So for example I could say phrase dot is upper.

And this is going to give me back a true or a false value.

So it's going to be true.

If the string is entirely uppercase or false if it's not.

And you can see here we're getting a false value because this is an uppercase.

I can also use these functions in combinātion组合 with each other.

So for example I could say phrase dot upper and then parentheses.

And then after this I could say dot is upper.

And now what it's going to do is it's going to run this upper function it's going to convert it into uppercase.

And then it's going to run this is upper function right after that.

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

So you can see now we're getting a true value.

So you can use these functions one after another it can be really useful.

And there's a few other ones that I want to show you.

So we can also figure out the length长度 of this string.

So if I wanted to figure out how many characters were inside of this string I could just say LEN.

And I'm actually going to make an open parentheses.

And I'm going to make a closed parentheses.

So I'm essentially saying LEN.

And this is another function it's the length function.

And inside of this LEN function I'm actually passing传入 this phrase variable I'm basically giving the length function this and it's going to spit out吐出 a number.

So it'll tell me how many characters are inside of this string.

And so you can see here we get 15 because there's 15 characters inside of draft academy.

So that's how we can get the length of a string.

And that's going to be really useful as we go forward and Python we can also get individūäl 单独的characters inside of a string.

So imagine if I wanted to just grab one of these characters like imagine if I wanted to figure out what the first character in this string is, we can actually use an open and closed squárebrackèt括号 just like that.

And in here I can specify the index索引 of the character that I want to grab.

So if I wanted to grab the first character in this string I can put in a zero.

So if I say phrase square bracket zero this is going to give me that capitäl首字母 G.

And you can see down here that's what gets printed out.

Now I want to point something out in Python when we're working with strings a string gets indexed starting with zero.

So you'll notice that in order to access this G I had to put a zero in here.

And that's because in Python when we use indexes on a string we start with zero.

So it's 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 in the string i is at position one r is at position two a is at position three etc.

So we start counting at zero.

So if I wanted to access the first character in the string this G I have to say phrase zero and that's just a special thing in Python and actually most programming languages do that.

10/14

So they'll start with zero.  

So for example, if I wanted to access this a, I'm going to have to put zero 123.  

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

As you can see, we get an a right here.  

So this is actually really useful, being able to grab a specific character inside of a string.  

And you're going to be using that a lot as we go forward in Python.  

There's also another really awesome function that we can use, and it's called the index function.  

And basically, what the index function will do is it'll tell us where a specific character or string is located inside of our string.  

So I could say phrase dot index.  

And I can actually give this a value.  

So sometimes when we use these functions in Python, we can actually give them information.  

So I can give this a value.  

And we would call this passing a päramëtër参数.  

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

So for example, I could say inside of here, capital G.  

And now this is going to return the index of the capital G inside of our string.  

So it should give us back a zero because G is at the zero of index.  

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 inside of here, like a lowercase a, it should give me 0123 because that's where the first a is inside of this string.  

So I can click the play button.  

And you'll see it's giving us a three.  

You can also put actual words in here.  

So for example, I could put like Academy in here, or I could even just put like a C A D.  

And this is going to tell me where this starts inside of my string.  

So when I click play, it's going to give me an eight because that's where Academy starts, right 012345678.  

If I put something in here that wasn't in the string, so for example, if I put a Z in here that's not in here, it's actually going to throw an error.  

So when I play this, you'll see that we get this error down here because Z is not found inside of our program.  

So that index function again can be really useful, and we'll be using that a lot.  

There's also one more that I want to show you, which is called replace.  

So we can say phrase dot replace.  

And in here, I can actually give this two parameters.  

So I can give this replace function two values that it can use.  

And the first thing I want to give it is what I want to replace.  

So for example, I could say draft.  

And then I'm going to put a commä逗号 in here.  

And I want to put in here what I want to replace draft with.  

So I could just say like elephant.  

And now instead of saying draft Academy, this is going to print out elephant Academy.  

So you can see down here, it just says elephant Academy.  

So this replace function can be really awesome because we can replace certain words or even certain letters inside of our strings with other ones.  

So those are just some basic ways that we can work with strings inside of Python.  

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

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

But if you just do a Google search, you can find all sorts of Python functions that you can try out and use, and you know, see what they do.  

But you definitely want to get comfortable working with strings in Python because you're going to be working with them a lot.  

In this tutorial, I want to talk to you about working with numbers in Python.  

Numbers are one of the most common data types in Python, and any Python program you write is most likely going to be dealing with numbers at some point.  

So I want to talk to you guys about just the basics of using numbers.  

We're going to talk about the different types of numbers that we can represent in Python.  

And I also want to show you guys some awesome functions that we can use with numbers.  

So we can do certain like mathëmaticäl数学上的 opërātions运算, we can do some awesome things with numbers.  

I'm going to show you guys all of that in this video. It's going to be awesome.  

So first thing we can do is we can actually just like print out a number.  

So if I want to write a number in Python, it's really easy.  

I basically just write the number.  

So I could say like print two.  

And this is just going to print the number two out onto the screen.  

So I can use a whole number like this.  

I could also use a decimal number.  

So I could say like 2.0987.  

And Python is going to be just fine with that.  

We'll be able to print that out under the screen as well.  

I can also make numbers negätive负数.  

So I could make this 2.0987 a negative number.  

And Python will be able to handle that just fine too.  

In Python, we can also use basic arithmetic.  

So I could do things like äddition加法, sübtraction减法, mûltïplïcātion乘法.  

So I could say like three plus four.  

And now this is going to print this out.  

Or why don't we say like three plus 4.5.  

And this will be able to print out the answer.  

So we get 7.5.  

And we can do addition, we can do subtraction, we can do division除法, or we could do like multiplication as well.  

So you can pretty much do whatever you want inside of Python.  

And if you want to make more complex mathematical èquātions方程式, we can also use things to specify order of operations.  

So I could use parentheses.  

So for example, if I said three times four plus five, what Python's going to do is it's going to mûltiplý相乘 three and four together.  

So it's going to be 12.  

And then we'll add five to that.  

So we should get 17.  

But let's say that we wanted to change up the order, right?  

I could actually put a parentheses around four and five.  

And now it's going to add them first.  

So it'll add four and five.  

So we'll get nine and we'll multiply that times three.  

So we should end up with得到 27.  

So you can see we can use those parentheses to specify order of operations.  

So I could make like a very complex little equation here just by using numbers and parentheses.  

We can also use one other operation, which is called the modülûs系数百分比% opërātör运算符.  

So I could say like 10 and use this përcent百分比 sign three.  

And this is actually read 10 mod three.  

And basically what this is going to do is it's going to take the first number divided by the second number, and it's going to spit out the rèmāinder余数.  

So it's basically going to say, okay, 10 divided by three, that's going to be three with a remainder of one.  

And so this is going to give us the remainder.  

And you'll see down here, we get one because that's the remainder of 10 divided by three.  

And that modulus operator can come in handy派上用场 quite a bit.  

We can also store these numbers inside of variables.  

So we can store them inside of these variable containers.  

So I could come over here, and I could create a variable called like my underscore num.  

And we'll just store a value of let's say like five in here.  

And so I could come down here and I can just print out my underscore num.  

And we'll be able to print that number out to the screen just fine. 

So in addition to doing things like basic ärithmëtic算术, we can also do some other stuff.  

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

So if I wanted to convert this number into a string, I can just say str, and I can put the number that I want to convert inside of these parentheses.  

And so what's going to happen now is this number is going to be converted into a string.  

So when I click the run button, you'll see it's just printing out five, but now instead of being a number, this is actually a string.  

And this is going to come in handy when you want to print out numbers alongside与...一起 strings.  

So I can come over here and I can say like my favorite number, and we'll be able to print this number along with this string.  

So I can click play.  

And it says five, my favorite number.  

If I was to get rid of this string right here and I wasn't converting the number into a string, now Python is going to have a problem with this.  

It's not going to allow us to do that.  

You can see we get an error.  

So anytime you want to print out a number next to a string, you got to make sure that you use that little string function.  

So there's a bunch of other stuff that we can do with numbers.  

And a lot of times in Python, you're going to want to be using specific like more ädvanced高级 math operators.  

So there's a bunch of different math functions that we can use on our numbers.  

And a function is basically just like a little cöllection集合 of code that does something.  

So a function could perform an operation like a mathematical operation on our number.  

It could also give us information about our number.  

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

The first one is called ABS and it stands for absölūte value绝对值.

So we can get the absolute value of a number.

And so over here I'm just going to make this variable negative five.

And basically I can just say ABS and I can make an open and closed parentheses over here.

And this is going to give me the absolute value of this number up here.

So when I click the play button you'll see it's just giving us five because five is the absolute value of negative five.

I can use a few other functions.

There's another one which is called pow.

It's so it's just POW.

And I'm actually not going to use this variable.

We just use a normal number.

And this function is going to allow us to pass it two pieces of information.

So I can give this function two pieces of information.

The first is going to be like a number.

And the second is going to be the power that I want to take that number to.

So I could say like three comma two.

And this is basically just going to be three rāised to the power of 次方two.

So it's just going to be three squáred平方.

So we should get nine down here.

You can see that we do.

So I could pass in like you know some crazy number like we could pass in a four and we could raise it to the power of six.

And so we should get like a pretty big number here.

And you can see we get 4096.

So this pow function is really useful for taking numbers to specific powers.

So in addition to using this pow function we can also use another function which is called max.

So I can say max.

And what this is going to do is it's basically going to return the larger of the two numbers that we pass into it.

So right now I'm giving this a four and a six.

And this should tell us which number is higher.

So over here it's just printing out six because that's the bigger number.

I can also use another function called min.

And this is going to do the opposite.

So now instead of printing out the max number it's going to print out the smallest number.

And it's going to print out four down here as you can see another cool function is called the round function.

And this is going to allow us to round a number.

So it's just going to follow like standärd标准的 rounding rules.

So if I said like 3.2 inside of here now it's going to round it down to just normal three.

But if I said like 3.7 it's going to round it up to four.

So that'll allow you to round a number.

So there's a few other functions that I want to show you guys but in order to get access to them I'm actually going to have to do something called importing输入.

And in Python we can actually import èxtërnäl外部的 code into our files.

So if I want to access these specific math functions I have to import something called Python math.

So I can just say from up here math import and this star.

And basically what this is going to do is it's going to go out and it's going to grab a bunch of different math functions that we can use.

So I already showed you guys a few of these different math functions but there's actually a bunch more.

And in order to access them we're going to need to include this line of code inside of our file.

So once we have that now I can access a few others.

So there's another function which is called the floor methöd方法.

And it's just FLOR.

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

So it's essentially just going to chop off切去this decimal point.

And you can see over here now we get a three because we're using that floor function there's also another one called cēiling.

And that's just going to do the exact opposite.

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

So no matter what we'll always get four with we have a three point whatever here.

And there's another one which is called square root平方根.

So it's just SQRT.

And essentially this is just going to return the square root of a number.

So I could say like 36.

And now we should get six back.

So those are awesome very interesting functions.

And inside of this math module so we would call this a modūle模块.

And you don't have to worry too much about what that is right now.

Just know that when we put this line of code into our program it gives us access to能够使用 a lot more math function.

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

And there's a bunch of different math functions in Python.

And if you want you can basically just go online and search for different math functions.

I showed you guys a few here.

And I would say the ones that we looked at here are probably the most commonly used but there's a bunch more.

And like I said you know there's tons of documentation on all this stuff.

So you can just look up you know math functions in Python.

And there's going to be tons and tons like lists of these things that you can use inside of your programs to perform different math operations.

But for now that's just been a little bit about using numbers a little bit about doing you know certain math operations and using functions with those numbers.

In this tutorial I'm going to show you how to get input from a user.

So we're basically going to allow a user to input information into our program.

And I'm actually going to take the information that the user inputs I'm going to store it inside of a variable.

And then we're going to be able to do something with that variable.

So I want to show you guys how we can actually go about getting input from someone.

And down here in my Python file all I have to do to get input from someone is I just have to type out input and then an open and closed parentheses.

And what this is basically going to do is it's going to tell Python like hey we want to get input from a user and Python will allow the user to type in some information.

And inside of these parentheses we can actually type a prompt提示.

So generally when we want the user to enter in some information we want to tell them what we want them to enter.

And that's what we can put inside of here.

So I'm just going to say enter your name.

So we're actually going to create a little Python app which is going to allow the user to enter in their name.

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

So we'll say like hi and then whatever the name they entered was.

So what I want to do is I actually want to store whatever the user inputted into our program inside of a variable.

So I can actually go over here and I can create a variable I could just call it name.

And I could set this variable equal to input.

So what I'm doing now is I'm taking the value that the user inputs and I'm storing it inside of this variable container called name.

And so now I have access to whatever they entered in.

So what we can do is we can say print.

And inside of the parentheses I can just print out hello.

And then I'm just going to add on to this name.

And actually why don't we put an exclamation point?

So essentially we're getting a name from the user we're storing it inside of the name variable.

And I'm printing it out onto the screen.

So I'm going to go ahead and play this.

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

So it's saying enter your name.

Now in here I can actually type in a value.

So I can type in Mike.

And then I can just hit enter.

And Python's going to go ahead store this value inside of that variable.

And then it's going to print out hello Mike.

And so this is kind of cool.

And actually what we could do is we could prompt the user to enter in more than one piece of information.

So I could actually copy this line of code.

And I'm going to paste it below here.

And why don't we have the user enter in another variable?

So we could have them enter in for example their age.

And over here we just want to say enter your age.

And so now we're able to get two pieces of information from the user.

And I could say hello name.

And then I can say you are and I'll print out their age.

So I'm just going to print age.

So now I'm basically able to get both of these pieces of information from the user I can get their name.

And I can get their age.

So when we run this program I can type in both of those pieces of information.

So to say answer your name.

So we could just make up a name like john and then enter the age let's say john is 25.

And now it's going to say Hello john you are 25.

10/21

So the program is able to get input from the user.

And then it's able to use that information in order to say hi to the user and tell them how old they are.

And so getting input from the user is a great way to make your programs more inter-active.

And here we're just scratching the sürfàce接触到表面 right?

We're just basically getting to you know a piece of information from the user but you can use these inputs to do awesome things in Python.

And as we go through this course you're going to see some really cool things that we can do with these inputs.

In this tutorial I'm going to show you how to build a very basic calcúlātör计算器 in Python we're basically going to build a calculator where we'll get two numbers from a user and then we'll add those numbers together and print the answer out onto the screen.

So let's look at some practice in getting input from users.

And we'll also talk about getting numbers from users as öppōsed to 不同于,不只是just strings.

So I'm really excited to get started on this calculator.

The first thing we want to do is create two variables and inside of those variables we want to store the two numbers that the user wants to add together.

So down here I'm just going to say num one.

And that's what we'll call our first variable is equal to.

And now I need to get input from the user.

So I'm going to say input.

And inside these parentheses I'm going to say enter a number.

And now I basically want to do the same exact thing but for the second number.

So we'll paste this guy down here.

And I'm just going to say num two.

And then we'll just say enter another number.

So now we're able to store the user's input inside of these two variables.

So now all I want to do is just print out the result.

So I'm actually going to create a variable called rsûlt结果.

So I'm just going to say result is equal to and basically we just want to add these two numbers together.

So I'm going to say num one plus num two.

And now all we have to do is just print out the result.

So I'm just going to print result.

So we basically have our entire program written out here.

And this will allow us to enter in two numbers and then we'll add them together and print out the answer.

So I'm going to click play.

And down here we can enter our number.

So I'm going to enter the first number we'll say five.

And I'm going to enter my second number.

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 that's not quite the answer we were looking for right five plus 8.3 is definitely not 58.3.

And here's what happened.

When we get input from a user by default Python is just going to convert it into a string.

So it doesn't matter what we put down there when we're entering the numbers Python's always just going to be like okay this is a string that's it.

Sounds good right?

It's just going to make it a string.

In order for us to make these num one and num two variables numbers we're actually going to have to convert the strings that we get from the user into numbers.

And so the way that we can do that is by basically just coming over here.

And when we add the two numbers together I can use a special Python function in order to convert those strings into numbers.

And there's actually two Python functions that we could use for this.

The first is called int.

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

And basically what this is going to do is it's going to convert whatever is inside of these open and closed parentheses into an intègër number整数.

And an integer number is basically a whole number.

So it's like one two three four five.

So it's a whole number.

In other words it can't have a decimäl point小数点.

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

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

In other words into whole numbers that don't have decimals.

And it's going to add them together and we'll print out the results.

So why don't we do that?

And we'll just see if this works.

So I'm going to click the play button.

And down here we'll enter our first number.

So I'm just going to enter in a four.

And then I'll enter in a five.

And now you can see it's printing out the correct answer four plus five.

And so that's essentially our basic calculator.

But we do have one problem with this calculator.

So I'm going to run it again.

And I'll show you guys with that problem is if I wanted to add in a decimal number so we'll add 4.3 and 5.5.

You'll see that we're getting this error.

And that's because I'm using this int function up here.

And this int function is looking for a whole number.

So if I put decimal numbers inside of there it's going to break the program.

There's another function we can use instead of an int we can say flōat浮动.

And a float is basically just a number that has decimal.

So it's a decimal number.

So I can convert both of these guys into floats.

And now I'll be able to use decimal numbers.

So I can click play.

And I can just enter in some numbers.

So I could say like four.

And I could also say like 5.5.

And now it's going to be able to add both of those numbers even if one of them is a decimal.

And that's the beauty of using that little float function.

So in certain cïrcûmstances在某些情况下 you might want the user to only be able to enter in an integer to a whole number.

But in a lot of cases like in this calculator we want them to we want them to be able to enter any number that they want.

So we're going to use this float function.

So I'm just going to rēcap回顾 what we did one more time we created two variables num one and num two.

And we basically stored the two numbers that the users input into those variables.

And then we converted them into numbers.

And we added them together and we printed out the results.

So this is a pretty cool calculator.

I'm happy with it.

Hopefully you guys learned something.

And as we go forward in this Python course you're going to learn all sorts of ways that we can make this calculator more complex.

In this tutorial I want to show you guys how to build a madlibs game in Python.

So a madlibs game is basically just a game where you can enter in a bunch of randöm随机 words.

So like you know verbs nouns names colors.

And you basically take all of those words and put them into a story randomly.

So over here my web browser I just have an example of a madlibs.

So you know down here we're basically just taking random words like an adjective or a noun or a plūräl复数 noun and entering them in side of a story.

So like you read through the story and then like some random word pops up弹出 and madlibs can be pretty fun.

And I'm sure you guys have you know at least heard of madlibs before.

But if not that was just a little introduction.

So I want to create a madlibs in Python.

And this is going to be really cool.

So down here I have this like little pōèm诗 which just says rōses玫瑰 are red vīölèts紫罗兰 are blue I love you.

And so you know this is kind of like a classic poem.

But I think we should mix it up a little bit and create a madlib for this poem.

And so instead of having this red color over here I want to have the user enter in their own color.

Instead of having violets why don't we have them enter in a plural noun?

And instead of saying I love you why don't we have them say I love and then some celebrity?

So celebrity.

So this is the basic structure for our little madlib right?

I want to basically have the user enter in a color enter in a plural noun and enter in a cëlebrïty名人.

And then I'll take all of those words and put them inside of our madlib.

And hopefully we end up with something that's pretty funny.

So how can we actually go about building this in Python?

Well the first thing we're going to have to do is we're going to have to be able to get these words from the user right?

That's pretty obvíöus显而易见.

So I think what we should do is we should create three variables called color plural noun and celebrity.

And we'll store the user's inputs inside of those variables.

So up here let's create those variables.

The first variable I want to create is going to be called color.

So say color.

And I want to set this equal to the input.

So I'm going to say input.

And inside of here I'm just going to give them a prompt.

So I'll say enter a color.

And we can basically just copy this line of code.

And I'm going to paste it down below.

And we'll paste one more time.

So now we're going to create our plural noun variable.

So I'll say PL URL plural noun.

And we'll just say enter a plural noun.

And actually over here I want to make a cōlön冒号 as well.

So then finally we'll create our celebrity variable and we'll say enter a celebrity.

And so now basically we're creating three variables called color plural noun and celebrity.

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

So last thing we have to do is replace these guys with those variables.

So I'm basically just going to add in the color over here.

And we're going to add in the plural noun.

So I can just go like this.

And finally we'll just add in that celebrity.

So now we have a pretty cool mad lib.

And it should be functional起到作用 .

Everything should work.

It looks like it's good to go.

So let's come over here and we'll run our program.

And then down here in the console we can play our little mad libs game.

So let's say enter a color I'm just going to say mägentä品红 enter a plural noun.

Why don't we say microwaves and enter a celebrity let's say Tom Hanks.

So when I click enter what should happen is we should get our mad libs printed out.

So I'm going to click enter.

And now we have our mad libs.

So it says roses are magenta microwaves are blue.

I love Tom Hanks.

So it's a pretty good mad lib.

And obviously you know the more the more print statements we put over here you know the better the mad lib could could be.

And we could store as many you know variables up here as we wanted.

This was just a very simple mad lib.

We only took in three inputs but you know you can see how you could take something like this and create a little mad libs game.

So your homework for this lesson is to go off and make your own mad libs game.

Hopefully one that's a little bit funnier than mine.

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

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

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

And when you're dealing with large amounts of data you want to make sure that you can manage it and õrgänīze组织 it properly.

And the list is essentially just a structure that we can use inside of Python to store lists of information.

So we can take a bunch of different data values we can put them inside a list and it allows us to organize them and keep track of管理,跟踪 them a lot easier.

So generally you would create a Python list and you would put a bunch of related values inside of that list and then you can use it through-out在...之间 your program.

So I'm going to show you guys just the basics of using lists we're going to look at some of the common use cases and really just get you up to speed with what lists are and why they're useful.

So over here in my program I'm actually going to create a list.

And we create a list a lot like we create a Python just like a normal Python variable.

So the first thing we want to do when we're making a list is give it a name.

So we want to give it a dèscriptive解释的 name something that's going to basically describe what's inside the list.

In my case for this example let's make a list of like friends.

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

So I can say friends.

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

And we want to create a list we can use these open and closed square brackets.

So whenever you use these open and closed square brackets Python basically knows okay they want to store a bunch of values inside of this list.

So why don't we go ahead and start adding some values?

So we can say like my friends are like Kevin Karën and let's say Jim.

So here I have three values inside of my friends ärrāy阵列.

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

So I would create a variable I would give it one value maybe a string or a number or a boolean.

But when we make lists we're able to store mûltiple多样的 values inside of the same like little item inside of the same object right?

And then what I can do is I can access these individual items inside of my program.

So inside of this friends list we're basically storing all of these different values and that can be really useful.

So now that we've stored some values let's talk about what you can put inside of a list.

So you can really put any piece of information in Python we could put things like strings numbers or booleans.

So if I wanted instead of just putting strings here I could change like one of these to a number and I could change one of these to a boolean and it wouldn't matter Python is going to be fine with that.

But let's change these back to strings just for the sake of 为了...的缘故the example.

So now that we've created our list how can we access individual elëmënts基础{元素} inside of this list?

It's a good question.

I'm gonna go ahead and print something out to the screen.

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

So I can just say print friends.

And then when I run this program if we look down here in the console you can see it's just printing out the actual list of friends right?

But let's say that I wanted to print out a specific element inside of this list.

Well I can actually refer to elements by their index.

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

And the index is actually start at zero.

So we would say that this first element in the list Kevin has an index of zero.

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

And this third element Jim would have an index of two.

So even though Jim is technicälly严格意义上 the third element inside of the list it has an index of two.

And that's because we start indexing index position one.

And so that's just kind of how lists work.

And that's how list indexes work.

So if I wanted to access a specific element inside of my friends list I can just refer to it by its index.

So over here after we type out friends I'm just gonna type an open and closed square bracket.

And inside of this open and closed square bracket I want to put the index of the element inside the list that I want to access.

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

So I can say friends zero.

10/22

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

If I wanted to access this Jim value over here, I can put a two.

And that will give us that Jim value, you can see right there. So we can access elements based off of their index, we can also access elements based off of their index from the back of the list.

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

And you can see that sort of does so when we use negatives, it starts indexing from the back of the list.

So if I said negative two, this is gonna give us Karen. And so you can use the negatives to access items at the back of the list.

Just keep in mind though, and this is kind of interesting.

We would say this first element here is index position zero, right?

But we would say the first element at the back of the list is index position negative one.

So just keep that in mind. So there's actually other ways that we can access like just prtions of部分 this list.

So for example, let's say I just wanted to select the last two elements in this list.

And I didn't want to select the first one. Well, in here, I can just say one with a cōlön冒号.

And this will grab the element at index position one and all of the elements after that. So now we should just grab Jim and Karen, or Karen and Jim in that order.

I could also specify a range. So actually let me add in a couple other elements just so we can illüstrāte阐明 this better.

So I'll add in two more names. So if I wanted to, I could select like the element at index position one up to the element at index position three. So it's going to grab this one.

So that's one, two. And it's actually not going to grab the third one. So it's it's going to grab all the elements up to but not including three.

So it'll grab Karen and Jim. So let's go ahead and run this. And you can see it grabs Karen and Jim. So you can either just put like one with a colon after it, and then it'll grab that index position and everything after it, or you can specify like a range that you want to grab from.

So that's how we can access elements inside of this list. But we can also modify elements. So for example, I could come down here, let's say I want to modify this value right here, Karen.

Well, all I have to do is just access that element. So I could say friends and Karen is at index position one. And I can change this to whatever I want.

So I could say friends index position one is equal to Mike. And now when we print out friends index position one, that value is going to be updated. So it's just going to say Mike instead of saying Karen.

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

So those are kind of the basics of working with lists in Python. And actually in the next tutorial, I'm going to show you guys even more ways that we can use these lists, we're going to learn how to like add elements to a list, dèlēte删除 elements from a list, copy lists, and a bunch of cool stuff with these lists.

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

In this tutorial, I want to talk to you guys about using functions with lists in Python. And Python, a list is one of the most important structures where we can store our information.

A list basically allows you to take a bunch of different values and organize them and store them inside of their own little list structure.

So down here in my program, you can see I have two lists. I have a list of lucky numbers. And this is just a bunch of different numbers. And then I have a list of friends. So this could be like a list of somebody's friends. And it's basically just a bunch of strings with names in them.

So these two lists are storing a bunch of different values. And what makes lists awesome is not only could I store like this many values, I mean, I could store hundreds or thousands or millions of values.

And when you're working with a lot of data and a lot of information in Python, these lists are invalúäble非常宝贵的.

So because lists are so important, there's actually a bunch of different functions that we can use with lists. And those functions allow us to do things like modify the lists and get information about the list.

So in this tutorial, I just want to show you a bunch of those functions, we're going to talk about some of the most common and the most popular functions that you would use on lists to make them more powerful and make them more easy to use.

So the first thing we can do is obviously just print out a list. So down here I'm printing out this friends list. And when I want to run my program, you see down here we're just printing out all the elements in the list.

So it's always good to be able to print out all the elements inside of a list. But there's a lot of other functions that we can use.

So one function is called the èxtend扩展 function. And the extend function will basically allow you to take a list and äppend附加 another list onto the end of it.

So I could say like friends dot extend. And inside of these parentheses, I can pass in the list that I want to add on to the friends list.

So I could say like lucky numbers. And now when I print out this friends list, not only is it going to have all the elements in the friends list, it's also going to have all the elements inside the lucky numbers list.

You can see down here, we have all of our friends. And then we also have the numbers over here.

So that's a really easy way to essentially just like add two lists together.

We can also just add individual elements onto a list. So I could come down here and I could say friends dot append.

And this will actually allow me to append another item onto the end of this list. So over here, I could just add another name.

So we could say creed, for example, and now I'm like adding another friend onto the end of this list. So when I run the program, in addition to having the original items that were in the list up here, we also have this additional item at the end, and that append function is always going to add the item onto the end of the list.

But let's say that you wanted to add an item into the middle of the list. So like right here or right here, there's another list function called insert插入. And insert is going to take two päramëters参数. The first parameter is going to be the index where you want to insert the item.

So why don't we insert it here at index position one? I'm just going to say one. And then I'm going to type in the name of the element that I want to add. So we can just say like Kelly. And now at index position one, we're going to have the value Kelly, and all of these other values are going to get pushed up, they're going to get pushed to the right one index position.

So now I run the program, you can see we have Kevin, this new element Kelly, and then all the other elements have been pushed off to the right, we can also remove elements.

So for example, let's say I wanted to remove this gym value, all I have to do is just say friends dot rèmóve移动. And I can just type in whatever element I want to remove.

So I want to remove this gym element. And so now when we run the program, Jim is going to be gone from the list, and we just have these four guys left, you can also remove all of the elements from the list.

So if I just wanted to completely like rēset重新安装 the list and get rid of everything, I could just say friends dot clear. And this is going to give us an empty list.

So it'll get rid of every single element inside the list. There's also another really cool method which is called pop. And you can just type out pop like that. And basically what pop is going to do is it's going to pop an item off of this list.

So when I click the play button, you'll see here that we're left with Kevin, Karen, Jim, and Oscar. So we basically got rid of the last element inside the list.

And that's what pop does, it pops an element off of the list. So it basically removes the last element in the list. So we have our list, but let's say that we wanted to figure out if a certain element was in this list, right, if a certain value is in here, so let's say I wanted to check to see if my friend Kevin was in this list, well, I could say friends dot index.

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

So it'll tell me if Kevin's in the list. So now when I click play, you can see down here it says zero. If I was to say like Oscar in here, for example, now it'll give me the index of Oscar. 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 list. And so now we are going to get back an error, because it says Mike is not in the list.

So that's an easy way to figure out if a specific element is inside of the list. And if it is, then you know what index it's at.

You can also count the number of similar elements in the list. So I'm actually going to come down here, copy this Jim element, and we'll add another Jim element into here.

So I could come down here and I could say friends dot count. And we'll pass in Jim. And this will tell me how many times the value Jim shows up inside of this list.

So it should say two, which it does down there, because we have two values with the text Jim. And so we have all these values, we could also sort this list.

So I could come over here and I could say friends dot sõrt排序. And what this will do is it'll sort the list in äscending上升 order.

So we have a bunch of text here, and you'll see what it does. Basically, what it's going to do is it's going to put it in alphäbeticäl按字母 order.

So you can see we have J K O and T. So the list went from being just random order, like whatever we had up here, to now it's in alphabetical order.

And that's also going to work for lucky numbers. So I could say like lucky numbers dot sort, and then we'll print out lucky numbers.

And these should now be in ascending order, which I'm realizing they already are up here.

But if I was to take this 42 and put it down here, now it will figure everything out and put it in ascending order.

Anyway, we can also rèvêrse反转 a list. So if I wanted, I could reverse this lucky numbers list, I could say lucky numbers dot reverse.

And now when we print it out, instead of being 482 15 16 23, it's going to be 23 16 15 842.

So it's basically just reverse the order of the list. And finally, there's one more list function I want to show you guys which is copy.

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

And now friends two is going to have all the same attribútes属性 as friends, because it's basically just like copied it, basically took a copy of that friends list.

So you can see it's the same exact thing, and copying lists can come in handy in a lot of different situations. So those are just some basic list functions and just to kind of like, you know, get your feet wet尝试 and working with lists.

Obviously, like the more you work with them, the more you practice using these different functions, the more comfortable you're going to get. But that's just kind of like a basic overview of what you can do.

In this tutorial, I want to talk to you guys about tuples元组 in Python. Now a tuple is a type of data structure, which basically means it's a container where we can store different values.

Now if you're familiar with lists in Python, a tuple is actually very similar to a list. It's basically a structure where we can store multiple pieces of information.

But a tuple has a few key differences from lists that we're going to talk about in this tutorial.

First thing I want to do is I want to show you how to create a tuple. So I can come down here and I can create a tuple. And one of the most common examples of tuples is cōõrdïnātes坐标.

So let's say I had a series of like XY coordinates, I could create a variable called coordinates. And I'm just going to set this equal to an open and closed parentheses.

And whenever we want to create a tuple inside of Python, we're going to use these open and closed parentheses. And inside of here, inside of these parentheses, we're going to put the values that we want to store.

So I could, for example, put a list of numbers. So this could be like my coordinates, I could have like four and five.

So this would be my tuple with coordinates inside of it. And one of the things about tuples that makes them unique is a tuple is immútäble不可改变的.

And you'll hear that word a lot when we're talking about tuples, immutable, it basically means that the tuple can't be changed or modified. Once we create our tuple up here, you cannot modify it, you cannot change it, you can't add elements to it, you can't ērāse抹去 elements from it, you can't change any of the elements inside the tuple.

What you see is what you get, once you create the tuple, it's as it is, it can't change. So I could come down here and I can print out some of the attributes inside the tuple.

And the way I can access them is just by using an open an closed square bracket. And in here, I can pass in an index. So tuples are indexed starting at zero.

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

So if I said coordinate zero, now it should print out four. And down here, we're printing out four. So if I wanted to print out that five, we can just put a one inside of those square brackets.

And now we'll print out that five. So I can access the elements inside of a tuple, I can easily access each, each entry条目.

But if I was to come over here and try to change one of the elements, so if I said coordinates, and coordinates one, and I tried to set it equal to something else like 10, Python's actually going to give us an error when we run this program.

So I'm going to run the program, you see we get this error, it says type error, tuple object does not support item ässīgnmënt分配, because it's immutable, because we can't change it.

So that's basically what tuples are, how we can create them. And you know, a tuple, like I said, it's very simple, you just create it, and then you have it.

And now I want to talk to you guys about a common topic in Python, which is the difference between tuples and lists.

So remember, a list is basically doing the same thing, a list. If we wanted to create a list, we would just use square brackets instead of those parentheses.

And if I was using a list, I could assign different values to it, I could, I could basically mútate转变 any of the elements, I could add, delete, modify, change, whatever I wanted with the list.

But with a tuple, we can't do that. And that's sort of the basic difference. And really, Python doesn't say anything more than that.

It basically just says like one of them's immutable, one of them's not. But in practical use cases, generally, people will use tuples for data that's never going to change.

So you'll, you'll store data inside of a tuple, when that data doesn't need to be mutated. That's why something like coordinates is such a good candidate候选人 for a tuple, because once you have coordinates, like, we're probably not going to modify them, right, we might add other coordinates.

So for example, if I wanted, I could create a list of tuples. So I can say like 4567, maybe we have some other coordinates like 8034. So here I have, you know, I have a list and inside of it, we have these tuples.

But remember, these tuples can't be modified, the values can't be changed. So really, it's just a case by case具体情况 basis, I would say by a huge mäjõrïty多数, you're going to be using lists most of the time.

Tuples are more of a niche小众市场, you know, they're using more special situations. But there are plenty situations where you want to use tuples.

So if you're in a situation where you want to store data that can't be changed or mutated, then tuples are the way to go.

So yeah, that's just some basics about tuples, some basics about the difference between lists and tuples. And so a tuple is a great thing to add into your Python repertoire.

10/23

In this tutorial, I want to talk to you guys about using functions in Python. Now a function is basically just a collection of code which performs a specific task. So I can take a bunch of lines of code that are basically doing one thing, I can put them inside of a function.

And then when I wanted to do that task or do that one thing that the function was doing, I can just call the function. And so functions really help you to organize your code a lot better, they allow you to kind of break up your code into different, you know, little chûnks小块 that are doing different things.

And they're really just awesome. So functions are like a very core concept概念 when we're talking about programming in Python.

So I'm going to show you guys how to create an awesome function today. Let's say for the purposes of this tutorial, we want to create a function that says hi to the user.

So the one task that our function performs is basically just saying hi to whoever is writing the program. So over here, if I want to write a function, the first thing I have to use is a keyword in Python, it's called def.

So D E F. And basically, when Python sees this keyword, it's going to say, okay, this person wants to use a function. So after we type out def, we need to give this function a name.

So just like when we are creating variables, we give them descriptive names, we also want to do the same thing with functions.

So if I'm creating a function, I can just give it a specific name, which basically says like what it's doing. So we're going to create a function that says hi.

So I'm just going to call this function say_hi, just like that. And once we type out the name of the function, and so also I could type out say_hi with no underscore or I can type out say_hi with an underscore, both are considered like good practices in Python.

A lot of times if you just have a simple like two-word function like this, you don't need to put an underscore, but we could put an underscore there.

If we wanted, after we type out the name of the function, I'm just going to type an open and close parentheses. And then I'm going to type a colon. And basically what this is doing is it's saying to Python, like all right, all the code that comes after this line is going to be inside of our function.

And in order to write code that's going to end up being inside the function, we actually have to indent缩进 it. So over here, you'll notice that when I clicked enter, my text editor automatically indented the text.

So it's automatically like using this indent here. And that's kind of like one of the rules in Python, is like the code that goes inside of this function needs to be indented.

So if I was to write some code, like out here, this is no longer going to be considered inside the function. So you can see as I type out code, and obviously this isn't real code, but as I type text over here, that's indented, this little like mårker标记 over here is basically saying like, Oh yeah, that's inside the function.

But then when I write code over here, that is like not at the same indentation level as this stuff, it's no longer considering it inside the function.

So that's just a little thing. Any code inside this function needs to be indented. Alright, so our function is just going to say hi to the user. So I'm just going to have it print out some text, it's going to say hello, user.

So this is a very simple function. Obviously, we just have one line of code, and inside of a function, you could have, you know, as many lines of code as you want. But for our purposes, we only need one line in order to perform our function.

So now all we have to do is call this function. So if I want to execute the code inside of this function, I have to do something called calling it.

So if I was to just run my program as it is right now, I'm just going to run it, you'll see that nothing happens over here, right?

Even though this function is printing out hello, user, when I run the program, it's not doing it. And that's because the code inside of a function isn't going to get executed by default.

The code inside of a function is only going to get executed when we specify that we want to execute it.

And in order to do that, we're going to have to do something called calling the function. So in order to call the function, you basically just type out the function's name and those open and close parentheses.

So I'm just going to type out say_hi, and open and close parentheses. And now when we run this program again, you'll see that it prints out hello, user.

So we're executing the code inside of the function. And I want to show you guys just one more thing here, just talking to you guys about how these functions actually work.

So up here, I'm going to print top. And then down here, I'm going to print bottom. So I want to show you guys the flow流程 of these functions inside the program.

So when I run this program, you'll see we print out top, hello, user, and then bottom. So essentially what's happening is when Python goes through and executes this program, it goes over here to this first line, it says okay, we want to print out the word top.

And then it goes down here and it says okay, we want to execute the say_hi function. So Python actually jumps up and it goes over to this say_hi function, and it's going to execute all of the code inside of this function. So it's going to go through, execute all this code.

And then once it's done executing all the code in the function, it's going to jump back down here, and it's going to move on to the next line, which is bottom.

So that's kind of like the flow of functions. Again, with functions, generally when we're naming these functions, you want them to be named in all lowercase. And usually when we're naming stuff in Python, if there's two or more words, we're going to use an underscore or an space in between them.

So I could write this out as say_hello. But in a lot of situations, though, if I have a function like this where the name is really short, I might just be easier to leave it without an underscore, but why don't we just put an underscore in there just to be a super Python official.

All right, so now we can actually make these functions a little bit more powerful. And what we can do is we can give them information.

So a lot of times when we write a function, we're going to want to have additional information that gets passed in. And these are called parameters. So a parameter is a piece of information that we give to the function.

So over here, I can actually specify that this function needs to rècēive收到 some parameters. So I can basically say like, Hey, if you're going to call this function, you need to give us some information, you need to give us some parameters.

And all I have to do to do that is just type out the name of the parameter that I want to receive. So why don't we allow the code calling this function to tell it what name to say hi to up here, I can just say name.

And basically what this means is it means whenever I call this say_hi function, we have to give it a name. So down here, if I was to call this, I have to include a name in here.

So I can say like Mike, and what we can do now is we can actually access this parameter or this variable inside of our function.

So I could come over here and instead of saying hello, user, I could say hello, name. And basically what this is going to do is it's just going to say hello to whatever name got passed in here.

So I'm actually going to copy this, and we will do this twice. So I'll say hello, Mike, hello, Steve. And now when I run this program, you'll see that instead of just saying hello, user, it's saying hello to whichever无论哪个 name I passed into the function.

So that's why this can be really useful, right? We can give the function information, and depending on the information we give it, it'll perform its task a little bit differently.

I could also include more than one parameter. So you can, I mean, technically you could have like as many parameters as you want. So I can put another one in here, we can say age.

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 pass in an age for down here. And I'm just going to pass in strings. So we can say hello, name, you are age. So I'm passing in two pieces of information.

And now when we run this program, it's going to call the function, and it's going to use both of those pieces of information. So I say hello, Mike, you are 35, hello, Steve, you are 70.

So essentially we're writing out this one line of code, which just prints out like hello to someone, and we're allowing this function to receive two parameters, so the name and the age, and depending on the name and the age, the function's going to print out hello a little bit differently.

And that's kind of the beauty of using functions. So you could pass anything you want into a function. So for example, I could pass in an integer instead of a string for the age.

So like I could pass in the number, the only difference is over here we're going to have to convert this into a string. But it's going to work just the same.

So you could pass in strings, numbers, Booleans, arrays, you can really pass any type of data into a function, and it's going to work. So you can see here, we get the same result.

10/28

So that's the basics of functions.

And as you go through with Python you're going to be using functions more and more.

And generally it's a good idea to break your code up into different functions.

So whenever you have like a grouping of code that's designed to perform a specific task that's usually a good candidate候选人 to be put inside of a function.

In this tutorial I want to talk to you guys about using the return statement in Python functions.

Now Python function is basically just a collection of Python code that performs a specific task.

And when we want to perform that task in our Python programs we can call a Python function.

And a lot of times when we're calling a Python function we just kind of call it and it does its thing.

And then we move on with the program.

But sometimes when we call a function we're actually going to want to get information back from that function.

So when I call the function I want it to basically execute its task I want it to execute all the code.

And then I want it to like give me some information back.

So that could be like a pärticúlär特定的 value it could be something like telling me how the task went.

Basically the function can just cömmúnicāte传达 back to us like Hey here's some information.

And that's what the return keyword can allow us to do the return keyword can basically allow Python to return information from a function.

So I'm going to show you guys how we can do this I want to create a very simple function.

And it's just going to cúbe 立方a number.

So when we cube a number we take it to the power of three.

So if I was going to cube like two it'd be like two raised to the power of three.

Right it's just pretty basic math.

So we're going to make a function that will cube a number.

And I'll show you guys how we can use that return statement.

So first thing we want to do is make a function I'm just going to say def cube we'll call the function cube.

And over here we can specify any parameters that this function will take.

So basically any values that we want to give this function.

So we're going to give it one function which is just going to be a number.

So I'm just going to call it num.

And then over here I'm going to type out a colon.

And then down here we can put any of the code that we want to be included in this function.

So basically this function is just going to cube this number.

So we could just type out num times num times num.

Right that's going to give us num cubed.

So that's all well and good.

And if I come down here I can execute this function.

So I can basically just call it I'll say cube.

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 to save this.

And now let's go ahead and run this code.

So we're on the code.

And you'll see down here nothing happened right?

So nothing got printed out in our program.

In fact when we call this cube function it didn't really do anything.

So let me try to print this out.

So maybe if we print this function out it'll give us back the information that we want.

So type out print and we'll print out the value of the function I'm just going to click play.

You look down here it says none.

So when I printed out the value of this function it told me none.

But really this function is süppōsed应当 to be cubing the number.

And here's how we can use the return statement in Python.

I can come over here and I can just say return.

And now Python is going to return whatever value we put over here to the right.

So when I call this cube function and I give it a three Python is going to go off it's going to execute this function.

When it sees this return statement it's going to say okay I want to give a value back to whatever call of this function.

And it's going to return this value.

Now when I print out cube three it should print out the answer because we get an answer back.

So when I play this you'll see down here it says 27.

So we got the correct answer.

So I could do this with anything why don't we cube like four or something?

So now we're on this.

And you can see we're constäntly不断的 getting the correct answer back.

So that's the basics of using this return statement it allows us to return a value back to the caller.

In other words back to whatever's calling the function.

So I just want to illüstrāte阐明 this point a little bit fûrthër更近一步 I'm going to come up here I'm actually going to create a variable.

So I'm going to create a variable called result.

And I'm just going to set it equal to cube four.

This variable result is going to store the value that gets returned from the cube function.

So it's not going to store like cube four it's going to store the value that gets returned from executing that function.

So now if I came down here and printed out result it's actually just going to print out 64 again because that's the value that it's storing.

So this return statement can be really useful for like getting information back from a function we can use something like a parameter like this four as a parameter and we can give information to a function.

And then we can use the return statement to get information back from a function.

And I do just want to point out one more thing I'm actually not able to put any code after this return statement.

So if I came down here and tried to put like a print statement this actually isn't going to work.

So this is never going to get printed out.

So if I just typed out like code right here when we run this program it's not going to type out code.

That's because when I use this return keyword it breaks us out of the function.

So Python's going to go through it's going to execute the code in the function whenever it sees this return keyword it's just going to break back out and we're going to be done.

So we'll never be able to rēach到达 this line of code.

So that's the basics of using return statements.

And you can return any data type you want we could easily return like a string or a Boolean or an array it doesn't really matter you can return whatever you want.

And a lot of times in a lot of situations you're going to want to get a value back from a function.

In this tutorial I want to talk to you guys about using if statements in Python.

Now if statements are a special structure in Python where we can actually help our programs to make dècisions判断.

So by using an if statement I could execute certain code when certain cönditions条件 are true.

And I can execute other code when other conditions are true.

So basically if statements allow our programs to respond to the input that they're given.

So depending on the data that we're using in the program our programs will be able to respond.

So when certain data is certain values we can do certain things.

And when other data is other values we can do other things.

So you know your programs are essentially becoming a lot smarter.

And actually if statements are things that we as human beings deal with every day.

So you know as you go throughout your day generally you're dealing with if statements all the time.

So I want kind of give you guys a bríef简短的 introduction into if statements and we'll talk about some of the common if statements that you'd see in everyday life.

And then we'll go ahead and do a really awesome example to kind of like show you guys what if statements can do for us.

So over here in my text editor I just have this little text file.

And in here I wrote out a bunch of if statements that a lot of people will èncounter遇到 on a daily basis.

So here's the first one it says I wake up if I'm hungry I eat breakfast.

So let's break this down a little bit up here it says if I'm hungry.

And this is a condition right?

This is either going to be true or it's going to be false right?

If it's true I'm going to go ahead and eat breakfast right?

If it's false I'm just going to skip whatever was in here and move on, right?

So that's the basics of an if statement right?

We have a condition in this case if I'm hungry if the condition is true then we do the action.

So we eat breakfast.

Otherwise we just skip it.

So down here we have another one it says I leave my house.

If it's cloudy多云的 I bring an umbrella.

Otherwise I bring sunglasses.

And so this one is a little bit more complex than the one we just looked at.

You'll see we have our condition right?

It says if it's cloudy if that condition's true we're going to bring the umbrella.

But in this case if the condition's false there's something else that we want to do.

So in the case up here if this condition was false we just moved on.

But now if this condition's false then we're going to go ahead and bring sunglasses.

So that's another example of an if statement it's a little bit more complex.

Finally I have one more down here and this one's even more complex it says I'm at a restaurant.

If I want meat I order a steāk牛排.

So if this condition's true we order the steak.

But if this condition is false then we're going to move on and we're actually going to check another condition.

So I'm going to say otherwise if I want pastä面食 I order spaghetti and meatballs.

This is another condition.

If this condition's true, we get the spaghetti.

If it's not true, then finally we're going to move on down here, and we'll just default to ordering the salad.

So all three of these if statements are valid有效的 if statements, it's just they get more and more complex as we go down the list.

So hopefully that's, you know, it can kind of wrap your head around what if statements are we're basically specifying conditions.

If those conditions are true, we're going to do certain things.

And if those conditions are false, we can do other things, or we can check other conditions.

All right, so let's head over to our Python file, and we'll start writing some actūäl真实的 Python.

So in here in my app dot Python file, I want to create an if statement.

And I'm going to show you guys a very basic if statement in this tutorial.

And then in the next tutorial, we're going to look at some more advanced if statements.

And so what I want to do is create a Boolean variable.

So I'm going to create a Boolean variable that's going to store whether or not the user is a male.

So it's just going to be called is underscore male.

And I'm going to set this equal to true because I'm a male.

So we have this Boolean variable, right?

And right now we have it set equal to true.

I can actually use an if statement to check to see what the value of this variable is.

And if it's true, I can do something.

So if I want to use an if statement, all I have to do is just type out if, and then I just need to type out a condition.

So basically, I would have to type out like something that's going to be true or false.

Like, for example, in the other page, we were specifying like if I'm hungry, or if it's cloudy, here, we're just going to check to see if is mail.

So in other words, if the person is male.

And so remember, this has to be a, like, it has to be able to be rèdúcïble可简化 to a true or a false value.

So in our case, we're just using a Boolean because that's like the easiest thing we can do.

So I said if I specified my true or false condition, and now I want to just type a colon.

And what I want to do now is make a new line.

And so anything that I put below this with an indentation is actually going to be executed when that condition’s true.

So I could put print.

And down here, I could just put you are a male.

And now you'll see what happens when I run this program, it's going to go down here and it's going to say you are a male.

But if I changed is mail to false.

So instead of this being true, I just change it to false.

Now all of a sudden it's not going to print anything.

So it's just going to be blank空白的 down here.

That's because this condition wasn't true.

So anything that I put below this if declärātion声明 and that has an indentation like that is going to be executed when the if statement is true.

We can also use another keyword in Python, which is called else.

And else is basically going to be like otherwise.

So you remember before we said, if it's cloudy I'll bring an umbrella.

Otherwise, I'll bring sunglasses.

Well, this is going to be like our otherwise.

So I can say down here else.

And now anything that I put below else is going to get printed out when that condition isn't true.

So why don't we just put you are not a male.

So now we're able to cover both of the situations for this variable.

If it's false, we can cover that situation.

So it says you are not a male.

If it's true, we can cover that situation as well.

So it'll say you are a male.

So that's pretty cool.

But we can also make these more complex.

And actually before I talk about that, you can put as much code as you want inside of an if statement.

So I could have like, you know, dozens of lines of code if I wanted.

I'm just giving you guys a very simple example.

So why don't we make this more complex?

And I'm going to add another variable into the mix组合,混乱.

So I'm going to add another, you know, Boolean attribute属性 into this mix.

I'm going to make another variable called is tall.

And we're going to set this equal to true as well.

So now I have two variables in the mix.

Let's say I wanted to write an if statement that will check both of those variables.

So I could write an if statement that's like, if they're male and they're tall, or if they're male and they're not tall, or if they're not male and they're tall, like I could write out a bunch of if statements to check the combïnātion组合 of these variables.

And so the way I can do that is by using one of two keywords.

So the first keyword I want to show you guys is the or keyword.

So I can come over here and I can say if is male or is underscore tall.

And what this is basically going to do is it's going to say if the person is either male, or if they're tall, then we want to do something.

So this is only going to be true when one of these values is true, when one or both of those values is true.

So down here, we could actually put instead of you are a male, we could put you are a male or tall or both, that's going to be more äpprōpríāte合适的.

And then down here, we would say you are neither male nõr也不是 tall.

And that's because if we execute the code inside of this else that means that both of these guys are false.

Right, so I'm saying if they're male or they're tall, we're going to execute whatever is in here.

Otherwise, that means they're neither male nor tall. We're just going to execute this down here.

So let me show you guys how this works.

I'll click play.

And 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 these to false.

So let's set is male equal to false.

Now, it's still going to say you are a male or tall or both.

But if I set them both equal to false, now it's going to say you're neither male nor tall.

So that's kind of the basics of using the or operator.

And there's another operator we can use which is called and ,and basically it's similar to or except both of these conditions have to be true.

So this is basically saying if the person is male and they're also tall, then we're going to execute whatever is in here.

So we could actually change this text to say you are a tall male, because we know for a fact that if this is getting executed, they're both male and tall.

We can also change this print statement down here.

This could say you are either not male or not tall or both.

So you can see it's basically just saying that you're either not one of them or you're not both of them.

So we click the play button and you'll see because they're both false it's going to say you are either not male or not tall or both.

你可能感兴趣的:(python)