python学习记录2,,一、两个模块(sys


一、两个模块(sys和os)

 1 #!/usr/bin/env python 2 # _*_ coding: UTF-8 _*_ 3 # Author:taoke 4 import sys 5 print(sys.path)#打印环境变量 6 print(sys.argv[0])#当前文件相对路径,sys.argv是一个列表,第一个元素为程序本身的相对路径,之后的为程序运行是的输入参数 7  8 import os 9 #cmd_res= os.system("dir")#执行命令不保存结果10 cmd_res = os.popen("dir").read()#保存命令执行的结果并返回保存地址11 print("-->",cmd_res)12 os.mkdir("new_dir")#创建一个目录

sys和os两个模块的简易使用

import

现在当前目录下寻找模块,在环境变量中寻找模块

存放第三方模块的路径 C:\Python36-32\Lib\site-packages

二、python中string与bytes之间的转换

1 #!/usr/bin/env python2 # _*_ coding: UTF-8 _*_3 # Author:taoke4 str = "我爱北京天安门"5 str_endode = str.encode("utf-8")6 str_endode_decode = str_endode.decode("utf-8")7 print(str,type(str))8 print(str_endode,type(str_endode))9 print(str_endode_decode,type(str_endode_decode))
运行结果:我爱北京天安门 <class ‘str‘>b‘\xe6\x88\x91\xe7\x88\xb1\xe5\x8c\x97\xe4\xba\xac\xe5\xa4\xa9\xe5\xae\x89\xe9\x97\xa8‘ <class ‘bytes‘>我爱北京天安门 <class ‘str‘>

三、列表(List)

技术分享

 1 #!/usr/bin/env python 2 # _*_ coding: UTF-8 _*_ 3 # Author:taoke 4 names = ["xiaoming","xiaohong","xiaohei","xiaoxiao"] 5  6 print(names) 7 print(names[0],names[2]) 8 print(names[1:3])#顾头不顾尾,切片 9 10 names.append("xiaobingbing")11 print(names)12 names.insert(1,"renma")13 print(names)

List中的浅copy和深copy

#!/usr/bin/env python# _*_ coding: UTF-8 _*_# Author:taokeimport copynames = ["xiaoming","xiaohong",["Jack","Toms"],"xiaohei","xiaoxiao"]names2 = names.copy()#浅copynames3 = copy.copy(names)#浅copynames4 = copy.deepcopy(names)#深copynames[2][0] = "JACK"print(names)print(names2)print(names3)print(names4)
运行结果:
[‘xiaoming‘, ‘xiaohong‘, [‘JACK‘, ‘Toms‘], ‘xiaohei‘, ‘xiaoxiao‘][‘xiaoming‘, ‘xiaohong‘, [‘JACK‘, ‘Toms‘], ‘xiaohei‘, ‘xiaoxiao‘][‘xiaoming‘, ‘xiaohong‘, [‘JACK‘, ‘Toms‘], ‘xiaohei‘, ‘xiaoxiao‘][‘xiaoming‘, ‘xiaohong‘, [‘Jack‘, ‘Toms‘], ‘xiaohei‘, ‘xiaoxiao‘]

四、tuple(元组)

不可以更改的列表,只能查。

五、string(字符串方法)

str.rjust:右对齐str.ljust:左对齐str.center:中间对齐str.zfill:默认的方式str.find:字符串查找,没有返回-1str.index:查找字符串位置,没有返回错误str.rfind:从右开始查找str.rindex:同上str.count:统计字符串出现的次数str.replace:字符串替换str.strip:去除字符串开头末尾的空格str.lstrip:去除左边空格str.rstrip:去除右边空格str.expandtabs:把字符串里的table换成等长的空格str.lower:str.upper:str.swapcase:将字符串字符大小写反转str.capitalize:字符串首字符大写str.title:字符串中首字母大写str.split:字符串拆分成列表str.splitlines:将字符串中按行拆分放到列表中‘-‘.join(strList):用‘-’将列表strList连接成字符串str.startswith:测试字符串是否是以指定字符开头的str.endswith:测试字符串是否是以指定字符结尾的str.isalum:判断字符串是否全是字符或数字并至少有一个字符str.isalpha:判断字符串是否全是字母str.isdigit:判断字符串是否全是数字str.isspace:判断字符串是否含有空格str.islower:判断字符串是否全是小写str.isupper:判断字符串是否全是大写str.istitle:判断首字母是否是大写import stringstring.atoi("123",base=10/8/16):转换字符串到int类型的数字string.atol:转换字符串到长整形数字string.atof:转换字符串到浮点型

python学习记录2

评论关闭