python common basic data types and functions

1, a digital type int

  - int (string converted to digital)

a = "123"
print(type(a),a)

b = int(a)
print(type(b),b)

num = "0011"
v = int(num,base=16)
print(v)

    PS: type print data type, base = 16 is to print out the decimal num results in hexadecimal.

 

  - bit_lenght (the current number of binary digits)

age = 5
r = age.bit_length()
print(r)

    PS: 5 to 101 binary number (three digits), so the print result of three.

 

2, the type of string str

  - capitalize (first letter capitalized)

test = "alex"
v = test.capitalize()
print(v)

    PS: Output Alex.

 

  - Lower and casefold and islower (the first two letters will change all lowercase, casefold more advanced, a lot can change lowercase unknown special symbols, the last to determine whether lowercase)

test = "aLex"
v1 = test.casefold()
print(v1)
v2 = test.lower()
print(v2)
test = "aLex"
v = test.islower()
print(v)

    PS: Alex first stage output, the output of the second segment False, Upper and issupper supra, the string of uppercase and determines whether the string to uppercase.

 

  - Center and ljust and the rjust (width are provided, and then the contents centered, left aligned, right aligned)

test = "aLex"
v = test.center(20,"*")
print(v)
test = "aLex"
v = test.ljust(20,"*")
print(v)
test = "aLex"
v = test.rjust(20,"*")
print(v)

    PS: outputs ******** aLex ********, aLex ****************, ********** ****** aLex, 20 refers to the generation of the total length of the set, "*" is filled with content and only one character (available in Chinese), the default do not fill the space.

 

  - COUNT (look for sequences in a string of occurrences)

test = "aLexalexr"
v1 = test.count('ex')
print(v1)
v2 = test.count('ex',5,6)
print(v2)

    PS: indicates the number of lookups ex appear in the string, represents 5,6 Look defined between the fifth to the sixth character, v1 output 2, v2 0 output.

 

  - endsWith and startsWith (ending what character and start what character)

test = "aLex"
v1 = test.endswith('ex')
print(v1)
v2 = test.startswith('ex')
print(v2)

    PS: v1 is True, v2 is Flase, 'ex' can be added later in determining which parameter indicates the interval.

 

  - the Find and index (from front to back string substring find the location of the first occurrence)

test="alexalex"
v1 = test.find('ex')
print(v1)
v2 = test.find('ex',5,8)
print(v2)

    PS: v1 output 2, v2 represents the look in [5,8) interval, if no results are returned to find -1, index usage above, but find no error will result, recommended find.

 

  - the format (format, the string is replaced with a placeholder value specified)

test = 'i am {name},age{a}'
print(test)
v = test.format(name='alex',a=19)
print(v)
test = 'i am {0},age{1}'
print(test)
v = test.format('alex',19)
print(v)

    PS: output as two pieces of code, can replace the contents described in numerical order.

 

  - isalnum and the isalpha (former determines whether string contains only letters and numbers, which determines whether the letters and characters)

test = "uasf_+"
v = test.isalnum()
print(v)
test="as2df"
v = test.isalpha()
print(v)

    PS: the first stage output False, if the output is removed _ + True; second stage output False, if removed two output True.

 

  - expandtabs (according to the number of characters for a string punctuation)

test="usebcsdy\tsdvdve"
v = test.expandtabs(6)
print(v)

    PS: every six punctuation characters, when faced \ t automatically cut off and the number of characters occupy the rest of the print results usebcs dy \ t sdvdve.

 

  - isdecimal and isdigit and IsNumeric (determines whether the input is a number, the first common, because it can be used to calculate the second number can also identify a special third number can be recognized on the Chinese)

test=""
v1 = test.isdecimal()
v2 = test.isdigit()
print(v1,v2)
test=""
v1 = test.isdecimal()
v2 = test.isdigit()
v3 = test.isnumeric()
print(v1,v2,v3)

    PS: the first stage output v1 False, v2 output True; second segment v1, v2 output False, v3 output True.

 

  - isIdentifier (used to determine whether the legal variable name)

a = "def"
v = a.isidentifier()
print(v)

    PS: Output True, isidentifier judgment rule only letters, numbers and underscores.

 

  - isPrintable (whether there is a character not displayed)

test = "oiuas\tdfkj"
v = test.isprintable()
print(v)

    PS: \ t After printing is not displayed, the output False, remove \ t output is True.

 

  - isspace (to determine whether all spaces)

test = ""
v = test.isspace()
print(v)

    PS: empty string output False, when all spaces output True.

 

  - title and istitle (former converted to capitalize the first letter of each word in the title format, which determines whether the title format)

test = "Return True if all cased characters in S are uppercase and there is"
v1 = test.title()
print(v1)
v2 = test.istitle()
print(v2)

    PS: v1 all the words capitalized, v2 output False.

 

  - the Join (string elements in each of the specified delimiter splicing)

= Test " girlfriend fangfang " 
V = "  " .join (Test)
 Print (V)

    PS: Output "girlfriend Fang Fang", the space can also be spliced ​​to replace other symbols.

 

  - Strip and lstrip and The rstrip (Removes the specified string from both sides, left, right and most preferentially matching substring, the default remove spaces, \ t, \ n)

test = " alex "
v1 = test.strip()
v2 = test.lstrip()
v3 = test.rstrip()
print(v1,v2,v3)
test = "alexxa"
v = test.rstrip('xa')
print(v)

    PS: the first stage output alex, alex (blank), (blank) alex, the output of the second paragraph of ale.

 

  - maketrans and Translate (corresponding to create characters and replace)

v = "liuguifangxiaobaobei"
m = str.maketrans("aeiou","12345")
new_v = v.translate(m)
print(new_v)

    PS: the string is replaced with 12345 aeiou output l35g53f1ngx314b14b23.

 

  - Partition and rpartition , Split and rsplit (two front left and right divided according to a given character string comprising characters divided into three parts, the divided two split does not include multi-character)

test = "testasdsddfg"
v1 = test.partition('s')
v2 = test.split('s')
print(v1,v2)

    PS: v1 output [ 'te', 's',' tasdsddfg '], rpartition from the right side of the first divided s, v2 output [' te ',' ta ',' d ',' ddfg '],' s' after division can also add several parameters s, rsplit the right division.

 

  - splitlines (can only be divided according to a newline)

test = "liu\ngui\nfang\n"
v = test.splitlines(False)
print(v)

    PS: output [ 'liu', 'gui', 'fang'], True and False parameters decide whether to keep line breaks.

 

  - swapcase (all character case conversion)

test = "aLex"
v = test.swapcase()
print(v)

    PS: output AlEX.

 

  - Replace (substring in the replacement string)

test = "liuguifangu"
v = test.replace("gu",'bbb')
print(v)

    PS: Output "liubbbifangu", the 'bbb' number of parameter settings can also add the string to be replaced.

 

3, list the type of list

   - the append (be appended to the original list)

li = [11,22,33,44]
li.append(5)
li.append("alex")
li.append([1234,2314])
print(li)

    PS: output [11,22,33,44, 'alex', [1234,2314]].

 

  - the Clear (Clear List)

li = [11,22,33,44 ] 
li.clear () 
print (li)

    PS: output [], an empty string.

 

  - Copy (Copy String)

= li [11,22,33,44 ] 
v = li.copy ()
 print (v)

    PS: This is a shallow copy copy.

 

  - COUNT (count the number of elements appear)

= li [11,22,33,22,44 ] 
v = li.count (22 )
 print (v)

    PS: Output 2.

 

  - Extend (extended original list)

= Li [11,22,33,44 ] 
li.extend ([ 9898, " Liugui Fang " ]) 
li.extend ( " Liugui Fang " )
 Print (Li)

    PS: output [11,22,33,44,9898 'Liugui Fang', 'Liu', 'you', 'aryl'], is an iterative Extend the object, i.e. extended by character, to the append will append a whole to the back.

 

  - index (the index value acquisition position)

= li [11,22,33,22,44 ] 
v = li.index (22 )
 print (v)

    PS: left start index, to a given value when the index index is stopped, the output 1.

 

  - INSERT (insert elements at the specified index)

= [11,22,33,44 ] 
li.insert (0, 99 )
 print ()

    PS: 99 represent insertion, the output [99,11,22,33,44] 0 position.

 

  - POP (delete a value, and can get the value deleted)

= li [11,22,33,44 ] 
v = li.pop ()
 print (li)
 print (v)

    PS: pop is to remove the last default parameters, can be added to delete parameter specifies the value of the output li [11,22,33], v 44 output.

 

  - the Remove (delete the specified value in the list)

li = [11,22,33,22,44 ] 
li.remove ( 22 )
 print (li)

    PS: left side search, delete search stops, the output [11,33,22,44].

 

  - Reverse (Flip list)

li = [11,22,33,44 ] 
li.reverse () 
print (li)

    PS: Output [44,33,22,11].

 

  - the Sort (sort the list)

it = [11,44,22,33 ,] 
li.sort () 
print (it) 
li.sort (reverse = True)
 print (you)

    PS: Output [11,22,33,44] and [44,33,22,11], default from small to large.

 

4, tuple type tuple

   - COUNT (get a specified number of characters that appear in the tuple)

tu = (11,22,33,22,44 ,) 
of = tu.count (22 )
 print (in)

    PS: Output 2.

 

  - index (the index value acquisition position)

tu = (11,22,33,22,44 ,) 
of = tu.index (22 )
 print (in)

    PS: retrieve left side, to a given value when the index index is stopped, the output 1.

 

5, the dictionary dict

   - fromkeys (a sequence of values to create a dictionary and specify unified under)

v = dict.fromkeys(["k1",123,"999"],123)
print(v)

    PS:输出{123:123,'k1':123,'999':123},注意字典是无序的。

 

  -get(根据key获取值)

dic = {"k1":'v1',"k2":'v2'}
v = dic.get('k1',111)
print(v)

    PS:输出v1,如果k1不在字典里,则输出参数111,如无参数默认输出None。

 

  -poppopitem(删除并获取值)

dic = {"k1":'v1',"k2":'v2'}
v = dic.pop('k1',90)
print(dic,v)
k,v = dic.popitem()
print(dic,k,v)

    PS:前者删除给定键的键值并返回值,如果不存在键则返回参数90;后者随机删除一对键值并返回键值对。

 

  -setdefault(在字典中设置值)

dic = {"k1":'v1',"k2":'v2'}
v = dic.setdefault('k11','123')
print(dic,v)

    PS:输出{'k1':'v1','k11':'123''','k2':'v2'}  123,若字典中包含给定键,则返回该键对应的值,否则返回为该键设置的值。

 

  -update(更新字典)

dic = {"k1":'v1',"k2":'v2'}
dic.update(k1=123,k3=456,k5="asdf")
print(dic)

    PS:输出{'k1':123,'k5':'asdf','k2':'v2','k3':345},当字典里有给出的key时更新值,没有时则加入键值对。

 

  -keysvaluesitems(提取字典的全部键、值、键值对)

dic = {"k1":'v1',"k2":'v2'}
v1 = dic.keys()
v2 = dic.values()
v3 = dic.items()
print(v1,v2,v3)

    PS:字典还有clear(清除)、copy(拷贝)等用法和其他数据类型类似,就不提了(主要是懒)。

 

Guess you like

Origin www.cnblogs.com/liuguifang/p/11502329.html
Recommended