之前學習while一直有糾結的地方,先整理下,還是以fishc的練習題為例。

python004.jpg
temp = input('Please enter an integer:') #請用戶輸入整數
temp1 = int(temp) #對用戶輸入的字符進行整數賦值
while temp1: #根據客戶輸入的整數進行倒序循環(huán)
space = temp1 #設置空格變量,并賦值
while space: #空格循環(huán)
print(' ', end='') #空格輸出后不換行
space -= 1 #每循環(huán)1次,空格數-1
start = temp1 #設置星星變量,并賦值
while start: #星星循環(huán)
print('*', end='') #星星輸出后不換行
start -= 1 #每循環(huán)1次,星星數-1
temp1 -= 1 #客戶輸入的整數循環(huán)每循環(huán)1次數值-1
print() #循環(huán)一次后換行
結果示例

image.png
1、list
如果訪問list時,索引值超出范圍會提示錯誤,且list的索引值起始值是‘0’
>>> pc = ['顯示器', '鼠標', '鍵盤', '機箱', '電源']
>>> len(pc)
5
>>> pc[0]
'顯示器'
>>> pc[6]
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
pc[6]
IndexError: list index out of range
所以最后一個元素的索引值肯定是len() - 1
2、tuple
定義tuple時,我個人認為2個重點:‘()’和‘,’
>>> pc = (1)
>>> pc1 = (1,)
>>> pc
1
>>> pc1
(1,)
>>> type(pc)
<class 'int'>
>>> type(pc1)
<class 'tuple'>
可以看到,變量pc實際上是整數,而變量pc1才是tuple。tuple再顯示時哪怕只有1個元素也會顯示成"(1,)"