1.while+continue
# 1. 使用循环打印出0-9的数字
count = 0
while count < 10 :
print ( count)
count += 1
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 :
if count == 5 :
count+= 1
continue
print ( count)
count+= 1
else :
print ( '哈哈哈哈' )
'''当while循环没有被中断(break)的时候,就会执行'''
3.死循环
'''在程序中,可千万不能出现死循环'''
while True:
print(100)
'''能用for循环实现的,都不要使用while循环'''
4.for循环
name_list = [ 'kevin' , 'tony' , 'jack' , 'tom' ]
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 )
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 ) )
print ( oct ( 10 ) )
print ( hex ( 10 ) )
print ( int ( '0b1010' , 2 ) )
print ( int ( '0o12' , 8 ) )
print ( int ( '0xa' , 16 ) )
6.2浮点型
>> > s = '12.3'
>> > res= float ( s)
>> > res, type ( res)
( 12.3 , < class 'float' > )