python list列表基礎(元組)
‘’‘元組 不能被修改的列表 []索引’’’
‘’’
tuple = (1,2,3,4,5,6)
print(tuple[2])
3
tuple[2]=100#嘗試修改
print(tuple)
#報錯TypeError
‘’’
‘’‘遍歷元組’’’
‘’‘tuple = (1,2,3,4,5,6)
for i in tuple:
print(i)
1
2
3
4
5
6’’’
‘’‘修改元組變數’’’
‘’‘tuple=(1,2)
for i in tuple:
print(i)
1
2
tuple=(3,4)
for i in tuple:
print(i)
3
4’’’
‘’’
############################################
if 語句 if True/False
‘’’
‘’'cars=[‘audi’,‘bmw’,‘amg’,‘toyota’]
for i in cars:
if i==‘bmw’:
print(i.upper())
else:
print(i)
audi
BMW
amg
toyota’’’
‘’‘car=‘bmw’
if car in cars:
print(car.upper())
BMW’’’
‘’’ = 與 == ,= 是賦值,== 是判斷相等,區分大小寫,結果是布林值’’’
‘’‘car=‘Audi’
print(car==‘audi’)
False
print(car.lower()==‘audi’)
True’’’
‘’’ != 是判斷不相等,區分大小寫,結果是布林值’’’
‘’‘car=‘Audi’
print(car!=‘audi’)
True
print(car.lower()!=‘audi’)
False’’’
‘’’== != > < >= <= 常用來比較數字’’’
‘’'age=17
if age < 18:
print(‘你還未成年’)
你還未成年’’’
‘’‘and 和 or 檢查多條件 ‘’’
‘’‘True and True = True
True and False = False
False and False = False
False or False = False
False or True = True
True or True = True’’’
‘’'age = 20
if (age>=18) and (age<=22): #True and False = True
print(‘你還不能結婚,太小了’)
if (age>=18) or (age<=22): #True and False = True
print(‘你可以談戀愛了’)’’’
‘’‘檢查列表是否包含某值,返回布林值’’’
‘’'cars = [‘audi’,‘amg’,‘bmw’]
print(‘bmw’ in cars)
True
print(‘toyota’ in cars)
False’’’
‘’‘檢查列表不存在某值’’’
‘’'cars = [‘audi’,‘bmw’]
amg = ‘amg’
if amg not in cars:
print(amg.title()+’ is not exist’)
Amg is not exist’’’
‘’‘布林表示式記錄條件,跟蹤程式狀態’’’
game = True
study = False
‘’‘if-else語句,if條件未通過就執行else後面的語句’’’
‘’'age = 17
if age>18:
print(‘成年了’)
else:
print(‘未成年’)
未成年’’’
‘’‘if-elif-else語句 一次只執行一個程式碼段,if未通過執行elif,elif未通過執行else,elif可以有多個,else可以沒有’’’
‘’'age = 17
if age<15:
print(15)
elif age>18:
print(18)
else:
print(‘17’)
17’’’
‘’‘if-if-if語句 同時執行多個程式碼段’’’
‘’'cars = [‘amg’,‘bmw’,‘toyota’]
if ‘amg’ in cars:
print(‘amg’)
if ‘audi’ in cars:
print(‘audi’)
if ‘toyota’ in cars:
print(‘toyota’)
amg
toyota’’’
‘’‘if語句處理列表’’’
‘’'room = [‘gang’,‘liang’,‘dong’]
for name in room:
if name==‘liang’:
print(‘get out’)
else:
print(‘welcome’)
welcome
get out
welcome’’’
‘’‘確定列表不空’’’
‘’'cars = []
if cars:#不空
print(‘人滿了’)
else:
cars.append(‘liang’)
print(‘welcome’+str(cars))
welcome[‘liang’]’’’
‘’‘使用多個列表’’’
‘’'cars = [‘amg’,‘bmw’,‘toyota’,‘gang’]
room = [‘gang’,‘liang’,‘dong’]
for name in room:
if name in cars:
print(name + ’ have a car’)
else:
print(name+’ dont people have car’)
print(‘finish’)
gang have a car
liang dont people have car
dong dont people have car
finish’’’
相關文章
- python列表(list)和元組(tuple)詳解Python
- 豬行天下之Python基礎——3.2 列表 & 元組Python
- Python - 基礎資料型別 list 列表Python資料型別
- Python 選列表 list 還是元組 tuple 的思考Python
- Python基礎:資料型別-列表與元組(6)Python資料型別
- python基礎之序列型別的方法——列表&元組Python型別
- 列表、元組、字串是有序序列嗎?Python基礎教程字串Python
- Python基礎_元組Python
- Python 基礎 3 - 元組Python
- Python基礎(05):元組Python
- python元組和列表Python
- Python零基礎學習筆記(十五)——list(列表)Python筆記
- Python3之字串str、列表list、元組tuple的切片操作Python字串
- Python List 列表list()方法Python
- Python列表、元組、字典使用Python
- Python列表基礎Python
- Python 列表(List)Python
- python列表(List)Python
- Python list(列表)Python
- python之tuple元組,基礎篇Python
- Python基礎之list列表寫入檔案的四種方法Python
- Python List 列表list()方法分享Python
- python_列表——元組——字典——集合Python
- Python 學習之元組列表Python
- Python之列表&元組小練Python
- Python 基礎 2 - 列表Python
- Python基礎(03):列表Python
- Python元組、列表、集合及列表去重操作Python
- Python基礎:使用list & tuplePython
- python資料型別 列表+元組Python資料型別
- python 元組,列表 迴圈遍歷Python
- Python基礎知識七 元組&字典&集合Python
- Python - 基礎資料型別 tuple 元組Python資料型別
- python第三天(list,元組,dictionary)Python
- python基礎(三)——操作列表Python
- Python基礎 04----列表Python
- python如何返回元組,列表或字典的?Python
- Python 元組,不可變的列表,滾雪球學 PythonPython