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你好世界

python你好世界_你好,世界! Python方式相关推荐

  1. python如何连接创建我的世界_我的Python世界 玩《Minecraft我的世界》学Python编程...

    1 Python 基础 1 1.1 Python 的历史 1 1.2 Python 的发展 1 1.3 Python 的优缺点 2 1.3.1 Python 的优点 2 1.3.2 Python 的缺 ...

  2. python的运行方式_详解python运行三种方式

    方式一 交互式编程 交互式编程不需要创建脚本文件,是通过 Python 解释器的交互模式进来编写代码. linux上你只需要在命令行中输入 Python 命令即可启动交互式编程,提示窗口如下: $ p ...

  3. 文科生自学python要多久_怎么自学python,大概要多久?

    都让开!本人文科生,自学Python 2年半,作为一个曾经完全0基础,啥都不懂纯靠自学学会python的文科生,有一些不成熟的小建议可以分享一下. 首先不要觉着编程难,只要你认识26个英文字母,有一点 ...

  4. python append函数_让你python代码更快的3个小技巧!速度提高了一倍还多

    大家好!今天呢,我们来聊一聊如何加速你的 python 代码. Python 语言的优点可以列举出许多,语法简单易懂.模块丰富.应用广泛等等.但是世界上没有有完美的东西,python 一个明显缺点就是 ...

  5. python代码技巧_让你python代码更快的3个小技巧

    大家好!今天呢,我们来聊一聊如何加速你的 python 代码. Python 语言的优点可以列举出许多,语法简单易懂.模块丰富.应用广泛等等.但是世界上没有有完美的东西,python 一个明显缺点就是 ...

  6. python开发中级_针对中级Python开发人员的13个项目构想

    python开发中级 Learning the basics of Python is a wonderful experience. But the euphoria of just learnin ...

  7. python len函数_知识清单Python必备的69个函数,你掌握了吗?

    本文纲要 Python 作为一门高级编程语言,为我们提供了许多方便易用的内置函数,节省了不少开发应用的时间.目前,Python 3.7 共有 69 个内置函数,一些是我们耳熟能详的函数,另一些却不是很 ...

  8. python递归函数例题_递归案例python

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 而对应的中文翻译 "递归" 却表达了两个意思:"递 ...

  9. 浙江python信息技术教材_人工智能、Python…浙江省三到九年级将使用信息技术新修订教材...

    浙江省教研室相关负责人表示,目前根据现行的高中教材,对小学.初中的老教材进行了修订,新教材将于今年9月投入使用,最新的线上教师培训也刚刚结束.扣哒世界作为全球最大的中小学人工智能和Python代码编程 ...

  10. php java python容易入门_先学习Python之后再学习PHP与java会更好吗

    学过python语言之后在去学习PHP javascript 这些编程语言是不是会更好的能快速理解, 有python基础之后,再去学习php语言 理解上 是不是更方便上手, 我现在主要想学习专研 PH ...

最新文章

  1. java 方法不同_java同一个类不同方法间的同步
  2. [YTU]_2434 ( C++习题 商品销售)
  3. Java基础提升篇:equals()方法和“==”运算符
  4. 【深度学习】深入浅出卷积神经网络及实现!
  5. R语言观察日志(part9)--RMarkdown之输出格式
  6. python 生成随机数_python 生成随机数模块random 常用方法总结
  7. Avalonia-.NET 的跨平台 UI 框架
  8. grafana导出pdf监控日报
  9. React 之 JSX
  10. Zabbix监控MySQL工具
  11. WordPress简约mkBlog博客主题模板v2.1
  12. 真解决EasyUi的 select 使用 class=“easyui-combobox“ 样式绑定onSelect/onChange事件
  13. “驱动人生”升级通道传木马,技术分析报告来了
  14. 涨知识--地球自转会影响飞机飞行时间吗?
  15. 小伙用Python 分析了 20 万场吃鸡数据
  16. 初探SEO关键词竞争程度的智能算法思路
  17. 数学建模的基本办法和步骤 ##数模学习1
  18. MCU单片机面试题(1)
  19. 关键词生成原创文章及句子的软件!开发原理分析
  20. 物联网卡能否永久使用

热门文章

  1. 苹果服务器是不是在维修,苹果维修中心玩转这些才是真懂iPhone
  2. [原创]淘宝天猫抢购软件“tbBuyer“
  3. vue3 - composition api
  4. lingo入门教程之二 --- 集合运用
  5. linux服务器安装杀毒软件
  6. 【Java代码之美】 -- Java11新特性解读
  7. 交流电桥———实验原理
  8. 解决win10微软应用商店打不开的问题
  9. [RFID]射频识别技术基础
  10. 小心!QQ和MSN聊天记录也会随时被监控