09-- python learning data type built-in method 2

A. List Type

1. Role: storing a plurality of values ​​by location

2. Definitions:

l=[1,2.5,'qqq']

3. Type conversion:

Whenever for loop can be traversed types can be used as a parameter to the list () turn as a list

res = list('niupi')

4. The built-in method:

4.1 accessed by index value (Forward + Reverse Access Access): which can be changed to take

 l = [111, 'egon' , 'hello']
 Forward taken
 print (l [0])
 Reverse taken
 print (l [-1])
 may be taken to be changed: the index modifying value corresponding to the presence of
 l [0 ] = 222
 Print (L)

4.2 slice (care regardless of the end, step)

l = [111, 'Egon', 'hello', 'a', 'b', 'c', 'd', [1, 2, 3]]
# print (l [0: 3])
# print ( s [0: 5: 2]) # 0 2 4

4.3 length

# Print (referred to as ([1, 2, 3]))

4.4 members and not in operation in

# print('aaa' in ['aaa', 1, 2])
# print(1 in ['aaa', 1, 2])

4.5 add value to the list

4.5.1 Additional

# L = [111, Egon, 'hello']
# l.append (3333)
# l.append (4444)
# print (l) 

4.5.2 interpolated value

# l=[111,'egon','hello']
# l.insert(0,'alex')
# print(l)


4.5.3 extend added value

New_l = # [l, 2,3]
# L = [111, 'Egon', 'Hello']
# l.append (new_l)
# Print (L)

# Extend achieve the above code
# l.extend (new_l)
# l.extend ( 'ABC')
# Print (L)


4.6, delete

# Way: common deletion method, simply delete no return value
# L = [111, 'Egon', 'Hello']
# del L [. 1]
# X = L del [. 1] # throws an exception, It does not support the assignment syntax
# Print (L)

# Second way: l.pop () according to the index deleted deleted values returned
# L = [111, 'Egon', 'Hello']
# l.pop () # is not specified index of default remove the last
# l.pop ()
# Print (L)

# RES = l.pop (1)
# Print (L)

# Print (RES)

# three ways: l.remove () to delete an elemental return None
L = # [111, 'Egon', [l, 2,3], 'Hello']
# l.remove ([l, 2,3])
# Print (L)
# l.remove RES = ( 'Egon' )
# Print (RES) None #

4.7 cycle

# l=[1,'aaa','bbb']
# for x in l:
# l.pop(1)
# print(x)

4.8 need to master the operation

= L [. 1, 'AAA', 'BBB', 'AAA', 'AAA']
4.8.1 l.count ()
# Print (l.count ( 'AAA'))

4.8.2 l.index ()
# Print (l.index ( 'AAA'))
# Print (l.index ( 'aaaaaaaaa')) # can not find the given

4.8.3 l.clear ()
# l.clear ()
# Print (L)

4.8.4 l.reverse (): not sorting is inverted list
# L = [. 1, 'Egon', 'Alex', 'LXX']
# l.reverse ()
# Print (L)

4.8.5 l.sort ( ): the list of elements must be of the same type can sort
# L = [. 11, -3,9,2,3.1]
# l.sort () # default from small to large, called ASC
# l.sort ( reverse = True) # descending rows, arranged in descending order of
# Print (L)

# L = [. 11, 'A', 12 is]
# l.sort ()

# L = [ 'C', 'E', ' A ']
# l.sort ()
# Print (L)


4.9 understand:

The string can, according to the character position corresponding to the size ratio of sequentially pk
size # string is distinguished in the order in ASCI code table, the table at the back in front of the character is greater than
# print ( 'a'> ' b' )
# Print ( 'ABZ'> 'ABCDEFG')

# learn: the size of the list may be the same ratio, the same principle of the string, but the element corresponding to the position of the same type must be
# l1 = [1, 'abc ', 'zaa' ]
# L2 = [. 1, 'ABC', 'ZB']
#
# Print (L1 <L2)


4.10 supplement

# 1, queue: FIFO, FIFO
# L = []
# # enqueue operation
# l.append ( 'First')
# l.append ( 'SECOND')
# l.append ( 'THIRD')
#
# Print (L)
# # dequeue
# Print (l.pop (0))
# Print (l.pop (0))
# Print (l.pop (0))

# 2, stack: LIFO, Last In first out
l = []
# stack operation
l.append ( 'First')
l.append ( 'SECOND')
l.append ( 'THIRD')

Print (L)
# dequeue
Print (l.pop ())
Print (L. POP ())
Print (l.pop ())

II. Tuple

 Tuple is "an immutable List"

1, the role:

By index / position storing a plurality of values, for reading not only for upgrading

2. Definition:

A plurality of spaced apart any type () within a comma element # t = (1,1.3, 'aa ') # t = tuple ((1,1.3, 'aa')) # print (t, type (t) )


# (mean 10) # single brackets denote contains X =
# Print (X, type (X))

# T = (10,) # if the tuple is only one element, must be a comma
# print (t, type (T))

# T = (1, 1.3, 'AA') # T = (0-> 1 memory address value, 1-> memory address value of 1.3, 2> value 'aaa' memory address)
T # [0] = 11111

# T = (1, [11,22]) # T = (0-> 1 memory address value, 1-> values [1,2] of the memory address,)
# Print (ID (T [0]), ID (T [. 1]))
# # T [0] = 111111111 # can be changed
# # t [1] = 222222222 # can be changed
#
# T [. 1] [0] = 11111111111111111
# # Print (T)
# Print (ID (T [0]), ID (T [. 1]))

3. Type Conversion

# print(tuple('hello'))
# print(tuple([1,2,3]))
# print(tuple({'a1':111,'a2':333}))


4, built-in method

1, according to the index value (Forward + Reverse take take):

We can only take T = # ( 'AA', 'BBB', 'CC')
# Print (T [0])
# Print (T [-1])


2, a slice (care regardless of the end, step)

# t=('aa','bbb','cc','dd','eee')
# print(t[0:3])
# print(t[::-1])


3, the length

# t=('aa','bbb','cc','dd','eee')
# print(len(t))

4, members and not in operation in

# print('aa' in t)


5, circulation

# for x in t:
# print(x)

III. Dictionary

1, the role:

Can be removed with a key value corresponding descriptive

2. Definition:

{} Separated by commas within the plurality of key: value, which value can be any type, but the
# key must be immutable and can not be repeated

 A way of making the dictionary:

D = {# 'K1': 111, (l, 2,3): D = dict # 222} (...) Print # (D [ 'K1'])
# Print (D [(l, 2,3 )])
# Print (type (D))

# D} = {# out of the defined default empty dictionary
# print (d, type (d ))

 Create dictionary of way:

# d=dict(x=1,y=2,z=3)
# print(d,type(d))

3, data type conversion

# info=[
# ['name','egon'],
# ('age',18),
# ['gender','male']
# ]
# # d={}
# # for k,v in info: # k,v=['name','egon'],
# # d[k]=v
# # print(d)

Three ways of making the dictionary

: # Res = dict (info) # line of code to get the above-described operation for loop
# print (res)


 Four ways of making the dictionary:

Fast Initialization a dictionary
# Keys = [ 'name', 'Age', 'Gender']
# # D = {}
# # for K in Keys:
# # D [K] = None
# # Print (D)
# D = {} .fromkeys (keys, None) # line of code to get the above-described operation for loop
# print (d)

4, built-in method

1, by the access key values: can be kept desirable

# D = { 'K1': 111}
# for assignment: key is present, is modified
# D [ 'K1'] = 222
# for assignment: key does not exist, create a new value
# d [ 'k2'] = 3333
# Print (d)

2, the length len

# D = { 'k1': 111, 'k2': 2222, 'k1': 3333, 'k1': 4444}
# print (d)
# print (len (d))

3, members of the operations in and not in: According to key

# d={'k1':111,'k2':2222}
# print('k1' in d)
# print(111 in d)

4, delete

D = { 'K1': 111, 'K2': 2222}
 4.1 universal Remove
# del D [ 'K1']
# Print (D)

 4.2 POP Delete: delete element according key, return the value argument delete key corresponding to
# res d.pop = ( 'K2')
# Print (D)
# Print (RES)

 4.3 popitem delete: delete random, returns a tuple (delete key, delete the value)
# d.popitem RES = ()
# Print (D )
# Print (RES)


5, key Keys (), the value of values ​​(), on the key-value items ()

=> Is obtained in a hen python3
D = { 'K1': 111, 'K2': 2222}
'' '
in the python2
>>> D = {' K1 ': 111,' K2 ': 2222}
> >>
>>> d.keys () #. 6, the cycle
[ 'K2', 'K1']
>>> d.values ()
[2222, 111]
>>> d.items ()
[( 'K2', 2222), ( 'K1', 111)]
>>> dict (d.items ())
{ 'K2': 2222, 'K1': 111}
>>>
'' '

6, for circulation

# for k in d.keys():
# print(k)
#
# for k in d:
# print(k)

# for v in d.values():
# print(v)

# for k,v in d.items():
# print(k,v)


# print(list(d.keys()))
# print(list(d.values()))
# print(list(d.items()))

7. The need to have a built-in method

= {D 'K1': 111}
. 1, d.clear ()

2, d.update ()
# d.update ({ 'K2': 222, 'K3': 333, 'K1': 111111111111111})
# Print (D)

. 3, d.get (): the key value, good fault tolerance
# print (d [ 'k2' ]) # key does not exist, error

# Print (d.get ( 'K1')) # 111
# print (d.get ( 'k2') ) # key is not present is not given, returns None

. 4, d.setdefault ()
# info} = {
# IF 'name' in info:
# ... # equivalent Pass
# the else :
# info [ 'name'] = 'Egon'
# Print (info)

# 4.1 if the key has not added, the dictionary returns a value corresponding key
info = { 'name': 'Egon'}
RES = info.setdefault ( 'name', 'Egon')
# Print (info)

Print (RES)

# 4.2 if the key is not added, the dictionary returns a value corresponding key
info {} =
RES = info.setdefault('name','egon')
# print(info)
print(res)

Guess you like

Origin www.cnblogs.com/heirenxilou/p/12462373.html