python你好世界_你好,世界! Python方式

python你好世界

by Thomas Noe

通过托马斯·诺

你好,世界! Python方式 (Hello World! The Pythonic way)

你好,世界 (Hello world)

The first program developers are often introduced to is the infamous Hello World. It doesn’t matter what language you’re using, you have probably seen one. If not in a tutorial, than out in the wild.

臭名昭著的Hello World经常被介绍为第一个程序开发人员。 使用哪种语言都没有关系,您可能已经看过一种语言。 如果不在教程中,那将是无用的。

为什么 (The why)

This post is to celebrate Free Code Camp expanding towards supporting Python, among other cool languages. Check out the announcement for yourself.

这篇文章是为了庆祝Free Code Camp扩展为支持Python,以及其他很酷的语言。 亲自查看公告

Note that this post isn’t meant to be a tutorial for brand new programmers. I have included links to help readers get started with Python.

请注意,本文并非旨在为全新的程序员提供教程。 我提供了一些链接来帮助读者开始使用Python。

给我看一些代码 (Show me some code)

Enough talk. Let’s check out the way you would write Hello World in Python. Deep breath now. And off we go.

聊够了。 让我们看看用Python编写Hello World的方式。 现在深呼吸。 然后我们走。

Python3 (Python3)
print('Hello World!');

Fascinating right? Those of you who are used to JavaScript might not be very impressed. The JS Hello World example wouldn’t be much different.

令人着迷吧? 那些熟悉JavaScript的人可能不会留下深刻的印象。 JS Hello World示例不会有太大不同。

JavaScript (JavaScript)
console.log('Hello World!');
Ruby (Ruby)

Ruby’s is in the same ballpark

露比在同一个球场

puts "Hello World!"

To put the simplicity of these into context let’s look at another two examples.

为了将这些简单性放在上下文中,让我们看另外两个示例。

C (C)
#include <stdio.h>
int main(int argc, char* argv[]){    printf("Hello World!\n");    return 0;}
Java (Java)
public class HelloWorld {    public static void main(String[] args) {        System.out.prinln("Hello World!");    }}

There has been a shift in the last few years where the programming community has started to lean towards the prior three languages as introductory languages over the latter two. Perhaps these Hello World’s give you a small taste of why. What do you think?

过去几年发生了变化,编程社区开始倾向于使用前三种语言作为后两种语言的入门语言。 也许这些“ Hello World”让您对其中的原因有所了解。 你怎么看?

Okay, back to Python.

好的,回到Python。

那Python性的东西呢? (What about this Pythonic thing?)

I will use this last section to skim the surface of what the word Pythonic is and we will look at a Pythonic Hello World.

我将在最后一节中略过Pythonic一词的含义,然后我们来看一个Pythonic Hello World。

Pythonic到底是什么? (What the hell is Pythonic?)

When people think about this question, they may think of Python’s famous

当人们想到这个问题时,他们可能会想到Python的著名

import this

example. Which when ran will give you this:

例。 哪个运行时会给您以下信息:

Beautiful is better than ugly.Explicit is better than implicit.Simple is better than complex.Complex is better than complicated.Flat is better than nested.Sparse is better than dense.Readability counts.Special cases aren't special enough to break the rules.Although practicality beats purity.Errors should never pass silently.Unless explicitly silenced.In the face of ambiguity, refuse the temptation to guess.There should be one-- and preferably only one --obvious way to do it.Although that way may not be obvious at first unless you're Dutch.Now is better than never.Although never is often better than *right* now.If the implementation is hard to explain, it's a bad idea.If the implementation is easy to explain, it may be a good idea.Namespaces are one honking great idea -- let's do more of those!

Take from that what you will. Let’s focus on one line from the text.

从中得到什么。 让我们专注于文本中的一行。

There should be one-- and preferably only one --obvious way to do it.
应该有一种-最好只有一种-显而易见的方法。

To me this line describes the mentality behind the word Pythonic and idiomatic Python.

对我来说,这行内容描述了Pythonic和惯用Python一词的思路。

If you’re falling asleep at the keyboard, at least add this to your reading list.

如果您要在键盘上入睡,请至少将其添加到阅读列表中

难道不总会有“一种方法”吗? (Shouldn’t there always be ‘one way to do it?’)

That’s up to you. Despite what language you use. Let’s look at an example from the Perl community (which Ruby has inherited.)

随你(由你决定。 尽管您使用哪种语言。 让我们看一下Perl社区的一个示例(Ruby继承了该示例。)

There’s more than one way to do it (TMTOWTDI or TIMTOWTDI, pronounced Tim Toady)

不止一种方法去做一件事 (TMTOWTDITIMTOWTDI,发音添哈巴狗 )

There's more than one way to do it - Wikipedia, the free encyclopediaThere's more than one way to do it ( TMTOWTDI or TIMTOWTDI, pronounced Tim Toady) is a Perl programming motto. The…en.wikipedia.org

有多种方法可以做到这一点 -Wikipedia, 免费的百科全书库 有多种方法可以做到这一点(TMTOWTDI或TIMTOWTDI,发音为Tim Toady)是Perl编程的座右铭。 该… en.wikipedia.org

(TIL there’s a pronunciation!)

(直到有一个发音!)

返回代码 (Back to the code)

Let’s skip the rest of the philosophy lesson and dive into the Pythonic Hello World code example. I’m going to include a very basic function (oh my!) so it’s not so confusing when we look at the lines.

让我们跳过其余的哲学课程,并深入研究Pythonic Hello World代码示例。 我将包含一个非常基本的功能( 哦,我的! ),因此当我们查看这些行时,它不会太令人困惑。

# is how you start a Python comment

#是开始Python注释的方式

# section onedef main():  print("Hello World!")
# section two
if __name__ == "__main__":  main()

Okay?

好的?

撕下 (Tear it down)

Section one

第一节

def main():  print("Hello World!")

define a function that takes no arguments and doesn’t return any value named main

定义一个不带参数且不返回名为main的值的函数

print Hello World! to the console when main is called

打印Hello World! 调用main时转到控制台

Section two

第二节

if __name__ == "__main__":  main()

__name__ is assigned to the calling module…

__name__已分配给调用模块…

In short:

简而言之:

  • if the module is imported __name__ will be the set to the importing module

    如果模块是导入的__name__将设置为导入模块
  • if the file is directly ran then execute the if statement

    如果文件直接运行,则执行if语句

Let’s look at one more modified example before we wrap this up

在结束之前,让我们看另一个修改的示例

# fcc-greet.py
def greet(name):  print("Hello {}, welcome to Free Code Camp!".format(name))
if __name__ == "__main__":  from sys import argv  greet(argv[1]) # first command argument

The print statement and the last line may be a little much for some newer users. Instead of explaining them I’m going to show you two different ways to use our new Python program.

对于一些较新的用户,打印语句和最后一行可能会有点过多。 我将向您展示两种不同的方式来使用我们的新Python程序,而不是解释它们。

The first is through the terminal/command prompt:

第一种是通过终端/命令提示符:

$ python fcc-greet.py t3h2mas

which prints this to the console

将其打印到控制台

Hello t3h2mas, welcome to Free Code Camp!
您好t3h2mas,欢迎来到免费代码营!

Using `fcc-greet.py` as a module:

使用`fcc-greet.py`作为模块:

# my-program.py
import fcc-greet
users = ["t3h2mas", "BoilingOil", "mamptecnocrata"]map(fcc-greet.greet, users)

thank you to the above users for their permission to use their username :+1:

感谢上述用户允许使用其用户名:+1:

which would output

将输出

Hello t3h2mas, welcome to Free Code Camp!
您好t3h2mas,欢迎来到免费代码营!
Hello BoilingOil, welcome to Free Code Camp!
您好BoilingOil,欢迎来到免费代码营!
Hello mamptecnocrata, welcome to Free Code Camp!
您好mamptecnocrata,欢迎来到免费代码营!

That last example might have a little much going on. Just focus on the output!

最后一个例子可能有很多事情要做。 只专注于输出!

That completes our example program using Pythonic idioms. We finished with a program that can be called from the prompt with a supplied argument as well being used as a module easily from different programs.

这就完成了使用Pythonic习语的示例程序。 我们完成了一个程序,该程序可以从提示符处通过提供的参数来调用,也可以从其他程序轻松用作模块。

结语 (wrap up)

This concludes our small taste of idiomatic Python. This post was intended to be supplementary reading rather than a full scope tutorial. The Python community sure knows what it likes. See

这总结了我们对惯用Python的一些小品味。 本文旨在作为补充阅读,而不是完整的教程。 Python社区肯定知道它喜欢什么。 看到

pep8 Python style guide

pep8 Python样式指南

Welcome to Python.orgThis document gives coding conventions for the Python code comprising the standard library in the main Python…www.python.org

欢迎使用Python.org。 该文档提供了Python代码的编码约定,该代码由主Python中的标准库组成... www.python.org

pep257

pep257

Welcome to Python.orgThe aim of this PEP is to standardize the high-level structure of docstrings: what they should contain, and how to say…www.python.org

欢迎来到Python.org 此PEP的目的是标准化docstring的高级结构:它们应包含的内容以及怎么说的方式 。www.python.org

for more on Pythonic guides.

有关Pythonic指南的更多信息。

刚接触Python? (new to Python?)

This looks like a good starting point

这看起来是一个很好的起点

Getting started with python - The Python GuruPython is a general purpose programming language created by Guido Van Rossum. Python is most praised for its elegant…thepythonguru.com

python入门-Python Guru Python是Guido Van Rossum创建的通用编程语言。 Python因其优雅而广受赞誉... thepythonguru.com

Here’s a great list of tutorials…

这里有很多教程……

For programmers:

对于程序员:

BeginnersGuide/Programmers - Python WikiBecause this is a Wiki page, users can edit it. You are therefore free to add details of material that other Python…wiki.python.org

BeginnersGuide / Programmers-Python Wiki 因为这是Wiki页面,所以用户可以对其进行编辑。 因此,您可以自由添加其他Python… wiki.python.org 的材料的详细信息。

For beginners

对于初学者

BeginnersGuide/NonProgrammers - Python WikiIf you've never programmed before, the tutorials on this page are recommended for you; they don't assume that you have…wiki.python.org

BeginnersGuide / NonProgrammers-Python Wiki 如果您以前从未编程过,推荐您使用本页上的教程; 他们不认为您有… wiki.python.org

Python社区 (Python Communities)

Reddit:

Reddit:

Python Education * /r/learnpythonSubreddit for posting content, questions, and asking for general advice about learning the Python programming language.reddit.comPython * /r/Pythonnews about the dynamic, interpreted, interactive, object-oriented, extensible programming language Pythonreddit.comlearn programming * /r/learnprogrammingA subreddit for all questions related to programming in any language.reddit.com

Python Education * / r / learnpython Subreddit,用于发布内容,问题并寻求有关学习Python编程语言的一般建议。 reddit.com Python * / r / Python 有关动态,解释性,交互式,面向对象,可扩展编程语言的新闻Python reddit.com 学习编程* / r / learnprogramming 针对与任何语言编程相关的所有问题的 子目录 reddit.com

Gitter:

金葱:

FreeCodeCamp/FreeCodeCampWelcome to our main chat room. We have many official chat rooms for hanging out and getting help. Here's the list…gitter.imFreeCodeCamp/pythonThis is the best place to discuss Python and get help with it. Be sure to check out https://github.com/freecodecamp…gitter.im

FreeCodeCamp / FreeCodeCamp 欢迎来到我们的主要聊天室。 我们有许多官方聊天室供您闲逛并获得帮助。 这是列表... gitter.im FreeCodeCamp / python 这是讨论Python并获得帮助的最佳位置。 请务必查看https://github.com/freecodecamp…gitter.im

IRC:

IRC:

Python.org -IRCGuideThe official home of the Python Programming Languagewww.python.org

Python.org -IRCGuide Python编程语言的官方主页 www.python.org

翻译自: https://www.freecodecamp.org/news/hello-world-the-pythonic-way-ea006c56038c/

python你好世界

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值