I Love Python. I Learn Python. I Teach Python. I Am a Python.

2014/07/11

Writing a Python-3 program in Chinese. 用中文寫 Python-3 程式。

Have you ever tried to write a program in non-English which is your own native language, like Chinese?

In addition to 30 keywords or so, Python-3 allows you to use non-English characters as identifiers. That means you can write a program almost in your own native language, which is usually the one you feel the most fluent to speak and to THINK.

It is an interesting option for me, I enjoy it, and love it. I suggest you to give it a try.

There are several discussion about this idea as follows:


A bunch of examples are here.




有沒有試過用 中文 寫程式?

Python-3 除了 約 30個 關鍵字(keywords) (列在文後當參考)不可用中文之外,
其他部分允許程式員使用中文。
(其實任何使用 Unicode 編碼的語言皆可,
我偶爾也會用一下希臘字母,或日文假名。)

中文 是我最流利(fluent)的語言,
也是我的華人朋友們(包含大朋友、小朋友以及老朋友)最易懂的語言,
用中文來與這些朋友們溝通(包括我自己的自言自語),
是最有效率、最不易誤解的語言。

誠意推薦你也來試試看。
會有意想不到的收穫喔!


先提供一個例子如下:






印= print

number= 10
print("This is a number: ", number)

theta= 90
print("This is an angle: ", theta)

數= 10
印("這是一個數: ", 數)

θ= 90
印("這是一個角度: ", θ)

def count_to(a_number):
    n=1
    while n <= a_number:
        print(n)
        n = n+1

def 算到(一個數):
    n= 1
    while n <= 一個數:
        印(n)
        n= n+1

count_to(10)

算到(10)

>>> 
>>> 
>>> number= 10
>>> print("This is a number: ", number)
This is a number:  10
>>> theta= 90
>>> print("This is an angle: ", theta)
This is an angle:  90
>>> def count_to(a_number):
    n=1
    while n <= a_number:
        print(n)
        n = n+1

        
>>> count_to(10)
1
2
3
4
5
6
7
8
9
10
>>> 

>>> 
>>> 印= print
>>> 數= 10
>>> 印("這是一個數: ", 數)
這是一個數:  10
>>> θ= 90
>>> 印("這是一個角度: ", θ)
這是一個角度:  90
>>> def 算到(一個數):
    n= 1
    while n <= 一個數:
        印(n)
        n= n+1

        
>>> 算到(10)
1
2
3
4
5
6
7
8
9
10
>>> 

再提供一個 複雜一點的 龜作圖 (Turtle graphics) 例子如下:
from turtle import *

sayHello= 'Hello, World!'

print(sayHello)

for i in range(100):
    forward(100)
    left(170)

penup()
goto(0, 100)

pendown()
write(sayHello)

mainloop()
        
from turtle_tc import *

問候語= '你好,全世界!'

印(問候語)

for i in 範圍(100):
    前進(100)
    左轉(170)

提筆()
前往(0, 100)

下筆()
寫(問候語)

主迴圈()
        
from turtle import *

def turtleDraw(*x, **y):
   
    clear()
    shape('turtle')
    
    goto(0,0)
    setheading(0)
    stamp()
    write('Hello, world!')
    
    pendown()
    for i in range(100):
        forward(100)
        left(100)
    penup()
    
    onscreenclick(turtleDraw)

onscreenclick(turtleDraw)
from turtle_tc import *

def 龜畫圖(*x, **y):
   
    清除()
    形狀(龜形)
    
    前往(0,0)
    設頭向(0)
    蓋印()
    寫('你好,世界!')
    
    下筆()
    for i in 範圍(100):
        前進(100)
        左轉(100)
    提筆()
    
    在點擊幕時(龜畫圖)

在點擊幕時(龜畫圖)






執行本程式需要 turtle 模組,
turtle.py 已經包含在 Python 原裝環境之中,無須額外安裝。
執行本程式需要 turtle_tc 模組,
You can get turtle_tc.py here.
https://github.com/renyuanL/pythonTurtleInChinese
最簡單的 安裝 (installation是把它與你的程式 放在同一目錄 (put it in the same directory) 即可。
Python keywords are not considered to translate. They are usually short, concise and not many.

Python 關鍵字(keywords) 約 30個, 它們都有特定的 語法 功能, 因此我們不把它們中文化,列表如下:
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
>>> len(keyword.kwlist)
33
>>> 

In addition to these keywords, use as many non-English (Chinese) words as you like. Sometimes, even Greek characters (αβγδ) or Japanese kanas (あいうえお) will appear in my programs.

除此之外, 盡量用中文吧! 中英夾雜也無不可。 如果偶爾穿插幾個希臘字母 αβγδ πθμσ ΣΨΓΔ 若有助於閱讀,也無傷大雅。


沒有留言:

張貼留言