Python學習之路1-變數和簡單資料型別

VPointer發表於2018-05-29

《Python程式設計:從入門到實踐》筆記

本章主要介紹Python的基本資料型別以及對這些資料的簡單操作。

1. 入門儀式

作為一個合格的程式設計師,必須精通各種語言的Hello, World!,以下是學習Python的第一段程式碼:

print("Hello, World!")
複製程式碼

2. 變數

變數就是資料的別稱,和數學上的變數類似。 例如上述程式碼用變數表示:

message = "Hello, World!"
print(message)
複製程式碼

變數有一定的命名規則:

  • 變數名只能包含字母、數字和下劃線,且不能以數字開頭
  • 變數名不能包含空格,一般用下劃線分隔變數中的單詞,也可以用駝峰命名法,但Python提倡用下劃線
  • Python中的關鍵字和自帶函式不能用於變數名
  • 變數名應該簡短明瞭
  • 慎用小寫字母 l 和大寫字母O,因為這兩個字母容易被看成數字1和0

同時也請注意,Python直譯器不對程式碼進行拼寫檢查,應儘量避免命名錯誤,比如變數名中少寫個字母之類的,否則會出現NameError

3. 字串

字串就是一系列被引號括起來的字元,在Python中,引號可以是單引號,也可以是雙引號,還可以是三引號。單雙引增加了Python字串的靈活性,減少了轉義字元的使用,比如字串中有且只有單引號時,最外層可以用雙引號,反之亦然。三引號主要用於字串是多行的情況,同時它也常用於註釋。例子如下:

"This is a string."
'This is also a string.'

'I told my friend, "Python is my favorite language!"'
"The language 'Python' is named after Monty Python, not the snake."
"One of Python's strengths is its diverse and supportive community."

"""
this is line 1;
this is line 2.
"""

'''
this is line 1;
this is line 2.
'''
複製程式碼

注意,若字串中出現了和最外層引號相同的引號時,會出現SyntaxError

字串首字母大寫: 字串中每個單詞首字母大寫:

# 程式碼:
name = "ada lovelace"
print(name.title())

# 結果:
Ada Lovelace
複製程式碼

字串全部大寫和小寫

# 程式碼:
name = "Ada Lovelace"
print(name.upper())
print(name.lower())

# 結果:
ADA LOVELACE
ada lovelace
複製程式碼

字串拼接: Python中用+號進行字串拼接:

# 程式碼:
first_name = "ada"
last_name = "lovelace"
full_name = first_name + " " + last_name
print(full_name)
print("Hello, " + full_name.title() + "!")

# 結果:
ada lovelace
Hello, Ada Lovelace!
複製程式碼

刪除字串首尾的空白:刪左空白,刪有空白,刪兩端空白

>>> temp = ' python '
>>> temp
' python '
>>> temp.lstrip()
'python '
>>> temp.rstrip()
' python'
>>> temp.strip()
'python'
複製程式碼

4. 數字

特別注意Python中的預設除法:兩個整數相除,如果除不盡,會有小數,而不是隻保留整數(如C/C++, Java, Python2)

#整數
>>> 2 + 3
5
>>> 3 - 2
1
>>> 2 * 3
6
>>> 3 / 2
1.5 #不是1
>>> 3 ** 2 #乘方運算
9
>>> (2 + 3) * 4
20

#浮點數(結果包含的小數位數可能不確定)
>>> 0.1 + 0.1
0.2
>>> 0.2 + 0.1  #和計算機內部數字的表示方法有關
0.30000000000000004
>>> 3 * 0.1
0.30000000000000004
複製程式碼

數字與字串的拼接: 使用str()函式,否則會報TypeError

# 程式碼:
age = 18
message = "Happy " + age + "rd Birthday!"

# 結果:
Traceback (most recent call last):
  File "E:/Code/Python/Study/day1/chapter1.py", line 38, in <module>
    message = "Happy " + age + "rd Birthday!"
TypeError: must be str, not int
複製程式碼

正確語法:

# 程式碼:
age = 18
message = "Happy " + str(age) + "rd Birthday!"
print(message)

# 結果:
Happy 18rd Birthday!
複製程式碼

5. 註釋

Python中的註釋為#號,從#號開始到本行結束的中間這部分均為註釋內容,不會被執行。

# 程式碼:
#Say hello to everyone
print("Hello Python people!")   # Test

# 結果:
Hello Python people!
複製程式碼

6. Python之禪

在python命令列中執行如下程式碼,即可檢視Python社群所推崇的程式碼原則:

>>> 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!
複製程式碼

迎大家關注我的微信公眾號"程式碼港" & 個人網站 www.vpointer.net ~

Python學習之路1-變數和簡單資料型別

相關文章