//方法一 In [25]: l1 Out[25]: [1, 2, 3, 4] In [26]: while l1: ....: print l1[0] ....: l1.pop(0) ....: 1 2 3 4 //方法二 In [66]: count = 0 In [67]: count Out[67]: 0 In [68]: while count < len(l1): ....: print l1[count] ....: count += 1 ....: 1 2 3 4创新互联专注于谯城企业网站建设,自适应网站建设,购物商城网站建设。谯城网站建设公司,为谯城等地区提供建站服务。全流程按需规划网站,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务
In [215]: sum = 0; i = 0 In [216]: while i <= 100: .....: i+=1 .....: if i % 2 == 0: .....: sum = sum +i .....: In [217]: print sum 2550
In [91]: print d1 {'y': 45, 'x': 2, 'z': 90} In [95]: while keylist: ....: print keylist[0] ....: keylist.pop(0) ....: else: ....: print len(d1) ....: y x z 3
In [96]: l1 = [] In [97]: x = 1 In [98]: while x < 100: ....: l1.append(x) ....: x += 2 ....: In [99]: print l1 [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]
In [34]: l1 = [1,2,3,4] In [35]: while l1: ....: print l1[-1] ....: l1.pop() ....: 4 3 2 1
In [120]: d1={} In [121]: x=0 In [122]: l1=[0,1,2,3,4,5,6] In [123]: l2=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"] In [130]: while x < len(l1): d1[l1[x]] = l2[x] x += 1 .....: In [131]: print d1 {0: 'Sun', 1: 'Mon', 2: 'Tue', 3: 'Wed', 4: 'Thu', 5: 'Fri', 6: 'Sat'}
关于while语法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果喜欢这篇文章,不如把它分享出去让更多的人看到。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。