- 条件语句
条件语句语法格式为if...elif...else,
Python里没有switch...case...
语句,语法如下:
if 表达式1:
语句块
elif 表达式2:
语句块
else:
语句块
条件语句嵌套
if 表达式1:
语句块
if 表达式2:
语句块
elif 表达式3:
语句块
else:
语句块
elif 表达式4:
语句块
else:
语句块
- 循环语句
Python有两种循环结构:while
和 for...in...
while
当其后的判断条件满足时,就是执行缩进的语句块,直到while
后面的条件不满足就停止。语法格式:
while 判断条件:
执行语句块
for x in seq
就是把seq的每个迭代元素代入变量x,然后依次执行缩进的语句块。语法格式:
for var in sequence:
语句块
跳出循环有两种语法:break
和continue
。两者区别在于:break
是跳出整个循环,而continue
是跳出当前循环。
list = ['a','b','c','d','e']
for x in list:
if x!='c':
print(x)
else :
continue
输出结果为
a
b
d
e
换为break,输出结果为:
a
b
- 列表
List列表是python中最常用的数据类型。列表中元素的类型可以不相同,它支持数字,字符串甚至可以包含列表(所谓嵌套)。列表定义格式:list = [obj1,obj2,obj3,…]
,与其他编程语言的数组概念相似,下标从0开始,即list[0]=obj1
。列表截取的语法格式:变量[头下标:尾下标]
列表常见操作
#!/usr/bin/python3.5
L=[] #创建空列表
list = ['hello','python','world',110,4.5]
print(list) #打印所有元素
#['hello', 'python', 'world', 110, 4.5]
print(list[1:4]) #第2个到第5个
#['python', 'world', 110]
print(list[2:]) #第2个到最后
#['world', 110, 4.5]
print(list[:3]) #开始到第3个
#['hello', 'python', 'world']
print(list[1]) #第1个元素(注意:返回元素而不是列表,所以返回值没有中括号)
#python
print(list * 2) #重复两次list
#['hello', 'python', 'world', 110, 4.5, 'hello', 'python', 'world', 110, 4.5]
list[4] = 2.5 #允许修改列表元素值
print(list)
#['hello', 'python', 'world', 110, 2.5]
del list[2] #删除第3个元素
print(list)
#['hello', 'python', 110, 2.5]
list1 = [1,2,3,4,5]
list2 = [6,7,8]
print(len(list1)) #len(list):列表元素个数
#5
print(list1+list2) #list1+list2:列表拼接
#[1, 2, 3, 4, 5, 6, 7, 8]
print(list2 * 2) #list*n:列表重复
#[6, 7, 8, 6, 7, 8]
print(9 in list1) #obj in list:判断元素是否在列表中
#False
for x in list1:print(x,end="+") #列表迭代
#1+2+3+4+5+
print(max(list1)) #max(list):返回列表最大值
#5
list1.append(6) #list.append(obj):列表尾部添加元素obj
print(list1)
#[1, 2, 3, 4, 5, 6]
print(list1.count(6)) #list.count(obj):计算obj在列表出现的次数
#2
list1.insert(0,7) #list.insert(index,obj):在下标为index的位置的前面插入obj
print(list1)
#[7, 1, 2, 3, 4, 5, 6, 6]
- 元组
Tuple元组和List列表很相似,元组元素也可以不相同,可以包含数字、字符串等,但元组和列表的定义格式不同,且元组元素不允许修改,列表允许修改。元组定义格式:tuple = (obj1,obj2,obj3,…)
。
>>> tup = (); #创建空元组tup
>>> tuple = ('hello','world',12,3.6,'python') #创建元组tuple
>>> print(tuple)
('hello', 'world', 12, 3.6, 'python')
>>> print(tuple[1]) #第1个元素。返回的是元素,而不是元组
world
>>> print(tuple[1:4]) #第2个到第4个元素
('world', 12, 3.6)
>>> print(tuple[1:]) #第2个到最后一个元素
('world', 12, 3.6, 'python')
>>> print(tuple[:3]) #从开始到第3个元素
('hello', 'world', 12)
>>> tuple[1] = 2 #元组不允许修改元素
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
- 集合
集合Set是一个无序不重复的序列。通常用来进行成员关系测试和删除重复元素,且支持集合运算(并交差等)。定义格式:
s1 = {1,2,3}
print(s1)
#{1, 2, 3}
set2=set('dog')
print(set2)
#{'g', 'd', 'o'} 无顺序排列
集合运算操作
set1 = set('abcdefg')
set2 = set('abchijk')
print(set1-set2) #差 A – B:差集A – B。在A中,且不在B中
#{'d', 'f', 'e', 'g'}
print(set1|set2) #并 A | B:并集A∪B。在A或B中的对象
#{'b', 'a', 'h', 'k', 'f', 'c', 'e', 'i', 'g', 'j', 'd'}
print(set1&set2) #交 A & B:交集A∩B。在A和B中的对象
#{'b', 'a', 'c'}
print(set1^set2) #对称集 A ^ B:对称集 (A-B)∪(B-A)。在A或B中的对象,但不同时在A和B中
#{'h', 'k', 'f', 'e', 'i', 'j', 'g', 'd'}
- 字典
字典和列表也很相似。列表,元组是有序的对象结合,字典是无序的对象集合。两者之间的区别在于:字典当中的元素是通过键值对来存取的。定义格式:dic = {'key1':val1,'key2':val2,'key3':val3}
dic = {'name':'Mary','sex':'W','age':23}
print(dic)
#{'name': 'Mary', 'sex': 'W', 'age': 23}
print(dic['name']) #输出键为'name'的值
#Mary
print(dic.keys()) #输出所有的键
#dict_keys(['name', 'sex', 'age'])
print(dic.values()) #输出所有的值
#dict_values(['Mary', 'W', 23])
dic['id'] = '20150001' #新增key
print(dic)
#{'name': 'Mary', 'sex': 'W', 'age': 23, 'id': '20150001'}
dic['id'] = '20150002' #修改键值
print(dic)
#{'name': 'Mary', 'sex': 'W', 'age': 23, 'id': '20150002'}