7.3 data types and built-in method

Integer

  1. Uses: QQ number, phone number, ID number (without the X's) ...

  2. Define the mode: the internal principle equivalent age = 18 # age = int (18)

  int only pure digital conversion string, the string will not work with any other, including the decimal point

Hexadecimal conversion

  1. Binary Decimal: 10

  1*(2**1) + 0*(2**0)

  print(int('10',2)) 

  2. octal decimal turn: 110

  1*(8**2) + 1*(8**1) + 0*(8**0)

  print(int('10',2)) 

  3. Hex turn Decimal: 110

  1*(16**2) + 1*(16**1) + 0*(16**0)

  print (int ( '110', 16)) 10 rpm decimal radix.

Other binary decimal conversion

Decimal Binary

  print (bin (12)) # 0b1100 0b indicates that the following number is a binary number

  Decimal to octal

  print (oct (12)) # 0o14 0o indicates that the following numbers are octal

  Decimal to hexadecimal

  print (hex (12)) # 0xc 0x indicates that the following number is a hexadecimal number

  And immutable type variable

Variable Type: if the data value is changed, id unchanged

Immutable type: if the data value is changed, id has changed

  The int type is immutable

The concept of order and disorder

  Use the index value of the data is ordered, or disordered.

  So integral are unordered

Float

  1. Role: salary, height, weight

  2. Definition: internal principle is salary = 1.1 # salar = float (1.1)

String type

  1. Role: descriptive data

  2. Definitions: internal principle is s = 'hello baby' # s = str ( 'hello baby')

1. Press the index value (value only)

Take forward

s = 'hello world'
print(s[0])

Reverse Take

s = 'hello world'
print(s[-1])

2. Slice

Care regardless of the end, the step, the interception of a new small strings from a larger string, the left head is fixed, it is fixed to the right end of

S = ' Hello Big Baby ~ ' 
Print (S [0:. 5])   # Hello 
Print (S [0: 10:. 1])   # Hello Big step is not written by default. 1 
Print (S [0: 10: 2] )   # hlobg step representing a taken every few

Negative values

s = 'hello big baby~'
print(s[-1])  
print(s[5:0:-1])  
print(s[-1:-10:-2])  

3. length

len (): statistics is the number of characters in the string (special)

p = ' abcde ' 
print (referred to as (a))

4. The members of the operation

in and not in: determining whether there is a substring of string in a large

print('egon' in 'egon is dsb and egon is sb')
print('g' in 'egon is dsb and egon is sb')
print('jason' not in 'egon is dsb and egon is sb')
View Code

5. Remove the left and right sides of the character string

strip (): Delete the default left and right sides of the spaces, brackets plus characters, delete the corresponding character

name = '  francis  '
print(name.strip())
name1 = '1francis'
print(name1.strip('1'))

lstrip (): Only delete the character to the left

name = '  francis  '
print(name.lstrip())

rstrip (): Only the right to delete characters

name = '  francis  '
print(name.rstrip())

6. Segmentation

split (): for a certain string according to the tissue separator, it can be cut into split list, the value for further

date = ' pick n | 123 | handsome ' 
print (data.split ( ' | ' ))

rsplit (): from right to left split

= Data ' JASO n-| 123 | Handsome ' 
Print (data.rsplit ( ' | ', . 1)). 1 # represents the number of the largest divided

Additional: join (): the plurality of types of data elements of the container to be spliced ​​by a specified character string

data = ['1','2','3','a','b']
data1 = '|'.join(data)
print(data1)

7. cycle

data = 'jaso n|123| handsome'
for i in data:
    print(i)

8.upper and lower

All uppercase characters

name = 'FranCis'
print(name.upper())

All lowercase characters

name = 'FranCis'
print(name.lower())

9. in order to determine what the beginning and end characters

= name ' Francis ' 
Print (name.startswith ( ' F. ' ))   # determines whether the string to begin with what 
Print (name.endswith ( ' S ' ))   # determines whether the end of the string to what

Three uses of 10.format

1. The first (by occupying a position consistent with the principles% s)

str1 = 'my name is {} my age is {}'.format('francis',23)
str2 = 'my name is {} my age is {}'.format(23,'francis',)
print(str1)
print(str2)

2. The second (by index placeholder)

str1 = 'my name is {0} my age is {1},{1},{0}'.format('francis',23)
print(str1)

3. The third (by name placeholder, the placeholder dictionary of similar key)

str1 = 'my name is {name} my age is {age},{age},{name}'.format(name='francis',age=23)
print(str1)

11.replace

A value in the replacement string

= STR ' Francis Age IS 23 is, Francis has A CAR ' 
RES = str.replace ( ' Francis ' , ' XXX ' ) 
RES1 = str.replace ( ' Francis ' , ' XXX ' ,. 1)   # . 1 represents replacement times, replace the default full- 
Print (str)
 Print (RES)
 Print (res1)

12.isdigit

Determining whether the character string contains all digital

age = '18'
age1 = 'aaa'
print(age.isdigit())
print(age1.isdigit())

13.find,rfind,index,rindex,count

= S ' Kevin Kevin IS and O IS DSB SB ' 
Print (s.find ( ' DSB ' ))   # returns the index where the d-characters 
Print (s.find ( ' XXX ' ))   # can not find the time no error returns -1 
Print (s.find ( ' I ' , 0,4))   # can also be limited by the look index 
Print (s.index ( ' O ' ))   # index character where the return pass value 
Print (s.index ( ' i ' , 0,3))   # returns the index where the preaching character, will not be found error 
Print (s.count ( 'the n- ' ))   # number of characters appear in statistics
View Code

14.center, light, rjust, zfill

= S ' Francis ' 
Print (s.center (13 is, ' * ' ))   # similar centered 
Print (s.ljust (40, '  ' ))   # similar Left 
Print (s.rjust (40, '  ' ))   # is similar to right-align 
Print (s.zfill (40))

15.expandtabs

Extended tabs

s10 = 'a\tbc'
print(s10.expandtabs(100))
View Code

16.captalize, swapcase, title

= S12 ' HELLO WORLD sH 20 is ' 
Print (s12.capitalize ())   # the Hello World initials, does not affect digital 
Print (s12.swapcase ())   # invert case 
Print (s12.title ())   # each the first letter of the word capitalized

17.is Digital Series

B = num1 ' . 4 '  # bytes 
num2 = u ' . 4 '  # Unicode, without adding to python3 u is the Unicode 
num3 = ' One '  # Chinese digital 
Num4 = ' IV '  # Roman numerals 
# '' .isnumeric (): Unicode, Chinese figures, insofar as it represents the Roman numeral numbers are identifying 
Print (num2.isnumeric ())
 Print (num3.isnumeric ())
 Print (num4.isnumeric ()) 

# '' .isdecimal (): Unicode recognizes only ordinary Arabic numerals 
Print (num2.isdecimal ())
 Print (num3.isdecimal ())
 Print (num4.isdecimal ()) 

#'' .isdigit (): bytes, the unicode isdigit usually used has to meet the needs of 
Print (num1.isdigit ())
 Print (num2.isdigit ())
 Print (num3.isdigit ())
 Print (num4.isdigit ( ))
View Code

From the above string is ordered (whenever the index data is ordered), and the type is immutable.

List Type

Role: a plurality of data, multiple accounts, multiple phone numbers, multiple names

Definition: there may be [] values ​​within a plurality of any type, separated by commas, the internal principle is l = [1,3,5,7] # l = list ([1,3,5,7])

1. Press index values ​​(preferably can be kept), sliced

  Forward Value

= L [1,2,3,4 ]
 Print (L [0:. 4: 1])   # 0: represents the range of the representative. 4 step 
Print (L [0 ::])

  Reverse Value

L = [1,2,3,4 ]
 print (l [5 :: - 1])

  Stored value

L = [1,2,3,4 ] 
l [0] = 69
 print (I)

2.append,insert,extend

Add data to the list

l = [11,22,33,44,55]
l1 = [99,88,77,66]
# 1.尾部添加一个66
l.append(66)  # 注意append值能将被添加的数据当作列表的一个元素
print(l)
# 2.任意位置添加元素
l.insert(2,96)  # 通过索引在任意位置添加元素
print(l)  # 注意insert值能将被添加的数据当作列表的一个元素
# 3.添加容器类型数据
l.extend(l1)  # 内部原理for循环l1一个个追加到列表的尾部
print(l)
View Code

3.len

列表长度,数据个数。

4.in和not in

判断数据是否属于列表

5.删除

del

l = [11,22,33,44,55]
print(l)
del l[2]  # del适用于所有的删除操作,用索引删除
print(l)

remove

l = [11,22,33,44,55]
print(l)
l.remove(33)  # 指定要删除的元素的值
print(l)

6.数据弹出

  尾部弹出

l = [11,22,33,44,55]
res1 = l.pop()  # 尾部弹出
res2 = l.pop()
res3 = l.pop()
print(res1,res2,res3)

  指定位置的数据弹出

l = [11,22,33,44,55]
res1 = l.pop(1)  # 可以指定索引 按照索引弹出元素
print(res1)

7.循环

l = [11,22,33,44,55]
for i in l:
    print(i)

 

Guess you like

Origin www.cnblogs.com/francis1/p/11129670.html