(本科)第3章 序列ppt课件.pptx
《(本科)第3章 序列ppt课件.pptx》由会员分享,可在线阅读,更多相关《(本科)第3章 序列ppt课件.pptx(99页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、课程主讲人:(本科)第3章 序列ppt课件Chap3 SequenceDepartment of Computer Science and TechnologyDepartment of University Basic Computer TeachingNanjing UniversityNanjing University序列3Nanjing University序列aStr = Hello, World!aList = 2, 3, 5, 7, 11aTuple = (Sunday, happy )x = range(10) pList = (AXP, American Express C
2、ompany, 78.51),(BA, The Boeing Company, 184.76),(CAT, Caterpillar Inc., 96.39),(CSCO, Cisco Systems, Inc., 33.71),(CVX, Chevron Corporation, 106.09)4序列是一种最基本最重要的数据结构Nanjing University5A AStrings字符串B BLists列表C CTuples元组D Drange objectsrange对象Nanjing University3.1.1 索引6Nanjing University序列的索引 序列类型对象一般
3、有多个成员组成,每个成员通常称为元素,每个元素都可以通过索引(index)进行访问,索引用方括号“”表示。如:7Nanjing University序列的索引8week0123456Monday Tuesday WednesdayThursdayFridaySaturday Sunday-7-6-5-4-3-2-1序列0-N1-(N-1)2-(N-2)N-2-2N-1-1访问模式 元素从0开始通过下标偏移量访问 一次可访问一个或多个元素Nanjing University索引的使用9 aList = Mon., Tues., Wed., Thur., Fri., Sat., Sun. aLis
4、t1Tues. aList-1Sun. aStr = apple aStr1pSourceNanjing University序列相关操作10值比较对象身份比较布尔运算切片重复连接判断成员序列类型转换内建函数序列类型其他内建函数标准类型运算通用序列类型操作序列类型函数Nanjing University3.1.2 标准类型运算11Nanjing University标准类型运算符=!=值比较isis not对象身份比较notandor布尔运算12Nanjing University值比较13 apple 1,3,5 != 2,4,6True aList1 = Tues.True 1, Mond
5、ay o, k (o, k)Traceback (most recent call last): File , line 1, in o, k (o, k)TypeError: unorderable types: list() 1 , 2 , 3 1 , a , 3Traceback (most recent call last): File , line 1, in 1 , 2 , 3 1 , a , 3TypeError: unorderable types: int() aTuple = (BA, The Boeing Company, 184.76) bTuple = aTuple
6、bTuple is aTupleTrue cTuple = (BA, The Boeing Company, 184.76) aTuple is cTupleFalse aTuple = cTupleTrueSourceNanjing University布尔(逻辑)运算15 ch = k a = ch = z or A = ch aStr = American Express Company aStr9: 16ExpressSourceNanjing University切片19 aList = Mon., Tues., Wed., Thur., Fri., Sat., Sun. aList
7、 0: 5Mon., Tues., Wed., Thur., Fri. aList: 5Mon., Tues., Wed., Thur., Fri. aList5: 7Sat., Sun.SourceNanjing University切片20 aList-2: -1Sat. aList-2: -3 aList-2:Sat., Sun. aList:Mon., Tues., Wed., Thur., Fri., Sat., Sun.SourceNanjing University切片21切片操作的另一种格式,可以选择切片操作时的步长:sequencestartindex : endindex
8、: stepsaList0: 5 aList0: 5: 10123456Nanjing University切片22 aList = Mon., Tues., Wed., Thur., Fri., Sat., Sun. aList1: 6: 3Tues., Fri. aList:3Mon., Thur., Sun. aList:-3Sun., Thur., Mon. aList5: 1: -2Sat., Thur.SourceNanjing University切片23 aStr = apple aStr0: 3app aTuple = (3, 2, 5, 1, 4, 6) aTuple1:
9、: 2(2, 1, 6)SourceNanjing University切片24 aList = Mon., Tues., Wed., Thur., Fri., Sat., Sun. day = aListint(input(The day of the week(1-7): ) - 1The day of the week(1-7): 5 print( Today is + day + .)Today is Fri.SourceNanjing University切片25 week = Monday, Tuesday, Wednesday, Thursday, Friday, Saturda
10、y, Sunday print(week1, week-2, n, week1:4, n, week:6, n, week:-1)Tuesday SaturdayTuesday, Wednesday, Thursday Monday, Tuesday, Wednesday, Thursday, Friday, Saturday Sunday, Saturday, Friday, Thursday, Wednesday, Tuesday, MondaySourceNanjing University重复26重复操作的形式为:sequence * copies apple * 3appleappl
11、eapple (1, 2, 3) * 2(1, 2, 3, 1, 2, 3) aTuple = (3, 2, 5, 1) aTuple * 3(3, 2, 5, 1, 3, 2, 5, 1, 3, 2, 5, 1) P , y, t, h, o, n * 2P, y, t, h, o, n, P, y, t, h, o, nSourceNanjing University连接27连接操作的形式为:sequence1 + sequence2 1, 2, 3 + 4, 5, 61, 2, 3, 4, 5, 6 (1, 2, 3) + (4, 5, 6)(1, 2, 3, 4, 5, 6) pine
12、 + applepineapple t, h, e + appleTraceback (most recent call last): File , line 1, in t, h, e + appleTypeError: can only concatenate list (not str) to listSourceNanjing University判断成员28 aList = Mon., Tues., Wed., Thur., Fri., Sat., Sun. Mon. in aListTrue week in aListFalse week not in aListTrueSourc
13、e判断一个元素是否属于一个序列操作的形式为:obj in sequenceobj not in sequenceNanjing University判断成员29 username = Jack, Tom, Halen, Rain input(please input your name: ) in usernameplease input your name: HalenTrueSourceNanjing University3.1.4 序列类型函数30Nanjing University序列类型转换内建函数31list()tuple() str() list(Hello, World!)H,
14、 e, l, l, o, , , W, o, r, l, d, ! tuple(Hello, World!)(H, e, l, l, o, , , W, o, r, l, d, !) list(1, 2, 3)1, 2, 3 tuple(1, 2, 3)(1, 2, 3)SourceNanjing University序列类型转换内建函数32list()tuple() str() str(123)123 str(t,h,e)(t, h, e)SourceNanjing University序列类型其他常用内建函数33len() enumerate()sorted() max()sum()min
15、() zip() reversed() aStr = Hello, World! len(aStr)13 sorted(aStr) , !, , H, W, d, e, l, l, l, o, o, rSourceNanjing University序列类型其他常用内建函数34 aStr = Hello, World! len(aStr)13Source nList = 3, 2, 5, 1 sorted(nList)1, 2, 3, 5 nList3, 2, 5, 1Sourcelen()sorted()Nanjing University序列类型其他常用内建函数35 nList = 3,
16、2, 5, 1 reversed(nList) list(reversed(nList)1, 5, 2, 3Sourcereversed() Nanjing University序列类型其他常用内建函数36 sum(a, b, c)Traceback (most recent call last): File , line 1, in sum(a, b, c)TypeError: unsupported operand type(s) for +: int and str sum(1, 2, 3.5)6.5Sourcesum()Nanjing University序列类型其他常用内建函数37
17、aList = Mon., Tues., Wed., Thur., Fri., Sat., Sun. max(aList)Wed. max(1, 2.5, 3)3 max(1, 5, 3,1, 2.5, 3)1, 5, 3 max(1, 5, 3, 1,1, 9, 3)1, 9, 3Sourcemax()和min()Nanjing University序列类型其他常用内建函数38 seasons = Spring, Summer, Fall, Winter list(enumerate(seasons)(0, Spring), (1, Summer), (2, Fall), (3, Winte
18、r) list(enumerate(seasons, start = 1)(1, Spring), (2, Summer), (3, Fall), (4, Winter)Sourceenumerate()Nanjing University序列类型其他常用内建函数39 list(zip(hello, world)(h, w), (e, o), (l, r), (l, l), (o, d)Sourcezip()Nanjing University字符串40Nanjing University3.2.1 字符串的表示41Nanjing University字符串的表示形式42 aStr = The
19、 Boeing Company bStr = The Boeing Company cStr = The Boeingcompany aStrThe Boeing Company bStrThe Boeing Company cStrThe BoeingnCompanySource单引号三引号双引号Nanjing University字符串的表示形式43 dStr = Im a student. dStrIm a student. eStr = No pain, No gain. is a good saying. eStrNo pain, No gains. is a good saying
20、. break fast # breakfast或breakfast等形式亦可breakfastSourceNanjing University字符串的表示形式44 cStr = The Boeingcompany cStrThe BoeingnCompany fStr = Its said that. where there is a will, there is a way. fStrIts said thatnwhere there is a will, there is a way.Source三引号分行输入Nanjing University字符串的表示形式45 gStr = rd:
21、pythonn.py gStrd:pythonn.pySource原始字符串操作符Nanjing University字符串的创建和访问46 aStr = The Boeing Company print(football)footballSource aStr = The Boeing Company hStr = aStr:4 + IBM + aStr-8: hStrThe IBM CompanySource创建方式:访问方式:直接输出赋值切片Nanjing University字符串的创建和访问不可变47 testStr = hello testStr0 = HTraceback (mo
22、st recent call last): File , line 1, in testStr0 = HTypeError: str object does not support item assignment hStrThe IBM Company hStr = hStrSourceNanjing University常用转义字符48字符说明t横向制表符n换行r回车双引号单引号反斜杠(在行尾时)续行符ooo 值为八进制数ooo的字符xhh值为十六进制数hh的字符 aStr = 101tx41n bStr = 141tx61n print(aStr, bStr)AAaaSourceNanji
23、ng University字符串常用方法49capitalize() center()count()encode()endswith() find()format()index()isalnum()isalpha()isdigit()islower()isspace()istitle()isupper()join()ljust()lower()lstrip()maketrans()partition() replace()rfind()rindex()rjust()rpartition()rstrip()split()splitlines() startswith()strip()swapca
24、se()title()translate() upper()zfill()Nanjing University字符串常用方法50 aStr = Python! aStr.center(11) Python! Sourcecenter() bStr = No pain, No gain. bStr.count(no)0 bStr.count(No)2Sourcecount()Nanjing University字符串小例子51给出一个字符串,不区分大小写,字符串中可能包含A-Z,a-z, (空格)等字符。输出字母a(包括大小写)出现的次数。测试数据:abc&ABC。# Filename: cha
25、r_count.pys1 = abc&ABCs = s1.lower()n = s.count(a)print(n)FileNanjing University字符串常用方法52 bStr = No pain, No gain. # 逗号后面有一个空格! bStr.find(No)0 bStr.find(no)-1 bStr.find(No, 3)9 bStr.find(No, 3, 10)-1 bStr.find(No, 3, 11)9Sourcefind()Nanjing University字符串常用方法53 bStr = No pain, No gain. # 逗号后面有一个空格! b
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 本科第3章 序列ppt课件 本科 序列 ppt 课件
限制150内