Python Learning Tutorial 3

Source: Internet
Author: User
Tags delete key

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.