# 南柯学习python第六天 (2.17 主要内容是for循环)

1.while+continue

# 1. 使用循环打印出0-9的数字
# while循环
count = 0
while count < 10:
    print(count)
    count += 1
# for循环
for i in range(0,10):
    print(i)
2. 打印0-10的数字不打印6
count=0
while count < 11:
    if count ==3:
        count+=1
        continue #
    print(count)
    count+=1
        
'''continue结束本次循环,并且回到while循环的条件处从新判断'''

2.while+else

# count=0
# while count < 10:
#     print(count)
#     count+=1
# else:
#     print('哈哈哈哈')

count=0
while count < 10:
    if count ==5:
        count+=1
        continue
    print(count)
    count+=1
else:
    print('哈哈哈哈')

    
'''当while循环没有被中断(break)的时候,就会执行'''

3.死循环

'''在程序中,可千万不能出现死循环'''
while True:
    print(100)
'''能用for循环实现的,都不要使用while循环'''

4.for循环

# 1. 使用while循环打印以下列表中得数据
name_list = ['kevin', 'tony', 'jack', 'tom']

# i=0
# while i<4:
#     print(name_list[i])
#     i+=1

for name in name_list:
    print(name)
    
"""
	语法格式:
        for 变量 in 可迭代对象:  字符串、列表、字典、元组
        	print(name)
"""

for i in 'helloworld':
    print(i)
PS:for后面的变量名命名的时候,如果没有特殊的含义,我们一般使用i,j,k,v,item等等


'''重点'''
d = {'username':'kevin', 'age':18, 'hobby':'music'}
for i in d:
    print(i, d[i])

5.range关键字

配合for循环使用
第一种玩法
for i in range(10) # 一个参数, 打印数字列表,从0开始
	print(i)
第二种玩法
for i in range(4, 18):  # 顾头不顾尾
    print(i)
第三种玩法
for i in range(2, 20, 3):# 步长 # 顾头不顾尾
    print(i)
拓展
"""
推断:https://movie.douban.com/top250?start=0&filter=    第一页
	https://movie.douban.com/top250?start=25&filter=    第二页
	https://movie.douban.com/top250?start=50&filter=    第三页
	https://movie.douban.com/top250?start=75&filter=    第四页
	https://movie.douban.com/top250?start=100&filter=   第五页
	...
	
	https://movie.douban.com/top250?start=225&filter=   第十页
"""

url = 'https://movie.douban.com/top250?start=%s&filter='
for i in range(0,250,25):
    print(url % i)
    
'''小补充:range在不同解释器版本中是有区别的。'''

6.for的搭配使用

6.1for+break
for i in range(10):
    if i == 3:
        break
    print(i)
6.2for+continue
for i in range(10):
    if i == 3:
        continue
    print(i)
6.3for+else
for i in range(10):
    if i == 3:
        break
    print(i)
else:
    print('哈哈哈哈')

6.数据类型的啊内置方法(重点,难点)

6.1整型
# 进制转换
print(bin(10))  # 0b1010
print(oct(10))  # 0o12
print(hex(10))  # 0xa

# 记忆:0b代表二进制  0o代表八进制  0x代表十六进制

print(int('0b1010', 2))
print(int('0o12', 8))
print(int('0xa', 16))
6.2浮点型
# float同样可以用来做数据类型的转换
>>> s = '12.3'
>>> res=float(s)
>>> res,type(res)
(12.3, <class 'float'>)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值