while循环:
1.while+continue
# 使用循环打印出0-9的数字
# count = 0
# while count < 10:
# print(count)
# count += 1
# 使用循环打印出0-9的数字,但是不打印3
count = 0
while count < 10:
if count == 3:
count += 1
continue
print(count)
count += 1
'''continue结束本次循环,碰到continue,会立马执行循环条件的判断'''
2.while+else
# while+else
# count = 0
# while count < 5:
# print(count)
# count += 1
# else:
# print('我执行了')
count = 0
while count < 5:
if count == 3:
break
print(count)
count += 1
else:
print('我执行了')
# 一般情况,else跟if连用
总结:
当没有人为终断循环体时候,会执行else,否则,不走else
二.for循环
for循环的原理:
for循环能实现的功能,while循环都可以实现
for的语法更简洁,取值更方便
# while实现
name_list = ['ly', 'jason', 'tom', 'tony']
# 使用循环,打印出每一个元素
count = 0
while count < 4:
print(name_list[count])
count += 1
# for循环实现
name_list = ['ly', 'jason', 'tom', 'tony']
for i in name_list:
print(i)
'''
for i in 可迭代对象: # 字符串,列表,元祖,字典,集合...
print(i)
'''
# for循环不能写数字
for i in 1.123:
print(i)
# 循环字段暴露的是k
d = {'username': 'ly', 'password': 123}
for i in d:
# print(i)
print(i, d[i]) # d['username']
'''
for i in d: 里面的i值可以是任意的变量,如果没有好的变量名,一般叫i, j, k, v, item等
'''
1.for+break
for i in range(10):
if i == 3:
break
print(i)
2.for+continue
for i in range(10):
if i == 3:
continue
print(i)
3.for+else
for i in range(10):
if i == 3:
break
print(i)
else:
print('我执行了')
4.for循环的嵌套
for i in range(4):
for j in range(4):
print(i, j)
三.range关键字
1. 第一种玩法:# 只写一个参数的情况,代表从0开始的有序数字,顾头不顾尾
for i in range(10):
print(i)
2. 第二种玩法: # 写两个参数,可以自定义起始位置,顾头不顾尾
for i in range(3,10):
print(i)
3. 第三种玩法:# 写三个参数,第三个参数代表步长,顾头不顾尾
for i in range(0,100,10):
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=
base_url = 'https://movie.douban.com/top250?start=%s&filter=
for i in range(0, 250, 25):
print(base_url % i)
四.数据类型的内置方法
1. int
转换类型
# 只能转换纯数字
# print(int('123.123'))
# print(int('helloworld'))
# print(int('110'), type(int('110')))
# age = input('')
# age = int(age)
'''方法的表现形式:方法名()'''
# 进制之间的转换
print(bin(13)) # 1101 => 0,1 # 十进制转二进制
print(oct(13)) # 1101 => 0-7 # 十进制转八进制
print(hex(13)) # 1101 => 0-9, a,b,c,d,e,f # 十进制转十六进制
# 记忆:0b开头的代表二进制,0o开头的代表八进制, 0x开头的代表十六进制
print(int('0b1101', 2))
print(int('0o15', 8))
print(int('0xd', 16))
2. float
print(float('11.12'), type(float('11.12')))
3. str
# 把其他数据类型转为字符串
# print(str(123))
# print(str(11.12))
# print(str([1, 2, 3, 4]))
# print(str({'username': 'ly', 'age': 18}))
# print(str((1, 2, 3)))
# print(str({1, 2, 3}))
# print(str(True))
# s = 'helloworldhelloworldadasdsadsadhelloworldhelloworldhelloworld'
# 支持索引取值
# print(s[0])
# print(s[4])
# print(s[-1])
# print(s[-4])
# 支持切片操作
# print(s[1:4]) # 顾头不顾尾
# print(s[1:9:3]) # 顾头不顾尾, 第三个参数代表步长
# 计算字符串长度的
# print(len(s)) # 重点
# strip
# string = ' hello world '
string = '@@@hello world@@@'
# print(string)
# print(string.strip()) # 只取出首尾,中间的不管, 默认就是取出的空格
# print(string.strip('@')) # 只取出首尾,中间的不管, 默认就是取出的空格
# print(string.lstrip('@')) # 只取出首尾,中间的不管, 默认就是取出的空格
# print(string.rstrip('@')) # 只取出首尾,中间的不管, 默认就是取出的空格
# username = input('请输入用户名:')
# username = username.strip()
# if username == 'ly':
# print('ok')
# else:
# print('error')
# split
source_data = 'ly|18|male'
print(source_data.split('|')) # ['ly', '18', 'male'] 重点
print(source_data.split('|', maxsplit=1)) # ['ly', '18|male'] 只切割一次