跳至内容

面向非程序员的 Python 2.6 教程/入门

来自维基教科书,开放的书籍,面向开放的世界

简单程序

[编辑 | 编辑源代码]

Python 语言非常简单,即使是非程序员也能理解。以下是一些示例

 print "Hello, World!"

此程序在屏幕上打印“Hello, World!”。即使是更长的程序也能轻松理解

  # Stuff after number signs are comments that have no functionality
  # They are useful for explaining how things work though.
 name = raw_input("What is your name?")  # Asks the person using the program what their name is
  if name == "John":                     # If the person types John and presses enter
       print "Hello, John! How are you?" # The program prints Hello, John! How are you?
  elif name == "Teddy":                  # If the person types Teddy and presses enter
       print "Go away!"                  # The Program prints Go Away!
  else:                                  # If the person types any thing else and the presses enter
       print "Good day," , name          # The Program prints Good day followed by what the person typed

奇怪的功能

[编辑 | 编辑源代码]

Python 的一些奇怪功能包括使用缩进来表示分组。例如,

 if value == input:
    do this

 if value == input:
  do this

不同。相反,一些语言更喜欢用这种方式编写代码

 {if value
     == input[d
   o
       this
    ]

在其他语言中,你可以以任何你想要的方式组织它。在 Python 中则不同。

华夏公益教科书