【Python3.7學習筆記】三、變數和
- 2020 年 1 月 11 日
- 筆記
【Python3.7學習筆記】三、變數和簡單數據類型
學習筆記目錄
【Python3.7學習筆記】一、環境搭建 【Python3.7學習筆記】二、第一個python程式 【Python3.7學習筆記】三、變數和簡單數據類型 【Python3.7學習筆記】四、列表 【Python3.7學習筆記】五、字典
目錄
變數
- python程式的運行過程 運行hello_world.py時,python都做了什麼?我們分析一下 hello_world.py
print("Hello My Python World!")
運行上面程式碼,輸出:
Hello My Python World!
運行文件hello_world.py時,末尾的py指出這是一個Python程式,編輯器將使用python解釋器運行它。python解釋器讀取整個程式,確定每個單詞的含義。看到單詞print時,解釋器不管括弧中的內容是什麼,都會將括弧中的內容列印到螢幕。
- 重構hello_world程式,使用變數 修改程式碼如下:
message = "Hello My Python World!" print(message)
運行上面程式碼,輸出:
Hello My Python World!
我們定義一個變數message,並給它賦值為Hello My Python World!,然後將它列印到螢幕。
- 繼續重構hello_world程式 程式碼如下: message = "Hello My Python World!" print(message) message = "Hello My Python perfect World!" print(message) 運行上面程式碼,輸出:
Hello My Python World! Hello My Python perfect World!
修改變數的值,最新的值為變數的值
變數的命名和使用規則
- 變數名只能包含字母、數字、下劃線。變數名可以字母和下劃線開頭,不能以數字開頭
- 變數名不能包含空格,可以用下劃線分隔單詞,如:student_name
- 不能將python關鍵字和函數名做變數名
- 變數名應該使用簡單有意義的單詞描述
- 使用小寫字母l和大寫字母O,需要謹慎,它們可能被錯認為數字1和0
使用變數時避免命名錯誤
message = "Hello World!" print(mesage1)
運行上述程式碼時,解釋器會提示一個traceback資訊,指出什麼地方出錯

NameError:name 『message1』 is not defined 變數名沒有定義
字元串
python中,定義:用單引號或雙引號括起來的都是字元串,比如:
# -*- coding: utf-8 -*- message = "我是一個程式猿" print(message) message = '我是一個python程式猿' print(message) message = '我是一個"python"程式猿' print(message) message = "我是一個'程式猿'" print(message) message = "我是一個python`s程式猿" print(message)
運行結果:

字元串相關函數
- title()以首字母大小的方式顯示每個單詞
- upper()將字元串全部改為大寫
- lower()將字元串全部改為小寫
- rstrip()去掉右邊空格
- lstrip()去掉左邊空格
- strip()去掉左右空格 註:字元串相關函數比較多,以後單開一個文章詳細介紹
拼接字元串
python使用+號來拼接字元串
使用製表符或換行符添加空白
- 製表符t
- 換行符n 案例:
name = "ada lovelace" print(name.title()) name = "Ada" print("Ada=="+name) print("Ada lower()=="+name.lower()) print("Ada upper()=="+name.upper()) print(name.lower().title()) name = "ADA" print("ADA=="+name) print("ADA lower()=="+name.lower()) print("ADA upper()=="+name.upper()) print(name.lower().title()) name = "ada" print("ada=="+name) print("ada lower()=="+name.lower()) print("ada upper()=="+name.upper()) print(name.lower().title()) # title()以首字母大寫的方式顯示每個單詞,即將每個單詞的首字母都改為大寫 # lower()將單詞的所有字母變成小寫字母 # upper()將單詞的所有字母變成大寫字母 # 字元串使用+拼接 first_name = "ada" last_name = "lovelace" full_name = first_name +" "+last_name print(full_name) # 製表符t或換行符n添加空白 print("python") print("tPython") print("Language:nPythonnCnJavaScript") print("Language:ntPythonntCntJavaScript") # 刪除空白 favorite_language = ' Python ' print(favorite_language) # 兩端刪除空白 print(favorite_language.strip()) # 左側刪除空白 print(favorite_language.lstrip()) # 右側刪除空白 print(favorite_language.rstrip())
數字
整數
python中,可對整數執行加減乘除運算
- 加減乘除
>>> 1+1 2 >>> 4-2 2 >>> 2*4 8 >>> 3/2 1.5
- 乘方運行 ** ,m**n表示:m的n次方
>>> 2**2 4 >>> 2**3 8 >>> 2**4 16 >>> 2**5 32 >>> 2**6 64 >>>
- 運算次序,乘除加減,括弧優先等
>>> 2+3*4 14 >>> (2+3)*4 20 >>>
浮點數
python將帶小數點的數字稱為浮點數,浮點數運算
>>> 0.1+0.1 0.2 >>> 0.2+0.2 0.4 >>> 2*0.1 0.2 >>> 2*0.3 0.6 >>>
浮點數小數位數不確定
>>> 0.1+0.2 0.30000000000000004 >>> 3*0.1 0.30000000000000004 >>>
使用函數str()避免類型錯誤
# 使用函數str()避免類型錯誤 age = 23 #1 message = "Happy " + age + "rd Birthday!" message = "Happy " + str(age) + "rd Birthday!" print(message)
如果使用#1處的程式碼,python會提示錯誤:

- 使用str()函數,將數值23轉換為字元串
注釋
如果編寫注釋
python中,注釋用#標識,python解釋器會忽略#後面的內容
# 我是一個python程式猿 message = '我是一個python程式猿' print(message)
該編寫什麼樣的注釋
編寫注釋的目的描述程式碼做什麼,如何做。盡量編寫有意義的注釋
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!
歡迎大家一起交流討論