[Python] notes, string, for, while

Key words

enumerate, zip, single line 

 

 

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

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

# Time complexity problems, O (n)

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

print(str1)


This is not to say # split
# About strip
# Lstrip ( 'e'), rstrip ( 'e') this is also the schematic 
# Introduce the use sting.find function

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

key_word='is'
print(s4.find(key_word,1,10))
Results are shown as # 8, which is 'i' index positions
print(s4[8])
print(s4[8:10])

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

# Format function and% s% d

# It can only be this way 
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'))

 

"""
Questions
Mode 1
s=''
for n in range(10000):
s+=str(n)
print(s)
Mode 2
l=[]
for n in range(10000):
l.append(str(n))
s=''.join(l)


Mode 3, then someone suggested way
s=''.join(map(str,range(0,10000)))

tips:
1 embodiment complexity O (n), the mode 2 is O (n)
But the actual join, and append piecing speed faster than + = s, then when the data will be faster than that of 1000w
Of course, the most reasonable way or 3

"""

 

# 7, the input and output

 


#8,
Key # 
# Single line cycle mode, a plurality of rows corresponding to a cyclic manner
# Time complexity associated
Scene and for the difference #while

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 must be combined if use 
# Omitted induction conditional wording


#dict dictionary cycle
d1 = { "all": 1, 'bob': 2 'celin': 3}
for k,v in d1.items():
print (p (k), v)

# Key: value # omitted way

# Enumerate use
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
# Interactive use while answering system
# While input: # as long as the input is not empty on the implementation of
# For higher efficiency
if # fit for single-line mode
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)

# another way
'''
out1=[]
for xx in x:
for yy in y:
if xx != yy
out.append((xx,yy))

'''

# Questions
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)

  

 

Guess you like

Origin www.cnblogs.com/allen514519/p/10926397.html