![](https://i-blog.csdnimg.cn/blog_migrate/a56edd75f27fe8e0f8b5318c8cd67539.png)
1.while+continue
# 1. 使用循环打印出0-9的数字
a = 0
while a <= 9:
print(a)
a += 1
# 2. 打印0-10的数字不打印6
count=0
while count < 11:
if count ==3:
count+=1
continue #
print(count)
count+=1
'''continue结束本次循环,并且回到while循环的条件处从新判断'''
2.while+else(了解)
当while循环没有被中断(break)的时候,就会执行
# 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('哈哈哈哈')
3.死循环
在程序中,可千万不能出现死循环
能用for循环实现的,都不要使用while循环
while True:
print(100)
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+break
for i in range(10):
if i == 3:
break
print(ik
7.for+continue
for i in range(10):
if i == 3:
continue
print(i)
8.for+else
for i in range(10):
if i == 3:
break
print(i)
else:
print('哈哈哈哈')
9.for循环嵌套
for i in range(1,10):
for j in range(1,i+1):
print('%s*%s=%s' %(i,j,i*j),end=' ')
print()
10.数据类型及内置方法
方法 + 名字()
整型(进制转换)
# 进制转换
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))
float同样可以用来做数据类型的转换
# 进制转换
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))
把其他类型转为字符串
# 1. 把其他数据类型转为字符串
print(str(19))
print(str(19.1))
print(str([1, 2, 3, 4]))
print(str({'username':'kecin', 'age':18}))
print(str((1,2,3,4)))
print(str(True))
print(str({11,22,33}))
按索引取值
str1 = 'hello python!'
1.1 正向取(从左往右)
>>> str1[6]
p
# 1.2 反向取(负号表示从右往左)
>>> str1[-4]
h
# 1.3 对于str来说,只能按照索引取值,不能改
>>> str1[0]='H' # 报错TypeError
切片
2.1 顾头不顾尾:取出索引为0到8的所有字符
>>> str1[0:9]
hello pyt
# 2.2 步长:0:9:2,第三个参数2代表步长,会从0开始,每次累加一个2即可,所以会取出索引0、2、4、6、8的字符
>>> str1[0:9:2]
hlopt
# 2.3 反向切片
>>> str1[::-1] # -1表示从右往左依次取值
!nohtyp olleh
长度(len)
获取字符串的长度,即字符的个数,但凡存在于引号内的都算作字符)
len(str1) # 空格也算字符
13
成员运算 in 和 not in
4.1 int:判断hello 是否在 str1里面
>>> 'hello' in str1
True
# 4.2 not in:判断tony 是否不在 str1里面
>>> 'tony' not in str1
True
.strip .lstrip .rstrip
括号内不指定字符,默认移除首尾空白字符(空格、\n、\t)
>>> str1 = ' life is short! '
>>> str1.strip()
life is short!
# 5.2 括号内指定字符,移除首尾指定的字符
>>> str2 = '**tony**'
>>> str2.strip('*')
tony
切分split
括号内不指定字符,默认以空格作为切分符号
>>> str3='hello world'
>>> str3.split()
['hello', 'world']
括号内指定分隔字符,则按照括号内指定的字符切割字符串
>>> str4 = '127.0.0.1'
>>> str4.split('.')
['127', '0', '0', '1'] # 注意:split切割得到的结果是列表数据类型
循环
>>> str5 = '今天你好吗?'
>>> for line in str5: # 依次取出字符串中每一个字符
... print(line)
...
今
天
你
好
吗
?