This chapter focuses on three very important data types in Python: List tuple dictionary
#-*-Coding:utf-8-*-
#1 declarations List
List1 = ["A", "B", "C"]
#判断列表中是否存在元素
Print ("A" in List1)
#追加元素
List1.append (123)
#迭代输出
For I in List1:
Print (i)
#list长度
Print (len (list1))
#list中最大的值max最小值min
Print (max ([12,2,3]))
#pop--Removes the last value and returns the value
numbers = [12,32,54,787,24]
Print Numbers.pop ()
#常用方法: Reverse Sort Remove Insert Count index
#2 declaring elements of the Zu Yuanzu element cannot modify the tuple use the Bracket series with square brackets
Tup1 = (' Physics ', ' chemistry ', 1977,2000)
#虽然元素在元祖中不允许修改但是我们可以对他们进行组合
Tup2 = (200,300)
TUP3 = tup1+tup2
Print Tup3
#将列表转换成元祖
Listzh = [12,23,34,45]
Tuplezh = tuple (Listzh)
For I in Tuplezh:
Print (i)
#将元祖转换成列表
Tupzh = (147,258,369)
Lizh = List (Tupzh)
Print ("Convert meta-ancestor to list {0}", Lizh)
#3字典是另一种可变容器模型, and can store any type of object.
#字典的每个键值 (Key=>value) pairs with colons (:) split, each pair separated by commas (,), and the entire dictionary is included in curly braces ({})
DIC = {"Key1": 123, "Key2": 456}
Print dic
#访问字典里的值
Print dic[' Key1 ']
Print dic[' Key2 ']
#修改字典
dic["Key2"] = 789;
Print dic
#删除字典元素
Dict = {' Name ': ' Zara ', ' age ': 7, ' Class ': ' First '};
Del dict[' Name ']; # Delete key is ' Name ' entry
Dict.clear (); # Empty Dictionary all entries
Del Dict; # Delete Dictionary
#1) does not allow two occurrences of the same key. When created, if the same key is assigned a value of two times, the latter value is remembered, as in the following example:
#2) key must be immutable, so it can be used as a number, string, or tuple, so it is not possible to use a list.
Python Learning Tutorial 3