天天看點

第一章 變量和簡單的數字類型——python

1.1 變量

  • 每個變量都存儲了一個值
  • 在程式中可以随時修改變量,但Python将始終記錄變量的最新值
message = "Hello Huang ZB!"
print(message)

message = "Goodbye Huang ZB!"
print(message)
           

[外鍊圖檔轉存失敗,源站可能有防盜鍊機制,建議将圖檔儲存下來直接上傳(img-103bRHf3-1631544730921)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20210913222032160.png)]

1.1.1 使用變量名時避免命名錯誤

檢視Traceback明白錯誤

message = "Hello Huang ZB!"
print(mesage)
           

[外鍊圖檔轉存失敗,源站可能有防盜鍊機制,建議将圖檔儲存下來直接上傳(img-XNQ3OMRi-1631544730924)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20210913222115160.png)]

1.2 字元串

Def:字元串就是一串字元。雙引号、單引号都可表示

1.2.1 修改字元串大小寫的方法

name = "huang zhibin"
print(name.title())            #title()函數作用:将每個單詞首字母改為大寫
           
Huang Zhibin

其他方法:

name = "huang zhibin"
print(name.title())   #title()函數作用:将每個單詞首字母改為大寫
print(name.upper())   #upper()函數作用:将字元串内容全部轉換為大寫
print(name.lower())   #lower()函數作用:将字元串内容全部轉換為小寫
           

Huang Zhibin

HUANG ZHIBIN

huang zhibin

1.2.2 合并字元串

方法:拼接

first_name = 'huang'
last_name = 'zhibin'
full_name = first_name + ' ' + last_name
print('Hello, ' + full_name.title() + '!')    #這個 + 不可或缺
           
Hello, Huang Zhibin!

1.2.3 使用制表符或換行符來添加空白

  • 在字元串中添加制表符,使用 \t (也可以了解為進位符)
print("python")

print("\tpython")             # \t 表示制表符
           

python

python

  • 在字元串中添加換行符,使用 \n

Languages:

Python

C

JavaScript

  • 同一字元串中可以同時包含制表符和換行符 字元串" \n\t ": 讓python換到下一行

Languages:

Python

C

JavaScript

1.2.4 删除空白

  • python能夠找出字元串開頭和末尾多餘的空白,為確定開末尾無空白,使用方法 rstrip()
  • 為確定開開頭無空白,使用方法 lstrip()
  • 同時剔除字元串兩端的空白,使用方法 strip()
information = '    人生苦短,我學python    '
print(information.rstrip())
print(information.lstrip())
print(information.strip())
           

​ 人生苦短,我學python

人生苦短,我學python #右邊空格依然存在!

人生苦短,我學python

1.2.5 使用字元串時需要避免文法錯誤

再修改程式時文法錯誤也是一個重要的檢查名額

1.3 數字類型

1.3.1 整數

>>> 2+3
5
>>> 5-6
-1
>>> 4*5
20
>>> 36/6
6.0
>>> 3**2
9
>>> 2+2**2
6
>>> (2+2)*2
8
           

1.3.2 浮點數

>>> 0.2+0.3
0.5
>>> 0.2-0.3
-0.09999999999999998
           
  • 保留兩位小數

1.3.3 複數

>>> 2+6j
(2+6j)
>>> (2+6j).real
2.0
>>> (2+6j).imag
6.0
           

1.3.4 使用函數str()避免類型錯誤

age = 21
message = "Happy " + str(age) + "rd Birthday!"     #将非字元串值轉化為字元串
print(message)
           
Happy 21rd Birthday!

1.4 注釋

單行注釋

#注釋内容

多行注釋

‘’’

注釋内容

‘’’

注釋不能嵌套!!!!!

1.5 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!