一阶段——知识点整合

1、while while...else 循环语句

number=0
sum=0
while number<=100:
    sum+=number
    number+=1
print(sum)#结果为5050

#如果循环被break打断,就不会执行else,反之执行else
count=0
while count<=5:
    count+=1
    if count==2:
        break
    print('loop',count)
else:
    print('循环完成')

count=0
while count<=5:
    count+=1
    if count==2:
        pass
    print('loop',count)
else:
    print('循环完成')
View Code

2、if 判断语句

#第一种用法
number=5
if number==5:
    print('该数字等于5')
else:
    print('不等于5')

#第二种用法
number=5
if number==1:
    pass
elif number==5:
    print('相等')
else:
    print(不相等)

#第三种用法
number=5
if number==5:
    print('该数字等于5')
View Code

3、数据类型 int、str、bool

#int:数字类型
i=2
b=4
print(i+b,i*b,i/b,i%b,i-b,i**b)#结果为6 8 0.5 2 -2 16

#str:字符串类型 可以跟int类型相乘,也可以两个字符串相加
str1='asd'
str2=''
print(str1+str2,str1*2)#结果为asd六 asdasd

#bool True和False
View Code

4、input 用户交互

1 username=input('请输入您的账号>>>')
2 password=input('请输入您的密码>>>')
View Code

5、%d(输出的是数字类型) %s(输出的是字符串类型) 格式化输出

username=input('请输入您的账号>>>')
password=input('请输入您的密码>>>')
msg='您的账号是%s,您的密码是%d'%(username,int(password))#int(password)是把字符串类型变成数字类型
print(msg)#结果为  您的账号是qwe,您的密码是123
View Code

6、逻辑运算

'''
not:not False==Trun,反之等于False
and:一真一假为假,两个真为真;两个假为假
or:一真一假为真;两个真为真;两个假为假
优先级关系:() > not > and > or
'''
#6.1、and、or、not运算符运算
print(not 2>1 and 3<2 or 4>1)#F and F or T -->F or T --> T

#6.2、x or y 如果x为True,则返回x;否则返回y;x and y 如果x为True,则返回y;否则返回x
print(1 or 2) #结果为1
print(0 or 3) #结果为3
print(1 and 2) #结果为2
print(0 and 2) #结果为0

#6.3、混合运算
print(0 or 1 and 3 or 4 and 5) #0 or 3 or 5 --> 3 or 5 -->3
print(1>2 and 3 or 4 and 3<2) #F and 3 or 4 and F --> F or F --> F
View Code

7、数据类型转换

#int --> bool 非零转换成True,零转换成False
print(bool(1),bool(-1),bool(0))#True True False

#bool --> int 只有1和0
print(int(True),int(False))#1 0

#str --> int 不能转换带字母和特殊字符的
s='123e'
print(int(s))#报错
s='123'
print(int(s),type(int(s)))#123

#int --> str
i=1
print(str(i),type(str(i)))

#str --> bool 空字符串转换成False,非空字符串转换成True
s=''
s1='as'
print(bool(s),bool(s1))
View Code

8、int数字类型的操作方法

#bit_length 计算该数字的二进制的有效位数
i=100
print(i. bit_length())#7
View Code

9、字符串的索引和切片 (PS:索引从左往右是从0开始,即:a是0;从右往左是从-1开始,即:g是-1;切片:顾头不顾尾)

s='abcdefg'
print(s[0],s[-2])#a f
print(s[0:3],s[:],s[0:])#abc abcdefg abcdefg
print(s[0:5:2],s[-1::-1],s[::-1],s[-1:-6:-2])#ace gfedcba gfedcba gec
View Code

10、字符串的操作方法

s=' xiao Le Xian Sheng 1asda23\ta'
print(s.capitalize())#首字母大写
print(s.upper())#字母全部大写
print(s.lower())#字母全部小写
print(s.swapcase())#反转,小写字母变大写,大写字母变小写
print(s.title())#只要用空格或特殊字符还有数字隔开的单词都首字母大写
print(s.center(32,'*'))#居中,不填写填充物,默认为空白填充,也可以自己设定填充物
print(s.expandtabs())#只要字符串出现‘\t’的,在‘\t’前面字符自动补全到8位,超过8位的补全到16位,超过16位的补全到32位
print(s.startswith('x'),s.startswith('x',1,5))#检查以什么开头,返回True和False,还可以指定位置检查
print(s.endswith('a'),s.endswith('3',-3,0))#检查以什么结尾,返回True和False,还可以指定位置检查
print(s.find('a'),s.find('q'),s.find('ao'),s.find('a',1,8))#通过元素找索引,找不到返回-1,可以多个元素一起找,但只返回最前面那个元素的索引
# print(s.index('a'),s.index('q'))#通过元素找索引,但找不到会报错
print(s.strip(),s.strip('a'))#默认删除前后空格,可以添加删除的元素,但只会删除前后的,遇到字符就停下来
print(s.rstrip('a'))#只删除后面的指定元素,默认删除空格
print(s.lstrip())#只删除前面的指定元素,默认删除空格
print(s.count('x'),s.count('q'))#统计个数,没有返回0
print(s.split(),s.split('x'))#拆分分割,默认为空格为分割符号,也可指定元素为分割符号(PS:str转换成list的方法)
print(s.replace('xiao Le','小樂'),s.replace('xiao Le','小樂',1))#替换,默认替换全部,也可指定次数
print(s.isalpha())#判断字符串是否由字母组成
print(s.isdigit())#判断字符串是否由数字组成
print(s.isalnum())#判断字符串是否由字母和数字组成
#for 循环输出字符串元素
for i in s:
    print(i)
View Code
#format三种用法
s='我叫{},喜欢{},再说一次我叫{}'.format('liuel','youxi','liule')
print(s)
s1='我叫{0},喜欢{1},再说一次我叫{0}'.format('liuel','youxi')
print(s1)
s3='我叫{name},喜欢{hobby},再说一次我叫{name}'.format(hobby='youxi',name='liule')
print(s3)
View Code

11、列表的索引和切片(跟字符串索引和切片一样)

li=['alex',[1,2,3],'user','age','','abc']
print(li[0],li[0:],li[0:5:2])
View Code

12、列表的增删改查

#
li=['alex',[1,2,3],'user','age','','abc']
li.append('hhh')#将添加的元素添加到最后面
li.insert(2,'name')#将需要添加到规定索引的元素,添加到规定的索引
li.extend('asd')#迭代增加到列表的后面,不能迭代int类型的数据

#
li=['alex',[1,2,3],'user','age','','abc']
li.pop(0)#按索引删除元素,有返回值
li.remove('user')#按元素删除,没有会返回值,会返回一个None
del li[0:2]#按切片去删除,但顾头不顾尾,比如:删除到索引为2的元素,他只会删除索引为0和1的元素
li.clear()#清空列表里的元素
del li#删除整个列表

#
li=['alex',[1,2,3],'user','age','','abc']
li[0]='name'#按索引去改
li[0:2]='123'#切片去改,先删除索引为0和1的元素,不会删除索引为2的元素(顾头不顾尾),再把需要添加的元素迭代增加

#
li=['alex',[1,2,3],'user','age','','abc']
print(li[0:2])#切片查看,顾头不顾尾
#for循环查看
for i in li:
    print(i)
View Code

13、排序

li=[1,3,5,7,8,2,4,6]
li.sort()#正向排序
li.sort(reverse=True)#反向排序
li.reverse()#反转
print(li)
View Code

14、列表嵌套

li=['alex',[1,2,'name'],'user','age','','abc']
print(li[0][2])#查找索引为0的元素,查找到之后,查找这个元素索引为2的元素
print(li[1][2][2])
View Code

15、元祖  只能查,不能改

tu=(1,2,'alex',[1,2,'liu'],3,'xiao')
print(tu[0])#通过索引查找看元素
print(tu[0:2])#切片查看,顾头不顾尾
#for循环输出元祖
for i in tu:
    print(i)
View Code

16、join和range操作方法

s='alex'
li=['ad','cd']
print('_'.join(s),''.join(li),type(''.join(li)))#以自己规定的符号隔开,也是列表转成字符串的一种方法

for i in range(0,10):
    print(i)#正序输出1到9
for i in range(10,0,-1):
    print(i)#倒序输出10到1
for i in range(0, 10, -1):
    print(i)  # 结果是空白,不会报错
View Code

17、字典的增删改查

#
dic1={
    'age':18,
    'name':'liu',
    'sex':'',
}
dic1['hobby']='name'
dic1['age']=20#有键值对就覆盖,没有就增加,也可用作字典的修改
dic1.setdefault('yundong')
dic1.setdefault('age','23')#有键值对的不做任何修改,没有的就增加,没有值会默认添加None
print(dic1)

#
dic1={
    'age':18,
    'name':'liu',
    'sex':'',
    'hobby':'youxi',
    'user':'xiaole',
    'pass':123
}
dic1.pop('name')#按键去删,有返回值
dic1.popitem()#随机删除
del dic1['age']#按键去删,没有返回值
dic1.clear()#删除字典里所有元素
del dic1#删除整个字典
print(dic1)

#
dic1={
    'age':18,
    'name':'liu',
    'sex':'',
}
dic2={
    'age':20,
    'hobby':'youxi'
}
dic1.update(dic2)#把dic2的键值对覆盖到dic1的键值对去,有键值对的覆盖,没有就增加
print(dic1,dic2)

#
dic1={
    'age':18,
    'name':'liu',
    'sex':'',
}
print(dic1.keys(),dic1.values(),dic1.items())#依次打印键、值和键值对
print(dic1['name'])#通过键去查看,没有这个键会报错
print(dic1.get('hobby',None))#get也是通过键去查看,没有这个键的会返回你设置返回值
#for循环查看
for i in dic1.keys():
    print(i)
for i in dic1.values():
    print(i)
for x,y in dic1.items():
    print(x,y)
View Code

18、集合的增删查

#
set={'alex','liu','xiao','age'}
set.add('name')#随机在某个位置增加
set.update('pass')#随机在迭代增加
print(set)

#
set1={'alex','liu','xiao','age'}
set1.pop()#随机删除
set1.remove('alex')#指定元素删除
set1.clear()#删除集合里所有元素
del set1#删除整个集合
print(set1)

#
set={'alex','liu','xiao','age'}
for i in set:
    print(i)
View Code

19、交集(&和intersection)、并集(|和union)、差集(-和difference)、反交集(^和symmetric_difference)、子集(<和issubset)和超集(>和issuperset)

set1={1,2,3,4,5}
set2={4,5,6,7,8}
print(set1.intersection(set2),set1 & set2)
print(set1.union(set2),set1 | set2)
print(set1.difference(set2),set1 - set2)
print(set1.symmetric_difference(set2),set1 ^ set2)
print(set1.issubset(set2),set1 < set2)
print(set2.issuperset(set1),set2 > set1)
View Code

20、深浅copy

#浅copy(copy和:)  拷贝出来的列表第一层与原来是不同的内存地址,第二层指向原来的地址
l1=[1,2,3,['a','b',1],'c']
l2=l1.copy()
l1[0]=5
l1[3][0]='s'
print(l1,l2)

#深copy  拷贝所有内容. 包括内部的所有,形成一个新的对象,虽然与之前的值和内容一模一样,但是它们完完全全的两个对象
import copy
l1=[1,2,3,['a','b',1],'c']
l2=copy.deepcopy(l1)
l1[0]=5
l1[3][0]='s'
print(l1,l2)
View Code

21、编码和解码

#str --->byte  encode 编码
s = '二哥'
b = s.encode('utf-8')
print(b)

#byte --->str decode 解码
s1 = b.decode('utf-8')
print(s1)
View Code

22、文件操作

x=open('文件的名字',mode='r',encoding='utf-8')
print(x.read())
x.close()
y=open('某盘:\文件名.后缀',mode='r',encoding='utf-8')
print(y.read())
y.close()
相对路径和绝对路径读取文件
x=open('文件的名字',mode='r',encoding='utf-8')
print(x.read())
x.close()
x=open('文件的名字',mode='rb')
print(x.read())
x.close()
只读 r rb
#如果没有这个文件就创建,有这个文件就覆盖里面的内容
x=open('文件的名字',mode='w',encoding='utf-8')
x.write('hhhh')
x.close()
x=open('文件的名字',mode='r')
x.write('xxxxx'.encode('utf-8'))
x.close()
只写 w wb
x=open('文件的名字',mode='a',encoding='utf-8')
x.write('hhhh')
x.close()
x=open('文件的名字',mode='ab')
x.write('xxxxx'.encode('utf-8'))
x.close()
追加 a ab
x=open('文件的名字',mode='r+',encoding='utf-8')#先读取文件,在添加
print(x.read())
x.write('hhhh')
x.close()

x=open('文件的名字',mode='r+',encoding='utf-8')#先添加内容,会覆盖原有的一些内容,在读取为覆盖的内容
x.write('hhhh')
print(x.read())
x.close()

x=open('文件的名字',mode='r+b')#读取是bytes类型
print(x.read())
x.write('hhhh'.encode('utf-8'))
x.close()
读写 r+ r+b
x=open('文件的名字',mode='w+',encoding='utf-8')
x.write('hhhh')
x.seek(0)
print(x.read())
x.close()

x=open('文件的名字',mode='w+b')
x.write('hhhh'.encode('utf-8'))
x.seek(0)
print(x.read())
x.close()
写读 w+ w+b

23、文件操作的操作方法

x=open('文件的名字',mode='r',encoding='utf-8')
print(x.read(3))#按字符定光标,然后输出字符
x.seek(0)#按字节定光标位置
print(x.tell())#检测光标位置
print(x.readable())#检测是否可读
print(x.readline())#一行一行的读
print(x.readlines())#把每一行都当成元素,添加到列表
#x.truncate(2)#截取,要在r+模式才能用
x.close()

#同时打开多个文件,这个也可以不用close关闭文件的命令
with open('文件的名字',mode='r',encoding='utf-8') as x,\
        open('文件的名字',mode='r',encoding='utf-8') as y:
    print(x.read())
    print(y.read())
View Code

 

posted @ 2019-08-24 10:58  精灵飞舞之季的低语  阅读(168)  评论(0编辑  收藏  举报