Python学习笔记(三)
-
dict字典
a.定义
A dictionary consists of keys and values. It is helpful to compare a dictionary to a list. Instead of the numerical indexes such as a list, dictionaries have keys. These keys are the keys that are used to access values within a dictionary.b.创建
keys | values |
---|---|
Mon | 1 |
Tue | 2 |
Wed | 3 |
dict = {‘Mon’:1, ‘Tue’:2, ‘Wed’:3}
c.字典方法
dict[‘Mon’] #返回Mon的value
dict[‘Thus’]:4 #添加
del(dict[‘Thus’]) #删除
‘Mon’ in dict#查找‘Mon’是否在dict中
dict.keys()#查找dict的所有的keys
dict.values()#查找dict的所有的values
- 集合 { }-curly brackets
a.特性
集合中相同元素只显示一个
b.创建
A = {‘a’,‘b’,‘c’}
B = set([‘a’,‘b’,‘c’})
c.方法
A.add(‘d’) #添加元素
A.remove(‘c’)#移除元素
‘a’ in A #检查元素是否在集合中,返回True/False
A&B 或 A.intersection (B) #返回A交B
A.difference (B) #返回A非B
A.union (B) #返回A并B
A. issuperset (B) #检查A是否是B的母集
A. issubset (B) #检查A是否是B的子集
- 判断语句(要求掌握多条件判断)
A=int(input('请输入数字:')) #如果输入数字6
if A = 2: #False
print('等于二')
elife A > 2 #True
print('大于二') #返回'大于二', 结束
else:
print('小于二')
根据True or False值进行。(注意if与else后冒号!!)
- 三目表达式
A=int(input('请输入数字:'))
print('大于等于2') if A>=2 else print ('小于二')
- 循环语句(Loops)
满足条件既循环,知道不满足循环结束。
for语句
while语句
与if语句相似,注意冒号!