跳至內容

面向非程式設計師的 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 中則不同。

華夏公益教科書