week 1基礎データ型試験

31788 ワード

#  ,   。
# 1, (3 )

1. , ,
2.
3.
4.
5. python

# 2,       。(2 )

0 1 bit( ), 。
bit , 。
8bit = 1bytes

# 3,’  ’  utf-8   ,        ,   ?  gbk   ,        ,   

utf-8: 48 , 6
gbk:32 ,4
   unicode:32 4
# 4, , 。(12 )

s.capitalize() ,
s.count str
s.split ,
s.upper
s.startswith
s.endswith
s.center ,
s.swapcase
s.strip()
s.find()
s.index
s.replace() ,
len(s)


# 5, , , , , False ?(5 )

:0, '', [], (), {}

# 6,  Python2 python3      。(3 )

python2:1. 2.print() print{' ')3. ascii
python3:1. 2.print(' ')3. :utf-8
# 7,   ,     ,           (    ,  4 )

# li = [1,3,2,’a’,4,’b’,5,’c’]
# li l3,l3 = [’1,2,4,5]
# li l4,l4 = [3,’a’,’b’]
# li l5,l5 = [‘c’]
# li l6,l6 = [‘b’,’a’,3]
li = [1,3,2,'a',4,'b',5,'c']
print(li[0:-1:2])
print(li[1:-2:2])
print(li[-1::])
print(li[-3::-2])
# 8,     。
# a, , , ( 3 , 1 , 3 。 9 )
# ( )
# lis = [[‘k’,[‘qwe’,20,{‘k1’:[‘tt’,3,’1’]},89],’ab’]]
# lis ’tt’ ( )。
# 3 ’100’( )。
# ’1’ 101( )。

lis = [['k',['qwe',20,{'k1':['tt',3,'1']},89],'ab']]
print(lis[0][1][2]['k1'][0].upper())


lis[0][1][2]['k1'][0] = 'TT'
print(lis)

lis2=lis[0][1][2]['k1'][1] = '100'
print(lis)

lis3=lis[0][1][2]['k1'] = ['tt','100',1]
print(lis)

lis4=lis[0][1][2]['k1'] = ['tt',3,101]
print(lis)

lis5=lis[0][1][2]['k1'][2]=101
print(lis)
# b,   ,     ,           (5 )
# dic = {‘k1’:’v1’,’k2’:[‘alex’,’sb’],(1,2,3,4,5):{‘k3’:[‘2’,100,’wer’]}}
# ’k2’ ’23’。
# ’k2’ ’a’。
# (1,2,3,4,5) ’k4’,’v4’。
# (1,2,3,4,5) (1,2,3),’ok’。
# ’k3’ ’wer’ ’qq’。
dic = {'k1':'v1','k2':['alex','sb'],(1,2,3,4,5):{'k3':['2',100,'wer']}}
dic['k2'].append('23')
print(dic)

dic['k2'].insert(0,'a')
print(dic)

dic[(1,2,3,4,5)]['k4']='v4'
print(dic)

dic[(1,2,3,4,5)][(1,2,3)]='ok'
print(dic)

dic[(1,2,3,4,5)]['k3'][2]='qq'
print(dic)
# 9,   (4 )。

# Int str , ? ?
int ''  str  str          int(str)
# Int   bool      ,        ?     ?
False---->int  0    True---->int 1
 0   ,0  
# str   bool      ,        ?     ?
       bool  False     True
# str   list     ?    ?
str -> list    , split
list -> str join
# 10,      (5 )。
# li = [‘alex’,’wusir’,’rain’] s=’alexwusirrain’
# li = [‘alex’,’wusir’,’rain’] s=’alex*wusir*rain’
# s = ‘alexwusirlex’, li = [‘a’,’exwusirlex’]
# s = ‘alex wusir’, li = [‘alex’,’wusir’]
# s = ‘alex’ s1 = ‘a_l_e_x’
li = ['alex','wusir','rain']
si=''.join(li)
print(si)

s2='*'.join(li)
print(s2)

s ='alexwusirlex'
s3 = s.split('l',1)
print(s3)

s4 = 'alex wusir'
s5 = s4.split(' ')
print(s5)

s = 'alex'
s1 = '_'.join(s)
print(s1)
# 11,    while  , for    1-2+3-4+5.......+99   。(10 )
count = 0
sum = 0
while count < 100:
    if count %2 == 0:
        sum -= count
    else:
        sum += count
    count += 1
print(sum)


sum = 0
for i in range(1,100):
    if i %2 == 0:
        sum -= i
    else:
        sum += i
print(sum)
# 12,  range  100,99,98,....1,0(2 )

for i in range (100,-1,-1):
    print(i)
# 13,                           (       )(6 )
comment = input('    ').strip()
s = 0
for i in range(1,len(comment),2):
    if comment[i].isdigit():
        s = s + 1
print(s)
# 14,    (           ):(6 )
# li= [11,22,33,44,55,77,88,99,90], 66 key , 66 key 。
li = [11,22,33,44,55,77,88,99,90]
result = {}
for row in li:
    if row > 66:
        if 'k1' not in result:
            result['k1']=[]
        result['k1'].append(row)
    if row < 66:
        if 'k2' not in result:
            result['k2'] = []
        result['k2'].append(row)
print(result)
# 15,     li    ,         ,    ’A’  ’a’  ,  ’c’       ,          ,            。(3 )
li = ['taibai','alexC','AbC','egon','Ritian','Wusir','aqc']
l2 = []
for i in li:
    if (i[0] == 'A' or i[0] =='a') and i[-1]=='c':
        l2.append(i)
print(l2)

l2 = []
for i in li:
    i.strip()
    if (i.startswith('A') or i.startswith('a')) and i.endswith('c'):
        l2.append(i)
print(l2)
# 16,           :(3 )
#  :content = input(‘     :’)  #      :5+8+7....(         ),           ,               (  None):
# dic={‘ ’:None}

dic={'      ':None}
content = input('     ').strip().split('+')
sum = 0
for i in content:
    sum = int(i)+sum
print(sum)
dic['      ']=sum
print(dic)
 
# 17,         (        ,   4 )。(6 )
# list3 = [
# {"name": "alex", "hobby": " "},
# {"name": "alex", "hobby": " "},
# {"name": "alex", "hobby": " "},
# {"name": "alex", "hobby": "Massage"},
# {"name": "wusir", "hobby": " "},
# {"name": "wusir", "hobby": " "},
# ]

# ?
#
# list4 = [
# {"name": "alex", "hobby_list": [" ", " ", " ", "Massage"]},
# {"name": "wusir", "hobby_list": [" ", " "]},]

    
list4 = [
    {"name": "alex", "hobby_list": ["  ", "  ", "  ", "Massage"]},
    {"name": "wusir", "hobby_list": ["  ", "  "]},]

list5=[]
dic1={'name':'alex','hobby_list':[]}
dic2={'name':'wusir','hobby_list':[]}
for i in list3:
    if i['name'] == 'alex':
        dic1['hobby_list'].append(i['hobby'])
    else:
        dic2['hobby_list'].append(i['hobby'])
list5.append(dic1)
list5.append(dic2)
print(list5)

     ?
  :
dic ={'alex':
           {"name": "alex", "hobby_list": ["  ", "  ", "  ", "Massage"]},
      'wusir':
            {"name": "wusir", "hobby_list": ["  ", "  "]}
      }
print(list(dic.values()))
    :
dic = {}
for i in list3:
    if i['name'] not in dic:
        dic[i['name']]={'name':i['name'],'hobby_list':[i['hobby']],}
    else:
        dic[i['name']]['hobby_list'].append(i['hobby'])
print(list(dic.values()))
# 18,   :    hr           。(10 )
# 1), :
# user_list = [
# {'username':'barry','password':'1234'},
# {'username':'alex','password':'asdf'},
# ]
# 2) :board = [' ',' ',' ']
# 3)Hr , ( , , Q q ), Hr , board , , *( ****), user_list , , user_list , , , 。
board = ['  ','   ','    ']
user_list=[]
while True:
    username = input('      :Q/q  ').strip()
    if username.upper() == 'Q':break
    password = input('').strip()
    for i in board:
        if i in username:
            username = username.replace(i,'*'*len(i))
    user_list.append({'username':username,'password':password},)
    print('     %s,   %d' % (username, int(password)))
print(user_list)
 
 

転載先:https://www.cnblogs.com/kateli/p/8659268.html