将理论应该到实践中,才会发现觉得很简单的问题却很容易混淆或者出错,特记录下Python学习中的小小知识点。python
如下内容是在学习 程序设计入门—Python 的部分笔记
正则表达式
学习网址:http://mooc.study.163.com/learn/1000002017?tid=2001464005#/learn/announce”app
1.Python中变量、函数、模块命名规则函数
rang(2,10)----[2,3,4,5,6,7,8,9]学习
rang(2,10,3)----[2,5,8]spa
rang(10,2,-1)----[10,9,8,7,6,5,4,3]设计
备注:range()函数为“左闭右开”,即左边起始值包含在内,右边终止值不包含。3d
能够转换为须要的数据类型:radius=float(raw_input('R: '));注意若是写提示字符,使用单引号。code
\n 回车; \t Tab; \\ 一个\; \' 单引号’;\" 双引号”; orm
elif至关于 else:if
if - elif - else 语句中else条件要放到最后;
关键字__函数名__参数
例:def printString(count): -----函数头
print 'hello world!' -----函数体
count=1
return count
strName='xiao ming'
strName="xiao ming"
strNamr='''xiao ming''' #能够保存全部格式信息
s=‘I\'m a student.’注意要使用转义字符
finish:子序列结束位置的下一个字符的索引值(索引值要比想要字符的索引值最大值+1)
也能够加计算参数 s='hello world' s=[0:11:2] 输出 hlowrd
得到逆字符串 s=[::-1] 输出
注意:
字符串一旦生成,内容是不能被改变的 例如 s='abc' s[1]='1' 会提示错误
经过切片操做,生成一个新的字符串 s=s[:1]+'1'+s[2:] (得将结果赋值给s ,s的内容才回更新)
s.replace(src,dst) 使用dst替换src的内容
s.find()
s.split()
dir(s)获取其余更多方法的使用
#mode 有r(读,默认),w(写)等 f=open(filename,mode) #按行读文件内容 for line in f: pass #关闭文件 f.close() #写文件 f.write(str)
【任何一个字符都对应一个数字】能够直接比较数字大小
【字符串比较】字典序
#简单的例子 print "he {} a boy {}".format('is','!') #还能够在{}中定义格式
#读取30个数字,并计算平均数 nums=[] for i in rang(30): nums,append(float(raw_input())) s=0 for num in nums: s+=nums avg = s/30 print avg
#冒泡排序 def bubble_sort(list_t): exchanged = True lenT = len(list_t)-1 while exchanged: exchanged = False for i in range(lenT): if list_t[i]>list_t[i+1]: s=list_t[i] list_t[i]=list_t[i+1] list_t[i+1]=s exchanged = True lenT -= 1 return list_t nums=[2,7,9,8,6,0,3] bubble_sort(nums) print nums