-----------------------------------------
1,基础篇。
#encoding=utf-8
#上面可以随便写的,只要符合oding=[]
#basic1.py //相当于导入了asic1模块
#可以使用 basic1.new_str
#这里给下面语句做一个注释
new_str="这是一个全局变量" #注释
"""
这是一个标准脚本的写作格式,此处为脚本文档
"""
def hello():
"""
此处为脚本程序的解释
@author:李亚飞
@copyright:归本人所有
@version:1.0
"""
return "hello world"
#一定要下一行空4格。
#程序主体:
#if语句判断是否是主程序
#调用其他函数时,要带上括号和参数
if __name__=="__main__":
print(hello()) #print hello() 提示SyntaxError: Missing parentheses in call to 'print'
print basic1.__doc__
基础篇6
>>> d=open('d:a.txt','w') 创建一个文件,以读的方式
>>> d.write("hghagd \n hage\n hadslae") 向文件中写入内容
>>> d.flush() 刷新
>>> d.close() 关闭
>>> d.open('d:a.txt','r') 错误,d已经关闭
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'file' object has no attribute 'open'
>>> d=open('d:a.txt','r') 打开一个文件以写的方式
d.readline(3) //一行读几个。
>>> d.readline() 一次读一行
'hghagd \n'
>>> d.read() 将剩余的读完
' hage\n hadslae'
>>> d.seek(0) #将d文件从头开始
>>> d.read(100) #一次读多少字节
'hghagd \n hage\n hadslae'
>>>
基础篇7
str="abc"不可变
str[0]=1 错误
arr=[1,3,5] 可变
arr[0]=3 正确
>>> a
'this is world'
>>> b=a.replace("this","that") 替换
>>> b
'that is world'
>>> b
'that is world'
>>> a
'this is world'
>>> a.find("is") 查询
2 角标
>>> a.find("s")
3
>>> a.find('s',4,len(b)-1)
6 查询,后面跟起始结束位置
>>> a[6]
's'
>>> a.find(sub [,start[,end]]) 其中sub为必填项,[]里面的为选填项。
%s 为字符串占位符,>>> a="this %s a %s world"%("is","beautiful")
>>> a
'this is a beautiful world'
format替换 >>> a="this {0} a {1} world".format("is","beautiful")
>>> a
'this is a beautiful world'
输入参数替换 >>> b="this {be} a {adj} world".format(adj="beautiful",be="is")
>>> b
'this is a beautiful world'
字典 >>> c="this %(be)s a %(adj)s world"%{'be':'is','adj':'beautiful'}
>>> c
'this is a beautiful world'
%d 为数字占位符
基础篇9列表
>>> a=[1,2,3]
>>> a
[1, 2, 3]
>>> a[0]
1
>>> a[-1] 最后一个值
3
>>> a[2]=7 替换
>>> a
[1, 2, 7]
列表的索引
>>> a=[1,2,4,5]
>>> a[0:4:1] //正向索引
[1, 2, 4, 5]
>>> a[-1:-4:-1] //反向索引
[5, 4, 2]
>>> a[:]
[1, 2, 4, 5]
>>> a[:3] 缺的就是结尾或者开头
[1, 2, 4]
>>>
列表的添加
>>> a.append(7) append 末尾添加
>>> a
[1, 2, 4, 5, 7]
>>> b=[3,65,7]
>>> a+b
[1, 2, 4, 5, 7, 3, 65, 7]
>>> a.extend(b) extend添加
>>> a
[1, 2, 4, 5, 7, 3, 65, 7]
>>> a
[1, 2, 4, 5, 7, 3, 65, 7, 3, 65, 7, 3, 65, 7]
>>> a.insert(2,90) insert//插入,可以控制插入位置
>>> a
[1, 2, 90, 4, 5, 7, 3, 65, 7, 3, 65, 7, 3, 65, 7]
>>>
3,列表的修改
>>> a=[1,2,3]
>>> a[2]=7 替换
>>> a
[1, 2, 7]
4,列表的删除
>>> a
[1, 2, 90, 4, 5, 7, 3, 65, 7, 3, 65, 7, 3, 65, 7]
>>> del a[2] del删除
>>> a
[1, 2, 4, 5, 7, 3, 65, 7, 3, 65, 7, 3, 65, 7]
>>>
>>> a
[1, 4, 5, 7, 3, 65, 7, 3, 65, 7, 3, 65, 7]
>>> a.remove(7) //删除第一个匹配的值
>>> a
[1, 4, 5, 3, 65, 7, 3, 65, 7, 3, 65, 7]
>>>
1,基础篇。
#encoding=utf-8
#上面可以随便写的,只要符合oding=[]
#basic1.py //相当于导入了asic1模块
#可以使用 basic1.new_str
#这里给下面语句做一个注释
new_str="这是一个全局变量" #注释
"""
这是一个标准脚本的写作格式,此处为脚本文档
"""
def hello():
"""
此处为脚本程序的解释
@author:李亚飞
@copyright:归本人所有
@version:1.0
"""
return "hello world"
#一定要下一行空4格。
#程序主体:
#if语句判断是否是主程序
#调用其他函数时,要带上括号和参数
if __name__=="__main__":
print(hello()) #print hello() 提示SyntaxError: Missing parentheses in call to 'print'
print basic1.__doc__
基础篇6
>>> d=open('d:a.txt','w') 创建一个文件,以读的方式
>>> d.write("hghagd \n hage\n hadslae") 向文件中写入内容
>>> d.flush() 刷新
>>> d.close() 关闭
>>> d.open('d:a.txt','r') 错误,d已经关闭
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'file' object has no attribute 'open'
>>> d=open('d:a.txt','r') 打开一个文件以写的方式
d.readline(3) //一行读几个。
>>> d.readline() 一次读一行
'hghagd \n'
>>> d.read() 将剩余的读完
' hage\n hadslae'
>>> d.seek(0) #将d文件从头开始
>>> d.read(100) #一次读多少字节
'hghagd \n hage\n hadslae'
>>>
基础篇7
str="abc"不可变
str[0]=1 错误
arr=[1,3,5] 可变
arr[0]=3 正确
>>> a
'this is world'
>>> b=a.replace("this","that") 替换
>>> b
'that is world'
>>> b
'that is world'
>>> a
'this is world'
>>> a.find("is") 查询
2 角标
>>> a.find("s")
3
>>> a.find('s',4,len(b)-1)
6 查询,后面跟起始结束位置
>>> a[6]
's'
>>> a.find(sub [,start[,end]]) 其中sub为必填项,[]里面的为选填项。
%s 为字符串占位符,>>> a="this %s a %s world"%("is","beautiful")
>>> a
'this is a beautiful world'
format替换 >>> a="this {0} a {1} world".format("is","beautiful")
>>> a
'this is a beautiful world'
输入参数替换 >>> b="this {be} a {adj} world".format(adj="beautiful",be="is")
>>> b
'this is a beautiful world'
字典 >>> c="this %(be)s a %(adj)s world"%{'be':'is','adj':'beautiful'}
>>> c
'this is a beautiful world'
%d 为数字占位符
基础篇9列表
>>> a=[1,2,3]
>>> a
[1, 2, 3]
>>> a[0]
1
>>> a[-1] 最后一个值
3
>>> a[2]=7 替换
>>> a
[1, 2, 7]
列表的索引
>>> a=[1,2,4,5]
>>> a[0:4:1] //正向索引
[1, 2, 4, 5]
>>> a[-1:-4:-1] //反向索引
[5, 4, 2]
>>> a[:]
[1, 2, 4, 5]
>>> a[:3] 缺的就是结尾或者开头
[1, 2, 4]
>>>
列表的添加
>>> a.append(7) append 末尾添加
>>> a
[1, 2, 4, 5, 7]
>>> b=[3,65,7]
>>> a+b
[1, 2, 4, 5, 7, 3, 65, 7]
>>> a.extend(b) extend添加
>>> a
[1, 2, 4, 5, 7, 3, 65, 7]
>>> a
[1, 2, 4, 5, 7, 3, 65, 7, 3, 65, 7, 3, 65, 7]
>>> a.insert(2,90) insert//插入,可以控制插入位置
>>> a
[1, 2, 90, 4, 5, 7, 3, 65, 7, 3, 65, 7, 3, 65, 7]
>>>
3,列表的修改
>>> a=[1,2,3]
>>> a[2]=7 替换
>>> a
[1, 2, 7]
4,列表的删除
>>> a
[1, 2, 90, 4, 5, 7, 3, 65, 7, 3, 65, 7, 3, 65, 7]
>>> del a[2] del删除
>>> a
[1, 2, 4, 5, 7, 3, 65, 7, 3, 65, 7, 3, 65, 7]
>>>
>>> a
[1, 4, 5, 7, 3, 65, 7, 3, 65, 7, 3, 65, 7]
>>> a.remove(7) //删除第一个匹配的值
>>> a
[1, 4, 5, 3, 65, 7, 3, 65, 7, 3, 65, 7]
>>>