[python] 笔记,string ,for,while

关键词

enumerate ,zip,单行 

 

 

-------------------------------------------------------------------------

#6,字符串 
import time
s1='hello'
print(s1[0]+'\t'+s1[1:])
s2='H'+s1[1:]
print(s2)
s3="""can be print!!!"""
print(s3)

# 时间复杂度问题, O(n)

#
l=[]
for i in range(100):
l.append(str(i))
str1=''.join(l)

print(str1)


# split 这个就不说了
# 关于 strip
# lstrip('e') , rstrip('e') 这个也简略了 
#介绍 sting.find 函数的使用

s4='my name is Allen '
s5=s4.strip()
print ((s4)+(s1))
print ((s5)+(s1))

key_word='is'
print(s4.find(key_word,1,10))
# 结果显示 为8 ,也就是 'i'的索引位置
print(s4[8])
print(s4[8:10])

start=s4.find(key_word,1,10)
end=start+len(key_word)
print(s4[start:end])

# format 函数 和 %s %d

# 只能 这样了 
print ('no data is availiable for person with id:{},name:{}'.format(232,'bobo'))
line='no data is availiable for person with id :{},name:{}'
print (line.format(123,'allen'))

old_line='no data is availiable for person with id:%d,name:%s'
print(old_line%(323,'celin'))

 

"""
思考题
方式1
s=''
for n in range(10000):
s+=str(n)
print(s)
方式2
l=[]
for n in range(10000):
l.append(str(n))
s=''.join(l)


方式3,后来有人建议的方式 是
s=''.join(map(str,range(0,10000)))

tips:
方式1复杂度O(n),方式2的是 O(n)
但是 实际 append 以及join拼接速度 比s+= 要快,于是在 1000w 以上的数据时 则会更快一点
当然最合理的还是 方式3

"""

 

#7,输入和输出

 


#8,
#重点 
#单行循环方式,对应多行循环方式
#时间复杂度相关
#while 和for 的场景区别

print ('chaper 9')
for i in range(3):
if i==0:
print('no one')
elif i==1:
print ('only 1')
elif i==2:
print('heheh 2 ')
print('-'*30) 

# else elif 必须结合if使用 
#省略条件判断 的写法归纳


#dict 字典的循环
d1={'allen':1,'bob':2,'celin':3}
for k,v in d1.items():
print(str(k),v)

# key:value #方式省略

# enumerate使用
print('-'*30) 
l=[1,2,3,4,5,6,7,8]
for index,item in enumerate(l):
if index <5:
print(index,item)

# continue ,break

# for ,while
# 交互问答系统 使用while
# while input: # 只要input 不是空就执行
# for 的效率更高
# 单行模式的 if 配合for
print('-'*30) 
#str='value is bigget than 4' if i>4 else '4 is bigger one' for i in l
x=[1,3,5,7,11,15,17,19]
y=[3,6,9,12,15,18]
out=[]
out=[(xx,yy) for xx in x for yy in y if xx != yy]
print(out)

# 另一种方式
'''
out1=[]
for xx in x:
for yy in y:
if xx != yy
out.append((xx,yy))

'''

#思考题
print('-'*30)

attr=['name','dob','gender']
values=[['allen','11111','male'],
['bob','22222','male'], 
['celin','33333','female'] ]

out2=[dict(zip(attr,value))for value in values]
print(out2)

  

 

转载于:https://www.cnblogs.com/allen514519/p/10926397.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值