Python Basics (3) _ Lists, Ganso, and dictionaries

Source: Internet
Author: User

mutable objects and immutable objects

  Once the instance is created, the identity and type are immutable,

If the value is not modifiable, the object is immutable

If the value can be modified, it is a mutable object

#在id不动, the type does not move, and value is modified, which is called a mutable

#可变指的是: The value is variable if the ID and type do not move

properties and methods for objects
A property is the value of an object, which is a function that will perform certain operations on the object itself when called, using the. operator to access the properties and methods of the object, such as
A=3+4j
A.real

b=[1,2,3]
B.append (4)

7 Identity comparison, type comparison, value comparison
X=1
Y=1
X is y #x与y是同一个对象 and is a comparison of ID, which is the identity
Type (x) is type (y) #对象的类型本身也是一个对象, so you can compare the identities of two object types with IS
x = = y #== compares the values of two objects for equality
assigning operations to variables
    • Chained assignment: y=x=a=1
    • Multi-value assignment: x,y=1,2 x,y=y,x
    • Incremental assignment: X+=1
does not depend on the index value, the iteration method takes the value

T= (a)

For I in T:

Print (i)

Category:

First, List
Definition: [] separated by commas, by index, holding various data types, each position represents an element
features :
1. can store multiple values
2. Can modify the value of the specified index position, variable
3. Define list elements in left-to-right order, order access from 0, ordered

List creation  

list_test=[' LHF ', ' OK '
Or
List_test=list (' abc ')
Or
list_test=list ([' LHF ', ' OK ') 1

List common operation functions:

Print (l[0]) #列表取值

Len (l) #计算列表长度

L[1:3] #切片 Note: Slices are read operations and do not modify the original data

Append () #追加, the default appended element is at the right
L.append (' Alex ') #向列表l中追加元素 ' Alex ', plus on the right

Insert () #插入, specify the insertion position when you use it
L.insert (0, ' Alex ') #向列表l的第一个索引位置追加元素 ' Alex '

Pop () #删除, delete the last item by default, the rightmost element, to specify a location
L.pop (0) #删除列表第一个元素

Print (L.pop (0)) #输出打印刚刚删除的元素

in# contains

' Alex ' in List #判断元素 ' Alex ' is in the list, the return value is TRUE or False

Other actions for the list
Index () #查看某一元素的索引号, no error found

Extend () #扩展列表

L.extend ([i]) #将列表 [All-in-one] append to the list L, merged into a list,
Remove () #删除元素 by default (that is, delete from the left)
L.remove (' A2 ') #指定元素删除
Sort () #排序, from small to large, no return value
Reverse () #反转操作, no comparison [1,2,3,4]>>[4,3,2,1]

Two, the meta-group

Definition: Similar to a list, except [] Change to ()
Characteristics:
1. Can hold multiple values, the elements of a tuple can be any data type
2. Immutable
3. Define the tuple elements in left-to-right order, with the subscript starting from 0 sequential access, ordered
Tuple creation
ages = (11, 22, 33, 44, 55)
Or
t = tuple ((11, 22, 33, 44, 55))

Ganso Common operation functions: similar to lists

t= (' A ', ' B ', 1, (3,4))
Print (t[4][0]) #取值
Print (T[1:3]) #切片
Print (len (t)) #长度
Print (' A ' in T) #包含, return value TRUE or Fales

Third, the dictionary

  

Definition: {key1:value1,key2:value2},key-value structure, key must be immutable type, or can be hash
Basic form: Key:value
Characteristics:
1. can store multiple values
2. Can modify the value of the specified key, variable
3. Unordered

Dictionary Common operation functions:

Print () #字典的取值是无序的

Python Basics (3) _ Lists, Ganso, and dictionaries

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.