【Python第16课到30课 】

【16课】字符串格式化2 元组(tuple)的用法 ,('Mike',87)就是元组 print " %s 's score is %d " %('Mike',87) name = 'Lily' score = 90 print "%s 's score is %d "%(name,score) 【17课】 类型转化 int(x) float(x) str(x) 把x转化为字符串 bool(x) 以下结果为真 int ('123') == 123 float( '3.3') == 3.3 str(111) == '111' bool (0)== false 【18课】bool类型转换 先跳过 【19课】函数 函数定义的关键字 def 。 括号和冒号不可少 函数举例 定义 def sayHello(): print 'hello world' 使用 sayHello() 【20课】命令行常用命令 1.dir 显示当前目录下的文件名和文件夹 2.cd 目录名 进入到当前目录下的子目录里 3.跳回上级目录命令cd .. 4.切换到D盘 输入D: 【21课】函数的参数 注意return的用法 def sayHello(someone): print someone+' hello world' def plus(num1,num2): return num1+num2 sayHello('mike') c=plus(2,3) print(c) 【22课】函数应用示例 猜数字 from random import randint def isEqual (num1,ans): if num1ans: print ('too big') return False if num1==ans: print' correct' return True ans=randint(1,101) print 'guess what I think' bingo= False while bingo == False: num=input() bingo=isEqual(num,ans) 【23课】if,elif,else 上个程序的改进版本 from random import randint def isEqual (num1,ans): if num1ans: print ('too big') return False else: print' correct' return True ans=randint(1,101) print 'guess what I think' bingo= False while bingo == False: num=input() bingo=isEqual(num,ans) 【24课】if的嵌套 【25课】初探list(列表) range(1,10)就是一个list 其结果是[1, 2, 3, 4, 5, 6, 7, 8, 9] 自定义列表 L=[1,1,2,3,5,8,13] L=[365,'everyday',0.618,True] 可以用for ... in 来遍历 【26课】操作list 先定义 L=[365,'everyday',0.618,True] 1.查 计数从0开始 print L[0] 2,改 L[0]=123; 3.增 L.append(1024) 4.删 del L[0] 举例: L=[365,'everyday',0.618,True] print L[0] print L L[0]=123 print L L.append(1024) print L del L[0] print L Ps: random 的另一个方法 choice 【27课】list切片 list有两类常用操作:索引index,切片slice 先定义 L=[365,'everyday',0.618,True] 1.负数的索引 L[-1] 表示L中的倒数第一个元素(有倒数第0个元素) L[-3]表示倒数第3个元素 2.切片 L[s,e]开始位置s包含在切片中,结束位置不包括 例如L[1,3]的结果是['everyday',0.618] 如果不指定开始位置,则切片从list第一个元素开始 如果不指定结束位置,则切片到list最后一个元素结束 如果都不指定,则返回整个list的一个拷贝 切片中也可以用负数 L[1,-1]从第一个(注意还有第0个)到倒数第一个元素(注意有倒数第0个元素) Ps,range的用法 range(b),0到b-1 range (a,b) ,a 到b-1 range(a,b,d),a 到b-1的间距为d的等差数列 例子: >>> range(5) [0, 1, 2, 3, 4] >>> range(1,5) [1, 2, 3, 4] >>> range(1,10,2) [1, 3, 5, 7, 9] 【28课】字符串的分割 1.把字符串按照空格进行分割 (默认用法) sentence= ' I am an English sentence' sentence.split() print sentence 举例: sentence= ' I am an English sentence' lists = sentence.split() #默认按照空白字符进行分割 print sentence print lists 结果 I am an English sentence ['I', 'am', 'an', 'English', 'sentence'] 2.把字符串按照指定分割符号进行分割 指定分割符号为‘.' 举例1.1 sentence= 'Hi.I am here.Bye' lists = sentence.split('.') print sentence print lists 结果1.1: Hi.I am here.Bye ['Hi', 'I am here', 'Bye'] 举例1.2 sentence= 'Hi.I am here.Bye.' #不同之处在于结尾有个分号 lists = sentence.split('.') print sentence print lists 结果1.2: Hi.I am here.Bye. ['Hi', 'I am here', 'Bye', ''] 注意区别结果1和结果2.结果2多出了最后的空字符串 每个’.'都会被作为分隔符,即使它的后面没有其他字符,也会有一个空串被分割出来。 举例2 lists = 'aa'.split('a') print lists 结果 ['', '', ''] 【29课】连接list s= ";" li=['apple','pear','orange'] fruit=s.join(li) #s为分隔符,li为list,fruit为string print fruit fruit=''.join(li) #无缝拼接 print fruit 结果 apple;pear;orange applepearorange 【30课】字符串的索引和切片 1.for ...in 遍历字符串中的每个字符 word='helloworld' for c in word print c 2.索引访问 print word[0] print word[-2] print word[5:7] 3.不可通过索引访问更改其中字符 以下为错误用例: word[1]='a' 4.连接字符 word='helloworld' newword=','.join(word) print newword 结果 h,e,l,l,o,w,o,r,l,d
原文地址:https://www.cnblogs.com/2012begin/p/3640759.html