1. 程式人生 > >Python3學習之路1

Python3學習之路1

Python簡介

  python是吉多·範羅蘇姆發明的一種面向物件的指令碼語言,可能有些人不知道面向物件和指令碼具體是什麼意思,但是對於一個初學者來說,現在並不需要明白。大家都知道,當下全棧工程師的概念很火,而Python是一種全棧的開發語言,所以你如果能學好Python,那麼前端,後端,測試,大資料分析,爬蟲等這些工作你都能勝任。

為什麼選擇Python

  關於語言的選擇,有各種各樣的討論,在這裡我不多說,就引用Python裡面的一個彩蛋來說明為什麼要選擇Python,在Python直譯器裡輸入import this 就可以看到。

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
>>>

安裝Python

比較簡單直接看連線,不在過多描述

http://www.runoob.com/python/python-install.html

編碼

預設情況下,Python 3 原始碼檔案以 UTF-8 編碼,所有字串都是 unicode 字串。 字元編碼的知識可以看這篇文章

變數

  1. 變數名只能是 字母、數字或下劃線的任意組合
  2. 變數名的第一個字元不能是數字
  3. 變數對大小寫敏感
  4. 以下關鍵字不能宣告為變數名
>>> print(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', 'retu
rn', 'try', 'while', 'with', 'yield']

註釋

Python中單行註釋以 # 開頭,例項如下:

#!/usr/bin/python3
# 第一個註釋
print ("Hello, Python!") 
# 第二個註釋

多行註釋可以用多個 # 號,還有 ''' 和 """:

"""
第五註釋
第六註釋
"""
print ("Hello, Python!")

行與縮排

python最具特色的就是使用縮排來表示程式碼塊,不需要使用大括號 {} 。

縮排的空格數是可變的,但是同一個程式碼塊的語句必須包含相同的縮排空格數。例項如下:

if True:
    print ("True")
else: 
    print ("False")

多行語句

Python 通常是一行寫完一條語句,但如果語句很長,我們可以使用反斜槓()來實現多行語句,例如:

total = item_one + \
item_two + \
item_three

在 [], {}, 或 () 中的多行語句,不需要使用反斜槓(),例如:

total = ['item_one', 'item_tw
o', 'item_three', 
'item_four', 'item_five']