-- coding: UTF-8 -:
数据类型 基本语句大同小异
if True:
pass
x//y 取整
x%y 取模
complex(x, y)
万能的数学模块 import math
random 模块 random.uniform(1,10)
字符串访问
s[1:3] -12 切片
s[:3] -012
s[3:] -345—
old() chr()
正索引 左侧0开始
负索引 右侧-1开始
‘on’ in str
list
append
del l[1]
l.count(‘d’)
l.index(‘d’)
l.remove(‘d’)
l.sort()
l.copy()
元组 (,)
len() max() ming() tuple()
字典 dict
集合 set add update remove clear len
time 模块
time.localtime()
datetime模块
def func()
pass
写文件:
import io
wf = io.open('test.txt', 'w', encoding='utf-8')
wf.write('Tom\n'.decode('utf8'))
wf.writelines(['Hello\n'.decode('utf8'), 'Python'.decode('utf8')])
===
测试一些基础语句
# -*- coding: UTF-8 -*:
import io
import os
import datetime
wf = io.open('test.txt', 'w', encoding='utf-8')
wf.write('Tom\n'.decode('utf8'))
wf.writelines(['Hello\n'.decode('utf8'), 'Python'.decode('utf8')])
wf.close()
print(os.getcwd()) #查看当前路径
print(os.listdir('F:/')) #获取文件和目录名列表
print(os.path.abspath('.'))#path绝对路径
print(os.path.split('F:/tmp.txt')) #拆分目录和文件,以元组的形式返回
print(os.path.join('F:/','tmp.txt')) #path拼接
print(datetime.datetime.utcfromtimestamp(os.path.getctime('F:/note/study.txt')))#文件或目录在系统中创建的时间
print(datetime.datetime.utcfromtimestamp(os.path.getmtime('F:/note/study.txt')))#最后一次修改时间
print(datetime.datetime.utcfromtimestamp(os.path.getatime('F:/note/study.txt')))#最后访问时间
print(os.path.exists('F:/note/study.txt'))#判断文件或目录是否存在,返回布尔值True False
print(os.path.isdir('F:'))#判断是否目录,返回布尔值
print(os.path.isfile('F:/note/study.txt'))#判断是否文件
print(os.path.getsize('F:/note/study.txt'))#返回path大小,byte单位,如果是目录则返回0
print(os.path.getsize('F:/android'))#0
#os.mkdir('F:/note/test') #创建一个名为test的目录
#os.makedirs('F:/note/test1/test2') #有就不創建了
os.chdir('/note')#更换目录
print (os.path.isfile('F:/note/test'))
print(os.system('ping www.baidu.com'))#调用shell脚本
#try 没test目录的情况下去创建一个
if not os.path.exists('F:/note/test'):
os.chdir('F:') # 更换目录到F
os.mkdir('F:/note/test')
def getNum(n):
try:
return 10/n
except IOError:
print('Error:IOError argument')
except ZeroDivisionError:
print ('Error ZerDivisionError argument')
else:
print ('else--->')
finally:
print ('finally--->')
print(getNum(0))
print(getNum(1))