1.列表-使用[]
表示
python列表(list)中文字典
1. 一种容器,里面可以存放任何类型的数据
2. 性质:
1. 列表中的元素是有序的
2. 通过索引取值
3. 声明方式:L = [112,'葫芦娃']
4. 取值方式:列表名[元素所在序列号]
1. 正向索引(从0开始)
L = ['lll',100,['llld',200]]
L[3] = ['llld',200]
2. 反向索引(从-1开始)
L[-1]=111
5. 从列表中随机抽取一个元素的方法
import random
all_list = ['石头','见到','布']
computer = random.choice(all_list)
- 列表常用方法
方法名 | 作用 | 示例 |
---|---|---|
append(元素) | 在列表中追加一个元素 | L.append(“123”) |
sort() | 对列表中的元素进行排序 | L = [12839] L.sort() L的值:12389 |
[0:2] | 对列表中元素进行切片 | L = [0,1,2,3]结果为:[0,1] |
- 石头剪刀布游戏
import random
all_list = ['石头','剪刀','布']
computer = random.choice(all_list)
your = input("""
(0)石头
(1)剪刀
(2)布
请出拳(0/1/2)
""")
if your not in ['0','1','2']:
print("请输入合法的值")
else:
# 较为麻烦写法
# if (computer == '石头' and your == '石头') or (computer == '剪刀' and your == '剪刀') or (computer == '布' and your == '布'):
# print("电脑出拳:%s 你出拳:%s 平局!!"%(computer,your))
# elif (computer == '石头' and your == '剪刀') or (computer == '剪刀' and your == '布') or (computer == '布' and your == '石头'):
# print("电脑出拳:%s 你出拳:%s 你输了!!"%(computer,your))
# else:
# print("电脑出拳:%s 你出拳:%s 你赢了!!" % (computer, your))
#较为简洁写法
win = ['石头剪刀','剪刀布','布石头']
lose = ['剪刀石头','石头布','布剪刀']
your = all_list[int(your)]
if (your+computer) in win:
print("电脑出拳:%s 你出拳:%s 你赢了!!" % (computer, your))
elif (your+computer) in lose:
print("电脑出拳:%s 你出拳:%s 你输了!!" % (computer, your))
else:
print("电脑出拳:%s 你出拳:%s 平局!!" % (computer, your))
print('\033[32m窗前明月光\033[0m')
输出的字符为绿色-
写一个注册用户程序
- 输入用户名(不能包含特殊字符和空白字符)
- 输入密码(非明文密码)
- 使用到的模块:
- string
string.punctuation
特殊字符
string.whitespace
空白字符 - getpass
import getpass
password = getpass.getpass("请输入密码")
- string
2.while循环
- Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务。
- 基本结构
while 判断条件:
执行语句……
- 练习
+ 用while循环实现随机生成n位密码/验证码(字母、数字、下划线)
import random
import string
number = input("请输入要生成密码的位数")
if number.isdigit():
number = int(number)
all_chars = string.ascii_letters + string.digits + '_'
i = 1
password = ''
while i <= number:
n = random.choice(all_chars)
password = password + n
i = i + 1
print(password)
else:
print("请输入数字")
- 一个球从100米高度落下,每次弹回原高度的一半,总共弹起多少次?(假定:最小弹起高度是0.01米),总共走了多少米。
height = 100
count = 0
distance = 0
while height >= 0.01:
distance = height + distance
height /= 2
count += 1
distance = height + distance
print(count)
print(round(distance))
- 在控制台中录入一个整数,判断是否为素数。(只能被1和自身整除的数字)
number = int(input("请输入一个整数"))
i = 2
while i < number:
if number % i == 0:
break
i += 1
else:
print("%s为素数" % number)
break
语句- 作用:用于循环语句中,用来终止循环
- break语句一旦执行,整个循环结束,一般和if语句组合使用
- 加入while循环后的石头剪刀布游戏代码
import random
all_list = ['石头','剪刀','布']
menu = """
(0)石头
(1)剪刀
(2)布
(q)退出
请出拳(0/1/2/q)
"""
while True:
computer = random.choice(all_list)
your = input(menu)
if your not in ['0','1','2','q']:
print("请输入合法的值")
elif your == 'q':
break
else:
win = ['石头剪刀','剪刀布','布石头']
lose = ['剪刀石头','石头布','布剪刀']
your = all_list[int(your)]
if (your+computer) in win:
print("电脑出拳:%s 你出拳:%s 你赢了!!" % (computer, your))
elif (your+computer) in lose:
print("电脑出拳:%s 你出拳:%s 你输了!!" % (computer, your))
else:
print("电脑出拳:%s 你出拳:%s 平局!!" % (computer, your))
3.for循环
- Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。
适合于已经直接循环次数的程序
- 基本结构
for iterating_var in sequence:
statements(s)
- 循环使用 else 语句
在 python 中,for … else 表示这样的意思,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)的情况下执行,while … else 也是一样。
用户注册代码
import string
import getpass
special_char = string.punctuation + string.whitespace
isright = True
while isright:
username = input("请输入用户名")
for letter in username:
if letter in special_char:
print("请不要输入特殊符号")
break
else:
isright = False
password = getpass.getpass("请输入密码")
4. 循环适用环境
for循环适合于已经直接循环次数的程序
while循环适合于不知道循环次数的程序