“你好,世界!”Python教程
介绍“你好,世界!” ( 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, a 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 t o the program. The first word that comes after the program's name when the program is executed is assigned using the . The second word (addressee) is sys.argv[2] and so on.The program's name itself is sys.argv[0].
在这⾥,我们将第⼀个命令⾏参数的值``问候''给程序。 执⾏程序时,在程序名称后的第⼀个单词是使⽤分配的。 第⼆个单词(收件⼈)是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, . 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()python新手代码你好
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 lse 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!
你好,世界!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。