循环与数据类型的内置方法

内容概要

  • while+continue的使用
  • while+else的使用
  • 死循环
  • for循环
  • 数据类型的内置方法

详细

1. while+continue的使用

例:使用while循环打印出0-10
代码:
count = 0
while count < 11:
print(count)
count += 1
打印结果:
image
例:使用while循环打印出0-10但是不打印4
代码:步骤
1.定义一个起始变量
count = 0
2.循环
while count < 11:
5.判断 如果count为4则不打印
if count == 4:
count += 1
跳过本次循环 开始下一次循环
continue
3.打印变量的值
print(count)
4.变量值自增1
count += 1
打印结果:
image
continue会让循环体代码直接回到条件判断处重新判断

2.while+else的使用

例:
count = 0
while count < 5:
print(count)
count += 1
else:
print('嘿嘿嘿') # 会执行else子代码
image

count = 0
while count < 5:
if count == 3:
break
print(count)
count += 1
else:
print('嘿嘿嘿') # 不会执行else子代码
image
当while循环没有被人为中断(break)的情况下才会走else

3.死循环使用

格式:
while True:
print(1)
尽量少试运行 会让CPU极度繁忙 甚至奔溃

4.for循环

格式:
for 变量名 in 可迭代对象:
print('%s')
可迭代对象可以有:
字符串、列表、字典、元组、集合;

变量名如果没有合适的名称 那么可以使用i,j,k,v,item等

其实for循环能做到的事情 while循环都可以做到
但是for循环语法更加简洁 并且在循环取值问题上更加方便
例:
name_list = ['jason', 'tony', 'kevin', 'jack', 'xxx']
循环取出列表的每一个元素并打印

先while实现:
count = 0
while count < 5:
print(name_list[count])
count += 1
打印结果:
image

再用for循环实现:
for name in name_list:
for循环体代码
打印结果:
image

for循环字符串时:空格也会被输出
image

for循环字典:默认只能拿到k
image

image

1. for循环中的关键词range

一般作为整型(整数)的范围使用
例:
一个参数 从0开始 顾头不顾尾
for i in range(10):
print(i)
image

两个参数 自定义起始位置 顾头不顾尾
image

三个参数 第三个数字用来控制等差值
image

在python2.X 中 range 会直接生成一个列表
在python2.X 中有一个 xrange 也是迭代器
在python3.X 中 range 是一个迭代器 节省内存空间

2. for+break

break功能类似于在whil中 也是用于结束本层循环
image

3.for+continue

continue功能类似于在whil中 也是用于结束本次循环
image

4.else

else功能类似于在whil中 也是在for循环正常结束的情况下才会执行
image

非正常结束的循环中:
image

5.for循环的嵌套使用

例:
打印
*
**




image

反向的:
image

经典之乘法口诀表:
for i in range(1, 10):
for j in range(1, i + 1):
print('%s*%s=%s' % (i, j, i * j), end=' ')
print()
image

5.数据类型的内置方法

在日常生活中不同类型的数据具有不同的功能

1.整型int

在代码中展示出来的效果就是 名字()
用的最多的是类型转换:
res = '123'(字符串)
print(type(res))
显示为 str
res = int(res)
print(type(res))
显示结果为 int
image

int在做类型转换的时候 只能转换纯数字
例如:
int('123.123') # 报错 不识别小数点
int('jason123') # 报错 不识别除数字以外的数据

int其实还可以做进制数转换:
print(bin(100)) # 将十进制的100转换成二进制 结果: 0b1100100
print(oct(100)) # 将十进制的100转换成八进制 结果: 0o144
print(hex(100)) # 将十进制的100转换成十六进制 结果: 0x64

在分别转换回十进制数
》》》 0b开头为二进制数 0o开头为八进制数 0x开头为十六进制数
print(int('0b1100100', 2)) # 结果: 100
print(int('0o144', 8)) # 结果: 100
print(int('0x64', 16)) # 结果: 100
image

2.浮点型float

类型转换
res = '123.23'
print(type(res))
res = float(res)
print(type(res))
print(float('123')) # 123.0
image

3.字符串str

类型转换
print(str(123))
print(str(123.21))
print(str([1, 2, 3, 4]))
print(str({'name': 'jason', 'pwd': 123}))
print(str((1, 2, 3, 4)))
print(str(True))
print(str({1, 2, 3, 4}))
所有数据类型均可转换为字符串
image
基本用法
例:
res = 'hello world!'
1.索引取值
print(res[1])
结果:
e
image

2.切片操作 顾头不顾尾
res = 'hello world!'
print(res[1:4])
image

3.步长操作
res = 'hello world!'
正常取值
image
有步长参数再取值
image

4.索引支持负数
res = 'hello world!'
print(res[-1])
结果:! (最后一位)
image

print(res[-5:-1])
结果:orld 顾头不顾尾
image

print(res[-5:-1 :-1])
方向来回冲突 不成立 无法执行
image

5.统计字符串内部字符的个数
关键词:len
res = 'hello world!'
print(len(res))
结果:12
image

6.移除字符串首尾指定的字符
关键词:strip
例:
name = '空空jason空空'
print(name, len(name))---# 结果为 9
print(len(name.strip()))---# 默认移除首尾的空格 结果为 5
image
例:
name1 = '$$jason$$'
print(name1.strip('$'))

结果: jason 首尾符号均被移除

print(name1.lstrip('$'))
结果:jason$$ l代表life(左边) 移除左边所有符号
print(name1.rstrip('$'))
结果:$$jason r代表right(右边) 移除右边所有符号
image
例:
username = input('username>>>:').strip()
if username == 'jason':
print('老板好')
else:
print('去你妹的')
image

7.按照指定的字符切割字符串
关键词:split
该方法的结果是一个列表
例:
image

如何查看数据类型都有哪些内置方法:
代码+点 .

posted @ 2021-11-05 20:28  Deity_JGX  阅读(38)  评论(0编辑  收藏  举报