python基础06

shanghaipudong / 2023-05-18 / 原文

练习

# 猜年龄的游戏
# 1. 如果猜正确了,就直接结束程序
# 2. 猜错的话,继续让它猜,给三次机会,三次全猜错,程序不要结束,问:是否继续猜,是,在给三次机会,否则退出
'''先定义一个正确的值'''
age = 21
count = 0  # 计数器
while True:
    if count == 3:
        j = input('三次机会用完,按1继续,按2退出')
        if j == "1":
            count = 0
        else:
            print('再见')
            break
    gusse = input('猜猜我多大:')
    gusse = int(gusse)
    if gusse < age:
        print("猜小了,加油")
        count += 1
    elif gusse > age:
        print("猜大了,加油")
        count+=1
    else:
        print("猜对了,真棒")
        break

 while+coutinue

# 请听题:循环打印出0-10之间的数字
count=0
while count<10:
    print(count)
    count+=1
# 请听题:循环打印出0-9之间的数字,但是不打印6
count = 0
while count < 9:
if count == 6:
count += 1
continue
print(count)
count += 1

while+else

count = 0
while count < 9:
    if count == 6:
        count += 1
        break
    print(count)
    count += 1
else:
    print('sb')
#当while没有被人为(break)中断时,就会执行else代码块

死循环

while True:
    print(123)
count = 0
while count < 9:
print(count)
上面两种都是死循环,编程中坚决不能出现死循环

 for循环

for循环所做的事,while都能做

for循环代码更加简洁,不会出现死循环

# 请听题:
name_list = ['kevin', 'tank', 'jason', 'tony']
# 循环取出列表中每一个人名
count = 0
while count < 4:
    print(name_list[count])
    count += 1
for j in name_list:
print(j)

语法格式:

for变量名in可迭代对象:可迭代对象就是循环对象,字符串、列表、字典、元组等

当你没有合适的变量名是,可以使用i、j、k、item等

# 循环打印字段的k和v
d = {'username': 'kevin', 'age': 18}
for i in d:
    print(i, d[i])

 range关键字

三种玩法:

1.
for
i in range(10):#range只有一个整数时,表示从0——10之间的数 print(i)
2.
for i in range(2,10):#当有两个参数时,表示从2——10之间的数
print(i)
3.
for i in range(2,10,2):#当里面有三个参数时,表示2、4、6、8,第三个参数代表步长
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=125&filter= 第三页的数据
...
s=https://movie.douban.com/top250?start=%s&filter
https://movie.douban.com/top250?start=225&filter= 最后一页的数据"""
s='https://movie.douban.com/top250?start=%s&filter'
for i in range(0,250,25):
print(s%i)

range在不同版本解释器中的区别
在python2中直接打印出结果

在python3中做了优化,把它变成了迭代器, 就是节省内存资源

for+break

d = [1, 2, 3, 4]
for i in d:
    if i == 3:
        break
    print(i)

for+continue

d = [1, 2, 3, 4]
for i in d:
    if i == 3:
        continue#跳出本次循环,直接取下一位  
    print(i)#1、2、4

for+else

d = [1, 2, 3, 4]
for i in d:
    if i == 3:
        break
    print(i)
else:
    print('heihei')#当不被人为打断时,执行else代码块

打印九九乘法表

for i in range(1, 10):
    for j in range(1, i + 1):
        print('%s*%s=%s' % (i, j, i * j), end=' ')
        print()

数据类型内置方法

什么内置方法

就是给每种数据类型内置的方法

它的表现形式:名字()int() print()

1.整型(int)

进制转换

常见的进制数:二进制、八进制、十进制、十六进制(a b c d e f)

十进制转二进制:除2取余法

# int它也支持二进制转换

print(bin(10))   # 0b 1010  0b代表的就是二进制
print(oct(10))   # 0o 12    0o代表的是八进制
print(hex(10))   # 0x a     0x代表的是十六进制
# 把二进制转为十进制
print(int('0b1010', 2))
print(int('0o12', 8))
print(int('0xa', 16))#第二个参数代表进制数

2.浮点型

# 1.3 float同样可以用来做数据类型的转换
s = '12.3'
res = float(s)
res, type(res)  # (12.3, <class 'float'>)

3.字符串

s = 12.3
s = str(12.3)
print(type(s))  # <class 'str'>
print(str(s), type(str(s)))
print(str(1.11), type(str(1.11)))
print(str('helloworld'), type(str('helloworld')))
# print(str([1, 2, 3, 4]), type(str([1, 2, 3, 4])))
print(str({'a': 1, 'b': 2}), type(str({'a': 1, 'b': 2})))
print(str((1, 2, 3)), type(str({'a': 1, 'b': 2})))
print(str(({1, 2, 3})), type(str({'a': 1, 'b': 2})))

字符串的内置方法

l = [1, 2, 3, 4]
res1 = str(l)
'helloworld'
print(res1[0])
print(res1[2])

字符串支持索引取值

res = 'helloworldhelloworldhelloworldhelloworldhelloworldadadasdasdsadhelloworldhelloworldhelloworld'
print(res[0])
print(res[-2])  # (-)代表方向
# 切片
print(res[0:3])  # 表示从到到右取三位,参数0:表示从第几位开始,参数3:表示取几位
print(res[0:9:2])  # 参数2:表示步长
# 反向切片
print(res[0:])  # helloworld
print(res[2:])  # lloworld  冒号右边不写,一直切到结束
print(res[:6])  # hellow 冒号左边不写从0开始切
print(res[::3])  # hellow 冒号左边不写从0开始切
print(res[::-1])  # dlrowolleh,翻转字符串其中一种方式
# 长度len
print(len(res))  # 93  length---->len
print(len([1, 2, 3, 4]))
print(len({'a': 1, 'b': 2}))
# 5.strip移除字符串首尾指定的字符(默认移除空格)
res1 = '@@hello@world@@'
print(res1.strip())  # 默认什么都不写,去掉的是空格
print(res1.lstrip())  # 默认什么都不写,去掉左边空格
print(res1.rstrip())  # 默认什么都不写,去掉右边的空格
print(res1.rstrip('@'))  # 只能去除两边的特殊符号,中间的去不掉
# 6.切分split
res12 = 'kevin|18|male'
print(res12.split())  # 默认是空格切分,切分之后是列表的形式
print(res12.split('|'))  # 默认是空格切分,切分之后是列表的形式
print(res12.rsplit('|', maxsplit=1))  # 默认是空格切分,切分之后是列表的形式