目錄
簡(jiǎn)介
第一課:python 的介紹
第二課:變量與操作符
第三課:字符串和數(shù)字
第四課:條件執(zhí)行語(yǔ)句
第五課:列表與元組
第六課:循環(huán)語(yǔ)句
第七課:集合
第八課:字典
第九課:函數(shù)
第十課:面向?qū)ο?/a>
第十一課:文件的操作
第十二課:學(xué)生管理系統(tǒng)
字符串的索引
我們?cè)谥耙呀?jīng)接觸過(guò)字符串缺猛,它在 python 是str
類型±恚現(xiàn)在讓我我們來(lái)更深入地了解一下问裕。
一個(gè)字符串是一系列字符的組合,因此我們可以通過(guò)方括號(hào)
操作符號(hào)加上序號(hào)來(lái)獲取單個(gè)字符。
words = '小明愛吃瓜'
print(words[1])
明
咦,第一個(gè)字符不是小
嗎稠歉?為什么這里打印了明
,我的 python 壞掉了嗎蹂窖?
你的 python 沒(méi)有壞掉轧抗,因?yàn)樵诮^大多數(shù)包括 python 的程序語(yǔ)言中,計(jì)數(shù)都是從 0
開始的瞬测。
這意味著我們使用序號(hào)0
才能拿到小
字横媚。
words = '小明愛吃瓜'
print(words[1])
明
我們?nèi)∽詈笠粋€(gè)字符豈不是要先數(shù)下字符串有多少個(gè)字符,如果字符串是一篇論文月趟,我們不是要數(shù)到腦殼疼灯蝴?
這樣子的話讓 python 幫我們數(shù)吧,內(nèi)建的len
函數(shù)可以幫我們數(shù)出字符串的長(zhǎng)度孝宗。由于序號(hào)從 0 開始穷躁,長(zhǎng)度減 1 就是我們最后一個(gè)字符的序號(hào)。
words = '小明愛吃瓜'
length = len(words)
print('長(zhǎng)度:', length)
print('最后一個(gè)元素是:', words[length-1])
長(zhǎng)度: 5
最后一個(gè)元素是: 瓜
拿最后一個(gè)字符除了數(shù)長(zhǎng)度之外我們還有更好的辦法因妇,就是用 -1
作為序號(hào)问潭。
因?yàn)?python 里同時(shí)有另一種取值方式,那就是 -1 作為最后一個(gè)元素的序號(hào)婚被,往前逐個(gè)遞減狡忙。
words = '小明愛吃瓜'
print('最后一個(gè)元素是:', words[-1])
最后一個(gè)元素是: 瓜
字符串切片
字符串中的一部分叫作切片,例如"愛吃"
相對(duì)于"小明愛吃瓜"
址芯。我們同樣可以用方括號(hào)操作符去獲取字符串的切片, 獲取方式為 [a:b]
灾茁。其中 a
為起始序號(hào),b
為結(jié)束序號(hào)谷炸。
注意的是獲取的切片包含 a
不包含 b
北专。
例如要獲取"愛吃"
。
words = '小明愛吃瓜'
sliced = words[2:4]
print(sliced)
愛吃
當(dāng)我們將 a
置為空時(shí)旬陡,切片結(jié)果為 b
之前不包含 b
的所有字符拓颓。當(dāng)我們將 b
置為空時(shí),切片的結(jié)果就為 a
之后并包含 a
的所有字符描孟。
words = '小明愛吃瓜'
print(words[:4])
print(words[2:])
小明愛吃
愛吃瓜
當(dāng) a
等于 b
時(shí)返回結(jié)果為空字符串录粱,當(dāng) a
與 b
都置空時(shí)返回當(dāng)前字符串的拷貝對(duì)象腻格。
words = '小明愛吃瓜'
print(words[2:2])
print(words[:])
輸出
小明愛吃瓜
in 操作符
in
操作符可以左右各接收一個(gè)字符串,判斷左邊的字符串是否被包含在右邊的字符串中啥繁,返回結(jié)果是一個(gè) bool
對(duì)象菜职。
words = '小明愛吃瓜'
print('小明' in words)
print('小紅' in words)
輸出
True
False
字符串相關(guān)函數(shù)
我們依舊可以用dir
查看字符串中有哪些可用的函數(shù),并且使用 help
查看其幫助文檔旗闽。
words = '小明愛吃瓜'
print(dir(words))
print(help(words.upper))
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
Help on built-in function upper:
upper() method of builtins.str instance
Return a copy of the string converted to uppercase.
我們介紹幾個(gè)常用的方法酬核。首先是upper
,將字符串的字母轉(zhuǎn)換成大寫适室。
words = 'happy'
print(words.upper())
HAPPY
很顯示嫡意,對(duì)應(yīng)的 lower
就是小寫。
words = 'HAPPY'
print(words.lower())
happy
isdigit
的用法是判斷字符串是否是書面意思上的數(shù)字捣辆。
words = '10'
print(words.isdigit())
words = '10ab'
print(words.isdigit())
help(str.isdigit)
True
False
Help on method_descriptor:
isdigit(self, /)
Return True if the string is a digit string, False otherwise.
A string is a digit string if all characters in the string are digits and there
is at least one character in the string.
很多內(nèi)置函數(shù)的學(xué)習(xí)可以用 help
函數(shù)去查看和了解蔬螟。建議同學(xué)們可以經(jīng)常使用,這里就不過(guò)多贅述了汽畴。
數(shù)字類型
常見的數(shù)字類型包括 int
(整型)和 float
(浮點(diǎn)型)旧巾。在之前我們已經(jīng)用 str
類型來(lái)表示我們生活中的一些常見內(nèi)容了,比如姓名忍些、地址鲁猩、小說(shuō)等我們都用 str
類型進(jìn)行保存。
我們還需要一些類型罢坝,來(lái)表示我們的體重廓握、年齡、商品的價(jià)格等數(shù)據(jù),并且能對(duì)他們進(jìn)行數(shù)學(xué)上的運(yùn)算嘁酿。這時(shí)就需要int
和 float
類型隙券。為什么需要定義兩種類型呢?
因?yàn)閷?duì)于 int
(整型)的數(shù)據(jù)闹司,它可以用來(lái)表達(dá)次數(shù)的概念娱仔。我去了兩次北京,而不能說(shuō)我去了 2.5 次北京开仰。因此在使用上整數(shù)與帶小數(shù)的數(shù)字必須有所區(qū)分,所以把數(shù)字分成 int
與 float
兩個(gè)類型薪铜。
int 整型
當(dāng)我們給變量賦值一個(gè)不帶小數(shù)點(diǎn)的數(shù)字時(shí)众弓,這個(gè)變量就默認(rèn)為int
(整型)。
n = 12
print(type(n))
<class 'int'>
注意隔箍,兩個(gè)整型經(jīng)過(guò)除法運(yùn)算后谓娃,不管結(jié)果是不是書面意義上的整數(shù),其都為 float
(浮點(diǎn)型)蜒滩。
n = 4 / 2
print(type(n))
<class 'float'>
float 浮點(diǎn)型
一個(gè)數(shù)字只要帶有 .
都為浮點(diǎn)型滨达。
f1 = 1.5
f2 = 1.
f3 = .5
print(f1, type(f1))
print(f2, type(f2))
print(f3, type(f3))
1.5 <class 'float'>
1.0 <class 'float'>
0.5 <class 'float'>
練習(xí)
1.定義姓名奶稠、年齡、身高三個(gè)變量來(lái)保存關(guān)于你自身的數(shù)據(jù)捡遍,你能說(shuō)出他們分別屬于什么類型嗎锌订?
2.打印出你姓名中的姓,假設(shè)你是慕容XX
復(fù)姓的話又該如何画株?
解析
1.定義姓名辆飘、年齡、身高三個(gè)變量來(lái)保存關(guān)于你自身的數(shù)據(jù)谓传,你能說(shuō)出他們分別屬于什么類型嗎蜈项?
name = '小明'
age = 18
height = 1.8
print('name 的類型是:', type(name))
print('age 的類型是:', type(age))
print('height 的類型是:', type(height))
name 的類型是: <class 'str'>
age 的類型是: <class 'int'>
height 的類型是: <class 'float'>
2.打印出你姓名中的姓,假設(shè)你是慕容XX
復(fù)姓的話又該如何续挟?
name = '王小明'
print('姓:', name[0])
name = '慕容小明'
print('姓:', name[:2])
姓: 王
姓: 慕容