1. dict字典
a. 定义
字典是一种可变容器模型,可存储任意类型对象。字典中的每一个key对应一个value,其格式如下:
dic={key1:value1,key2:value2}
b. 创建
c. 字典的方法
2. 集合
2.1 特性
set集合是一个无序的不重复元素序列。
2.2 创建
我们可以使用大括号 { }
或者 set()
函数创建集合,如:
s = {value1,value2}
#或者
set(value)
2.3 方法
- 添加:
s.add( x )
- 删除:
s.remove( x ) # x不存在会发生错误
、s.discard( x )# x不存在不会发生错误
、s.pop() #随机删除
- 其它:
len(s) #计算元素个数
、s.clear() #清空集合
、set.copy() #复制一个集合
其它还有很多可以参考http://www.runoob.com/python3/python3-set.html
3. 判断语句(要求掌握多条件判断)
if 语句格式如下:
num=input("输入一个数字:")
if num.isnumeric():
num = int(num)
if num%2 == 0:
if num%5 == 0:
print ("您输入的数字可以整除2 和5")
else:
print ("您输入的数字可以整除2,但不能整除5")
else:
if num%5 == 0:
print ("您输入的数字可以整除5,但不能整除2")
else:
print ("您输入的数字不能整除2和5")
else:
print("您输入的不是一个纯数字")
4. 三目表达式
Python中没有像其它语言一样的三目表达式,但是我们可以这样写:
a,b =1,2
c = ""
c = "变量1" if a>b else "变量2" #如果a>b,c="变量1";否则 c="变量2"
5. 循环语句
- while()循环语句
while True: scan = input("请输入密码:") if scan == "Hello World" : print("验证成功!!!") break else: continue
- for()循环语句
for i in range(0,10) : #in 后面只能是不可迭代对象,否则报错
print(i)
for str in "abcdefghigjkl":
print(str)