“你好,世界!” Python教程

本文详细介绍了一个简单的Python程序,从创建“你好,世界!”程序开始,逐步深入到导入模块、分配值、定义类和函数,以及如何执行程序。通过具体示例,文章帮助读者理解Python的基本语法和编程概念。
Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

介绍“你好,世界!” ( Introducing "Hello, World!" )

The simplest program in Python consists of a line that tells the computer a command. Traditionally, the first program of every programmer in every new language prints "Hello, World!" Start up your favorite text editor and save the following in a file:

Python中最简单的程序由告诉计算机命令的一行组成。 传统上,每种程序员使用每种新语言编写的第一个程序都将打印“ Hello,World!”。 启动您喜欢的文本编辑器,并将以下内容保存在文件中:

print "Hello, World!"

To execute this program, save it with a suffix of .py—HelloWorld.py—and type "python" and the filename in a shell like this:

要执行此程序,请保存后缀.py(HelloWorld.py),然后在外壳程序中键入“ python”和文件名,如下所示:

> python HelloWorld.py

The output is predictable:

输出是可预测的:

Hello, World!
你好,世界!

If you prefer to execute it by its name, instead of as an argument to the Python interpreter, put a bang line at the top. Include the following on the first line of the program, substituting the absolute path to the Python interpreter for /path/to/python:

如果您希望按其名称执行它,而不是作为Python解释器的参数来执行,请在顶部放置一个爆炸行。 在程序的第一行包含以下内容,用/ path / to / python替换Python解释器的绝对路径:

#!/path/to/python

Be sure to change the permission on the file to allow execution if necessary for your operating system.

确保更改文件权限,以在操作系统必要时允许执行。

Now, take this program and embellish it a bit.

现在,使用该程序并对其进行修饰。

导入模块和分配值 ( Importing Modules and Assigning Values )

First, import a module or two:

首先, 导入一两个模块

import re, string, sys

Then let's define the addressee and the punctuation for the output. These are taken from the first two command line arguments:

然后,为输出定义收件人和标点符号。 这些取自前两个命令行参数:

greeting = sys.argv[1]
addressee = sys.argv[2]
punctuation = sys.argv[3]

Here, we give "greeting" the value of the first command-line argument ​to the program. The first word that comes after the program's name when the program is executed is assigned using the sys module. The second word (addressee) is sys.argv[2] and so on.The program's name itself is sys.argv[0].

在这里,我们将第一个命令行参数的值``问候''给程序。 执行程序时,在程序名称后的第一个单词是使用sys模块分配的。 第二个单词(收件人)是sys.argv [2],依此类推。程序的名称本身是sys.argv [0]。

一类叫轻浮 ( A Class Called Felicitations )

From this, create a class called Felicitations:

从此,创建一个名为Felicitations的类:

class Felicitations(object):
def __init__(self):
self.felicitations = [ ]
def addon(self, word):
self.felicitations.append(word)
def printme(self):
greeting = string.join(self.felicitations[0:], "")
print greeting

The class is based on another type of object called "object." The first method is mandatory if you want the object to know anything about itself. Instead of being a brainless mass of functions and variables, the class must have a way of referring to itself. The second method simply adds the value of "word" to the Felicitations object. Finally, the class has the ability to print itself via a method called "printme."

该类基于另一种称为“对象”的对象。 如果您希望对象了解有关自身的任何信息,则第一种方法是强制性的。 该类不是笨手笨脚的函数和变量,而必须具有一种引用自身的方式。 第二种方法只是将“单词”的值添加到Felicitations对象中。 最后,该类具有通过称为“ printme”的方法进行打印的能力。

Note: In Python, indentation is important. Every nested block of commands must be indented the same amount. Python has no other way to differentiate between nested and non-nested blocks of commands.

注意:在Python中, 缩进很重要 。 每个嵌套的命令块都必须缩进相同的数量。 Python没有其他方法可以区分嵌套和非嵌套命令块。

定义功能 ( Defining Functions )

Now, make a function that calls the last method of the class:

现在,创建一个调用该类的最后一个方法的函数:

def prints(string):
string.printme()
return

Next, define two more functions. These illustrate how to pass arguments to and how to receive output from functions. The strings in parentheses are arguments on which the function depends. The value returned is signified in the "return" statement at the end.

接下来,再定义两个函数。 这些说明了如何将参数传递给函数以及如何从函数接收输出。 括号中的字符串是函数所依赖的参数。 返回的值在末尾的“ return”语句中表示。

def hello(i):
string = "hell" + i
return string
def caps(word):
value = string.capitalize(word)
return value

The first of these functions take an argument "i" which is later concatenated to the base "hell" and returned as a variable named "string." As you see in the main() function, this variable is hardwired in the program as "o," but you could easily make it user-defined by using sys.argv[3] or similar.

这些函数中的第一个使用参数“ i”,该参数随后连接到基本“ hell”并作为名为“ string”的变量返回。 如在main()函数中看到的那样,该变量在程序中被硬连线为“ o”,但是您可以使用sys.argv [3]或类似名称轻松地将其定义为用户定义。

The second function is used to capitalize the parts of the output. It takes one argument, the phrase to be capitalized, and returns it as a value "value."

第二个函数用于大写输出的各部分。 它接受一个参数,即要大写的短语,并将其作为值“ value”返回。

Main()事物 ( The Main() Thing )

Next, define a main() function:

接下来,定义一个main()函数:

def main():
salut = Felicitations()
if greeting != "Hello":
cap_greeting = caps(greeting)
else:
cap_greeting = greeting
salut.addon(cap_greeting)
salut.addon(", ")
cap_addressee = caps(addressee)
lastpart = cap_addressee + punctuation
salut.addon(lastpart)
prints(salut)

Several things happen in this function:

此函数中发生了几件事:

  1. The code creates an instance of the Felicitations class and call it "salut," which allows access to the parts of Felicitations as they exist in salut.

    该代码创建Felicitations类的实例并将其称为“ salut”,它允许访问salut中存在的Felicitation的各个部分。
  2. Next, if "greeting" does not equate to the string "Hello," then, using function caps(), we capitalize the value of "greeting" and assign it to "cap_greeting." Otherwise, "cap_greeting" is assigned the value of "greeting." If this seems tautological, it is, but it is also illustrative of conditional statements in Python.

    接下来,如果“ greeting”不等于字符串“ Hello”,则使用功能caps()将“ greeting”的值大写并将其分配给“ cap_greeting”。 否则,为“ cap_greeting”分配“ greeting”的值。 如果这看起来是重言式的,那是对的,但这也说明了Python中的条件语句。
  3. Whatever the outcome of the if...else statements, the value of "cap_greeting" is added onto the value of "salut," using class object's append method.

    无论if ... else语句的结果如何,都使用类对象的append方法将“ cap_greeting”的值添加到“ salut”的值上。
  4. Next, we append a comma and a space to salut in preparation for the addressee.

    接下来,我们在逗号和空格之间打招呼,以准备收件人。
  5. The value of "addressee" is capitalized and assigned to "cap_addressee."

    “收件人”的值被大写并分配给“ cap_addressee”。
  6. The values of "cap_addressee" and "punctuation" are then concatenated and assigned to "lastpart."

    然后将“ cap_addressee”和“标点”的值连接起来并分配给“ lastpart”。
  7. The value of "lastpart" is then appended to the content of "salut."

    然后,将“ lastpart”的值附加到“ salut”的内容中。
  8. Finally, the object '"salut" is sent to the "prints" function to be printed to the screen.

    最后,将对象“ salut”发送到“打印”功能以打印到屏幕上。

用弓绑起来 ( Tying It Up With a Bow )

Alas, we are not done yet. If the program is executed now, it would end with no output whatsoever. This is because the function main() is never called. Here is how to call main() when the program is executed:

las,我们还没有完成。 如果程序现在执行,它将以任何输出结束。 这是因为从未调用过函数main()。 这是在程序执行时如何调用main()的方法:

if __name__ == '__main__':
main()

Save the program as "hello.py" (without the quotes). Now, you can start the program. Assuming the Python interpreter is in your execution path, you can type:

将程序另存为“ hello.py”(不带引号)。 现在,您可以启动该程序。 假设Python解释器在您的执行路径中,则可以键入:

python hello.py hello world !

and you will be rewarded with the familiar output:

您将获得熟悉的输出:

Hello, World!
你好,世界!

翻译自: https://www.thoughtco.com/quick-tutorial-on-python-2813561

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值