I. Basic data type 1. String
Class: Str
Method: Select str, hold command (CTRL) + Left button to jump to the corresponding method
The usual methods of string are summarized as follows:
Function: Implement the first letter of the string, not change itself, will generate a new value
Capitalize
Example:
1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 name = ' Hexin ' 4 v = name.capitalize () #调用str类, execute the method where capitalize is 5 prin T (v)
Output:
Hexinhexin
Function: Turn all uppercase letters into lowercase, and support multiple language changes in addition
Casefold
Example:
1 name = ' Hexin ' 2 v = name.casefold () 3 print(name) 4 print (v)
Output:
Hexinhexin
Function:
Lower: lowercase all caps, limited English
Upper: Capitalize all lowercase
Lower
Example:
1 name = ' Hexin ' 2 v = name.lower () 3 print(name) 4 print (v)
Results:
Hexinhexin
Function: text centered, padding character in white space
Parameter 1: Total length, Parameter 2: space-filled characters (length 1)
Center
Example:
1 name = ' Hexin ' 2 v = name.center (' * ') 3 print(name) 4 print (v)
Output:
hexin*******hexin********
Function: Indicates the number of occurrences of a subsequence to find in a string
Parameter 1: The value to find (sub-sequence), Parameter 2: Start position (index), parameter 3: End position (index)
Count
Example:
1 name = ' HEXINDDAKLFJSL;DFJCNLJDAJSFLAJDF ' 2 v = name.count (' A ') 3 i = Name.count (' A ', 0,15) 4 print(name) 5 Prin T(v) 6 print (i)
Output:
31
Function: Judge whether to end with XX
Parameter 1: Judging value, Parameter 2, 3: Position of start and end (number)
EndsWith
Example:
1 name = ' HEXINDDAKLFJSL;DFJCNLJDAJSFLAJDF ' 2 v = name.endswith (' df ') 3 i = Name.endswith (' n ', 0,5) 4 print(name ) 5 Print(v) 6 print (i)
Output:
Hexinddaklfjsl;dfjcnljdajsflajdftruetrue
function: Find tab \ t, replace (with previous value)
Expandtabs
Example:
1 name = "AL\TE\TX\NALEX\TUU\TKKK" 2 v = name.expandtabs (5) #包含前面的值, 5 length 3 print (v)
Output:
Al e xalex uu KKK
Function: Find the index position of the specified subsequence, there is no return-1
Find
Example:
1 name = ' Hexin ' 2 v = name.find (' 0 ') 3 i = Name.find (' x ') 4 print(v) 5 print (i)
Output:
-12
Function: String formatting
format
Format_map
Example:
1 TPL1 = "I am:%s; age:%s; Gender:%s"% ( ' Hexin ', ' m ', ' Man ') 2 print(TPL1) 3 4 tpl2 = "I am: {0}; Age: {1}; Gender: {2}" 5 v2 = Tpl2.format ("Li Jie", 19, ' All Lines ') 6 print (v2) 7 8 tpl3 = "I am: {name}; Age: {}; Gender: {Gender}" 9 V3 = Tpl3.format (name= ' Li Jie ', age=19,gender= ' casual ') 10 Print(v3) one page tpl4 = "I am: {name}; Age: {Gender}; Gender: {"} "V4 = Tpl4.format_map ({' name ': ' Li Jie ', ' ages ': +, ' gender ': ' in ') }) Print (v4)
Output:
I am: hexin; age:; Gender: Man I am: Li Jie; age: N; Gender: All Right I am: Li Jie; age: N; Gender: Casual I am: Li Jie; age: 19; Gender:
Function: Whether it is a number or a Chinese character
isalnum
Example:
1 name = ' Hexin0 good ' 2 v = name.isalnum () 3 print (v)
Output:
True
- One) isdecimal,isdigit,isnumeric
Function: Whether it is a number
isalnumIsdecimalIsDigit
Example:
1 num = ' two ' 2 V1 = num.isdecimal () # ' 123 ' 3 v2 = Num.isdigit () # ' 123 ', ' ② ' 4 v3 = num.isnumeric () # ' 123 ', ' two ', ' ② ' 5 prin T (V1,V2,V3)
Output:
False False True
Function: Whether it is a valid identifier
Isidentifier
Example:
1 n = ' 1name ' 2 u = ' name ' 3 v = n.isidentifier () 4 i = u.isidentifier () 5 print(v) 6 print (i)
Output:
Falsetrue
function: all lowercase (uppercase)
Islower
Example:
1 name = ' Hexin ' 2 name1 = ' hexin ' 3 v = name.islower () 4 i = name1.islower () 5 print(v) 6 print (i)
Output:
TrueFalse
Function: Contains implied XX (False for invisible characters including \n,\t)
isprintable
Example:
1 name = ' Hexindas\talj,hexin ' 2 v = name.isprintable () 3 print (v)
Output:
False
Function: Element stitching
Join
Example:
1 name = ' Hexin ' 2 3 v = ' _ '. Join (name) # inner loop each element 4 print(v) 5 6 name_list = [' 1 ', ' 2 ', ' 3 ', ' 4 ']7 v = "+". Join (Nam E_list) 8 Print (v)
Output:
H_e_x_i_n1+2+3+4
Function: Fill left and right, like center
Ljust
Example:
1 name = ' Hexin ' 2 v = name.ljust (+, ' * ') 3 i = Name.rjust (6, ' * ') 4 print(v) 5 print (i)
Output
Hexin**********hexin
Function: Create correspondence, translate transformations
Maketrans
Example:
1 m = Str.maketrans (' Aeiou ', ' 12345 ') # correspondence 2 name = "AKPSOJFASDUFASDLKFJ8AUSDFAKJSDFL;KJER09ASDF" 3 v = name.translate (m) 4 print (v)
Output:
1kps4jf1sd5f1sdlkfj815sdf1kjsdfl;kj2r091sdf
Function: Split, preserve split elements
Partition
Example:
1 content = "9sb6sb6" 2 v = content.partition (' SB ') # Partition3 print (v)
Output:
(' 9 ', ' SB ', ' 6sb6 ')
function: Replace
Replace
Example:
1 content = "1SB2SB3SB4" 2 v = content.replace (' sb ', ' Love ') 3 print(v) 4 v = content.replace (' sb ', ' Love ', 1) 5 pri NT (v)
Output:
1love2love3love41love2sb3sb4
Features: Remove whitespace, \n,\t, custom
Strip
Example:
1 name = ' hexin \ t ' 2 v = name.strip () # blank, \n,\t3 print (v)
Output:
Hexin
Function: Fill 0
Zfill
Example:
1 name = ' Hexin ' 2 v = Name.zfill (3) print (v)
Output:
000000000000000hexin
2. Integer
class int
Function: The minimum number of digits for the binary representation of the current integer
Bit_length
Example:
Age = 4 # 100print (Age.bit_length ())
Output:
3
Function: Gets the byte representation of the current data
to_bytes
Example:
Age = Allv = age.to_bytes (10,byteorder= ' big ') v = age.to_bytes (10,byteorder= ' little ') print (v)
Output:
B ' \x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00 '
3.list List
Class List
Variable type
Function: Append
Append
Example:
User_list = [' Tom ', ' Liu ', ' Jack ', ' n '] # variable type user_list.append (' hex ') print (user_list)
Output
[' Tom ', ' Liu ', ' Jack ', ' n ', ' hex ']
Function: Empty
Example:
User_list = [' Tom ', ' Liu ', ' Jack ', ' n '] # variable type user_list.clear () print (user_list)
Output:
[]
function: Shallow copy
Example:
User_list = [' Tom ', ' Liu ', ' Jack ', ' n '] t = user_list.copy () print(user_list) print (t)
Output:
[' Tom ', ' Liu ', ' Jack ', ' n '[' Tom ', ' Liu ', ' Jack ', ' n ']
Function: Count
Example:
User_list = [' Tom ', ' N ', ' Liu ', ' Jack ', ' n ', ' n ']t = User_list.count (' n ') print (user_list) print (t)
Output:
[' Tom ', ' N ', ' Liu ', ' Jack ', ' n ', ' n ']3
Features: Extending the original list
Example:
User_list = [' Tom ', ' N ', ' Liu ', ' Jack ', ' n ', ' n ']user_list.extend (' 9 ') print (user_list)
Output:
[' Tom ', ' N ', ' Liu ', ' Jack ', ' n ', ' n ', ' 9 ']
Function: Find element index, no error
Example:
User_list = [' Tom ', ' N ', ' Liu ', ' Jack ', ' n ', ' n ']v = User_list.index (' n ') print (v)
Output:
1
Function: Delete and get elements, index
Example:
User_list = [' Tom ', ' N ', ' Liu ', ' Jack ', ' n ', ' n ']v = user_list.pop (1) print (v) print (user_list)
Output:
n[' Tom ', ' Liu ', ' Jack ', ' n ', ' n '
Function: Delete, value
Example:
User_list = [' Tom ', ' N ', ' Liu ', ' Jack ', ' n ', ' n ']user_list.remove (' n ') print (user_list)
Output:
[' Tom ', ' Liu ', ' Jack ', ' n ', ' n ']
Function: Flip
Example:
User_list = [' Tom ', ' N ', ' Liu ', ' Jack ', ' n ', ' n ']user_list.reverse () print (user_list)
Output:
[' n ', ' n ', ' Jack ', ' Liu ', ' N ', ' Tom ']
Function: Sort
Example:
num = [11,2,3,6,111]num.sort () print(num) num.sort (reverse=True) print (num)
Output:
[2, 3, 6, one, 111][111, one, 6, 3, 2]
4.range
py2.7: Generate numbers now
Range (1,11) # Generate 1,23,,4,56.10
Py3: Will not be generated immediately, only the loop iteration, only one generation;
For I in range (1,11): print(i) for J in range (1,11,2): print(j) for K in range (10,0,-1): Print (k)
123456789101357910987654321
Example
# li = [' Eric ', ' Alex ', ' Tony ']
# for I in range (0,len (LI)):
# ele = li[i]
# Print (ele)
Li = [' Eric ', ' Alex ', ' Tony ']
For I in Li:
Print (i)
# for I in range (0,len (LI)):
# print (I+1,li[i])
Output
Ericalextony
5.enumerate
Function: Generate an extra column of sequential numbers
Example
Li = [' Eric ', ' Alex ', ' Tony ']for I,ele in Enumerate (li,1): print(i,ele) #1 eric#2 alex#3 Tony
Li = [' Eric ', ' Alex ', ' Tony ']for I,ele in Enumerate (li,1): print(i,ele) v = input (' Please enter product serial number: ') v = Int (v) item = Li[v-1]print(item) #1 eric#2 alex#3 tony# Please enter the product serial number: 1#eric
6.tuple tuples
Cannot be modified, son cannot be modified, grandson can
User_tuple = (' Hex ', ' Eric ', ' Seven ', ' Hex ')
Function: Get the number
User_tuple = (' Hex ', ' Eric ', ' Seven ', ' Hex ') v = user_tuple.count (' hex ') print (v)
#2
Function: Get the first index position worth
User_tuple = (' Hex ', ' Eric ', ' Seven ', ' Hex ') v = user_tuple.index (' hex ') print(v) #0
- 4) Note: Tuple last comma
Example
Li = (' hx ',) print (LI)
- 5) cannot be modified by itself, but grandson can
User_tuple = (' Alex ', ' Eric ', ' Seven ', [' 1 ', ' 2 ', ' 3 '], ' A4 ')
# User_tuple[0] = 123 execution Error
# User_tuple[3] = [11,22,33] Execution error
USER_TUPLE[3][1] = ' 0 '
Print (User_tuple)
7.dict
Variable type
Function: Empty
DiC = {' K1 ': ' v1 ', ' K2 ': ' V2 '}dic.clear () print (DIC)
function: Shallow copy
DiC = {' K1 ': ' v1 ', ' K2 ': ' v2 '}v = dic.copy () print (v)
Function: Gets the specified value according to key, no error is present
DiC = {' K1 ': ' v1 ', ' K2 ': ' v2 '}v = dic.get (' k1111 ', 1111) print (v)
Function: Delete and get the corresponding value values
# dic = {' K1 ': ' v1 ', ' K2 ': ' v2 '}# v = dic.pop (' k1 ') # print (DIC) # print (v)
Output:
{' K2 ': ' v2 '}v1
function: Random Delete key value pair, and get to delete key value
DiC = {' K1 ': ' v1 ', ' K2 ': ' v2 '}v = dic.popitem () print(DIC) print (v)
Output:
{' K1 ': ' V1 '} (' K2 ', ' v2 ')
DiC = {' K1 ': ' v1 ', ' K2 ': ' v2 '}k,v = Dic.popitem () # (' K2 ', ' v2 ') print(DIC) print (k,v)
Output:
{' K2 ': ' v2 '}K1 v1
DiC = {' K1 ': ' v1 ', ' K2 ': ' v2 '}v = Dic.popitem () # (' K2 ', ' v2 ') print(DIC) print (v[0],v[1])
Output:
{' K1 ': ' v1 '}K2 v2
Function: Add, delete if not present
DiC = {' K1 ': ' v1 ', ' K2 ': ' V2 '}dic.setdefault (' K3 ', ' v3 ') print(DIC) dic.setdefault (' K1 ', ' 1111111 ') Print (DIC)
Output:
{' K2 ': ' v2 ', ' K1 ': ' v1 ', ' K3 ': ' V3 '} {' K2 ': ' v2 ', ' K1 ': ' v1 ', ' K3 ': ' V3 '}
Features: Batch additions or modifications
DiC = {' K1 ': ' v1 ', ' K2 ': ' V2 '}dic.update ({' K3 ': ' V3 ', ' K1 ': ' V24 '}) print (DIC)
Output:
{' K1 ': ' v24 ', ' K2 ': ' v2 ', ' K3 ': ' V3 '}
Function: Creates a new dictionary from the sequence key and value set to value.
Example:
DIC = Dict.fromkeys ([' K1 ', ' K2 ', ' K3 '],123) dic[' k1 '] = ' asdfjasldkf ' Print (DIC)
Output:
{' K2 ': 123, ' K1 ': ' asdfjasldkf ', ' K3 ': 123}
8.set
collection, non-repeating list, mutable type.
S1 = {"Alex", ' Eric ', ' Tony '}print(Type (S1)) print (S1)
Output:
<class ' Set ' >{' Alex ', ' Eric ', ' Tony '}
Function: Presence in output S1, value not present in S2
S1 = {"Alex", ' Eric ', ' Tony ', ' II '}s2 = {"Alex", ' Eric ', ' Tony ', ' hexin '}v = s1.difference (s2) print (v)
Output:
{' II '}
Function: exists in S1, S2 does not exist, then S1 is emptied and then re-assigned
S1 = {"Alex", ' Eric ', ' Tony ', ' II '}s2 = {"Alex", ' Eric ', ' Tony ', ' Hexin '}s1.difference_update (S2) print (S1)
Output:
{' II '}
Function: exists in S1, value not present in S2 and S2, value not present in S1
S1 = {"Alex", ' Eric ', ' Tony ', ' II '}s2 = {"Alex", ' Eric ', ' Tony ', ' hexin '}v = s1.symmetric_difference (s2) print ( V)
{' II ', ' hexin '}
Function: Intersection
S1 = {"Alex", ' Eric ', ' Tony ', ' II '}s2 = {"Alex", ' Eric ', ' Tony ', ' hexin '}v = s1.intersection (s2) print (v)
Output:
{' Eric ', ' Alex ', ' Tony '}
Features: and set
S1 = {"Alex", ' Eric ', ' Tony ', ' II '}s2 = {"Alex", ' Eric ', ' Tony ', ' hexin '}v = s1.union (s2) print (v)
Output:
{' Alex ', ' Hexin ', ' Eric ', ' II ', ' Tony '}
Function: Remove
S1 = {"Alex", ' Eric ', ' Tony ', ' II '}s2 = {"Alex", ' Eric ', ' Tony ', ' Hexin '}s1.discard (' Alex ') print (S1)
Output:
{' Eric ', ' Tony ', ' II '}
Features: Adding
S1 = {"Alex", ' Eric ', ' Tony ', ' Li Quan ', ' Li Quan '}s1.update ({' Alex ', ' 123123 ', ' FFF '}) print (S1)
Output:
{' FFF ', ' Li Quan ', ' 123123 ', ' Tony ', ' Alex ', ' Eric ', ' Li Quan 11 '}
9. Boolean Values
A = Trueb = Flase
The number conversion only 0 is flase, the string only "" is Flasev = bool () print (v) Two, the basic operation
1. Arithmetic operations:
Note: The order in which operators are executed, from the previous
2. Comparison operation:
3. Assignment operation:
4. Logic operation:
5. Member Operations:
"Basic operations of basic data types for Python"