(1)试着计算下面的运算,然后在Python中验算,看结果是否正确。
a = 21 b = 10 c = 0 c = a + b print ("1:c 的值为:", c) c = a - b print ("2:c 的值为:", c) c = a * b print ("3:c 的值为:", c) c = a / b print ("4:c 的值为:", c) c = a % b print ("5:c 的值为:", c)
(2)列表运算,然后在Python中验算,看结果是否正确。
list1 = ['Python', 1997, 'hello', 2000]; list2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; print ("list1[0]: ", list1[0]) print ("list1[2:4]: ", list1[0]) print ("list2[1:4]: ", list2[1:4]) print ("list2[2:-1]: ", list2[2:-1])
(3)元组运算(请体会与列表的不同) ,然后在Python中验算,看结果是否正确。
tup1 = ('Python', 1997, 'hello', 2000); tup2 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10); print ("list1[0]: ", tup1[0]) print ("list1[2:4]: ", tup1[0]) print ("list2[1:4]: ", tup2[1:4]) print ("list2[2:-1]: ", tup2[2:-1])
(4)脚本练习。将上面列表运算的内容存为脚本文件,并运行。