未登录,请登录后再发表信息
最新评论 (0)
播放视频

6/44 Hello World示例

Demo: Hello World | Python for Beginners [6 of 44]

如何安装
>> So, Christopher has shown you how to get
Christopher已经展示了怎样
Visual Studio Code working with the Python extension,
使用VS Code来安装Python拓展
now let’s go into Visual Studio Code and
现在就让我们进入其中
actually start trying out some of our code.
真正开始尝试写下我们自己的代码
Now, when you work inside Visual Studio Code,
当你在VS Code里工作时
you’re going to need to create a folder on your file system first,
你需要先在文件系统中创建一个文件夹
and that’s a place where you can keep
你可以在文件夹中保存
all your files together that you’re working on.
你所有编写的文件
So I’ve already gone ahead and I’ve created a folder.
我事先已经创建好了一个文件夹
So as soon as I open up Visual Studio Code,
当我打开VS Code
I’m going to want to go to “File,” “Open
我会点击“文件”
folder,” and open up that folder that I’ve precreated.
然后“打开文件夹” 来打开创建好的文件夹
So I’ve already created
我已经创建了一个
a folder called Intro to Python development,
名为“Intro to Python development”的文件夹
I’m going to open that folder now,
我现在打开这个文件夹
and now that gives me a place to
让我有一个地方来
store all the files I’m going to be working on.
存储所有我需要的文件
Once I’ve got that folder open and I can see
打开文件夹之后 可以看到
the folder name listed over here on the left,
在左边有一个文件夹名列表
then I can say “New file” to
然后我们点击“新文件”
create a new Python file inside that folder.
在这个文件夹下创建新Python文件
So now, I can try out that print statement.
现在 我试一下print语句
So we say, ”Print, Hello world.”
输入 print(‘hello world’)
Now, initially, there’s no way that
现在 VS Code是没办法
Visual Studio Code can tell that
一开始就知道
what I’ve written is a Python module.
我写的是Python模块
So it’s when I save the file
所以当我在保存文件时
that I tell Visual Studio it’s Python.
我会告诉VS Code这是Python
So I’ll just save it by going to ”File,”
我点击“文件”里的“保存”
“Save,” or Control S if you like keyboard shortcuts,
或者快捷键Ctrl+S 来保存文件
and once it’s saved,
当保存之后
I’m going to make sure I call it Helloworld.py,
我要保证命名为“Helloworld.py”
and that PY extension is a cue
py拓展名会告诉VS Code
to Visual Studio Code that I’ve just created a Python script.
我创建的是一个Python脚本
You’ll see right away some colors appearing.
你会马上看到出现了一些颜色
That’s a good sign because it’s actually using code highlighting
这是个好迹象 因为它使用了代码高亮
to indicate that the hello world is the string by showing up in red and so on.
“hello world”变红了 表示这是字符串
So I can tell now that
现在我知道
Visual Studio Code recognizes this as a Python script.
VS Code识别出这是Python脚本
Then I want to run that.
接着我想运行它
So to run that code,
要运行代码
and this will be your ultimate test of whether
还得最终检测你到底有没有
you got all your Python installed correctly,
正确安装好所有的Python文件
you just type “Python” and then the name of your script,
你可以输入python 然后是你的脚本名
and I called mine Helloworld.py.
我这里就是 “Helloworld.py”
If you forget your file name,
如果你忘了你的文件名
don’t worry, it’s at the top of the tab,
不用担心 标签页上就有
and then you hit “Return” and sure enough,
然后敲下回车键就好了
there’s Hello world displayed on the screen.
屏幕上就出现了“hello world”
So I’ve successfully run my first script in Python.
我已经成功运行我第一个Python文件
As I mentioned, you can put
正如我之前提到的 你可以
single quotes or double quotes around your string.
在字符串左右用单引号或者双引号
I’m just going to use Control S to save that file again,
我现在用Ctrl+S再次保存文件
go back to “Command line,” I’m going to use
然后回到“命令行” 我将按下
the up arrow which allows me to go to the previously run command,
上箭头键来使用之前的运行指令
and rerun my script,
再次运行我的脚本
but this time with double quotes around the string,
但是这次字符串是用双引号括起来的
and sure enough, still displays Hello world.
好了 仍然输出“hello world”
So as I mentioned, single quotes,
刚才说了 单引号
double quotes, either one will work just fine.
双引号 每个都可以运行
Now what if I want someone to input a value?
现在如果我想让某人输入一个值呢?
Well, I could create a variable called Name,
好 我可以创建一个变量 name
and say “Input, what is your name?”
输入input(“what is your name?”)
Then I can say hello,
然后是“hello”
and then maybe after I say hello,
接着可能我说完“hello”之后
I say let’s print the name on the screen.
要在屏幕打印输出名字name
So let’s say hello to the person who has typed in their name.
这样就能对输入名字的人说你好“hello”
So now, save that.
好了 保存
Go to my code, rerun that,
重新运行我的代码
and it says, “What is my name?”
它在问我名字“what is your name?”
My name is Susan, and now I
我叫Susan 所以
see “Hello Susan” appearing in the output.
接下来就输出显示“hello Susan”
So the input command I said is
所以我使用的input指令
a way for me to ask for input from a user,
让我能向用户请求输入
and then I can take that input and
然后我就用这个输入
store it back inside a variable.
存储到一个变量中
Once you start having multiple lines of output displaying on the screen,
当你有多行想输出到屏幕
you may want those blank lines.
你可能想要空行
So at anytime you can do something like
所以你可以这样写
“Print, hello,” and then I can just say “Print, world”
print(‘hello’)接着是print(‘world’)
and you will notice those appear on different lines,
你会看到这两条指令是出现在不同行的
so you can see hello world appear in different lines.
所以你可以看到“hello world”出现在不同行
I’m going to show you another useful little command
现在我要讲另外一个有用的小指令
in Visual Studio Code here,
在VS Code里面
we’re starting to get to the bottom of the command line output.
我们在命令栏输出的最底下面
So it just typing the word “CLS,” we’ll clear
只需要输入“CLS”
the screen and take us back to the top of this output here.
就会清空输出显示 重新回到命令栏顶部
Useful little thing when you’ve got
另一个有用的是
a lot of outputs displaying on the screen.
当你有许多要输出在屏幕上
Another way of putting output over multiple lines is
将这些输出分成多行的另一种方式
you can use that slash n. Slash n is an escape character that means,
你可以使用“\n” \n是转义字符
please insert a new line.
意思是换行
So that will also display a line of code over two lines of output.
所以它会这行代码分成两行输出
So slash n is a way of saying new line, right inside a string.
在字符串右边插入\n就是要换行
This is really handy when used when
这用起来很方便
displaying output to use on a console application,
当在控制台应用中显示输出的时候
but as I mentioned, print is also very
我也说过 print也是一种
popular for coders trying to debug their code.
程序员常常用来调试的方法
So if I was writing some code that was doing some math,
所以如果我写一些数学运行的代码
x equals 532 plus 12,
x等于532加12
and then I say y equals x divided by 0,
然后y等于x除以0
now I know that dividing by 0 is going to cause an error,
现在我知道除以0会发生错误
and then I say “Print, yay I did math.”
然后我写print(‘yay i did math’)
So I write a very simple little program here,
这里我写了一个非常简单的程序
and I save it, and I go down,
保存 接下来
and I run that program,
我要运行这个程序
and it blows up.
然后它报错了
Now, I’m using a very simplified example.
现在这是个很简短的例子
You can probably look at my code
你可以仔细查看我的代码
and guess which line of code blowed up.
猜是哪行代码有问题
It’s the divide by 0.
是因为除以了0
But what if this was inside a 500,
但是如果这里有500行
600 line program with multiple modules and functions,
或者600行 还有许多模块和函数
sometimes it’d be trough to track
可能有时候就很难去追踪到
down where that line is blowing up,
出现错误的那一行
even though in the error message it
即使报错的信息中
does give you a line number which gives you a hint.
它确实提示了你有问题的行数
So sometimes when I’m trying to debug my code,
所以有时候我要调试我的代码
I will literally write things like
我就会直接写一些东西
saying ”Adding numbers,” sorry,
像”Adding number”
promised I would use single quotes here,
等下 我想我要在这儿用单引号
‘Adding numbers’, and then I add my numbers,
‘Adding numbers’ 然后我加了一个数
and then I’ll say print, ‘dividing numbers,’ and then I will divide the numbers,
然后这里 print(‘dividing numbers’) 我会除以一个数
and then I have my print statement saying, ‘Yey I did math.’
然后我用 print(‘yay I did math’)
That way, when I go and I run my code,
这样 当我保存运行代码时
you can see that the adding numbers message appeared,
你可以看到有加法的信息
the dividing numbers message appeared,
然后是除法的信息
and then I got an error message.
然后就报错了
So it can help me flow back
所以这可以帮助我追踪
through my code to figure out what code executed successfully,
从代码中找出哪些代码执行成功了
and whichever print statement I didn’t see is
而那些没有出现的print语句
probably the one right after the line that failed.
可能是前面一行代码中有错误
So don’t be afraid to use print statements as a debugging tool,
所以不要害怕使用print语句来做为调试工具
as you’re learning to code.
当你在学习代码的时候

发表评论

译制信息
视频概述

Hello World几乎是每个语言刚开始学习的一个实例,视频中讲述了如果在VS Code中编写python代码以及输入输出的一些基本语句和技巧。

听录译者

收集自网络

翻译译者

eyas闯江湖

审核员

审核员1024

视频来源

https://www.youtube.com/watch?v=wWwr0tDSqnE

相关推荐