python 基础知识拾遗1(基础类型、运算符、文件)
运算符:
1.在Python中逻辑运算可以使用连续判断
如下代码执行结果输出 "Yes!"
a=1 b=2 c=3 if a<b<c: print("Yes!") else: print("No!")
列表:
1.列表切片,可以有三个参数,第一个为起始位置,第二个为终止位置(不包括),第三个为步长(默认为1)
names=["Alex","Tenglan","Eric","Rain","Tom","Amy"] names[0::2] #输出结果为:['Alex', 'Eric', 'Tom'] 隔一个取一次 names[0::3] #输出结果为:['Alex', 'Rain'] 隔两个取一次
元组:
元组数据不可以修改,使用小括号"()"定义,列表使用中括号"[]"定义
names = ("alex","jack","eric") #定义一个元组,只有三个元素
字符串:
name.capitalize() #首字母大写 name.casefold() #大写全部变小写
str.replace(oldStr,newStr) #将str字符串中的oldStr替换成newStr
"hello".ljust(20,"-") #输出 '--------------------hello' 左边补全 "hello".center(20,"-") #输出 '----------hello----------' 两边补全 "hello".rjust(20,"-") #输出 'hello--------------------' 右边补全 name.count('lex') #统计 lex出现次数 name.encode() #将字符串编码成bytes格式 name.endswith("Li") #判断字符串是否以 Li结尾 "Alex\tLi".expandtabs(10) #输出'Alex Li', 将\t转换成多长的空格 name.find('A') #查找A,找到返回其索引, 找不到返回-1 # 使用format、format_map函数填充字符串的的对应变量,可是使用变量、索引位置,默认使用索引位置 format : >>> msg = "my name is {}, and age is {}" >>> msg.format("alex",22) 'my name is alex, and age is 22' >>> msg = "my name is {1}, and age is {0}" >>> msg.format("alex",22) 'my name is 22, and age is alex' >>> msg = "my name is {name}, and age is {age}" >>> msg.format(age=22,name="ale") 'my name is ale, and age is 22' format_map >>> msg.format_map({'name':'alex','age':22}) 'my name is alex, and age is 22' msg.index('a') #返回a所在字符串的索引 '9aA'.isalnum() #检测字符串是否由字母和数字组成 True str = "this2009"; # 字符中没有空格 print str.isalnum(); #True str = "this is string example....wow!!!"; print str.isalnum(); #False str.isdigit(): #判断字符串是否只包含数字,这里的数字包括十进制数字和其它特殊数字(如上标数字等)。一般地,一个数字是拥有如下属性值的字符:Numeric_Type=Digit或Numeric_Type=Decimal。 str.isnumeric(): #判断字符串是否只包含数字字符。数字字符范围很大,一般来说,数字字符是拥有如下属性值的字符:Numeric_Type=Digit, Numeric_Type=Decimal或Numeric_Type=Numeric。 #比较isdecimal()、isdigit()、isnumeric(),几个方法检测的范围依次扩大。 name.isprintable #判断字符串所包含的字符是否全部可打印 name.isspace #判断是否为可打印字符串 name.istitle #判断是否首字母大写,其他字母小写 name.isupper #判断是否为小写/大写 "|".join(['alex','jack','rain']) #将字符串加入到可迭代对象里面去,iterable必须是每一个元素是字符串,否则会跑出TypeError异常 'alex|jack|rain' maketrans >>> intab = "aeiou" #This is the string having actual characters. >>> outtab = "12345" #This is the string having corresponding mapping character >>> trantab = str.maketrans(intab, outtab) #创建映射表 >>> >>> str = "this is string example....wow!!!" >>> str.translate(trantab) #根据映射表执行转换 'th3s 3s str3ng 2x1mpl2....w4w!!!' msg.partition('is') 输出 ('my name ', 'is', ' {name}, and age is {age}') >>> "alex li, chinese name is lijie".replace("li","LI",1) 'alex LI, chinese name is lijie' msg.swapcase #大小写互换 >>> msg.zfill(40) #不足在左侧补0 '00000my name is {name}, and age is {age}' >>> b="ddefdsdff_哈哈" >>> b.isidentifier() #检测一段字符串可否被当作标志符,即是否符合变量命名规则 True
文件:
打开文件
方式一: f = open('lyrics') #打开文件
方式二: with open(fileName,"r") as fr:
方式一需要在最后调用f.close()方法关闭文件,方式二对文件的操作必须在with语句块中完成,不需要调用f.close()方法
打开文件的模式有:
r,只读模式(默认)。
w,只写模式。【不可读;不存在则创建;存在则删除内容;】
a,追加模式。【可读; 不存在则创建;存在则只追加内容;】
"+" 表示可以同时读写某个文件
r+,可读写文件。【可读;可写;可追加】
w+,写读
a+,同a
"U"表示在读取时,可以将 \r \n \r\n自动转换成 \n (与 r 或 r+ 模式同使用)
rU
r+U
"b"表示处理二进制文件(如:FTP发送上传ISO镜像文件,linux可忽略,windows处理二进制文件时需标注)
rb
wb
ab
常用方法:
str = "" with open(fileName,"r",encoding="utf-8") as fr: #使用只读的方式打开文件,并设置字符编码,使用with方式,可以不显示的调用打开文件的方法,但对文件的操作必须在with语句块内 line = fr.readline() #读取一行,同时光标往后移动一行,返回内容为String rd = fr.read() #读取指定大小内容,默认大小不详,单位为字节 返回类型为String lines = fr.readlines() #返回一个list,每一行为一个元素 lines = ''.join(lines) with open(fileName,"w",encoding="utf-8") as fw: #使用只写的方法打开文件,同时清除文件原来的内容 fw.write(str.replace(oldStr,newStr)) #replace,将字符串内的旧字符串替换成新字符串
20180208补充内容
while:
....
else:
else中的语句只有当循环使用break方式终止的时候不会被执行,在continue和其他正常执行完情况下都会被执行
print("",end="") end参数用于指定打印一行最后一个字符的值,默认不指定的情况下是"\n",默认情况下一个print都是打印一行并回车
浙公网安备 33010602011771号