上一篇:python入門引導(dǎo)(一·初識(shí))
python基礎(chǔ)知識(shí):語法,變量,數(shù)據(jù)類型
一·語法
縮進(jìn)
python語法優(yōu)美,跟其他語言最大的區(qū)別就是,它使用嚴(yán)格的縮進(jìn)方式來區(qū)分代碼塊,從而保證python代碼風(fēng)格統(tǒng)一,在閱讀上不會(huì)像C,C++,JAVA等語言一樣大括號(hào)層次閱讀不是那么清爽。
下面是一個(gè)簡(jiǎn)單的對(duì)比:
# python # C
if today == "Friday": if (today == "Friday") {
print("So happy") printf("So happy");
else: } else {
print("So bad") printf("So bad");
}
PEP8規(guī)范,使用四個(gè)空格表示一個(gè)縮進(jìn)
Use 4 spaces per indentation level
編寫python的時(shí)候,我們可以將編輯器的tab建改成四個(gè)空格,也可以每次手動(dòng)空格,心里默念,1,2,3,4, 本人就是這么過來的,淚奔?。?!
二·變量類型
python常用變量類型有int(整型),str(字符串),float(浮點(diǎn)型),list(列表),tuple(元組),dict(字典),set(集合)
整型
數(shù)學(xué)里的整數(shù),在python里的類型是int
# 定義一個(gè)整型變量,值234
num1 = 234
num2 = -234
浮點(diǎn)型
數(shù)學(xué)里的小數(shù),在python里的類型是float
num_f1 = 23.45
num_f2 = -23.45
字符串
python類型str,任意使用引號(hào)引起來的內(nèi)容,可以是' ', " ", ''' ''', """ """這里的任意一種形式。
string1 = 'This is string1'
string2 = "This is string2"
string3 = '''This is string3'''
string4 = """This is string4"""
但是三個(gè)引號(hào)對(duì)的里面的字符串自帶格式保持的
with_format_string = """This is a string with format
This line with 2 space indent
This line with 4 space indent
This line without indent.
print(with_format_string)
上面的語句會(huì)按照原樣輸出。
列表
python類型list,列表就是數(shù)組,它是一個(gè)有序的序列,列表中每個(gè)元素可以存放python的任何數(shù)據(jù)類型,如:
list1 = [1, 2, 3, 4, 5] # 整數(shù)列表
list2 = ["word1", "word2", 3, 4, "word5"] # 整數(shù)加字符串列表
list3 = [[1, 3], [2, 4], ["word1", "word2"]] # 列表的列表
元祖
python類型tuple,這個(gè)數(shù)據(jù)類型和列表差不多,唯一的區(qū)別是,元祖是不可變類型,列表是可變類型(關(guān)于可變與不可變類型,我會(huì)在后面詳細(xì)講解)。元祖使用小括號(hào)括起來的,如:
tuple1 = (1, 2, 3, 4, 5)
tuple2 = ("word1", "word2", 3, 4, "word5")
tuple3 = ([1, 3], [2, 4], ["word1", "word2"])
元祖用處較少,暫時(shí)作為了解。
字典
python類型dict,鍵值對(duì),如:
student = {
"name": "Someone",
"age": 22,
"favor": ["walk", "running", "jumping"]
}
通過冒號(hào)前面的鍵來獲得冒號(hào)后面的值
集合
python類型set,集合是一個(gè)無序的序列,它存儲(chǔ)的數(shù)據(jù)是不重復(fù)的。
unique_number = set([1, 3, 4, 5, 6, 6, 7, 3])
print(unique_number)
# 輸出結(jié)果 {1, 3, 4, 5, 6, 7}
小結(jié)
上面我們對(duì)python的數(shù)據(jù)類型做了個(gè)簡(jiǎn)單的介紹,我們需要重點(diǎn)掌握的是字符串,列表,字典。下面我用一個(gè)簡(jiǎn)單的小程序來幫組你鞏固這幾個(gè)數(shù)據(jù)類型的使用。(fruits.py)
# coding=utf-8
# 因?yàn)槲募杏兄形淖⑨專源a第一行要指定文件編碼
import sys # 這是模塊的導(dǎo)入,sys是python內(nèi)置的模塊
# 定義了一個(gè)字典fruits_dict,包含兩個(gè)鍵("apple", "banana"),
# 對(duì)應(yīng)的值又是字典,鍵color對(duì)應(yīng)的值是個(gè)字符串列表,shape對(duì)應(yīng)的值是字符串。
fruits_dict = {
"apple": {"color": ["red", "green"], "shape": "circle"},
"banana": {"color": ["yellow"], "shape": "line-bar"}
}
if __name__ == "__main__":
# sys.argv 是一個(gè)列表,存儲(chǔ)命令行傳入的參數(shù)
# 命令: python3 fruits.py apple,那么sys.argv里面存放 ['fruits.py', 'apple']
# if判斷是否argv長(zhǎng)度為2,即使判斷用戶是否傳入了apple
if len(sys.argv) != 2:
print("Usage: python fruits.py <fruits_name>")
# python自帶的推出程序的函數(shù),即使下面的程序都不會(huì)執(zhí)行了
# 括號(hào)里面的1,表示本次退出是錯(cuò)誤的,程序沒有正常運(yùn)行完
exit(1)
# 將傳入的水果名賦值給字符串變量input_fruits
input_fruits = sys.argv[1]
# if判斷input_fruits變量的值是否在fruits_dict的鍵中
# 存在,則打印出水果的屬性來
if input_fruits in fruits_dict:
# print函數(shù)中的%s是python字符串的格式化占位符,表示這里是一個(gè)
# 字符串,在這條print語句中,三個(gè)占位符都用的%s,python會(huì)自動(dòng)吧
# 列表也轉(zhuǎn)換成字符串的
print("The %s with color: %s, and shape: %s" % (
input_fruits,
fruits_dict[input_fruits]["color"],
fruits_dict[input_fruits]["shape"]
))
# 不存在,打印對(duì)應(yīng)信息
else:
print("The only have 'apple' and 'banana'")
然后在終端運(yùn)行:
python fruits.py apple
# or
python fruits.py banana
# or
python fruits.py orange
# or
python fruits.py
上述代碼文件中注釋較多,是為了讓新手免去少許困惑,不要唄嚇到奧。
以上就是本章節(jié)的全部?jī)?nèi)容了,希望讀者能有個(gè)好的收獲,如有不對(duì)的地方請(qǐng)多多指教。