美文网首页
【第14天】python全栈从入门到放弃

【第14天】python全栈从入门到放弃

作者: 36140820cbfd | 来源:发表于2019-08-10 22:43 被阅读0次

1过滤掉列表中长度小于3的字符串,并将剩下的转换成大写字母

代码块
lst=['wangsiyu','nez','zhangdejun','xus','hulud']
new=[i.upper() for i in lst if len(i)>3]
print(new)   #['WANGSIYU', 'ZHANGDEJUN', 'HULUD']

2. 求(X,Y),其中X是0-5之间偶数,Y是0-5之间奇数组成的元组组合里列表

代码块
res=[]
for i in range(6):
    if i %2==0:
        for l in range(6):
            if l%2==1:
                res.append((i,l))

print(res)   #[(0, 1), (0, 3), (0, 5), (2, 1), (2, 3), (2, 5), (4, 1), (4, 3), (4, 5)]
代码块:使用列表推倒式
res=[(x,y) for x in range(6) if x%2==0  for y in range(6) if y%2==1]
print(res)

3.将M=[[1,2,3],[4,5,6],[7,8,9]]变成[3,6,9]

代码块
M=[[1,2,3],[4,5,6],[7,8,9]]
res=[]
for i in M:
    res.append(i[2])
print(res)

4. M=[3,6,9]变成[[1,2,3],[4,5,6],[7,8,9]]

代码块
M=[3,6,9]
res=[]
for i in M:
    a=[i-2,i-1,i]
    res.append(a)
print(res)   #[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
代码块:第二种方法
M=[3,6,9]
res=[[i-2,i-1,i] for i in M]
print(res)

5. range的步长参数

代码块
for i in range(1,10,2): # 从1到9,隔2个取值
    print(i,end=' ')   #1 3 5 7 9
print()
for i in range(20,10,-2):
    print(i,end= ' ')   #20 18 16 14 12 

6.迭代器iter()和next()

代码块
res=list(range(10))
a=iter(res)   #就等于res.__iter__()
print(next(a))   #就等于res.__next__()
print(next(a))   #就等于res.__next__()
print(next(a))   #就等于res.__next__()

7. print()函数的参数

代码块
print('wangsiyu','mayun',sep='__',end='我是结尾')   #wangsiyu__mayun我是结尾

8. hash算法

目的是唯一性

dict 查找效率非常高, hash表.用空间换的时间 比较耗费内存

代码块
name='wangsiyu'
print(hash(name))    #-1029105058

num=123
print(hash(num))   #123

lst=[1,2,3,4]
# print(hash(lst))   #列表可变,不能hash

9. 动态导入模块

代码块
import os
mo=input('请输入你要导入的模块:')
__import__(mo)
print(dir(mo))   #打印该模块下的方法

10. 用help()帮助自己

print(help(input))

11, 打印某个变量的类型

代码块
name=123
print(type(name))   #<class 'int'>

12, 十进制,八进制,十六进制之间的转换

代码块
print(bin(10))  #十进制转二进制
print(hex(10))  #十进制转十六进制
print(oct(10))  #十进制转八进制

13. callable()查看是否可以被调用

代码块
def func():
    print(333)

name='wangsiyu'

print(callable(func))   #True
print(callable(name))   #False

14 eval()函数

代码块
res=input('请输入a+b:')
print(eval(res))  #可以动态执行,必须有返回值

15 exec,eval,compile

代码块
code='for i in range(10):print(i)'
res=compile(code,'',mode='exec')  #编译
print(res)   #<code object <module> at 0x02A69B78, file "", line 1>
exec (res)   #执行

code2='4+6+7'
res2=compile(code2,'',mode='eval')
print(res2)   #<code object <module> at 0x03989C28, file "", line 1>
print(eval(res2))   #17

code3="name=input('请输入姓名')"
res3=compile(code3,'',mode='single')
exec (res3)

16. encode/decode/bytesarray/bytes

代码块
a='骑行'
res1=a.encode('utf-8')
print(res1)   #b'\xe9\xaa\x91\xe8\xa1\x8c'

res2=res1.decode('utf-8')
print(res2)  #骑行


b=bytes('人民',encoding='utf-8')
print(b)  #b'\xe4\xba\xba\xe6\xb0\x91'
print(b.decode('utf-8'))

c=bytearray('日本',encoding='utf-8')
print(c.decode('utf-8'))

17 查看字节在内存中的地址

代码块
name='中国'.encode('utf-8')
print(name)
loca=memoryview(name)
print(loca)   #<memory at 0x02C4CF38>
print(id(name))  #47469152

18 enumerate枚举的使用

代码块
name=['wangsiyu','nezha','sunwukong','xusong']
for i,l in enumerate(name):
    print(i,l)
    
# 输出:
# 0 wangsiyu
# 1 nezha
# 2 sunwukong
# 3 xusong

19 zip函数的使用

代码块
name=['wangsiyu','nezha','xusong']
age=['17','32','19']
hobby=['sex','kiss','sleep']

for i in zip(name,age,hobby):
    print(i)

输出:
('wangsiyu', '17', 'sex')
('nezha', '32', 'kiss')
('xusong', '19', 'sleep')

20 其它内置函数

代码块
print(sum(list(range(10))))    #sum求和

var='你好啊'
res=reversed(var)
print(tuple(res))     #('啊', '好', '你')

lst=[1,2,3,4,5,6,7,8]
print(lst[1:5:2])   #[2, 4]

name='你好\n%我是爸爸'
print(name)
print(repr(name))   #'你好\n%我是爸爸' 原样输出

print(ord('a'))       #97返回a在ascii表中的位置
print(ord('智'))     #返回在国标码的位置   26234

print(chr(97))    #知道位置,计算字符
print(chr(26234))   #智

for i in range(128):   #打印ascii表的内容
    print(chr(i),end=' ')


别跑,点个赞再走

相关文章

网友评论

      本文标题:【第14天】python全栈从入门到放弃

      本文链接:https://www.haomeiwen.com/subject/uonrjctx.html