Hi, my name is Mosh, and I'm going to be your instructor in this Python course. 嗨，我的名字是Mosh，我将成为你Python课程的导师。
In this course, you're going to learn everything you need to get started programming in Python. 在本课程中，您将学习使用Python开始编程所需的一切。
Python is one of the most popular programming languages, and every day people use it to do Python是最流行的编程语言之一，每天都有人用它来做
cool things like automation, they use it in AI, as well as 很酷的事情，比如自动化，他们在AI中使用它，以及
building applications and websites like Instagram and Dropbox. So if you're looking 构建应用程序和Instagram和Dropbox等网站。所以，如果你正在寻找
for a job, Python is for you. In this course I'm going to teach you everything 对于一份工作，Python适合你。在这个课程中，我将教你一切
you need to get started with Python. We're going to talk about all the core concepts 你需要开始使用Python。我们将讨论所有核心概念
in Python, and then we're going to build three Python projects together. 在Python中，然后我们将一起构建三个Python项目。
Here's the first project we're going to build. We're going to learn how to create this beautiful website 这是我们要构建的第一个项目。我们将学习如何创建这个美丽的网站
for an imaginary grocery store. Here on the homepage we can see all 对于想象中的杂货店。在主页上我们可以看到所有
of the products in the shop, and we also have an admin area for managing the 在商店里的产品，我们还有一个管理区域来管理
stock. We're going to build this using a popular Python framework called Django. 股票。我们将使用一个名为Django的流行Python框架来构建它。
Now if you have never built a website before, don't worry, I will teach you everything from scratch. 现在，如果您以前从未建立过网站，请不要担心，我会从头开始教您一切。
You're also going to learn how to use Python in machine learning, or artificial intelligence. 您还将学习如何在机器学习或人工智能中使用Python。
So you will learn how to write a Python programming that will predict 因此，您将学习如何编写可预测的Python编程
the music that people like based on their profile. Just like how YouTube 人们喜欢的音乐基于他们的个人资料。就像YouTube一样
recommends videos based on the videos you have watched before. I will also show you 根据您之前观看过的视频推荐视频。我也会告诉你
python to automate boring repetitive tasks that waste your time. python自动化浪费时间的无聊重复性任务。
You will write a Python program that will process thousands of spreadsheets 您将编写一个Python程序来处理数千个电子表格
in under a second. I've designed this Python course for anyone who wants to learn 在一秒钟之内。我为任何想要学习的人设计了这个Python课程
Python. If you're a beginner, don't worry, I will hold your hand through this entire course. 蟒蛇。如果您是初学者，请不要担心，我将全力以赴。
You're not too old or too young, and Python is super easy to learn. You can write your first 你不是太老或太年轻，Python非常容易学习。你可以写第一个
Python program in literally seconds. Plus I'm going to give you plenty 几秒钟内的Python程序。另外我会给你很多
of exercises to help you build your confidence writing cool Python programs. 练习可以帮助您建立自信，编写出色的Python程序。
My name is Mosh, I'm a software engineer with two decades of experience 我的名字是Mosh，我是一名拥有二十年经验的软件工程师
and I've taught over 3 million people how to code. I'm super excited to be teaching you 我已经教过300多万人如何编码。我很高兴能教你
Python in this course. So I hope you stick around and learn this beautiful 本课程中的Python。所以我希望你留下来学习这美丽
powerful programming language. 强大的编程语言。
In this tutorial I'm going to show you how to download 在本教程中，我将向您展示如何下载
and install Python on your computer. So the first thing I want you to do, 并在您的计算机上安装Python。所以我要你做的第一件事，
is open up your browser and head over to Python 打开你的浏览器并转向Python
.org. On this page click on downloads, .ORG。在此页面上单击下载，
here you can see the latest version of Python currently, at the time of recording this video 在这里，您可以在录制此视频时看到最新版本的Python
the latest version is python 3.7.2. 最新版本是python 3.7.2。
Chances are in the future, when you are watching this tutorial, there is a newer 有可能在未来，当你看这个教程时，有一个更新的
version of Python available, don't worry, all the materials you're going to learn in this course 可用的Python版本，请不要担心，您将在本课程中学习所有材料
will apply to the latest version of Python as well. So let's go ahead 也将适用于最新版本的Python。让我们继续吧
and download Python 3, alright now, 并下载Python 3，好了，
look at your downloads folder, here, we should have Python 3 installer 看看你的下载文件夹，在这里，我们应该有Python 3安装程序
simply double click that, if you're on Windows 如果你在Windows上，只需双击即可
you're going to see this check box here, add Python to 你会在这里看到这个复选框，添加Python
python, this is really important, make sure to tick this box, otherwise you're not going to be able to python，这非常重要，请务必勾选此框，否则您将无法执行此操作
follow this tutorial. If you're on a Mac, 按照本教程。如果你在Mac上，
you're going to see an installer like this, with this setup wizard we're going to install Python 3 你会看到这样的安装程序，使用这个安装向导，我们将安装Python 3
on our computer. So simply click continue, and again, 在我们的电脑上。所以只需点击“继续”，然后再次
and again, and agree with the license agreement, and 再次，并同意许可协议，和
install it, this is going to take a few seconds so I'm going 安装它，这将需要几秒钟，所以我要去
to pause the recording. 暂停录音。
Alright, we have successfully installed Python 3 on our computer. 好的，我们已经在我们的计算机上成功安装了Python 3。
Let's close this. Alright, next we need to install 让我们关闭这个。好的，接下来我们需要安装
a code editor, we se a code editor to write our code, just like 一个代码编辑器，我们是一个代码编辑器来编写我们的代码，就像
how we use Microsoft Word to write documents, we use a code editor to write 我们如何使用Microsoft Word编写文档，我们使用代码编辑器编写
code. Now there are so many code editors out there, the one that I'm going to show you in this 码。现在有很多代码编辑器，我将在此向您展示
tutorial is PyCharm. That is one of the most popular code editors 教程是PyCharm。这是最受欢迎的代码编辑之一
for writing Python code. You can get it from jetbrains.com 用于编写Python代码。你可以从jetbrains.com获得它
/pycharm. Now more accurately pycharm / pycharm。现在更准确的pycharm
is considered an IDE, which is short for integrated development 被认为是IDE，它是集成开发的缩写
environment. And that's basically a code editor on steroids, 环境。这基本上是类固醇的代码编辑器，
it has some additional features that makes it really easy to write code. So 它有一些额外的功能，使编写代码非常容易。所以
let's go ahead and download PyCharm. Now here on the 让我们继续下载PyCharm。现在在这里
download page, you can see two versions of PyCharm, one is the professional addition 下载页面，你可以看到两个版本的PyCharm，一个是专业的补充
which has additional features and you have to pay for them. The other is the 它有额外的功能，你必须付钱。另一个是
community edition, that is absolutely free, and that's what we're going to use in this tutorial. 社区版，这是完全免费的，这就是我们将在本教程中使用的内容。
So, go ahead and download this as well. 所以，继续下载这个。
Now, once again look at your downloads folder, once again you should have pycharm 现在，再一次看看你的下载文件夹，你应该再次拥有pycharm
so double click this, now if you're 所以双击这个，现在，如果你是
in Windows you're going to see an installation wizard, simply click next, next, next 在Windows中，您将看到一个安装向导，只需单击下一步，下一步，然后单击下一步
until you install pycharm. If you're on Mac you need to drag and drop this icon 直到你安装pycharm。如果您使用的是Mac，则需要拖放此图标
onto the applications folder, so drag and drop, 到应用程序文件夹，所以拖放，
alright, now let's double click this to run it, 好吧，现在让我们双击它来运行它，
the first time we run pycharm we're going to get this warning, because this is an application 我们第一次运行pycharm时会得到这个警告，因为这是一个应用程序
that we download from the internet. So let's go ahead and open it, 我们从互联网上下载。那么让我们继续吧，打开它，
next you're going to see this dialogue box for importing some settings, leave this to 接下来你将看到这个用于导入一些设置的对话框，留下这个
do not import settings, it doesn't really matter. Okay, 不要导入设置，这并不重要。好的，
On this page, select I've never used PyCharm you can see some keyword 在这个页面上，选择我从未使用过PyCharm你可以看到一些关键字
shortcuts that might be useful in the future, just accept this, and down at the bottom, 可能在将来有用的快捷方式，只需接受这个，然后在底部，
click the next button. And then next again, 单击下一步按钮。再接下来，
one more time, and then finally start using pycharm. 再多一次，然后终于开始使用pycharm。
So here's the main page of PyCharm every time you open 所以这里是每次打开时PyCharm的主页
it, click on create new project, 它，点击创建新项目，
this is the location for our project, let's call our project Hello 这是我们项目的位置，让我们调用我们的项目Hello
World. Now before click and create, expand 世界。现在点击并创建之前，展开
this item here, make sure that base interpreter is set to Python 这个项目，确保基本解释器设置为Python
3. Python interpreter is basically a program Python解释器基本上是一个程序
that knows how to execute Python code, it will know how to interpret 知道如何执行Python代码，它将知道如何解释
or translate Python instructions into instructions a computer can understand. 或者将Python指令翻译成计算机可以理解的指令。
Now, Mac computers, by default come with an older 现在，Mac电脑默认配备较旧的电脑
installation of Python, that's Python 2, it's considered legacy 安装Python，即Python 2，它被认为是遗留的
which means it's no longer maintained or supported. So earlier we downloaded 这意味着它不再被维护或支持。所以我们之前下载了
Python 3, make sure that this is set to Python 3, if not Python 3，确保将其设置为Python 3，如果没有
from this list, select Python 3.7. 从此列表中，选择Python 3.7。
Alright, now let's go ahead and create this project, 好的，现在让我们继续创建这个项目，
Now here right click on 现在点击右键
Hello World folder and go to new Hello World文件夹并转到新文件夹
Python file, call this file app.py. Python文件，调用此文件app.py.
So by convention, all Python files should have this 所以按照惯例，所有Python文件都应该有这个
py extension. Alright, let's go ahead, py扩展。好的，我们继续吧
now let's collapse this project panel by clicking here and write our 现在让我们点击这里折叠这个项目面板并写下我们的
first python program. Simply type print, pr 第一个python程序。只需输入print，pr
int, all in lowercase, open and close parenthesis, int，全部为小写，打开和关闭括号，
and inside these parenthesis, add quotations we 在这些括号内，添加引号我们
can either use single quotes or double quotes, now 现在可以使用单引号或双引号
in between the codes write your name. I am Mosh Hamedani, so 在代码之间写下你的名字。我是Mosh Hamedani，所以
here, so this is your first Python programming. With these piece of code 这里，这是你的第一个Python编程。有了这些代码
you can print your name on the screen. Now to run this, go 你可以在屏幕上打印你的名字。现在运行它，去吧
on the top, under the run menu, click run, 在顶部，在运行菜单下，单击运行，
also note that there is a shortcut associated with this command, when 还要注意，有一个与此命令关联的快捷方式
I program I always use shortcuts because that increases my productivity. 我编程我总是使用快捷方式，因为这可以提高我的工作效率。
So here on a Mac computer, the shortcut is control, option, and 所以在Mac计算机上，快捷方式是控件，选项和
R. On Windows it's different. So let's do that, now R.在Windows上它有所不同。现在让我们这样做吧
it's asking where you want to run this, click on app, 它问你要在哪里运行它，点击应用程序，
down below, you should see this little terminal window, this 在下面，你应该看到这个小终端窗口，这个
is a little window into our program, so here you can see our results for 是我们程序的一个小窗口，所以在这里你可以看到我们的结果
output for our program. In the future as you learn more Python, you'll be 我们程序的输出。将来随着你学习更多Python，你将成为
able to build applications that have a graphical user interface, or gooey. 能够构建具有图形用户界面或粘性的应用程序。
That is a little bit complicated, so for now we're going to use this little 这有点复杂，所以现在我们将使用这一点
terminal window to see the results of our program. So as you can see my name is 终端窗口可以看到我们程序的结果。所以你可以看到我的名字
printed here, now as you print code, this window might get in the way so 打印在这里，现在当你打印代码时，这个窗口可能会妨碍你
always resize it or minimize itty putting this 总是调整它或最小化itty
icon here, so this was our first python program. Now 图标在这里，所以这是我们的第一个python程序。现在
if you didn't see the result I showed you in this video, use the comment box below and 如果您没有看到我在此视频中向您展示的结果，请使用下面的评论框和
tell me what error you encountered, I'll do my best to help you move forward. 告诉我你遇到了什么错误，我会尽力帮助你前进。
In this Python tutorial, we're gong to take this program to the next level and make it more interesting. 在这个Python教程中，我们将把这个程序提升到一个新的水平并让它变得更有趣。
So I'm going to show you how to draw a dog hair. Now as part of 所以我要告诉你如何画狗毛。现在作为一部分
this tutorial you will learn how Python code gets executed and you will also learn 在本教程中，您将学习如何执行Python代码，您也将学习
about a few programming terms. So press enter, and 关于一些编程术语。所以按回车键，然后按
on line 2 write another print statement, so print, open and close 在第2行写另一个打印语句，所以打印，打开和关闭
parenthesis, add a quotation, now here you want to draw a dog. 括号，添加引文，现在在这里你要画一条狗。
So, add an o, that is the head of our dog, followed by 所以，添加一个o，这是我们的狗的头，然后
4 hyphens. So this is the body. 4个连字符。所以这就是身体。
alright, now one more time another print statement with 好吧，现在又一次打印声明了
quotations, now we need to draw the legs. So add a space. 报价，现在我们需要画腿。所以添加一个空格。
Follow it by 4 vertical bars. So, like this, 按照4个垂直条跟着它。所以，像这样，
so here's a little imaginary dot. Now let's 所以这里有一个小小的想象点。现在让我们来吧
run this program and see what we get. So on the top right corner you should see this play button, 运行这个程序，看看我们得到了什么。所以在右上角你应该看到这个播放按钮，
click that, there you go, so we have our name and 点击那里，你去，所以我们有我们的名字和
right below that we have our imaginary dog. Now what you need to understand 就在下面，我们有想象中的狗。现在您需要了解的内容
here, is that our python code gets executed line by line 这是我们的python代码逐行执行
from the top. So earlier I told you about Python interpreter 从一开始。所以早些时候我告诉过你关于Python解释器的事
that is the program that knows how to translate or interpret 这是一个知道如何翻译或解释的程序
our Python code into instructions that a computer can understand. 我们的Python代码是计算机可以理解的指令。
So when we run this program by clicking this program here, 所以当我们点击这个程序运行这个程序时，
python interpreter starts executing or running our program python interpreter开始执行或运行我们的程序
line by line from the top. So first it executes line 1, 从顶部逐行。所以首先它执行第1行，
then, moves onto line 2, and so on. So this is how python 然后，移动到第2行，依此类推。所以这就是python
programs get executed. Now let me show you something cool. Let's add 程序被执行。现在让我告诉你一些很酷的东西。我们来补充一下
another print statement, with quotations now 另一份印刷声明，现在引用
in between the quotations, add a star or an asterisk, like 在引文之间，添加星号或星号，如
this. Now after the quotation and before the parenthesis, 这个。现在在引用之后和括号之前，
add a space, once again, add an asterisk, 添加一个空格，再次添加一个星号，
space, 10. What is going on here? Well, 空间，10。这里发生了什么？好，
anywhere we have quotations like here or here 我们在这里或这里引用的任何地方
we're defining a string, a string is a programming term which means 我们定义一个字符串，字符串是一个编程术语，意思是
a series of characters so here we have a string, we also have 一系列字符所以这里我们有一个字符串，我们也有
a string on line 3, as well as line 2 and line 1. 第3行的字符串，以及第2行和第1行。
Now here, we're multiplying the string by 现在，我们将字符串乘以
number 10. So this is the multiplication operator, just like the multiplication 数字10.所以这是乘法运算符，就像乘法运算一样
operator we have in math. So with this piece of code we can 算术我们有数学。所以我们可以使用这段代码
draw 10 asterisks on the terminal, let me show you. So let's run this 在终端上画10个星号，让我告诉你。让我们来运行吧
program one more time, there you go. So we have 10 程序再一次，你去。所以我们有10个
asterisks. Now what we have here, this piece of code here, 星号。现在我们在这里，这段代码，
is called an expression. An expression is a piece of 被称为表达式。表达是一块
code that produces a value. So when Python interpreter 产生值的代码。所以当Python解释时
tries to execute line 4, first it will evaluate 尝试执行第4行，首先它将进行评估
the code that we put in between parenthesis, so we could evaluate our expressions 我们放在括号之间的代码，所以我们可以评估我们的表达式
Our expression will produce 10 asterisks and then 我们的表达式将产生10个星号然后
those asterisks will be printed on the terminal. Now as an exercise 那些星号将印在终端上。现在作为练习
you can use these print statements to draw another shape, you can draw a heart, a 你可以使用这些打印语句来绘制另一个形状，你可以画一颗心，一个
ball, whatever you like. I will see you in the next tutorial. 球，无论你喜欢什么。我将在下一个教程中看到你。
One of the questions I get a lot on my channel is 我在频道上遇到的一个问题是
how long does it take to learn Python and become job ready? Well there is no single 学习Python并准备工作需要多长时间？好吧没有单身
answer. It really depends on you and how much effort and commitment you want to put 回答。这真的取决于你以及你想付出多少努力和承诺
into this. But I would say if you spend 2 hours every day 进入这个。但我会说如果你每天花2个小时
consistently, after about 3 months you should be able to write 一直以来，大约3个月后你应该能够写出来
basic python programs, but quite honestly, that doesn't get you a job. 基本的python程序，但老实说，这不会让你找到工作。
In order to get a job, you need to specialize in one area, 为了找到工作，你需要专注于一个领域，
what do you want to use python for? Do you want to build web applications or 你想用python做什么？您想构建Web应用程序还是
desktop applications? Or do you want to use it in machine learning and artificial intelligence? 桌面应用？或者你想在机器学习和人工智能中使用它吗？
So whatever you want to do you need to take additional courses. 所以无论你想做什么，你都需要参加其他课程。
For example if you want to become a web developer in addition to learning 例如，如果您想成为一名Web开发人员，除了学习
Python, you should also learn about html, some css, some Python，你还应该学习html，一些css，一些
JavaScript and Django which is a popular Python framework JavaScript和Django是一个流行的Python框架
for building web applications. Learning all these things would take you another 用于构建Web应用程序。学习所有这些东西会带给你另一个
6 months, so in total you need 9-12 months to become 6个月，总共需要9-12个月才能成为
job ready. At that point you can get a junior developer job with a 准备工作。那时你可以获得一个初级开发人员的工作
salary of about 50-60 thousand dollars a year. Now as you work more 一年的工资约为50-60万美元。现在，当你工作更多
as you do more Python projects, your resume starts to build and 随着你做更多的Python项目，你的简历开始构建和
you can ask for $100,000 a year, or 120,000 dollars 你可以要求每年10万美元，或120,000美元
a year, depending on where you are, what company, what geographical area, 一年，取决于你在哪里，什么公司，什么地理区域，
it really depends, there is no single answer. So are you excited to 这真的取决于，没有单一的答案。所以你兴奋吗？
learn Python and get started on this career path? If you are, I would encourage you 学习Python并开始这个职业道路？如果你是，我会鼓励你
to make a commitment and spend 2 hours every day practicing 做出承诺，每天花2小时练习
python. And use the comment box below and let me know why you are learning 蟒蛇。并使用下面的评论框告诉我你为什么学习
Python, what do you want to do with it? What's your dream job? I would love to hear your story. Python，你想用它做什么？你梦寐以求的工作是什么？我很想听听你的故事。
In this Python tutorial, you're going to learn about variables, which are one of the most fundamental 在这个Python教程中，您将学习变量，这是最基本的变量之一
concepts in programming, they're not specific to Python, they exist 编程中的概念，它们不是特定于Python，它们存在
in pretty much every programming language out there. We use variables 几乎所有的编程语言都在那里。我们使用变量
to temporarily store data in a computer's memory. Here's an example, 暂时将数据存储在计算机的内存中。这是一个例子，
let's type price = 10, when Python 当Python时，让我们输入price = 10
interpreter executes this code, it will allocate some memory, then it will store 解释器执行此代码，它将分配一些内存，然后它将存储
the number 10 in that memory, and finally it will attach this 那个记忆中的数字10，最后它将附加这个
price label in that memory location. As a metahor 该内存位置的价格标签。作为一个metahor
imagine we have a box. In that box, we have number 10 想象我们有一个盒子。在那个方框中，我们有10号
and price is the label that we put on the box. Now we can use this label 和价格是我们放在盒子上的标签。现在我们可以使用这个标签了
anywhere in our program to access the value that we have in that box. 在我们的程序中的任何地方访问我们在该框中的价值。
This is a very simplified explanation. So now, let's 这是一个非常简化的解释。现在，让我们吧
print price on the terminal. Print, now this time we're not going to 在终端上打印价格。打印，现在这次我们不打算
add quotations, because if we put quotations here, we will see the text 添加报价，因为如果我们在这里加上引文，我们会看到文字
price on the terminal. Now the value of the price variable. 终端的价格。现在价格变量的价值。
So, put it in quotations, and type price, 所以，把它放在报价单中，然后键入价格，
now, let's run this program one more time, there you go. So we see 10, 现在，让我们再一次运行这个程序吧。所以我们看到10，
on the terminal. So this is how we define variables, we 在终端上。所以这就是我们定义变量的方式，我们
start with an identifier which is the name of our variable, then, 以标识符开头，标识符是我们变量的名称，然后，
an equal sign and finally a value. Now more accurately, when 一个等号，最后一个值。现在更准确，何时
this number 10 is about to be stored in the memory, first it will 这个数字10即将存储在存储器中，首先它将存储
get converted to this binary for presentation. So this number 10, 转换为此二进制文件进行演示。所以这个数字10，
is in the decimal system which has all the digits from 0 to 9. 在十进制系统中，其所有数字都是0到9。
Computers don't understand all these digits, they only understand 计算机不理解所有这些数字，他们只能理解
0s and 1s. So when we store the number 10 in the computer's memory 0和1。所以当我们将数字10存储在计算机的内存中时
first it will get converted to it's binary representation which will be 首先它将转换为它的二进制表示
a bunch of 0's and 1's, like 001, 001, whatever 一堆0和1，如001,001，无论如何
I don't know. Then it will get stored in the computer's memory. So, 我不知道。然后它将存储在计算机的内存中。所以，
let's take this program to the next level. On the second line we can 让我们把这个程序提升到一个新的水平。在第二行我们可以
update the value of this price variable, so we can reset it 更新此价格变量的值，以便我们重置它
to a new value like 20, now when we run our program, 现在当我们运行我们的程序时，到20这样的新值，
we should see 20, because as I told you before Python interpreter executes our 我们应该看到20，因为正如我在Python解释器执行之前告诉你的那样
code line by line from the top. So first we set 从顶部逐行编码。所以首先我们设定
the price to 10, then we reset it to 20, and finally we 价格为10，然后我们重置为20，最后我们
print it on the terminal, let's run the terminal, there you go, so, 将它打印在终端上，让我们运行终端，你去，所以，
we see 20 here, okay? Now these 我们在这里看到20，好吗？现在这些
numbers that we have here are whole numbers without a decimal point. 我们这里的数字是没有小数点的整数。
In programming, we refer to these numbers as integers. 在编程中，我们将这些数字称为整数。
But integer is a number without a decimal point. We can also use 但是整数是一个没有小数点的数字。我们也可以使用
numbers with a decimal point for example on line 2, we can define 例如在第2行上带小数点的数字，我们可以定义
another variable called rating and set it to number 另一个名为rating的变量并将其设置为number
4.9. Now in programming, we refer to this kind of number as 4.9。现在在编程中，我们将这种数字称为
a floating point number of float for short. So we have integers 浮点数浮点数。所以我们有整数
and floats. We can also define a variable and set 和漂浮。我们还可以定义变量和集合
it to a string, for example, name equals 它是一个字符串，例如，名称等于
Mosh, we also have another kind of value which is called boolean, Mosh，我们还有另一种叫做boolean的值，
which can be true or false. They are line yes and no in English. 这可能是真是假。他们是肯定的，不是英文的。
Here is an example, I'm going to define a variable, is underline 这是一个例子，我将定义一个变量，下划线
published so we use an underscore to separate 发表所以我们使用下划线分开
multiple words in our variables name. We set this to 我们变量名中的多个单词。我们将此设置为
true, or false. These are 对或错。这些是
boolean values. now note that Python is a case 布尔值。现在请注意Python就是一个例子
sensitive language, which means it's sensitive to lower case and upper case letters. 敏感语言，这意味着它对小写和大写字母很敏感。
So when defining variables we should always use 所以在定义变量时我们应该总是使用
lowercase letters, but here false and true are special keywords in the 小写字母，但这里的false和true是特殊的关键字
language, so if we spell it with a lowercase f, Python doesn't 语言，所以如果我们用小写f拼写它，Python就不会
understand it. You can see we have a red underline here, which indicates an error. 明白它。你可以看到我们这里有一个红色下划线，表示错误。
Make sure to spell this with a capital F, or if you want to set this to true, 确保拼写为大写字母F，或者如果要将其设置为true，
make sure the T is capital, so in this 确保T是资本，所以在此
program, you're storing simple values in our computer's memory. Simple 程序，你将简单的值存储在我们的计算机内存中。简单
values can be numbers, which can be integers or floats 值可以是数字，可以是整数或浮点数
or they can be strings or booleans. But in Python 或者它们可以是字符串或布尔值。但在Python中
we can also store complex values like lists and values. And that's what I'm going to show you 我们还可以存储列表和值等复杂值。这就是我要告诉你的
in the future. So before going any further, I want you 在将来。所以在继续前进之前，我想要你
to do a little exercise. Imagine we're going to write a program for a hospital. 做一点运动。想象一下，我们要为医院写一个程序。
So we check on a patient named John Smith. He's 因此，我们检查一位名叫John Smith的病人。他是
20 years old and is a new patient. I want you to define 3 variables here, 20岁，是一名新病人。我希望你在这里定义3个变量，
for his name, his age, and another variable 因为他的名字，他的年龄和另一个变数
for if this is a new or an existing patient. So pause the video and spend one 因为这是新患者还是现有患者。所以暂停视频并花一个
minute on this exercise. When you're done, come back, continue and see my solution. 这个练习分钟。当你完成后，回来，继续看看我的解决方案。
Alright, so here we need 好的，我们需要这里
three variables, the first one is the patient's name, 三个变量，第一个是患者的姓名，
we set that to John Smith. We can also 我们把它设置为John Smith。我们也可以
call this full name, these are both valid names for our variables. 调用这个全名，这些都是我们变量的有效名称。
The second variable is for the age of our patient. So age 第二个变量是我们患者的年龄。所以年龄
is 20, and finally we need a variable to tell if this is a new 是20，最后我们需要一个变量来判断这是否是新的
or existing patient. That's where we can use a boolean value. 或现有的病人。这就是我们可以使用布尔值的地方。
So, we define a variable, is new and 所以，我们定义一个变量，是新的和
we set it to true. 我们将其设置为true。
So, you have learned how to print 所以，你已经学会了如何打印
messages on the terminal window. In this tutorial, I'm going to show you how to 终端窗口上的消息。在本教程中，我将向您展示如何操作
receive input from the user. So we're going to write a small program that asks 接收来自用户的输入。所以我们要编写一个要求的小程序
the user's name and then we'll print a greeting message customized 用户名，然后我们将打印自定义的问候消息
for that user. So instead of print we're going to use 对于那个用户。因此，我们将使用而不是打印
input. Now both these input and print 输入。现在这两个输入和打印
are functions that are built into Python. As a metaphor think 是内置于Python中的函数。作为一个比喻思考
of the remote control of the TV. On this remote control we have a bunch of 电视的遥控器。在这个遥控器上我们有一堆
buttons, these are the functions built into your tv, you can turn it on, 按钮，这些是你的电视内置功能，你可以打开它，
turn it off, change the volume and so on. In Python we also have 把它关掉，改变音量等等。在Python中我们也有
functions for common tasks such as printing messages, receiving input, 用于常见任务的功能，如打印消息，接收输入，
and so on. So we're going to use the input function, now whenever we 等等。所以我们现在只要我们使用输入函数
have these parenthesis, we're going to say we're calling or executing 有这些括号，我们会说我们正在打电话或执行
that function, it's like pressing a button on a remote control. So we're 这个功能，就像按遥控器上的按钮一样。所以我们是
going to call the input function and in between parenthesis 将调用输入函数并在括号之间
we want to add a string to print something on the terminal, 我们想添加一个字符串来在终端上打印一些东西，
what is your name? With a question mark followed by 你叫什么名字？随后是问号
a space. You will see why in a second. So this input function 空间。你会在一秒钟内看到原因。所以这个输入功能
will print this message on the terminal, and then it will wait for the user to 将在终端上打印此消息，然后它将等待用户
enter a value. Whatever the user enters this input function will 输入一个值。无论用户输入什么输入功能都将
return. So now we can get that value and store it in the memory using a variable. 返回。所以现在我们可以使用变量获取该值并将其存储在内存中。
So we get the result and put it in a variable called 所以我们得到结果并将其放在一个名为的变量中
name. Okay? Now on the second line 名称。好的？现在在第二行
we want to print a message like Hi John or Hi Mosh or whatever, so, 我们想打印像Hi John或Hi Mosh等消息，所以，
print, quotations Hi with a space, 打印，报价嗨带空格，
now after the quotation we want to dynamically print 现在报价后我们要动态打印
what we have in the name variable. So we had 我们在名称变量中有什么。所以我们有
a plus sign and then name. So here we have 加号然后命名。所以我们有
Hi which is a string, we're concatenating or 嗨这是一个字符串，我们连接或
combining the string with another string, that is what we have 将字符串与另一个字符串组合，这就是我们所拥有的
in the name variable. So here's another example of 在名称变量中。所以这是另一个例子
an expression. Remember what is an expression? It's a piece of code that 一种表达。还记得什么是表达吗？这是一段代码
uses a value. So this expression concatenates or combines 使用一个值。所以这个表达式连接或组合
2 strings. Let's run this program and see what happens. So run 2个字符串。让我们运行这个程序，看看会发生什么。所以跑
okay, here is a question, what is your name? Mosh, 好的，这是一个问题，你的名字是什么？ MOSH，
now note that earlier we added a space after the question mark, we 现在请注意，我们之前在问号后添加了一个空格
did this, so here in the terminal window the cursor is 这样做，所以在终端窗口中光标是
separated from the question mark, otherwise it would be so close. So let's type 与问号分开，否则它会如此接近。所以我们打字吧
whatever here, plus enter, now we get this message, Hi, Mosh. 无论在这里，再加上输入，现在我们收到这条消息，嗨，莫什。
Now here's a little exercise for you. I want you to extend this program 现在，这是一个适合你的小练习。我希望你扩展这个程序
and ask two questions. First all the person's name and then 并问两个问题。首先是所有人的名字然后
their favorite color. And then print a message like Mosh likes 他们喜欢的颜色。然后打印像Mosh喜欢的消息
blue. So pause the video, do this exercise and then come back and continue watching. 蓝色。所以暂停视频，做这个练习，然后回来继续观看。
Alright, so here's the first question right after that, 好吧，所以这是第一个问题，在那之后，
all the input functions one more time, this time we're going to ask a different question. 所有的输入功能再一次，这次我们要问一个不同的问题。
What is your favorite color? 什么是你最喜欢的颜色？
Now, we get the new value and store it in the variable 现在，我们获取新值并将其存储在变量中
called color, or you could call it favorite underline. 称为颜色，或者你可以称之为喜欢的下划线。
color. Either works. And finally we're going to change 颜色。要么有效。最后我们要改变
what we pass to the print function, so first we print the name 我们传递给print函数，所以首先打印名称
then we concatenate this with a string, here we're going to 然后我们用字符串连接它，我们将在这里
type likes, we also put one space before and 类型喜欢，我们之前也放了一个空格
after likes, and once again we concatenate this. 在喜欢之后，我们再一次将它连接起来。
With the favorite color. So, favorite color, 随着喜欢的颜色。所以，最喜欢的颜色，
now let's run this program, so what is your name 现在让我们运行这个程序，你叫什么名字
Mosh enter, favorite color, blue, enter, Mosh进入，最喜欢的颜色，蓝色，输入，
we get this message, Mosh likes blue. 我们得到这个消息，莫什喜欢蓝色。
Hey guys Mosh here, I just wanted to let you know that 嘿伙计莫什在这里，我只是想让你知道
you really don't have to memorize anything in this course because I've put together a cheat sheet 你真的不需要记住这门课程中的任何内容，因为我已经整理了一份备忘单
with summary notes. So you can quickly review the materials in this course. 附有摘要说明。因此，您可以快速查看本课程中的资料。
The link is below this video. So I have done my best to create 该视频下方的链接。所以我尽力去创造
the best possible Python course on Youtube. And I really appreciate Youtube上最好的Python课程。而且我真的很感激
it if you support my hard work by liking this video and sharing it with 如果你支持我的辛勤工作，喜欢这个视频并与之分享
others, so they can learn as well. And be sure to subscribe to my channel, for more 其他人，所以他们也可以学习。请务必订阅我的频道，以获取更多信息
tutorials like this. Alright, now, let's move onto the next tutorial. 像这样的教程。好的，现在，让我们进入下一个教程。
In this Python tutorial, we're going to write a program that will ask the year that we were born 在这个Python教程中，我们将编写一个程序，询问我们出生的那一年
in, and then it will calculate our age and print it on the terminal. in，然后它将计算我们的年龄并在终端上打印。
So, let's start with our input function, input 那么，让我们从输入函数输入开始吧
let's print birth here, followed by a colon, 让我们在这里打印出生，接着是冒号，
and a space. Now let's get the return value and store it in a variable, 和一个空间。现在让我们获取返回值并将其存储在变量中，
called birth_year. 叫做birth_year。
So as I told you before, we use an underscore to separate multiple words 正如我之前告诉过你的那样，我们使用下划线来分隔多个单词
Next, we need to calculate the age, so we define another 接下来，我们需要计算年龄，所以我们定义另一个
variable called age, and here we do some basic math, 变量叫做年龄，这里我们做一些基本的数学，
currently we are in 2019, so let's write an expression 目前我们在2019年，所以让我们写一个表达式
like this. 2019 - birth 像这样。 2019年 - 出生
year. Now finally let's print 年。现在终于打印了
age on the terminal. Let's run our program and see what happens. 在终端上的年龄。让我们运行我们的程序，看看会发生什么。
So, my birth year is 1982, enter, 所以，我的出生年份是1982年，进入，
oops, we got an error, what is going on here? So 哎呀，我们得到了一个错误，这里发生了什么？所以
whenever you see this message, that means there is something wrong with your program. With the information 无论何时看到此消息，都表示您的程序出现问题。随着信息
here, we can find exactly where the error occurred. So, 在这里，我们可以准确找到错误发生的位置。所以，
next to the file you can see the file that generated this error, in this case 在这种情况下，您可以在文件旁边看到生成此错误的文件
that is app.py. So 这是app.py.所以
currently our program only has a single file, but real complex programs often 目前我们的程序只有一个文件，但通常是真正复杂的程序
have hundreds or even thousands of files. So in this file, 有数百甚至数千个文件。所以在这个文件中，
on line 2, this is where we got this error, and right 在第2行，这是我们得到这个错误的地方，对
below that we can see the piece of code that generated this error. 下面我们可以看到生成此错误的代码段。
So that is where we're calculating the age, and right below 这就是我们计算年龄的地方，就在下面
that you can see the type of error. In this case, we have a type error, and here's the 你可以看到错误的类型。在这种情况下，我们有一个类型错误，这里是
message. Unsupported operand types for 信息。不支持的操作数类型
subtraction. Int and str. 减法。 Int和str。
So int is short for integer and that represents a whole number 因此int是整数的缩写，表示整数
and str is short for string, so here we're subtracting 而str是字符串的缩写，所以这里我们减去
a string from an integer, and Python doesn't know what to do with it. 一个整数的字符串，Python不知道如何处理它。
Let me explain. So I'm going to close the terminal window. So after 让我解释。所以我要关闭终端窗口。所以之后
the first line we executed we have this birth year variable set 我们执行的第一行我们有这个出生年份变量集
to a string, so whatever we type in the terminal is always 到一个字符串，所以我们在终端中输入的内容总是如此
treated as a string, even if you type the number, in other words, 被视为字符串，即使你键入数字，换句话说，
when we run this program, this birth year variable will be set 当我们运行这个程序时，将设置这个出生年份变量
to a string, with four characters. 到一个字符串，有四个字符。
1982. This string is different from the actual number 这个字符串与实际数字不同
1982. One is an integer and the other is a string. 1982.一个是整数，另一个是字符串。
Right? So, back to line 2, where this 对？所以，回到第2行，在哪里
error occurred. At run time, which means when we run our program 发生了错误。在运行时，这意味着我们运行我们的程序
this expression on the right side of the assignment operator 这个表达式位于赋值运算符的右侧
is going to look like this. 2019 - 会是这样的。 2019年 -
string 1982. Python doesn't know how to 字符串1982. Python不知道如何
interpret or how to evaluate this expression. To fix this problem 解释或如何评估这个表达式。解决这个问题
we need to convert this 1982 into an integer 我们需要将1982年转换为整数
and then we'll be able to subtract it from 2019 and that 然后我们将能够从2019年减去它
is easy. So far you have learned about two built in functions 简单。到目前为止，您已经了解了两个内置函数
one is print the other is input. We have a few other functions 一个是打印，另一个是输入。我们还有一些其他功能
for converting values into different types. So we have 用于将值转换为不同的类型。所以我们有
int for converting a string into an integer, we also have int用于将字符串转换为整数，我们也有
float for converting a string into a float, or a number float用于将字符串转换为float或数字
with a decimal point. And we also have bool for converting a string 带小数点。我们也有bool转换字符串
into a boolean value. So to fix this problem, 转换为布尔值。所以要解决这个问题，
we need to go back on line 2, 我们需要回到第2行，
and pass this birth year variable to the int function like this. 并将此生成年份变量传递给int函数，就像这样。
int parenthesis, like this int括号，像这样
so we pass this string to the int function, int will convert it into an itneger 所以我们将这个字符串传递给int函数，int将它转换为itneger
and then Python interpreter will be able to evaluate this expression. 然后Python解释器将能够评估此表达式。
Now let's run this program one more time, so birth 现在让我们再一次运行这个程序吧
year is 1982 enter so I am 一年是1982年进入所以我
37 years old. In Python we have a useful function 37岁。在Python中，我们有一个有用的功能
for getting the type of variables, for example, let's print the type 例如，为了获取变量的类型，让我们打印类型
of birth year, so right after line 1, 出生年份，所以在第1行之后，
let's print, now here we're going to call another built in function, 让我们打印，现在我们要打电话给另一个内置函数，
called type, and now let's pass birth 叫做类型，现在让我们过世
year, okay, now similarly after line 一年，好吧，现在同样排队
3, let's also print the type of age, so 3，我们也打印年龄的类型，所以
print type of age. 印刷年龄。
Okay? So let's run our program, so birth year one more 好的？所以，让我们运行我们的计划，让诞生一年
time, 1982, okay, here's the result 时间，1982年，好的，这是结果
so the type of birth year as you can see is a class 所以你可以看到出生年份的类型是一个班级
of str or strings, we look at classes in the future so str或字符串，我们将来看看类
for now don't worry about them, and also below them you cans ee the type of 现在不要担心它们，而且在它们之下，你可以使用它们的类型
the age variable is int or integer. So age变量是int或integer。所以
here's what you need to take away. Whenever you use the input function, 这就是你需要带走的东西。无论何时使用输入功能，
you always get a string, so if you're expecting a numerical value 你总是得到一个字符串，所以如果你期望一个数值
you should always convert that string into an integer or 你应该总是将该字符串转换为整数或
a float. So here's a little exercise for you. I want you to write a program 一个浮子。所以这里有一点练习。我要你写一个程序
ask the user their weight and then convert it to kilograms and print it 询问用户他们的体重，然后将其转换为公斤并打印出来
on a terminal. So pause the video, do the exercise and when you're ready come back 在终端上。所以暂停视频，做运动，当你准备好回来
to watch it. 看它。
Alright so let's use our input function and ask 好吧，让我们使用我们的输入功能并询问
for the weight in pounds 以磅为单位的重量
here we get the weight in lbs or pounds 在这里，我们得到磅或磅的重量
now we need to convert this into kilograms, it's very easy so 现在我们需要将它转换成千克，这很容易
we defined another variable weight_kg 我们定义了另一个变量weight_kg
we set this to weight_lbs 我们将其设置为weight_lbs
times 0.45. 时间0.45。
And finally let's print weight underline 最后让我们打印重量下划线
kg. Let's run this Python program and see what happens. So 公斤。让我们运行这个Python程序，看看会发生什么。所以
my weight is 160, alright once again we got an error, 我的体重是160，好吧再一次我们得到了一个错误，
can't multiply sequence by non int of type 不能将序列乘以非int类型
float. So as I told you before, this input function returns 浮动。正如我之前告诉过你的那样，这个输入函数会返回
a string, so we cannot multiply a string by a float. 一个字符串，所以我们不能用float乘以一个字符串。
Python doesn't know what to do with it. So in this case, we should convert this number Python不知道如何处理它。所以在这种情况下，我们应该转换这个数字
by an integer or float and then multiply by 0.45. 用整数或浮点数再乘以0.45。
So let's call the int function. 所以让我们调用int函数。
And pass weight underline lbs. 并通过重量下划线lbs。
And run our program one more time, 160 再次运行我们的程序，160
okay, so I am 72 kg's. 好的，所以我72公斤。
In this tutorial, you're going to learn more about Python strings. 在本教程中，您将学习更多有关Python字符串的知识。
So I've defined this course variable and set it to Python for beginners 所以我已经定义了这个课程变量，并为初学者设置了Python
now earlier I told you you could use both single and double quotes 现在我告诉过你，你可以使用单引号和双引号
to define a string, but there are times you have to use a specific form, 定义一个字符串，但有时你必须使用一个特定的表单，
otherwise you're going to run into issues. Here's an example. Imagine you wanted to 否则你会遇到问题。这是一个例子。想象一下你想要的
change this string into Pythons course for Beginners. 将此字符串更改为初学者的Pythons课程。
So we want to add an apostrophe, like this, 所以我们要添加一个撇号，像这样，
Course for Beginners. You can immediately say this is going 初学者课程。你可以马上说这是事实
crazy, because our string starts here and then terminates here, 疯了，因为我们的字符串从这里开始然后在这里终止，
all these characters that we have here after the second apostrophe 在第二个撇号后我们在这里所有这些字符
Python interpreter doesn't know what they are. So to solve this problem Python解释器不知道它们是什么。所以要解决这个问题
we need to use double quotes to define our string so we can have 我们需要使用双引号来定义我们的字符串，以便我们可以
a single quote in the middle of the string. So let's change this 字符串中间的单引号。所以让我们改变这一点
to double quotes, now you can see it adds another double quote to close it, 加双引号，现在你可以看到它添加另一个双引号来关闭它，
you have to manually remove this, and also one more time 你必须手动删除它，还有一次
at the beginning of the string, we need to add another double quote. Now 在字符串的开头，我们需要添加另一个双引号。现在
you can see error is gone, so if you print course 你可以看到错误消失了，所以如果你打印课程
we see Python course for beginners. 我们为初学者看了Python课程。
Beautiful. Now let's say we don't want this apostrophe here, so we have 美丽。现在让我们说我们不希望这个撇号，所以我们有
Python for Beginners, but we want to put Beginners in Python for Beginners，但我们想把初学者放进去
double quotes. Once again, if you add a double quote here Python interpreter gets 双引号。再一次，如果你在这里添加双引号Python解释器获取
confused because it assumes the second double quote indicates that 困惑，因为它假定第二个双引号表示
end of the string, so it doesn't know what these characters are, so to solve this 字符串的结尾，所以它不知道这些字符是什么，所以要解决这个问题
we need to change our double quotes to single quotes 我们需要将双引号更改为单引号
like this. And then we can add double quotes in the middle 像这样。然后我们可以在中间添加双引号
of the string. Now let's run this program, there you go. 的字符串。现在让我们运行这个程序吧。
So we get Python for Beginners. So these are the cases 所以我们得到Python for Beginners。所以这些都是案例
for using single or double quotes. Now in all the examples 使用单引号或双引号。现在在所有的例子中
I've shown you so far we only deal with short strings, but what if you wanted to define 到目前为止，我已经向你展示了我们只处理短字符串，但是如果你想定义的话
a string that is multiple lengths? For example, what if you wanted to define a string 一个多长度的字符串？例如，如果要定义字符串，该怎么办？
for the message that we send in an email. In that case we need to use 对于我们通过电子邮件发送的消息。在那种情况下，我们需要使用
triple quotes. So. We delete this. 三重报价。所以。我们删除了这个。
Now we add three quotes, so 1, 2, 3, there you go, 现在我们添加三个引号，所以1,2,3，你去了，
So, we have three quotes to start our string and three 所以，我们有三个引号来启动我们的字符串和三个
to terminate it. Again these quotes can be single or double quotes. 终止它。同样，这些引号可以是单引号或双引号。
Okay? Now, with this we can define a string that spans 好的？现在，通过这个我们可以定义一个跨越的字符串
multiple lines. For example, we can say Hi Jon 多行。例如，我们可以说Hi Jon
here is our first email to you. Thank you, 这是我们给您的第一封电子邮件。谢谢，
The Support Team. Like that. 支持团队。像那样。
Now, let's run this program and here's the 现在，让我们运行这个程序，这就是
result. So, we get this beautiful multi line 结果。所以，我们得到了这条美丽的多线
string. Now let's change this back to something simple 串。现在让我们把它改成简单的东西
so, we can look at other characteristics of strings and Python. 所以，我们可以看一下字符串和Python的其他特性。
So I'm going to use single quotes and set the course name to Python 所以我将使用单引号并将课程名称设置为Python
for Beginners. Now here we're going to use square 对于初学者。现在我们将使用square
brackets to get a character and a given index in this string. 用于获取此字符串中的字符和给定索引的括号。
Let me show you. So to get the first character we use square brackets 让我演示给你看。因此，要获得第一个字符，我们使用方括号
and type 0. So the index of the first character 并键入0.所以第一个字符的索引
in the string is 0. In other words, this is how Python 在字符串中是0.换句话说，这就是Python的方式
strings are indexed. 0, 1, 2, 3, 4, etc. 字符串被索引。 0,1,2,3,4等
So the index of the first character is 0, the second character is 所以第一个字符的索引是0，第二个字符是
1, and so on. So let me delete this and run this program 1，依此类推。所以让我删除它并运行这个程序
we get p. We can also use a negative 我们得到了p。我们也可以使用否定的
index here. And this is one of the features that we don't have in other programming languages 索引在这里。这是我们在其他编程语言中没有的功能之一
as far as I know. So we have negative index we can get the characters 我所知道的。所以我们有负面的索引，我们可以得到字符
started from the end. So if I pass negative 1 here, 从最后开始。所以，如果我在这里传递负面1，
Assuming that 0 is the index of the first character 假设0是第一个字符的索引
negative 1 is the index of the last character. So when we run 负1是最后一个字符的索引。所以，当我们跑
this program we should see s. Let's run it, there you go, 这个程序我们应该看到s。我们去吧，你去吧
we get s, if we pass negative 2, this will return 我们得到s，如果我们通过负2，这将返回
the second character from the end. Let's run it one more time, 从结尾开始的第二个角色。让我们再跑一次，
now we get R because that is the second character from the end. 现在我们得到R因为这是结尾的第二个字符。
Okay? So place close attention to this square brackets syntax 好的？因此请密切关注此方括号语法
because quite often it's the topic for online Python tests 因为它经常是在线Python测试的主题
or university exams, so if you're preparing for a python test, 或大学考试，所以如果你准备进行python测试，
make sure to watch this tutorial one more time and understand exactly how 请务必再次观看本教程，并确切了解具体方法
this square brackets syntax works, we can also use a similar 这个方括号语法有效，我们也可以使用类似的
syntax to extract a few characters instead of 1 character. 语法提取几个字符而不是1个字符。
For example, if we type 0, 例如，如果我们输入0，
colon 3, Python interpreter will return all the characters 冒号3，Python解释器将返回所有字符
starting with this index all the way to this 从这个索引开始一直到这个
second index, but it does not return the character at this index. 第二个索引，但它不返回此索引处的字符。
In other words, back to these indexes 换句话说，回到这些索引
so you have 0, 1, 2, 3, and so on. When you run this program. 所以你有0,1,2,3等等。当您运行此程序时。
Python interpreter will return the characters starting from the Python解释器将返回从开头的字符
index 0 all the way to index 3, 索引0一直到索引3，
but excludes the character and index 3, 但不包括角色和指数3，
so when we run this Python program we're going to see pint (?). Let me show you, so we're going to 所以当我们运行这个Python程序时，我们将看到pint（？）。让我告诉你，所以我们要去
delete this line, run this program, there you go. 删除这一行，运行这个程序，你去。
We get pint. Now here we also have default values for 我们得到品脱。现在我们也有默认值
the start and end index. So if we don't supply 开始和结束索引。所以，如果我们不供应
the end index, Python will return all the characters to the end of the string. 在结束索引中，Python会将所有字符返回到字符串的末尾。
Let's take a look. So run this program, there you go, 让我们来看看。所以运行这个程序，你去，
Python for Beginners. But if you change the start index to 1, Python初学者。但是如果将起始索引更改为1，
this will exclude the first character so when we 这将排除第一个字符，所以当我们
run this program, we see ython so p is removed. 运行这个程序，我们看到ython所以p被删除了。
Okay? Now similarly we have a default value for the 好的？现在同样我们有一个默认值
start index, so if we don't supply the start index 启动索引，所以如果我们不提供启动索引
but add an end index like 5. Python interpreter will assume 但添加一个像5这样的结束索引.Python解释器将假设
0 as the start index, so, let's run 0作为起始索引，所以，让我们运行
this program, there you go, we get pytho. 这个程序，你去，我们得到pytho。
Now what if we leave both the start and end 现在如果我们离开开始和结束都会怎样
index? Well, I told you? Now in this case 0 will be assumed 指数？好吧，我告诉过你了？现在在这种情况下将假设为0
as the start index, and the length of the string will assume 作为起始索引，字符串的长度将采用
as the end index. So with this syntax, you can basically copy 作为最终指数。因此，使用此语法，您基本上可以复制
or clone a string. In other words, if I define 或克隆一个字符串。换句话说，如果我定义
another variable here, let's call it another and set it to course 另一个变量在这里，我们称之为另一个变量并将其设置为路线
square brackets with just a colon, now this expression will 方括号只有一个冒号，现在这个表达式会
return all the characters in the course variable so 返回课程变量中的所有字符
variable will be copy of our first variable. Let's take a look, so, 变量将是我们的第一个变量的副本。我们来看看，所以，
let's print another, and load our 让我们打印另一个，加载我们的
program there you go, we get Python for beginners. So once again 你去的程序，我们为初学者提供Python。再一次
the square bracket syntax is pretty important if you're preparing for online 如果您正在准备在线，方括号语法非常重要
python tests, or college exams, make sure to watch this tutorial again. python测试，或大学考试，请务必再次观看本教程。
Now here's a little exercise for you. I'm going to delete all this code 现在，这是一个适合你的小练习。我要删除所有这些代码
define a variable, called name, and set it to Jennifer. 定义一个名为name的变量，并将其设置为Jennifer。
Now when we print name 现在当我们打印名字
of 1: negative 1 1：负1
what do you think we're going to see on the terminal? I want you to use your knowledge 您认为我们将在终端上看到什么？我希望你运用你的知识
to tell what we're going to see on the terminal, we're now running this program. So 告诉我们将在终端上看到什么，我们现在正在运行这个程序。所以
pause the video, think about it for a few seconds, then come back and continue watching. 暂停视频，想想它几秒钟，然后回来继续观看。
So this expression will return the characters starting from 所以这个表达式将返回从中开始的字符
index 1 which is the second character 索引1是第二个字符
all the way to the first character from the end, but excluding 一直到最后的第一个角色，但不包括在内
the character at this index. In this case, the first character from the 这个索引的字符。在这种情况下，第一个字符来自
in is r, so r will be excluded, in other words we're going to see 在r中，所以r将被排除在外，换句话说我们将会看到
all the characters starting from e all the way 从e开始的所有角色
to the second e. Let's take a look. So I'm going to run this program 到第二个e。让我们来看看。所以我要运行这个程序
there you go. This is what we get, 你去吧这就是我们得到的，
I hope you guessed it right. 我希望你猜对了。
In this tutorial, we're going to look at 在本教程中，我们将看一下
formatted strings in Python programming language. Formatted strings are Python编程语言中的格式化字符串。格式化的字符串是
particularly useful in situations where you dynamically generate some 在动态生成某些内容的情况下特别有用
text with your variables. Let me show you. Let's say we have two variables 带有变量的文本。让我演示给你看。假设我们有两个变量
first name and last name. So first we set this to 名字和姓氏。所以首先我们将其设置为
John, and last we set this to Smith. Now it's better 约翰，最后我们把它设置为史密斯。现在好多了
to call these variables first name and last name, because they're more 将这些变量称为名字和姓氏，因为它们更多
descriptive. But here I'm using shorter names because I want you to see the entire code on display 描述。但在这里我使用较短的名字，因为我希望你看到显示的整个代码
screen. So let's say with these two variables, we want to generate 屏幕。让我们说这两个变量，我们想要生成
some text like this. Jon, in square brackets 这样的一些文字。乔恩，方括号内
Smith is a coder. Let's say we want to print this on 史密斯是一名编码员。假设我们要打印这个
the terminal. How do we do this? Well, we define another variable 终点站。我们如何做到这一点？好吧，我们定义另一个变量
like message, now here we add the first name, 喜欢消息，现在我们在这里添加名字，
now we need to concatenate this, with a string that contains a space 现在我们需要使用包含空格的字符串来连接它
and a square bracket next we need to add a last name, 然后我们需要添加姓氏，方括号
then we need to add a string that contains 然后我们需要添加一个包含的字符串
the closing square brackets followed by is a coder 后面的方括号是一个编码器
okay? So, then, 好的？那么，那么，
if you print message and run this program 如果您打印消息并运行此程序
to see John Smith is a coder, right? Now, 看约翰史密斯是一个程序员，对吧？现在，
while this approach perfectly works, it's not ideal 虽然这种方法完美有效，但并不理想
because as our text gets more complicated it becomes harder to 因为随着我们的文本越来越复杂，它变得越来越难
visualize the output. So someone else reading this code, they have to 可视化输出。所以别人读这段代码，他们必须这样做
visualize all the string concatenations in their head. This is where we use 可视化头部中的所有字符串连接。这是我们使用的地方
formatted strings, they make it easier for us to visualize the output. So, 格式化字符串，它们使我们更容易可视化输出。所以，
I'm going to define another variable, let's say 我要定义另一个变量，比方说
msg short for message, and set this to a formatted string. A formatted msg信息简短，并将其设置为格式化字符串。格式化
string is one that is prefixed with an f. So f, string是一个以f为前缀的字符串。所以f，
quotes. Now in between the quotes, first we want to add 引号。现在在引号之间，首先我们要添加
the value of the first name variable, so, 第一个名称变量的值，所以，
we add curly braces and here we type first. 我们添加花括号，在这里我们先键入。
Next we add a space, we add our square brackets, in between the square 接下来我们添加一个空格，我们在方块之间添加方括号
brackets, we want to display the last name so once again we add curly 括号，我们要显示姓氏，所以我们再次添加卷曲
braces, and type last, and finally here we type 大括号，键入最后，最后在这里输入
is a coder. So this is what we call the formatted 是一个程序员。所以我们称之为格式化
string. With these curly braces, we're defining place holders 串。有了这些花括号，我们就定义了占位符
or holes in our string, and when we run our program these holes 或者我们的字符串中的洞，当我们运行我们的程序这些洞
will be filled with the value of our variables. So here we have two place holders 将填充我们变量的值。所以这里有两个占位符
or two holes in our string. One is for the value of our first name 或者我们的弦上有两个洞。一个是我们名字的价值
variable and the other is for the value of the last name variable. 变量，另一个是姓氏变量的值。
But compare this formatted string with string 但是将此格式化的字符串与字符串进
concatenation. With this formatted string we can easily visualize 级联。使用这种格式化的字符串，我们可以轻松地进
what the output looks like, right? Now let's print this other 输出是什么样的，对吧？现在让我们打印另一个
terminal to make sure we get the same exact output. So, let's print message 终端，以确保我们得到相同的确切输出。那么，让我们打印消息
there you go. So Jon 你去吧乔恩
Smith is a coder. So to define formatted strings, 史密斯是一名编码员。所以要定义格式化字符串，
prefix your strings with an F and then use curly braces 用F作为字符串前缀，然后使用花括号
to dynamically insert values into your strings. 动态地将值插入到字符串中。
In this Python tutorial, I'm going to show you some really cool things you can do with Python 在这个Python教程中，我将向您展示一些使用Python可以做的非常酷的事情
strings. So let's start by defining a variable, 字符串。那么让我们从定义变量开始，
course and we set that to Python for 当然，我们将其设置为Python for
beginners. Now to calculate the number 初学者。现在计算数量
of characters in this string, you can use a built in function called len. 在这个字符串中的字符，你可以使用一个名为len的内置函数。
So len we give it this course variable, and then, 所以len我们给它这个课程变量，然后，
we can print the result. Let's run this program, 我们可以打印结果。我们来运行这个程序，
so as you can see we have 20 characters in this string, this is 所以你可以看到我们在这个字符串中有20个字符，这是
particularly useful when you receive input from the user. For example you have 当您收到用户的输入时特别有用。比如你有
noticed that when you fill out a form online, each input field 注意到当你在线填写表格时，每个输入字段
often has a limit. For example, you might have 50 characters for your name, 经常有一个限制。例如，您的姓名可能包含50个字符，
so using this len function we can enforce a limit on the number of characters 所以使用这个len函数我们可以强制限制字符数
in an input field. If the user types in more characters than we allow, 在输入字段中。如果用户输入的字符多于我们允许的字符数，
we can display an error, now, this, 我们可以显示错误，现在，这个，
len function is another function built into Python, it's len函数是Python内置的另一个函数
a general purpose function, so it's not limited to counting the number of characters in a 通用函数，因此它不仅限于计算a中的字符数
string, in the future when we look at lists, I want to show you that we can use this 字符串，将来当我们查看列表时，我想告诉你我们可以使用它
function to count the number of items in a list. So it's a general purpose 用于计算列表中项目数的函数。所以这是一个普遍的目的
function. Now we also have functions specifically for strings 功能。现在我们还有专门针对字符串的函数
for example we have functions for converting all these characters to upper case 例如，我们有将所有这些字符转换为大写的函数
or lower case. To access these functions we use the dot operator. 或小写。要访问这些函数，我们使用点运算符。
let me show you. So first we type course, 让我演示给你看。首先我们输入课程，
then dot look these are all the functions 然后点看这些都是功能
that are specific to strings. Now in more accurate terms, 特定于字符串。现在用更准确的术语来说
you refer to these function as methods, this is a term 你将这些函数称为方法，这是一个术语
in object oriented programming that we want to look at in the future, but for now, what I want you to take away, 在面向对象的编程中，我们希望将来看一下，但就目前而言，我希望你带走的东西，
is that when a function belongs to something else, or is specific 当一个函数属于别的东西，或者是特定的
to some kind of object, we refer to that function as a method. 对于某种对象，我们将该函数称为一种方法。
For example, here we have this function, upper, for converting the string 例如，这里我们有这个函数upper，用于转换字符串
into upper case, now more accurately because this function 进入大写，现在更准确，因为这个功能
is specific to a string, we refer to this as a method. In contrast 特定于字符串，我们将其称为方法。相反
len and print are general purpose functions, they don't belong len和print是通用功能，它们不属于
to strings or numbers or other kinds of objects. 字符串或数字或其他类型的对象。
So this is the difference between functions and methods. Now let's take look at this 所以这是功能和方法之间的区别。现在我们来看看这个
upper method. So, let's print the results 上层方法。那么，让我们打印结果
and we run our program, there you go, 我们运行我们的程序，你去，
you get all these characters displayed in uppercase. Now note this method 你得到所有这些以大写字母显示的字符。现在请注意这种方法
does not change or modify our original string, in fact 事实上，它不会改变或修改我们的原始字符串
it creates a new string and returns it. So, 它会创建一个新字符串并返回它。所以，
if we print our course variable right after we 如果我们在我们之后立即打印我们的课程变量
call the upper method, we can see that our course variable still 调用upper方法，我们可以看到我们的课程变量仍然存在
has it's original form, so let's run this program one more time, there you 有它的原始形式，所以让我们再次运行这个程序，在那里
go. Look, here is our original course variable, it's not modified. 走。看，这是我们原来的课程变量，它没有被修改。
Now similar to the upper method we have another method 现在类似于上层方法，我们有另一种方法
for converting a string into lower case. So let me show you. 用于将字符串转换为小写。所以，让我告诉你。
Print, course.lower. 打印，course.lower。
Now, let's run the program, so on the second line you can see, 现在，让我们运行程序，所以在第二行你可以看到，
all characters are in lower case. Now there are times 所有字符均为小写。现在有时候了
that you want to find a character or a sequence of characters in a string. 你想在字符串中找到一个字符或一系列字符。
In those situations you can use the find method. So let me delete 在这些情况下，您可以使用find方法。所以让我删除
these few lines. Call course. 这几行。通话课程。
find here we pass a character, let's say 在这里我们传递一个角色，让我们说
p, and this will return the index of the first occurrence p，这将返回第一次出现的索引
of that character, let me show you. So let's print the result 那个角色，让我告诉你。所以让我们打印结果
we get 0, because the 我们得到0，因为
index of the first capital p in the string is 0. 字符串中第一个大写字母p的索引是0。
As another example, if we pass a lower case o here, 另一个例子，如果我们在这里传递小写字母o，
let's see what we get, we get 4 because 让我们看看我们得到了什么，我们得到4因为
the index of this o here is 4. Now note that the 这里的o的索引是4.现在请注意
find method is case sensitive, so it's sensitive to find方法区分大小写，因此它对
lower case and and upper case characters. As an example if you pass an upper case 小写和大写字符。例如，如果你传递大写
here and run this program, we get negative 1 because 在这里运行这个程序，我们得到负面因为1
we don't have an upper case o anywhere in this string, okay? 我们在这个字符串的任何地方都没有大写o，好吗？
We can also pass a sequence of characters, for example, we can pass 我们也可以传递一系列字符，例如，我们可以通过
beginners with a capital B 有资本B的初学者
let's run this program, we get 11 because 让我们运行这个程序，我们得到11因为
beginners starts with index 11, now we 初学者从索引11开始，现在我们
also have method for replacing a character or a sequence of characters 还有替换字符或字符序列的方法
and that is called replace. So let's change find 那被称为替换。所以让我们改变发现
to replace let's say we want to replace beginners with 替换让我们说我们想要替换初学者
absolute beginners, so we add a comma to pass a second value 绝对的初学者，所以我们添加一个逗号来传递第二个值
to this function, or more accurately this method. We add a string, 对这个功能，或者更准确地说这个方法。我们添加一个字符串，
here I'm going to pass absolute beginners. 在这里，我将通过绝对的初学者。
Okay, now let's run this program, 好的，现在让我们运行这个程序，
so, we get python for absolute beginners. 所以，我们得到绝对初学者的python。
Again, this method like the find method is case sensitive so 同样，像find方法这样的方法也区分大小写
if you pass beginners all in lowercase, this method is 如果你以小写形式传递所有初学者，这个方法是
not going to find this exact word in our string, so it's not going to 不会在我们的字符串中找到这个确切的单词，所以它不会
place it with absolute beginners. Let's take a look. So I'm going to run the program 把它放在绝对的初学者。让我们来看看。所以我要运行该程序
One more time, look, we still get python for beginners. 再一次，看，我们仍然为初学者获得python。
We can also replace a single character, for example we can replace 我们也可以替换单个字符，例如我们可以替换
capital p with let's say capital 我们说资本是资本
j. Now when we run this program we get 学家现在我们运行这个程序时，我们得到了
jython for beginners. So these are the find and replace methods jython适合初学者。所以这些是查找和替换方法
and one last things I want to show you in this tutorial. There are 以及我想在本教程中向您展示的最后一件事。有
times that you want to check the existence of a character or sequence of characters in your 你要检查你的角色或角色序列是否存在的时间
string. In those situations you use the in operator, 串。在那些情况下，你使用in运算符，
so let's say you want to know if this string contains the word python. 所以，假设你想知道这个字符串是否包含单词python。
We can write an expression like this. String 我们可以写一个这样的表达式。串
python space in space course. 太空课程中的python空间。
So we're checking to see if python is in 所以我们正在检查python是否在
course variable. And this is an expression that produces a boolean 课程变量。这是一个产生布尔值的表达式
value, and I get true or false, so we refer to this expression 值，我得到真或假，所以我们引用这个表达式
as a boolean expression, now if we print this on the terminal, 作为布尔表达式，现在如果我们在终端上打印它，
we should get true, and by the way 我们应该成真，顺便说一下
I'm going to delete the second line, we don't need it anymore, so run the program 我要删除第二行，我们不再需要了，所以运行程序
we get true, but if I change this capitol p to a lower case 我们变成了现实，但是如果我将这个国会大厦改为小写的话
p and run the program we get false because we don't have p并运行我们得到的程序，因为我们没有
is exact sequence of characters in our strings. 是我们的字符串中的确切字符序列。
Now note that the difference between the in operator and the find method 现在请注意in运算符和find方法之间的区别
is that our find method returns the index of 是我们的find方法返回索引
character or sequence of characters but the in operator 字符或字符序列，但in运算符
produces a boolean value. Do we have this or not? 产生一个布尔值。我们有没有这个？
So that's the difference. Now let's recap all the cool things you learned 这就是差异。现在让我们回顾一下你学到的所有很酷的东西
to do with strings in this tutorial. We can use the len function 在本教程中使用字符串。我们可以使用len函数
to count the number of characters in a string, this is the general 要计算字符串中的字符数，这是一般的
purpose function built into python, we also have specific functions python内置的目的函数，我们也有特定的功能
for strings which we refer to as methods, these include 对于我们称之为方法的字符串，这些包括
upper for converting a string into uppercase 用于将字符串转换为大写的upper
you also have lower and title methods, 你也有低级和标题方法，
you learn about the find method which returns the index 您将了解返回索引的find方法
of a character or sequence of characters, we have 我们有一个字符或一系列字符
the replace method for replacing characters and words in a string and finally 替换方法，用于替换字符串中的字符和单词，最后
you learned about the in operator. So some characters 你了解了in运算符。所以有些人物
in a string. 在一个字符串中。
So, you have learned that in Python programming language you have 2 所以，你已经了解到在Python编程语言中你有2个
types of numbers, integers which are whole numbers like 10, they don't have 数字的类型，整数，如10，他们没有
a decimal point, and floating point numbers or floats. 小数点，浮点数或浮点数。
Which are numbers with a decimal point. Now in this tutorial you're going to look 哪个是带小数点的数字。现在在本教程中你将会看到
at the arithmetic operations supported in python language these are the 在python语言支持的算术运算中，这些是
same arithmetic operations that we have in math, we can add numbers, 我们在数学中有相同的算术运算，我们可以添加数字，
multiply them and so on. So let's look at a few examples, 乘以它们等等。那么让我们看一些例子，
we can print, 10 plus 3, so 我们可以打印，10加3，所以
this is the addition operator, we also have subtraction, we 这是加法运算符，我们也有减法，我们
have multiplication, we have two kinds of division, 有乘法，我们有两种划分，
here's one with a forward slash, let's run this program and see what we get. 这是一个正斜杠，让我们运行这个程序，看看我们得到了什么。
we get a floating point number. But we also have 我们得到一个浮点数。但我们也有
another division operator for getting an integer. So 获取整数的另一个除法运算符。所以
if we add another slash here and run this program 如果我们在这里添加另一个斜杠并运行该程序
we get an integer. We have 我们得到一个整数。我们有
another operator called modulis (?) which is a percent sign. 另一个名为modulis（？）的运算符是百分号。
And this returns the remainder of the division. So when we run this program 这将返回除法的其余部分。所以当我们运行这个程序时
we should get 1, there you go. And 我们应该得到1，你去。和
one last operator we have here is exponent which is the power. 我们这里有的最后一个算子是指数即幂。
So, that is indicated with 2 asterisks and this will return 因此，用2个星号表示，这将返回
10 to the power of 3. So let's run this program 10到3的幂。所以让我们运行这个程序
we get 1000 so these are the arithmetic operators 我们得到1000所以这些是算术运算符
in python programming language. Now for all these operators that you learned 在python编程语言中。现在为您学习的所有这些操作员
we have an augmented assignment operator. That is 我们有一个增强赋值运算符。那是
very useful, let me show you. So let's say we have a variable 非常有用，让我告诉你。所以我们假设我们有一个变量
called x we set it to 10, now we want to increment this by 调用x我们将其设置为10，现在我们想要增加它
3, we'll have to write code like this. X we set this to 3，我们必须编写这样的代码。我们将此设置为
x plus 3. So Python interpreter will add x plus 3.因此Python解释器将添加
10 to 3, the result is 13, and then it gets stored 在图10到3中，结果是13，然后它被存储
into x again. So when we print x we should see 再次进入x。因此，当我们打印x时，我们应该看到
13, there you go. So this is how you can increment 13，你去。所以这就是你可以增加的方式
a number, right? Now augmented assignment operator is 一个数字，对吗？现在扩充赋值运算符是
a way to write the same code but in a shorter form. This 一种编写相同代码但以较短形式编写的方法。这个
is how it works. We type x plus 它是如何工作的。我们输入x plus
equals 3. What we have on line 3 is exactly 等于3.我们在第3行的确切内容
like what we have on line 2. So this is what we call 就像我们在第2行所拥有的那样。所以这就是我们所说的
the augmented assignment operator we have augmented or 我们增强的扩充赋值运算符或
enhanced the assignment operator. Now in this particular 增强了赋值运算符。现在在这个特别的
case we are incremented a number using the augmented assignment operator, 我们使用扩充赋值运算符递增一个数字，
but we can also subtract or multiply a number by a given value 但我们也可以用给定的值减去或乘以一个数
for example, let's delete what we have on line 2, 例如，让我们删除第2行的内容，
we can type subtract equals 3. So here we 我们可以输入减法等于3.所以我们这里
subtracted 3 from x. When we run this program we should see 7, 从x中减去3。当我们运行这个程序时，我们应该看到7，
there you go. 你去吧
Now let me ask you a question, I'm going to clear all this code here to 现在让我问你一个问题，我将在这里清除所有这些代码
define x and set it to 10 plus 3 定义x并将其设置为10加3
times 2. What do you think is the result of this expression? 你认为这个表达的结果是什么？
this is a very basic math question that unfortunately a lot of people fail to answer. 这是一个非常基本的数学问题，不幸的是很多人都没有回答。
The answer is 16. Because in math we have this 答案是16.因为在数学中我们有这个
concept called operator precedence which means the order of 称为运算符优先级的概念，表示运算符优先级
operations. So the multiplication operator has 操作。所以乘法算子有
a higher precendence which means it's applied first which means 3 x 更高的优先级，这意味着它首先应用，这意味着3 x
2 is executed first, the result is 6 and then 首先执行2，结果是6然后执行
its added to 10, that's why x showed up as 16 after 它增加到10，这就是为什么x出现后为16的原因
we run this code, let's verify that. So, print x 我们运行此代码，让我们验证一下。所以，打印x
run the program, x is 16. So this is what we call 运行程序，x是16.所以这就是我们所说的
operator precedence, it's just a basic math concept. 运算符优先级，它只是一个基本的数学概念。
It's not about python programming language. So all the other programming languages 这不是关于python编程语言。所有其他编程语言
behave the same way, so here's the order, first we have 行为方式相同，所以这是订单，首先我们有
the exponentiation which is the power, like 2 to the power of 3, 指数是幂，就像3的幂，2
then we have multiplication or division 然后我们有乘法或除法
and finally we have addition or subtraction. 最后我们有加法或减法。
This is the order of operations. Let me show you another example. 这是操作的顺序。让我再举一个例子。
Here I'm going to add the exponentiation operator, so, 这里我要添加指数运算符，所以，
2 to the power of 2. Once again, what do you 2到2的力量。再次，你是什么
think is the result of this expression? Pause the video and think about it for a few seconds. 认为是这个表达的结果？暂停视频并思考几秒钟。
The answer is 22. 答案是22。
Because the exponentiation operator takes precedence, so first 因为取幂运算符优先，所以首先
2 to the power of 2 is executed, the result is 4, 2执行2的幂，结果是4，
then 4 is multiplied by 3, that is 12, 然后4乘以3，即12，
and finally 12 is added to 10. So x should be 22. 最后12加到10.所以x应该是22。
So let's run this program and verify this. So I'm going to 那么让我们运行这个程序并验证这一点。所以我要去
delete these lines here. Run the program, there you go. X 在这里删除这些行。你去的地方运行程序。 X
is 22. Now let me bring back these rules here. 现在让我在这里带回这些规则。
We can also use parenthesis to change the order of operations 我们还可以使用括号来改变操作的顺序
so if we have parenthesis we always 所以，如果我们总是有括号
takes priority. In this case we can add parenthesis 优先考虑。在这种情况下，我们可以添加括号
around 10 + 3, so this piece of 3 will be 大约10 + 3，所以这3块将是
executed first, the result is 13, then the exponentiation 先执行，结果是13，然后是取幂
operator will be executed, so 2 to the power of 2 is 4, 运算符将被执行，所以2到2的幂是4，
and finally 4 is multiplied by 13. 最后4乘以13。
Now here is a little exercise for you. I'm going to set x to 现在这里有一点练习。我要把x设置为
parenthesis 2 + 3 x 10 minus 括号2 + 3 x 10减去
3. What is the result of this? Pause the video and think about it for a few seconds. 这是什么结果？暂停视频并思考几秒钟。
So you learned that parenthesis always 所以你总是学会括号
overrides the order, so this piece of code is executed first. The result 覆盖顺序，因此首先执行这段代码。结果
of these 5. Then, between the multiplication and 这些5.然后，乘法和
subtraction, you know that multiplication takes precedence. 减法，你知道乘法优先。
So next, 5 will be multiplied by 10, the result is 接下来，5将乘以10，结果是
50 and finally we have subtraction. So 50 minus 50，最后我们有减法。所以50减
3 will be 47. Let's verify this, print 3将是47.让我们验证一下，打印
x, run the program there you go, I hope you guessed it right. x，在那里运行程序，我希望你猜对了。
So this is all bout operator precedence, it's a very important topic 所以这是所有bout运算符优先级，这是一个非常重要的主题
and I see it quite often in Python tests. So if you're preparing for a Python test 我经常在Python测试中看到它。所以，如果你正在准备Python测试
make sure to watch his tutorial one more time. 一定要再看一遍他的教程。
In this tutorial, we're going to look at a few useful functions for working with numbers 在本教程中，我们将介绍一些用于处理数字的有用函数
let's start by defining a variable like x and set it to 2 让我们首先定义一个像x这样的变量并将其设置为2
.9. Now to round this number we can use the built in 0.9。现在围绕这个数字，我们可以使用内置
round function, so we call the round function, give it x, 圆函数，所以我们调用圆函数，给它x，
and then print the result. Let's run this program 然后打印结果。我们来运行这个程序
so, we get 3, we have another useful built in function called 所以，我们得到3，我们有另一个有用的内置函数调用
abs which is short for absolute, and this is the absolute function abs是绝对的缩写，这是绝对函数
we have in math, we give it a value and it always returns 我们在数学中，我们给它一个值，它总是返回
the positive representation of this value, even if the value is negative. 这个值的正面表示，即使该值为负。
Here's an example. Let's call the abs function 这是一个例子。我们来调用abs函数
and give it negative 2.9 When we run this program 并给它负面2.9当我们运行这个程序
we're gong to see 2.9 on the terminal. So let's go ahead 我们要在终端上看到2.9。让我们继续吧
there you go. So absolute always returns a positive number. 你去吧所以绝对总是返回一个正数。
But technically in Python we have a handful of built in functions for performing 但从技术上讲，我们在Python中有一些用于执行的内置函数
mathematical operations, if you want to write a program that involves complex 数学运算，如果你想编写一个涉及复杂的程序
mathematical calculations, you need to import the math module. 数学计算，您需要导入数学模块。
A module in Python is a separate file with some reusable Python中的模块是一个单独的文件，有一些可重用
code. We use these modules to organize our code into different 码。我们使用这些模块将代码组织成不同的代码
files. As a metaphor think of a super market. When you go to a super market 文件。作为一个比喻，想到一个超级市场。当你去超级市场
you see different sections for fruits and vegetables, cleaning products, 你看到不同的水果和蔬菜部分，清洁产品，
junk food and so on. Each section in the super market is like a module 垃圾食品等。超级市场中的每个部分都像一个模块
in Python. So in Python we have this math module which contains a bunch 在Python中。所以在Python中我们有这个数学模块，其中包含一堆
of rustable functions for performing mathematical calculations. 用于执行数学计算的生锈函数。
So, let me show you how to use this module. On the top 那么，让我告诉你如何使用这个模块。在顶端
we type import, math, all in lowercase 我们输入import，math，all in smallcase
with this we can import the math module. now math is 有了这个，我们可以导入数学模块。现在数学是
an object like a string, so we can access it's functions or more accurately 像字符串这样的对象，所以我们可以更准确地访问它的函数
it's methods using the dot operator. So if you type 它是使用点运算符的方法。所以，如果你输入
math. look these are all the mathematical 数学。看这些都是数学的
functions available in this module. For example you can call the 此模块中提供的功能。例如你可以打电话给
seal method to get the sealing of a number. So if you pass 2.9 here 密封方法得到一些密封。所以如果你在这里通过2.9
and then print the result we should see 3. 然后打印我们应该看到的结果3。
Let me delete all this other code here. Alright, let's run this program 我在这里删除所有这些其他代码。好吧，让我们运行这个程序
there you go. So we get 3. Another useful method is the 你去吧所以我们得到3.另一个有用的方法是
floor method, so let's give that a try, floor of 2. 地板方法，所以让我们试一试，2楼。
9. What do you think we're going to get? We get 你认为我们会得到什么？我们得到了
2. Now there are so many functions built in this module and we don't really have time 2.现在这个模块中有很多功能，我们没有时间
to go through all of them. But let me show you how we can learn about them on your own. 经历所有这些。但是，让我告诉你我们如何能够自己了解它们。
Open up your browser and search for Python 3 打开浏览器并搜索Python 3
math module. Make sure to add the version python 3. 数学模块。确保添加python 3版本。
Because the math module in python 2 is slightly different from the math module in 因为python 2中的数学模块与数学模块略有不同
python 3. So python 3 math module, python 3.所以python 3数学模块，
now here you can see the documentation of this kind of module 现在，您可以在这里看到这种模块的文档
let's go let's have a look, if you scroll down, 如果向下滚动，让我们一起来看看，
we can see the list of all the functions and their explanation. 我们可以看到所有功能的列表及其解释。
So as an exercise I encourage you to have a quick look at this 所以作为练习，我鼓励你快速浏览一下
documentation. See what functions are there for you in case you need them. 文档。如果您需要，请查看适合您的功能。
In this tutorial, I'm going to talk to you about if statements in Python 在本教程中，我将与您讨论Python中的if语句
if statements are extremely important in programming and they 如果陈述在编程中非常重要，那么它们
allow us to build programs that can make decisions based on some condition. 允许我们构建可以根据某些条件做出决策的程序。
So if some conditions are true we're going to do certain things, otherwise we're going to do other 因此，如果某些条件属实，我们会做某些事情，否则我们会做其他事情
things. Here's an example. Over here I've got this text file with 的东西。这是一个例子。在这里，我有这个文本文件
a bunch of rules for our program. If it's a hot day, perhaps we want to 我们计划的一系列规则。如果这是炎热的一天，也许我们想要
tell the user it's a hot day, so make sure to drink plenty of water. 告诉用户这是炎热的一天，所以一定要多喝水。
Otherwise, if it's cold, so here's another condition 否则，如果它很冷，那么这是另一个条件
if this condition is true we're going to tell the user it's a cold day 如果这个条件成立，我们将告诉用户这是一个寒冷的日子
so where warm clothes. And otherwise if it's 那么温暖的衣服。否则，如果是的话
either hot or cold, we want to tell the user it's a lovely day. 无论是热还是冷，我们都想告诉用户这是一个美好的一天。
So let me show you how to write a program that simulates these rules. So, 那么让我告诉你如何编写一个模拟这些规则的程序。所以，
back to our program here, we start by defining a boolean variable 回到我们的程序，我们首先定义一个布尔变量
is underline hot. We set this to true. 强调热。我们将此设置为true。
Next, we add an if statement, so if, here 接下来，我们添加一个if语句，所以如果在这里
we need to add a condition, in this case we're going to use our boolean variable. So, 我们需要添加一个条件，在这种情况下我们将使用我们的布尔变量。所以，
is underline hot. So if this values to true. 强调热。所以，如果这个值为真。
Then we're going to do certain things. In this case, we want to tell the user 然后我们要做某些事情。在这种情况下，我们想告诉用户
hey it's a hot day, drink plenty of water. So, back to our program 嘿，这是炎热的一天，多喝水。所以，回到我们的计划
after our condition we add a colon, now, 在我们的条件后，我们添加一个冒号，现在，
note that when I press enter pycharm automatically indents 请注意，当我按下输入pycharm时会自动缩进
our cursor. Now any code that we write here will be executed 我们的光标。现在我们在这里写的任何代码都将被执行
if this condition is true, otherwise it will be ignored. Here 如果此条件为真，否则将被忽略。这里
is an example. Let's write a print statement, here I'm going to use 就是一个例子。让我们写一个打印声明，我将在这里使用
double quotes because I want to use an apostrophe in our string. So, 双引号因为我想在字符串中使用撇号。所以，
it's a hot day. Now let's press enter 这是炎热的一天。现在让我们按回车键
you can see the cursor is still indented, that means we can write more code 你可以看到光标仍然是缩进的，这意味着我们可以编写更多的代码
that we executed if this condition is true, in this case let's say we don't want to 如果这个条件成立，我们执行了，在这种情况下，假设我们不想这样做
write any extra code, so to terminate this block 写任何额外的代码，以便终止这个块
we need to press shift and tab, now the cursor is at the beginning of the line 我们需要按shift和tab，现在光标位于行的开头
so lets write a print statement with a message like 所以让我们写一个带有消息的print语句
enjoy your day. Now when we run this program, because 祝您愉快。现在当我们运行这个程序时，因为
this condition is true, you're doing to say this message followed by 这种情况是真的，你要说的是这条消息
this second message, take a look, so run, there you go, it's a hot day 这第二条消息，看看，跑步，你去，这是一个炎热的一天
enjoy your day. But if I go over here and 祝您愉快。但是如果我在这里过去的话
change this boolean value to false and run the program again, our 将此布尔值更改为false并再次运行程序，我们的
first message disappears and we only see the second message, enjoy 第一条消息消失，我们只看到第二条消息，享受
your day. So this is how if statements work. Now back 你的一天。所以这就是if语句的工作方式。现在回来了
here we can add another print statement let's say 我们可以在这里添加另一个印刷语句
drink plenty of water. 喝很多的水。
Now because this spirant statement is also indented it will be 现在因为这个spirant语句也缩进了它
executed if this condition is true. So I'm going to revert is hot to true 如果此条件为真，则执行。所以我要回归真的很热
and run the program one more time. There you go, so 并再次运行该程序。你去，所以
it's a hot day, drink plenty of water, and enjoy your 这是炎热的一天，多喝水，享受你的生活
day. Alright now let's add a second rule here, if 天。好了，现在让我们在这里添加第二个规则，如果
it's hot we're going to execute these two lines, otherwise if it's not hot 很热，我们要执行这两行，否则如果它不热
we want to print a different message. So here 我们想要打印不同的消息。所以在这里
we are moving an indentation and typing els 我们正在移动缩进并输入els
colon. Now when we press enter once again our cursor 结肠。现在，当我们再次按下输入我们的光标
indented so the code that we write here will be executed if this 缩进，这样我们在这里写的代码将被执行
condition is not true. So here we can print 条件不正确。所以在这里我们可以打印
it's a cold day, print, 这是一个寒冷的日子，打印，
where warm clothes. 暖和的衣服。
Now let's run our program one more time. So we get the message 现在让我们再次运行我们的程序。所以我们收到了消息
about a hot day followed by enjoy your day. You don't see any message 关于炎热的一天，然后享受你的一天。你没有看到任何消息
about a cold day. Now if we go back here on the top. 大约一个寒冷的日子。现在，如果我们回到顶部。
And change this boolean value to false and run our program 并将此布尔值更改为false并运行我们的程序
we see different set of messages. It's a cold day 我们看到不同的消息集。这是一个寒冷的日子
wear warm clothes and enjoy your day. But there's a problem with 穿上保暖的衣服，享受你的一天。但是有一个问题
our program. If it's not hot it doesn't necessarily mean that it's cold, it means 我们的计划。如果它不热，它并不一定意味着它很冷，这意味着
it's a lovely day. So the absence of heat doesn't mean its cold. 这真是美妙的一天。因此，没有热量并不意味着它的冷。
Back to our conditions, here on line 4 we have this rule that says 回到我们的条件，在第4行我们有这个规则说
if it's a cold day, then print these messages 如果是寒冷的一天，那么打印这些消息
otherwise if it's neither hot or cold say it's a lovely day. 否则，如果既不热也不冷，说这是美好的一天。
So, to implement this rule we need to go back and put 因此，要实施此规则，我们需要返回并放置
py and define another variable. So let' say py并定义另一个变量。所以，让我们说吧
is underline cold we set this to 我们设定这个是强调冷
true. Now here we need to add a second condition. So after 真正。现在我们需要添加第二个条件。所以之后
our first if statement, we can use an el if statement 我们的第一个if语句，我们可以使用el if语句
to define a second condition. So here's how it works. So, 定义第二个条件。所以这是它的工作原理。所以，
el if which is short for els if or el如果是或者是els的缩写
otherwise if, now here we add another condition, so, 否则如果，现在我们在这里添加另一个条件，所以，
is cold So if t's cold you want to 很冷，所以如果你想要冷
execute these few lines. So let's cut these 执行这几行。所以让我们削减这些
from here, and move them under our second condition 从这里开始，并在第二个条件下移动它们
and finally if none of these conditions are true, 最后如果这些条件都不成立，
you want to print a different message. It's a lovely 你想打印不同的消息。这很可爱
day. So, right now, 天。那么，现在，
is hot is false, is cold is true, 热是假的，冷是真的，
so when we run this program, python interpreter is going to execute the first 所以当我们运行这个程序时，python解释器将执行第一个
if statement, in this case because our condition is false, these two 如果声明，在这种情况下因为我们的条件是假的，这两个
lines will be ignored, then Python interpreter will look at 将忽略行，然后Python解释器将查看
line 7, it will evaluate this condition, in this case is 第7行，它将评估这种情况，在这种情况下是
cold is true so we're going to see these two messages on the terminal. 冷是真的，所以我们将在终端上看到这两条消息。
Now, in this case, because one of these conditions was true, this els statement 现在，在这种情况下，因为其中一个条件是真的，这个els声明
will be ignored, so we are not going to see this message, and finally 将被忽略，所以我们不会看到这个消息，最后
as before, we are always going too see this message. So let's run our 和以前一样，我们也总是看到这个消息。让我们来运行吧
program, there you go. it's a cold day, 程序，你去。这是一个寒冷的日子，
where warm clothes and and enjoy your day. Now, 温暖的衣服，享受你的一天。现在，
back to the top, if we change is cold 回到顶端，如果我们改变是冷
to false it's neither hot nor cold so it's going to be a lovely day. 假的它既不热也不冷，所以这将是一个美好的一天。
Let's run the program, and here you go, it's a lovely day 让我们来运行这个程序，你走了，这是一个美好的一天
enjoy your day. So these are the basics of using if 祝您愉快。所以这些是使用if的基础知识
statements. As you can see they are very useful in programming and with these 声明。正如您所看到的，它们在编程和使用它们时非常有用
we can build all kinds of rules into our programs. Okay here's an 我们可以在我们的程序中构建各种规则。好的，这是一个
exercise or you. Imagine the price of a house is 1 million dollars. 运动还是你。想象一下房子的价格是100万美元。
Now if the buyer has good credit, they will need to put down 10 percent 现在如果买家有良好的信誉，他们将需要减少10％
of the price of this property otherwise they need to put down 20 percent 这个属性的价格，否则他们需要放下20％
write a program with these rules an display the down payment 用这些规则编写一个程序，显示预付定金
card for a buyer with good credit. You will see my solution 信用良好的买家卡。你会看到我的解决方案
next. 下一个。
Alright let's define a variable for the price of this house so price 好吧让我们为这个房子的价格定义一个变量，所以价格
we set this to 1 million so 1 with 6 0s. 我们将其设置为100万，因此1为6 0。
Next we need a variable to tell if this buyer has good credit 接下来我们需要一个变量来判断这个买家是否有良好的信誉
so has good credit, 所以有良好的信誉，
and we set this to true, now we need an if statement 我们将其设置为true，现在我们需要一个if语句
so if has good credit has true 所以，如果有良好的信誉是真的
colon, here we need to calculate a down payment so 冒号，在这里我们需要计算首付款
the down_payment should be equal to down_payment应该等于
0.1 x the price. That is 10 percent of the price of this 0.1 x的价格。这是这个价格的10％
property. Otherwise colon 属性。否则结肠
the down payment should be 0.2 首付应为0.2
times price. Now finally we remove the 次价。现在终于我们删除了
indentation and print here we can use a form 缩进和打印在这里我们可以使用表格
of valid string, first we add a label, down payment 有效字符串，首先我们添加标签，首付
colon and right after that we add a placeholder 冒号，然后我们添加一个占位符
or a hole for our down payment variable. So curly braces 或者是我们的首付款变量的漏洞。如此花括号
down payment. Let's run this 首付。我们来试试吧
program, so down payment for a buyer with good credit 计划，所以为信誉良好的买家支付首期款项
is 100,000 dollars. Now let's improve this by adding a 是10万美元。现在让我们通过添加一个来改进这个
dollar sign before this number. So back to our formatted string, 这个数字之前的美元符号。回到我们的格式化字符串，
just before the curly brace I'm going to add a dollar sign let's 就在大括号之前，我要添加一个美元符号
run this one more time, that is better. 再跑这一次，那就更好了。
In this tutorial I'm going to talk to you about the logical operators within Python. 在本教程中，我将与您讨论Python中的逻辑运算符。
We use these operators in situations where we have multiple 我们在有多个运算符的情况下使用这些运算符
conditions. Here is an example. Let' say we're building an application for 条件。这是一个例子。让我们说我们正在构建一个应用程序
processing loans. If an applicant has high income 处理贷款。如果申请人有高收入
and good credit, then they're eligible 并且信誉良好，那么他们就有资格了
for a loan. So in this example we have two conditions, one is having 贷款。所以在这个例子中我们有两个条件，一个是
high income and the other is having good credit. So if both 高收入，另一个是良好的信誉。如果两者都是
these conditions are true, then the applicant is eligible for a loan. 这些条件属实，那么申请人有资格获得贷款。
So this is where we use the logical and operator. 所以这就是我们使用逻辑和运算符的地方。
We use this operator to combine two conditions, and by the way 我们使用此运算符来组合两个条件，顺便说一下
this is not specific to python programming language, pretty much any programming language 这不是特定于python编程语言，几乎任何编程语言
that supports if statements also supports the logical operators. 支持if语句也支持逻辑运算符。
So, back to our program, let's define two variables, 那么，回到我们的程序，让我们定义两个变量，
has high income, we set this to true. 收入很高，我们认为这是真的。
And another one has good 另一个人很好
credit, we also set this to true, now our if statement 信用，我们也将此设置为true，现在我们的if语句
if has high income has true, 如果有高收入的话，
and has good credit is also true, 并且有良好的信誉也是如此，
then we're going to print eligible 然后我们打算打印合格
for null. So this is where 为null。所以这就是地方
we're using the and operator. So if both these conditions are true 我们正在使用和运营商。所以如果这两个条件都是真的
then this message will be printed. If one of them is false, we're not going to see 然后将打印此消息。如果其中一个是假的，我们就不会看到
this message. Let's try this out. So I'm going to run this program 这条信息。我们来试试吧。所以我要运行这个程序
so we see it eligible for loan, but if we change 所以我们认为它有资格获得贷款，但如果我们改变了
either of these conditions to false, and run the program again 这些条件之一为假，并再次运行程序
look, the message disappears. 看，消息消失了。
So this is the logical and operator. We also have the 所以这是逻辑和运营商。我们也有
logical or, and we want to use that in situations where we want to do certain things 逻辑或，我们希望在我们想要做某些事情的情况下使用它
at least one of the conditions is true, for example let's change 至少有一个条件是真的，例如让我们改变
the rule for this program, such that if the applicant has high income, 该计划的规则，如果申请人有高收入，
or good credit, then they're eligible for a loan, 或良好的信誉，那么他们有资格获得贷款，
so if either or both these conditions are true then the candidate is 所以如果这些条件中的任何一个或两个都是真的那么候选人就是
eligible. Now back to our program we can 合格。现在我们可以回到我们的计划
implement this rule by using the logical or operator. So we simply replace 使用逻辑或运算符实现此规则。所以我们简单地替换
and with or, now when we run this program we're going to 并且，或者，现在当我们运行这个程序时，我们将要去
see this message because at least one of our conditions is true, 看到这条消息，因为至少我们的一个条件是真的，
let's take a look. So the applicant is eligible for a loan 让我们来看看。所以申请人有资格获得贷款
for a loan because they have good credit. If you change this to false 贷款，因为他们有良好的信誉。如果将此更改为false
but set the other condition to true, we still 但是将另一个条件设置为true，我们仍然如此
see the same result, but if both these conditions are false 看到相同的结果，但如果这两个条件都是假的
then we're not going to see this message anymore. So this is the difference 然后我们就不会再看到这个消息了。所以这就是区别
between these operators. With the logical and operator 这些运营商之间。与逻辑和运算符
both conditions should be true, with the logical 这两个条件应该是正确的，合乎逻辑的
or operator at least one condition should be true 或者操作员至少有一个条件应该是真的
we also have another logical operator called not 我们还有一个名为not的逻辑运算符
and that basically inverses any boolean value we give it, if we give it, 并且基本上反转我们给它的任何布尔值，如果我们给它，
we give it a true boolean value it converts it to false. For example 我们给它一个真正的布尔值，它将它转换为false。例如
let's make up a new room, if applicant 如果申请人，让我们组成一个新的房间
has good credit and doesn't have 有良好的信誉，没有
a criminal record then 那么犯罪记录
they're eligible for a loan. Let me show you how to implement this. So, 他们有资格获得贷款。让我告诉你如何实现这一点。所以，
we go back to our program, in this example we don't need a 我们回到我们的程序，在这个例子中我们不需要
first variable for let's delete that. Let's set this variable to true 第一个变量让我们删除它。我们将此变量设置为true
we also define another variable like has criminal 我们还定义了另一个变量，如有犯罪
record. We set this to false. 记录。我们将此设置为false。
Now, we want to check to see if this applicant has good credit and not 现在，我们想检查这个申请人是否有良好的信誉，而不是
a criminal record. This is where we use the not operator. 犯罪记录。这是我们使用not运算符的地方。
So, if they have good credit, 所以，如果他们有良好的信誉，
and not criminal 而不是犯罪
record. So, in this example, has criminal 记录。所以，在这个例子中，有犯罪
record is set to false, when we use the not operator this 当我们使用not运算符时，record设置为false
basically gets changed to true, so we have two conditions that are true. 基本上变为true，所以我们有两个条件是真的。
Here's ones and here's another one. So our applicant is 这是一个，这是另一个。所以我们的申请人是
eligible for a loan. And when we run this program we see 有资格获得贷款。当我们运行这个程序时，我们看到了
this familiar message. However 这个熟悉的消息。然而
if an applicant has a criminal record, so let's change this to 如果申请人有犯罪记录，那么让我们改为
true, now when we run this program we can see our applicant is 是的，现在当我们运行这个程序时，我们可以看到我们的申请人
not eligible because when we apply then operator on this 没有资格，因为当我们申请运营商时
variable, we'll get false. So true changes to false. 变量，我们会变错。所以真正的变化是假的。
And we'll end up with two conditions, one that's true 我们最终会有两个条件，一个是真的
and the other is false. And that's why this message is 而另一个是假的。这就是为什么这个消息
not printed. So this is all about the logical operators in 没打印。所以这就是关于逻辑运算符的全部内容
python. 蟒蛇。
In this tutorial I'm going to talk to you guys about 在本教程中，我将与你们谈谈
comparison operators in Python. We use comparison operators Python中的比较运算符。我们使用比较运算符
in situations where we want to compare a variable with a value, 在我们想要将变量与值进行比较的情况下，
for example, if temperature is greater than 30, then 例如，如果温度大于30，那么
we want to print it's a hot day. Otherwise, if it's less 我们想打印它是一个炎热的一天。否则，如果它更少
then 10, it's a cold day, otherwise it it's neither hot 然后10点，这是一个寒冷的日子，否则它既不热也不热
nor cold. And by the way I'm taking about celsius, not farenheit. 也冷。顺便说一下，我采取的是摄氏度，而不是华丽的。
So, to build these rules into our program, we need to use comparison 因此，要将这些规则构建到我们的程序中，我们需要使用比较
operators. Back to app.py, I define this 运营商。回到app.py，我定义了这个
temperature value, let's write an if statement, if temperature 温度值，让我们写一个if语句，如果温度
now we want to check to see if this is greater than 30, so we use 现在我们要查看是否大于30，所以我们使用
the greater than operator. If this is greater than 30, 大于运营商。如果大于30，
we want to print it's a hot day 我们想打印它是一个炎热的一天
otherwise, let's just print 否则，让我们打印
it's not a hot day. Now, 这不是一个炎热的日子。现在，
when we run this program, we're going to see this second message because 30 当我们运行这个程序时，我们将看到第二条消息，因为30
is not greater than 30. So our first condition a value 不大于30.所以我们的第一个条件是一个值
is to false. let's verify that. So run, 是假的。让我们验证一下。所以跑，
it's not a hot day. Now if you change 这不是一个炎热的日子。现在如果你改变了
the temperature to 35 and run this again, 温度达到35并再次运行，
we're going to see a different message, it's a hot day, so this is where we use 我们将看到一个不同的消息，这是一个炎热的日子，所以这是我们使用的地方
comparison operators. Now what we have here as you know is an expression 比较运算符。现在我们所知道的是一个表达式
because it's a piece of code that produces a value. So more accurately this is 因为它是一段产生价值的代码。所以这更准确
a boolean expression. So this is the greater than operator, 布尔表达式。所以这是大于运营商，
we also have greater than or equal to, we have 我们也有大于或等于
less then, less then or equal to, 少于，少于或等于，
here's the equality operator, so if the temperature equals to 30, 这是等于运算符，所以如果温度等于30，
then you can say it's a hot day. Note that this is different from the assignment operator 然后你可以说这是一个炎热的一天。请注意，这与赋值运算符不同
that has only one equals sign. You can see that if we use only one equal sign 那只有一个等号。你可以看到，如果我们只使用一个等号
here we immediately get this red underline because this is simply 在这里，我们立即得到这个红色下划线，因为这很简单
an assignment statement. We're changing the value of the temperature. 转让声明。我们正在改变温度的价值。
you are setting the value of 你正在设定的价值
something else. So we don't have a boolean expression, you are not producing a 别的。所以我们没有布尔表达式，你没有生成一个
boolean value. Okay? So, our equality operator 布尔值。好的？所以，我们的平等运算符
has two equal signs and finally we have not 有两个相同的标志，最后我们没有
equal which is an exclamation followed by an equal sign. 相等，这是一个感叹号，后跟一个等号。
Now here's an exercise for you. You have probably seen that when you fill out a 现在，这是一个练习。你可能已经看到了当你填写一个
form online, sometimes the input fields have validation messages, 在线表单，有时输入字段有验证消息，
for example, let's say we have an input field for the user to enter their name. 例如，假设我们有一个输入字段供用户输入他们的名字。
Now if the name is less then 3 characters wrong we want to display a validation 现在如果名称少于3个字符错误，我们想要显示验证
error, like name must be at least three characters, 错误，如名称必须至少三个字符，
otherwise, if the name is more then 50 characters long then we want 否则，如果名称超过50个字符，那么我们想要
to display a different validation error like name can be a maximum 显示不同的验证错误，如名称可以是最大值
of 50 characters. Otherwise if the name is between 3 and 50 characters 50个字符。否则，如果名称在3到50个字符之间
then we just want to tell the user that name looks good. 那么我们只想告诉用户名字看起来不错。
So go ahead, and write a plan to implement these rules. 所以继续，并写一个计划来实现这些规则。
Alright let's define a variable called name and set it to 好吧，让我们定义一个名为name的变量并将其设置为
let's say j. So we're assuming this is what the user types into an input field. 让我们说j。所以我们假设这是用户键入输入字段的内容。
Now, we want to get the number of characters in this string. 现在，我们想要获得此字符串中的字符数。
So we use the len function, right? Len of name. 所以我们使用len函数，对吗？ Len的名字。
When we print this we get 1, right 当我们打印这个时，我们得到1，对
you have seen this before. Now here we want to use an if statement so 你以前见过这个。现在我们要使用if语句
if len of name is less then 如果len的名字少于那么
3, then we want to print 3，然后我们要打印
name must be at least 3 characters 名称必须至少为3个字符
now here we need a second condition 现在我们需要第二个条件
to check the upper limit. So el if 检查上限。所以，如果
len of name is greater than 50, len的名字大于50，
then we want to print a different message, name, 然后我们要打印一个不同的消息，名称，
must be a maximum 必须是最大的
of 50 characters. 50个字符。
Okay? And otherwise if else 好的？否则如果否则
none of these conditions are true that means the name looks good. So, 这些条件都不正确，这意味着名称看起来不错。所以，
print, name looks good. Let's 打印，名字看起来不错。让我们
run our program. So in this case we get this message 运行我们的程序。所以在这种情况下我们得到这个消息
because our name is too short. Now if you go back here and 因为我们的名字太短了现在，如果你回到这里
type something really really long. And then 输入真的很长的东西。然后
we run our program we're going to see a different message name must be a 我们运行我们的程序，我们将看到一个不同的消息名称必须是一个
maximum of 50 characters and finally if 最多50个字符，最后如果
we type a proper name here like John Smith 我们在这里输入一个像John Smith一样的名字
and run our program we get name 并运行我们的程序，我们得到名字
looks good. 看起来不错。
Here's another good exercise that combines 这是另一个结合的好运动
many of the materials you have learned so far, so earlier you built a program to 到目前为止你学到的很多材料，所以早些时候你建立了一个程序
convert someone's weight from pounds to kilograms. Now we want to extend this program 将某人的体重从磅转换为公斤。现在我们要扩展这个程序
and allow the user to enter their weight in either kilograms or 并允许用户输入公斤或公斤的重量
pounds and then we will convert it to the other unit. Here's how our program 磅，然后我们将它转​​换为另一个单位。这是我们的计划
is going to work. So I enter my weight in pounds so 将要工作。所以我以磅为单位输入体重
100 and 60 now it's telling me if it's in pounds or 100和60现在它告诉我它是以磅还是
kilograms. So here I'm adding l to lbs or k for kilograms. 公斤。所以在这里我将l加到lbs或k为千克。
And by the way, this program is not case sensitive so 顺便说一下，这个程序不区分大小写
when I enter a capital l or lowercase l it takes 当我输入大写字母l或小写字母时，它需要
it as pounds. Now it tells me ur set it to 它像磅。现在它告诉我你把它设置为
kilos. Let's run this program one more time, 公斤。让我们再一次运行这个程序，
this time I'm going to enter my weight in kilo's, so send it to 这次我要用千克输入我的体重，所以把它送到
is the weight and the unit is kilograms so k, 是重量，单位是千克，所以k，
and it says you are 160 pounds. 它说你的体重是160磅。
So go ahead and spend a few minutes on this exercise, you will see my solution next. 所以继续花几分钟进行这个练习，接下来你会看到我的解决方案。
Alright first let's ask the user their weight. So we use the input 好吧首先让我们问用户他们的体重。所以我们使用输入
function, weight colon we get the return value 函数，重量冒号我们得到返回值
and store it in the variable called weight. 并将其存储在名为weight的变量中。
Now the second question, so one more time we use the input function 现在是第二个问题，所以我们再一次使用输入函数
el for pounds. el为磅。
Or k for kilograms. So, 或k为公斤。所以，
let's get that too and store it in a variable called unit 让我们也把它存储在一个名为unit的变量中
now we need an if statement. So if unit 现在我们需要一个if语句。所以，如果单位
equals l then we need to convert this weight 等于l然后我们需要转换这个重量
into kilograms. However, with this implementation we are only allowing 公斤。但是，通过此实现，我们只允许
the user to enter a capitol l, if they enter a lowercase l 用户输入国会大厦l，如果他们输入小写l
this code is not going to work. So this is where we use the upper 这段代码不起作用。所以这就是我们使用鞋帮的地方
method of string objects so this unit is a string because as I told you 字符串对象的方法，所以这个单位是一个字符串，因为我告诉你
before, the input function always returns a string. 之前，输入函数总是返回一个字符串。
So, we can use the dot operator to access all it's methods or 因此，我们可以使用点运算符来访问它的所有方法或
functions, here we call the upper method, this 函数，这里我们称之为上层方法，这个
will convert whatever the user enters to upper case and then we'll convert it 将用户输入的任何内容转换为大写，然后我们将转换它
to a capital l. Now, if this condition is true, 到首都现在，如果这个条件成立，
then we need to get the weight and multiply it by 0, 那么我们需要得到权重并乘以0，
.45 However, as you know this .45但是，如你所知
weight is a string object, and we cannot multiply a string weight是一个字符串对象，我们不能乘以字符串
by a floating point number, we talked about this earlier in this course. 通过浮点数，我们在本课程的前面讨论了这个问题。
So first we need to convert this weight to a numerical value. So 首先，我们需要将此权重转换为数值。所以
right here, when we call the input function, we can get the return 就在这里，当我们调用输入函数时，我们可以获得返回
value and pass it to the int function. So, value并将其传递给int函数。所以，
we call the int function and give it 我们调用int函数并给它
the return value of the input function. Now, the 输入函数的返回值。现在
in function will return an integer so we can store it in this weight in函数将返回一个整数，因此我们可以将它存储在此权重中
variable. So here's the converted weight, let's store it in 变量。所以这是转换后的重量，让它存储起来
a variable called converted, 一个名为convert的变量，
then we print here we can use a formatted string, so 然后我们在这里打印我们可以使用格式化的字符串，所以
we prefix this string with f ur 我们在此字符串前加上f ur
we add curly braces to dynamically insert the value of 我们添加花括号来动态插入值
converted variable. And finally we add kilo. 转换变量。最后我们加了千克。
Otherwise, if the unit is kilograms. 否则，如果单位是公斤。
We need to divide the weight by 0.45. q 我们需要将重量除以0.45。 q
So, weight divided by 0.45 and 所以，体重除以0.45和
just to refresh your memory, this division operator returns a floating point number 为了刷新你的记忆，这个除法运算符返回一个浮点数
but if we use double slashes we'll get an integer. 但如果我们使用双斜线，我们将得到一个整数。
In this case, we want to get a floating point number, 在这种情况下，我们想得到一个浮点数，
finally let's print a formatted string, ur 最后让我们打印一个格式化的字符串，你的
curly braces, converted 花括号，转换
pounds. Okay? Now 磅。好的？现在
let's run this program and see what happens. So weight is 让我们运行这个程序，看看会发生什么。所以重量是
160 and lbs in 160和lbs in
and that equals to 72 kilos, perfect, if we run it one more time, 这相当于72公斤，完美，如果我们再运行一​​次，
and enter 72 kilos 并输入72公斤
we get 160 pounds. 我们得到160磅。
In this tutorial I'm going to show you how to use y loops in python. We 在本教程中，我将向您展示如何在python中使用y循环。我们
use y loops to execute a block of code multiple times and there are often 使用y循环多次执行一段代码，经常有
useful in building interactive programs and games. In a future tutorial 有助于构建交互式程序和游戏。在以后的教程中
I'm going to show you how to build a simple game using a y loop. 我将向您展示如何使用ay循环构建一个简单的游戏。
So, let's get started with the basics, we write a y statement 所以，让我们开始基础知识，我们写一个声明
and right after that, we type a condition followed by a colon 然后，我们键入一个条件，然后是冒号
as long as this condition is true the quote that we write 只要这个条件是真的，我们写的引用
in this block will be repeatedly executed. Here is an example. 在这个块中将被重复执行。这是一个例子。
We can define a variable like i, as in short for 我们可以定义一个像i这样的变量，简而言之
index and set it to 1. Now we set our condition to index并将其设置为1.现在我们将条件设置为
i less than or equal to 5, so as long as I 我小于或等于5，所以只要我
is less then or equal to 5, we can print i, 小于或等于5，我们可以打印i，
on the terminal. And then we need to increment i, 在终端上。然后我们需要增加i，
by 1. So we set i to i plus 1. The reason 由1.我们设置i加i加1.原因
we do this is that if we don't do this I will be 1 forever 我们这样做，如果我们不这样做，我将永远是1
so we'll end up with an infinite loop. Because this condition will 所以我们最终会得到一个无限循环。因为这种情况会
always be true. One is always less then 5, so in every iteration of this 永远是真的。一个总是小于5，所以在每次迭代中
loop, we increment i by 1, so at some point, i is gonna be 循环，我们将i递增1，所以在某些时候，我会成为
six and then that is when this condition will be false and then 六，然后就是这个条件将是假的然后
we'll jump out of this loop, okay? Now to demonstrate how everything works 我们会跳出这个循环，好吗？现在来演示一切如何运作
after this loop I'm going to add a print statement 在这个循环之后，我将添加一个print语句
say done. So note that these two lines 说完了。所以请注意这两行
are indented so they are part of the y block. 缩进，因此它们是y块的一部分。
Okay, now let's go ahead and run this program and see what happens. So, 好的，现在让我们继续运行这个程序，看看会发生什么。所以，
take a look, we get the numbers 1-5 followed by 看一看，我们得到数字1-5后跟
done. So heres how this program gets executed 完成。所以这是该程序如何执行
first we set i to 1 now python interpreter executes line 首先我们将i设置为1现在python interpreter执行line
2, this condition is true because i is less then 5, 2，这个条件是真的，因为我不到5，
so i is printed on the terminal and then incremented by 所以我打印在终端上，然后递增
1. Then the control moves back to the beginning of the y loop. 然后控件移回y循环的开头。
So it doesn't go to the next statement. So, 所以它没有进入下一个声明。所以，
we come back here and now we are in the second iteration. In the second iteration 我们回到这里，现在我们正处于第二次迭代。在第二次迭代中
i is 2, and because 2 is less then 5, our condition is still 我是2，因为2小于5，我们的状况仍然存在
true, so i will be printed on the terminal, and once again it will be 是的，所以我将被打印在终端上，并且它将再次出现
incremented by 1, so at some point i is going to be 6, 增加1，所以在某些时候我将是6，
and that's when this condition will be false so our look will 那就是这个条件是假的，所以我们的样子会
be terminated and this done message will be printed on the terminal. 终止，这个完成的消息将打印在终端上。
So this is the basics of y loops. Now let's make this program a little 所以这是y循环的基础知识。现在让我们做一下这个程序吧
bit more interesting. Here we can write an expression like this. We 更有趣。在这里我们可以写出这样的表达式。我们
add a string, and in this string we add an asterisk 添加一个字符串，在这个字符串中我们添加一个星号
and then we multiply this string by i. So 然后我们将这个字符串乘以i。所以
with this expression, we can repeat a string, when we multiply a string 使用这个表达式，当我们乘以一个字符串时，我们可以重复一个字符串
by a number, that string will be repeated. For example, if 通过数字，该字符串将重复。例如，如果
i is two, this expression will produce a 我是两个，这个表达式会产生一个
string with two asterisks. Now let's run the program 带有两个星号的字符串。现在让我们运行程序
and see what we get. So we see this little triangle 看看我们得到了什么。所以我们看到这个小三角形
shape here. Because in the first iteration i is 1, so, 形状在这里。因为在第一次迭代中我是1，所以，
1 times an asterisk produces 1 asterisk. 1次星号产生1个星号。
In the second iteration i is 2, so when we multiply 2 在第二次迭代中，我是2，所以当我们乘以2时
by 1 asterisks, we'll get 2 asterisks. 通过1个星号，我们将获得2个星号。
In this tutorial I'm going to show you how to use a y loop to build 在本教程中，我将向您展示如何使用ay循环来构建
a guessing game like this. So we have this secret number which is currently set to 像这样的猜谜游戏。所以我们有这个秘密号码，目前设定为
9. Now the computer is asking me to make a guess. So, 现在电脑要我猜猜。所以，
let's say 1 is not right because the secret number is 9, okay, 让我们说1是不对的，因为秘密号是9，好的，
try again, 2, no it's not right, let's try again, so I 再试一次，2，不，它不对，让我们再试一次，所以我
only have three chances to make a guess. If I can't guess the number the program 只有三次机会猜测。如果我猜不出程序的数量
tells me that I failed. let's run the program one more time, this time 告诉我，我失败了。让我们再次运行程序，这一次
I'm going to guess the number, it's 9, there you go, it says you in. 我猜这个数字，它是9，你去了，它说你进去了。
So let's go ahead and build this program using a y loop. 所以让我们继续使用ay循环构建这个程序。
Alright, let's start by defining a variable to store our secret number 好吧，让我们首先定义一个变量来存储我们的密码
so, we call the secret underline number and set it to 9. 所以，我们称秘密下划线编号并将其设置为9。
Now we need a while loop to repeatedly ask a user to 现在我们需要一个while循环来反复询问用户
make a guess. So while condition colon 做一个猜想。因此，条件结肠
What is our condition here? Well we want to give our user a 我们的病情是什么？好吧，我们想给我们的用户一个
maximum of three guesses. So similar to the last tutorial, we can define 最多三次猜测。与上一个教程类似，我们可以定义
a variable like I, set it to 0, and assume this represents 像I这样的变量，将其设置为0，并假设它代表
the number of guesses the user has made. And then we write our condition 用户猜测的次数。然后我们写下我们的条件
as i less then 3. Note that here I'm not 因为我不到3岁。请注意，我不是
using less then or equal to operator, because with this condition 使用少于或等于运算符，因为有这种情况
our loop will be executed 4 times, while i is 0, 我们的循环将被执行4次，而我是0，
one, two, and three, so here we should use 一，二，三，所以在这里我们应该使用
the less then operator. Now if we give this code to someone else it's unclear 操作员越少。现在，如果我们将此代码提供给其他人，则不清楚
what does i represent here, it's only in our head that 我在这里代表什么，它只在我们的头脑中
i represents the number of guesses the user has made. So as 我代表用户所做的猜测次数。这样
a best practice, always use meaningful and descriptive names for your variables. 最佳实践，始终为变量使用有意义和描述性的名称。
So it's better to rename this variable to guess, count. 因此，最好将此变量重命名为guess，count。
Let me show you how to rename. So right click on i variable, 我来告诉你如何重命名。所以右键单击i变量，
and then go to refactor and 然后去重构和
rename it. Look at the shortcut. On a Mac computer it's shift and 重命名。看看快捷方式。在Mac电脑上它是转移和
f 6. Now in this dialogue box we can easily f 6.现在在这个对话框中我们可以很容易地
rename your variable and pycharm will update all the references to that 重命名您的变量，pycharm将更新对该变量的所有引用
variable so we don't have to manually update each instance, okay? Let's 变量，所以我们不必手动更新每个实例，好吗？让我们
change this to guess_count 将此更改为guess_count
enter, there you go, now that is better, 进入，你去，现在更好，
also it's better to store 3 in a separate variable to make our code 最好将3存储在一个单独的变量中以制作我们的代码
more readable, because it's not quite clear what does 3 represent here. 更具可读性，因为它不太清楚3代表什么。
So, let's define a variable called guess limit 所以，让我们定义一个名为guess limit的变量
say to 3, and then we can change 3 to guess 说到3，然后我们可以改变3来猜测
underline limit, now our code is more readable 下划线限制，现在我们的代码更具可读性
while guess count is less then guess limit, see it reads like a story 虽然猜计数少于猜测限制，但看起来就像一个故事
this is how you should write code. Okay, so 这就是你应该如何编写代码。可以，然后呢
while this condition is true, we want toast the user to make a guess. So here 虽然这种情况属实，但我们想要让用户敬酒进行猜测。所以在这里
we use our input function, guess 我们使用输入函数，猜测
Now whatever the user enters comes out as a string so we need to convert it to an integer. 现在用户输入的任何内容都以字符串形式出现，因此我们需要将其转换为整数。
So right here, we pass the result to the end function 所以就在这里，我们将结果传递给end函数
and then get it and store it in a separate variable called guess. 然后得到它并将其存储在一个名为guess的单独变量中。
So at this point the user made a guess, now we need to 所以在这一点上用户做了一个猜测，现在我们需要
increment guess count so guess count 递增猜数，所以猜数
we set it to plus equal 1 or 我们将它设置为加1等于1
okay, now we need to check to see if the user will make the right guess. 好的，现在我们需要检查用户是否会做出正确的猜测。
So here we need an if statement. If what the user guessed 所以这里我们需要一个if语句。如果用户猜到了什么
equals our secret number, 等于我们的秘密号码，
again see our code is so readable. It's like a story you can read it like plain 再次看到我们的代码是如此可读。这就像一个故事，你可以像平原一样阅读它
English. So if this condition is true we want to 英语。因此，如果这种情况属实，我们希望
tell the user they won. So print you won 告诉用户他们赢了。所以打印你赢了
now lets go ahead and run our program up to this point. 现在让我们继续运行我们的程序到目前为止。
So okay it's asking me to make a guess, I'm going to make 所以，好吧，它要求我做出猜测，我要做
the wrong guess so one it asked me again, 2 one more time, 错误的猜测，所以它再次问我，2再一次，
3, okay, what is missing in this implementation is the 3，好的，这个实现中缺少的是
message that tells me that I failed. We're going to take care of it momentarily, but 消息告诉我，我失败了。我们会暂时处理它，但是
let's run the program one more time and make the right guess. So, 9 让我们再次运行程序并做出正确的猜测。所以，9
okay it says you won, but it's still asking me to make 好吧，它说你赢了，但它仍然要求我做
a guess, because our while loop is going to get executed 3 times. Look 一个猜测，因为我们的while循环将被执行3次。看
1 and 2. So we need to change our program 1和2.所以我们需要改变我们的计划
such that if the user makes the right guess, we need to terminate our 如果用户做出正确的猜测，我们需要终止我们的
while loop, we need to jump out of it. How do we do that? while循环，我们需要跳出它。我们怎么做？
So, over here if the user makes the right guess, after 所以，在这里，如果用户做出正确的猜测，之后
we print this message we can use the brick statement to terminate 我们打印此消息，我们可以使用brick语句来终止
terminate a loop, when python interpreter sees this, it's going to 终止一个循环，当python解释器看到它时，它会发生
immediately terminate our loop, it's not going to evaluate this condition again. 立即终止我们的循环，它不会再次评估这个条件。
Now let's run our program and see what happens. So, I'm going to 现在让我们运行我们的程序，看看会发生什么。所以，我要去
guess the right number, you won and look, you are 猜对了，你赢了，看，你是
now asked to make two more guesses, beautiful. Now the last thing we need 现在要求做两个更多的猜测，美丽。现在我们需要的最后一件事
to add here is the message that tells the user that they failed if they could not guess 在这里添加的是一条消息，告诉用户如果他们无法猜测他们就会失败
the right number. How do we do that? Well in Python 正确的号码。我们怎么做？好在Python
our while loops can optionally have an else part. 我们的while循环可以选择有一个else部分。
similar to the if statements. So earlier you learned that our if statements 类似于if语句。所以早些时候你学会了我们的if语句
can optionally happen else part. Here, so 可以选择发生其他部分。在这里，所以
if this condition is true, do this, 如果这个条件成立，那么这样做，
otherwise do something else. In this case our if statement doesn't have 否则做别的事。在这种情况下，我们的if语句没有
an else part. Now, similar to the if statements 另一部分。现在，类似于if语句
Our while loops, our while statements can also have an else part. 我们的while循环，我们的while语句也可以有一个else部分。
So, right at this level we can add 所以，在这个级别，我们可以添加
an else block, so else colon. 一个else块，所以else冒号。
And the code that we write here will get executed if this while loop 如果这个while循环，我们在这里写的代码将被执行
completes successfully without an immediate 没有立即成功完成
break. In other words. If the user guesses the 打破。换一种说法。如果用户猜到了
right number, you break this loop, you jump out of it so the code that 正确的数字，你打破这个循环，你跳出它所以代码
we write in the else block will not get executed. 我们写在else块中不会被执行。
But if the user cannot guess this number, you're never going to break out of 但是，如果用户无法猜出这个数字，那么你永远不会突破
this loop, so this loop will be executed to completion untill 这个循环，所以这个循环将执行完成，直到完成
this condition become false. In that case, the code that we write 这种情况变得虚假。在那种情况下，我们编写的代码
in the else block will get executed, and this is the perfect opportunity for us 在else块中将被执行，这对我们来说是一个绝佳的机会
to tell the user hey, you made three guesses but none of them were right. 告诉用户嘿，你做了三个猜测，但没有一个是正确的。
So, print, sorry you 所以，打印，对不起你
failed. Now, let's test the program one more time. 失败。现在，让我们再次测试该程序。
So, guess 1, 2, 3, 所以，猜1,2,3，
sorry you failed, let's run it one more time. This time I'm going to make 抱歉，你失败了，让我们再次运行它。这次我要做
a wrong guess, and then the right guess, 一个错误的猜测，然后正确的猜测，
we won and our loop terminated immediately. 我们赢了，我们的循环立即终止。
Alright, now it's time for you to practice what you have learned so far. 好了，现在是时候练习到目前为止学到的东西了。
So once again we're going to build a game this game is a simulation 所以我们再次构建一个游戏，这个游戏就是模拟游戏
card game. Now our game doesn't have a graphical user interface or gooey 纸牌游戏。现在我们的游戏没有图形用户界面或粘糊糊的
and it doesn't really matter for now, our focus is entirely on building the 它现在并不重要，我们的重点完全在于构建
engine for this game. So let's see how this works. 这场比赛的引擎。那么让我们看看它是如何工作的。
When we run this, we get this little symbol here, and our program 当我们运行它时，我们在这里得到这个小符号和我们的程序
is waiting for us to enter a command. If you type help 正在等我们输入一个命令。如果您输入帮助
either a lower case or upper case we get the list of commands 无论是小写还是大写，我们都会获得命令列表
that our program or our game currently supports. So we can type the 我们的程序或我们的游戏目前支持。所以我们可以输入
start command to start our car, we can type 启动命令启动我们的汽车，我们可以打字
stop command to stop our car, and quit to terminate the game. 停止命令停止我们的车，并退出终止游戏。
Any other commands that we type our program is going to tell us hey I don't understand that. 我们输入程序的任何其他命令都会告诉我们嘿，我不明白。
For example, if I type asd here, it's going to say I don't understand 例如，如果我在这里输入asd，那就说我不明白
that.If you type start, we get this message, 如果你输入start，我们收到这条消息，
car started, ready to go, if you type stop it 汽车启动，准备好了，如果你输入停止它
says car stopped, and finally if we hit quit 说汽车停了，最后如果我们打了戒
our program terminates, this is a fantastic exercise for you to practice 我们的课程终止，这是一个很棒的练习练习
wha you have learned, so pause the video and spend 5-10 minutes 你学会了什么，所以暂停视频，花5-10分钟
to build this program. 建立这个计划。
Alright, we're going to start with a while loop with a condition 好吧，我们将从有条件的while循环开始
What is our condition here? We want to run this loop 我们的病情是什么？我们想要运行这个循环
until the user types quit. So we can define a variable 直到用户输入quit。所以我们可以定义一个变量
or a story to command what the user enters, and then we can run this 或者一个故事来命令用户输入什么，然后我们可以运行它
loop as long as the command does not equal to quit. So right before 只要命令不等于退出就循环。就在此之前
the loop, we define a variable, command, and initially we set it 循环，我们定义一个变量，命令，最初我们设置它
to an empty string. An empty string is a string that has no characters 一个空字符串。空字符串是没有字符的字符串
in it. We only have the quotes. So then 在里面。我们只有报价。那么
we type out our condition as while command 我们输入条件为while命令
does not equal to quit then do something. 不等于退出然后做某事。
Now immediately we have a problem here because we're assuming that the user 现在我们立即遇到问题，因为我们假设用户
types the command in lower case, so if they type this in upper case they 以小写形式键入命令，因此如果他们以大写形式键入它们
our program is not going to behave properly, so to fix this problem, you need to 我们的程序不会正常运行，所以要解决这个问题，你需要
call the lower method of the string object and then compare the results. 调用字符串对象的lower方法，然后比较结果。
With this quit. You could also call this upper 随着这个退出。你也可以称之为上层
and then type quit in upper case. It's about our personal preference 然后以大写形式键入quit。这是我们个人的偏好
in this demo I'm going to use lower case characters. 在这个演示中，我将使用小写字符。
So, okay, now in this loop we need to ask the user to enter 所以，好的，现在在这个循环中我们需要让用户输入
a command. So once again we're going to use our input function, we're going to add 一个命令。所以我们将再次使用我们的输入函数，我们将添加
a greater then symbol followed by a space, 一个更大的符号后跟一个空格，
whatever the user enters, we get it and store it in our 无论用户输入什么，我们都会将其存储在我们的网站中
command variable. Now apart from 命令变量。现在除了
quit command, there are three other commands that we need to support. Start, 退出命令，我们需要支持其他三个命令。开始，
stop, and help. So here we need an if statement to compare 停下来，帮忙。所以这里我们需要一个if语句进行比较
what the user enters with one of the supported commands. 用户使用其中一个支持的命令输入的内容。
So, if command.lower 所以，如果是command.lower
equals start then you want to print the message 等于开始然后你想打印消息
like the car started. So print the car started. 像汽车一样开始了。所以打印车开始了。
Ready to go, it doesn't matter, 准备好了，没关系，
now the second condition. What if its not start, maybe it's stop? So, 现在是第二个条件。如果它没有开始，也许它会停止怎么办？所以，
el if command.lower el if command.lower
equals stop, there you go, then we print a 等于停止，你去，然后我们打印一个
different message car stopped. Now look at our code. 不同的消息车停了。现在看看我们的代码。
We have repeated this lower lower lower multiple times. 我们多次重复这个较低的较低的下部。
This is bad, in programming we have a term called dry, which is short for 这很糟糕，在编程中我们有一个名为dry的术语，这是一个简称
don't repeat yourself. So whenever you have duplicate 不要重复自己。所以每当你有重复
your code that means you're doing something wrong. So how can we solve this problem. 你的代码意味着你做错了什么。那么我们怎样才能解决这个问题呢。
Well, instead of calling the lower method in each condition, 好吧，而不是在每个条件中调用lower方法，
we can call it right here when we get the input from the user, so 当我们从用户那里得到输入时，我们可以在这里调用它
this input function as you know returns a string, we can 你知道这个输入函数返回一个字符串，我们可以
immediately call the lower method on this string, and with this command 立即调用此字符串上的lower方法，并使用此命令
will always be in lowercase, so we don't need to 将永远是小写，所以我们不需要
call this method in every condition. Look, 在每种情况下都要调用此方法。看，
we remove the duplication and also our conditions 我们删除重复和我们的条件
are shorter and easier to read. There is also one more place 更短，更容易阅读。还有一个地方
we need to modify so, it's right here. That is better. 我们需要修改，它就在这里。那样更好。
Now, the third command. We need one more el if. 现在，第三个命令。我们还需要一个el if。
If the command equals help, then, 如果命令等于help，那么，
we want to show the commands that we support. So, 我们想要显示我们支持的命令。所以，
here we're going to print a multi line string. So we 这里我们要打印多行字符串。所以我们
use triple quotes like this, and 使用这样的三重引号，和
give the user a guideline like this. So start 为用户提供这样的指南。所以开始
to start the car stop 开始停车
to stop the car, and 停车，和
quit to quit. Now finally we need 退出戒烟。现在我们终于需要了
an else part, so if what the user enters is none of these commands, 一个else部分，所以如果用户输入的是这些命令，
we're going to tell them, hey we don't understand these, 我们要告诉他们，嘿，我们不明白这些，
else, colon print 否则，冒号打印
sorry I don't understand that. 对不起，我不明白。
And by the way note that here becomes I'm using double 顺便说一下，这里变成我正在使用双倍
quotes, I can easily use a single quote as an apostrophe, okay? 引用，我可以轻松地使用单引号作为撇号，好吗？
So let's run our program up to this point and see what happens. 所以让我们运行我们的程序直到这一点，看看会发生什么。
Alright, let's type start car is 好吧，让我们打字开车
started, beautiful, stop, car is stopped, help, 开始，美丽，停止，车停，帮忙，
we get this guideline, but there's so much indentation 我们得到了这个指导方针，但是有很多缩进
before our commands, we'll fix that in a second. And finally 在我们的命令之前，我们将在一秒钟内修复它。最后
let's test the quit command, oops, our program 让我们测试quit命令，oops，我们的程序
didn't work properly. Here is the reason. With these if statements, 没有正常工作。这就是原因。有了这些if语句，
we're comparing the command with start stop, and 我们将命令与start stop进行比较，并且
help. Anything else will end up here, so that's why our 救命。其他任何东西都会在这里结束，所以这就是我们的原因
program says it doesn't understand that. So that's why our program 程序说它不明白。这就是为什么我们的计划
says it doesn't understand that command. However, after this el statement 说它不明白那个命令。但是，经过这个el声明
the control will be moved to the beginning of the loop. 控件将移动到循环的开头。
At this point our command is quit, so our loop will complete and the 此时我们的命令退出，所以我们的循环将完成并且
program terminates. In other words when we run this program 程序终止。换句话说，当我们运行这个程序时
and type quit, our program actually quits but 并输入quit，我们的程序实际上退出了
we still see this message which shouldn't appear here. How can we solve this problem? 我们仍然会看到此消息不应出现在此处。我们怎样才能解决这个问题？
Well, we can come back here and just before 好吧，我们可以回到这里，就在此之前
the else block, add another el if, something like else块，添加另一个el，如果
this. el if command equals 这个。 el if命令等于
quit then you can immediately break. 退出然后你可以立即打破。
This will solve our problem, but note that we have kind of repeated 这将解决我们的问题，但请注意我们有点重复
this expression in two places. The reality is that 这个表达在两个地方。现实是这样的
we don't really need this condition on the top, because with these if statements 我们并不需要在顶部使用这个条件，因为这些if语句
well more accurately with this el if we can jump out of this loop and terminate 如果我们可以跳出这个循环并终止，那么更准确地使用这个el
our program. So, we can simplify our condition 我们的计划。所以，我们可以简化我们的条件
to something like this. True. So while true means 这样的事情。真正。所以虽然真实意味着
this block of code is going to get executed repeatedly, 这段代码将重复执行，
until we explicitly break out of it, 直到我们明确地突破它，
okay? Now let's test our program one more time. So, 好的？现在让我们再次测试我们的程序。所以，
quit now our program terminates and we don't see that message 现在退出我们的程序终止，我们没有看到该消息
beautiful. So let's fix the last problem. You saw that when we 美丽。所以让我们解决最后一个问题。你看到我们的时候
typed help, these guidelines appeared with so much in 键入的帮助，这些指南出现了这么多
indentation, and here's the reason, look, right here in our code, 缩进，这就是原因，看看，就在我们的代码中，
they are already indented. So when we use triple quotes, what we type here 他们已经缩进了。所以当我们使用三重引号时，我们在这里输入的内容
will be printed exactly as is. So, because we 将完全按原样打印。所以，因为我们
have an indentation here, this indentation will also be printed on the terminal. 这里有一个缩进，这个缩进也将印在终端上。
So, let's delete these 所以，让我们删除这些
okay, run the program one more time, type help, 好的，再次运行程序，键入help，
the indentation is gone. Beautiful. Now here's a challenge for you. I want you to 缩进消失了。美丽。现在，这对您来说是一个挑战。我要你去
take this program to the next level. So right now if we type 把这个程序提升到一个新的水平。所以如果我们打字的话
start we get this message car started. And if we type start again 开始我们得到这个消息汽车开始。如果我们再次输入
we get the same message. It would be better if 我们收到相同的消息。如果更好的话
we got a message like car is already started so it doesn't make sense to 我们收到一条消息，比如汽车已经启动，所以它没有意义
start a car twice. Similarly, if we type stop 开车两次。同样，如果我们键入stop
it says car stopped, if we type it again we get the exact same 它说汽车停了，如果我们再次输入，我们会得到完全一样的
message, it doesn't make sense to stop the car twice. So here's what I need you to do 消息，两次停车是没有意义的。所以这就是我需要你做的事情
if the car is stopped and the user tries to stop it again, 如果汽车停止并且用户试图再次停车，
the program should say hey, the car is already stopped, what are you doing? And similarly 程序应该说嘿，车已经停了，你在做什么？同样地
if the car is already started and the user tries to start it again, 如果汽车已经启动并且用户试图再次启动它，
the program should yell at the user. So go ahead and make the necessary changes to 程序应该对用户大喊大叫。所以继续进行必要的修改
implement this scenario. 实现这个场景。
Alright to add this to our program, we need 好的，我们需要将它添加到我们的程序中
to know if the car is started or not. So there is one more piece of information 知道汽车是否启动。所以还有一条信息
we need to store in the memory. What is the kind of data we need to store here? 我们需要存储在内存中。我们需要在这里存储什么类型的数据？
A boolean. Is the car started or not, it's a matter of yes 布尔值。汽车是否启动，这是一个问题
or no. True or false. So on the top, here 或没有。对或错。所以在这里，顶部
we can define another variable like started and initially we set it 我们可以定义另一个变量，比如start，最初我们设置它
to false. So the car is not started, right? Now 为假。所以车没开，对吗？现在
when the user types the start command, here we need to check 当用户键入start命令时，我们需要检查
to see if the car is already started. If not the we'll start it 看看汽车是否已经启动。如果不是，我们将启动它
or otherwise we'll yell at the user. So in this block we'll write 否则我们会对用户大喊大叫。所以在这个块中我们会写
another if statement, if it's already started 另一个if语句，如果它已经开始了
and we print car is already 我们已经打印汽车了
started. Otherwise, so if you add an 开始。否则，如果你添加一个
el statement here. And at this point, el声明在这里。在这一点上，
you set started to true. So we start 你开始设置为真。所以我们开始吧
the car and we print this message, okay? Now we need to make a similar 汽车，我们打印这条消息，好吗？现在我们需要做一个类似的
change for the stop command. So if the car is 更改停止命令。所以，如果汽车是
already stopped we need to print a different message. If 已停止，我们需要打印不同的消息。如果
not started, so here we're using the not operator to see 没有开始，所以我们在这里使用not运算符来查看
if the car is stopped. So if it's not started that means 如果汽车停了所以，如果它没有开始那就意味着
it's stopped, okay? So if it stopped we print 它停了，好吗？所以，如果它停止我们打印
car is already stopped with 汽车已停止了
double p's, otherwise so else 双p，否则如此
we need to stop the car, how do we do that? We 我们需要停车，我们该怎么做？我们
set started to false. And then we'll print this message. 设置开始为假。然后我们将打印此消息。
As easy as that. Let's go ahead and run our program. 就这么简单。让我们继续我们的计划吧。
So, initially our car is stopped. So I'm going to type 所以，最初我们的车停了。所以我打算打字
stop, it says the car is already stopped, so lets 停下来，它说汽车已经停了，所以让我们
start it, okay, now our car is started, let's 启动它，好吧，现在我们的车开始了，让我们开始吧
start it one more time. The program is yelling at us. So we can 再开始一次。该计划正在向我们大喊大叫。所以我们可以
not start the car twice. Beautiful. Now let's stop it 不要开车两次。美丽。现在让我们停止它
it says the car is stopped, let's stop it one more time, we get this message 它说汽车停了，让我们再停一次，我们收到这条消息
again. 再次。
In this tutorial, I'm going to talk to you guys about 在本教程中，我将与你们谈谈
for loops in python. In the last tutorial, you learned about while loops 在python中的for循环。在上一个教程中，您了解了while循环
you learned that we use while loops to execute a block of code multiple times. 你了解到我们使用while循环来多次执行一段代码。
In python we have another kind of loop, that is a for loop, and we use that 在python中我们有另一种循环，即for循环，我们使用它
and we use that to iterate over items of a collection, such as a string. 我们使用它来迭代集合的项目，例如字符串。
Because a string is a sequence of characters, so it looks like a collection 因为字符串是一系列字符，所以它看起来像一个集合
so we can use a for loop to iterate over each character in a 所以我们可以使用for循环迭代a中的每个字符
string and then do something with it. Here's an example. We type out 字符串，然后用它做一些事情。这是一个例子。我们输入
for then we define a variable, this is what we call a loop 然后我们定义一个变量，这就是我们所说的循环
variable. In each iteration, this variable will hold 变量。在每次迭代中，此变量都将成立
one item. So, let's call it item, in 一个项目。所以，我们称之为项目
here we type out a string like Python, and then 在这里我们输入一个像Python一样的字符串，然后
colon. So with this for loop we can iterate over a string and in each 结肠。因此，使用此for循环，我们可以遍历字符串并在每个字符串中迭代
iteration this item variable will hold one character at a time 迭代此项变量将一次包含一个字符
in the first iteration it will be set to p then 在第一次迭代中，它将被设置为p
in the second iteration it will be set to y, and in the third iteration it will be set to 在第二次迭代中，它将被设置为y，在第三次迭代中，它将被设置为
t and so on. So here we are inside our for block, 等等。所以这里我们在我们的区块内，
because of the indentation here, so whatever we type here will be executed in 因为这里的缩进，所以我们在这里输入的任何内容都将被执行
each iteration, for now we can simply print this item 每次迭代，现在我们可以简单地打印这个项目
now let's run this program and see what happens. 现在让我们运行这个程序，看看会发生什么。
So you can see each character in this string is printed on 所以你可以看到这个字符串中的每个字符都被打印出来
a new line. Let's look at another example. In Python we can 一条新线。让我们看另一个例子。在Python中我们可以
define lists using square brackets, so let me remove this string from 使用方括号定义列表，所以让我从中删除此字符串
here, and define a list using square brackets, 在这里，并使用方括号定义列表，
a list is simply a list of items, a list of numbers, a list of 列表只是一个项目列表，一个数字列表，一个列表
customers, a list of emails, products, blog posts, whatever. 客户，电子邮件列表，产品，博客文章，等等。
So here we can define a list of names like Mosh, 所以在这里我们可以定义像Mosh这样的名字列表，
Jon, Sarah, and then go ahead 乔恩，莎拉，然后继续
and run our program so we can see in each iteration we get one name 并运行我们的程序，以便我们可以在每次迭代中看到我们得到一个名称
and print it on a new line. We can also 并将其打印在新行上。我们也可以
loop over a list of numbers, for example, 循环遍历数字列表，例如，
1, 2, 3, 4, let's run it, again 1,2,3,4，让我们再次运行它
we see each number on a new line, but what if we want 我们在新的一行上看到每个数字，但如果我们想要的话
a list of numbers? We don't to explicitly type out a list 一个数字列表？我们不会明确地输入列表
with let's say 100 or 1000 numbers, we don't want to type, 5, 6 让我们说100或1000个数字，我们不想输入，5,6
7 all the way to 100. That is when we use the range function. 7一直到100.那就是我们使用范围功能。
So, let me delete this, 所以，让我删除这个，
In Python we have a built in function called range, for creating 在Python中，我们有一个内置函数，称为range，用于创建
range of numbers. So, we give it a number, let's say 10, 数字范围。所以，我们给它一个数字，比方说10，
let's run this program, now we can see 让我们运行这个程序，现在我们可以看到
here on the terminal we have 0 all the way 在终端上我们一直有0
to 9. So 10 is not included. 至9.所以10不包括在内。
So basically when we call the range function, this range creates 所以基本上当我们调用范围函数时，这个范围会产生
an object, it's not a list, it's a special kind of object we can 一个对象，它不是一个列表，它是一种特殊的对象我们可以
iterate over, in each iteration this object will spit out a new number. 迭代，在每次迭代中，这个对象将吐出一个新的数字。
We can also work with a range of numbers here, let's say you want to start 我们也可以在这里使用一系列数字，假设您想要开始
from 5, and go all the way to 10. 从5开始，一直到10。
So, let's run our program, now we have the numbers 那么，让我们运行我们的程序，现在我们有了数字
5, 6, 7, and 9. Also, this range 5,6,7和9.此外，这个范围
function can optionally take a step, so we can pass 功能可以选择一步，所以我们可以通过
two asa step to this function, 两个步骤到这个功能，
and when we run our program, we can see our first number 当我们运行我们的程序时，我们可以看到我们的第一个数字
is 5, now we go two steps forward to get 7, 是5，现在我们前进两步，得到7，
once again we go to two steps forward we get 9 and that is 再一次，我们前进两步，我们得到9，那就是
the end of our range. So this is the basics of using for loops in python. 我们系列的终点。所以这是在python中使用for循环的基础知识。
Now here's an exercise for you. I want you to write a program 现在，这是一个练习。我要你写一个程序
to calculate the total cost of all the items in a shopping cart. 计算购物车中所有商品的总成本。
So let's say we have a list of prices like 10, 所以我们假设我们有一个像10这样的价格清单，
20, and 30, I want you to use a for loop, 20和30，我希望你使用for循环，
to calculate the total cost of all the items in our imaginary shopping cart, 计算我们想象中的购物车中所有商品的总成本，
so calculate that, and then print it on the terminal. That's pretty 所以计算一下，然后将它打印在终端上。真漂亮
easy. And you should do it in a couple minutes. 简单。你应该在几分钟内完成。
So as you learned we use for 所以当你学会我们用的时候
loops to iterate over all the items in a collection, 循环迭代集合中的所有项目，
a collection can be a string, it can be a list, it can be a range object that is 一个集合可以是一个字符串，它可以是一个列表，它可以是一个范围对象
returned from the range function. Anything, any kind of object that has multiple 从范围函数返回。任何东西，任何有多种物体的物体
items. So in this example we're going to use a for loop to iterate over 项目。所以在这个例子中我们将使用for循环来迭代
the list of prices. So for item in 价格清单。所以对于项目
prices, colon, and by the way this loop variable 价格，冒号，顺便说一下这个循环变量
we don't have to call this item, can call this anything. For example, in this case 我们不必称这个项目，可以称之为任何东西。例如，在这种情况下
we can rename it to price. So for price, 我们可以将它重命名为价格。所以对于价格，
in prices. Now in each iteration this 价格。现在在每次迭代中
price will hold one value. In the first iteration it's going to hold 10, then it's 价格将保持一个价值。在第一次迭代中，它将保持10，然后是
gooing to be 20, and then it's going to be 30. So we need to define gooing是20，然后它将是30.所以我们需要定义
another variable to calculate the total so 另一个变量来计算总数
we define that outside of our for loop, let's call it total 我们在for循环之外定义，让我们称它为total
and initially we set it to 0. Now in each 最初我们将它设置为0.现在每个
iteration we get the current price and add it to the total so 迭代我们得到当前价格并将其添加到总数中
we write total equals total plus price 我们写总等于总加价
or as you learned earlier we can use the augmented 或者如前所述，我们可以使用增强版
assignment operator to simplify this code. 赋值运算符以简化此代码。
So, after our for loop. This total variable has 所以，在我们的for循环之后。这个总变量有
a total of all the prices we can simply print it here, 我们可以在这里简单地打印所有价格，
or we can use a formatted string, so a string 或者我们可以使用格式化的字符串，所以一个字符串
prefix with f, the other label, like total, 前缀为f，另一个标签，如总数，
curly braces to dynamically include some value in our string, 花括号在我们的字符串中动态包含一些值，
in this case our total variable. So let's go ahead 在这种情况下我们的总变量。让我们继续吧
and run this program, there you go, so the total cost of 并运行这个程序，你去，所以总成本
all the items in our imaginary shopping cart is 60. 我们想象中的购物车中的所有商品都是60。
In this tutorial I'm going to talk to you guys about nested loops 在本教程中，我将与您讨论嵌套循环
in python. Using a nested loop basically means adding one loop 在python中。使用嵌套循环基本上意味着添加一个循环
inside of another loop, and with this technique we can do some amazing things 在另一个循环中，使用这种技术我们可以做一些惊人的事情
for example we can easily generate a list of coordinates. So, a 例如，我们可以轻松生成坐标列表。所以，一个
acquired (?) as you know is a combination of x and y 如你所知，获得（？）是x和y的组合
value. Let's say 0 and 0. Now let's say you wan to generate 值。让我们说0和0.现在让我们说你想生成
a list of coordinates like this. So we have 0 and 0, 像这样的坐标列表。所以我们有0和0，
then we'll have 0 and 1, then 0 and 2, next 接下来我们将有0和1，然后是0和2
we're going to change x, so we're going to use 1 for x, 我们要改变x，所以我们将使用1代表x，
and once again we're going to use these 3 values for the y coordinates 我们再一次将这三个值用于y坐标
so 1 and 0 then 1 and 1 所以1和0然后是1和1
and 1 and 2, you got the point. We can 1和2，你明白了。我们可以
easily generate these coordinates using nested loops. Let me show you. 使用嵌套循环轻松生成这些坐标。让我演示给你看。
So, we start with one loop, let's say for x in 所以，我们从一个循环开始，比方说x in
range 4. With this loop, we can generate value 范围4.通过这个循环，我们可以产生价值
for the x coordinate. Let's print this on the terminal, 对于x坐标。我们在终端上打印出来，
Okay, so, we get the values, 0 好的，我们得到的值是0
to 3. Now for each x, like 0, we 现在对于每个x，就像0，我们
should generate a few y values. So that is where we use 应该生成几个y值。这就是我们使用的地方
a nested loop. So inside of this loop we're going to add 嵌套循环。所以在这个循环中我们要添加
another loop, so instead of just printing x first we want to add 另一个循环，所以不是只打印x，而是先添加
another loop, for y in range 另一个循环，y在范围内
let's say 3, now we can print x and y together 让我们说3，现在我们可以一起打印x和y
so, print, here, we use a formatted string. 所以，打印，在这里，我们使用格式化的字符串。
to display coordinates like this. So we add parenthesis 显示这样的坐标。所以我们添加括号
inside of this parenthesis first we need to add x so curly braces 首先我们需要在这个括号内加上x这样的花括号
x then a comma followed by x然后是逗号，然后是
another set of curly braces, and y, 另一组花括号，和y，
let's run this program and see what we get. 让我们运行这个程序，看看我们得到了什么。
There you go. So, we have these coordinates 你去吧所以，我们有这些坐标
0 and 0, 0 and 1, 0 and 2, then you have 0和0,0和1,0和2，那么你有
1 and 0 1 and 1 1 and 2 and so on. So let me explain exactly 1和0 1和11和2等。所以让我解释一下
how this program gets executed. So in the first iteration, 该程序如何执行。所以在第一次迭代中，
of our outer loop, x is 0. Now we are 我们的外循环，x是0.现在我们是
on line 2, here we have a new loop which we call an 在第2行，这里我们有一个新的循环，我们称之为
inner loop. In this inner loop, in the first iteration y 内循环。在这个内循环中，在第一次迭代y中
is going to be 0, so we print 0 and 0 on the 将是0，所以我们打印0和0
terminal. Now the control goes back to line 2 or our 终奌站。现在控制回到第2行或我们的
inner loop. In this second iteration y will be set to 1, but 内循环。在第二次迭代中，y将设置为1，但是
we are still in the first iteration of our outer loop. So 我们仍处于外循环的第一次迭代中。所以
x is still 0, but now y is incremented to 1. x仍为0，但现在y增加到1。
So that is why we see 0 and 1 on the terminal. 这就是为什么我们在终端上看到0和1的原因。
Once again, the control goes back to line 2, we are in the third 控制再次回到第2行，我们在第3行
iteration of our inner loop, so this will continue until our inner loop 迭代我们的内循环，所以这将继续直到我们的内循环
completes. That is when y reaches 2 because 完成。那是当y达到2时因为
this range function generates number 0 to 3 but not including 此范围函数生成数字0到3但不包括
3. So we'll have 0 1 and 2. After this inner loop completes, 所以我们将得到0 1和2.在这个内循环完成之后，
then the control goes back to line 1, and at this point we're going to 然后控制回到第1行，此时我们要去
be in the second iteration of our outer loop. So x will be 在我们的外循环的第二次迭代中。所以x会
1, then the control will be moved to line 2, or our inner 1，然后控件将移动到第2行，或我们的内部
loop, at this point, this range function is going to generate the numbers 0 循环，此时，此范围函数将生成数字0
to 3 one more time. So this inner loop will be executed 3 times. 到了3再一次。所以这个内循环将执行3次。
And then we'll go back to our outer loop. So this is how 然后我们将回到我们的外循环。所以这是怎么回事
nested loops get executed. 嵌套循环执行。
Okay, here's an exercise for you, but this one is a little bit 好的，这是给你的练习，但这个练习有点儿
more challenging then the exercises you have done so far. So I really don't 比你迄今为止所做的练习更具挑战性。所以我真的没有
expect you to do it, but if you do it, wow, I will be so proud of you, 期待你这样做，但如果你这样做，哇，我会为你感到骄傲，
so see what I've done here? Using nested loops, 那么看看我在这里做了什么？使用嵌套循环，
I've written some code to draw this f shape, can you see that? So I 我已经写了一些代码来绘制这个f形状，你能看到吗？所以我
let me give you a hint, first of all we have this 让我给你一个提示，首先我们有这个
list, called numbers, in this list we have these values, 列表，被叫号码，在这个列表中我们有这些值，
5, 2, 5, 2, 5, 2. These values determine 5,2,5,2,5,2。这些值决定了
the number of x's we have in each line. So, 每行中x的数量。所以，
for example, the first item in this list, this tells us that we should have 例如，这个列表中的第一项，这告诉我们应该有
5 x's on the first line. There you go, so 1, 2, 3, 第一行5 x。你去，所以1,2,3，
4, 5, on the second line we're going to have 2 x's, 4,5，在第二行，我们将有2 x，
on the third line we're going to have 5 x's like this. 在第三行，我们将有5 x这样的。
So I've written code to convert a simple list of numbers 所以我编写了代码来转换一个简单的数字列表
into a shape like this. Now here's a tip for you. 变成这样的形状。现在这里给你一个提示。
Using your for loop you need to iterate over this list. In 使用for循环，您需要遍历此列表。在
each iteration you get one number, this determines the number of x's 每次迭代得到一个数字，这决定了x的数量
to be displayed on that particular line. So if you want to cheat, 显示在该特定行上。所以，如果你想作弊，
you can get this number and multiply by a string that contains x, 你可以得到这个数字并乘以一个包含x的字符串，
so if you multiply x by 5, we'll get 5 x's, that's not what 因此，如果你将x乘以5，我们将得到5 x，那不是什么
I want you to do. I want you to use an inner loop here to 我要你做。我希望你在这里使用内循环
generate a string that contains 5 x's. So imagine 生成一个包含5 x的字符串。想象一下
in Python we cannot multiply a string by a number 在Python中，我们不能将字符串乘以数字
so to solve this problem we need a nested loop. So go ahead and spend 所以要解决这个问题，我们需要一个嵌套循环。所以继续花钱
five minutes on this exercise. And by the way, do your best to solve this. It is 五分钟的练习。顺便说一句，尽力解决这个问题。它是
a little bit challenging, but it's not extremely difficult. It just requires 有点挑战，但并不是非常困难。它只需要
a little bit of focus. You'll see my solution next. 一点关注。接下来你会看到我的解决方案。
Alright so first we need to iterate over 好吧首先我们需要迭代
all the items in this list. So for item 此列表中的所有项目。所以对于项目
in numbers, or, you could rename this variable 在数字中，或者，您可以重命名此变量
to x_count. That is the number of x's 到x_count。这是x的数量
on each line. Okay? Now I told you that if you want to cheat you can write code 在每一行。好的？现在我告诉你，如果你想作弊，你可以编写代码
like this. Print x times 像这样。打印x次
x underline count. If you run this program we get the same x下划线计数。如果你运行这个程序我们得到相同的
output. So this is the beauty of Python, with Python 输出。所以这就是Python的美丽，用Python
we can write expressions like this, we can multiply a string by a number to 我们可以写这样的表达式，我们可以将一个字符串乘以一个数字
repeat it. A lot of other programming languages don't support this feature. But 重复。许多其他编程语言不支持此功能。但
for this exercise, I wanted you to imagine that we don't have this feature in Python 对于本练习，我想让你想象我们在Python中没有这个功能
so you will have to use an inner loop to solve this problem. Here's how 所以你必须使用内循环来解决这个问题。这是如何做
it works. In the first iteration, x count is going to be 5. 有用。在第一次迭代中，x count将为5。
So we need to generate 5 x's. How can we do that? 所以我们需要生成5个x。我们怎么做？
Well, let's say we define a variable called output and initially set it to 好吧，假设我们定义了一个名为output的变量，并将其初始设置为
an empty string. Now we need to add 5 x's to this string. So, 一个空字符串。现在我们需要为此字符串添加5 x。所以，
we can use another loop for count in range, 我们可以使用另一个循环来计算范围，
of x underline count. So we're using the range function x下划线计数。所以我们正在使用范围功能
to generate a sequence of numbers from 0 up to 从0到最多生成一系列数字
x count. So in our first iteration x count is going to be 5, so x计数。所以在我们的第一次迭代中，x count将是5，所以
range of 5 would generate the numbers 1, 2, 3, 4. 范围5将生成数字1,2,3,4。
So this inner loop will be executed 5 times. 所以这个内循环将被执行5次。
That is exactly what this count represents. 这正是这个数字所代表的。
So now in each iteration we simply need to 所以现在在每次迭代中我们都需要
append an x to our output variable. So we set 将x附加到输出变量。所以我们设定
output, plus, equals x, 输出，加，等于x，
and then after this inner loop we simply print 然后在这个内循环之后我们简单地打印
the output. With his we'll print 5 x's on 输出。有了他，我们将打印5 x
the first row. Then we go to the second iteration of our outer loop. 第一排。然后我们进入外循环的第二次迭代。
At this point x count is going to be 2, now 此时x count将是2，现在
on line 3, we're going to reset our output variable to an 在第3行，我们将把输出变量重置为
empty string. So we start over. Then we go to our inner loop, 空字符串。所以我们重新开始。然后我们去内循环，
this loop will be executed 2 times, so we'll append to x's 这个循环将被执行2次，所以我们将附加到x
to the output variable and then print it, as simple as that. 到输出变量然后打印它，就这么简单。
So see, it wasn't really that difficult, but it was slightly more difficult then the 所以看，它并不是那么困难，但它稍微困难一些
previous exercises. So let's run this program, there you go. 以前的练习。所以，让我们运行这个程序吧。
Now if you're adventurous, I want you to modify the values that we have 现在，如果你喜欢冒险，我希望你修改我们拥有的价值观
in our numbers list, 在我们的号码列表中，
to print and l here. 在这里打印和l。
Now in this tutorial we're going to take a closer look at lists. 现在在本教程中，我们将仔细研究列表。
So, I'm going to define a list of names, let's send them 所以，我要定义一个名单列表，让我们发送它们
to John Bob Mosh 致John Bob Mosh
Sarah and Mary. So, 莎拉和玛丽。所以，
if you print this list here, what we see on 如果你在这里打印这个列表，我们看到的是什么
the terminal look exactly like how we define our lists we have 终端看起来与我们定义列表的方式完全一样
square brackets, and in between these square brackets we have our item 方括号，在这些方括号之间我们有我们的项目
our items, so we have 5 strings in this list. 我们的项目，所以我们在这个列表中有5个字符串。
So we can also access an individual element using an index just like 所以我们也可以使用索引来访问单个元素
how we can access an individual character in a string using an index. 我们如何使用索引访问字符串中的单个字符。
So, here we type out square brackets, 所以，在这里我们输入方括号，
and specify an index. The index of the first item in this list 并指定一个索引。此列表中第一项的索引
is 0. So, let's run this program, there you go, 是的。所以，让我们运行这个程序，你去了，
we get Jon now if you want to print out the third element in this list 如果你想打印出这个列表中的第三个元素，我们现在就得到Jon
it's index is 2, so names of 2, 它的索引是2，所以2的名字，
returns Mosh, now we can also pass 回到莫什，现在我们也可以通过
a negative index here, so negative 1, refers to the last item 这里的负指数，所以负1，指的是最后一项
in this list, that is Mary, let's run the program, 在这个列表中，就是Mary，让我们运行程序，
there you go, we see Mary, if you pass negative 2, this 你去，我们看到玛丽，如果你通过负2，这
returns the second item from the end of the list. So let's run the program, 返回列表末尾的第二项。那么让我们运行程序，
there you go, we get sarah, so this is exactly like accessing 你去，我们得到莎拉，所以这就像访问
individual characters in a string. We can also 字符串中的单个字符。我们也可以
use a colon to select a range of items, for example if 使用冒号选择一系列项目，例如if
you pass 2 colon. This will get all items 你通过2冒号。这将获得所有项目
starting from the index of 2, that is Mosh here all the 从索引2开始，这就是莫什所有的
way to the end of the string. So let's run this program 通往字符串末尾的方式。所以让我们运行这个程序
there you go. We get this list with 3 你去吧我们得到这个列表3
items, Mosh, Sarah, and Mary. We can also specify 物品，莫什，莎拉和玛丽。我们也可以指定
an end index, let's say 4, so this will return all 一个结束指数，让我们说4，所以这将返回所有
the items up to this index, but it doesn't include the item at this index. 直到此索引的项目，但不包括此索引处的项目。
So when we run this program we only see Mosh and 因此，当我们运行这个程序时，我们只看到Mosh和
Sarah, the item I've indexed 4which is the 5th element or 莎拉，我索引的项目4是第5个元素或者
5th item in this list is not returned. So Mary 此列表中的第5项不会被退回。玛丽
is not returned. And also here we have default values, so if you 不归还。而且这里我们有默认值，所以如果你
leave out the end index this expression is going to return 省略这个表达式要返回的结束索引
all the items starting from the index of 2, to the end of the list 从索引2开始到列表末尾的所有项目
or if you leave out the start index, this expression asumes 或者如果你省略了起始索引，这个表达式就会出现
0 as the default index, so it will return all the items from the 0作为默认索引，因此它将返回所有项目
beginning to the end of the list. And by the way, 从列表的末尾开始。顺便说一下，
just like strings, these square brackets here, don't modify our 就像字符串，这里的方括号，不要修改我们的
original list, they simply return a new list. For example, if we pass 原始列表，他们只是返回一个新列表。例如，如果我们通过
2 here, you can see this returns a new list with 3 items. 2在这里，你可以看到这返回一个包含3个项目的新列表。
So if you go back here and print our original 所以，如果你回到这里打印我们的原件
list of names right after you can see it's not affected. 您可以看到名称列表没有受到影响。
So here we want to use square brackets with a colon to select a 所以这里我们要使用带有冒号的方括号来选择a
range of items, we get a new list, and by the way we can also 项目范围，我们得到一个新的清单，顺便说一下我们也可以
modify any of the elements in this list. For example let's say we made a mistake and 修改此列表中的任何元素。例如，假设我们犯了一个错误
the first item shouldn't be john with an h, 第一项不应该是约翰与h，
so we want to remove the h, that is very easy, so we access it, 所以我们要删除h，这很容易，所以我们访问它，
using an index that is names of 0, and we set it to a new value 使用名称为0的索引，我们将其设置为新值
like this. Now, let's print our list 像这样。现在，让我们打印我们的列表
so, you can see the first item in this list 所以，你可以看到这个列表中的第一项
is now data. So this is the basics of lists. And here's a 现在是数据。所以这是列表的基础知识。这是一个
exercise for you. I want you to write a program to find the largest number 为你锻炼。我希望你写一个程序来找到最大的数字
in your list. This is a fantastic exercise for beginners, so go ahead and 在你的清单中。对于初学者来说这是一个很棒的练习，所以请继续
spend a few minutes on this, then come back and continue watching. 花上几分钟，然后回来继续观看。
Alright, let's 好吧，我们来吧
define a list of numbers, numbers, with a bunch of random numbers 用一堆随机数定义一个数字，数字列表
3, 6, 2, 8, 4 and 10. 3,6,2,8,4和10。
Now to find the largest number in this list. We need to define another 现在找到这个列表中的最大数字。我们需要定义另一个
variable let's call it max, this variable will hold 变量让我们称之为最大值，这个变量将成立
the largest number, now initially you want to assume 最大的数字，现在最初你想要假设
the first item in this list is the largest number. So we set max to 此列表中的第一项是最大的数字。所以我们将max设置为
numbers of 0. We're only assuming that the first item 数字为0.我们只假设第一项
is the largest number. Chances are our assumption is wrong. So we need 是最大的数字。机会是我们的假设是错误的。所以我们需要
to iterate over this list, we need to loop through it, get 迭代这个列表，我们需要遍历它，得到
each item and compare it with max. If it's greater than Max, then 每个项目并与最大值进行比较。如果它大于Max，那么
we need to reset max to that number. So in the first iteration we get 我们需要将max重置为该数字。所以在第一次迭代中我们得到了
3, and max is also 3. Is 3 greater than 3，最大也是3.大于3
3. No, so we move on, we get the second number, is 不，所以我们继续前进，我们得到第二个数字，是
6, greater than 3? It is, so we need to reset max 6，大于3？是的，所以我们需要重置最大值
to 6. Once again, we continue, we get 2, these two greater 再次，我们继续，我们得到2，这两个更大
than 6, no it's not, so we move on, then we get 8, 不是6，不是，所以我们继续前进，然后我们得到8，
is 8 greater than 6? It is, so we should reset max to 8. 是8大于6？它是，所以我们应该将max重置为8。
That is pretty easy. So here we need a for loop, for 这很容易。所以这里我们需要一个for循环
number in numbers colon 数字冒号
now we need too heck to see if this number is greater than max, 现在我们需要看看这个数字是否大于最大值，
so, if number is greater than max, 所以，如果数字大于最大值，
max colon then we need to reset max, so max 最大冒号，然后我们需要重置最大值，所以最大
to this new number, that's all we had to do. 对于这个新数字，这就是我们所要做的。
So, let's print max, and run our 所以，让我们打印最大值，然后运行我们的
program we can see the largest number in this list is 10. 程序我们可以看到这个列表中最大的数字是10。
It doesn't matter whether this number is at the end of the list or the beginning. 这个数字是在列表的末尾还是在开头是无关紧要的。
So if I move 10 and put it right at the beginning 所以，如果我移动10并在开头就把它放好
we should still see the same result. Let's run our program, 我们仍然应该看到相同的结果。让我们运行我们的程序，
we still see 10, if I put this somewhere in the middle, our 我们仍然看到10，如果我把它放在中间的某个地方，我们的
program should still work. Let's put it right after 2. 程序应该仍然有效。我们把它放在2之后。
Run the program, we still get 10. 运行程序，我们仍然得到10。
In this tutorial I'm going to talk to you guys about two dimensional 在本教程中，我将与你们讨论二维问题
lists in python. Two dimensional lists are extremely powerfully and they have a lot python中的列表。二维列表非常强大，它们有很多
if applications in data science and machine learning. Here's 如果应用于数据科学和机器学习。这里的
an example, In math we have a concept called matrix, which is like 例如，在数学中我们有一个叫做矩阵的概念，就像
a rectangular array of numbers, let me show you. So 一个矩形的数字，让我告诉你。所以
we have 1, 2, 3, 4, 5, 6, 7, 8, 9. 我们有1,2,3,4,5,6,7,8,9。
So we have a rectangular array of numbers. You have rows, 所以我们有一个矩形数组。你有行，
and columns. So this is a 3 x 3 matrix 和列。所以这是一个3 x 3矩阵
in math. Now we can model this in python using 在数学中。现在我们可以在python中使用它来建模
a 2 dimensional list. A 2 dimensional list is a list where 二维列表。二维列表是其中的列表
each item in that list is another list. So, 该列表中的每个项目都是另一个列表。所以，
you want to define a matrix. We set it to a list, each 你想定义一个矩阵。我们将它们设置为一个列表
item in this list, is going to be another list, and that list represents 此列表中的项目将是另一个列表，该列表表示
the items in each row. So, the first 每行中的项目。所以，第一个
item in our list is going to be another list, and in this other list we're going to have 我们列表中的项目将成为另一个列表，在另一个列表中我们将会有
the values 1, 2, and 3. Now the second 值1,2和3.现在是第二个
item in our matrix list, once again, we have a list, 我们的矩阵列表中的项目，再次，我们有一个列表，
this list represents the items in the second row. So, 此列表表示第二行中的项目。所以，
4, 5, and 6, and finally 7, 8 4,5和6，最后是7,8
9. So as you can see we have a 2 dimensional list 9.所以你可以看到我们有一个2维列表
Each item in our outer list is another list. 我们外部列表中的每个项目都是另一个列表。
Okay? So, this is how we can implement a matrix in 好的？所以，这就是我们如何实现矩阵的方法
python, now to access an individual item in our matrix, python，现在访问矩阵中的单个项目，
once again we use square brackets, let me delete this stuff, 再次我们使用方括号，让我删除这些东西，
alright, so how do we access 1 here, 好的，那么我们如何在这里访问1，
well, you start with our list then we add square 好吧，你从我们的列表开始然后我们添加square
brackets first we need to go and get the first item in this list. 括号首先我们需要去获取此列表中的第一项。
Right? So we pass 0, now this expression 对？所以我们传递0，现在这个表达式
returns another list. That is the inner list. In this list, 返回另一个列表。这是内部清单。在此列表中，
let's say you want to access the second item, so, 假设你要访问第二个项目，所以，
once again we add square brackets, and we pass 1, that 我们再一次添加方括号，我们传递1，即
is the index of 2 in this list, alright. 这个列表中的索引是2，好吧。
So if we print this on the terminal 所以，如果我们在终端上打印这个
we get 2. Okay? So using 2 square brackets 我们得到2.好吗？所以使用2个方括号
we can access individual items in our matrix, and 我们可以访问矩阵中的各个项目
also we can modify these values using this syntax, so, 我们也可以使用这种语法修改这些值，所以，
before printing this, let's change this to 20, so matrix 在打印之前，让我们将其更改为20，因此矩阵
of 0, and 1, let's change it to 20, 0和1，让我们把它改成20，
and then print it, there you go, it's modified, now here you 然后打印它，你去，它被修改，现在在这里
can also use nested loops to iterate over all the items 也可以使用嵌套循环迭代所有项目
in this matrix. Let me show you. So, 在这个矩阵中。让我演示给你看。所以，
we start with a rows for the rows in matrix, 我们从矩阵中的行开始，
so with this loop, we are iterating over our Matrix 所以有了这个循环，我们正在迭代我们的Matrix
list, in each iteration row will contain 1 列表，在每个迭代行中将包含1
list, 1 item, okay? Now, here we need to 列表，1项，好吗？现在，我们需要
use an inner loop. So we need to loop over this row 使用内循环。所以我们需要遍历这一行
which is a list of items. We can type out 4 这是一个项目列表。我们可以打出4
item in row colon and print 行冒号和打印中的项目
item. Okay? So, let's run this program, 项目。好的？那么，让我们运行这个程序，
there you go, we get all the item 你去，我们得到所有的项目
in our list. Hey, I just 在我们的列表中。嘿，我
wanted to make a quick announcement. In case you haven't seen my website yet, head over to codewithmosh.com 我想快速公布一下。如果您还没有看过我的网站，请访问codewithmosh.com
this is my coding school, where you can find plenty of courses 这是我的编码学校，在那里你可以找到很多课程
on web and mobile application development. In fact recently I published a 在网络和移动应用程序开发。事实上最近我发表了一篇
comprehensive python course that goes way beyond this beginners 全面的python课程，超越了这个初学者
youtube. And if you're serious about learning python and getting a job, I highly encourage YouTube上。如果你认真学习python和找工作，我非常鼓励
you to enroll in this course. Just like this tutorial you can watch it anytime, 你参加这门课程。就像本教程一样，您可以随时观看，
anywhere as many times as you want, and you will also receive a certificate 您可以随时随地多次获得证书
of completion that you can add to your resume, and the course comes with a 30 day 完成后，您可以添加到您的简历，该课程为期30天
money back guarantee, so if you're not happy we'll give all your money back, 退款保证，所以如果你不开心，我们会把你所有的钱还给我，
no questions ask. The price for this course is 149 dollars, but 没有问题。这门课程的价格是149美元，但是
the first 200 students can get it for only 15 dollars. So if you're interested, 前200名学生只需15美元即可获得。所以，如果你有兴趣，
the link is below this video, click the link, and get started. 链接在此视频下方，单击链接，然后开始使用。
In this tutorial, I'm 在本教程中，我是
going to talk to you guys about the list methods, or list functions. These are the 打算跟你们谈谈列表方法或列表功能。这些是
operations that we can perform in a list. So let's define 我们可以在列表中执行的操作。所以我们来定义
a list of numbers, here we pass a bunch of random numbers, like 一个数字列表，这里我们传递一堆随机数，比如
5, 2, 1, 7, and 5,2,1,7和
4. Now there are a number of things we can do with this list, we can 我们可以用这个列表做很多事情
add new items to it, we can remove existing items, we can check for the existence of an item 添加新项目，我们可以删除现有项目，我们可以检查项目是否存在
these are the operations that we can perform on a list. So, 这些是我们可以在列表上执行的操作。所以，
the we type numbers, we can see all these functions or more accurately 我们输入数字，我们可以更准确地看到所有这些功能
the methods that are available in our list objects. 我们的列表对象中可用的方法。
So we can call the append method to add a new item to this list. Let's say 因此我们可以调用append方法将新项添加到此列表中。让我们说吧
13. Actually, no it's not a good number. 实际上，不，这不是一个好数字。
I'm joking I'm not superstitious, so let's add 20, it 我在开玩笑，我不是迷信，所以让我们加20，它
doesn't really matter and then print our list 没关系，然后打印我们的清单
when we run the program we can see 20 is added at the end of this list 当我们运行程序时，我们可以看到在此列表的末尾添加了20
but what if you want to add a number somewhere in the middle, or at the beginning 但是，如果你想在中间或开头的某处添加一个数字，该怎么办？
of our list? For that we use a different method, that is called 我们的名单？为此，我们使用一种称为的不同方法
insert. So, insert, now this method 插入。所以，插入，现在这个方法
takes 2 values, let me show you. So when we open parenthesis 需要2个值，让我告诉你。所以当我们打开括号时
look at this little tool tip above the insert method. 看看插入方法上方的这个小工具提示。
You see the first value that we need to pass here is an index, so 你看到我们需要传递的第一个值是索引，所以
this is the index at which we want to insert this new item. Let's 这是我们要插入此新项目的索引。让我们
say we want to add an item at the beginning of our list, so we passed our index position 假设我们想在列表的开头添加一个项目，所以我们通过了索引位置
of 0, and then the second value is the actual 为0，然后第二个值是实际值
object we want to add to this list. Let's say we want to add the number 10, 我们要添加到此列表的对象。假设我们要添加数字10，
now when we print this list you can see that the 现在，当我们打印此列表时，您可以看到
number 10 is placed at the beginning of the list and 数字10放在列表的开头和
all the other items are pushed to the right. We can also remove an item so 所有其他项目都被推到右边。我们也可以删除一个项目
we call remove and pass the item that 我们调用remove并传递该项
we want to remove 5. Now we 我们要删除5.现在我们
print our list so we see 5 is gone, and we have 打印我们的列表，所以我们看到5已经消失，我们有
2, 1, 7, 4. If you want to remove all the items in the list, you can call 2,1,7,4。如果要删除列表中的所有项目，可以拨打电话
the clear method, so, clear 明确的方法，所以，明确
this method doesn't take any values, so, we simply 这个方法不带任何值，所以，我们简单地说
call it and it empties our list, all the items are removed. 调用它并清空我们的列表，删除所有项目。
We also have another useful method called hop, 我们还有另一个有用的方法叫做hop，
and with this we can remove the last item in a list. 并且我们可以删除列表中的最后一项。
Let me show you. So, we run our program, you can see the number 4 让我演示给你看。所以，我们运行我们的程序，你可以看到数字4
is removed from the end of our list. Now if you want to 从我们列表的末尾删除。现在，如果你想
check for the existence of an item in our list, you can call the index method. 检查列表中是否存在项目，可以调用索引方法。
So, we call index and pass a value here, like 5 所以，我们调用index并在这里传递一个值，比如5
and this returns the index of the first 并返回第一个索引
occurrence of this item. So let's print this on the terminal. 发生这个项目。所以让我们在终端上打印出来。
We don't need this line anymore. So the 我们不再需要这条线了。所以
index of 5 is 0. What if we pass a number that doesn't exist index of 5是0.如果我们传递一个不存在的数字怎么办？
in this list? Let's say 50? Run the program, 在这个清单？我们说50？运行程序，
we get an error. We get a value error. 50 is not 我们收到一个错误。我们得到一个值错误。 50不是
in the list. There is also another way to check for the existence of an item, 在列表中。还有另一种检查项目是否存在的方法，
we can use the in operator. So, let me show you 我们可以使用in运算符。所以，让我告诉你
we type our 15 in numbers, 我们输入15个数字，
earlier we used the in operator with a string, we 我们之前使用了带字符串的in运算符
check for the existence of a character or a sequence of a character in a string, 检查字符串中是否存在字符或字符序列，
now here we're checking for the existence of 50 in the list of numbers, so 现在我们在这里检查数字列表中是否存在50，所以
let's print this, we get a boolean value, 让我们打印这个，我们得到一个布尔值，
false, so unlike the index method, this expression, false，这与索引方法不同，这个表达式，
doesn't generate an error, so it's safer to use this. We also have another 不会产生错误，因此使用它更安全。我们还有另一个
method for counting the occurances of an item, let's say we have 计算物品出现的方法，假设我们有
another 5 over here. 另外5人在这里。
Now we can call numbers.count and pass 5 现在我们可以调用numbers.count并传递5
and this should return 2 because we have 2 5s in this list. 这应该返回2，因为我们在这个列表中有2 5个。
Take a look. There you go. That is pretty useful 看一看。你去吧这非常有用
now if you want to sort your lists you can call sort method. So, 现在，如果要对列表进行排序，可以调用sort方法。所以，
we call the sort method here, this method doesn't take any values so, 我们在这里调用sort方法，这个方法不带任何值，所以
look at he return value, that is none, none 看看他的回报值，即没有，没有
is an object in python that represents the absence of a value. 是python中的一个对象，表示缺少值。
So this sort method doesn't really return any values it simply sorts this 所以这种排序方法并没有真正返回任何值，它只是对它进行排序
list, in place, so instead of printing the return value 列表，到位，所以而不是打印返回值
oft his method, we simply call it to sort our list 他的方法，我们只是称它来排序我们的列表
and then print our list. Take a look. 然后打印我们的列表。看一看。
Now, all the items are assorted in ascending order. We can also 现在，所有项目按升序排列。我们也可以
sort the items in descending order, so after we sort the list 按降序对项目进行排序，因此在对列表进行排序后
we can call the reverse method. We can simply reverse 我们可以调用反向方法。我们可以简单地逆转
our list. Now let's go ahead and run our program, take a look. 我们的清单。现在让我们继续运行我们的程序，看一看。
Our numbers are sorted in descending order. 我们的数字按降序排列。
And one last method I want to show you here that is pretty useful is the copy 我想在这里向你展示的最后一种方法是复制
method. So copy, with this method you can get a copy 方法。所以复制，用这种方法你可以得到一份副本
of our list. So let's define another variable called numbers 2, 我们的清单。那么让我们定义另一个名为number 2的变量，
now numbers 2, is a copy of our original list. So if 现在的数字2，是我们原始列表的副本。因此，如果
you make any changes to our original list, if you add new items to it, if you remove 如果您删除了新项目，则对原始列表进行任何更改
existing items, these operations are not going to impact our second list. 现有项目，这些操作不会影响我们的第二个清单。
Let me show you. So after we show you a copy of our numbers list 让我演示给你看。在我们向您展示我们的号码列表副本之后
let's add a new item to this list, so numbers.apphend 让我们在这个列表中添加一个新项，所以numbers.apphend
ten. So the first list is updated, so now we have a 十。所以第一个列表已更新，所以现在我们有了
new item in our first list, then lets print the second list. 我们的第一个列表中的新项目，然后让我们打印第二个列表。
Take a look, we don't have the number 10 here, because these are 看一看，我们这里没有数字10，因为这些都是
2 independent lists. So these are all the operations 2个独立清单。所以这些都是操作
that we can perform on lists. We can add new items to a list, we can 我们可以在列表上执行。我们可以将新项目添加到列表中
remove exxisitn items, we can check for the existence of an item, we can sort 删除exxisitn项目，我们可以检查项目的存在，我们可以排序
our list, and copy them. Now here's an exercise for this tutorial, 我们的清单，并复制它们。现在这是本教程的练习，
I want you to write a program, to remove the duplicates on our list. 我希望你写一个程序，删除我们列表中的重复项。
Again, this is a fantastic exercise. So spend a few minutes on this and then 再次，这是一个很棒的练习。所以花几分钟就可以了
come back and continue watching. 回来继续看。
Alright let's say we have a list of numbers with 好吧，让我们说我们有一个数字列表
a bunch of duplicates. So 2, 2, 4, 6, 一堆重复。所以2,2,4,6，
6, 3, 4, 6, 1. We want to remove 6,3,4,6,1。我们要删除
the duplicates. So we need to define another list 重复。所以我们需要定义另一个列表
let's call that new list. Initially we 让我们称之为新名单。最初我们
set it to an empty list. Then we need to iterate over 将其设置为空列表。然后我们需要迭代
our first list, get each item, and if we don't have that number 我们的第一个清单，获取每个项目，如果我们没有这个数字
in this unix list, then we'll add it to this second list. 在这个unix列表中，然后我们将它添加到第二个列表中。
As simple as that. So, for number 就如此容易。所以，对于数字
in numbers, now we need to check to see if we 在数量上，现在我们需要检查一下，看看我们是否
have this number in the second list. So we use the in 在第二个列表中有这个号码。所以我们使用in
operator. If number not 运营商。如果数字不是
in units 单位
so if we don't have an operator in this units list, 所以如果我们在这个单位列表中没有运营商，
then we'll need to add it, so units. 然后我们需要添加它，所以单位。
apphend number, that's all we have to do. apphend number，这就是我们所要做的。
So, let's go ahead and print the units list 那么，让我们继续打印单位列表
there you go. So you have 2, 4, 你去吧所以你有2,4，
6, 3,and 1. The duplicates are removed. 6,3和1.删除重复项。
In this tutorial I'm going to talk to you guys about another important structure. 在本教程中，我将与您讨论另一个重要结构。
in Python called tuple. Tuples are similar to lists 在Python中称为元组。元组类似于列表
so we can use them to store a list of items. But unlike lists we can 所以我们可以用它们来存储一个项目列表。但与列表不同，我们可以
not modify them, we cannot add new items, we cannot remove existing items, we 不修改它们，我们无法添加新项目，我们无法删除现有项目，我们
tuples are immutable. We cannot mutate or change them. So 元组是不可变的。我们不能改变或改变它们。所以
let me show you. So I'm going to start by defining a list of numbers, 让我演示给你看。所以我将首先定义一个数字列表，
1, 2, 3, 4, so we use square brackets to define lists and 1,2,3,4，所以我们使用方括号来定义列表和
parenthesis to define tuples, so if we change this to 用于定义元组的括号，因此如果我们将其更改为
parenthesis, 1, 2, 3. Now we have a 括号，1,2,3。现在我们有了
tuple. So if we type numbers. 元组。所以，如果我们输入数字。
look here we don't have the append or insert methods, so we cannot add new items to this tuple. 看这里我们没有append或insert方法，所以我们不能向这个元组添加新项。
We also don't have remove clear and pop, we cannot 我们也没有删除清晰和流行，我们不能
remove any of these items here. We only have two methods, count, 在这里删除任何这些项目。我们只有两种方法，伯爵，
and index. We use count to count the number of occurrences in an item, 和索引。我们使用count来计算项目中出现的次数，
and index, to find the index of the first occurrence, 和index，找到第一次出现的索引，
of an item. So we can only get information about a tuple, we can't change it. 一件物品。所以我们只能获得有关元组的信息，我们无法改变它。
And by the way, these other methods that you see here, they start 顺便说一句，你在这里看到的其他方法，他们就开始了
with two underscores, we refer to these as magic methods, they're more of an advanced 有两个下划线，我们将这些称为魔术方法，它们更像是一种先进的方法
topic, so they go beyond the scope of this tutorial. If you're interested 主题，因此它们超出了本教程的范围。如果你有兴趣
to learn bout them. You can get my python course, I've covered them in detail. 学习回合他们。你可以得到我的python课程，我已经详细介绍了它们。
So similar to lists you can address individual items using 与列表类似，您可以使用单个项目来处理
squre bracketts,s o we can get the first item like this and then squre bracketts，所以我们可以得到这样的第一个项目然后
print another terminal, there you go, the first item is 1, 打印另一个终端，你去，第一项是1，
but if you try to change the first item we'll get an error, so, 但如果你试图改变第一项我们会得到一个错误，所以，
numbers of 0, we set it to 10 and run our program 数字为0，我们将其设置为10并运行我们的程序
there you go. We get this type error, because the tuple object 你去吧我们得到这种类型错误，因为元组对象
does not support item assignment. So we cannot 不支持项目分配。所以我们做不到
mutate or change tuples, they are immutable. Now practically 变异或改变元组，它们是不可变的。现在实际上
speaking, most of the time you'll be using lists, but tuples are also 说来，大多数时候你会使用列表，但元组也是
useful. If you want to create a list of items and make sure 有用。如果要创建项目列表并确保
no where in your program you accidentally modify that list, then it's better to 没有在你的程序中你不小心修改该列表，那么它最好
use a tuple, 使用元组，
In this tutorial I'm going to show you a powerful 在本教程中，我将向您展示一个强大的功能
feature we have in python called unpacking. 我们在python中称为解包的功能。
So lets find a tuple called coordinates 所以我们找一个叫做坐标的元组
and here we pass 3 values, 1, 2, 3. So you imagine these are 在这里我们传递3个值，1,2,3。所以你想象这些是
the coordinates for x y and z. Now let's say xy和z的坐标。现在让我们说吧
we want to get these values and use them in a few expressions, a few complex 我们希望得到这些值，并在一些表达式中使用它们，一些复杂的表达式
expressions in our program. Maybe we want to include them as part of a large 我们程序中的表达式。也许我们想把它们作为一个大的一部分
complex formula. So together we'll have to write code like this, 复杂的公式。所以我们一起编写这样的代码，
coordinates of 0, then let's say we want to multiply this 坐标为0，那么就说我们想要乘以这个
by coordinates of 1, and then multiply it by coordinates 坐标为1，然后乘以坐标
of 2, our code is getting a little bit too long, this is just 2，我们的代码有点太长了，这只是
a very simple example. But let's say we want to use these values in quite a few places 一个非常简单的例子。但是，假设我们想在很多地方使用这些值
in our program, a better approach is to get these values 在我们的计划中，更好的方法是获得这些价值
and store them in separate variables like we can get 并将它们存储在我们可以得到的单独变量中
coordinates of 0, and store it in x, then you can 坐标为0，并将其存储在x中，然后就可以了
get coordinates of 1 and store it in y. 获取坐标1并将其存储在y中。
And similarly we get coordinates of 2, 同样我们得到2的坐标，
and then store it in z. Now we started repeating coordinates of 然后将其存储在z中。现在我们开始重复坐标
2 or coordinates of 0 multiple times, we can simply work with these 2或多次坐标0，我们可以简单地使用这些
variables, x times y times z, that is better. 变量，x乘以y乘以z，这是更好的。
Right? So nothing new so far. But in Python we have a powerful 对？所以到目前为止还没有什但在Python中我们有一个强大的功能
feature called unpacking and with that we can achieve 称为解包的功能，我们可以实现
the same result with far less code. So we can define our variables 代码少得多的结果相同。所以我们可以定义变量
x, y, and z. And set them to our x，y和z。并将它们设置为我们的
tuple. What we have on line 6 is exactly 元组。我们在第6行的确是如此
identical to what we have on lines 2-4. 与第2-4行的相同。
So this is a shorthand to achieve the same result. 所以这是实现相同结果的简写。
So let me delete this and explain how this code works. 所以让我删除它并解释这段代码是如何工作的。
When Python interpreter sees this statement, it will get the 当Python解释器看到这个语句时，它会得到
first item in this tuple and assign it to the 此元组中的第一项并将其分配给
variable. Then it will get the second item in this tuple, then 变量。然后它会得到这个元组中的第二个项目
similarly we get the third item in this tuple and assign it to 类似地，我们得到这个元组中的第三个项目并将其分配给
the third variable we have here, so we are unpacking 我们在这里的第三个变量，所以我们正在解包
this tuple into 3 variables. Now if we 这个元组分为3个变量。现在，如果我们
print x you can see x 打印x你可以看到x
is 1, similarly y is 2, there you go. 是1，同样y是2，你去。
So this is unpacking. And by the way this is not 所以这是拆包。顺便说一下，这不是
limited to tuples, we can use this feature for lists as well. So, 限于元组，我们也可以将此功能用于列表。所以，
if I change parenthesis to square brackets, now 如果我现在将括号更改为方括号
coordinates is a list, so we can unpack our list into 3 variables coordinates是一个列表，因此我们可以将列表解压缩为3个变量
now we run our program we can see y is 2. 现在我们运行我们的程序，我们可以看到y是2。
In this tutorial I'm going to talk to you about dictionary 在本教程中，我将与您讨论字典
in python. We use dictionaries in situations where we want to 在python中。我们在我们想要的情况下使用词典
store information that comes as key value pairs. Here's 存储作为键值对的信息。这里的
an example. Think of a customer. A customer has a bunch of attributes 一个例子。想想客户。客户有很多属性
like name, email, phone number, address 喜欢名字，电子邮件，电话号码，地址
and so on. Now each of these attributes has a value. 等等。现在，每个属性都有一个值。
For example the name can be jon smith the email can be 例如，电子邮件的名称可以是jon smith
jon@ gmail.com the phone can be whatevr jon @ gmail.com手机可以是whatevr
so what we have ere is a bunch of key value pairs. So in this 所以我们所拥有的是一堆关键价值对。所以在这
example, our keys are name, email, and phone, 例如，我们的密钥是姓名，电子邮件和电话，
and each key is associated with a value. So this is where we use 并且每个键都与一个值相关联。所以这就是我们使用的地方
a dictionary. With a dictionary, we can store a bunch of key value pairs. 一本字典。使用字典，我们可以存储一堆键值对。
So let me show you how to define a dictionary in python. 那么让我告诉你如何在python中定义一个字典。
Back to our program, I'm going to define a variable, customer, 回到我们的程序，我将定义一个变量，客户，
and here we set it to curly braces. With these curly braces we can 在这里我们将它设置为花括号。有了这些花括号，我们可以
define dictionary. In this example we have an empty dictionary that doesn't have 定义字典。在这个例子中，我们有一个没有的空字典
any key value pairs. Now we can add one or more key 任何键值对。现在我们可以添加一个或多个密钥
value pairs in between the braces. So let's 大括号之间的值对。那么我们
add a key value pair here, I'm going to set the key to a name, and 在这里添加一个键值对，我要将键设置为一个名称，然后
the value to John Smith, then we add a comma, 约翰史密斯的价值，然后我们添加一个逗号，
to add another key value pair. So let's set age to 添加另一个键值对。所以让我们设定年龄
30 let's add another key value pair is 30让我们添加另一个键值对
underline verified and we set this to a boolean. 下划线已验证，我们将其设置为布尔值。
Now what matters here is that these keys should be 现在重要的是这些键应该是
unique. So if I add another key value pair here, 独特。所以，如果我在这里添加另一个键值对，
set age to 40, now look pycharm 将年龄设定为40，现在看起来像pycharm
has highlighted the age key, because we have duplicated that and 突出了年龄的关键，因为我们重复了那个和
that's not allowed. So each key should be unique in a dictionary, just like, 这是不允许的。所以每个键在字典中应该是唯一的，就像，
the dictionaries we have in the real world, in a real dictionary we have a bunch of words 我们在现实世界中的词典，在一本真正的词典中我们有一堆词
and they're definition each word is only listed once in a dictionary. 并且它们的定义每个单词仅在字典中列出一次。
We don't have the word book twice. So let's delete the second 我们没有两次这个词。所以让我们删除第二个
duplicate key value pair, so the keys should be unique, 重复键值对，因此键应该是唯一的，
and in this example, I'm using strings, but they can also be 在这个例子中，我正在使用字符串，但它们也可以
numbers, we're going to look at that later, but the value can be anything, it can be 数字，我们稍后会看，但价值可以是任何东西，也可以
a string, a number, a boolean, a list, literally anything. 一个字符串，一个数字，一个布尔值，一个列表，字面上的任何东西。
Now we can access each item in this dictionary 现在我们可以访问这本词典中的每个项目
using square brackets. So, we type customer, 使用方括号。所以，我们输入客户，
square brackets and then specify key like name 方括号然后指定键名称
and this will return the value associated with 这将返回与之关联的值
the name key. Let's print it on the terminal, have a look, 名称键。我们把它打印在终端上，看看，
there you go, so, the name is John Smith, now 你去，所以，现在这个名字叫约翰史密斯
what if we pass a key that doesn't exist, lets say first date. 如果我们传递一个不存在的密钥怎么办，让我们说第一次约会。
You run the program, you get a key error because we don't have 你运行程序，你得到一个关键错误，因为我们没有
a key called birth date. Also, if we spell 一个叫出生日期的钥匙。另外，如果我们拼写
name with let's say a capital N, we get the same 名字让我们说一个大写N，我们得到相同的
error because we don't have a key with the exact same sequence of 错误，因为我们没有完全相同序列的密钥
characters in this dictionary. Now to get around this we can 这本词典中的字符。现在我们可以解决这个问题
use the get method, so instead of using the square brackets 使用get方法，而不是使用方括号
we call the get method and specify the key 我们调用get方法并指定密钥
Now if you use a key that doesn't exist here, it doesn't yell at us. For example, 现在，如果您使用此处不存在的密钥，它不会对我们大喊大叫。例如，
if you pass birth date 如果你通过出生日期
it simply returns the non value. Earlier I told you that none is an object 它只返回非值。早些时候我告诉过你，没有一个是对象
that represents the absence of a value. So 这表示没有价值。所以
instead of getting a key error we get none and we can also option 而不是得到一个关键错误我们得到没有，我们也可以选择
supply a default value, for example, if this dictionary doesn't have this 提供默认值，例如，如果此字典没有此值
key, we can supply the default value, let's say 关键，我们可以提供默认值，比如说
January 1st 1980. Let's run the program 1980年1月1日。让我们运行该程序
now instead of getting none we get this default value. 现在我们没有得到任何一个，而是得到这个默认值
So, this is how we can access the value associated with the key 因此，这就是我们如何访问与密钥相关的值
in a dictionary. We can also update these values for example 在字典中。例如，我们也可以更新这些值
before a print statement we can write code like this, customer of 在打印语句之前，我们可以编写这样的代码，客户
name, let's update the name to jack 名称，让我们将名称更新为jack
smith, now this little warning is telling us here we can put jac 史密斯，现在这个小警告在这里告诉我们，我们可以把jac
smith here instead of defining it once, and then update 史密斯在这里，而不是一次定义，然后更新
it. Don't worry about them, it doesn't really matter. Now with this line if 它。不要担心它们，这并不重要。现在用这条线if
we print the name of this customer, we should see jack smith, let me show you. 我们打印这个顾客的名字，我们应该看杰克史密斯，让我告诉你。
So, I'm going to use the square bracket notation again, let's print 所以，我将再次使用方括号表示法，让我们打印
the name of the customer you can see that is updated here, 您可以在此处更新的客户名称，
we can also add a new key here, let's set the 我们也可以在这里添加一个新密钥，让我们设置
birthdate to some value like january first 出生到一些价值，如1月1日
1980. And then we can print it here, 1980.然后我们可以在这里打印，
so as you see we can easily add new key value pairs 因此，您可以轻松添加新的键值对
to a dictionary. So this is the basics of using dictionaries 到字典。所以这是使用字典的基础知识
in python. They're extremely important and they have a lot of applications in the real 在python中。它们非常重要，它们在真实中有很多应用
world. Okay, here's an exercise for you. So here we have 世界。好的，这是给你的练习。所以我们有
this program that asks our phone number. Let's type 1 这个程序询问我们的电话号码。我们输入1
234. We type it in digits and then this will translate 我们用数字输入，然后翻译
it, to words, take a look. Enter, it prints, 1, 2, 3, 它，言语，看看。输入，打印，1,2,3，
4. That's a pretty cool program, so go ahead and 那是一个非常酷的计划，所以继续吧
spend a few minutes on this exercise, it's pretty easy, I will see you 在这个练习上花几分钟，这很容易，我会看到你
next. 下一个。
Alright, so first we need to get the users phone number, we call the input function 好吧，首先我们需要获取用户的电话号码，我们称之为输入功能
with the label phone, we get the result and store it 使用标签电话，我们得到结果并存储它
in this variable, now let's say the user enters 123 在这个变量中，现在让我们说用户输入123
4. So we need to look through this string, get each character and 所以我们需要查看这个字符串，得到每个字符和
translate it to a world, so what we need to implement this scenario. 把它翻译成一个世界，所以我们需要实现这个场景。
is a dictionary, because a dictionary is a structure that allows us to 是一本字典，因为字典是一种允许我们使用的结构
a key to a value. So we can have a dictionary with keys 价值的关键。所以我们可以有一个带键的字典
like 1234. And we map each of these keys 像1234.我们映射每个键
to a word. So we cannot put digit 1 to 一句话。所以我们不能把数字1放到
the word 1 we can map 2 单词1我们可以映射2
to two you get the point. So let's define 两点你明白了。所以我们来定义
a dictionary, you can call it digits, underline mapping 字典，你可以称之为数字，下划线映射
now this dictionary I'm going to add a few key value pairs. 现在这本词典我要添加几个键值对。
One, we map it to 1, 2, we 一，我们将它映射到1,2，我们
to 2, 3, to 3, 到2,3或3，
and finally, 4 to 4. Now technically we should 最后，4到4.现在技术上我们应该
add all the digits from 0 to 9, but I don't want to waste all your time 添加0到9之间的所有数字，但我不想浪费你所有的时间
typing repetitive things here, you got the point. So lets move on, 在这里输入重复的东西，你明白了。让我们继续前进，
now we need to look through the phone string. So 现在我们需要查看电话串。所以
4 character in phone, we get each 手机中有4个字符，我们每个都有
character and then use it to access a key value pair in 字符，然后使用它来访问键值对
dictionary. So, digits underline mapping 字典。所以，数字下划线映射
we can use square brackets or call the get method, 我们可以使用方括号或调用get方法，
I would prefer to use the get method, so in case the user enters some character 我更喜欢使用get方法，以防用户输入一些字符
that is not part of our dictionary, our program is not going to yell at them. 这不是我们字典的一部分，我们的节目不会对他们大喊大叫。
So, we call the gt method and pass this character 因此，我们调用gt方法并传递此字符
a the key, and if we don't have this key, we can supply 钥匙，如果我们没有钥匙，我们可以提供
a default value like exclamation mark, so with this we get a word 一个默认值，如感叹号，所以我们得到一个单词
now we need to add this word to an output string, so we can define 现在我们需要将这个单词添加到输出字符串中，以便我们定义
an output string, initially we set it to an empty string, 输出字符串，最初我们将其设置为空字符串，
in each iteration we get this and add it to our output string. 在每次迭代中，我们得到它并将其添加到输出字符串中。
So, we type output, plus equals. 所以，我们输入输出加上等于。
This and we should also add a 这个，我们还应该添加一个
space at get. So the words are not close to each other, okay? 得到的空间。所以这些话并不相互接近，好吗？
That's all we have to do. Now finally let's print 这就是我们所要做的。现在终于打印了
this output, run our program, so I want to 这个输出，运行我们的程序，所以我想
type 1, 3,4, 5, lets 类型1,3,4,5，让
see what we get. We get 1, 3, 4 with an exlamantion 看看我们得到了什么。我们得到1,3,4和exlamantion
mark. 标记。
In this tutorial I'm going to show you something really cool 在本教程中，我将向您展示一些非常酷的东西
that you can do with dictionaries, so here in this program you can type 你可以用字典做，所以在这个程序中你可以输入
a message like good morning followed by a smiley face, 早上好，然后笑脸，
when we press enter, we get this beautiful smiley face, or 当我们按下回车时，我们会得到这个美丽的笑脸，或者
we can type i am sad, with a sad smiley face 我们可以打字，我带着悲伤的笑脸
and it gets translated to this beautiful emoji. 它被翻译成这个美丽的表情符号。
So this is another application if using dictionaries, we have a dictionary 所以这是另一个应用程序，如果使用字典，我们有一个字典
that maps these characters into smiley faces. 将这些角色映射到笑脸。
So lets go ahead and build this program together, we start by 所以，让我们一起构建这个程序，我们从一开始
calling the input function, here we pass a greater than symbol 调用输入函数，这里我们传递一个大于符号
as an indicator for the user to type a message, we give that message, store 作为用户键入消息的指示符，我们给出该消息，存储
it here, as you know that is a string, now we need to 它在这里，因为你知道这是一个字符串，现在我们需要
split this string by a space, so if the user types 将此字符串拆分为空格，因此如果用户键入
good morning with a smiley face, we want to break this down into 带着笑脸的早上好，我们想把它打破
3 words, good morning, and smiley face. 3个字，早上好，笑脸。
To do that we can call the split method. So we call 为此，我们可以调用split方法。所以我们打电话
message.split and pass message.split并传递
a string with one space as a separator. What this method 一个字符串，其中一个空格作为分隔符。这个方法是什么
does is basically it goes through this string and anywhere it finds this 它基本上是通过这个字符串和它找到它的任何地方
character, in this case a space, it uses it as a boundary to 字符，在这种情况下是一个空格，它用它作为边界
separate this string into multiple words. And then it will return 将此字符串分成多个单词。然后它会回来
a list. Let me show you. So let's store that here, words, and then 一个列表。让我演示给你看。所以，让我们把它存储在这里，然后再存储
Then print it on the terminal, 然后将其打印在终端上，
let's go ahead and run our program. So if you type good morning 让我们继续我们的计划。所以如果你输入早上好
sunshine, enter, we get a list with three items 阳光，进入，我们得到一个有三个项目的清单
each item is a string, okay? Now, 每个项目都是一个字符串，好吗？现在，
back to our program, we need to define a dictionary for mapping special characters 回到我们的程序，我们需要定义一个字典来映射特殊字符
like these 2 into a smiley face, so, 像这两个笑脸，所以，
we can call that emojis 我们可以称之为表情符号
set it to curly braces, here we add a key value pair, 将它设置为花括号，这里我们添加一个键值对，
a smiley face, and we map that to a string, 一个笑脸，我们把它映射成一个字符串，
in this string we want to add an emoji, if you're on a mac keyboard you can press 在这个字符串中我们想添加一个表情符号，如果你在mac键盘上你可以按
control command and space and this should bring up 控制命令和空间，这应该提出来
this emoji box, if you're on Windows honestly I 这个表情符号框，如果你老实说我在Windows上
really don't know how you can do this, but there must be applications who do this for you. So lets go 真的不知道你怎么做到这一点，但必须有应用程序谁为你这样做。那就放手吧
ahead and pick this happy smiley face, good, 领先，挑选这个快乐的笑脸，好，
now lets add another key value pair, or sad face 现在让我们添加另一个键值对或悲伤的脸
we map this to, 我们将此映射到
this face here, good. So, we 这张脸在这里，好。所以，我们
have a list of words now we need to look through this list, get 有一个单词列表现在我们需要查看这个列表，得到
each word and potentially map it to an emoji. 每个单词并可能将其映射到表情符号。
So we can write a for loop for word in words, 所以我们可以用单词为单词写一个for循环，
now we want to go to our dictionary, and see if you have an item with this 现在我们想去我们的字典，看看你是否有一个项目
word as the key. If you have an item with that key, we get 作为关键词。如果您有一个带有该密钥的项目，我们就会得到
it's value, otherwise we want to use the same word. So if a user types 这是值，否则我们想要使用相同的词。因此，如果用户输入
good morning, we don't want to translate those words, we just want to return those. So that is the case, 早上好，我们不想翻译那些话，我们只想归还那些。就是这样，
where we use the get method to supply a default 我们使用get方法提供默认值
value, so we wan to get an item with a key word, and 价值，所以我们想得到一个关键词的项目，和
if we don't have an item with that key, we simply use that word as 如果我们没有带有该键的项目，我们只需将该词用作
the default value. Now similar to the exercise that we did in the last tutorial 默认值。现在类似于我们在上一个教程中所做的练习
we need an output variable. So let's define that here, 我们需要一个输出变量。所以我们在这里定义
output set it to an empty string, now back to line 输出将其设置为空字符串，现在返回到行
9, we get the return value of the get method, 9，我们得到get方法的返回值，
and add it to our output variable. So output 并将其添加到我们的输出变量。所以输出
plus equals whatever we get from the get method, and finally 加上等于我们从get方法得到的东西，最后
we need to append a space at the end. 我们需要在最后附加一个空格。
That's it, we're done. So let's print the output 就是这样，我们完成了。所以让我们打印输出
and run our program and I'm going to type good morning 并运行我们的计划，我打算早上好
sunshine with a smiley face, we get that 带着笑脸的阳光，我们明白了
beautiful, let's try the sad face as well, I am sad. 美丽，让我们试试悲伤的脸，我很伤心。
Too bad. Beautiful! So as you can see 太糟糕了。美丽！所以你可以看到
dictionaries have a lot of use cases in real applications. 字典在实际应用程序中有很多用例。
Here's a solution from the last tutorial where we created an emoji 这是我们创建表情符号的上一个教程的解决方案
converter. Now so far we have been writing all our code right here, 转换器。到目前为止，我们已经在这里编写了所有代码，
in app,py, but as our programs grow we need a better way to organize our code. 在app，py，但随着我们的程序的增长，我们需要一种更好的方式来组织我们的代码。
We need to break up our code into smaller, more manageable 我们需要将代码分解为更小，更易于管理的代码
and more maintainable chunks which we call functions. So function 以及我们称之为功能的更可维护的块。功能如此
is a container for a few lines of code that perform a specific 是执行特定代码的几行代码的容器
task for example you have learned about a few of the built in functions in 例如，你已经了解了一些内置函数
python like print, and input, each of these functions python就像打印，输入，这些功能中的每一个
have a purpose, they know how to perform a specific task. So when we 有目的，他们知道如何执行特定任务。所以，当我们
build large complex programs, we should break up our code into smaller 构建大型复杂程序，我们应该把我们的代码分解成更小的
reusable chunks which we call functions to better organize our code. 可重用的块，我们称之为函数以更好地组织我们的代码。
And that's what I'm going to show you in this tutorial. So let's write this simple 这就是我将在本教程中向您展示的内容。所以让我们写这个简单
program, for printing a greeting message. So we're going to do a print, 程序，用于打印问候消息。所以我们打算打印一下，
hi there, and lets add another message 你好，然后添加另一条消息
welcome aboard. So here we have a simple 欢迎乘船。所以这里我们有一个简单的
program with only two lines of code. Now let's say these two lines 程序只有两行代码。现在让我们说这两行
potentially we're going to need them in other programs so we can put them in a function that 我们可能会在其他程序中需要它们，所以我们可以把它们放在一个函数中
we can reuse. Let me show you how to do that, so, 我们可以重用。让我告诉你如何做到这一点，所以，
we start typing out death, that is a reserve keyword in 我们开始输入死亡，这是一个保留关键字
Python, and it's short for define. When Python interpreter sees this it Python，它是define的缩写。当Python解释器看到它时
knows that we're defining a function, next we need to give our function 知道我们正在定义一个函数，接下来我们需要给出我们的函数
a name, lets say greet_user. 一个名字，让我们说greet_user。
So all the best practices you learned about naming your variables also apply here, 因此，您在命名变量时学到的所有最佳实践也适用于此处，
which means we should name our functions with lower case characters 这意味着我们应该用小写字符命名我们的函数
if there are multiple words, we need to separate them using an underscore 如果有多个单词，我们需要使用下划线将它们分开
and you should always, always, always use meaningful 而且你应该始终始终使用有意义的东西
descriptive names for your functions. So, here we define a function 功能的描述性名称。所以，这里我们定义一个函数
called greet user, after that we'll need to add parenthesis 叫做greet user，之后我们需要添加括号
followed by a colon. Now you know that whenever we add a colon at the end of a line 然后是冒号。现在你知道每当我们在一行末尾添加冒号时
we're telling python that we're defining a block of code. So when we 我们告诉python我们正在定义一段代码。所以，当我们
press enter, the next line is indented. Now any code that we write here 按回车键，下一行缩进。现在我们在这里写的任何代码
will belong to this function. So I'm going to move these two lines 将属于此功能。所以我要移动这两行
these two print statements inside of our function. You can simply 我们函数中的这两个print语句。你可以简单
press tab to indent them, beautiful, also lets remove 按Tab键缩进它们，漂亮，也让我们删除
this extra line break, that is better, so these two lines 这个额外的换行符，那就更好了，所以这两行
now are part of our greet user function, so whenever we 现在是我们问候用户功能的一部分，所以每当我们
call this function, these two lines will be executed. Okay? Now, 调用此函数，这两行将被执行。好的？现在，
let's remove indentation so here we are outside of our function, 让我们删除缩进，所以这里我们不在我们的函数中，
let's do a print statement and print start 让我们做一个打印声明和打印开始
after that let's call our function, so greet user. 之后让我们调用我们的函数，所以问候用户。
And finally let's do another create statement and print 最后让我们做另一个创建语句和打印
finish. Now we have this little warning here, this 完。现在我们在这里发出这个小小的警告
underline, if you hover your mouse over this line, this tool was telling you 请注意，如果您将鼠标悬停在此行上，此工具会告诉您
that we should add two blank lines after a function definition. This is coming from 我们应该在函数定义后添加两个空行。这是来自
pep8 which is a document that defines the best practices for pep8是一个定义最佳实践的文档
formatting our code. We'll look at that in the future. So for now to make pycharm 格式化代码。我们将来会看到这一点。所以现在要做pycharm
happy, lets add an extra line break to this function 快乐，让我们为这个功能添加额外的换行符
so whenever we define a function we need to add two lines breaks after. 所以每当我们定义一个函数时，我们需要在之后添加两个换行符。
Okay? so, now let's run our program and see what happens. 好的？那么，现在让我们运行我们的程序，看看会发生什么。
Alright, so we have 4 messages on the terminal 好的，所以我们在终端上有4条消息
first we get the start message then we get the messages 首先我们得到开始消息然后我们得到消息
coming from our create function, so either and welcome aboard, and finally we see 来自我们的创造功能，所以要么欢迎乘坐，最后我们看到
the finished message. So let me explain the flow of our program 完成的消息。那么让我解释一下我们程序的流程
when python runs this code it doesn't actually print these 当python运行此代码时，它实际上并不打印这些代码
two messages on the terminal because these two lines of code are inside 终端上有两条消息，因为这两行代码在里面
of this function, so they would only get executed if we call 这个函数，所以如果我们调用它们只会被执行
this function. If we don't call this function, these lines don't get executed. 这个功能。如果我们不调用此函数，则不会执行这些行。
Okay? So the execution of our program actually starts here, 好的？所以我们程序的执行实际上从这里开始，
the first message that we see on the terminal is the start message. After 我们在终端上看到的第一条消息是开始消息。后
that, Python sees that we are calling the greet function, so 那，Python看到我们正在调用greet函数，所以
it will jump over here and then execute these two lines. 它将跳过这里，然后执行这两行。
Then, it will jump out of this function and continue the normal 然后，它将跳出此功能并继续正常
execution of our program so it will print the finish message on the terminal. 执行我们的程序，以便在终端上打印完成消息。
So this is how functions work. Also note that the order of this 这就是函数的工作原理。还要注意这个顺序
code matters, so here we're calling the greet function after 代码很重要，所以我们在这之后调用greet函数
after we have defined it. If you try and call it before you define it you will see an error. 在我们定义它之后。如果您在定义它之前尝试调用它，您将看到错误。
For example, if we call the greet 例如，如果我们打电话问候
user function right here, see we immediately get this red underline 用户功能就在这里，看到我们立刻得到这个红色下划线
which says unresolved reference, greet user which basically 说未解决的参考，基本上迎接用户
means python doesn't know what is this greet user, it's not defined anywhere. 意味着python不知道这个问候用户是什么，它没有在任何地方定义。
So we always define our functions first, and then 所以我们总是首先定义我们的函数然后
call them. 给他们打电话。
Here's a function we created in the last tutorial 这是我们在上一个教程中创建的函数
now I got a question for you, what is the difference between 现在我有一个问题给你，有什么区别
calling this functions nd the print function of python? The difference 调用这个函数和python的print函数？区别
is that the print function takes some information, in this case 在这种情况下，打印功能需要一些信息
the message that we want to print, but our greet function doesn't take 我们要打印的消息，但我们的greet函数不需要
any information. Wouldn't it be nicer if you could pass the name of the user here and then 任何信息。如果你可以在这里传递用户的名字，那会不会更好
instead of saying hi there, you could print the name of the user. So, 而不是在那里打招呼，你可以打印用户的名字。所以，
in this tutorial, I'm going to show you how to pass information to your functions, and 在本教程中，我将向您展示如何将信息传递给您的函数，以及
that is pretty easy, so back to the definition of our greet 这很简单，所以回到我们的问候的定义
user function, inside of these parenthesis, we can add parameters 用户函数，在这些括号内，我们可以添加参数
these parameters are pale holders for receiving information. 这些参数是用于接收信息的苍白持有者。
For example, you can add a name parameter, and we can pass the 例如，您可以添加名称参数，我们可以传递
name of the user when calling this function. So let's pass Jon 调用此函数时用户的名称。让我们通过乔恩
here, now when we call this function and pass this value 现在，当我们调用此函数并传递此值时
this name parameter will be set to Jon. So it 此name参数将设置为Jon。所以
will act like a local variable that we defined inside of this function. 将像我们在此函数内定义的局部变量。
Just imagine, we didn't have this parameter, and instead we had 试想一下，我们没有这个参数，相反，我们有
a variable like name that was set to jump. Now here we could simply 像设置为跳转的名称之类的变量。现在我们可以简单地说
print the name of this user, so we could use a formatted string, 打印此用户的名称，以便我们可以使用格式化的字符串，
and print the value of the name variable, right? Now, 并打印name变量的值，对吧？现在，
we don't really have this local variable defined in this function 我们实际上并没有在这个函数中定义这个局部变量
we have a parameter, it's exactly the same thing. So 我们有一个参数，它完全一样。所以
with these parameters we can receive information in our functions. Now let's go ahead and 使用这些参数，我们可以在函数中接收信息。现在让我们继续吧
run this program, so take a look. 运行这个程序，所以看一看。
We get Hi John welcome aboard, we can take this to the next 我们欢迎嗨约翰欢迎，我们可以把它带到下一个
level. Now we can call this function one more time. So 水平。现在我们可以再次调用此函数。所以
greet user, and this time supply a different name. Let's say Mary. 问候用户，这次提供一个不同的名称。让我们说玛丽。
So we are reusing our greet function and passing it a different 所以我们重用我们的greet函数并将它传递给另一个函数
value. Imagine if we didn't have this function you would have to 值。想象一下，如果我们没有这个功能你就必须这样做
repeat this line twice , once for hi jon and another time for hi mary. 重复这一行两次，一次为hi jon，另一次为hi mary。
So this is the benefit of defining functions, we can take a few 所以这是定义函数的好处，我们可以采取一些
lines of code that have a specific purpose and put them inside of a 具有特定目的的代码行并将它们放在一个
function, so now let's run our program one more time 功能，现在让我们再次运行我们的程序
there you go. So you get hi job, welcome aboard, then you get hi mary 你去吧所以你得到工作，欢迎乘坐，然后你得到喜欢的玛丽
welcome aboard. Now note that when a function has a parameter 欢迎乘船。现在请注意，当函数有参数时
we are obligated to pass a value for that parameter. 我们有义务传递该参数的值。
So if you remove jon from here and run 所以，如果你从这里删除jon并运行
our program, we get this type error, greet user, 我们的程序，我们得到这种类型的错误，问候用户，
missing one required positional argument. 缺少一个必要的位置参数。
Argument in programming is the value that we supply to a function, so 编程中的参数是我们提供给函数的值，所以
back to this example, marrying this case is an 回到这个例子，结婚这个案子是一个
argument that we pass to the name parameter. And lot of 我们传递给name参数的参数。还有很多
programmers don't know the difference between parameters and arguments and think they are the same. 程序员不知道参数和参数之间的区别，并认为它们是相同的。
But they are not, parameters are the holes or placeholders 但它们不是，参数是漏洞或占位符
that we define in our function for receiving information, 我们在接收信息的功能中定义，
arguments are the actual pieces of information that we supply to these functions. 参数是我们提供给这些函数的实际信息。
Okay? So, let's bring back 好的？所以，让我们带回来
this jon string, now we can also define multiple 这个jon字符串，现在我们也可以定义多个
parameters, for example, let's rename this parameter to first name as I told 参数，例如，让我们将这个参数重命名为我告诉的名字
you before, you can right click here, go to refactor and then 你以前，你可以点击这里，然后转到重构
rename, or preferably you can use a short cut. So the shortcut for 重命名，或者最好是你可以使用捷径。所以快捷方式
this on Mac is shift and f6. 在Mac上这是转移和f6。
And this brings up the renamed dial up box, here we can change this to first 这会打开重命名的拨号框，在这里我们可以将其更改为第一个
underline name. Now we can add a second parameter 下划线名称。现在我们可以添加第二个参数
so comma followed by last underline name 所以逗号后跟最后一个下划线名称
so back to our formatted string, let's also print 所以回到我们的格式化字符串，让我们也打印
the last name, beautiful, so here when calling this function, 姓氏，美丽，所以这里调用这个功能，
then we can supply another argument so jon 然后我们可以提供另一个参数jon
smith and lets delete this line we don't really need it anymore 史密斯，让我们删除这一行，我们不再需要它了
run our program, take a look, hi jon smith, 运行我们的程序，看一看，嗨jon smith，
so in the last tutorial you learned that whenever 所以在上一个教程中你学到了什么
we define parameters for our functions we should always supply 我们为我们应该始终提供的功能定义参数
values otherwise we'll get an error. Let's look at that error one more time. 值否则我们会收到错误。让我们再看一次这个错误。
So In going to delete this second argument and only pass jon. 所以在去删除第二个参数并且只传递jon。
Run our program, we get this message, 运行我们的程序，我们收到此消息，
greet user missing one required, position 问候用户缺少一个必需的位置
argument. So these arguments that you have here like jon 论点。所以这些论点你在这里就像jon
and smith you refer to these as positional arguments. 史密斯你把它们称为位置论证。
Which means their position or argument matters. In other 这意味着他们的立场或论点很重要。其他
words, the first argument we add here is the value or the first parameter 单词，我们在这里添加的第一个参数是值或第一个参数
and the second argument is the value for the second parameter. 第二个参数是第二个参数的值。
So if you change their position or order, we'll get different results. 因此，如果您更改其职位或订单，我们将获得不同的结果。
Let's take a look. Let's pass smith first and then 让我们来看看。让我们首先通过史密斯然后
jon. Okay, good, delete, now let's 乔恩。好的，好的，删除，现在让我们
go with this, so instead of getting Hi Jon Smith, 顺其自然，所以没有得到嗨乔恩史密斯，
we get hi smith jon. So that is the reason we refer 我们得到了smith jon。这就是我们提到的原因
to these arguments as positional arguments, they're positioned or 把这些论点作为位置论证，它们是定位的
order matters. But in python we also have keyword arguments, and 订购事宜。但是在python中我们也有关键字参数，和
with those the position doesn't matter. Let me show you how they work. So, 与那些位置无关紧要。让我告诉你它们是如何工作的。所以，
let's say here you want to pass Jon as the value for the first 让我们说在这里你要传递Jon作为第一个值
name parameter. We can simply specify that here. So first name 名称参数。我们可以在这里指定一下。所以名字
equals jon. Now what we have here is a keyword argument. 等于琼。现在我们在这里有一个关键字参数。
the key word argument. So this combination of having the parameter name, 关键词论点。所以这个具有参数名称的组合，
followed by it's value is what we call a keyword argument, and what we get with this keyword 其后的值是我们称之为关键字参数，以及我们使用此关键字获得的内容
argument the position doesn't really matter. So here we're supplying 论证这个立场并不重要。所以这里我们供应
Jon as an argument fort he first parameter. Now similarly, 乔恩作为他第一个参数的论据。现在同样，
we can use a keyword argument for smith, so last 我们可以为smith使用关键字参数，所以最后
name equals smith, so that we have 2 名字等于史密斯，所以我们有2
keyword arguments. So with these keyword arguments we don't have to worry about 关键字参数。因此，使用这些关键字参数，我们不必担心
the order of the parameters. Now that doesn't mean we should always use keyword arguments. 参数的顺序。现在，这并不意味着我们应该始终使用关键字参数。
Not really. Most of the time we use positional arguments, but 并不是的。大多数时候我们使用位置参数，但是
in certain situations these keyword arguments helming us improve 在某些情况下，这些关键字参数可以帮助我们改进
the readability of our code. So on line 7 it's quite clear 我们的代码的可读性。所以第7行就很清楚了
that we are supplying smith as the last name and jon 我们提供史密斯作为姓氏和jon
as the first name. Now in this particular example, even if we didn't have 作为第一个名字。现在在这个特定的例子中，即使我们没有
the keyword arguments, just passing two positional arguments like jon 关键字参数，只传递两个位置参数，如jon
and smith, would be sufficient, because it's easy to guess 和史密斯一样，就足够了，因为它很容易猜到
what is the purpose of these arguments. But sometimes you're dealing with functions 这些论点的目的是什么？但有时你会处理功能
and you're passing numerical values to them, in those situations 在这些情况下，你将数值传递给它们
it may not be quite clear what those values represent. Let me show you an example. 这些价值代表什么可能不太清楚。让我举个例子。
Let's say we have a function for calculating the total cost of an order, 假设我们有计算订单总成本的功能，
so we still call that calculate, underline cost, 所以我们仍然称之为计算，强调成本，
so when calling this function, you need to supply three pieces of information. 因此，在调用此函数时，您需要提供三条信息。
One is the total cost of all the order items lets say 50 一个是所有订单商品的总成本，比方说50
dollars. The other is the shipping cost, let's say 美元。另一个是运费，比方说
5 dollars, and the last piece is discount, let's say 0, 5美元，最后一块是折扣，让我们说0，
.1. Someone reading this code, may not be sure 0.1。阅读此代码的人可能不确定
what these three values represent, in this case, we can improve 这三个值代表什么，在这种情况下，我们可以改进
the readability of this code, by using keyword arguments. So we can 通过使用关键字参数，此代码的可读性。所以我们可以
prefix each value with the name of parameter they target, for example, 为每个值添加前缀，以及它们所针对的参数名称，例如，
we can set total to 50 and then 我们可以将总数设置为50然后
shipping to 5, and then discount to 0.1. 运到5，然后折扣到0.1。
Now if you give this code to someone else, they 现在，如果您将此代码提供给其他人，他们
can immediately tell what these values represent. So that is the power 可以立即告诉这些值代表什么。这就是力量
of keyword arguments. So here's what I want you to take away. 关键字参数。所以这就是我想要你带走的东西。
For the most part, use positional arguments, but if you're dealing with 在大多数情况下，使用位置参数，但如果你正在处理
functions that take numerical values, see if you can improve the 取数值的函数，看看你是否可以改进
readability of your code by using keyword arguments. You simply 使用关键字参数可以读取代码。你干脆
prefix the arguments that you pass with the name of their parameters and this will 使用其参数名称为您传递的参数添加前缀，这将是
increase the readability of your code. Now there is one more thing you need to know about 提高代码的可读性。现在还有一件事你需要了解
keyword arguments. These keyword arguments should always come after 关键字参数。应始终遵循这些关键字参数
positional arguments. Let me show you what I mean. So, 位置论证。让我告诉你我的意思。所以，
back to our simple greet user function, let's use a keyword argument 回到我们简单的问候用户函数，让我们使用关键字参数
for the first thing. So, let's set 第一件事。那么，让我们来吧
first name to jon and see what happens. So we immediately get this error and jon的名字，看看会发生什么。所以我们马上得到这个错误
positional argument after keyword argument. 关键字参数后的位置参数。
So when calling this function we're supplying one keyword argument 所以当调用这个函数时，我们提供了一个关键字参数
and one positional argument, and python doesn't like this. So, 和一个位置参数，而python不喜欢这个。所以，
if you're mixing positional and keyword arguments, you should always use 如果你正在混合位置和关键字参数，你应该总是使用
and then the keyword arguments. 然后是关键字参数。
In other words, lets remove this keyword argument here, 换句话说，我们在这里删除这个关键字参数，
so jon is now a positional argument, is targeting the first 所以jon现在是一个位置论证，目标是第一个
parameter. Now after this positional argument we can 参数。现在我们可以在这个位置论证之后
supply any number of keyword arguments, so if you set last name to smith 提供任意数量的关键字参数，因此如果您将姓氏设置为smith
look, python doesn't have any problem with this because we are adding 看，python对此没有任何问题，因为我们正在添加
a keyword argument after a positional argument. 位置参数后的关键字参数。
So let's quickly recap, for the most part use positional arguments 因此，让我们快速回顾一下，大多数情况下使用位置参数
if you're dealing with functions that take multiple numerical values 如果你正在处理带有多个数值的函数
and it's not quite clear what those values represent, use keyword arguments 并且不太清楚这些值表示的是什么，使用关键字参数
to improve the readability of your code, and finally if 提高代码的可读性，最后如果
your passing both positional and keyword arguments, use the 你传递位置和关键字参数，使用
keyword arguments after the positional arguments. 位置参数后的关键字参数。
So far you have learned how to create functions and send them information using 到目前为止，您已经学会了如何创建函数并使用它们发送信息
their parameters in this tutorial I'm going to show you how to create functions that return 他们在本教程中的参数我将向您展示如何创建返回的函数
values, this is particularly useful if you're doing some kind of 价值观，如果你正在做某种事情，这个特别有用
calculation your function and you want to return the result to whoever is using your 计算你的功能，你想将结果返回给使用你的人
function. let me show you. So let's start by defining a 功能。让我演示给你看。所以让我们从定义一个开始
function, called square, this function simply calculates 函数，称为square，此函数只是计算
the square of a number. So we need to add one parameter here, 数字的平方。所以我们需要在这里添加一个参数，
that is is our number, now here we want to calculate number 那是我们的号码，现在我们想要计算数字
times number, that is the square of a number. 时间数，即数字的平方。
Now to return this color outside of this function we simply 现在我们简单地将此颜色返回到此函数之外
use the return statement. So return. Now, 使用return语句。所以回归。现在，
our square function is very simple, it only had one line of code. 我们的square函数非常简单，它只有一行代码。
But in a real program, you might have a function that has ten lines of code to calculate 但是在实际程序中，您可能拥有一个包含十行代码来计算的函数
something. So, this return statement is very 一些东西。所以，这个退货声明非常
valuable. So now we can return values to the caller of our function. 有价值。所以现在我们可以将值返回给函数的调用者。
So, with this, when we call the square function, 所以，有了这个，当我们调用square函数时，
let's say we give it 3, now this function returns a value, 假设我们给它3，现在这个函数返回一个值，
just like an input function of python, this input function 就像python的输入函数一样，这个输入函数
waits for the user to type something and press enter and then it will 等待用户输入内容并按Enter键然后它会
return the user's input as a string so we can get it 将用户的输入作为字符串返回，以便我们得到它
and store it in a variable like this, right? We have the same story with our 并将其存储在这样的变量中，对吧？我们和我们有同样的故事
story, with our square function. So our square function returns the value 故事，我们的方形功能。所以我们的square函数返回值
we can simply get it and store it in a variable. Let's call that 我们可以简单地获取它并将其存储在变量中。我们称之为
result. Now, we can print 结果。现在，我们可以打印
result on the terminal, there you go, so result is 9, 结果在终端上，你去，所以结果是9，
we could also pass this function called 我们也可以通过这个函数调用
directly inside of the print function without defining a separate variable. 直接在print函数内部，而不定义单独的变量。
Sometimes this is useful for writing shorter code, so in this case. 有时这对于编写更短的代码很有用，所以在这种情况下。
So, in this case, we don't really need this result variable. We simply want to print 所以，在这种情况下，我们并不真正需要这个结果变量。我们只想打印
the square of 3. So, we could simply pass this function 因此，我们可以简单地传递这个函数
call right here, as an argument for the print 在这里打电话，作为印刷品的论据
function. So let's delete this variable now our code is 功能。现在让我们删除这个变量，我们的代码是
shorter so with this we're calling the square function, this returns a 更短，所以我们调用square函数，这将返回一个
value and we'll pass that value as an argument to the print function. value，我们将该值作为参数传递给print函数。
Okay? When we run this program, we get the exact same 好的？当我们运行这个程序时，我们得到完全相同的
result. There you go. Now what happens if we don't 结果。你去吧如果我们不这样做会发生什么
use a return statement in our function. So in the last example we simply used 在我们的函数中使用return语句。所以在上一个例子中我们简单地使用了
a print statement. Let's temporarily remove 打印声明。我们暂时删除
this return statement and print the result 这个return语句并打印结果
on the terminal. Like this. So let's run our program 在终端上。像这样。所以让我们运行我们的程序
one more time and see what we get. We see two things, the number 再来一次，看看我们得到了什么。我们看到两件事，数字
9, and none. What is happening here? Well 9，没有。这里发生了什么？好
when Python interpreter executes this code, first it will call 当Python解释器执行此代码时，首先它将调用
the square function. So the control moves to the square function 方函数。因此控件移动到方形函数
here we calculate the square of this given number and then print it 这里我们计算这个给定数字的平方然后打印出来
on the terminal. That is the reason we see 9 right here, 在终端上。这就是我们在这里看到9的原因，
now, by default, all functions return the value 现在，默认情况下，所有函数都返回值
none. So if we don't have a return statement 没有。所以，如果我们没有退货声明
here, by default python returns none. None 在这里，默认情况下python返回none。没有
is an object that represents the absence of a value, it's like nothing 是一个代表缺少价值的对象，它就像什么都没有
or null i c, c++ java and Javascript. 或null ic，c ++ java和Javascript。
So, in this example, we didn't have a return statement, and 所以，在这个例子中，我们没有return语句，并且
by default python returned none from this function. 默认情况下，python从此函数返回none。
So, after this square function is executed, the value none 因此，执行此square函数后，值为none
is returned and passed as an argument to the print function, that is the reason 返回并作为参数传递给print函数，这就是原因
we see none on the second line. So here are the two things 我们在第二行看不到任何一个。所以这是两件事
you need to take away from this tutorial. By default all functions in Python 你需要从本教程中删除。默认情况下，Python中的所有函数
return none. You can change that, so if you have a function that 没有。你可以改变它，所以如果你有一个功能
calculates something, you can return the result using the return statement. 计算一些东西，你可以使用return语句返回结果。
Here's the code for our emoji converter program that we wrote earlier. 这是我们之前写的表情符号转换器程序的代码。
If you missed that tutorial, make sure to go back and watch it, as 如果您错过了该教程，请务必回过头来观看它
I told you before, there is a table of content right below this video in the 我之前告诉过你，这个视频下方有一个内容表
description. So we can simply click on the emoji converter tutorial to watch it. 描述。所以我们只需点击表情符号转换器教程即可观看。
Now, your exercise is to reorganize this code using a function. 现在，您的练习是使用函数重新组织此代码。
So I want you to extract a function in this code, because this algorithm 所以我希望你在这段代码中提取一个函数，因为这个算法
for converting these smiley faces into emoji's is something that 将这些笑脸变成表情符号就是这样的
we probably want to use in a couple different applications. We can use it in a chat 我们可能想在几个不同的应用程序中使用。我们可以在聊天中使用它
application in an email application and so on. We don't want to write all this code every time 在电子邮件应用程序中的应用程序等。我们不想每次都写这些代码
we want to convert these characters into smiley faces. 我们想把这些角色转换成笑脸。
So, out of all these lines of code that we have from line 1 to 因此，在我们从第1行到第1行的所有这些代码行中
line 10, the lines 10-2 belong to our 第10行，第10-2行属于我们的
algorithm, all these lines together implement this feature. 算法，所有这些线一起实现此功能。
So, you should put these lines in a separate function, 所以，你应该将这些行放在一个单独的函数中，
Now, why don't we have the input in this function? Because 现在，为什么我们没有这个功能的输入？因为
the input will can come in different forms, currently we're using the import function 输入可以有不同的形式，目前我们正在使用导入功能
to receive the input from the terminal, but in other applications 从终端接收输入，但在其他应用程序中
we might receive the input from a graphical user interface, so the input can come in 我们可能会从图形用户界面接收输入，因此可以输入输入
different forms. We don't want to put that in our function, because this line will not be reuseable. 不同的形式。我们不想把它放在我们的函数中，因为这行不可重用。
You want to give our function the actual message, 你想给我们的功能提供实际的信息，
as a string. We don't care how we got that message. In one 作为一个字符串。我们不关心我们是如何得到这个信息的。在一个
program you might get it from the terminal, in another program you might get it from 程序你可以从终端获得它，在另一个程序中你可以从中得到它
a graphical user interface. So that is the reason this line up code should not 图形用户界面。所以这就是这个排队代码不应该的原因
be in that function. By the same function, you shouldn't include 在那个功能。通过相同的功能，您不应该包括
the last line in that function because what we do with the output is different from 该函数的最后一行，因为我们对输出的不同之处
one program to another. In this program, we are printing the output 一个程序到另一个。在这个程序中，我们正在打印输出
in another program, you might send an output as an email, or as a response in 在另一个程序中，您可以将输出作为电子邮件发送，或作为响应发送
a chat application, so as a general rule of thumb you're function should 一个聊天应用程序，所以作为一般的经验法则，你的功能应该是
not worry about receiving input and 不用担心收到输入和
printing it. These lines of code should not belong to your functions. So go 打印它。这些代码行不应属于您的函数。去啊
ahead and reorganize this code by extracting a function. 提前并通过提取函数重新组织此代码。
Alright, on the top, first we need to define our function. So first we type 好吧，在顶部，首先我们需要定义我们的功能。所以首先我们输入
def, give our function a name like emoji_ def，给我们的函数一个像emoji_的名字
converter, so the name of this function clearly tells me what 转换器，所以这个功能的名称清楚地告诉我什么
it does. Each function should be responsible for one and only one task, 它确实。每个函数应该只负责一个任务，
and that task should be clearly explained in the name of the function. 并且应该在函数名称中清楚地解释该任务。
So, our emoji converter function should take 所以，我们的表情符号转换器功能应该采取
a parameter, we call that message, that is a string, right? Colon 一个参数，我们称之为消息，这是一个字符串，对吗？结肠
now we need to move the lines 现在我们需要移动线
5 all the way to the end of our for block 5一直到我们的阻止结束
into the function, so let's select all these lines, you can 进入功能，所以让我们选择所有这些线，你可以
cut them from here, by pressing command and x on Mac, or 通过在Mac上按下命令和x来切割它们，或者
control and x on Windows, then we paste it here, 在Windows上控制和x，然后我们将其粘贴到此处，
as you can see these lines are automatically indented 如您所见，这些线条会自动缩进
so, they are part of the emoji converter function, if this didn't happen on your 所以，它们是表情符号转换器功能的一部分，如果这没有发生在您的身上
computer, make sure to select these lines and use the 计算机，请务必选择这些行并使用
tab to indent them. Alright, so here's our emoji converter function 选项卡缩进它们。好的，所以这是我们的表情符号转换器功能
beautiful. Now after that 美丽。在那之后
in the main flow of our program, first we get the input from the user 在我们程序的主流程中，首先我们从用户那里获得输入
we store it in this variable message and then we call 我们将它存储在这个变量消息中然后我们调用
emoji converter and pass this message, 表情符号转换器并传递此消息，
this function should return a value, that is what we use to calculate 这个函数应该返回一个值，这是我们用来计算的
the end of this function, so right after this for block we need to add 这个函数的结束，所以在这之后我们需要添加
a return statement, return output, 一个return语句，返回输出，
now that our function returns a value, we can get it and store it 既然我们的函数返回一个值，我们就可以得到它并存储它
in a separate variable, let's call that result 在一个单独的变量中，让我们调用该结果
and then we can simply print that here, or 然后我们可以在这里打印，或者
we can make this code shorter, we don't really need this code here so we can simply 我们可以缩短这段代码，我们不需要这里的代码，所以我们可以简单
call the emoji converter, and print the result. 调用表情符号转换器，然后打印结果。
Like this. Now here we have an underline because 像这样。现在我们有一个下划线，因为
as I told you before we need to blank lines after 正如我之前告诉你的那样，我们之后需要空行
our function definitions. So currently we have only one blank line 我们的函数定义。所以目前我们只有一个空行
we need to add another black line, good, let's go ahead, 我们需要添加另一条黑线，好的，让我们继续，
and run this program, and make sure everything works as before 并运行此程序，并确保一切正常
so I'm going to type a message like good morning with a smiley face 所以我打算像笑脸一样早上好好打个招呼
good, let's run it one more time, times that, 好的，让我们再跑一次，那次，
to add okay beautiful, so, here's the 添加好的美丽，所以，这是
final result, we defined this function called 最后的结果，我们定义了这个函数调用
emoji converter, this function takes a parameter called message. 表情符号转换器，此函数采用一个称为消息的参数。
And finally returns the output. 最后返回输出。
In this tutorial I'm going to teach you how to handle errors in your 在本教程中，我将教你如何处理你的错误
python programs, so let's start by writing a small program python程序，所以让我们从编写一个小程序开始
to get the user age from the terminal. So we call the input variable 从终端获取用户年龄。所以我们调用输入变量
input, and a label here, 输入，这里有标签，
like age, this returns a string, so we need to pass it to 像年龄一样，这会返回一个字符串，所以我们需要将它传递给
the end funciton and store the result in 结束函数并将结果存储在中
a variable called age, now let's print age to make sure you have 一个名为age的变量，现在让我们确定你的打印年龄
implemented everything properly we run our program, we type 我们运行我们的程序正确实现了一切，我们打字
20, 20 is printed here, beautiful, now look at 20,20这里印的很漂亮，现在看看
the exit code of this program, exit code 0 means 该程序的退出代码，退出代码0表示
our program terminated successfully, there were no errors, but what if 我们的程序成功终止，没有错误，但如果
we run this program one more time and instead of entering 我们再次运行这个程序，而不是进入
a numerical value, we enter something like asd. 一个数值，我们输入类似asd的东西。
We get a value error with his message invalid literal 我们的消息无效文字会出现值错误
for int with base 10. Basically what this message is telling for base with base 10.基本上是这条消息的含义
us is that this string asd does not contain 我们是这个字符串asd不包含
a valid home number that can be converted to an integer. Now look at the exit 可以转换为整数的有效主号码。现在看看出口
code of this program, exit code 1 means our exit code crashed, 该程序的代码，退出代码1表示我们的退出代码崩溃，
so 0 always means success and anything but 0 means crash 所以0总是意味着成功，除了0意味着崩溃
now as a good python programmer you should anticipate this situation. 现在作为一个优秀的python程序员，你应该预见到这种情况。
You don't want to let your entire program crash just because the user entered 您不希望仅仅因为用户输入而导致整个程序崩溃
an invalid value. So instead of letting the program 无效的值。所以不要让程序
crash you should handle the situation and print a proper error message, and 崩溃你应该处理这种情况并打印一个正确的错误信息，并且
that's what I'm going to show you in this tutorial, now before we get any further 这是我将在本教程中向您展示的内容，现在在我们进一步了解之前
look at the type of error we get here, value error, 看看我们在这里得到的错误类型，值错误，
so remember this, we're going to get back to this shortly 所以记住这一点，我们很快就会回到这里
now how can we handle these errors, in python we have a construct called 现在我们如何处理这些错误，在python中我们有一个名为的构造
try accept we use that to handle errors. 尝试接受我们使用它来处理错误。
Let me show you how it works. So on the top 让我告诉你它是如何工作的。所以在顶部
we type try: now as you know we are defining 我们输入try：现在你知道我们正在定义
a code block so, I'm going to move these two lines, 一个代码块所以，我要移动这两行，
inside of our try block so press tab here. 在我们的try块里面，所以按下这里的标签。
And remove this black line, now these two lines are part of our 并删除这条黑线，现在这两条线都是我们的一部分
try block, right? So after try we type 试试块吧？所以在尝试之后我们输入
accept, and then we add the type of the error 接受，然后我们添加错误的类型
that our program may encounter, that is value error 我们的程序可能遇到的，那就是价值错误
okay? Then, we add a colon and in this block we can 好的？然后，我们添加一个冒号，在这个块中我们可以
define what should happen if our program encounters 定义如果我们的程序遇到会发生什么
an error of type value error. In this case we want to print 类型值错误的错误。在这种情况下，我们想要打印
a proper error message, so print, 一个正确的错误信息，所以打印，
invalid value. So with this try and accept we are telling python 无效值。因此，尝试接受我们告诉python
hey, go ahead and try running one or these two lines of code, 嘿，继续尝试运行一行或这两行代码，
if you encounter, an error of type. Value error 如果遇到，则为类型错误。价值错误
then instead of crashing the program, try this error message on the terminal 然后，而不是崩溃程序，在终端上尝试此错误消息
so this is how this works. Now more accurately in programming 所以这就是它的工作原理。现在更准确地编程了
we reefer to this kind of error as an exception. So an exception 我们把这种错误作为例外。所以例外
is a kind of error that crashes our program. So our 是一种崩溃我们程序的错误。所以我们的
user enters an invalid value, this line of code 用户输入无效值，这行代码
in line 2 will raise an exception, and on line 4 will catch it 第2行将引发异常，第4行将捕获它
and print this error message, so our program will no longer 并打印此错误消息，因此我们的程序将不再
crash. Let me show you. So let's go ahead and run this 崩溃。让我演示给你看。所以让我们继续吧
age 20, everything 20岁，一切
works just like before and the exit code is 0, let's 像以前一样工作，退出代码是0，让我们
rerun this program, and enter asd, 重新运行此程序，然后输入asd，
so we get this proper error message invalid error menu and once again 所以我们得到这个正确的错误消息无效错误菜单再次
the exit code is 0, so that means our program completed successfully. 退出代码为0，这意味着我们的程序成功完成。
Didn't crash, now, we can also handle different kinds of 没有崩溃，现在，我们也可以处理不同种类的
errors or exceptions, for example right when we get the age 错误或例外，例如当我们得到年龄时
let's define a variable income and set it to 20,000. 让我们定义一个可变收入并将其设置为20,000。
And then define another variable called risk and set it 然后定义另一个名为risk的变量并设置它
to income divided by age. 收入除以年龄。
Let's run this program and enter 0 as the game to see what happened. 让我们运行这个程序并输入0作为游戏，看看发生了什么。
So 0 is obviously a numerical value 所以0显然是一个数值
so when we enter this value, technically we shouldn't get any exceptions. 所以当我们输入这个值时，从技术上讲，我们不应该得到任何例外。
Take a look. Our program crashed, we 看一看。我们的计划崩溃了，我们
did a 0 division error, because we cannot divide a number by 0. 做了0分区错误，因为我们不能将数字除以0。
Once again look at the exit code, so we couldn't catch this kind of error or 再看看退出代码，所以我们无法捕捉到这种错误或
this kind of exception with this accept block. 这个接受块有这种异常。
Accept block is only cashing exceptions of type value Accept块只是兑现类型值的异常
error. And this happens when we try to convert a nonnumerical value, 错误。当我们尝试转换非数值时会发生这种情况，
to an integer. So in a situation like this. We should handle 到整数。所以在这种情况下。我们应该处理
different kinds of exceptions, we can add another 不同种类的例外，我们可以添加另一种
except statement for an exception of type 0 除了类型0的异常的语句
division error. Now in this block we can print a 分裂错误。现在在这个块中我们可以打印一个
different kind of message, like age cannot be 不同种类的信息，就像年龄不可能
0. Let's run our program one more time. Now, 我们再一次运行我们的程序。现在，
I'm going to enter 0 as the age so we get this 我将按年龄输入0，所以我们得到了这个
error message age cannot be 0 and look at the exit code. 错误消息年龄不能为0并查看退出代码。
It's 0, so our program didn't crash, so to recap, we 它是0，所以我们的程序没有崩溃，所以回顾一下，我们
`used try accept blocks to handle exceptions that are `使用try accept块来处理异常
raised in our programs, as a good programmer you should always anticipate 在我们的课程中提出，作为一名优秀的程序员，你应该始终期待
these kind of exceptions and handle them properly. 这些例外并正确处理它们。
In this tutorial I'm going to talk to you guys about comments in python. 在本教程中，我将与您讨论python中的注释。
We use comments to add notes or comments to our programs. 我们使用注释为我们的程序添加注释或注释。
For example, here I'm going to make a print statement, 例如，在这里我要做一个打印声明，
sky is blue. Now, before that, I 天空是蓝色的。现在，在那之前，我
can add a comment using a hi sign and then whatever we type here 可以使用hi符号添加注释，然后在此处输入任何内容
is going to get ignored it's not going to get executed. Let me show you so 将被忽略它不会被执行。让我告诉你
let's run this program, look we only see this 让我们运行这个程序，看看我们只看到这个
message. Sky is blue. So with this comment we can explain 信息。天空是蓝色的。所以我们可以解释这个评论
something about our code, we can use it as reminders to fix things or 关于我们的代码，我们可以使用它作为提醒来解决问题或
clear things up, or we can use it to communicate things with other developers 清除事物，或者我们可以用它来与其他开发者沟通
reading our code, why we have written this code in a certain way. 阅读我们的代码，为什么我们以某种方式编写了这段代码。
These are good use cases for using comments, we can also 这些是使用评论的好用例，我们也可以
have comments over multiple lines, so we can simply add more 对多行有评论，所以我们可以简单地添加更多
comments, each line should start with a new hi sign. Now 注释，每一行应以新的hi符号开头。现在
one thing you need to avoid when using comments is explaining what the 使用评论时需要避免的一件事是解释什么
code does. So here's an example of a bad comment. 代码呢。所以这是一个不好评论的例子。
Let me just delete these two lines, and with this comment I'm going to 让我删除这两行，并以此评论我要去
say print sky is blue. Why 说打印天空是蓝色的。为什么
is this a bad comment, because it's telling me the obvious, it's telling me 这是一个糟糕的评论，因为它告诉我显而易见的，它告诉我
this slide is going to print sky is blue. Now the problem with this comment is 这张幻灯片打印天空是蓝色的。现在这个评论的问题是
that apart from being verbose and repetitive, if you come here, 除了冗长和重复之外，如果你来到这里，
and change sky to ocean, now this comment gets 并改变天空到海洋，现在这个评论得到
outdated. So overtime we change this code, then we have to come back and 过时。所以加班我们改变了这个代码，然后我们必须回来了
modify the corresponding comment. So I've seen some developers 修改相应的评论。所以我见过一些开发者
add comments in front of their functions, for example, they define 在他们的功能前添加注释，例如，他们定义
a function let's say a square that takes a number 一个函数让我们说一个带数字的正方形
and simply returns number times number. 并简单地返回数字次数。
But it is obvious that this function calculates and returns the 但很明显，这个函数计算并返回
square of the given number. So there is really no need to add a comment and 给定数字的平方。所以真的没有必要添加评论和
explain that. Calculates and returns the 解释一下。计算并返回
square of a number. This is very repetitive and it creates 一个数字的正方形。这是非常重复的，它创造了
noiser code. Someone else reading your code they get distracted with all noiser代码。其他人在阅读你的代码后会分心
these unnecessary comments, so use comments to explain whys and hows. 这些不必要的评论，所以用评论来解释为什么和如何。
Not why's if you have made certain assumptions, we can use 不是为什么如果你做了某些假设，我们可以使用
comments to explain those assumptions. Or we can use them to add notes to remind yourself 评论来解释这些假设。或者我们可以使用它们来添加注释以提醒自己
or other developers to do something in the code. 或其他开发人员在代码中做某事。
So comments are good, but too much of a good thing is a bad thing. 所以评论很好，但太好的事情是件坏事。
In this tutorial I'm going to talk to you guys about classes in python. 在本教程中，我将与您讨论python中的类。
Classes are extremely important in programming and they're not 类在编程中非常重要，但它们不是
specific to python, in fact, a lot of other programming languages 具体到python，实际上还有很多其他的编程语言
do support the notion of classes. We use classes to define new types. 支持班级的概念。我们使用类来定义新类型。
For example, so far you have learned about the basic types in Python 例如，到目前为止，您已经了解了Python中的基本类型
like numbers, strings, and booleans, 像数字，字符串和布尔值，
these are the basic or simple types in python, you also learn 这些是python中的基本或简单类型，你也学习
about a couple of complex types like lists and 关于列表和列表等几种复杂类型
dictionaries. While these types are extremely useful, they cannot 字典。虽然这些类型非常有用，但它们不能
always be used to model complex concepts, for example, think about 总是被用来模拟复杂的概念，例如，思考
the concept of a point, or a shopping cart. A shopping cart is not 点或购物车的概念。购物车不是
a boolean, it's not a list, it's not a dictionary, it's a different kind of 一个布尔值，它不是一个列表，它不是一个字典，它是一个不同的类型
thing. So we can use classes to define new types to 事情。所以我们可以使用类来定义新类型
model real concepts. Now in this new model I'm going to show you how to 模型真实的概念。现在，在这个新模型中，我将向您展示如何操作
define a new type called point, and this new type is going to have methods 定义一个名为point的新类型，这个新类型将有方法
for working with points. For example, back to our main program. Let's 用于处理积分。例如，回到我们的主程序。让我们
create a list, numbers, 1, 2, 3, 创建一个列表，数字，1,2,3，
you know that here, when we type that dot, 你知道吗，当我们输入那个点时，
functions or methods available in list objects, now similar to this, 列表对象中可用的函数或方法，现在与此类似，
we're going to create a new type called point, this point is 我们要创建一个名为point的新类型，这一点是
going to have metas like move, draw, get, 会有像移动，画画，得到，
distance from another point, these are the operations that we can 距离另一点的距离，这些是我们可以进行的操作
perform on points. So let me show you how to do this. 在点上表演。那么让我告诉你如何做到这一点。
We start by defining a class by using the class keyword, 我们首先使用class关键字定义一个类，
and right after that we give our class a name. Now look at the 在那之后我们给了我们班级一个名字。现在看看
naming convention I've used here. I've capitalized the first letter here, this is what we call 我在这里使用的命名约定。我把这里的第一个字母大写了，这就是我们所说的
a pascal naming convention. So the naming convention we 一个pascal命名约定。所以命名约定我们
used for naming our classes is different from the convention we use for naming 用于命名我们的类与我们用于命名的约定不同
our variables and functions. For variables and functions we always 我们的变量和功能。对于变量和函数我们总是如此
use lower case letters and we separate multiple words using 使用小写字母，我们使用分隔多个单词
an underscore. But when naming classes, we don't use an underscore 一个下划线。但是在命名类时，我们不使用下划线
to separate multiple words, instead we capitalize 分开多个单词，而不是我们大写
the first letter of every word. So in this case our class has only a single 每个字的第一个字母。所以在这种情况下，我们的班级只有一个
word, if you had multiple words, let's say email client 换句话说，如果你有多个单词，那就说电子邮件客户端吧
look I've capitalized the first letter 看我把第一个字母大写了
of every word. Once again, this is called pascal naming convention 每一个字。再一次，这称为pascal命名约定
and it comes from the old pascal language that you might have heard of. 它来自你可能听说过的旧帕斯卡语。
So, back to our point class. Here we add 所以，回到我们的点类。在这里我们添加
a colon which means we're going to define a block, now 冒号意味着我们现在要定义一个块
in this block we can define all the functions or methods that belong 在这个块中，我们可以定义属于的所有函数或方法
to points, for example, we can define a function called 例如，我们可以定义一个名为的函数
move for moving a port, now note that as soon as we 移动移动港口，现在请注意我们
type open parenthesis, pycharm automatically adds self here, 键入open括号，pycharm自动在这里添加self，
this is a special keyword and we're going to get back to this shortly. 这是一个特殊的关键字，我们很快就会回到这里。
So, for now, in this method, let's print 那么，就目前而言，在这种方法中，让我们打印一下
move on the terminal, now let's define another 移动终端，现在让我们定义另一个
method like draw, so define draw 像draw这样的方法，所以定义draw
and in this method, let's print draw on the terminal, now 现在，让我们在终端上打印
let's remove the indentation, we're done 让我们删除缩进，我们已经完成了
with the definition of our point class, so we need to add two line breaks 有了我们的点类的定义，所以我们需要添加两个换行符
okay, so with this class we defined a new 好的，所以在这个课上我们定义了一个新的
type with this new type we can create new objects, so 使用这种新类型输入我们可以创建新对象，所以
an object is an instance of a class, a class simply defines 对象是类的实例，类只是定义
the blueprint or the template for creating objects, and 用于创建对象的蓝图或模板，以及
objects are the actual instances based on that blue print. 对象是基于该蓝图的实际实例。
So we can have tens of hundreds of points on the screen, these are the 所以我们可以在屏幕上有数百个点，这些是
objects or the instances. So to create an object, 对象或实例。所以要创建一个对象，
we type out the name of our class and then call it like a function 我们输入我们类的名称，然后将其称为函数
this creates a new object and then returns it. So then we can store 这会创建一个新对象然后返回它。那么我们可以存储
that object in a variable, let's call that point 1, 变量中的那个对象，让我们调用那个点1，
there you go, now when we use the dot operator. 你去，现在我们使用点运算符。
Look, we have these two methods that we 看，我们有这两种方法
defined, draw and move, we also have a bunch of other methods that we 定义，绘制和移动，我们还有一堆其他方法
start with two underscores, these are called magic methods 从两个下划线开始，这些被称为魔术方法
we'll look at them later in the course. Now, let's call the draw method 我们稍后会在课程中看到它们。现在，让我们调用draw方法
of our point object, and run our program 我们的点对象，并运行我们的程序
so you can see draw is printed on the terminal. Now 所以你可以看到画面印在终端上。现在
apart from methods these objects can also have attributes and these attributes 除了方法之外，这些对象还可以具有属性和这些属性
are like variables that belong to a particular object. For example, 就像属于特定对象的变量一样。例如，
here, after we've defined .1. 在我们定义之后.1。
We can set .1.x to 我们可以将.1.x设置为
10. And .1.1 to 20. 10.和.1.1至20。
Now we can easily print out these attributes, let's print 现在我们可以轻松打印出这些属性，让我们打印出来
.1.x and run our program one more time .1.x并再次运行我们的程序
take a look, so the x coordinate of .1 is 10. 看一下，所以.1的x坐标是10。
Now we can create another object, let's call that.2 现在我们可以创建另一个对象，让我们调用它
so .2 once again we type out the name 所以.2再一次我们输入了这个名字
of our class and then call it like a function. Now this object is completely 我们的类，然后将其称为函数。现在这个对象完全是
different then the first object. So if you print 不同的是第一个对象。所以，如果你打印
.2.x .2.x
look we get this attribute error, because this point object doesn't have an 看看我们得到这个属性错误，因为这个点对象没有
attribute called, so each object is a different 属性调用，所以每个对象都是不同的
instance of our points class. Now here 我们的积分类的实例。现在在这里
we can assign a value to .2.x. Let's 我们可以为.2.x赋值。让我们
send that to 1, and then run our program so we can see the 发送到1，然后运行我们的程序，以便我们可以看到
x coordinates of .2 is 1. So to recap .2的x坐标是1.所以回顾一下
we use classes to define new types, these types can have 我们使用类来定义新类型，这些类型可以有
methods that we define in the body of the class and they can also 我们在类的主体中定义的方法，它们也可以
have attributes that we can set anywhere in our programs. 我们可以在程序中的任何位置设置属性。
You have learned how to create new types using classes 您已经学习了如何使用类创建新类型
now therein a tiny problem in this implementation. 现在这里有一个很小的问题。
We can create a pooint object without an x or y coordinate 我们可以创建一个没有x或y坐标的pooint对象
let me show you. So point we create this 让我演示给你看。所以我们创造了这一点
object here, and before we set point.x let's 对象在这里，在我们设置point.x之前
print point .x run our program 打印点.x运行我们的程序
we get this attribute error which you 我们得到你的这个属性错误
saw in the last tutorial, so point object has no attribute called x 在上一个教程中看到，所以point对象没有名为x的属性
This is the problem we have here, it is possible, 这是我们在这里遇到的问题，有可能，
to have a point object without an x or y coordinates. 有一个没有x或y坐标的点对象。
And that doesn't really make sense, because whenever we talk about a point, 这并没有多大意义，因为无论何时我们谈论一个观点，
we ned to know where that point is located. To solve this problem, we 我们需要知道这一点的位置。为了解决这个问题，我们
use a constructor. A constructor is a function that gets called 使用构造函数。构造函数是一个被调用的函数
at the time of creating an object. So here on line 9, when creating this 在创建对象时。所以在第9行，创建时
point object, we want to pass values for x or y 点对象，我们想要传递x或y的值
coordinates, let's say 10 and 20, and with this, 坐标，比方说10和20，用这个，
this point object that we get here, will have it's 我们到这里的这一点对象将会有它
x and y coordinates initialized. So how do we do this? 初始化x和y坐标。那我们该怎么做呢？
We need to add a special method in this class called constructor. 我们需要在这个类中添加一个名为constructor的特殊方法。
So, on the top, I define a new function, 所以，在顶部，我定义了一个新函数，
look at the name of this function, double underscore init, double under 看看这个函数的名称，双下划线init，双下
score. So init is short for initialized, and this is the 得分了。所以init是初始化的简称，这就是
function or the method that gets called when we create a new point object. 函数或创建新点对象时调用的方法。
Now, press enter, so this automatically adds, 现在，按回车，这会自动添加，
self in parenthesis, we're going to come back to this shortly. 在括号中，我们将很快回到这里。
Now, right after self, we want to add 2 extra parameters, 现在，在我自己之后，我们想要添加2个额外的参数，
x and y, so let's type out x and y, x和y，所以让我们输入x和y，
and then in the body of this method, we should read the values past 然后在这个方法的主体中，我们应该阅读过去的值
here, and use them to initialize our object, so when we pass 在这里，并使用它们来初始化我们的对象，所以当我们通过
10 and 20, these arguments will be used for 10和20，这些参数将用于
x and y parameters. Now to initialize x和y参数。现在进行初始化
our object, we type our code like this. Self.x equals 我们的对象，我们这样输入代码。 Self.x等于
x. And then self.y equals y. X。然后self.y等于y。
What is going on here, this self that you see here is a reference to the current 这里发生了什么，你在这里看到的这个自我是对当前的参考
object. So, back to line 13, 宾语。所以，回到第13行，
when we create a new point object, self references that object in 当我们创建一个新的点对象时，自我引用该对象
memory, the same object, that we're referencing using this cariable. 内存，同一个对象，我们使用这个cariable引用。
So earlier we typed point.x equals 10, 所以早些时候我们输入了point.x等于10，
with this code we set the x attribute of this point object. 使用此代码，我们设置此点对象的x属性。
Now what we have in the constructor is exactly the same, we use the self 现在我们在构造函数中的内容完全相同，我们使用self
to reference the current object and then we set the x attribute 引用当前对象然后我们设置x属性
to the x argument passed to this function. So, 传递给this函数的x参数。所以，
using this init method, we can initialize our objects and 使用这个init方法，我们可以初始化我们的对象和
we refer to this method as a constructor, this method is used to construct 我们将此方法称为构造函数，此方法用于构造
or create an object, okay? so, 或创建一个对象，好吗？所以，
Now, let's remove line 14, so we created 现在，让我们删除第14行，所以我们创建了
point object with these values and then print point.x let's run our program 用这些值指向对象然后打印point.x让我们运行我们的程序
there you go, x is 10, we can also change 你去，x是10，我们也可以改变
these values later, so just before printing point.x we can set 这些值以后，所以在打印point.x之前我们可以设置
point.x to 11 and then run our program point.x到11然后运行我们的程序
so x is updated, beautiful, alright, here's a 所以x更新，漂亮，好吧，这是一个
exercise for you. I want you to define a new type called person 为你锻炼。我希望你定义一个名为person的新类型
these person objects should have a name attribute as well as a talk 这些人物对象应该具有名称属性以及谈话
method. This is a very easy exercise so, 方法。这是一个非常简单的练习，所以，
you can tackle it in probably a couple of minutes. 你可以在几分钟内解决它。
Alright, so 好吧，好吧
first we start with a class keyboard, we define this person class 首先我们从类键盘开始，我们定义这个人类
once again look at the naming convention I have used here, the first letter 再次看看我在这里使用的命名约定，第一个字母
of the first word in this case the only word is uppercase. 在这种情况下，第一个单词的唯一单词是大写。
Now, in this class we define all the methods we need, currently we only have 现在，在这个类中，我们定义了我们需要的所有方法，目前我们只有
method that is the talk method, so define talk, 方法是谈话方法，所以定义谈话，
pycharm automatically adds self, we come back to this shortly, pycharm自动添加自我，我们很快回到这里，
for now let's just print talk. 现在让我们打印谈话。
So this is the body of our person class. We remove the indentation 所以这是我们班级的主体。我们删除缩进
and add two line breaks after defining our 并在定义之后添加两个换行符
classes. Now we can create the personal object, let's call that jon, 类。现在我们可以创建个人对象，让我们称之为jon，
we set it to person, we can call jon. 我们把它设置为人，我们可以调用jon。
talk, let's run the program so we get this 说说，让我们运行程序，这样我们就可以了
message, beautiful, but our person objects should also have 消息，美丽，但我们的人物对象也应该有
a name attribute, it doesn't make sense to have a person without the name. 一个名称属性，没有名字的人是没有意义的。
So that is when we use the constructor function. So on the top, 这就是我们使用构造函数的时候。所以在顶部，
we add a new method, at the name of this method is double 我们添加一个新方法，这个方法的名称是double
underscore, init double underscore. So we 下划线，init双下划线。所以我们
call this method a constructor. Now in this method we need to 将此方法称为构造函数。现在我们需要这种方法
add another parameter which is name, so we 添加另一个名称的参数，所以我们
simply set self.name to name. Once 只需将self.name设置为name即可。一旦
again, self references the current object, we're setting the name 再次，self引用当前对象，我们正在设置名称
attribute of the current object, to the name argument passed to this method. 当前对象的属性，传递给此方法的name参数。
With this we can go back to line 9, and 有了这个，我们可以回到第9行，并且
when creating a person object we pass the name, let's see jon smith 在创建人物对象时，我们传递名称，让我们看看jon smith
now let's print jon.name and run our program 现在让我们打印jon.name并运行我们的程序
there you go. So jon Smith is printed on the terminal, beautiful, 你去吧所以jon Smith印在终端上，漂亮，
so this is all you had to do as part of this exercise. But 所以这就是你在练习中所要做的一切。但
let's make this program a little more interesting. Instead of printing 让我们让这个程序更有趣。而不是打印
this boring message here, let's print hi, I am Jon Smith. 这个无聊的消息，让我们打印嗨，我是乔恩史密斯。
So, we can use a formatted string with a message 因此，我们可以使用带有消息的格式化字符串
like Hi, I am, now we want to dynamically 喜欢你，我，现在我们想要动态
add the name of this person. How do we do this? Well, look at 添加此人的姓名。我们如何做到这一点？好吧，看看
the parameter of this talk method. As I told you before, 这个谈话方法的参数。正如我之前告诉你的，
every method in our class should have this parameter, self. 我们班中的每个方法都应该有这个参数self。
And they should be the very first parameter of each method, okay? So, 它们应该是每种方法的第一个参数，好吗？所以，
with this self we can get reference to the current object, so 有了这个自我，我们可以得到当前对象的引用，所以
we can add curly braces and type out self 我们可以添加花括号并输入self
.name. Thsi returns the name attribute of the current 。名称。 Thsi返回当前的name属性
person object. With this we can remove 人物。有了这个，我们可以删除
line 10, where we print jon.name and simply call 第10行，我们打印jon.name并简单地调用
jon.talk. Lets run our program. jon.talk。让我们运行我们的程序。
Hi, I am Jon Smith. There we go, beautiful. 嗨，我是Jon Smith。我们去，美丽。
Now we can build another person object, let's say Bob, is a new person 现在我们可以建立另一个人物，让我们说鲍勃，是一个新人
called Bob Smith, when we say 当我们说时，叫Bob Smith
Bob.talk. And run our program Bob.talk。并运行我们的程序
we get a different message, hi I'm Bob Smith, so 我们得到一个不同的消息，嗨，我是鲍勃史密斯，所以
each object is a different instance of a person class. 每个对象都是人类的不同实例。
In this tutorial Im going to talk to you about inheritance in Python, 在本教程中，我将与您讨论Python中的继承，
inheritance is a mechanism for using code and once again 继承是一种再次使用代码的机制
it's no limited to python, most languages that support classes 它不仅限于python，大多数支持类的语言
also support inherence. So let ms how you how this works. Let's say 也支持内在性。那么请告诉ms你如何运作。让我们说吧
we have this dog class with a simple walk method, in this method 在这种方法中，我们有一个带有简单步行方法的狗类
we're simply printing the walk message on the terminal, now let's say 我们只是在终端上打印步行消息，现在让我们说吧
in real program, instead of just one line of code here we're going to have 10 lines of 在实际程序中，我们将有10行代码，而不仅仅是一行代码
code. Now, what if in the future we want to define a 码。现在，如果将来我们想要定义一个
another class let's say cat and we want to add 另一个类让我们说猫，我们想要添加
this walk method there as well. Sow e'll have to repeat all that code, 这种步行方法也是如此。母猪必须重复所有代码，
in this new class, walk. 在这个新课上，走吧。
This is bad. because you have repeated or duplicated our 这是不好的。因为你重复或重复我们的
code. In programming we have a principle called dry 码。在编程中，我们有一个叫干的原则
which is short for don't repeat yourself. 这是简短的不要重复自己。
Let's say sometime in the future, we discover a problem with our walk method. If you 让我们说在将来的某个时候，我们发现walk方法存在问题。如果你
have repeated or duplicated this method in many other places, then we'll 在许多其他地方重复或重复这种方法，然后我们会
have to come back and fix that problem in every single place we have 必须回过头来解决我们所拥有的每个地方的问题
duplicated this code. So that's why in programming we should not define something 复制了这段代码。所以这就是为什么在编程中我们不应该定义一些东西
twice. So how can we solve this problem. 两次。那么我们怎样才能解决这个问题呢。
There are different approaches, one approach that is easier to understand for beginners is inherence 有不同的方法，一种方法更容易理解初学者是固有的
Now here's how it works. We define a new class called 现在这是它的工作原理。我们定义了一个名为的新类
maml and move the walk method right there. Then we'll have maml并将walk方法移动到那里。那我们就有了
the dog and cat classes inherit that method 狗和猫类继承了这种方法
from their parent just like how humans inherit certain 来自他们的父母，就像人类继承某些东西一样
attributes or behaviors form their parents. That' exactly the same, so 属于他们父母的属性或行为。这完全相同，所以
on the top, we define a new class, let's call it 在顶部，我们定义一个新类，让我们称之为
mammal then we need to move the walk method there. So, 哺乳动物然后我们需要在那里移动步行方法。所以，
we select it, cut it, using command and x on Mac, or 我们选择它，剪切它，在Mac上使用命令和x，或者
control x on Windows and then paste it here, beautiful, 在Windows上控制x，然后将其粘贴到这里，很漂亮，
now we want our dog class to inherit this class from the mammal class 现在我们希望我们的狗类从哺乳动物类继承这个类
so right after the class name we add parenthesis and 所以在类名后面我们添加括号和
then type out the name of the parent class. In this case, mammal. 然后输入父类的名称。在这种情况下，哺乳动物。
That's all we have to do. So with his the dog class 这就是我们所要做的。所以和他的狗班一起
will inherit all he methods defined in the mammal class. 将继承哺乳动物类中定义的所有方法。
Now there is a tiny problem here, if you look at line 9, there is a 现在这里有一个小问题，如果你看第9行，就有一个
red underline here. The reason this is happening is because 红色下划线。发生这种情况的原因是因为
python doesn't like an empty class, so right now our dog python不喜欢空类，所以现在我们的狗
class is empty, it doesn't have anything, we either need to add methods class是空的，它没有任何东西，我们要么需要添加方法
specific to dogs or just to make Python happy, 特定于狗或只是为了让Python开心，
we can use the path statement, and that basically means nothing. 我们可以使用路径语句，这基本上没有任何意义。
We're telling python interpreter, hey, pass this line, don't worry about it. But with this, we 我们告诉python翻译，嘿，通过这一行，不要担心。但有了这个，我们
don't have an empty class, okay? Now as I told you before 没有空课，好吗？正如我之前告诉你的那样
after each class I should have two line breaks so right now we 每节课后我应该有两个换行符，所以现在我们
only have one line break and that is the reason we have this little warning here, so let's add 只有一个换行符，这就是我们在这里发出这个小警告的原因，所以让我们补充一下
another black line, beautiful, so our dog class is 另一条黑线，美丽，所以我们的狗类是
inheriting the walk method from the mammal, you need to apply the same change 继承哺乳动物的步行方法，你需要应用相同的变化
to the cat class. So we'll have cat 到了猫班。所以我们会有猫
inherit from mammal, and then, 继承自哺乳动物，然后，
use the path statement here. With these changes, 在这里使用path语句。随着这些变化，
now we can create either a dog or cat object, let's say dog, 现在我们可以创建一个狗或猫对象，让我们说狗，
one, we set it to a dog object, and 一，我们将它设置为狗对象，并且
when we type dot look we have the walk method that is defined 当我们键入dot look时，我们有定义的walk方法
in the mamall class. We can easily call it, let's run our program 在天猫班。我们可以轻松地调用它，让我们运行我们的程序
there you go, you see the walk message, 你去，你看到步行信息，
so both the dog and cat classes are inheriting all the classes 所以dog和cat类都继承了所有类
methods defined in their parent class. Now here we can add methods 在其父类中定义的方法。现在我们可以添加方法了
specific to dogs, for example, we can define a method called 例如，我们可以定义一种叫做狗的方法
bark, and then print bark. 树皮，然后打印树皮。
Now, we don't need the path statement because we already defined 现在，我们不需要路径语句，因为我们已经定义了
one method in our dog class. So now when we type out 我们的狗类中的一种方法。所以现在当我们输入
dog1. look we have two methods DOG1。看，我们有两种方法
one is the walk method that we inherited and the bark method that we just 一个是我们继承的walk方法和我们刚才的bark方法
defined in the dog class. Similarly, we can add a method 在狗类中定义。同样，我们可以添加一个方法
specific to a cat, for example, we can define a method called 例如，我们可以定义一个名为cat的方法
be annoying, I hate cats, so let's print 很讨厌，我讨厌猫，所以让我们打印
annoying , now when we create the cat 很烦人，现在我们创造了猫
object, let's remove that, cat1 对象，让我们删除它，cat1
and type out cat1. you have these two methods 并输入cat1。你有这两种方法
dog and be annoying. So these are the basics of inheritance in python 狗，很烦人。所以这些是python中继承的基础知识
Now there is more to this that really goes beyond the scope of this beginners tutorial. 现在还有更多内容超出了本初学者教程的范围。
If you're interested to learn more about inheritance and other mechanisms 如果您有兴趣了解有关继承和其他机制的更多信息
for reusing code, I encourage you to get my complete Python course. 为了重用代码，我鼓励你完成我的完整Python课程。
In this tutorial, I'm going to talk to you about modules in python. 在本教程中，我将与您讨论python中的模块。
A module in python is basically a file with some python code. python中的模块基本上是一个带有一些python代码的文件。
And we use modules to organize our code into files. 我们使用模块将代码组织到文件中。
Just like sections in a super market. When you go to a super market there are different sections 就像超市里的部分一样。当你去超级市场时，有不同的部分
for fruits, vegetables, cleaning products, and so on. We don't have 用于水果，蔬菜，清洁产品等。我们没有
one section with all the products in the super market. We have the same concept 一个部分包含超市中的所有产品。我们有相同的概念
in programming. So instead of writing all our code, instead of writing 在编程中。因此，而不是写我们所有的代码，而不是写
all the functions and classes in app.py we want to break up our code 我们想要破解代码的app.py中的所有函数和类
into multiple files. We refer to each file as a module. 分成多个文件。我们将每个文件称为模块。
With this, not only is our code better organized and structured, 有了这个，我们的代码不仅更有条理和更有条理，
but we'll also have the ability to reuse our code. For example, 但我们也有能力重用我们的代码。例如，
over here in app.py, I've defined two functions for converting 在app.py这里，我已经定义了两个转换函数
weight from pounds to kilograms and vice versa. 体重从磅到千克，反之亦然。
Now we can take these two functions and put them in a separate module 现在我们可以将这两个函数放在一个单独的模块中
called converters, and then we can import that module into any 称为转换器，然后我们可以将该模块导入任何模块
program that needs these converter functions. Let me show you how to do this. 需要这些转换器功能的程序。让我告诉你如何做到这一点。
So, let's open up our project panel and right 那么，让我们打开我们的项目面板吧
click the project, go to new, and add the new file, 单击项目，转到新建，然后添加新文件，
we're going to call this file, converters.py. 我们打算调用这个文件，converter.py。
Then, we go back to app.py 然后，我们回到app.py.
select all this code, cut it and then 选择所有这些代码，然后剪切它
paste it into our new module. converters.py. 将其粘贴到我们的新模块中。 converters.py。
With this we have cleaned up our app module it's not bloated with different 有了这个，我们已经清理了我们的应用程序模块，它没有臃肿与不同
functions, so as we write more functions with classes, we put them in their 函数，所以当我们用类编写更多函数时，我们将它们放入其中
corresponding modules, a module should contain all the related 相应的模块，一个模块应该包含所有相关的
functions and classes, okay? Now you want to 功能和类，好吗？现在你想要
import the converters module into our app module, and that is really easy, on the 将转换器模块导入我们的app模块，这非常简单
top we write import converters 顶部我们写入口转换器
without extension, so we don't add .py we only type out the name of 没有扩展名，所以我们不添加.py我们只输入名称
the file. Let me close that project panel. Now, 文件。让我关闭那个项目小组。现在，
this converter is an object so we can use the dot operator 这个转换器是一个对象，所以我们可以使用点运算符
to access it's members. So currently we have defined 访问它的成员。所以目前我们已经定义了
these two functions inside of our module, kilogram to pound, 这两个函数在我们的模块里面，千克到磅，
and pounds to kilograms. So we can easily call one of these functions 和磅到公斤。所以我们可以很容易地调用其中一个函数
let's pass 70 kilos get the result and print it out. 让我们通过70公斤得到结果并将其打印出来。
Run our program, so, 运行我们的程序，所以，
that is under 55 pounds. Now there is also another 不到55磅。现在还有另一个
syntax for importing modules, let me show you, so 导入模块的语法，让我告诉你，所以
instead of importing the entire module we can import specific functions from that 我们可以从中导入特定的功能，而不是导入整个模块
module. Here's how it works. We type out from 模块。这是它的工作原理。我们输入
then we add the name of our module, so converters 然后我们添加模块的名称，所以转换器
import, now here if you press control and space 导入，现在在这里，如果你按下控制和空间
we can see the list of all the functions defined in this module. 我们可以看到这个模块中定义的所有函数的列表。
So here we can grab one of these functions let's say kilogram to pound, and 所以在这里我们可以抓住其中一个函数，让我们说千克到磅，和
with this we can directly call this function just like 有了这个我们就可以直接调用这个函数了
the function defined in this file. App.py. Compare 此文件中定义的函数。 App.py.相比
what we have on line 4 to what we have on line 6. On line 6 我们在第4行到第6行的内容。第6行
we'll have to prefix this function with the name of an object 我们必须在此函数前加上一个对象的名称
converters. So we have to type converters. and then we'll be able to 转换器。所以我们必须输入转换器。然后我们就可以了
access this function. In contrast. When we import 访问此功能。相反。当我们导入
a specific function from our module, then we can easily call that function 从我们的模块中的特定功能，然后我们可以轻松地调用该功能
without prefixing it with the module name. So here's what 没有为模块名称添加前缀。所以这就是
you need to take away. We use modules to better organize our code. Instead of 你需要带走。我们使用模块来更好地组织代码。代替
writing all the code, instead of writing all the functions in one file, you break up 编写所有代码，而不是将所有函数都写在一个文件中，你就分手了
our code across multiple files. Each file is called 我们的代码跨多个文件。每个文件都被调用
a module and it should contain all the related functions and classes 一个模块，它应该包含所有相关的函数和类
then we can import a module into another module, in this case, we are importing 然后我们可以将模块导入另一个模块，在这种情况下，我们正在导入
the converters module into our app module. And as you saw, 转换器模块进入我们的app模块。正如你所看到的，
there are two ways to import this module, we can import the entire module, and then 有两种方法可以导入这个模块，我们可以导入整个模块，然后
we'll get an object with the same name as the module so we can 我们将获得一个与模块同名的对象，以便我们可以
type converters . and then we'll be able to access all the 类型转换器。然后我们就可以访问所有了
functions and classes defined in this module. The other appraoch 此模块中定义的函数和类。另一个appraoch
is to impot a specific function or class from a module using the 是使用模块从模块中输入特定的函数或类
from import statement. Now here 来自进口声明。现在在这里
is your exercise. Earlier in the course you had an exercise for finding the 是你的运动。在课程的早些时候，你有一个练习找到
largest number in the list. So here on the top we have 列表中最大的数字。所以我们在顶部
a list of numbers, initially we set max to the first number in this list 一个数字列表，最初我们将max设置为此列表中的第一个数字
then, we look through this list, get each number, 然后，我们仔细查看这个列表，得到每个数字，
if the current number is grater than max, then we reset max. 如果当前数字大于最大值，那么我们重置最大值。
And finally when we are done with our loop we print max on the terminal. 最后，当我们完成循环时，我们在终端上打印max。
Now here's what I want you to do. This code currently 现在这就是我想要你做的。这个代码目前
has no organization. We don't have any functions, we don't have any modules, we wrote all 没有组织。我们没有任何功能，我们没有任何模块，我们写的都是
the code in app.py, that is okay for small programs, app.py中的代码，适用于小程序，
but as your programs grow, you need to properly organize your code into 但随着程序的增长，您需要正确地组织代码
classes and modules. So, as part of this exercise, I want 课程和模块。所以，作为这项练习的一部分，我想要
you to write a function, call it find max. 你写一个函数，称之为find max。
This function should take a list, and return the largest number in that list. 此函数应采用列表，并返回该列表中的最大数字。
Now after you do this, go ahead and put this function 现在执行此操作后，继续执行此功能
in a separate module. So extract it from here, and put it in a module, 在一个单独的模块中。所以从这里提取它，并把它放在一个模块中，
called utils. So in this module we're going to have a bunch of 叫做utils。所以在这个模块中我们将会有一堆
utility functions, oaky? Then import the utility 实用功能，橡木？然后导入该实用程序
module into the current module and call this function. 模块进入当前模块并调用此函数。
Finally, get the result and print it on the terminal, this is a 最后，得到结果并将其打印在终端上，这是一个
fantastic exercise to teach you how to properly organize your code. So, 精彩的练习，教你如何正确组织你的代码。所以，
go ahead and spend a couple minutes on this exercise. 继续花几分钟进行这项练习。
So the first 所以第一个
step is to define a function called find max. So on the 步骤是定义一个名为find max的函数。所以关于
top, I'm going to define function, find max 顶部，我要定义函数，找到最大值
this function should take a list of numbers, so we 这个函数应该有一个数字列表，所以我们
add a parameter called numbers. Now inside of this function you 添加一个名为numbers的参数。现在你在这个功能里面
want to have these few lines, this is the algorithm for finding the 想拥有这几行，这就是找到的算法
largest number in a list, we don't want to bring the first line, because this 列表中的最大数字，我们不想带第一行，因为这个
is where we create a list. So somewhere in our program we're going to create this list, 是我们创建列表的地方。所以在我们的程序中，我们将创建此列表，
and tehen pass it as an argument to pass max 并且tehen将它作为参数传递给最大值
function. Okay? So, let's go ahead and grab 功能。好的？那么，让我们继续前进吧
these few lines, cut them, and put them inside 这几行，切割它们，并把它们放在里面
of find max function. Alright, so we find the 找到最大功能。好吧，我们找到了
largest number in this list, finally we need to return it, so, 这个列表中最大的数字，最后我们需要返回它，所以，
return max. So here's our function. Now we 返回最大值所以这是我们的功能。现在我们
need to move this into a separate module. So let's go to the panel 需要将其移动到单独的模块中。那么我们来看看小组
right click the project and add a new 右键单击该项目并添加一个新项目
file, we can also add a new python file that's the same, let's call that 文件，我们也可以添加一个相同的新python文件，让我们调用它
utils so if you add a new python file utils所以如果你添加一个新的python文件
pycharm automatically adds .py extension, but if you go to pycharm自动添加.py扩展名，但如果你去
new file we'll have to explicitly type out 新文件，我们必须明确键入
.py. So make sure to get the extension right. 的.py。因此，请确保扩展正确。
Now, back to app.py, let's cut this code and 现在，回到app.py，让我们剪切这段代码吧
paste it into this new module, okay beautiful. 将它粘贴到这个新模块中，好漂亮。
Now I'm going o go back to app.py and import our new module. 现在我要回到app.py并导入我们的新模块。
We can either import the entire module, so import utils 我们可以导入整个模块，因此导入utils
and then access individual functions so we can call utils 然后访问各个函数，以便我们可以调用utils
.find max or we can explicitly .find max或我们可以明确
import this function from the utils module. It doesn't really make a difference. 从utils模块导入此函数。它并没有真正有所作为。
So, we could type from utils 所以，我们可以从utils输入
import find max. And then we 导入查找最大值然后我们
don't need to prefix this with the name of it's object, so we simply call 不需要在它前面加上它的对象名称，所以我们只需要调用
find max. Now we need to call this after we initialize our list. 找到最大值现在我们需要在初始化列表后调用它。
So first we need to define the list of numbers. 所以首先我们需要定义数字列表。
Then, we pass that list as an argument to find max 然后，我们将该列表作为参数传递给find
we get the result which is max and finally we print it 我们得到的结果是max，最后我们打印出来
on the terminal. let's go ahead and run our program, so the 在终端上。让我们继续运行我们的程序吧
largest number is 10, beautiful, 最大的数字是10，很漂亮，
now one last thing before we finish this tutorial, here we have this little warning 现在，在我们完成本教程之前，最后还有一件事，这里有一点警告
under max and the tool tip says shadows built in name 在max下，工具提示表示阴影内置名称
max. The reason we see this is because we actually have a function called 最大。我们之所以看到这个是因为我们实际上有一个叫做的函数
max. And it does the exact same job 最大。它完成了同样的工作
as our find max function. So this is actually built into python. 作为我们的发现最大功能。所以这实际上内置于python中。
But I ask you to do it as an exercise because it's a great exercise for beginners. 但是我要求你做这个练习，因为这对初学者来说是一个很好的练习。
Now, back to the warning, this warning is basically telling us that we are 现在，回到警告，这个警告基本上告诉我们我们是
overwriting the built in max function in python 覆盖python中内置的max函数
so we are changing the meaning of this function, and this is considered a bad practice, 所以我们正在改变这个功能的含义，这被认为是一种不好的做法，
let me show you what I mean. So let's comment out what we have on line 4, 让我告诉你我的意思。那么让我们评论我们在第4行的内容，
by putting a hash sign here, we can also use, 通过在这里放一个哈希符号，我们也可以使用，
a short cut command and slash on Mac, or control on slash Mac上的快捷命令和斜线，或斜线上的控制
on Windows. So now this line is commented out. Look at the 在Windows上。所以现在这条线被注释掉了。看着那（这
color of max here, it's purple, it's the same color as print. And this is the color 这里最大的颜色，它是紫色，它与印刷颜色相同。这就是颜色
used to identify the built in functions. So now we can call 用于识别内置函数。所以现在我们可以打电话
this function and give it a list of numbers 这个功能并给它一个数字列表
run our program we get the same exact result however, 运行我们的程序，我们得到相同的确切结果，
if we bring this line back, once again we can use the same 如果我们把这条线带回去，我们可以再次使用它
shortcut that is command and slash on Mac or control slash on Windows. 快捷方式是Mac上的命令和斜杠或Windows上的控制斜杠。
Now, we are redefining max, so up to 现在，我们正在重新定义最大值，所以最多
this line, max used to be a function, but after line 4 is executed 这一行，max曾经是一个函数，但在执行第4行之后
max is set to an integer, that is the largest number in this list. max设置为整数，即此列表中的最大数字。
So, we'll no longer be able to call it on line 5, 所以，我们将不再能够在第5行调用它，
let's run our program and see what happens. So there you go, 让我们运行我们的程序，看看会发生什么。那你去吧
we get this type error, int object is not 我们得到这种类型错误，int对象不是
callable, because max is now an integer, it's not a function, 可调用的，因为max现在是一个整数，它不是函数，
so we cannot call it. So to solve this problem, we can rename 所以我们不能称之为。所以要解决这个问题，我们可以重命名
this variable to something else. So right click here. 这个变量是别的东西。所以右键点击这里。
Go to refactor and rename, 去重构和重命名，
and rename this to maximum. 并将其重命名为最大值。
So you can see the warning is gone. As you can see we have the same warning 所以你可以看到警告消失了。如你所见，我们有同样的警告
in our utils module. So here we're redefining max 在我们的utils模块中。所以我们在这里重新定义最大值
and as I told you before this is a bad practice. We're changing the 正如我之前告诉你的那样，这是一种不好的做法。我们正在改变
meaning of built in functions in python. So, python中内置函数的含义。所以，
let's rename these as well, I'm going to use the short cut that is shift and 让我们重新命名这些，我将使用移位的捷径
f6 there you go and here we can change this to f6那里你去，我们可以改变这个
maximum. Enter, done. 最大值。输入，完成。
In this tutorial we're going to talk about packages in python, 在本教程中，我们将讨论python中的包，
packages are basically another way to organize our code, so currently 包基本上是组织我们的代码的另一种方式，所以目前
we have only 3 files for modules in our project but a real project 我们项目中的模块只有3个文件，但是一个真实的项目
can contain hundreds or even thousands of modules. 可以包含数百甚至数千个模块。
We don't want to add all those modules here, because over time this directory will get 我们不想在这里添加所有这些模块，因为随着时间的推移，这个目录将会得到
bloated with so many files, so a better approach is to organize related 有这么多文件臃肿，所以更好的方法是组织相关
modules, inside of a package, so a package is a container 模块，在包内，所以包是一个容器
for multiple modules In file system terms a package is 对于多个模块在文件系统术语中，包是
a directory or folder. So in our project we can add a new directory 目录或文件夹。所以在我们的项目中我们可以添加一个新目录
and in that directory we can add all the directed modules, as a metaphor think 并且在该目录中我们可以添加所有定向模块，就像一个隐喻所认为的那样
of a mall. When you go to a mall or a shopping center there are different 一个商场。当你去商场或购物中心时，有不同的
sections for mens women and kids clothing, so that is 对于男士女士和儿童服装，这样的部分
ilke a package, now when you go to the mens section, there are different areas ilke一个包，现在当你去男装部分，有不同的领域
fior different kinds of products, we have a section for shoes, t shirts, fior不同种类的产品，我们有一款适合鞋子，T恤，
jackets, and so on, so each of these is like a module, so, 夹克等等，所以每个都像一个模块，所以，
in this tutorial, I'm going to show you how to create and use packages in Python, 在本教程中，我将向您展示如何在Python中创建和使用包，
here in our project panel, right click the panel and 在我们的项目面板中，右键单击面板和
add a new directory. 添加一个新目录。
Let's call this directory e commerce. So we're going to create 我们将此目录称为电子商务。所以我们要创造
a package called e-commerce and in this package we're going to have 一个名为电子商务的软件包，我们将在这个软件包中使用
all the modules related for an ecommerce application. For example, 所有与电子商务应用程序相关的模块。例如，
we can have modules for sales, shipping, customer service 我们可以提供销售，运输，客户服务的模块
and so on. So let's go ahead 等等。让我们继续吧
Now here we have an empty directory, in order to convert this to a package 现在我们有一个空目录，以便将其转换为包
we need to add a special file in it. So, right 我们需要在其中添加一个特殊文件。所以，对
click this directory and add a new python file, 单击此目录并添加新的python文件，
call that file double underscore in it, double underscore 在其中调用该文件双下划线，双下划线
this is a special convention in Python. 这是Python中的一个特殊约定。
When Python interpreter sees a file with this name and 当Python解释器看到具有此名称的文件时
name in a directory, it treats this directory as a package, we also have a 在一个目录中的名称，它将此目录视为一个包，我们也有一个
shortcut for that in pycharm so let me delete this directory pycharm中的快捷方式，所以让我删除这个目录
and start again. So delete, once again right click 然后重新开始。所以删除，再次右键单击
the project, and go to new but instead of creating a new directory 该项目，并转到新的，而不是创建一个新目录
we can create a new python package, let's call 我们可以创建一个新的python包，让我们调用
our package ecommerce as you can see 我们的包电子商务，你可以看到
pycharm automatically creates this file for us so we don't have to manually create pycharm会自动为我们创建此文件，因此我们不必手动创建
it. Okay? Now, in this package, let's add a new module, 它。好的？现在，在这个包中，让我们添加一个新模块，
so a new python file, let's call it 所以一个新的python文件，让我们称之为
shipping okay? Now in this module we want to have 运费好吗？现在我们想要这个模块
a function for calculating the shipping costs. So define 用于计算运费的函数。所以定义
calculate, underline, shipping. 计算，下划线，运输。
Now don't worry about parameters or implementation, we simply print 现在不用担心参数或实现，我们只需打印
calculate underline shipping. 计算下划线运费。
Now lets say you want to import this shipping module 现在假设您要导入此运输模块
into our app module, with this new structure this shipping 进入我们的app模块，这个新的结构这个运输
module is now part of the ecommerce package, so we cannot import a direct 模块现在是电子商务包的一部分，因此我们无法直接导入
you have to start from the ecommerce package. So, 你必须从电子商务包开始。所以，
bak to app,py once again there are two ways to import this module. bak to app，py再一次导入这个模块有两种方法。
We can import the entire module or we can 我们可以导入整个模块，也可以
import one of it's functions or classes. So let me show you both approaches. 导入其中一个函数或类。那么让我告诉你两种方法。
First, we're going to import the entire module. Now what is the name of our 首先，我们将导入整个模块。现在我们的名字是什么
package? it is ecommerce, right? So ecommerce. 包？这是电子商务，对吗？所以电子商务。
shipping. So instead of typing import 运输。所以不要输入import
shipping we type import ecommerce 我们输入电子商务
.shipping. we have to prefix it with the name of it's package. 。运输。我们必须在它前面加上它的包名称。
Now to access any of the functions or classes in this module 现在访问此模块中的任何函数或类
we'll have to type out ecommerce.shipping. 我们必须输入ecommerce.shipping。
and there you go. Now we can access the calculate shipping 然后你去现在我们可以访问计算运费
function. So let's go ahead and run our program, 功能。那么让我们继续我们的计划，
we see this message printed on the terminal, beautiful. However 我们看到这个消息印在终端上，很漂亮。然而
with this approach, every time you want to call one of the functions 用这种方法，每次你想调用其中一个函数
to this module, we'll have to prefix it with ecommerce. 对于这个模块，我们必须在它前面加上电子商务。
shipping. that is very verbose. So when working with packages 运输。这非常冗长。所以在使用包时
we often use the second approach, using the 我们经常使用第二种方法，使用
from statement. So we can type from ecommerce 来自声明。所以我们可以从电子商务中输入
.shipping import .shipping导入
calculate shipping. Now we don't have to prefix this function 计算运费。现在我们不必为此函数添加前缀
with ecommerce.shipping, and we can call it multiple times 使用ecommerce.shipping，我们可以多次调用它
in this module, so our code is a little bit shorter. But what if 在这个模块中，所以我们的代码有点短。但是如果
you want to use multiple functions in this shipping module. We can either import them here, 您想在此运输模块中使用多个功能。我们可以在这里导入它们，
so calculate shipping, comma, let's say calculate_ 所以计算运费，逗号，让我们说计算_
tax. Or we can import the entire module and then 税。或者我们可以导入整个模块然后
access all he functions or classes in that module. So, 访问该模块中的所有函数或类。所以，
let me revert this back. To import the entire module 让我回复一下。导入整个模块
we type from ecommerce so we remove.shipping 我们从电子商务中输入，因此我们将remove.shipping
so from this package, import 所以从这个包中导入
the shipping module. Now the shipping module, 运输模块。现在发货模块，
is an object so we can access 是一个对象，所以我们可以访问
all the functions and classes defined here, using the dot operator, so 这里定义的所有函数和类，使用点运算符，所以
shipping. there you go, calculate shipping. So to 运输。你去，计算运费。所以
recap, when using the from statement we can either start from 回顾一下，当使用from语句时，我们可以从中开始
the package and import a specific module, or we can 包和导入一个特定的模块，或者我们可以
start from package.module 从package.module开始
and then import a specific function. Like calculate shipping. 然后导入特定的功能。喜欢计算运费。
So, this was the basics of using packages in 所以，这是使用包的基础知识
python. As I told you before, packages are extremely important, especially 蟒蛇。正如我之前告诉过你的那样，包装非常重要，尤其如此
when you want to work with a framework like django, we use django for building 当你想使用像django这样的框架时，我们使用django进行构建
web applications with python, and I'm going to show you that later in this course. 使用python的web应用程序，我将在本课程的后面向您展示。
So in Django we have quite a few packages and 所以在Django中我们有很多包和
each package contains several modules. So make sure to watch the tutorials, 每个包都包含几个模块。所以一定要看教程，
about modules and packages one more time, otherwise you're going to get confused later 关于模块和包再一次，否则你以后会感到困惑
when we get to Django. 当我们到达Django时。
In this tutorial I'm going to talk to you 在本教程中，我将与您交谈
guys about the built in modules in python, so python 关于python内置模块的人，所以python
comes with a standard library that contains several modules for common 附带一个标准库，其中包含几个常见的模块
tasks such as sending emails, working with date and time, 发送电子邮件，处理日期和时间等任务，
generating random values and passwords and so on. There are so many modules 生成随机值和密码等。有这么多模块
already built into Python and that means there is already lots of functionality 已经内置到Python中，这意味着已经有很多功能
that we can reuse. Let me show you where you can find 我们可以重用。让我告诉你你在哪里可以找到
this standard library. So open up this browser 这个标准库。所以打开这个浏览器
and search for python 3, module index, 并搜索python 3，模块索引，
make sure to add the version number, because the modules in python 2 are 确保添加版本号，因为python 2中的模块是
different from modules in python3. So here we search for python 3 与python3中的模块不同。所以我们在这里搜索python 3
module index, and the first page here, you can see is 模块索引，这里的第一页，你可以看到
python module index, or python 3, let's go ahead, python模块索引，或python 3，让我们继续，
so these are all the modules built into python, 所以这些都是内置于python中的模块，
for example we have modules for working with date and time, 例如，我们有用于处理日期和时间的模块，
we have modules for sending emails for encoding values, 我们有用于发送编码值的电子邮件的模块，
there are so many modules here. Now the explanation of all these is 这里有很多模块。现在解释所有这些是
beyond the scope of this course, that really requires it's own course. 超出本课程的范围，这确实需要它自己的课程。
Because we have so many modules here, but don't let this list intimidate you. 因为我们这里有很多模块，但是不要让这个列表吓倒你。
This is not something that you need to learn right now, as you learn python 当你学习python时，这不是你现在需要学习的东西
as you build more applications, gradually you will get familiar with more modules 随着您构建更多应用程序，您将逐渐熟悉更多模块
here, in fact nobody knows all the modules in python 实际上，没有人知道python中的所有模块
standard library. Everybody knows some modules based on the applications they have built. 标准库。每个人都基于他们构建的应用程序了解一些模块。
So in this tutorial, I'm going to show you how to use one of the built in modules, 所以在本教程中，我将向您展示如何使用其中一个内置模块，
for generating random values. So on the top, 用于生成随机值。所以在顶部，
we import the random module, now because 我们导入随机模块，现在因为
random is a built in module, we don't need to have a file here random是一个内置模块，我们不需要在这里有一个文件
called random.py. So python interpreter knows that random is a built 叫random.py。所以python解释器知道随机是一个内置的
in module as long as it knows where to find it. 在模块中，只要它知道在哪里找到它。
Now if you're curious where this random module is stored on the disk, let me show you, so 现在，如果你好奇这个随机模块存储在磁盘上的哪个位置，那么让我告诉你
here in the project panel, expand external 在项目面板中，展开外部
libraries, then expand python 3, 库，然后展开python 3，
and then one more time python 3.7 library 然后又一次python 3.7库
route. So this is a folder somewhere on your computer, in this folder, we have 路线。所以这是你计算机上的某个文件夹，在这个文件夹中，我们有
all the built in modules in python. So, python中的所有内置模块。所以，
if you scroll down, you should be able to find. 如果你向下滚动，你应该能够找到。
The random module. There you go. It's right here. So random. 随机模块。你去吧它就在这里。好随便喔。
py. You can open this and this shows you the source PY。你可以打开它，这会显示你的来源
code of this module. there you go. Now in this tutorial, 这个模块的代码。你去吧现在在本教程中，
we don't want to worry about hat, so let's go back to our app.py, 我们不想担心帽子，所以让我们回到我们的app.py，
and use this module to generate random values. So when we import 并使用此模块生成随机值。所以当我们进口时
this module, now we have this object, random, so we can use the dot 这个模块，现在我们有这个对象，随机，所以我们可以使用点
operator to acces it's methods. One method that is useful 运算符来访问它的方法。一种有用的方法
here is the random method, and every time we call it it generates 这是随机方法，每次调用它都会生成
a random value between 0 and 1. So here we can do a for loop, 0到1之间的随机值。所以这里我们可以做一个for循环，
let's say for I in, 让我说我，
range 3. So just to refresh your memory we are using the 范围3.所以只是为了刷新你的记忆我们正在使用
range function to create a range object. We can look through this range object range函数用于创建范围对象。我们可以查看这个范围对象
and in each iteration this object will spit out a value. 并且在每次迭代中，此对象将吐出一个值。
So with this for loop, we can execute this code 3 times. Now, let's 因此，使用此for循环，我们可以执行此代码3次。现在，我们来吧
indent this. Okay, and finally, let's print the result, 缩进这个。好的，最后，让我们打印结果，
There you go. So we can see 你去吧所以我们可以看到
that in each iteration we get a new random value between 0 and 1. 在每次迭代中，我们得到一个介于0和1之间的新随机值。
Now what if we want a random value for a particular age? 现在如果我们想要一个特定年龄的随机值怎么办？
Let's say random values between 10 and 20, well, 让我们说10到20之间的随机值，好吧，
there is another method here, rand int, we use 2 arguments here 这里有另一种方法，rand int，我们在这里使用2个参数
to specify our range, let's say 10 and 20. 指定我们的范围，比方说10和20。
Run our program one more time. Now we have the numbers 20 再次运行我们的程序。现在我们有20个数字
13 and 18. If we run our program one more time, 13和18.如果我们再次运行我们的程序，
We get different values, so 20, 19, 11, one more time. 我们得到不同的值，所以20,19,11，再一次。
Look, you also have another powerful method for 看，你还有另一个强大的方法
randomly picking an item from a list. Let's say we have a list of team members and you want to 从列表中随机挑选一个项目。假设我们有一个团队成员列表，你想要
randomly pick someone as the leader. Let me show you how to do that. 随机选择某人作为领导者。让我告诉你如何做到这一点。
So, we define the list of team members, we set it to 因此，我们定义了团队成员列表，我们将其设置为
jon, mary, bob, 琼，玛丽，鲍勃，
and mosh. Now we can call random 和莫什现在我们可以随机调用
.choice and pass our members list 。选择并通过我们的会员名单
this method randomly picks a method from this list and returns it. SO, 此方法从该列表中随机选择一个方法并返回它。所以，
we can store the result in leader and then 我们可以将结果存储在leader中然后
print it on the terminal. let's run our program, 将它打印在终端上。让我们运行我们的程序，
so now the leader is mary, if you run our program one more time 所以现在领导者是玛丽，如果你再次运行我们的计划
this line is mosh now it's mosh again one more time, 这条线是莫什，现在又一次又一次，
it really likes mosh, you get the point. So 它真的很喜欢，你明白了。所以
the random module is very powerful, and it has a lot of applications. 随机模块非常强大，它有很多应用程序。
Now here's a very cool exercise. I want you to write this program to 现在这是一个非常酷的练习。我想让你把这个程序写进去
roll a dice. So every time we run this program we get a different value. 掷骰子。因此，每次运行此程序时，我们都会得到不同的值。
Now we have 3 in 1, next time we get 3 and 6, one more time 现在我们有3合1，下次我们得到3和6，再一次
5 and 6, 4 and 3, double one, that's a very cool exercise. 5和6,4和3，双人，这是一个非常酷的运动。
So before we get started, I want you to pay attention to a few things. 所以在我们开始之前，我希望你注意一些事情。
I want you to define a class called dice 我希望你定义一个叫做骰子的类
in this class we're going to have a method called rope so every time we 在这个课程中，我们每次都会有一个叫做绳索的方法
call this method, we get a tuple, a tuple remember is a list of 调用这个方法，我们得到一个元组，一个元组记得是一个列表
values but values cannot be changed, you cannot add a new item to it, we 值但值无法更改，您无法向其添加新项目，我们
cannot remove it. So it's like a read only list. So every time, 无法删除它。所以它就像一个只读列表。所以每一次，
we call the row method, we should get the tuple of two random values. 我们调用row方法，我们应该得到两个随机值的元组。
So go ahead, and do this exercise, You'll see my solution next. 所以继续，做这个练习，接下来你会看到我的解决方案。
Alright, we start by importing the random module on the top, 好的，我们首先导入顶部的随机模块，
then we define a class called dice: 然后我们定义一个叫做骰子的类：
this class we want to have a method called row. So define row 这个类我们想要一个名为row的方法。所以定义行
parenthesis, this automatically adds self, now in this method, 括号，这会自动添加self，现在在这个方法中，
you want to generate two random values between 1 to 6. 你想生成1到6之间的两个随机值。
So we call random.randint. 所以我们调用random.randint。
And pass 1 and 6. So this generates the first number, 并传递1和6.所以这会产生第一个数字，
we can ut that here, now let's call this one more time, 我们可以在这里说，现在让我们再来一次，
so I'm going to copy what we have on line 5, put it here, and 所以我要复制第5行的内容，把它放在这里，然后
change this variable to second. So we have two variables first and second. 将此变量更改为秒。所以我们有两个变量第一和第二。
With two random values, finally we need to return them, 有两个随机值，最后我们需要返回它们，
in a tuple, so return parenthesis instead of square brackets, 在一个元组中，所以返回括号而不是方括号，
first and second, now 第一和第二，现在
in python when you want to return a python from a function, you don't have to add 在python中，当你想从函数返回一个python时，你不必添加
this parenthesis, so you can simplify your code and python will automatically 这个括号，所以你可以自动简化你的代码和python
interpret this as a tuple. So we are done 将此解释为元组。所以我们完成了
with a definition of our class, we remove the indentation and add 有了我们类的定义，我们删除缩进并添加
two line breaks. Now we create an object of this type 两个换行符。现在我们创建一个这种类型的对象
so you set dice to dice 所以你设置骰子骰子
next we roll the dice, so dice.roll and 接下来我们掷骰子，所以dice.roll和
print the result on the terminal, that's all you have to do. 在终端上打印结果，这就是你所要做的。
Let's run this program, so now we get 2 and 5, 让我们运行这个程序，现在我们得到2和5，
lets run it one more time, 3 and 2, 1 and 1, beautiful. 让它再次运行，3和2,1和1，美丽。
Now we have this little warning here, that says 现在我们在这里发出这个小小的警告
expected 2 black lines found 1. Once again this is 预计发现2条黑线1.再一次是这样
coming from pep 8, pep is short for python and 来自第8页，pep是python和
proposal. We have so many python enhancement proposals or peps. 提案。我们有很多python增强建议或者​​peps。
Pep 8 is a very famous 1, and in this document we have Pep 8是一个非常有名的1，在本文中我们有
all the best practices for formatting our code. So pep 格式化代码的所有最佳实践。所以鼓励
8 says, that after we define our classes we should have 2 lines breaks, we've already done that 8说，在我们定义了我们的类后，我们应该有2个换行符，我们已经完成了
so the reason this is complaining is because we had 1 line break before 所以这是抱怨的原因是因为我们之前有一次突破
the class, so let's add one more line break, now pycharm 这个类，让我们再添加一个换行符，现在是pycharm
is happy. So basically pycharm is following all the best 很开心所以基本上pycharm是最好的
practices defined in pep8. Now you don't have to memorize pep8中定义的实践。现在你不必记住
any of these best practices, as you code in pycharm, pycharm gives you a little more 任何这些最佳实践，因为你在pycharm中编码，pycharm会给你更多
warning, and gradually you will learn how to properly format your code, so it's 警告，逐渐你会学习如何正确格式化你的代码，所以它是
readable by other people. 其他人可读。
In this tutorial, I'm going to show you 在本教程中，我将向您展示
how to work with directories in python. So here in python3, 如何在python中使用目录。所以在python3中，
module index, you can see we have a module called path lib which provides an object 模块索引，你可以看到我们有一个名为path lib的模块，它提供了一个对象
oriented file system pact. That basically means it provides 面向文件系统协议。这基本上意味着它提供
classes that we can use to create objects to work with directories and 我们可以用来创建对象来处理目录和的类
files. So, if you click here, you can see how this module 文件。所以，如果你点击这里，你可以看到这个模块
works, we can see all kinds of examples. For example, here on there basic 工作，我们可以看到各种例子。例如，这里有基本的
useage, you can see this is how we import the path 用法，你可以看到这是我们如何导入路径
class from the path lib module. Let me zooming so you can see 路径lib模块中的类。让我放大，你可以看到
clearly. Okay, so look, from path lib, that's 清晰。好吧，那么从路径lib看，那就是
our module, import path, note the naming convention. 我们的模块，导入路径，注意命名约定。
P is capitalized, that means path is a class, so we need to create P大写，这意味着路径是一个类，所以我们需要创建
an instance of this class as you can see, right here, 你可以看到这个类的一个实例，就在这里，
now there's so much documentation here, you can read this on your own, but let me quickly show you 现在这里有很多文档，你可以自己阅读，但让我快速告诉你
a few examples to help you get started. So after pycharm 一些例子可以帮助您入门。所以在pycharm之后
on the top, from the path led module, that's 在顶部，从路径led模块，那是
import the path class. Now we need to create, 导入路径类。现在我们需要创建，
a path object to reference a file or directory 用于引用文件或目录的路径对象
on our computer. So there are basically to ways to do this we can use an absolute 在我们的电脑上。所以基本上有办法做到这一点，我们可以使用绝对
path, or a relative path which basically 路径，或基本上相对的路径
means a path starting from the current directory. For example, 表示从当前目录开始的路径。例如，
if you wanna reference this ecommerce directory in our project, we can 如果你想在我们的项目中引用这个电子商务目录，我们可以
use the relative path. So we start from the current directory and then 使用相对路径。所以我们从当前目录开始然后
go somewhere else. With absolute paths we start from the root 去别的地方。绝对路径我们从根开始
of our hard disk. For example, if you're on Windows you might have 我们的硬盘。例如，如果您使用的是Windows，则可能有
an absolute path like this, c drive, backslash, 像这样的绝对路径，c驱动，反斜杠，
program, files, backslash microsoft, 程序，文件，反斜杠微软，
so these are directories in c drive. 所以这些是c盘中的目录。
On Windows we use a backslash to build a path. If you're on 在Windows上，我们使用反斜杠来构建路径。如果你在
Mac or Linux, your paths will look a little bit different, so Mac或Linux，您的路径看起来会有所不同，所以
instead of backslash we have a forward slash lets say user/local 而不是反斜杠我们有一个正斜杠让我们说用户/本地
/bin. So these are examples of absolute paths. In /箱。所以这些是绝对路径的例子。在
this tutorial we're going to work with he relative path to work with this ecommerce 本教程我们将与他一起使用这个电子商务的相对路径
directory that we have in our project. Now if you didn't create this directory 我们项目中的目录。现在，如果您没有创建此目录
earlier, simply right click on the project and then go to new 之前，只需右键单击该项目，然后转到新项目
directory. oaky? So, let's close this panel, 目录。橡木？那么，让我们关闭这个小组，
we create a path object, now, 我们现在创建一个路径对象，
if you don't pas an argument here, this will reference the current directory 如果您不在此处传递参数，则会引用当前目录
alternatively, we can pass string, in this string we can 或者，我们可以传递字符串，在这个字符串中我们可以
add a file or a directory. let's say the ecommerce 添加文件或目录。让我们说电子商务
directory. Now this runs a path object, so we store it here, 目录。现在这会运行一个路径对象，所以我们将它存储在这里，
now this path object has a few interesting methods 现在这个路径对象有一些有趣的方法
for example, we can check to see if a path exists by calling 例如，我们可以通过调用来检查路径是否存在
the exist method. This simply returns a boolean so let's print it on the terminal, 存在的方法。这只是返回一个布尔值，所以让我们在终端上打印它，
there you go. So this path exists, 你去吧所以这条路存在，
what if it changes to ecommerce 1, and 如果它改为电子商务1，该怎么办？
run our program and get false, we can also create a new directory 运行我们的程序并得到错误，我们也可以创建一个新目录
for example, let's change our path to email, now when we 例如，让我们改变我们的电子邮件路径，现在我们
run this program, obviously we don't have this directory but we can 运行这个程序，显然我们没有这个目录，但我们可以
create it by calling the mk dir method 通过调用mk dir方法创建它
so make directory. Let's run our program 所以制作目录。让我们运行我们的程序
alright, this method returns none which basically means 好吧，这个方法没有返回基本上意味着什么
it doesn't return any values. Now if you look at the project panel you can see 它不会返回任何值。现在，如果您查看项目面板，您可以看到
here we have a new directory called emails, we can also delete this directory 这里我们有一个名为emails的新目录，我们也可以删除这个目录
so, instead of mk dir we call 所以，我们打电话给而不是mk dir
rmdir which is short for remove directory, but rmdir是删除目录的缩写，但是
let's go ahead, now we can see that directory is gone. We can also see 让我们继续吧，现在我们可以看到该目录已经消失。我们也可以看到
all the files and directories in a given path, that is, very useful if you want to 给定路径中的所有文件和目录，如果您愿意，这非常有用
write a little program to automate something. For example, 写一个小程序来自动化一些东西。例如，
you can iterate over all the spreadsheets in a directory one them and 您可以迭代目录中的所有电子表格
process them. Let me show you. First we change the path 处理它们。让我演示给你看。首先，我们改变路径
to the current directory and then we call the glob 到当前目录然后我们调用glob
method. With his method we cans each for files and 方法。使用他的方法，我们可以为文件和
directories in the current path. So as the first argument we need to pass a string 当前路径中的目录。所以作为第一个参数我们需要传递一个字符串
that defines a search pattern. We can type an asterisk 定义搜索模式。我们可以输入星号
and that means everything, all files and all directories. 这意味着一切，所有文件和所有目录。
We can optionally add an extension, so to get all the files, you 我们可以选择添加扩展名，以便获取所有文件
need the files you use star.star. With this 需要你使用star.star的文件。有了这个
pattern, we'll only get the files in the current directory. But not the directories. 模式，我们只会获取当前目录中的文件。但不是目录。
We can also search for all the py files, or all the excel 我们还可以搜索所有py文件或所有excel
spreadsheets. Anything. So let's search for all the py files 电子表格。任何东西。所以让我们搜索所有py文件
in the current directory, now when we run our program 在当前目录中，现在我们运行我们的程序
we get this generator object. Generator object 我们得到这个生成器对象。发电机对象
are kind of an advanced topic, and they are beyond the scope of this beginners 是一个高级主题，它们超出了这个初学者的范围
python course. But for now all you need to know is we can iterate python课程。但是现在你需要知道的是我们可以迭代
or loop through these generator objects. So instead of 或循环遍历这些生成器对象。而不是
printing this generator object, let's just iterate over it using 打印这个生成器对象，让我们使用它迭代它
a for loop, so, for, file in, 一个for循环，所以，for，file in，
the generator object that is returned from the glob method. q 从glob方法返回的生成器对象。 q
Let's print file. 我们打印文件。
We run our program, there you go. So these are all the 我们运行我们的程序，你去。所以这些都是
py files in my current directory, this could be different on your machine 在我当前目录中的py文件，这可能在您的计算机上有所不同
if you didn't follow the exercises I gave you earlier. For example, this 如果你没有按照我之前给你的练习。例如，这个
utils.py, we created this as part of one of the exercises earlier in this course. utils.py，我们在本课程前面的练习中创建了这个。
So don't worry if the files you see here are different on your machine. 因此，如果您在此处看到的文件在您的计算机上有所不同，请不要担心。
So, you can use the glob method to search for files using 因此，您可以使用glob方法来搜索文件
a pattern, we can also get all the files and directories in the current 一个模式，我们也可以得到当前的所有文件和目录
path, so, we just use one asterisk, then 路径，所以，我们只使用一个星号，然后
run our program, take a look, so we have this 运行我们的程序，看看，所以我们有这个
ecommerce directory, we have notes.txt, this is a file I 电子商务目录，我们有notes.txt，这是一个文件我
added earlier just to write notes as part of my presentation so you're not going to have 之前添加的只是为了写笔记作为我的演示文稿的一部分，所以你不会有
this file. We also have a bunch of directories and files. 这个文件。我们还有一堆目录和文件。
Even though python's standard library is comprehensive and has 尽管python的标准库是全面的并且具有
so many modules for common tasks, it's not complete by any means. 如此多的模块用于常见任务，它无论如何都不完整。
That's why we have this directory called python package 这就是为什么我们有这个名为python包的目录
index. Or pypi. And in this directory you can find hundreds 指数。或者pypi。在这个目录中你可以找到数百个
of packages for doing awesome things. These are packages that people like you 用于做令人敬畏的事情的包裹。这些是人们喜欢你的包
and I have built as part of our projects, and then they have published it as part of this directory. 我已经构建了项目的一部分，然后他们将其作为此目录的一部分发布。
For other people to use. So that's why we have a fantastic community in python, 供其他人使用。这就是为什么我们在python中有一个梦幻般的社区，
there are so many python developers out there and there are so many packages 那里有很多python开发人员，而且有很多包
right in front of us to reuse in our programs, let's say 我们可以在我们面前重复使用我们的程序
you want to send text messages in your programs, you don't have to build this functionality from scratch, 如果您想在程序中发送短信，则无需从头开始构建此功能，
we can simply come to pypi.org and 我们可以简单地来到pypi.org和
search for sms aas you can see there are so many 搜索短信，你可以看到有这么多
projects or apckages that give you this functionality. Of course not every 提供此功能的项目或apckages。当然不是每一个
package, not every project is complete or bug free, some of them are still 包，不是每个项目都是完整的或没有bug，其中一些仍然是
in development or they have bugs, but if you look around I'm pretty sure 在开发中或他们有错误，但如果你环顾四周我很确定
for any kind of function you can imagine there is a good 对于任何类型的功能，你可以想象有一个好的
reliable and well documented package for you. In my complete python course, 为您提供可靠且文档齐全的软件包。在我完整的python课程中，
I will show you some of those most useful packages on pypi. For example 我将向您展示一些pypi上最有用的软件包。例如
you will learn how to access the information on yelp.com in your 您将学习如何访问yelp.com中的信息
programs. So you can write a program and search for businesses 程式。所以你可以编写一个程序并搜索业务
registered on Yelp. Let's say you want to search for the best barber 在Yelp上注册。假设你想要寻找最好的理发师
in New York City. I will show you how to do that. You will also learn how to use a technique called 在纽约市。我会告诉你如何做到这一点。您还将学习如何使用一种名为的技术
web scraping, which means you can build an engine and have that engine 网络抓取，这意味着您可以构建引擎并拥有该引擎
browse a website, and extract information from HDML files. 浏览网站，从HDML文件中提取信息。
This is the same technique that Google does to index various 这与Google为各种索引编制的技术相同
websites. So they have several engines, or web crawlers, these web crawlers 网站。因此，他们有几个引擎或网络爬虫，这些网络爬虫
are continuously looking at various websites and extracting information 不断浏览各种网站并提取信息
from their pages. When you publish a blog post Google's web crawler 从他们的页面。当您发布博客帖子Google的网络抓取工具时
finds the title of your post, its keywords and so on. So, 查找帖子的标题，关键字等。所以，
I'm going to show you how to do that with Python, we'll also look at browser automation, and this is extremely 我将向您展示如何使用Python，我们还将看看浏览器自动化，这是非常的
powerful, because you can automate testing of your web 功能强大，因为您可以自动测试您的网络
applications. So you might get a job as a test engineer, and 应用。所以你可能会得到一份测试工程师的工作
your daily job is to browse a website, click here and there, fill out this form, that form, and 你的日常工作是浏览一个网站，点击这里，然后填写这个表格，表格和
ensure that this website is functioning properly. You can automate all that 确保本网站运作正常。你可以自动化所有这些
with a powerful python package called selenium, as I have shown in my complete, as I show in my complete 有一个强大的python包叫做selenium，就像我在我的完整中所展示的那样，正如我在我的完整中所展示的那样
python course. Now in this tutorial, I'm going to show you how to install python课程。现在在本教程中，我将向您展示如何安装
packages, from pypi.org. So, 包，来自pypi.org。所以，
here on pypi.org let's search for open py 在pypi.org上，让我们搜索open py
xl. This is a package that we use for working with excel XL。这是我们用于处理excel的包
spreadsheets. So let's search for this package, there you go, so currently 电子表格。所以，让我们来搜索这个包，你去，所以目前
it's at version 2.5. On the top you can see, 它的版本是2.5。在顶部你可以看到，
this instruction pip install open pyexcel. 这条指令pip install open pyexcel。
this is the command that we need to type in the terminal window to 这是我们需要在终端窗口中键入的命令
install this package into our program, so back to pycharm, 将此软件包安装到我们的程序中，所以回到pycharm，
here on the status bar. You can open up a terminal window for executing 在状态栏上。您可以打开终端窗口进行执行
various commands. So let's open it, now if you're on Windows 各种命令。所以，让我们打开它，现在，如果你在Windows上
you're probably going to see something different instead of this dollar sign, don't worry about that 你可能会看到不同的东西，而不是这个美元符号，不要担心
just execute this command I'm going to show you. So pip 只要执行这个命令，我就会告诉你。所以pip
install open py xl 安装open py xl
Make sure to spell everything properly. So pip is the tool, 确保正确拼写一切。所以pip是工具，
that comes with a python installation, and we use it to install or uninstall 随python安装一起提供，我们用它来安装或卸载
packages registered on pypi.org. So here, 在pypi.org上注册的软件包。所以在这里，
we're using pip to install this package. open py 我们正在使用pip来安装这个软件包。打开py
xl. Enter, let's go ahead with that, it's going to take a few XL。进入，让我们继续吧，这将需要一些
seconds, oaky done, now we can import this package and it's 秒，oaky完成，现在我们可以导入这个包了
modules just like the built in modules in python, or the modules that we have 模块就像python中的内置模块，或者我们拥有的模块
in our project. Now if you're curious where this package 在我们的项目中。现在，如果你好奇这个包裹的位置
is stored on disc let me show you. So open up the project panel, 存放在光盘上让我告诉你。所以打开项目面板，
and then expand external libraries, next 接下来扩展外部库
expand python 3, and then expand sight packages. 展开python 3，然后展开视觉包。
This is where all these packages that we installed with pip are stored. 这是我们使用pip安装的所有这些软件包的存储位置。
So here we have open py xl, you can open this, 所以这里我们打开py xl，你可以打开这个，
in this folder we have another folder called cell, now 在这个文件夹中，我们现在有另一个名为cell的文件夹
Now, look what we have here you have this init 现在，看看我们在这里有你的init
type, earlier I told you that whenever we add this file to a folder, python treats 类型，早些时候我告诉过你，每当我们将这个文件添加到文件夹时，python就会对待
that folder as a package, so sell is a package, and this 那个文件夹作为一个包，所以卖是一个包，而这个
package we have is modules. Sell, interface, read only and text. 我们的包是模块。销售，界面，只读和文本。
So as you can see, this package openpyxl has several 所以你可以看到，这个包openpyxl有几个
subpackages like cell, start, chart sheet and so on. 子包如cell，start，chart sheet等。
And in each package we have several modules, in the next tutorial 在下一个教程中，我们在每个包中都有几个模块
I will show you how to work with excel files. 我将向您展示如何使用excel文件。
So we have covered all the core concepts in Python, I hope you have learned a lot. 所以我们已经涵盖了Python中的所有核心概念，我希望你已经学到了很多东西。
please support my hard work by liking this video and sharing it with others. 请支持我的辛勤工作，喜欢这个视频并与他人分享。
And be sure to subscribe to my channel for more tutorials like this. Next we're 并确保订阅我的频道以获得更多这样的教程。接下来我们是
going to work on our first python project that involves automation. 打算开始我们的第一个涉及自动化的python项目。
You're going to write a python program that can process thousands of spreadsheets 您将编写一个可以处理数千个电子表格的python程序
in under a second. Are you excited? Let's get started. 在一秒钟之内。你兴奋吗？让我们开始吧。
In this tutorial I'm going to show you something really awesome you can do with python. You're going to 在本教程中，我将向您展示使用python可以做的非常棒的事情。你要去
learn how to process spreadsheets, and this is extremely valuable 学习如何处理电子表格，这非常有价值
in situation where you have hundreds or thousands of spreadsheets that need to 在您需要数百或数千个电子表格的情况下
be updated. If you want to do this by hand it may take hours or days 得到更新。如果您想手动完成此操作，可能需要数小时或数天
weeks, or even months, you can simply build a python program in less then 几周甚至几个月，你可以简单地构建一个python程序
half an hour. And have that python program, automatically process thousands of spreadsheets 半小时。并拥有该python程序，自动处理数千个电子表格
in under one second. So, here we're going to work on this 在一秒钟内。所以，我们将在这里开展工作
excel spreadsheet. You can download this below this video. Excel电子表格。您可以在此视频下方下载此内容。
In this spreadsheet we have three columns, transaction ID, 在这个电子表格中，我们有三列，交易ID，
product ID, and press. Now don't worry about the fourth column yet. 产品ID，然后按。现在不要担心第四列了。
So in this spreadsheet we're going to record all kinds of transactions, but let's say 因此，在这个电子表格中，我们将记录各种交易，但让我们说
due to an error, it could be a human error or a system error. The price that we have 由于错误，可能是人为错误或系统错误。我们的价格
here is wrong. Let's say we need to decrease this by 10 percent. 这是错的。假设我们需要减少10％。
If you want to do this manually, we'll have to come back here and 如果你想手动完成，我们将不得不回到这里
add a formula, so we typed out equal sign, let's say you want to recalculate 添加一个公式，所以我们输入等号，让我们说你要重新计算
say you want to recalculate the value of this cell. 说你想重新计算这个细胞的价值。
have to get that and then multiply it by 0.9 必须得到它，然后乘以0.9
and that is like 90% of the original price, we are reducing the original 这就像原价的90％，我们正在减少原价
reducing the price by 10%. Now when you hit enter you can see the updated price. 将价格降低10％。现在，当您点击输入时，您可以看到更新的价格。
Next we'll have to come back here, select this cell and 接下来我们将不得不回到这里，选择这个单元格
apply the same formula to all the other rows. Now what if you have thousands of rows 将相同的公式应用于所有其他行。现在，如果你有数千行
here? You'll have to scroll up and down, this is going to be very tedious. 这里？你必须向上和向下滚动，这将是非常乏味的。
So we're going to write a python program that will do this for us. It's going to 所以我们要编写一个python程序来为我们做这件事。它会
auotmate this process and not only that it will also auotmate这个过程，不仅如此
add this beautiful chart right here, 在这里添加这个美丽的图表，
once again if you want to open up hundreds or thousands of spreadsheets 再次，如果你想打开数百或数千个电子表格
and add a chart to each one, this is going to take at least a week or two 并为每个人添加一个图表，这至少需要一到两周
Our python program is going to do all of this in a matter of seconds. 我们的python程序将在几秒钟内完成所有这些工作。
So, let's get started. Alright the first thing I want you to do is 那么，让我们开始吧。好吧，我要你做的第一件事就是
to download this transactions file and add it to your project. 下载此事务文件并将其添加到您的项目中。
I put the link below this video for you to download. So if you're wondering 我把这个视频下面的链接供您下载。所以，如果你想知道
how to put this in this project, you can simply right click the project, 如何把这个放在这个项目中，你只需右键单击该项目，
and if you're on Mac you have this item where 如果你在Mac上你有这个项目在哪里
reveal in finder, if you're on Windows you should see something like reveal 在Finder中显示，如果你在Windows上，你应该看到像揭示的东西
in explorer, or file explorer, or whatever it's called 在资源管理器，文件资源管理器或其他任何调用
when you click this, this opens up the direct 当你点击它时，这会打开直接
on your machine where this is stored. So simply copy paste 在您的计算机上存储它。所以只需复制粘贴
the transactions file right here. Now, 交易文件就在这里。现在，
here on the top, first we need to import the open py excel package. 在顶部，首先我们需要导入open py excel包。
So import open py excel, we can give it a 所以导入open py excel，我们可以给它一个
an alias to make our code shorter. So asxl. 使我们的代码更短的别名。所以asxl。
This is not required but it just makes our code a little bit cleaner. 这不是必需的，但它只是使我们的代码更清洁一点。
Let me show you. So now instead of typing open pyxl. 让我演示给你看。所以现在不要输入open pyxl。
we type xl. okay so that's an alias for 我们输入xl。好的，这是别的
this package. Now in this package we have a function for loading an xl 这个包。现在在这个包中我们有一个加载xl的函数
workbook. So let's call load workbook, 工作簿。所以让我们调用加载工作簿，
and as the argument we pass transactions. 作为参数，我们传递交易。
.xlsx. So this we load 的.xlsx。所以我们加载
our excel workbook and return a workbook object. 我们的excel工作簿并返回一个工作簿对象。
Okay? Now if you open this workbook, you can see you add only 1 好的？现在，如果您打开此工作簿，您可以看到只添加1
sheet, and that is called sheet 1, so to access that you use 工作表，这称为工作表1，以便访问您使用的工作表
square brackets, and specify the name of the sheet, 方括号，并指定工作表的名称，
that is sheet one, make sure to spell this with a capital 这是第一张，请务必用大写拼写
S. Because this is case sensitive, if you don't spell it properly S.因为这是区分大小写的，如果你没有正确拼写它
you're going to get an error, so this returns a sheet, 你会得到一个错误，所以这会返回一张表，
now in this sheet, we have various cells. So 现在在这张表中，我们有各种细胞。所以
next you need to learn how to access a particular cell, and that is very easy. 接下来，您需要学习如何访问特定单元格，这非常简单。
back to py charm, with this sheet object, you can use 回到py魅力，用这个表格对象，你可以使用
square brackets and give the coordinate of a cell. 方括号并给出单元格的坐标。
Coordinate is the combination of the column and the row. 坐标是列和行的组合。
For example, this cell it's coordinate is a1. 例如，它的坐标为1的单元格。
So back to pycharm, we can type a1, and this returns 所以回到pycharm，我们可以输入a1，然后返回
our first cell. Also, there is another approach to getting 我们的第一个细胞此外，还有另一种方法
a cell, instead of using square brackets with a string, we can 一个单元格，而不是使用方括号和字符串，我们可以
use the cell method of the sheet object, so, 使用工作表对象的单元格方法，所以，
sheet.cell, you pass the row and the column, so row sheet.cell，你传递行和列，所以行
and column 1 these return the exact same 和第1列这些返回完全相同
cell. Now let's print the value of this cell, cell.value 细胞。现在让我们打印这个单元格的值cell.value
and run our program so 然后运行我们的程序
the value is transaction id, beautiful, 价值是交易ID，很漂亮，
so now we need to iterate over all these rows and for each row we need to get the value 所以现在我们需要迭代所有这些行，并且每行需要获取值
in the third column. That is the price. We'll get that and then multiply 在第三栏。这是价格。我们会得到它然后成倍增加
by 0.9. So first we need to know how many rows 0.9。首先，我们需要知道有多少行
we have in this spreadsheet. We can get that from the sheet object so sheet 我们在这个电子表格中。我们可以从表单对象中获取它
has an attribute called mx_ 有一个名为mx_的属性
row. Let's print this, so in this sheet, 行。我们打印这个，所以在这张表中，
we have 4 row, and you can verify that here. So we have 我们有4行，你可以在这里验证。所以我们有
a total of 4 rows. So we need to add a for loop. 共4行。所以我们需要添加一个for循环。
that would generate the numbers 1-4. So, 这会产生数字1-4。所以，
let's delete this print statement, nd add a for loop 让我们删除这个print语句，并添加一个for循环
for row in were going to use the range function unction to generate a range of numbers, for row将使用范围函数unction生成一系列数字，
starting from 1, all the way to sheet 从1开始，一直到表格
.max_grow. Plus 1. The reason we're .max_grow。加1.我们之所以如此
adding 1 to this, is because as I told you before this range function 为此添加1，是因为正如我在此范围函数之前告诉你的那样
will generate numbers, starting from this value all the way to this 将生成数字，从此值开始一直到此
value, but it will not include the second value. So currently 值，但不包括第二个值。所以目前
max row returns 4, if you use range of 1-4 this will generate max row返回4，如果使用1-4的范围，则会生成
the numbers 1, 2, and 3, but not 4. 数字1,2和3，但不是4。
So to include 4, we need to add 1 to it. Let's go over here, 所以要包括4，我们需要添加1。我们过来，
so we tied sheet.max underline row plus 1. 所以我们将sheet.max下划线加1加1。
Now before going any further let's just print row and make 现在再进一步让我们打印行和制作
sure our program is working up to this point. So let's run 确定我们的计划正在努力到这一步。让我们跑吧
this, we get the numbers 1-4 beautiful. 这个，我们得到数字1-4美丽。
So now we can easily get the cells in the third column. However 所以现在我们可以轻松地将细胞放入第三列。然而
we don't really want this first cell, that's the heading. 我们真的不想要第一个单元格，那就是标题。
so technically you want to ignore the first row. Back here, you want to change 所以从技术上讲，你想忽略第一行。回到这里，你想改变
our range function, and start from 2. Now, 我们的范围功能，从2开始。现在，
we're going to use sheet.cell to get access to the cell 我们将使用sheet.cell来访问单元格
at this row. And the column should be 3. 在这一排。列应为3。
We get a cell object, let's print it's value 我们得到一个单元格对象，让我们打印出它的价值
and make sure you're on the right track, so run the program 并确保你在正确的轨道上，所以运行该程序
these are the values we get. 5.95 这些是我们得到的价值。 5.95
6.95 and 7.95 these are the 这些是6.95和7.95
values of the third column. Next we need to multiply each 第三列的值。接下来我们需要将每个相乘
value by 0.9 so instead of printing 值为0.9而不是打印
cell.value we multiplied by 0.9 and this is cell.value我们乘以0.9，这是
the corrected underline price, 更正的下划线价格，
so this is the mistake we're going to make with our program, now 所以这就是我们现在要用我们的程序做的错误
we need to add a new cell to our worksheet, so we want to add all the corrected 我们需要在工作表中添加一个新单元格，因此我们要添加所有已更正的单元格
prices, ia new column, it could also overwrite the values in this column, 价格，新列，它也可以覆盖此列中的值，
but in this tutorial I'm going to show you how to add a new column. 但在本教程中，我将向您展示如何添加新列。
So using the same method, now we need to get a reference to the cell, to the given 因此，使用相同的方法，现在我们需要获得对给定的单元格的引用
row, but in the fourth column. So, we call sheet 行，但在第四列。所以，我们称之为表格
.cell, and pass row and four as the arguments. .cell，并传递row和four作为参数。
Once again, this returns a cell object, let's store that in a 再次，这将返回一个单元格对象，让我们将其存储在一个
separate variable and call it 单独的变量并调用它
price_cell. So note that my variable names are price_cell。请注意我的变量名称是
very descriptive. There is no guess work. You have corrected price which holds 非常具有描述性。没有猜测工作。你已经纠正了价格
actual value and we have corrected price, which is a cell 实际值，我们已经修正了价格，这是一个单元格
object in this spreadsheet. Next we have to set the value in this 此电子表格中的对象。接下来我们必须在此设置值
cell. So we set corrected price cell. 细胞。所以我们设置了更正价格单元。
value to this corrected price. With these few lines 这个修正价格的价值。有了这几行
we can easily update our spreadsheet, but first we need to save it. So, 我们可以轻松更新电子表格，但首先我们需要保存它。所以，
after our for loop, we call workbook. 在我们的for循环之后，我们调用工作簿。
save. Now let's save this in a new file because we don't want to 保存。现在让我们将它保存在一个新文件中，因为我们不想这样做
accidentally overwrite the original file in case our program has a bug 如果我们的程序有错误，请不小心覆盖原始文件
so, I'm going to call that transactions 2.sms 所以，我打算称那些交易2.sms
ex. And finally let's run our program, 恩。最后让我们运行我们的程序，
so, you can see that happened in a split of a second, now here we have a new file 所以，你可以看到发生在一秒钟的分裂中，现在我们有了一个新文件
tranactions 2. When we open this we get something like this. 交易2.当我们打开这个时，我们得到这样的东西。
So you can see we have this 4th column with the updated prices. Beautiful. 所以你可以看到我们的第4栏有更新的价格。美丽。
So half of the problem is solved, now we need to add a chart here. 所以问题的一半已经解决，现在我们需要在这里添加一个图表。
To add a chart, we need to add a couple classes on the top, so, 要添加图表，我们需要在顶部添加几个类，因此，
after this import statement let's type out from 在这个import语句之后让我们输入
open py excel.chart, import, bar chart, 打开py excel.chart，导入，条形图，
comma, reference. 逗号，参考。
So lets see what's going on here. In this package you have a module 所以让我们看看这里发生了什么。在这个包中你有一个模块
chart, and from this module we're importing two classes, bar chart, 图表，从这个模块我们导入两个类，条形图，
and reference. Again, look at the naming convention used 和参考。再看一下使用的命名约定
here. The first letter of e very word is capitalized. Also it's better 这里。 e字的第一个字母是大写的。它也更好
to add a line break after our import statements to make our 在我们的import语句后添加换行符来制作我们的
code cleaner. So, before we save our workbook, we need to 代码清理。因此，在我们保存工作簿之前，我们需要
add achart to the current sheet. First we need to select a range 将achart添加到当前工作表。首先，我们需要选择一个范围
of values. For this exercise, I'm going to select the values in the fourth column/. 价值观在本练习中，我将选择第四列中的值。
So all the values in rows 2-4 these are 因此，行2-4中的所有值都是
the values I'm going to use in our chart. Now in this part 2 example it doesn't really 我要在图表中使用的值。现在在这个第2部分的例子中，它并不是真的
make sense to add a chart per transaction, but that doesn't really matter 为每个交易添加一个图表是有意义的，但这并不重要
we just want a bunch of numbers to create a chart, so after our 我们只想要一堆数字来创建图表，所以在我们之后
for loop, we're going to use the reference class to select a range of for循环，我们将使用引用类来选择一个范围
values. Now the first argument in the constructor is the 值。现在构造函数中的第一个参数是
sheet. So we pass our sheet here, then we add 片。所以我们在这里传递我们的表格，然后我们添加
4 keyword arguments, the first one is min_row, we set this to 4个关键字参数，第一个是min_row，我们将其设置为
2. The second one is max_row we set 2.第二个是我们设置的max_row
this to the maximum row in this sheet. That is sheet, 这到此表中的最大行。那是表，
.max_row, so we want to select the cells in row .max_row，所以我们要选择行中的单元格
2-4. kay? Now to make this code a little bit cleaner 2-4。凯？现在让这段代码更清洁一点
I'm going to put this on a new line, so we 我打算把它换成一条新线，所以我们
can see clearly, now this is going to select all the cells 可以清楚地看到，现在这将选择所有细胞
in all the columns in these rows. That's not what we want. 在这些行的所有列中。那不是我们想要的。
we only want the values in the fourth column, so back here 我们只想要第四列中的值，所以回到这里
we need to set a couple more key word arguments men_call 我们需要设置几个关键词参数men_call
or column, we set that to for, and also 或列，我们将其设置为for，以及
max underline call, we set that to 4 as well. So we are limiting the range 最大下划线调用，我们也将其设置为4。所以我们限制范围
of cells we're selecting to the fourth column. 我们正在选择第四列的细胞。
Now, we're creating an instance of the reference class, let's store the 现在，我们正在创建一个引用类的实例，让我们存储它
result in a variable called values, so this values object 导致一个名为values的变量，所以这个值是对象
will have all these values in the fourth column, now we are ready to create 将在第四列中包含所有这些值，现在我们已准备好创建
a chart, so we create an instance of the bar chart class 一个图表，所以我们创建一个条形图类的实例
and store it in this object. 并将其存储在此对象中。
Next, we call chart.add_date and 接下来，我们调用chart.add_date和
pass our values. That is pretty straight forward finally we need to add this chart 传递我们的价值观这很简单，最后我们需要添加这个图表
to our sheet. So, we call sheet, 到我们的表。所以，我们称之为表格，
.add_chart and pass this chart object .add_chart并传递此图表对象
We should also specify where we want to add this chart. 我们还应该指定我们要添加此图表的位置。
let's say we want to add it here, on row 2, after the fourth column. 假设我们想在第四列之后的第2行添加它。
So the coordinate of this cell is e2, and this is going to be, 所以这个单元格的坐标是e2，这将是，
the top left corner of our chart. So as the coordinate I'm going to 我们图表的左上角。所以我要去的坐标
pass e2 that's all we had to do, done. Let's run our 通过e2这就是我们所要做的一切，完成了。让我们来吧
program one more time, beautiful, let's open up the update 程序再一次，美丽，让我们打开更新
workbook. And here's the end result, so 工作簿。这是最终结果，所以
right after the fourth column, we have this beautiful chart. Now we could take this to the next level, 在第四栏之后，我们有了这张漂亮的图表。现在我们可以把它提升到一个新的水平，
we could add a legend here, we could change the color of these bars, we could even use a different 我们可以在这里添加一个图例，我们可以改变这些条的颜色，我们甚至可以使用不同的颜色
kind of chart, it doesn't have to be a bar chart. So to learn more about this we can 有点图表，它不一定是条形图。所以我们可以了解更多相关信息
read the documentation for openpyxl. So 阅读openpyxl的文档。所以
our program is complete, for our code is dirty, it's not clean, it's not 我们的程序是完整的，因为我们的代码很脏，它不干净，不是
organized. We don't have any functions here. We have written all the code in app.py. 举办。我们这里没有任何功能。我们在app.py中编写了所有代码。
So let's goo ahead and organize this code like a professional software developer. 因此，让我们继续前进，像专业的软件开发人员一样组织这些代码。
So, on line 4, we are loading our workbook So, on line 4, we are loading our workbook
then we're getting a reference to the first sheet, but we don't really need these two lines here then we're getting a reference to the first sheet, but we don't really need these two lines here
this is purely for demonstration to show you how to access a cell. So, this is purely for demonstration to show you how to access a cell.所以，
let's delete these unnecessary lines, this is a very important practice let's delete these unnecessary lines, this is a very important practice
as you're coding always look at your code, always review it, see if you have some code that is as you're coding always look at your code, always review it, see if you have some code that is
not used. Always delete those. Okay, next we are iterating over not used. Always delete those. Okay, next we are iterating over
all the rows, fixing the prices, and then all the rows, fixing the prices, and then
we select the values to add a chart, and finally we save the workbook. we select the values to add a chart, and finally we save the workbook.
Now, if you were going to use this to automate the process of updating Now, if you were going to use this to automate the process of updating
thousands of spreadsheets, this program wouldn't work, because it's only thousands of spreadsheets, this program wouldn't work, because it's only
relying on this file. So we want o reorganize this code, and relying on this file. So we want o reorganize this code, and
move it inside of a function. This function should take the name of a function move it inside of a function. This function should take the name of a function
So let's define a function. Let's So let's define a function.让我们
call it process_workbook. call it process_workbook.
It gets a file name. Now, we move all the code inside It gets a file name. Now, we move all the code inside
of this function, so let's select everything, and press of this function, so let's select everything, and press
tab, beautiful, now back tab, beautiful, now back
to our function, instead of loading this transactions file we're going to load to our function, instead of loading this transactions file we're going to load
this file name, that's better, this file name, that's better,
Also, now that our program is working properly, there is really no need to store the Also, now that our program is working properly, there is really no need to store the
result in a separate file. So here we can result in a separate file. So here we can
simply overwrite the same file. So let's pass simply overwrite the same file. So let's pass
file name, that is the end of our function, so, file name, that is the end of our function, so,
now, we have this reusable function, we can simply reuse this now, we have this reusable function, we can simply reuse this
to process thousands of spreadsheets. Earlier in this python course you learned to process thousands of spreadsheets. Earlier in this python course you learned
how to find all the files in a directory, so we can get each file in a directory how to find all the files in a directory, so we can get each file in a directory
and pass the name of the file to this function, this function will go and pass the name of the file to this function, this function will go
ahead and update this spreadsheet in just a second or less. ahead and update this spreadsheet in just a second or less.
So, that was just one example of using python to automate So, that was just one example of using python to automate
repetitive boring task that waste your time. But automation is not repetitive boring task that waste your time. But automation is not
just about processing excel spreadsheets, there are so many things we can automate. just about processing excel spreadsheets, there are so many things we can automate.
Here's a question for you, what do you want to automate, how do you want to use python to make your Here's a question for you, what do you want to automate, how do you want to use python to make your
life easier? Use the comment box below and let me know. I love to life easier? Use the comment box below and let me know.我爱
hear about your ideas. Next we're going to work on project which involves machine learning, which is a hear about your ideas. Next we're going to work on project which involves machine learning, which is a
subset of artificial intelligence. Are you excited? subset of artificial intelligence. Are you excited?
Let's get started. 让我们开始吧。
In this section, you're going to learn about machine learning, which is a subset of In this section, you're going to learn about machine learning, which is a subset of
AI or artificial intelligence. It's one of the trending topics in the world, AI or artificial intelligence. It's one of the trending topics in the world,
these days, and it's going to have a lot of applications in the future. Here's an example. these days, and it's going to have a lot of applications in the future.这是一个例子。
Imagine I ask you to write a program to scan an image, and Imagine I ask you to write a program to scan an image, and
tell if it's a cat or a dog. If you want to build this program using tell if it's a cat or a dog. If you want to build this program using
traditional programming techniques, your program is going to get overly complex traditional programming techniques, your program is going to get overly complex
You will have to come up with lots of rules to come up with specific curves You will have to come up with lots of rules to come up with specific curves
edges and colors in an image to tell if it's a cat or a dog. edges and colors in an image to tell if it's a cat or a dog.
But if I give you a black and white photo, your rules may not work, they may break. But if I give you a black and white photo, your rules may not work, they may break.
Then you would have to rewrite them. Or I may give you a picture of a cat or a dog from a different Then you would have to rewrite them. Or I may give you a picture of a cat or a dog from a different
angle that we did not predict before. So solving this problem angle that we did not predict before. So solving this problem
using traditional programming techniques is going to get overly complex or using traditional programming techniques is going to get overly complex or
sometimes impossible. Now to make the matter worse. What if I ask you to extend sometimes impossible. Now to make the matter worse. What if I ask you to extend
this program such that it supports 3 kinds of animals. this program such that it supports 3 kinds of animals.
Cats, dogs, and horses, once again you will have to rewrite Cats, dogs, and horses, once again you will have to rewrite
all those rules. That's not going to work. So machine learning is a technique all those rules. That's not going to work. So machine learning is a technique
to solve these kinds of problems, and this is how it works. We build to solve these kinds of problems, and this is how it works.我们建立
a model or an engine and give it lots and lots of data. a model or an engine and give it lots and lots of data.
For example, we give it thousands or tens of thousands of pictures of For example, we give it thousands or tens of thousands of pictures of
cats and dogs. Our model will then find and learn patterns and the input cats and dogs. Our model will then find and learn patterns and the input
data, so we can give it a new picture of a cat that we haven't seen before. data, so we can give it a new picture of a cat that we haven't seen before.
And ask it, is it a cat or a dog or a horse And ask it, is it a cat or a dog or a horse
and it will tell us with a certain level of accuracy, the more input data we give it, the more accurate and it will tell us with a certain level of accuracy, the more input data we give it, the more accurate
our model is going to be. So that was a very basic example, our model is going to be. So that was a very basic example,
but machine learning has other applications in self driving cars, but machine learning has other applications in self driving cars,
robotics, language processing, vision processing, robotics, language processing, vision processing,
forecasting things like stock market trends and the weather, games and so on. forecasting things like stock market trends and the weather, games and so on.
So that's the basic idea about machine learning. Next we'll look at machine So that's the basic idea about machine learning. Next we'll look at machine
learning in action. learning in action.
A machine learning project involves A machine learning project involves
a number of steps, the first step is to import our data which a number of steps, the first step is to import our data which
often comes in the form of a csv file. You might have a database with lots of data, often comes in the form of a csv file. You might have a database with lots of data,
we can simply export that data and store it in a csv file for the we can simply export that data and store it in a csv file for the
purpose of our machine learning project. So we import our data, next, purpose of our machine learning project. So we import our data, next,
we need to clean it. And this involves tasks such as duplicated data. we need to clean it. And this involves tasks such as duplicated data.
If you have duplicates in the data, we don't want to feed this to the model, because If you have duplicates in the data, we don't want to feed this to the model, because
otherwise our model will learn bad patterns in our data and will produce the wrong result, otherwise our model will learn bad patterns in our data and will produce the wrong result,
so we should make sure that our input data is in a good, and clean shape. so we should make sure that our input data is in a good, and clean shape.
If there is data that is irrelevant we should remove them, if there are duplicates If there is data that is irrelevant we should remove them, if there are duplicates
or incomplete we can remove or modify them, if our data is or incomplete we can remove or modify them, if our data is
text based, like the name of countries, or genres of music, text based, like the name of countries, or genres of music,
or cats and dogs, we need to convert them to numerical values. or cats and dogs, we need to convert them to numerical values.
So this step really depends on the kinds of data we're working with, every project So this step really depends on the kinds of data we're working with, every project
is different. Now that we have a clean data set, we need to split it 是不同的。 Now that we have a clean data set, we need to split it
into two segments. One for training our model, and the other for into two segments. One for training our model, and the other for
testing it, to make sure our model produces the right result. testing it, to make sure our model produces the right result.
For example, if you have 1,000 pictures of cats and dogs, we can reserve For example, if you have 1,000 pictures of cats and dogs, we can reserve
80% for training, and another 20% for testing. 80% for training, and another 20% for testing.
The next step is to create a model, and this involves selecting The next step is to create a model, and this involves selecting
an algorithm to analyze the data. There are so many machine learning algorithms an algorithm to analyze the data. There are so many machine learning algorithms
out there, such as decision trees, neural networks and so on. out there, such as decision trees, neural networks and so on.
Each algorithm have pros and cons in terms of accuracy and performance Each algorithm have pros and cons in terms of accuracy and performance
so the algorithm you use, depends on the kind of problem so the algorithm you use, depends on the kind of problem
you're trying to solve and your input data. Now the good news isnthat we don't have to you're trying to solve and your input data. Now the good news isnthat we don't have to
explicitly program an algorithm, there are libraries out there that provide these explicitly program an algorithm, there are libraries out there that provide these
algorithms, one of the most popular ones, which we are going to look at algorithms, one of the most popular ones, which we are going to look at
in this tutorial, is side kick learn. So we build a model using an algorithm , in this tutorial, is side kick learn. So we build a model using an algorithm ,
next we need to train our model. So we feed it our training data. next we need to train our model. So we feed it our training data.
Our model will then look for the patterns in the data, so enxt Our model will then look for the patterns in the data, so enxt
we can ask it to make predictions. Back to our examples of cats and dogs, we can ask it to make predictions. Back to our examples of cats and dogs,
we can ask our model is this a cat or is this a dog and our model we can ask our model is this a cat or is this a dog and our model
will make a prediction, now the prediction is not always accurate will make a prediction, now the prediction is not always accurate
in fact when you start out, it's very likely your predictions are inaccurate. in fact when you start out, it's very likely your predictions are inaccurate.
So we need to envalue the predictions and measure their accuracy. So we need to envalue the predictions and measure their accuracy.
Then we need to get back to our model and either select a different algorithm Then we need to get back to our model and either select a different algorithm
that is going to produce a more accurate result for the kind of problem that is going to produce a more accurate result for the kind of problem
we're trying to solve. Or fine tune the parameters of our model. 我们正在努力解决。 Or fine tune the parameters of our model.
So each algorithm has parameters that we can modify to optimize the So each algorithm has parameters that we can modify to optimize the
accuracy. So these are the high level steps that you follow in a machine learning 准确性。 So these are the high level steps that you follow in a machine learning
project. Next we'll look at the libraries and tools for machine learning. 项目。 Next we'll look at the libraries and tools for machine learning.
In this lecture we're going too look at the popular python library In this lecture we're going too look at the popular python library
that we use in machine learning projects. The first one is numpy which that we use in machine learning projects. The first one is numpy which
provides a multidimensional array. A very, very, popular library. provides a multidimensional array. A very, very, popular library.
The seconnd one is pandas, which is a data analysis library that The seconnd one is pandas, which is a data analysis library that
provides a concept called data frame. Data frame is a two dimensional provides a concept called data frame. Data frame is a two dimensional
data structure similar to an excel spreadsheet. So we have rows and columns data structure similar to an excel spreadsheet. So we have rows and columns
we can select columns in a row or a column or a range of rows and we can select columns in a row or a column or a range of rows and
columns, again, very very popular in machine learning columns, again, very very popular in machine learning
and data science projects. The third library is mad plot lib which and data science projects. The third library is mad plot lib which
is a two dimensional plotting library for creating graphs on plots. is a two dimensional plotting library for creating graphs on plots.
The next library is sidekick learn, which is one of the most popular machine learning The next library is sidekick learn, which is one of the most popular machine learning
libraries that provides all these common algorithms like decision trees, libraries that provides all these common algorithms like decision trees,
neural networks and so on. Now, when working with machine learning neural networks and so on. Now, when working with machine learning
projects, we use an environment called jupiter for writing our code, technically we can still projects, we use an environment called jupiter for writing our code, technically we can still
use vs code or any other code editor, but these editors use vs code or any other code editor, but these editors
are not ideal for machine learning projects, because we need to frequently are not ideal for machine learning projects, because we need to frequently
inspect the data, and that is really hard in environments like vs code and inspect the data, and that is really hard in environments like vs code and
terminal. If you're working with a table of 10 or 20 columns terminal. If you're working with a table of 10 or 20 columns
visualizing this data in a terminal window is really, really difficult and messy, so thats why we use jupiter. visualizing this data in a terminal window is really, really difficult and messy, so thats why we use jupiter.
So that's why we use jupiter, it makes it really easy to inspect our data. So that's why we use jupiter, it makes it really easy to inspect our data.
Now to install jupiter, we are going to use platform called anaconda. Now to install jupiter, we are going to use platform called anaconda.
So, head over to Anaconda.com/download. So, head over to Anaconda.com/download.
On this page you can download Anaconda distribution for your operating system, On this page you can download Anaconda distribution for your operating system,
so, we have distributions for Windows Mac and so, we have distributions for Windows Mac and
Linux. So let's go ahead and install Linux. So let's go ahead and install
Anaconda for python 3.7. Download, Anaconda for python 3.7. Download,
Alright, so 好吧，好吧
here's Anaconda, download it on my machine, let's double click this. here's Anaconda, download it on my machine, let's double click this.
Alright, first it's going to run a program to determine if the software can be installed. Alright, first it's going to run a program to determine if the software can be installed.
So, let's continue and once again continue, So, let's continue and once again continue,
pretty easy, continue one more time, pretty easy, continue one more time,
I agree with the license agreement, we can use the default I agree with the license agreement, we can use the default
you ca use the default installation location so don't worry about that, just click install, you ca use the default installation location so don't worry about that, just click install,
give it a few second, now the beautiful thing about Anaconda is it will install give it a few second, now the beautiful thing about Anaconda is it will install
jupiter, as well as all those data popular science libraries jupiter, as well as all those data popular science libraries
like numpy, Pandas and so on. So we don't have to manually install this like numpy, Pandas and so on. So we don't have to manually install this
using pip. Alright now as part of using pip. Alright now as part of
the next step Anaconda is suggesting to install Microsoft vs code. We the next step Anaconda is suggesting to install Microsoft vs code.我们
already have this on our machine so we don't have to install it we can go with continue already have this on our machine so we don't have to install it we can go with continue
and close the installation, now finally we can move the and close the installation, now finally we can move the
this to trash because we don't need this installer in the future. this to trash because we don't need this installer in the future.
Alright, now open upa terminal window and type Alright, now open upa terminal window and type
jupiter, with a y, space, notebook. jupiter, with ay, space, notebook.
This will start the notebook server on your machine. So enter This will start the notebook server on your machine. So enter
there you go. This will start the notebook server 你去吧This will start the notebook server
on your machine, we can see these default messages here, don't worry about them, now on your machine, we can see these default messages here, don't worry about them, now
it automatically opens a browser window, pointing to local host it automatically opens a browser window, pointing to local host
port 888. This is what we call port 888. This is what we call
jupiter dashboard. On this dashboard we have a few tabs, the first tab jupiter dashboard. On this dashboard we have a few tabs, the first tab
is the files tab, and by default, this points to your home directory. is the files tab, and by default, this points to your home directory.
So every user on your machine has a home directory, this its my home directory So every user on your machine has a home directory, this its my home directory
on Mac, you can see here we have a desktop folder as well as documents, on Mac, you can see here we have a desktop folder as well as documents,
downloads and so on. On your machine you're going to see different folders, so somewhere downloads and so on. On your machine you're going to see different folders, so somewhere
so somewhere on your machine you need to create a jupiter notebook. I'm going to go to desktop so somewhere on your machine you need to create a jupiter notebook. I'm going to go to desktop
here's my desktop I don't have anything here, and then here's my desktop I don't have anything here, and then
click new, I want to create a notebook for python click new, I want to create a notebook for python
3. In this notebook we can write python code, and execute it line by line. 3. In this notebook we can write python code, and execute it line by line.
We can easily visualize our data as you can see over the next few videos. So, We can easily visualize our data as you can see over the next few videos.所以，
let's go ahead with this, let's go ahead with this,
alright, here's our first notebook, you can see by default it's called alright, here's our first notebook, you can see by default it's called
untitled, let's change that to Hello World so untitled, let's change that to Hello World so
this is going to be the hello world of our machine learning project. Let's this is going to be the hello world of our machine learning project.让我们
rename this now if you look at your desktop you can see this file rename this now if you look at your desktop you can see this file
hello world.ipynb. This is a jupiter hello world.ipynb. This is a jupiter
notebook. It's kind of similar to our py files where we write our 笔记本。 It's kind of similar to our py files where we write our
python code, but it includes additional data that jupiter uses to execute our python code, but it includes additional data that jupiter uses to execute our
code. So back to our notebook, let's 码。 So back to our notebook, let's
do a print hello world. do a print hello world.
And then, click this run button here And then, click this run button here
and here's the result printed in jupiter, so we don't have to and here's the result printed in jupiter, so we don't have to
navigate back and forth between the terminal window we can see all the result navigate back and forth between the terminal window we can see all the result
right here. Next I'm going to show you how to load a data set from a 就在这儿。 Next I'm going to show you how to load a data set from a
csv file in jupiter. csv file in jupiter.
Alright, in this lecture we're going to download a data set from a very popular website called Alright, in this lecture we're going to download a data set from a very popular website called
caggle.com. Caggle is basically a place to do data science projects. caggle.com. Caggle is basically a place to do data science projects.
So the first thing you need to do is to create an account, you can sign up with Facebook, So the first thing you need to do is to create an account, you can sign up with Facebook,
Google, or using a custom email and password, once you sign up then come back Google, or using a custom email and password, once you sign up then come back
here, on caggle.com, Here one the search bar here, on caggle.com, Here one the search bar
search for video game sales. search for video game sales.
This is the name of a very popular data set that we're going to use in this lecture. So, This is the name of a very popular data set that we're going to use in this lecture.所以，
here in this list you can see the first item with this kind of reddish here in this list you can see the first item with this kind of reddish
icon, so, let's go with that, as you can see this data icon, so, let's go with that, as you can see this data
set includes the sales data for more then 16,000 videos set includes the sales data for more then 16,000 videos
games. On this page you can see the description of various 游戏。 On this page you can see the description of various
columns in this data set, we have rank, name, platform, columns in this data set, we have rank, name, platform,
year, and so on, so here's our data source, it's a csv file year, and so on, so here's our data source, it's a csv file
called vg sales.csv, as you can see there are over 16,000 called vg sales.csv, as you can see there are over 16,000
rows, and 11 columns in this data set. rows, and 11 columns in this data set.
Right below that you can see the first few records of this data set. Right below that you can see the first few records of this data set.
So, here's our first record, the ranking for this game is 1, it's the wi sport So, here's our first record, the ranking for this game is 1, it's the wi sport
game for wii as the platform and it was released in the year 2006, game for wii as the platform and it was released in the year 2006,
now, what I want you to do is go ahead and now, what I want you to do is go ahead and
download the data set, and as I told you before you need to sign in download the data set, and as I told you before you need to sign in
before you can download this. So this will give you a zip file as you can see here, before you can download this. So this will give you a zip file as you can see here,
here's our csv file, now I want you to here's our csv file, now I want you to
put this right next to your jupiter notebook, on my machine that is on my desktop put this right next to your jupiter notebook, on my machine that is on my desktop
so I'm going to drag and drop this onto the desktop folder. so I'm going to drag and drop this onto the desktop folder.
Now, if you look at the desktop you can see here's my Now, if you look at the desktop you can see here's my
jupiter, hello world notebook, and right next to that we have jupiter, hello world notebook, and right next to that we have
vgsales.csv. With that, we go back vgsales.csv. With that, we go back
to our jupiter notebook, let's remove the first line and instead to our jupiter notebook, let's remove the first line and instead
import pandas import pandas
as pd. With his we're importing pandas module as pd. With his we're importing pandas module
and renaming it to pd, so we don't have to type pandas. several times in and renaming it to pd, so we don't have to type pandas. several times in
this code. Now let's type pd. this code. Now let's type pd.
read_csv. And pass the read_csv. And pass the
name of our csv file. That is vgsales. name of our csv file. That is vgsales.
csv. Now because this csv file, is in the current folder right next to our jupiter notebook, csv. Now because this csv file, is in the current folder right next to our jupiter notebook,
we can easily load it, otherwise we have to supply the full path we can easily load it, otherwise we have to supply the full path
to this file. So, this returns a to this file. So, this returns a
data frame object which is like an excel spreadsheet. Let me show you. data frame object which is like an excel spreadsheet.让我演示给你看。
So we store it here, and then we can So we store it here, and then we can
simply type df to inspect it. So one more time let's run this program simply type df to inspect it. So one more time let's run this program
here's our data frame with these rows and these columns so we have here's our data frame with these rows and these columns so we have
frank name platform and so on. Now this data frame object frank name platform and so on. Now this data frame object
has lots of attributes and methods that we're not going to cover in this tutorial, has lots of attributes and methods that we're not going to cover in this tutorial,
that's really beyond the scope of what we're going to do, so I'm going to leave it up to you to that's really beyond the scope of what we're going to do, so I'm going to leave it up to you to
read pandas documentation or follow other tutorials to find out about pandas data frames. read pandas documentation or follow other tutorials to find out about pandas data frames.
But in this lecture, I'm going to show you some of the most useful methods and attributes. But in this lecture, I'm going to show you some of the most useful methods and attributes.
The first one is shape, so shape The first one is shape, so shape
let's run this one more time, so here's the shape of this data set, we have over let's run this one more time, so here's the shape of this data set, we have over
16,000 records and 11 columns. 16,000 records and 11 columns.
Technically this is a 2 dimensional array of 16,000 Technically this is a 2 dimensional array of 16,000
and 11, okay? Now, we can see here we have another and 11, okay? Now, we can see here we have another
segment for writing code. SO we don't have to write all the code in the first segment segment for writing code. SO we don't have to write all the code in the first segment
so here in the second segment we can call one of the methods of the dataframe, so here in the second segment we can call one of the methods of the dataframe,
that is df.describe. that is df.describe.
Now when we run this program, we can see the Now when we run this program, we can see the
output for each segment right next to it. So here's our first output for each segment right next to it. So here's our first
segment, here we have these three lines, and this is the output of segment, here we have these three lines, and this is the output of
the last line. Below that we have our second segment, here we're calling the last line. Below that we have our second segment, here we're calling
the describe method, and right below that we have the output of the describe method, and right below that we have the output of
this segment So this is the beauty of jupiter, you can easily visualize this segment So this is the beauty of jupiter, you can easily visualize
our datsa, doing this with vscode in windows is really tedious and our datsa, doing this with vscode in windows is really tedious and
clunky. So what is the describe method returning? Basically it's clunky. So what is the describe method returning? Basically it's
returning some basic information about each column in this data set, So, returning some basic information about each column in this data set, So,
as you saw earlier we have columns like rank, year and so as you saw earlier we have columns like rank, year and so
on. These are the columns with numerical values. Now for each column we have 上。 These are the columns with numerical values. Now for each column we have
the count which is the number of records in that column, you can se the count which is the number of records in that column, you can se
our rank column has 16,5 our rank column has 16,5
-98 records whereas the year column has 16,3- -98 records whereas the year column has 16,3-
-27 records.. So this shows that some of our records don't -27 records.. So this shows that some of our records don't
have the value for the year column. We have null values. have the value for the year column. We have null values.
So in a real data science or machine learning project we'll have to use some So in a real data science or machine learning project we'll have to use some
techniques to clean up our data set. One option is to remove the records techniques to clean up our data set. One option is to remove the records
that don't have a value for the year column. Or we can assign them a default value. that don't have a value for the year column. Or we can assign them a default value.
That really depends on the project. Now another attribute for each column That really depends on the project. Now another attribute for each column
is mean so this is the average of all the values, now is mean so this is the average of all the values, now
in the case of the rank column, this rank doesn't really matter, but look at the year. in the case of the rank column, this rank doesn't really matter, but look at the year.
So the average year year for all these video games in our dataset is So the average year year for all these video games in our dataset is
2006. And this might be important in the problem we're trying to solve. 2006. And this might be important in the problem we're trying to solve.
We also have standard deviation, which is a We also have standard deviation, which is a
measure to quanitfy the amount of variation in our set of values, below that we have measure to quanitfy the amount of variation in our set of values, below that we have
min, as an example the minimum value for the year column min, as an example the minimum value for the year column
is 1980. So quite often when we work with a new data set, is 1980. So quite often when we work with a new data set,
we call the describe method to get some basic statistics about we call the describe method to get some basic statistics about
our data. Let me show you another useful attribute. our data. Let me show you another useful attribute.
So, in the next segment, let's type df.values. So, in the next segment, let's type df.values.
Let's run this, as you can see this returns Let's run this, as you can see this returns
a two dimensional array, this square bracket indicates the outer array a two dimensional array, this square bracket indicates the outer array
and a second one represents an inner array. and a second one represents an inner array.
So the first element inn our outer array, is an So the first element inn our outer array, is an
array itself, these are the values in this array, which basically array itself, these are the values in this array, which basically
represent the first row in our data set. So the video game we ranking represent the first row in our data set. So the video game we ranking
1, which is called wii sports. So this was a basic 1, which is called wii sports. So this was a basic
overview of pandas data frames, in the next lecture I'm going to show you overview of pandas data frames, in the next lecture I'm going to show you
some of the useful shortcuts of jupiter. some of the useful shortcuts of jupiter.
In this lecture I'm going to show to you some of the most useful shortcuts in Jupiter, In this lecture I'm going to show to you some of the most useful shortcuts in Jupiter,
Now the first thing I want you to pay attention to is this green bar on the left. Now the first thing I want you to pay attention to is this green bar on the left.
This indicates that this cell is currently in the edit mode, so we can This indicates that this cell is currently in the edit mode, so we can
write code here. Now, if we press the write code here. Now, if we press the
escape key, green turns to blue, and that means this cell is escape key, green turns to blue, and that means this cell is
currently in the command mode. So basically the activated cell can either currently in the command mode. So basically the activated cell can either
be in the edit mode or command mode. Depending be in the edit mode or command mode.根据
on the mode, we have different shortcuts, so here we're in the command mode. on the mode, we have different shortcuts, so here we're in the command mode.
If we press h, we can see the list of all the If we press h, we can see the list of all the
keybord shortcuts, right above this list we can keybord shortcuts, right above this list we can
see, Mac OS modifier keys, these are the see, Mac OS modifier keys, these are the
extra keys on a Mac keyboard. If you're a Windows extra keys on a Mac keyboard. If you're a Windows
user you're not gong to see these. So as an example, here is the shape of the user you're not gong to see these. So as an example, here is the shape of the
command key, this is control, this is option, command key, this is control, this is option,
and so on. With this guideline you can easily understand the shortcut associated with 等等。 With this guideline you can easily understand the shortcut associated with
each command. Let me show you. So here we have all each command.让我演示给你看。 So here we have all
the commmands when a cell is in the command mode. For example, we have the commmands when a cell is in the command mode. For example, we have
this command, open the command palette. This is exactly like the this command, open the command palette. This is exactly like the
command palette that we have in vs code. Here is a short cut command palette that we have in vs code. Here is a short cut
to execute this command. That is command shift and F. to execute this command. That is command shift and F.
Okay, so here we have lots of shortcuts, Okay, so here we have lots of shortcuts,
of course you're not going to use all of them all the time, but it's good to have a quick look here to see of course you're not going to use all of them all the time, but it's good to have a quick look here to see
what is available for you. Tis these shortcuts you can write code much faster. what is available for you. Tis these shortcuts you can write code much faster.
So let me show you somme of the most useful ones. I'm going to close this, So let me show you somme of the most useful ones. I'm going to close this,
now with our first cell in the command mode now with our first cell in the command mode
I'm going to press b, and this inserts, a new I'm going to press b, and this inserts, a new
cell below this cell. We can also go cell below this cell.我们也可以去
back to our first cell, press escape, now the cell is in the command back to our first cell, press escape, now the cell is in the command
mode. We can insert an empty cell above this cell by pressing a. 模式。 We can insert an empty cell above this cell by pressing a.
So either a or b. A for above or So either a or b. A for above or
b for below. Now if you don't want this cell, you can press d b for below. Now if you don't want this cell, you can press d
twice to delete it. Like this. twice to delete it.像这样。
Now in the cell I'm going to print a hello world message. So print Now in the cell I'm going to print a hello world message. So print
hello world. Now, to hello world. Now, to
run the code in this cell, we can click on the run button here, run the code in this cell, we can click on the run button here,
so, here's our print function, and so, here's our print function, and
right below that you can see the output of this function. But note that when you run right below that you can see the output of this function. But note that when you run
a cell, this will only execute the code in that cell. a cell, this will only execute the code in that cell.
In other words, the code in other cells will not be executed. In other words, the code in other cells will not be executed.
Let me show you want I mean, so in the cell below the cell, I'm going to delete the call Let me show you want I mean, so in the cell below the cell, I'm going to delete the call
to a describe method. Instead I'm going to print to a describe method. Instead I'm going to print
ocean. Now, I'm going 海洋。现在，我要走了
to put the cursor back in this cell where we print this hello world message to put the cursor back in this cell where we print this hello world message
and run this cell. So we can see hello world and run this cell. So we can see hello world
is displayed here, but the cell below is still displaying is displayed here, but the cell below is still displaying
the describe table, so we don't see the changes here. Now, the describe table, so we don't see the changes here.现在，
to solve this problem, we can go to the cell menu on the top, to solve this problem, we can go to the cell menu on the top,
and run all cells together. This can work for a and run all cells together. This can work for a
small project, but sometimes you're working with a large data small project, but sometimes you're working with a large data
set, so if you want to run all these cells together it's going to take a lot of time. That is set, so if you want to run all these cells together it's going to take a lot of time.那是
the reason jupiter saves the output of each cell, so we don't have to rerun that the reason jupiter saves the output of each cell, so we don't have to rerun that
code if it hasn't changed. So this notebook file that we have here, code if it hasn't changed. So this notebook file that we have here,
includes our source code organized in cells as includes our source code organized in cells as
well as the output for each cell. That is why it's different well as the output for each cell. That is why it's different
from a regular py file where we only have the source code. from a regular py file where we only have the source code.
Here we also have autocompletion and intellisence, so in this cell, Here we also have autocompletion and intellisence, so in this cell,
let's call df data frame let's call df data frame
. now if you press tab you can see all the attributes 。 now if you press tab you can see all the attributes
and methods in this object. So let's call and methods in this object. So let's call
describe, now with the cursor on the name of the method we can describe, now with the cursor on the name of the method we can
press shift and tab, to see this tool tip that describes press shift and tab, to see this tool tip that describes
what this method does and what parameter it takes. So here in front of what this method does and what parameter it takes. So here in front of
so here in front of signature you can see the describe method, these are the paramter so here in front of signature you can see the describe method, these are the paramter
and their default value, and right below that you can see and their default value, and right below that you can see
the description of what that method does. In this case, it describes the description of what that method does. In this case, it describes
generates descriptive statistics, that summarize the central tendency and so on. generates descriptive statistics, that summarize the central tendency and so on.
Similar to vs code, we can also convert a line to comment Similar to vs code, we can also convert a line to comment
by pressing command and slash on mac, or by pressing command and slash on mac, or
control slash on windows. Like this. Now that line is a comment, we can control slash on windows.像这样。 Now that line is a comment, we can
press the same shortcut one more time to remove the comment, press the same shortcut one more time to remove the comment,
so these are some of the most useful shortcuts in jupiter. so these are some of the most useful shortcuts in jupiter.
Now over the next few lectures we're going to work on a real machine learning project, but before we get there, Now over the next few lectures we're going to work on a real machine learning project, but before we get there,
let's delete all the cells here, so we start with only a single let's delete all the cells here, so we start with only a single
empty cell, so here, in this cell, first I'm going to press the empty cell, so here, in this cell, first I'm going to press the
escape button, now the cell is blue, so we're in the command mode, and escape button, now the cell is blue, so we're in the command mode, and
we can delete the cell by pressing d twice. There you go. we can delete the cell by pressing d twice.你去吧
Now, the next cell is activated and is in the command mode. Now, the next cell is activated and is in the command mode.
So, let's delete this as well, so we have two more cells to delete So, let's delete this as well, so we have two more cells to delete
there you go, and the last one, like this, there you go, and the last one, like this,
so now we have an empty notebook with a single cell. so now we have an empty notebook with a single cell.
Over the next few lectures, we're going to work on a real machine learning project, Over the next few lectures, we're going to work on a real machine learning project,
imagine we have an online music store, when our users sign up, imagine we have an online music store, when our users sign up,
we asked our age and gender, and based on their profile, we asked our age and gender, and based on their profile,
you recommend various music albums their likely to buy. So in this project, you recommend various music albums their likely to buy. So in this project,
you want to use machine learning to increase sales. you want to use machine learning to increase sales.
So, we want to build a model, we feed this model with some sample data, So, we want to build a model, we feed this model with some sample data,
based on the existing users. Our model will learn the patterns in our data, based on the existing users. Our model will learn the patterns in our data,
so we can ask it to make predictions. When a new user signs up, so we can ask it to make predictions. When a new user signs up,
we tell our model, hey, we have a new user with this profile, what we tell our model, hey, we have a new user with this profile, what
is the kind of music, that this user is interested in, our model will say jazz, or hip hop, is the kind of music, that this user is interested in, our model will say jazz, or hip hop,
or whatever, and baed on that we can make suggestions to the user or whatever, and baed on that we can make suggestions to the user
so, this is the problem we're going to solve, now back to the list of steps in the machine so, this is the problem we're going to solve, now back to the list of steps in the machine
learning projects, first we need to import our data, then, we should learning projects, first we need to import our data, then, we should
prepare or clean it, next we select a machine learning algorithm prepare or clean it, next we select a machine learning algorithm
to build a model, we treat our model and ask it to make predictions. to build a model, we treat our model and ask it to make predictions.
And finally, we evaluate our algorithm to see it's And finally, we evaluate our algorithm to see it's
accuracy. if it's not accurate we either fine tune our model or 准确性。 if it's not accurate we either fine tune our model or
select a different algorithm. So let's focus on the first step. select a different algorithm. So let's focus on the first step.
Head over to bit.ly/music.csv Head over to bit.ly/music.csv
this is a very basic csv that I've created for this project, it's just some random this is a very basic csv that I've created for this project, it's just some random
made up data it's not real. So we have a table with 3 made up data it's not real. So we have a table with 3
columns, age, gender, and genre. Gender columns, age, gender, and genre.性别
can either be one which represents a male, can either be one which represents a male,
or a 0, which represents a female, here I'm making a few assumptions. or a 0, which represents a female, here I'm making a few assumptions.
I'm assuming that men between 20 and 25 like hip hop men between I'm assuming that men between 20 and 25 like hip hop men between
26 and 30 like jazz, and after the age of 30, 26 and 30 like jazz, and after the age of 30,
they like classical music. For women I'm assuming they like classical music. For women I'm assuming
that if they are between 20 and 25 they like dance music, if that if they are between 20 and 25 they like dance music, if
they are between 26 and 30 they like acoustic music, and just like they are between 26 and 30 they like acoustic music, and just like
men, after the age of 30 they like classical music. Once again this is men, after the age of 30 they like classical music. Once again this is
a made up pattern, it's not the representation of the reality, so, a made up pattern, it's not the representation of the reality, so,
let's go ahead and download this csv. Click on this ... icon here let's go ahead and download this csv. Click on this ... icon here
and download this file. and download this file.
In my downloads folder, here we have this music.csv. In my downloads folder, here we have this music.csv.
I'm going to drag and drop this onto the desktop because that's where I've I'm going to drag and drop this onto the desktop because that's where I've
stored this hello world notebook. So I want you to put this csv file stored this hello world notebook. So I want you to put this csv file
right next to your jupiter notebook. right next to your jupiter notebook.
Now, back to our notebook, you need to read the csv file, so just like before Now, back to our notebook, you need to read the csv file, so just like before
first we need to import the pandas module, so import panda first we need to import the pandas module, so import panda
as pd. And then we'll call pd, as pd. And then we'll call pd,
.read_csv and .read_csv and
the name of our file is music.csv. As you saw earlier this the name of our file is music.csv. As you saw earlier this
returns a data frame which is a two dimensional array similar to an xl spreadsheet, returns a data frame which is a two dimensional array similar to an xl spreadsheet,
so let's call that music_ so let's call that music_
data. Now let's inspect 数据。 Now let's inspect
this music_data to make sure we loaded everything properly so this music_data to make sure we loaded everything properly so
run, so here's our data frame, beautiful, next we need run, so here's our data frame, beautiful, next we need
to prepare or clean the data. And that's the topic for the next lecture. to prepare or clean the data. And that's the topic for the next lecture.
next lecture. next lecture.
The second step in a machine learning project, is cleaning, or The second step in a machine learning project, is cleaning, or
preparing the data, and that involves tasks such as removing duplicate preparing the data, and that involves tasks such as removing duplicate
null values and so on. Now on this particular data set we don't have to do anything null values and so on. Now on this particular data set we don't have to do anything
kind of cleaning, because we don't have any duplicates, and as you can see kind of cleaning, because we don't have any duplicates, and as you can see
As you can see, all rows have values for all columns, so we don't have null values. As you can see, all rows have values for all columns, so we don't have null values.
But there is one thing we need to do, But there is one thing we need to do,
we should split this data set into two separate data sets, one into the first we should split this data set into two separate data sets, one into the first
two columns, which we refer to as the input set, and the other two columns, which we refer to as the input set, and the other
with the last column which we refer to as the output set. So when we train a model, with the last column which we refer to as the output set. So when we train a model,
so when we train a model we give it two separate data sets. The input set and the output set. so when we train a model we give it two separate data sets. The input set and the output set.
The output set which is in this case The output set which is in this case
the genre column contains the predictions so we're telling our model that the genre column contains the predictions so we're telling our model that
if we have a user who's 20 years old and a male they like if we have a user who's 20 years old and a male they like
hip hop. Once we train our model then we give it a new hip hop. Once we train our model then we give it a new
input set. For example, we have a new user who is input set. For example, we have a new user who is
21 years old and is a male, what is the genre of the music that this 21 years old and is a male, what is the genre of the music that this
user probably likes. As you can see in our input set, we don't have user probably likes. As you can see in our input set, we don't have
a sample for a 21 year old male, so we're going to ask a sample for a 21 year old male, so we're going to ask
our model to predict that. That is the reason we need to split this data our model to predict that. That is the reason we need to split this data
set into two separate sets. Input and output set into two separate sets. Input and output
So back to our code, this data frame object has a So back to our code, this data frame object has a
method called drop. Now method called drop.现在
if you put the cursor on the method name and press shift if you put the cursor on the method name and press shift
and tab, you can see this tool tip, so this is the signature of and tab, you can see this tool tip, so this is the signature of
this drop method, these are the parameters you can pass here. this drop method, these are the parameters you can pass here.
the parameter we're going to use in this lecture is set to none by default. the parameter we're going to use in this lecture is set to none by default.
Witht his parter we can specify the columns we want to drop, so Witht his parter we can specify the columns we want to drop, so
in this case we set columns in this case we set columns
to an array with one string, genre to an array with one string, genre
now this method doesn't actually modify the original data set. now this method doesn't actually modify the original data set.
In fact it will create a new data set but without thisq In fact it will create a new data set but without thisq
call. So by convention we use a capital X to call. So by convention we use a capital X to
represent that data set, so capital x equals this represent that data set, so capital x equals this
expression. Now, let's inspect x, expression. Now, let's inspect x,
so as you can see our input set or x so as you can see our input set or x
includes these two columns, age and gender, it doesn't have the includes these two columns, age and gender, it doesn't have the
output or predictions. Next we need to create our output set so, output or predictions. Next we need to create our output set so,
once again we start with our data frame once again we start with our data frame
using data, using square brackets we can get all the using data, using square brackets we can get all the
values in a given column. In this case, genre. Once again this returns values in a given column. In this case, genre. Once again this returns
a new data set. By convention we use a lower case y a new data set. By convention we use a lower case y
to represent that. So that is our output to represent that. So that is our output
data. Let's inspect that as well. So, 数据。 Let's inspect that as well.所以，
in this data set we only have the predictions or the answers. in this data set we only have the predictions or the answers.
So we have prepared our data, next we want to create a model using an So we have prepared our data, next we want to create a model using an
algorithm. 算法。
The next step is to build a The next step is to build a
model using a machine learning algorithm. There are so many algorithms out there, each algorithm model using a machine learning algorithm. There are so many algorithms out there, each algorithm
has its pros and cons, in terms of the performance and accuracy. has its pros and cons, in terms of the performance and accuracy.
In this lecture we're going to use a very simple algorithm called decision tree. In this lecture we're going to use a very simple algorithm called decision tree.
Now the good news is we don't have to explicitly Now the good news is we don't have to explicitly
program these algorithms, they're already implemented in a library called side kick program these algorithms, they're already implemented in a library called side kick
learn. So, here on the top, 学习。 So, here on the top,
from sklearn.tree from sklearn.tree
let's import the decision tree let's import the decision tree
classifier. So sklearn is the package classifier. So sklearn is the package
that comes with sidekick learn library, this is the most popular machine learning library that comes with sidekick learn library, this is the most popular machine learning library
in python. In this package, we have a module called tree. in python. In this package, we have a module called tree.
And in this module we have a class called decision tree classifier. And in this module we have a class called decision tree classifier.
This class implements the decision tree operator, okay? This class implements the decision tree operator, okay?
So, now we need to create a new instance of this class. So, now we need to create a new instance of this class.
So, at the end, let's create So, at the end, let's create
an object called models nd set it to anew instance of decision, an object called models nd set it to anew instance of decision,
tree classifier. Like this. tree classifier.像这样。
So now we have a model, next we need to train it so it learns So now we have a model, next we need to train it so it learns
patterns in the data, and that is pretty easy. You call model, patterns in the data, and that is pretty easy. You call model,
.fit this method takes .fit this method takes
2 data set. The input set and the output set. 2 data set. The input set and the output set.
So they are capital x, y. So they are capital x, y.
Now finally, we need to ask our model to make a prediction, so we can Now finally, we need to ask our model to make a prediction, so we can
ask it, what is the kind of music a 24 year old male likes? Now before ask it, what is the kind of music a 24 year old male likes? Now before
we do that, let's temporarily inspect our initial data set. That is we do that, let's temporarily inspect our initial data set.那是
music data. So, look what music data. So, look what
we got here. As I told you earlier, I've assumed we got here. As I told you earlier, I've assumed
that men between 20 and 25 like hip hop music but that men between 20 and 25 like hip hop music but
here we only have 3 samples for men age here we only have 3 samples for men age
20, 23,and 25. We don't have a sample for 20, 23,and 25. We don't have a sample for
a 21 year old male. So if you ask our model to predict the kind of music a 21 year old male. So if you ask our model to predict the kind of music
that a 21 year old male likes, we expect it to say hip hop. Similarly, that a 21 year old male likes, we expect it to say hip hop. Similarly,
I've assumed that women between 20 and 25 like dance music, I've assumed that women between 20 and 25 like dance music,
but we don' have a sample for a 22 year old female. So once again if but we don' have a sample for a 22 year old female. So once again if
you ask our model to predict the kind of music that a 22 year old woman you ask our model to predict the kind of music that a 22 year old woman
likes, we expect it to say dance. likes, we expect it to say dance.
So, with these assumptions let's go ahead and ask our model So, with these assumptions let's go ahead and ask our model
to make predictions. So let's remove to make predictions. So let's remove
the last line, and instead we're going to call model the last line, and instead we're going to call model
.predict. This method takes a .predict. This method takes a
2 dimensional array. So here's the outer array. In this array, each element 2 dimensional array. So here's the outer array. In this array, each element
is an array. So I'm going to pass is an array. So I'm going to pass
another array here, and in this array I'm going to pass a new input set. another array here, and in this array I'm going to pass a new input set.
a 21 year old male. So 21, 1 a 21 year old male. So 21, 1
that is like a new record in this table. Okay? So, that is like a new record in this table.好的？所以，
this is one input set, let's pass another input for a 22 year old this is one input set, let's pass another input for a 22 year old
female. So here's another array, here, 女。 So here's another array, here,
we add 22 comma 0, so we're asking our model to make 2 predictions we add 22 comma 0, so we're asking our model to make 2 predictions
at the same time. We get the result and store it 同时。 We get the result and store it
in a variable called predictions, and finally in a variable called predictions, and finally
let's inspect that in our notebook. let's inspect that in our notebook.
Run, look what we got, our Run, look what we got, our
model is saying that a 21 year old male likes hip hop, and a 22 year model is saying that a 21 year old male likes hip hop, and a 22 year
old female likes dance music. So our model successfully old female likes dance music. So our model successfully
will make predictions here. But wait a minute, building a model that makes will make predictions here. But wait a minute, building a model that makes
predictions accurately is not always that easy. predictions accurately is not always that easy.
After we build a model we need to measure it's accuracy. After we build a model we need to measure it's accuracy.
And if it's not accurate enough, we should either fine tune it or build a model using a different And if it's not accurate enough, we should either fine tune it or build a model using a different
algorithm. So next lecture I'm going to show you how to measure the accuracy of 算法。 So next lecture I'm going to show you how to measure the accuracy of
a model. a model.
In this lecture I'm going to show you In this lecture I'm going to show you
how to measure the accuracy of your models. Now in order to do so, how to measure the accuracy of your models. Now in order to do so,
first we need to split our data set into two sets. One for training and the other first we need to split our data set into two sets. One for training and the other
for testing, because right now, we are passing the entire data set, for testing, because right now, we are passing the entire data set,
for training the model and we're using 2 samples for for training the model and we're using 2 samples for
making predictions. That is not enough to calculate the making predictions. That is not enough to calculate the
accuracy of a model. A general rule of thumb is to annotate accuracy of a model. A general rule of thumb is to annotate
70-80% of our data to training, and another20-30% 70-80% of our data to training, and another20-30%
for testing, then instead of passing only two samples for for testing, then instead of passing only two samples for
making predictions, we can pass the data set for testing making predictions, we can pass the data set for testing
we'll get the predictions and then compare the predictions with the actual values. we'll get the predictions and then compare the predictions with the actual values.
In the test set. Based on that, we can calculate the In the test set. Based on that, we can calculate the
accuracy. That's really easy, all we have to do is import a couple functions and 准确性。 That's really easy, all we have to do is import a couple functions and
call them in this code. Let me show you. So first on this top call them in this code.让我演示给你看。 So first on this top
from sklearn.model from sklearn.model
underline selection module we import a function called underline selection module we import a function called
train test split. With this function we can easily split our train test split. With this function we can easily split our
data set into two sets. So training and testing, data set into two sets. So training and testing,
now, right here, after we define x and y, now, right here, after we define x and y,
sets, we call this funciton, so train sets, we call this funciton, so train
test split. We give it 3 test split. We give it 3
arguments, x, y, ad a keyword argument that specifies arguments, x, y, ad a keyword argument that specifies
the size of our test dataset. So test the size of our test dataset. So test
_size we set it to 0.2 So we're allocating 20% _size we set it to 0.2 So we're allocating 20%
of our data for testing. Now this functions returns a of our data for testing. Now this functions returns a
tuple, so we can unpack it into 4 variables tuple, so we can unpack it into 4 variables
right here. x_train 就在这儿。 x_train
x_test y_train and y x_test y_train and y
_test. so the first two variables _test. so the first two variables
are the input sets for training and testing. are the input sets for training and testing.
And the other are the output sets for training and testing, And the other are the output sets for training and testing,
now, when training our model, now, when training our model,
instead of passing the entire data set we want to pass only the training data set. instead of passing the entire data set we want to pass only the training data set.
So, x_train, So, x_train,
and y_train. Also, when making predictions and y_train. Also, when making predictions
instead of passing these two samples, we pass instead of passing these two samples, we pass
x_test. So thats the datas set. x_test. So thats the datas set.
That contains input values for testing. Now we get That contains input values for testing. Now we get
the predictions. To calculate the accuracy we simply have to compare these predictions the predictions. To calculate the accuracy we simply have to compare these predictions
with the actual values we have in the output set with the actual values we have in the output set
for testing. That is very easy. First on the top, for testing. That is very easy. First on the top,
we need to import a funciton, so, from sklearn.metric we need to import a funciton, so, from sklearn.metric
.metrics import accuracy .metrics import accuracy
_score. Now at the end. _score. Now at the end.
We call this function, so accuracy score and We call this function, so accuracy score and
give it two arguments y_test. give it two arguments y_test.
Which contains the expected values and Which contains the expected values and
predictions which contains the actual value. predictions which contains the actual value.
Now this functions returns an accuracy score between 0 Now this functions returns an accuracy score between 0
to 1. So we can store it here, to 1. So we can store it here,
and simply display it on the console. So let's go ahead and simply display it on the console. So let's go ahead
and run this program. So, the accuracy score and run this program. So, the accuracy score
is 1 or a 100% but if we run this one more time, we're going to see a different result is 1 or a 100% but if we run this one more time, we're going to see a different result
because every time we split our data set into because every time we split our data set into
training a test sets. We'll have different data sets, because this function training a test sets. We'll have different data sets, because this function
randomly picks data for training and testing. Let me show you, so put the cursor randomly picks data for training and testing. Let me show you, so put the cursor
in the cell, now we can see this is activated in the cell, now we can see this is activated
note that if you click this button here, it will run the cell and also insert a new note that if you click this button here, it will run the cell and also insert a new
cell below this cell. Let me show you. So if I go to this second cell cell below this cell.让我演示给你看。 So if I go to this second cell
press the skip button, now we are in the command mode, press the skip button, now we are in the command mode,
press d twice, okay now it's deleted, if we press d twice, okay now it's deleted, if we
click the run button, we can see, this code was click the run button, we can see, this code was
executed, and now we have a new cell, so if you want to run our cell multiple times executed, and now we have a new cell, so if you want to run our cell multiple times
every time you have to click this and then run every time you have to click this and then run
it, and then click again, and run it, it's a little bit tedious. So I'll show you a shortcut. it, and then click again, and run it, it's a little bit tedious. So I'll show you a shortcut.
Activate the first cell, and press control and enter Activate the first cell, and press control and enter
this runs the current cell without this runs the current cell without
adding a new cell below it. So, back here, lets run it multiple times adding a new cell below it. So, back here, lets run it multiple times
okay, now look the accuracy dropped to 0.75, it's okay, now look the accuracy dropped to 0.75, it's
still good, so the cuuracey scored here, is somewhere still good, so the cuuracey scored here, is somewhere
between 75% to a hundred %. But let me show you something, if I between 75% to a hundred %. But let me show you something, if I
change the test size, from 0.2 to 0. change the test size, from 0.2 to 0.
8, so essentially we're using only 20 percent of our data, 8, so essentially we're using only 20 percent of our data,
for training this model. And you're using the other 80% for training this model. And you're using the other 80%
for testing. Now let's see what happens when we run this cell multiple for testing. Now let's see what happens when we run this cell multiple
times. SO, control and enter, look, the accuracy immediately 倍。 SO, control and enter, look, the accuracy immediately
dropped to 0.4 One more time, now 46% dropped to 0.4 One more time, now 46%
40%, 26% 40%, 26%
it's really, really bad. The reason this is happening is because it's really, really bad. The reason this is happening is because
you are using very little data for training this model. This is one of you are using very little data for training this model. This is one of
the key concepts in machine learning, the more data we give to our model and the cleaner the key concepts in machine learning, the more data we give to our model and the cleaner
the data is, we get the better result, so if you have duplicates, the data is, we get the better result, so if you have duplicates,
irrelevant data, or incomplete values, our irrelevant data, or incomplete values, our
model will learn back patterns in our data. That's why it's really important to clean model will learn back patterns in our data. That's why it's really important to clean
before training our model. Now let's change this back to 0. before training our model. Now let's change this back to 0.
2, run this one more time, okay, now 2, run this one more time, okay, now
the accuracy is 1, 75%, now we drop to 50% the accuracy is 1, 75%, now we drop to 50%
again, the reason this is happening, is because we don't have enough data. again, the reason this is happening, is because we don't have enough data.
Some machine learning problems, require, thousands or even millions Some machine learning problems, require, thousands or even millions
of samples, to train model, the more complex a problem is, of samples, to train model, the more complex a problem is,
the more data we need. For example, here we're only dealing with a table of three the more data we need. For example, here we're only dealing with a table of three
columns, but if you want to build a model to tell if a picture of a cat or a dog or a horse, columns, but if you want to build a model to tell if a picture of a cat or a dog or a horse,
or a lion, we will need millions of pictures, the more animal or a lion, we will need millions of pictures, the more animal
it will support the more pictures we need. In the next lecture, we're going to talk about it will support the more pictures we need. In the next lecture, we're going to talk about
model persistence. model persistence.
So this is a So this is a
very basic implementation of building a training a model to make a prediction. very basic implementation of building a training a model to make a prediction.
Now to simplify things I have removed all the code that you wrote in the last lecture for Now to simplify things I have removed all the code that you wrote in the last lecture for
calculating the accuracy, because in this lecture, you're going to calculating the accuracy, because in this lecture, you're going to
focus on a different topic, So basically we import our data set. focus on a different topic, So basically we import our data set.
Create a model, train it, and then Create a model, train it, and then
ask it to make predictions, now this piece of code that you see here, is not what we ask it to make predictions, now this piece of code that you see here, is not what we
want to run everytime we have a new user or want to run everytime we have a new user or
every time we want to make recommendations to an existing user every time we want to make recommendations to an existing user
sometimes it's really time consuming. In this example, we're dealing with a sometimes it's really time consuming. In this example, we're dealing with a
very small data set that only has 20 records. But in a real application, very small data set that only has 20 records. But in a real application,
you might have a data set with thousands or trillions of examples, training for that you might have a data set with thousands or trillions of examples, training for that
might take seconds or minutes or even hours. might take seconds or minutes or even hours.
And that is wy model persistence is important, once in a while, And that is wy model persistence is important, once in a while,
you build and train our model and then we save it to a file. you build and train our model and then we save it to a file.
Now, next time we want to make predictions, we simply load the model from the file and Now, next time we want to make predictions, we simply load the model from the file and
ask it to make predictions. That model is already trained ask it to make predictions. That model is already trained
you don't need to retrain it, it's like an intelligent person. So let me show you you don't need to retrain it, it's like an intelligent person. So let me show you
how to do this. it's very very easy. On the top, how to do this. it's very very easy. On the top,
from sklearn.externals module from sklearn.externals module
we import joblib. This we import joblib.这个
job lib object has methods for saving and loading modules. So, job lib object has methods for saving and loading modules.所以，
after we train our model we'll simply call after we train our model we'll simply call
job lib .dump and give it job lib .dump and give it
two arguments. Our model and the name of the file two arguments. Our model and the name of the file
in which we want to store this model. Let's call this music in which we want to store this model. Let's call this music
-recommender.job lib -recommender.job lib
That's all we have to do. Now temporarily I'm going to comment That's all we have to do. Now temporarily I'm going to comment
out this line, we don't want to make any predictions, we just want to store out this line, we don't want to make any predictions, we just want to store
train model in a file. So let's run this cell with control, train model in a file. So let's run this cell with control,
and slash, okay, look, in the output and slash, okay, look, in the output
we have an array that contains the name of our model file. we have an array that contains the name of our model file.
So this is the return value of the dump method. Now back to our So this is the return value of the dump method.现在回到我们的
desktop, right next to my notebook we can see our job lib file, this is where our model is stored. desktop, right next to my notebook we can see our job lib file, this is where our model is stored.
It's simply a binary file. Now back to our It's simply a binary file.现在回到我们的
jupiter notebook. As I told you before in a real application we don't want to jupiter notebook. As I told you before in a real application we don't want to
train model every time. So let's comment out train model every time. So let's comment out
these few lines, on Mac, we can press these few lines, on Mac, we can press
command and slash and on windows control slash. command and slash and on windows control slash.
Okay, these lines are commented out, now this time, instead of dumping Okay, these lines are commented out, now this time, instead of dumping
our model, we're going to load it, so we call the load method, we don't have the model, we simply our model, we're going to load it, so we call the load method, we don't have the model, we simply
pass the name of our model file. This pass the name of our model file.这个
returns our trained model. Now with this returns our trained model. Now with this
two lines we can simply make predictions, so, earlier, two lines we can simply make predictions, so, earlier,
we assumed that men between 20-25 like hip hop music. we assumed that men between 20-25 like hip hop music.
let's print predictions and see if our model is behaving let's print predictions and see if our model is behaving
so control so control
so this is how we persist and load models. so this is how we persist and load models.
Earlier in this section, I told you that decision trees are the easiest Earlier in this section, I told you that decision trees are the easiest
to understand. And that's why we started machine learning with decision trees. In this lecture, we're going to 了解。 And that's why we started machine learning with decision trees. In this lecture, we're going to
export our model in visual format, so you will see export our model in visual format, so you will see
how this model makes predictions, that is really, really cool. how this model makes predictions, that is really, really cool.
Let me show you. So once again I've simplified this code 让我演示给你看。 So once again I've simplified this code
so we simply import our data set, create so we simply import our data set, create
input and output sets, create a model, and input and output sets, create a model, and
train it, that's all we are doing, now I want you to train it, that's all we are doing, now I want you to
follow along with me, type everything exactly as I show you in this lecture. Don't follow along with me, type everything exactly as I show you in this lecture.别
worry about what everything means we'll come back to it shortly. So on the top, worry about what everything means we'll come back to it shortly. So on the top,
from sklearnimport from sklearnimport
tree, this object has a method for exporting tree, this object has a method for exporting
our decision tree in a graphical format. So after we our decision tree in a graphical format. So after we
train our model, let's call tree train our model, let's call tree
_graph vis. Now here are a few arguments we need _graph vis. Now here are a few arguments we need
to pass. The first argument is our model, to pass. The first argument is our model,
the second is the name oft he output file. So here we're going to use keyword the second is the name oft he output file. So here we're going to use keyword
arguments, because this method takes so many parameters, and we want to selectively pass arguments, because this method takes so many parameters, and we want to selectively pass
keyword arguments without worrying about their order. So, keyword arguments without worrying about their order.所以，
the parameter we're going to set it out_file the parameter we're going to set it out_file
let's set this to music-recommend let's set this to music-recommend
.dot. This is the dot format, which is the .dot. This is the dot format, which is the
graph format, which is a graph description language, you will see that shortly. graph format, which is a graph description language, you will see that shortly.
Now the other parameter you want to set is feature Now the other parameter you want to set is feature
_names. We set this to an array of two strings, _names. We set this to an array of two strings,
age and gender. These are the features age and gender. These are the features
or the columns of our data set. So they are properties or features of or the columns of our data set. So they are properties or features of
our data. Okay? The other parameter our data.好的？ The other parameter
is class names. So class_names is class names. So class_names
we should set this to the list of classes or labels we have we should set this to the list of classes or labels we have
in our output data set, like hip hop, jazz, classical and so on. in our output data set, like hip hop, jazz, classical and so on.
So, this y data set includes all the genre So, this y data set includes all the genre
or all the classes of our data, but they are repeated a few times in this data set. or all the classes of our data, but they are repeated a few times in this data set.
So, here we call y.unit this returns So, here we call y.unit this returns
the unique list of classes, now we should sort this alphabetically. the unique list of classes, now we should sort this alphabetically.
So, we call the sorted function, and pass the So, we call the sorted function, and pass the
result a y.unique. result a y.unique.
The next parameter is labeled, we set The next parameter is labeled, we set
this to a string, all, once again don't worry, this to a string, all, once again don't worry,
about the details of these parameters, we're going to come back to these shortly. So, about the details of these parameters, we're going to come back to these shortly.所以，
set label to all, then round it, set label to all, then round it,
to true, and finally field to true. So, to true, and finally field to true.所以，
so this is the end result, now let's run this sound so this is the end result, now let's run this sound
using control and enter, okay, using control and enter, okay,
here we have a nw file music recommender .. that's a little here we have a nw file music recommender .. that's a little
bit funny. So we want to open this file with vs code. bit funny. So we want to open this file with vs code.
So drag and drop this into a vs code window. So drag and drop this into a vs code window.
Okay, here's a dot format, it's Okay, here's a dot format, it's
a textural language for describing graphs a textural language for describing graphs
Now to visualize this graph we need to install an extension in vs code. Now to visualize this graph we need to install an extension in vs code.
So on the left side, click extensions panel and search So on the left side, click extensions panel and search
for dot, dot. Look at this second extension here. for dot, dot. Look at this second extension here.
graphvis or .language graphvis or .language
by stephon vs. Go ahead and install this extension by stephon vs. Go ahead and install this extension
and then reload vs code. Once you do that, you can visualize and then reload vs code. Once you do that, you can visualize
this dot file. So let me close this tab. this dot file. So let me close this tab.
Alright, look at this ... on the right side. Click this, Alright, look at this ... on the right side. Click this,
you should have a new menu, open preview to the side. So like that you should have a new menu, open preview to the side. So like that
alright, here's the visualization of our decision tree, alright, here's the visualization of our decision tree,
let's close the dot file, there you go. This is exactly let's close the dot file, there you go. This is exactly
how our model makes predictions. So we have this how our model makes predictions. So we have this
binary tree, which means every node can have a maximum of two children. binary tree, which means every node can have a maximum of two children.
On top of each note we have a condition On top of each note we have a condition
if this condition is true we go to the child node on the left side. Otherwise we if this condition is true we go to the child node on the left side. Otherwise we
go to the child node on the right side. So let's see what's happening here, the first condition go to the child node on the right side. So let's see what's happening here, the first condition
is age less then or equal to 30 is age less then or equal to 30
.5. If this condition is false, that means that user is 30 years .5. If this condition is false, that means that user is 30 years
or older, so the genre of the music that their interested in is classical. or older, so the genre of the music that their interested in is classical.
So here we're classifying people based So here we're classifying people based
on their profile. That is the reason we have the word class here, on their profile. That is the reason we have the word class here,
so a user who is 30 years or older, belongs to the class of so a user who is 30 years or older, belongs to the class of
classical. Or people who like classical music. Now what if this condition classical. Or people who like classical music. Now what if this condition
is true. That means that user is younger then is true. That means that user is younger then
30, so, now we check the gender, if it's less then 0.5, 30, so, now we check the gender, if it's less then 0.5,
which basically means it equals to 0, then which basically means it equals to 0, then
we're dealing with a female. So we go to the child node here, we're dealing with a female. So we go to the child node here,
now once again we have another condition, so we are dealing with a female now once again we have another condition, so we are dealing with a female
who is younger than 30. Once again we ned to check their age so who is younger than 30. Once again we ned to check their age so
is the age less then 25.5? If that's the case is the age less then 25.5? If that's the case
then that user likes dance music, otherwise they like acoustic music. then that user likes dance music, otherwise they like acoustic music.
So this is a decision tree that our model uses to make predictions So this is a decision tree that our model uses to make predictions
Now if you're wondering why we have these floating point numbers Now if you're wondering why we have these floating point numbers
like 25.5 These are basically the rules like 25.5 These are basically the rules
that our model generates, based on the patterns in our dataset. that our model generates, based on the patterns in our dataset.
As we give our model more data, these rules will change so they are not always the same. As we give our model more data, these rules will change so they are not always the same.
So they are not always the same, also the more columns or the more features So they are not always the same, also the more columns or the more features
we have, our decision tree is going to get more complex. we have, our decision tree is going to get more complex.
We only have two features, age, and gender. Now back to our code We only have two features, age, and gender. Now back to our code
let me quickly explain the meaning of all these parameters, we set field to true let me quickly explain the meaning of all these parameters, we set field to true
so each box or each node is filled with a color. We set so each box or each node is filled with a color. We set
rounded to true, so they have rounded corners. We set label to all rounded to true, so they have rounded corners. We set label to all
so every node has labels that we can read. so every node has labels that we can read.
We set class names to the unique list of genres We set class names to the unique list of genres
and that's for displaying the class for each note, right here, and we set and that's for displaying the class for each note, right here, and we set
feature names to age and gender, sow e can see the rules feature names to age and gender, sow e can see the rules
in our notes. So that was a short and in our notes. So that was a short and
sweet introduction to machien learning. Now you can use the materials that you learn in this section sweet introduction to machien learning. Now you can use the materials that you learn in this section
and apply them to solve a different set of problems. Now here's a question for you: and apply them to solve a different set of problems. Now here's a question for you:
What ideas do you have for machine learning? What kind of problems do you want to solve What ideas do you have for machine learning? What kind of problems do you want to solve
with machine learning? Use the comments box below and let me know. I love to with machine learning? Use the comments box below and let me know.我爱
hear about your ideas! Next we're going to use python and a popular framework called hear about your ideas! Next we're going to use python and a popular framework called
django. To build a web application. Are you ready, let's get started django. To build a web application. Are you ready, let's get started
In this turorial, I'm going to show you how to create your first website with python, and A In this turorial, I'm going to show you how to create your first website with python, and A
and a very popular framework called django. So it's spelled with a silent D, and a very popular framework called django. So it's spelled with a silent D,
and is pronounced django. it's a web framework for and is pronounced django. it's a web framework for
perfectionists with deadlines. So if you're a perfectionist and you want to quickly put together perfectionists with deadlines. So if you're a perfectionist and you want to quickly put together
a website that is fast, scaleable and secure, a website that is fast, scaleable and secure,
django is your best friend. And here are the popular websites django is your best friend. And here are the popular websites
built with Django. Like Instagram, Spotify built with Django. Like Instagram, Spotify
YouTube, Washington Post, and so on. Now you might say YouTube, Washington Post, and so on. Now you might say
what is a framework, and why do we need a framework to build a web application or a web site? what is a framework, and why do we need a framework to build a web application or a web site?
Well, the framework is essentially a library of reusable modules Well, the framework is essentially a library of reusable modules
these modules provide functionality for common tasks these modules provide functionality for common tasks
for example, in the case of a web framework like django we have modules to work with http requests for example, in the case of a web framework like django we have modules to work with http requests
url's, sessions, cookies and so on. url's, sessions, cookies and so on.
These are the concerns of pretty much every website or These are the concerns of pretty much every website or
application out there. So all this functionality is baked into django, we don't have to application out there. So all this functionality is baked into django, we don't have to
code it from scratch, that is why we use a framework like django. Now, code it from scratch, that is why we use a framework like django.现在，
django. Now technically a framework is more then a library, django. Now technically a framework is more then a library,
apart from providing these modules it also provides a structure for each application apart from providing these modules it also provides a structure for each application
It tells us what folders or files we should have in our project. So this It tells us what folders or files we should have in our project.所以这
provides a consistency among various django projects. So as you provides a consistency among various django projects. So as you
move from one company to another and work on different projects, you can easily transition from one move from one company to another and work on different projects, you can easily transition from one
project to another, because all these applications follow the same structure project to another, because all these applications follow the same structure
now, with that interaction, let's create our first now, with that interaction, let's create our first
django project. So back to pycharm, close the hello world django project. So back to pycharm, close the hello world
project, and create a new project, create a new project, project, and create a new project, create a new project,
let's call this new project pyshop let's call this new project pyshop
go ahead, alright, now down at the bottom let's go ahead, alright, now down at the bottom let's
open up a terminal window, in this window we're going to open up a terminal window, in this window we're going to
install django. So we use pip to install install django. So we use pip to install
django. But be sure to add two equal signs here followed by django. But be sure to add two equal signs here followed by
2.1 With this we are telling pip that we want to install django version 2.1 With this we are telling pip that we want to install django version
2.1. The reason I'm doing this, is because in the future when you're watching this video, 2.1. The reason I'm doing this, is because in the future when you're watching this video,
chances are there is a newer version django out there. I chances are there is a newer version django out there.一世
want to make sure that you can easily follow these tutorials, even though what I'm going to show you want to make sure that you can easily follow these tutorials, even though what I'm going to show you
in these tutorials will most likely work with the latest version of django. I just want to in these tutorials will most likely work with the latest version of django.我只想
be on the safe side. So let's go ahead be on the safe side. So let's go ahead
alright, django is installed, alright, django is installed,
next we need to create a django project, so here press control and L next we need to create a django project, so here press control and L
this cleans up our terminal window, you don't have to do it, but it's just this cleans up our terminal window, you don't have to do it, but it's just
easier to see. To create a django project, we need to execute this command, django, easier to see. To create a django project, we need to execute this command, django,
-admin space -admin space
start project space, you're going to call this start project space, you're going to call this
project pyshop space period. project pyshop space period.
So when we install django, django brings a So when we install django, django brings a
command line utility called django admin so this is a utility or program command line utility called django admin so this is a utility or program
that we can execute form the command line or terminal, right here. that we can execute form the command line or terminal, right here.
Now, this program takes various arguments, in this case Now, this program takes various arguments, in this case
we want to use this argument start project, with this we're going to create we want to use this argument start project, with this we're going to create
a project called pushup in the current folder. a project called pushup in the current folder.
So this period is imported here, that means the current folder, if So this period is imported here, that means the current folder, if
you don't add this period, this utility is going to create an extra folder and that you don't add this period, this utility is going to create an extra folder and that
looks a little bit repetitive, so let's go ahead with this, looks a little bit repetitive, so let's go ahead with this,
alright, now, back to the project panel, if you expand py alright, now, back to the project panel, if you expand py
shop we can see that this is our project that we created in pycharm shop we can see that this is our project that we created in pycharm
and in this folder we have these files. and in this folder we have these files.
So, init.py, you have seen this before, that means So, init.py, you have seen this before, that means
this folder is package, so we can import various modules, this folder is package, so we can import various modules,
of this package into other modules, and in this module, of this package into other modules, and in this module,
we define various settings for our application, you're going to see this later in we define various settings for our application, you're going to see this later in
this course. You also have this url's module, and with this module 这个课程。 You also have this url's module, and with this module
we define what should the user see when they see slash we define what should the user see when they see slash
about/contact/products/shopping cart about/contact/products/shopping cart
again, we're going to work with this only. And finally we have this module, again, we're going to work with this only. And finally we have this module,
that wsgi, that is short for web server gateway that wsgi, that is short for web server gateway
interface. The purpose of this module is to provide a standard interface 接口。 The purpose of this module is to provide a standard interface
between applications built with django and web servers. This is between applications built with django and web servers.这是
an advanced topic, so for now, don't worry about it. Now, outside of this folder, an advanced topic, so for now, don't worry about it. Now, outside of this folder,
of this folder you also have this new folder in our project of this folder you also have this new folder in our project
manage.py. As the name implies, we use this to manage manage.py. As the name implies, we use this to manage
this django project. With this we can start our web server, this django project. With this we can start our web server,
we can work with our data base, again, we're going to work with this soon, we can work with our data base, again, we're going to work with this soon,
let's open up the terminal window one more time, now, let's open up the terminal window one more time, now,
type out this command, python if you're on windows or python type out this command, python if you're on windows or python
3 if you're on Mac, because as I told you at the beginning of the course, Mac by 3 if you're on Mac, because as I told you at the beginning of the course, Mac by
default comes with an installation of python, that is python 2. But that is default comes with an installation of python, that is python 2. But that is
this course is python 3. So python 3 space manage this course is python 3. So python 3 space manage
.py space run .py space run
server. What is going on here? So with python interpreter 服务器。这里发生了什么？ So with python interpreter
we are running this program manage.py and passing run server we are running this program manage.py and passing run server
as an argument. This manage.py is essentially the same program as an argument. This manage.py is essentially the same program
that we worked with here, django admin. that we worked with here, django admin.
But we use django admin before creating a django project, now we have a django project, so now But we use django admin before creating a django project, now we have a django project, so now
in this project we work with manage.py this in this project we work with manage.py this
is a module that contains some python code, so we use this module to manage is a module that contains some python code, so we use this module to manage
our django project. So let's go ahead and our django project. So let's go ahead and