05 built-in data types and methods

1. integer (int)

1.1 Use: keep QQ number, phone number, ID number and other string integer

1.2 Definitions way:

age = 18 # age = int (18) int digital-only put in brackets, the decimal point will not even

E.g:

int ( 'fsfsdfa') # error

int ( '1.2') # Error

1.3 hex conversion

  1.3.1 Other binary decimal turn (int) method

    print (int ( '1000', 2)) means is binary to decimal 1000

    print (int ( '1000', 8)) means octal to decimal 1000

    print (int ( '1000), 16)) means is a hexadecimal to decimal 1000

  1.3.2 Decimal turn the other band

    print (bin (12)) bin representative of converted into binary

    print (oct (12)) oct Representative converted into octal

    print (hex (12)) hex Representative converted to hexadecimal

1.4 Integer 'of the variable immutable type'

Variable Type: changing the value of the case where the same ID number

Immutable type: value is changed, ID number changed

x=20

print(id(x))#504131648

x=21

prnt(id(x))#504131680

2. Float

2.1 Role: salary, height, weight

2.2 definitions: height = 178.1 # height = float (178.1)

 

3. String

3.1 Uses: Descriptive

3.2 definitional manner: '', '' '', '' '' ''

s='hello world'  #s=str('hello world')

Lists and dictionaries can be converted into a string

s1 = str ([1,2,3,4])

print(type(s1))

s2=str({'name':'wuxi','pwd':123})

print(type(s2))

3.3 by index values ​​(taking into Forward + Reverse taken): Since strings are immutable, and therefore can only be taken, can not be modified

a='wuxi is handsome'

print(a[0])

3.4 sections (bulk value, has been taking multiple values):

Defined in [], the fill index value, which represents the index value to the index value which starts from the end. [] Within the main need care regardless of the end

Remove small section of string in a large string.

And 'value according to the index' a character is to take

a='wuxi is handsome'

Print (a [0: 4]) # in accordance with the order index, taken wuxi.

1.4.1 slices can specify the value of step

print (a [1: 5: 2]) the last digit 2, a representative of taking a spacer.

1.4.2 backwards can slice values ​​(default is from left to right) understanding of knowledge

print (a [-1]) # takes the last value

print (a [0: 5: -2]) # error, since the default is taken from the right, if taken backwards, before -2 index should also be changed. Correct below

print(a[-1:-10:-2])

3.5 length len:

Character length for a string of statistics

a='wuxi looks very good'

print (referred to as (a))

3.6 members in operation and not in:

For determination. Determining whether there is a character sub-string to another. The result is True or False

a1='wuxi looks very handsome'

print(‘wuxi’ in a1)#True

print(‘nice’ not in a1)#False

3.7 on both sides of the string to remove the last characters strip (default take space, remove the contents can be specified), regardless of the character space in the middle

name='        shilingling        '#.strip()

3.7.1 If the removal of specified content, the effect of removing the spaces lost, unless otherwise specified

name2 = 'fff shilingling' # .strip ( 'f') a space comma f, only the left shilingling

name3='*****shilingling******'#.strip('*')

name4='**+**/**shilingling ****_**'#.strip('*+/-')

3.7.2 User interaction

  name=input(:>>>).strip()

  print(name)

3.8 Segmentation split:

For some character strings tissue separator, the separator can be excised split, and output as a list, index, or conveniently may be the final decompressed value

Case 1 (save the results as a list that does not involve values):

 info='wuxi:178:23:75::/root/bin/bash'

res = info.split ( ':') # removed: Separator

print (res) # Results [ 'wuxi', '178', '23', '75', '', '/ root / bin / bash']

Case 2 (decompression value):

cmd='surfing|a.txt|334343'

cmd_name,filename,filesize=cmd.split('|')

3.9. Cycle, not to speak today

 

4. Other built-in methods on strings need to know

4.1 .lower () ,. upper () all lowercase or all uppercase

s='My Name is Shilingling'

res=s.upper()

print(res)#MY NAME IS SHILINGLING

print(s)#My Name is Shilingling

Experimentally known, calling string (ie. The latter method) does not change the string itself

4.2startswith,endswith

In what used to determine whether a string beginning or end

info='shilingling is beautiful'

print(info.startswith('shilingling'))

Three uses 4.3format of (a form of formatted input, pyhton recommend using the syntax)

4.3.1 by occupying a position with the same% s

info1='my name is {} my age is {}'.format('wuxi',18)

print(info1)#my name is wuxi my age is 18

4.3.2 transmission according to the corresponding index value placeholder and rear

info3='my name is {1} my age is {0}'.format('wuxi',18)

print(info3)#my name is 18 my age is wuxi

4.3.3 key parameter passing. Like a dictionary key: value

info4='my name is {name} my age is {age}'.format(name='wuxi',age=18)

4.4split, repli

 msg='get|a.txt|333331231'

print(msg.split('|',1))

print(msg.rsplit('|',1))

['get', 'a.txt|333331231']
['get|a.txt', '333331231']

4.5join

The opposite of split. split segmentation is to remove the separator; the separator, while the join is added


= Data 'Jason | 123 | Handsome'
res_list = data.split ( '|')
Print (res_list) # [ 'Jason', '123', 'Handsome']

. res_str = '$' the Join (res_list) # container the plurality of types of elements together into a string of characters designated by
Print (res_str) $ 123 $ # Handsome Jason
L = [ '. 1', 'a', 'B']
RES = '|' .join (L)
Print ( res) # 1 | a | b
4.6 replace 
Alternatively
STR = 'Egon Egon IS and IS DSB SB of He has BENZ A'
RES = str.replace ( 'Egon', 'Kevin',. 1)
Print (RES) and Egon DSB #kevin IS IS A SB of He has BENZ
Print (STR)

4.7isdigit determines whether the character string contains purely digital
True the while: 
Age = INPUT ( ">>>: ')
IF age.isdigit ():
Age = int (Age)
IF Age> 38 is:
Print (' good aunt ')
the else:
Print (' good input ')
5. built-in method needs to know:
5.1find, rfind, index, rindex, COUNT
S = 'MSL LSR iS iS DSB and SB'
print (s.find ( 'DSB')) # returns the index where the d character
print ( s.find ( 'sss')) # can not find the time to return -1
Print (s.find (' I ', 0,3)) with index # limit lookup quyu
Print (s.index (' O) ) # returns the index where the transmitted character
print (s.index ( 'i', 0,3)) # returns the index value of the transmitted character
print (s.count ( 'n') ) # number of characters appear in the count
5.2center, ljust, the rjust, zfill
S = 'Wuxi'
print (s9.center (12, '*')) 
print (s9.ljust (40, '$'))
print (s9.rjust (40, '$'))
print (s9.rjust (40 '') )
print (s9.zfill (40))
5.3expandtabs
s10 = 'a\tbc'
print(s10.expandtabs(100))
5.4captalize,swapcase,title
= S12 'HELLO WORLD SH10' 
Print (s12.capitalize ()) # the Hello world capitalized
print (s12.swapcase ()) # invert case
print (s12.title ()) # size of the first letter of each word
5.5
5, is a digital series 
num1 = B'4 '#Bytes in
num2 = u'4' # Unicode, python3 is no need to add u Unicode
num3 = 'One' # Chinese digital
num4 = 'Ⅳ' # Roman numerals
# '' .isnumeric (): unicode, Chinese numerals, Roman numerals represent numbers as long as all identifying
Print (num2.isnumeric ())
Print (num3.isnumeric ())
Print (num4.isnumeric ())

# '' .isdecimal (): Unicode only Arabic numerals identify common
Print (num2.isdecimal ())
Print (num3.isdecimal ())
Print (num4.isdecimal ())

# '' .isdigit (): bytes, using the unicode isdigit usually has to meet the needs of
Print (num1.isdigit ())
Print (num2.isdigit ())
Print (num3.isdigit ())
Print (num4.isdigit ())
6. The list of types of action: more equipment, more loving, and more courses, more than a girlfriend, etc.
Definition: there may be [] values within a plurality of any type, separated by commas 
my_girl_friends = [ 'alex', ' wupeiqi', 'yuanhao', 4,5] # nature my_girl_friends = list ([...])
or
l = list ( 'ABC')
L1 = list ({ 'name': 'Jason', 'password': '123'})
(L1) Print
internal principle list for loop and a value list to a stuffed
6.1 accessed by index value (Forward + Reverse Access Access): may be taken to deposit 
L = [1,2,3,4]
Print (L [0:. 4:. 1])
Print (L [ :: 0])
Print (L [::. 5 -. 1])
Print (ID (L))
L [0] = 69
Print (ID (L))
Print (L)

6.2 slice (care regardless of the end, step)
L = [ 'Egon', 'LXX', 'YXX', 444,555,66666]
Print (L [0:. 5])
Print (L [0:. 5: 2])
print (l [:: - 1] ) #

6.3 length
L = [ 'Egon', 'LXX', 'YXX', 444,555,66666, [l, 2,3]]
Print (len (L))

6.4 members in operation in and Not
L = [ 'Egon', 'LXX', 'YXX', 444,555,66666, [l, 2,3]]
Print ( 'LXX' in L)
Print (in 444 L)

追加6.5
l = [Egon, 'LXX', 'yxx]
l.append (44 444)
l.append (55555)
print (l)

6.6 is inserted to the front of the specified index value
L = [ 'Egon', 'LXX', 'YXX']
l.insert (0,11111)
Print (L)
l.insert (2,2222222)
Print (L)

Add multiple elements #
L = [ 'Jason', 'Nick']
l.extend ([ 'Tank', 'Sean'])

6.7 Delete

# Simply delete the value:
# mode 1:
del L [1] # generic
print (l)

Mode # 2:
RES = l.remove ( 'LXX') # specify the value to be deleted, return is None
Print (L, RES)

# Take a value from the list
res = l.pop (-1) # (default is removed from the end) in accordance with the deleted index values, return that value to delete
print (l, res)

Guess you like

Origin www.cnblogs.com/ZDQ1/p/11126736.html