Python(day2練習)
1,簡述變數命名規範。
①、字母、數字、下劃線任意組合
②、不能以數字開頭
③、不能是關鍵字
④、不建議用中文、拼音,不能太長(可描述性)
⑤、儘量用駝峰體或下劃線組成的變數名
2,name = input(‘>>>’) name是什麼資料型別。
字串型別(str)
3,if條件語句的結構。
①、 if: ...
②、 if: ....
else: ...
③、 if: ....
if: ...
else: ...
else: ...
④、 if:...
elif: ....
elif: ...
else: ...
4,while條件語句的結構。
①、while 條件:...
②、 while 條件:...
break
③、 while 條件:...
continue
④、 while 條件:...
else:... ###若while裡邊有break,則不執行else
#若while條件是錯的,則執行else
5,寫程式碼計算1-2+3....+99除了88以外所有數的和。
①
count = 1
sum = 0
while count <= 99:
if count % 2 == 1:
sum += count
if count == 88:
count += 1
#continue
if count % 2 == 0:
sum -= count
count += 1
print(sum) ###答案 138
②
count = 0
sum = 0
while count < 99:
count += 1
if count % 2 == 1:
sum += count
elif count == 88:
continue
else:
sum -= count
print(sum)
6,使用者登入(三次機會)並且每次輸錯時顯示剩餘登入機會(用到字串格式化。)
第六題完成的同學可做升級版:當剩餘機會為0時,可以詢問使用者是否在試試,如果使用者同意在嘗試,那就將在給他三次機會。
name = 'sunv'
pwd = 'lx'
count = 2
c = 0
while count >= 0:
username = input('請輸入使用者名稱:')
password = input('請輸入密碼:')
if username == name and password == pwd:
print('歡迎%s登陸系統!'%username)
break
else:
print('對不起,輸入錯誤,你還有%d次機會'%count)
count -= 1
c += 1
if count < 0 and c<6:
n = input('是否再試試?是請輸入:1,否請輸入2。')
if n == '1':
count = 2
else:
print('退出系統')
7,製作趣味模板程式(不太明白是不是這樣做)
print('歡迎進入我是歌手')
name = input('姓名:')
from1 = input('來自:')
flag = True
while flag:
duration = input('請選擇時長(1,2,3):')
if duration != '1' and duration != '2' and duration != '3':
print('請重新輸入:')
else:
flag = False
song = input('準備演唱曲目:')
msg = '''
歡迎來自%s的%s,準備唱時長為%s分鐘的歌曲,
下面請欣賞%s帶來的 %s!
'''%(from1,name,duration,name,song)
print(msg)
8,單行註釋及多行註釋。
#這是單行註釋
‘’’
這是多行註釋
‘’’
9,簡述你所知道Python2與python3的區別。
Python2:①部分原始碼亂、雜 ②raw_input
③關於中文(-*- encoding : utf-8 -*-)
Python3:①重構後簡潔清新 ②input
③可識別中文
10,continue與break的區別。
continue :跳出本次迴圈
break:跳出迴圈
2017-10-23
1,有變數name = "aleX leNb" 完成如下操作:
1) 移除 name 變數對應的值兩邊的空格,並輸出處理結果
print(name.strip()
2) 移除name變數左邊的’al’並輸出處理結果
print(name.lstrip('al'))
3) 移除name變數右面的’Nb’,並輸出處理結果
print(name.rstrip('Nb'))
4) 移除name變數開頭的a’與最後的’b’,並輸出處理結果
print(name.strip('ab'))
5) 判斷 name 變數是否以 "al" 開頭,並輸出結果
print(name.startswith('al')) ###True
6) 判斷name變數是否以”Nb”結尾,並輸出結果
print(name.endswith('Nb')) ###True
7) 將 name 變數對應的值中的 所有的“l” 替換為 “p”,並輸出結果
print(name.replace('l','p'))
8) 將name變數對應的值中的第一個’l’替換成’p’,並輸出結果
print(name.replace('l','p',1))
9) 將 name 變數對應的值根據 所有的“l” 分割,並輸出結果。
print(name.split('l'))
10) 將name變數對應的值根據第一個’l’分割,並輸出結果。
print(name.split('l',1))
11) 將 name 變數對應的值變大寫,並輸出結果
print(name.upper())
12) 將 name 變數對應的值變小寫,並輸出結果
print(name.lower())
13) 將name變數對應的值首字母’a’大寫,並輸出結果
print(name.title())
14) 判斷name變數對應的值字母’l’出現幾次,並輸出結果
print(name.count('l'))
15) 如果判斷name變數對應的值前四位’l’出現幾次,並輸出結果
print(name.count('l',0,3))
16) 從name變數對應的值中找到’N’對應的索引(如果找不到則報錯),並輸出結果
print(name.index('N'))
17) 從name變數對應的值中找到’N’對應的索引(如果找不到則返回-1)輸出結果
print(name.find('N'))
18) 從name變數對應的值中找到’X le’對應的索引,並輸出結果
print(name.find('X le')) ### 3
19) 請輸出 name 變數對應的值的第 2 個字元?
print(name[1:2])
20) 請輸出 name 變數對應的值的前 3 個字元?
print(name[:3])
21) 請輸出 name 變數對應的值的後 2 個字元?
print(name[-2:])
22) 請輸出 name 變數對應的值中 “e” 所在索引位置?
print(name.find('e'))
print(name.find('e',name.index('e')+1))
獲取子序列,去掉最後一個字元。如: oldboy 則獲取 oldbo。
s = ‘oldboy’
print(s[:-1])
2,有字串s = ‘132a4b5c’
1)通過對li列表的切片形成新的字串s1,s1 = ‘123’
s = '132a4b5c'
s1 = s[:3:2]
s2 = s[1:2]
print(s1+s2)
2)通過對li列表的切片形成新的字串s2,s2 = ‘a4b’
print(s[3:6])
3)通過對li列表的切片形成新的字串s3,s3 = ‘1245’
print(s[0::2])
4)通過對li列表的切片形成字串s4,s4 = ‘3ab’
print(s[1:-2:2])
5)通過對li列表的切片形成字串s5,s5 = ‘c’
print(s[-1:])
6)通過對li列表的切片形成字串s6,s6 = ‘ba3’
print(s[-3:0:-2])
3,使用while和for迴圈分別列印字串s=’asdfer’中每個元素。
①
s = 'asdfer'
i = 0
while True:
print(s[i])
i += 1
if i == len(s):
break
②
s = 'asdfer'
for i in s:
print(i)
4,實現一個整數加法計算器:
如:content = input(‘請輸入內容:’) # 如使用者輸入:5+9或5+ 9或5 + 9,然後進行分割再進行計算。
sum = 0
content = input('請輸入內容:')
c = content.split('+')
for i in c:
sum += int(i)
print(sum)
5,計算使用者
輸入的內容中有幾個整數。
如:content = input(‘請輸入內容:’) # 如fhdal234slfh98769fjdla
c = 0
content = input('請輸入內容:')
for i in content:
if i.isdigit():
c += 1
print(c)