国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python開發之for循環操作實例詳解

2019-11-25 17:02:20
字體:
來源:轉載
供稿:網友

本文實例講述了python開發之for循環操作。分享給大家供大家參考,具體如下:

下面是我做的一些學習記錄供大家參考:

#基本的for循環語句test_list = [2,"Jone",3,6,7,'hongten','hanyuan','good',"Tom"]#打印列表的長度print(len(test_list))#遍歷列表for i in test_list:  print(i)test_str = "hello,i'm hongten"print('打印字符串:' + test_str)#遍歷一個字符串print('遍歷一個字符串')for i in test_str:  print(i)test_tuple = [("a",1),("b",2),("c",3),("d",4)]print(test_tuple)#遍歷一個元組print('遍歷一個元組')for (i,j) in test_tuple:  print(i,j)test_dict = {'name':'hongten','age':'20','gender':'M','sports':'足球,乒乓球,游泳'}#字典迭代器for key in test_dict:  print(key + ':' + test_dict[key])L1 = [1,3,5,7]L2 = [2,4,6,8]#使用zip將兩個列表合并print(zip(L1,L2))for (i,j) in zip(L1,L2):  print(i,j)print('#######################################################')L3 = L2[:]L3.remove(8)print('L1,L3列表為:')print(L1)print(L3)for (i,j) in zip(L1,L3):  print(i,j)#可以看出來當長度不一的時候,多余的被忽略test_keys = ['name','age','gender','weight','hight']test_values = ['Hongten','20','M','55','170']#使用zip來構造一個字典print('字典中的keys:')print(test_keys)print('字典中的key對應的value:')print(test_values)print('構造字典后')test_dic = dict(zip(test_keys,test_values))for key in test_dic:  print( key + ':' + test_dic[key])

運行效果:

Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32Type "copyright", "credits" or "license()" for more information.>>> ================================ RESTART ================================>>> 92Jone367hongtenhanyuangoodTom打印字符串:hello,i'm hongten遍歷一個字符串hello,i'm hongten[('a', 1), ('b', 2), ('c', 3), ('d', 4)]遍歷一個元組('a', 1)('b', 2)('c', 3)('d', 4)gender:Mage:20name:hongtensports:足球,乒乓球,游泳[(1, 2), (3, 4), (5, 6), (7, 8)](1, 2)(3, 4)(5, 6)(7, 8)#######################################################L1,L3列表為:[1, 3, 5, 7][2, 4, 6](1, 2)(3, 4)(5, 6)字典中的keys:['name', 'age', 'gender', 'weight', 'hight']字典中的key對應的value:['Hongten', '20', 'M', '55', '170']構造字典后gender:Mage:20name:Hongtenweight:55hight:170>>>

希望本文所述對大家Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 德惠市| 和林格尔县| 长岭县| 山东省| 闸北区| 久治县| 古蔺县| 合阳县| 南昌县| 榆树市| 芒康县| 香港 | 茌平县| 祁东县| 郓城县| 久治县| 游戏| 鄄城县| 霍城县| 渭南市| 庆安县| 石棉县| 司法| 涿鹿县| 环江| 华亭县| 阿图什市| 盐城市| 宝坻区| 凤城市| 潞城市| 五常市| 中牟县| 宕昌县| 资兴市| 东城区| 温州市| 闽清县| 丹东市| 平利县| 靖安县|