#test4.1.py
     v1= "Hello"
     v2= "world"
     print("v1+v2 ",v1+v2)
     print("v1* 2 ", v1* 2)
     print("v1[1] ", v1[1])
     print("v1[1:4] ", v1[1:4])
     if( "H" in v1) :
         print("H ڱ v1 ")
     else :
         print("H ڱ v1 ")
     if( "M" not in v1) :
         print("M ڱ v1 ")
     else :
         print("M ڱ v1 ")
     print (r'\n')
     print (R'\n')
ϳнΪ
v1+v2  Helloworld
v1* 2  HelloHello
v1[1]  e
v1[1:4]  ell
H ڱ v1 
M ڱ v1 
\n
\n

4.2һַ෴˳
ʵ£#test4.2.py
v1=input("Please input a string:")
for i in range(0,len(v1)):
    print(v1[-i-1]end=)
£
Please input a string:abc
cba

4-3  ַƬһַеַ
ʵ£
#test4.3.py
v1=input("Please enter a string:")
v2=v1[::-1]
print(v2)
£
Please enter a string:12345
54321
4-4ַȡʵ£
#Test4.4.py
str = 'nihao'
print (str)          	# ַ
print (str[0:-1])    	# һڶַ
print (str[0])       	# ַһַ
print (str[2:5])     	# ӵʼַ
print (str[2:])      	# ӵʼַ֮
print (str * 2)      	# ַ
Ϊ
nihao
niha
n
hao
hao
nihaonihao


4-5ַѯ
ʵ£#test4.5py
     v1 = 'How are you!'
     v2 = " I am studenting Python "
     print ("v1[0]", v1[0])#±Ϊ0Ԫ
     print ("v2[1:5]", v2[1:5])#ע±1ʼ5
ϳнΪ
     v1[0] H
     v2[1:5] I am
4-6ַƴӡʵ£
#!test4.6.py
     v1='How re you!'
     v2='I am studeying Python'
     print ("Ѹַ", v1[:6] +v2[:5])ϳнΪ
Ѹַ How reI am
4-7ַʽʵ£
#test4.7.py

print (" %s  %d ꣡" % ('xiaomi', 20))
ϳнΪ
 xiaomin  20 ꣡
ʹformat()ĸʽ£
ģַ.format()
4-8ʹformat()ʽƽ2ˡ
#test4.8.py
>>> a,b=3,6
>>>print("{}{}ĳ˻Ϊ:{}".format(a,b,a*b))
36ĳ˻Ϊ:18
4-9ͨתַĻ
ʵ£test4.9.py
>>> print("ô\nҪѧpython")

ô
Ҫѧpython
>>> print('\x64')#16ӦASCIIĵ100ַΪַd
>>> print('\064')# 8ӦASCIIĵ52ַΪַ4
4-10  ĸСдתʹʾ
£test4.10.py
a='helLO'
print(a.upper())    # ȫд
print(a.lower())    # ȫСд
print(a.swapcase()) # Сд
print(a.capitalize())   # ĸдСд
print(a.title())    # ĸд
4-11  ַ봦ʹʾ
£test4.11.py
a='3 4'
print(a.ljust(10))  # ȡ̶ȣ룬ұ߲ÿո
print(a.rjust(10))  # ȡ̶ȣҶ룬߲ÿո
print(a.center(10)) # ȡ̶ȣм룬߲ÿո
print(a.zfill(10))  # ȡ̶ȣҶ룬߲0
Ϊ
3 4       
       3 4
   3 4    
00000003 4
4-12  ַʹʾ
#test4.12.py
a='hello world'
print(a.find('e'))  # ַָ,ûз-1    1
print(a.find('w',1,2))  # ͷβҲ򷵻-1ᱨҵʾ  -1
print(a.index('w',1,2)) # ͬϣҲᱨ
print(a.count('o')) # ͳֵַָĴ  2 
print(a.rfind('l')) # ұ߿ʼ     9


4-13  ַ滻ʹʾ
#test4.13.py
s='hello world'
print(s.replace('world','python'))
print(s.replace('l','p',2))
print(s.replace('l','p',5))

ִн
hello python
heppo world
heppo worpd
4-14 ַȥպʹʾ
#test4.14.py
a='  h e-l l o   '
print(a)
print(a.strip())
print(a.lstrip())
print(a.rstrip())
print(a.split('-'))#ʹַַָָΪ
print(s.split())
4-15  ַжϺʹʾ
£#test4.15.py

s='i love python'
print('{:s} isalnum={}'.format(s,s.isalnum()))
print('{:s} isalpha={}'.format(s,s.isalpha()))
print('{:s} isupper={}'.format(s,s.isupper()))
print('{:s} islower={}'.format(s,s.islower()))
print('{:s} isdigit={}'.format(s,s.isdigit()))
s='5678'
print('{:s} isdigit={}'.format(s,s.isdigit()))i love python isalnum=False
н
i love python isalpha=False
i love python isupper=False
i love python islower=True
i love python isdigit=False
5678 isdigit=True



4-16ѯбԪء
ʵ£
#!/test4.16.py

list1 = ['Python', 'nanfang', 2021, 2022];
list2 = [1, 2, 3, 4, 5, 6, 7 ,8,9,10];

print ("list1[1] :  ", list1[1])
print ("list2[3:8] :  ", list2[3:8])
ϳнΪ
     list1[1] :   nanfang
     list2[3:8] :   [4, 5, 6, 7, 8]
4-17бԪءʵ£
#test4.17.py
List1 = ['Python', 'south, 2021,2022]
print ("һԪΪ", list1[1])
list1[1] =north
print ("ºĵһԪΪ", list1[1])
ϳнΪ
     ڶԪΪ south
     ºĵڶԪΪ north  
4-18ɾбԪءʵ£
#!/test4.18.py

List1 = ['Python', 'south, 2021,2022]
print (list1)
del list1[1]
print ("ɾһԪأ", list1)
ϳнΪ
['Python', 'south, 2021,2022]
ɾһԪأ['Python', 20212022]
DelɾбĵԪأɾ
>>> list1=[]
>>> list1
[]
>>> del list1
>>> list1#ʱѳΪɾNameError: name 'list1' is not defined
4-19ʹúset()бظԪءʵ£
     #test4.19.py
     List1 = [1,1,2,3,4,6,6,2,2,9]
     List1 = list(set(lists))
     print(list1) 

4-20бȡӣʵ£
#test4.20.py
     lst1 = [ 'abc', 123 ,4.56, 'def']
     lst2 = [456, 'hij']
     print (lst1 )        # бlist1
     print (lst1[0])      # бlist1ĵһԪ
     print (lst1[1:3] )    # list1ڶԪ 
     print (lst1[2:] )      # list1ӵʼбĩβԪ
     print (lst2 * 2  )    # бlist2
     print (lst1 + lst2 )   # ӡϵбlist1,list2н
     ['abc', 123, 4.56, 'def']
     abc
     [123, 4.56]
     [4.56, 'def']
     [456, 'hij', 456, 'hij']
     ['abc', 123, 4.56, 'def', 456, 'hij']
4-21һλ4칫ң10˵ȴλķ䣬дķǶбǶб
     ʵ£
     #test4.21.py
     import random
     offices = [[],[],[],[]]# һб4칫,Ƕб
     names = ['A','B','C','D','E','F','G','H','I','J']# һб洢10λ˵
     i = 0
     for name in names:
         index = random.randint(0,3)    
         offices[index].append(name)
     i = 1
     for temp in offices:
         print('칫%dΪ:%d'%(i,len(temp)))
         i+=1
         for name in temp:
             print("%s"%name,end='-')
         print("\n")
    print(offices)
     н칫1Ϊ:2
     C-J-
     칫2Ϊ:4
     D-F-G-H-
     칫3Ϊ:2
     A-E-
     칫4Ϊ:2
     B-I-
     [['C', 'J'], ['D', 'F', 'G', 'H'], ['A', 'E'], ['B', 'I']]
4-22ѯԪеԪءʵ£
#test4.22.py
tup1 = ('Python', 'nanfang', 2020, 2021)
tup2 = (7,6,5,4,3,2,1 )
print ("tup1[0]: ", tup1[0])
print ("tup2[1:5]: ", tup2[1:5])
ϳнΪ
tup1[0]:  Python
tup2[1:5]:  (6, 5, 4, 3)
4-23Ԫӡʵ£
#test4.24.py
tup1 = (1,2)
tup2 = ('a ', 'b')
# ޸ԪԪزǷǷġ
# tup1[0] = 3
# һµԪ
tup3 = tup1 + tup2
print (tup3)ϳнΪ
(1, 2, 'a ', 'b')
4-24ɾԪ顣ʵ£
#test4.24.py
tup = ('Python', 'nanfang', 2020, 2021)
print (tup)
del tup
print ("ɾԪ tup : ")
print (tup) 
ʵԪ鱻ɾʱ쳣ϢϳнΪ
('Python', 'nanfang', 2020, 2021)
ɾԪ tup : 
Traceback (most recent call last):
  File "D:/python/Python38/124.py", line 5, in <module>
    print (tup)NameError: name 'tup' is not defined
4-25ȡԪеԪءʵ£
#test4.25.py

tup= ('Python', 'java, 'php')
print(tup[2])
print(tup[-2])
print(tup[1:])
ϳнΪ
php
java
('java', 'php')
4-26ͳ㡣ʵ£
#test4.26.py
a = set('abcde')
b = set('defg')
print(a)
print(a - b)    	# abĲ 
print(a | b)     	# abĲ
print(a & b)     	# abĽ
print(a ^ b)    	# abвͬʱڵԪ
ϳнΪ
{'b', 'a', 'c', 'e', 'd'}
{'b', 'a', 'c'}
{'b', 'a', 'c', 'd', 'e', 'g', 'f'}
{'d', 'e'}
{'b', 'a', 'c', 'g', 'f'}
4-27ϱȽ㡣ʵ£
#test4.27.py
s1={1,2,3,4,5}
s2={5,4,3,2,1}
s1==s2
s1!=s2
s1>s2
s1<s2
s1>=s2
s1<=s2
ϳнΪ
True  False False  False  True  True
4-28ϱ㡣ʵ£
#test4.28.py
s={1,2,3,4,5}
temp=0
for x in s:
              print(x,end='|')#ʹ÷ָ
              temp+=x
print(temp)

ϳнΪ
1|2|3|4|5|15







4-29ѯֵеļֵʵ£
#test4.29.py
     dict = {'': 'ǿ', '': 17, 'רҵ': ''}
     print ("dict['']: ", dict[''])
     print ("dict['רҵ']: ", dict['רҵ'])ϳнΪ
     dict['']:  ǿ
     dict['רҵ']:  
4-30޸ֵеļֵʵ£
#test4.30.py
dict = {'': 'ǿ', '': 17, 'רҵ': ''}
dict[''] = 18;               # 
dict['ѧУ'] = "ɽѧ"  # һֵ
print ("dict['']: ", dict[''])
print ("dict['ѧУ']: ", dict['ѧУ'])
ϳнΪ
dict['']:  18
dict['ѧУ']: ɽѧ
4-31ɾֵеļֵʵ£
#test4.31.py
dict = {'': 'ǿ', '': 17, 'רҵ': ''}
del dict[''] 	# ɾ 
dict.clear()     	# ֵ䣬
del dict         	# ɾֵ
print ("dict['']: ", dict[''])
print ("dict['ѧУ']: ", dict['ѧУ'])
нһ쳣Ϊִ del ֵͲˣнΪ
Traceback (most recent call last):
  File "D:/python/Python38/111.py", line 5, in <module>
    print ("dict['']: ", dict[''])
TypeError: 'type' object is not subscriptable
4-32ð򷨣ʵֽһбӴС
ȣ˽ʲôð
ðǽǽһԪأͨ㷨ǱΪΪˮðһһһð
ðԭ
ͨһԪԪؽбȽϣѡСһȻıȽϣֱȽһ
Ҳ˵ͨһֱȽϺбĩβԪӦбСԪء
ʵ£#ע֮ҪԶ
lst = [11,22,44,33,66,55,77,99,88,110]
for j in range(len(lst)-1):
              for i in range(len(lst)-1-j):
                             if lst[i] < lst[i+1]:
                                  lst[i],lst[i+1] = lst[i+1],lst[i]
print(lst)
нΪ
[110, 99, 88, 77, 66, 55, 44, 33, 22, 11]
