range()函数连续列表生成(ip地址)

range()函数连续列表生成(ip地址)

#define a variable to hold a string representing the base address
baseAddress = "192.168.0."
#next define a list of host addresses using the range
#standard library function(this will give us values of 1-19 host)
hostAddresses = range(20)
#define a list that will hold the result ip strings
#this starts out as a simple empty list
ipRange = []
#loop through the host addresses since the list host Addresses 
#contains the integers from 0-19 and we can create
#a loop in Python that processes each of the list elements
#stored in hostAddresses,where i is the loop counter value
for i in hostAddresses:
    #append the combined ipstrings to the ipRange list
    #because ip Range is a list object,the object has a set of 
    #attributes and methods.We are going to invoke the append method
    #each time through the loop and concatenate the base address
    #string with the string value of the integer
    ipRange.append(baseAddress + str(i))
#    |        |          |        |   |_value of the host address
#    |        |          |        |_function to convert int to str
#    |        |          |_The string '192.168.0'
#    |        |_The append method of the list ipRange
#    |_The list object ipRange
#Once completed we want to print out the ip range list
#here we use the print function and instruct it to print out
#the ip Range list object,I wanted to print out each of the
#resulting ip address on a separate line,so I looped
#through the ip Range list object one entry at a time.
for ipAddr in ipRange:
    print(ipAddr)

输出结果:
192.168.0.0
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
192.168.0.5
192.168.0.6
192.168.0.7
192.168.0.8
192.168.0.9
192.168.0.10
192.168.0.11
192.168.0.12
192.168.0.13
192.168.0.14
192.168.0.15
192.168.0.16
192.168.0.17
192.168.0.18
192.168.0.19

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值