Python(八)循环

循环

>for循环

各数据类型for循环实例

关于range()函数的for循环

for循环实例(查看本机网络接口)

>while循环

while循环实例(查看本机网络接口)

>九九乘法表


>for循环

  • for...in...
  • 用于已知次数的循环
  • 数字不可使用for循环
  • 各数据类型for循环实例

#各数据类型for循环实例
#author:xuer

#字符串
for i in "abc":
    print(ord(i))    ————>97 98 99

#元组
for i in ("a","b","c"):
    print(ord(i))    ————>97 98 99

#字典
for i in {"a":90,"b":80,"c":70}:
    print(i)                        ————>a b c
for i in {"a":90,"b":80,"c":70}.keys():            #以关键字key为索引
    print(i)                        ————>a b c
for i in {"a":90,"b":80,"c":70}.values():          #以value值为索引
    print(i)                        ————>90 80 70
for k,v in {"a":90,"b":80,"c":70}.items():         #key与value同时索引
	print(k,">>>>>",v)          ————>a >>>>> 90
                                         b >>>>> 80
                                         c >>>>> 70
  • 关于range()函数的for循环

#关于range()函数的for循环
#author:xuer

#for i in range(循环开始的数,循环结束的数,步长)
                                ————>结束的数,不包括在内

for i in range(10):	
	print("welcome!")    ————>welcome!
                                  welcome!
                                  welcome!
                                  welcome!
                                  welcome!
                                  welcome!
                                  welcome!
                                  welcome!
                                  welcome!
                                  welcome!
for i in range(1,11,2):
	print(i)             ————>1 3 5 7 9
  • for循环实例(查看本机网络接口)

#for循环实例
#查看本机网络接口
#author:xuer

import os
inf=os.popen("ipconfig").read()    #读取本机网卡配置信息
print("一共有%d个网卡"%inf.count("适配器"))  
ilist=inf.split("适配器")[1:]  #“适配器”后面的内容切片(“适配器”后有空格,所以从第二个开始切片)
                               #转换成列表格式
print("方法一:for循环")
print("网卡名称\t\t\t\t\tIP地址")    #打出表头
for nic in ilist:    #nic为ilist列表中的项(一项为一组信息)
    nlist=nic.split("\n")   #以"\n"为标志,将每项分开,分成若干个列表
    name=nlist[0].strip()[:-1]    #name为在每个列表中的第一个字符串的最后一个字符前面的所有字符
    flag=nic.find("IPv4")   #找到ipv4所在
    if flag<0:
        addr="未连接"
    else:
        addr=nlist[4].split(":")[1].strip()    #nlist[4]列表的第五个,切出“:”后的IP地址(“:”后有空格,从第二个开始取)
    print("%-45s"%(name),"%-10s"%(addr),sep="\t")

>while循环

  • while1=while True 死循环
  • 用于未知次数的循环,死循环
  • while循环实例(查看本机网络接口)

#while循环实例
#查看本机网络接口
#author:xuer

import os
inf=os.popen("ipconfig").read()    #读取本机网卡配置信息
print("一共有%d个网卡"%inf.count("适配器"))  
ilist=inf.split("适配器")[1:]  #“适配器”后面的内容切片(“适配器”后有空格,所以从第二个开始切片)

print("方法二:while循环")
i=0
print("索引\t网卡名称\t\t\t\t\tIP地址")
while i<len(ilist):    #len(ilist),可以查出列表长度(即列表中的元素个数)
    nic=ilist[i]
    nlist=nic.split("\n")    #以“\n”分割,将nic转换成列表格式
    name=nlist[0].strip()[:-1]    #取出name
    ipline=nlist[4].strip()       #取出IP地址所在字符串
    if ipline.startswith("IPv4"):   #检查字符串是否以“IPv4”开头
        addr=ipline[ipline.find(":")+2:]    #找到“:”,截取IP地址(“:”后有空格,从第三个字符开始截取)
                #ipline[ipline.find(":")+2:],语法:从所查找的字符开始算起(所查找的字符为第一个,即[0])
    else:
        addr="未连接"
    print(i+1,"%-45s"%(name),addr,sep="\t")
    i+=1

>九九乘法表

  • 所有for循环皆可以使用while循环代替
#嵌套循环语句
#九九乘法表
#author:xuer

print("【for循环版九九乘法表】")
for i in range(1,10):
    for j in range(1,i+1):
        print(i,"*",j,"=",i*j,end="\t")    #end="",以...结尾;\t,横向制表符(\v,纵向制表符)
    print()    #换行

print("【while循环版九九乘法表】")    
i=j=1
while i<10:
    while j<=i:
        print(i,"*",j,"=",i*j,end="\t")
        j+=1
    print()
    i+=1
    j=1

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值