The composition of the variable:
Variables are made up of variable names, assignment symbols, variable values, and variable types.
Variable name →n = 1← Variable Value
↑
Assignment symbols
Comparison method:
Identity comparison: The ID of the variable value is compared
>>> x = 300>>> ID (x)46137936>>> y = 300>>> id (y) is Yfalse>>>
Comparison of values: compare two variable values
>>> x = 300>>> y = 300>>> ID (x)54002288>>> ID (y)54002352>> > x = = Ytrue>>>
Type comparison: Compare types of two variables
>>> x = 300>>> y = 300>>> type (x)<class'int' >>>> type (y)<class'int' is type (y) True>>>
Variable assignment operation (supplemental):
x = 1 #就是一个赋值操作 No return value chain assignment: a=b=c=10 multivariate assignment: XY value interchange x = 1 y = 2 salted fish Exchange method: z = x x = y y = z Large God Exchange: Python: x,y=y,x Example:
>>> x = 1>>> y = 2>>> x, y = y,x>>> 2>>> y1>&G T;>
Increment Assignment:
x = x +=1 #+-*/extract sequence type: s = ' hello ' salted fish method: a = S[0] b = s[1] ... Great God Law: a,b,c,d,e=s only print out the corresponding values of a and E, use ' _ ' to populate the middle variable, ' _ ' convention is commonly known as the discarded variable a,_,_,_,e = s A,*_,e #使用 *_ represents the middle of all
>>> s ='Hello'>>> a,b,c,d,e =s>>>a'h'>>>b'e'>>>C'L'>>>D'L'>>>e'o'>>> a,*_,b =s>>>a'h'>>>b'o'>>>
Classification:
One: variable non-changeable
mutable: List, dictionary
Immutable: Numbers, strings, tuples
Two: The number of stored values
A value: A number, a string
Multiple values (container type): list, tuple, dictionary
Three: The method of taking value
Direct value: Number
Sequence type: string, tuple, list
Mapping Type: Dictionary
mutable or immutable definition: In the case of constant ID and type, data with variable values is a mutable type. Id,type,value Immutable types
Listing list:
Attributes: The elements in the list can be arbitrarily deleted and the list's ID and type will not change, and ist is a mutable type. All elements in the list can be either an int or a list (a child list). The values in lists are ordered.
Define a list:
L = [1, ' abc ', [3, ' a ']]
Value: Value by index
Print (L[0],l[2][0])
Cycle: Liang
#!/usr/bin/env python#-*-coding:utf-8-*-l=[1,2,3,[4,5]]count=0 whileCount <Len (l):Print(L[count]) Count+=1L=[1,2,3,[4,5]] forCountinchRange (len (l)):ifType (L[count]) islist: forIinchRange (len (l[count)):Print(L[count][i])Else: Print(L[count])
List Common actions:
Slice:
l = [' Hello ', ' n ', ' abc ', 343, ' PPP ']
Print (L[1:3])
D:\Python\Python36-32\python.exe e:/python/123.py
[' ABC ']
Process finished with exit code 0
Additional:
l = [' Hello ', ' n ', ' abc ', 343, ' PPP ']
L.append (' Add_1 ')
L.append (' add_2 ')
L.append (' Add_3 ')
Print (L)
D:\Python\Python36-32\python.exe e:/python/123. py['hello'ABC 'PPP''add_1'add_2 'add_3']process finished with exit code 0
Insert:
l = [' Hello ', ' n ', ' abc ', 343, ' PPP ']
L.insert (0, ' insert_1 ')
Print (L)
L.insert (2, ' insert_2 ')
Print (L)
L.insert (4, ' insert_3 ')
Print (L)
D:\Python\Python36-32\python.exe e:/python/123. py['insert_1','Hello', 12,'ABC', 343,'PPP']['insert_1','Hello','insert_2', 12,'ABC', 343,'PPP']['insert_1','Hello','insert_2', 12,'Insert_3','ABC', 343,'PPP']process finished with exit code 0
Delete:
l = [' Hello ', ' n ', ' abc ', 343, ' PPP ']
Print (L)
L.pop ()
Print (L)
L.pop (2)
Print (L)
D:\Python\Python36-32\python.exe e:/python/123. py['hello'ABC 'PPP'['hello'] ABC', 343['hello', 343]process Finished with exit code 0
Contains:
l = [' Hello ', ' n ', ' abc ', 343, ' PPP ']
Print (in L)
Print (' abc ' in L)
Print (' n ' in L)
D:\Python\Python36-32\python.exe e:/python/123. Pytruetruefalseprocess finished with exit code 0
Show Index Location:
l = [' Hello ', ' n ', ' abc ', 343, ' PPP ']
Print (L)
Print (L.index (12))
Print (L.index (' Hello '))
D:\Python\Python36-32\python.exe e:/python/123. py['hello'ABC 'PPP']10Process finished with exit code 0
Number of calculated elements:
l = [' Hello ', ' n ', ' abc ', 343, ' PPP ']
L.append (' Hello ')
Print (L)
Print (L.count (' Hello '))
D:\Python\Python36-32\python.exe e:/python/123. py['hello'ABC "PPP"'hello']2 Process finished with exit code 0
Extended list:
l = [' Hello ', ' n ', ' abc ', 343, ' PPP ']
L.extend ([1,2,3,4]) #只能扩展列表, if you need to add dictionaries and tuples to the list, you need to use append
Print (L)
D:\Python\Python36-32\python.exe e:/python/123. py['hello'ABC 'PPP', 1, 2, 3, 4]process finished with exit code 0
remove element: remove element by element name
l = [' Hello ', ' n ', ' abc ', 343, ' PPP ']
L.remove (' abc ') #如果列表有多个元素, only the first one is removed
Print (L)
D:\Python\Python36-32\python.exe e:/python/123. py['hello'PPP ']process finished with exit code 0
Sequential arrangement:
l=[2,3,1]
L.sort ()
Print (L)
D:\Python\Python36-32\python.exe e:/python/123. py[1, 2, 3]process finished with exit code 0
Reverse list:
L=[' A ', ' C ', ' Alex ']
L.reverse ()
Print (L)
D:\Python\Python36-32\python.exe e:/python/123. py['Alex'C 'a']process finished with exit code 0
Meta-group:
Attribute: All elements within a tuple cannot be changed, an element can be any data type
A tuple can contain a list, and the elements in a list in a tuple can be changed. Of course the list can also have tuples.
To define a tuple:
A = (' 1 ', ' 2 ', ' 3 ', [' s ', ' B '])
To change the elements of a list in a tuple:
A[3][0] = ABC
Value:
Print (A[0])
Cycle:
t = (' A ', 1, ' B ', 1, (3,4))
n = 0
While n < len (t):
Print (T[n])
n + = 1
For I in range (len (t)):
Print (T[i])
Common operations for tuples:
Count:
t = (' A ', 1, ' B ', 1, (3,4))
Print (T.count (1))
D:\Python\Python36-32\python.exe e:/python/123. py2Process finished with exit code 0
Index location:
t = (' A ', 1, ' B ', 1, (3,4))
Print (T.index (' B '))
D:\Python\Python36-32\python.exe e:/python/123. py2Process finished with exit code 0
Slice:
t= (' A ', 1, ' B ', 1, (3,4))
Print (T[1:3])
D:\Python\Python36-32\python.exe e:/python/123. PY ('b') Process finished With exit code 0
Contains:
t= (' A ', 1, ' B ', 1, (3,4))
Print (' A ' in t)
D:\Python\Python36-32\python.exe e:/python/123. Pytrueprocess finished with exit code 0
Python Foundation day-3