numeric types and String types
The 1.bin () function converts decimal into a binary
The 2.OCT () function converts decimal into octal
The 3.hex () function converts decimal into hexadecimal
Hexadecimal representation: 0-9 a B c d E F
4. Characteristics of numeric types:
Only one value can be stored
Once defined, cannot be changed
Direct access
Categories: integers, booleans, floating-point, plural
5. String type
Quotation marks contain string types
s1= ' Hello world ' s= "Hello World"
s2= "" "Hello World" ""
s3= ' Hello World '
Single-lead double-lead no difference
6. Common Operations for strings
Strip () remove whitespace, or remove other characters
Slipt () split, separated by a space by default. can also be split with other characters
Len () Length slice: such as print (X[1:3]) is also Gu Tou regardless of the tail
Print (X[0:5:2]) #0 2 4
Capitalize () Capitalize first letter
Center () Center Display for example: x= ' Hello ' Print (X.center (30, ' # '))
Count (): Count, Gu Tou regardless of the tail, count the number of characters, the space is also counted as a character
EndsWith () at what end
Satrtswith () Start with what
Find () finds the index position of the character, if it is negative, indicates that the lookup failed
Index () indexes
The difference between find () and index (), such as:
Format () string formatting
1.msg= ' name:{},age:{},sex:{} '
Print (Msg.format (' Haiyan ', 18, female))
2.msg= ' name:{0},age:{1},sex:{0} '
Print (Msg.format (' aaaaaa ', ' bbbbbb '))
3.msg= ' Name:{x},age:{y,sex:{z} '
Print (Msg.format (x= ' Haiyan ', y= ', z= ' women '))
IsDigit () Determine if it is a number
Islower () to determine whether it is all lowercase
Isupper () to determine if it is all uppercase
Lower () convert all to lowercase
Upper () Convert all to uppercase
Isspace () Determine if it is all spaces
Istitle () to determine if it is a title (capitalized in the first letter)
Swapcase () uppercase and lowercase letters flipped
Join () connection
Repalce () Replace
msg= ' Hello Alex '
Print (Msg.replace (' e '), ' A ', 1)
Print (Msg.replace (' e '), ' A ', 2)
Ljust () Align Left
x= ' ABC ' Print (X.ljust (10, ' * '))
Some methods of string formatting and string
1.%s,%d
Example 1:name= ' Egon '
Age=20
Print ("My name is%s my age is%s"% (name,age)) #%s can accept both a string and a number
Print (' My name is%s my age is%d '% (name,age)) #%d can only accept numbers
Example 2: Display of user information
1 while True:2 name=input ("Name:") 3 Age=input ("Age:") 4 sex=input ("Sex:") 5 height=input ("Height:") ) 6 msg= "7 ------------%s Info-----------8 name:%s 9 age:%s10 sex:%s11 height:%s12 ------------------------------ "% (name,name,age,sex,heigth) print (msg)
The results of the operation are as follows:
2. String methods
# name= ' Egon ' #name =str (' Egon ') # print (type (name)) #优先掌握 # # # # Remove white strip# msg= ' Hello ' # print (msg) # Print ( Msg.strip ()) # remove ' * ' # msg= ' ***hello********* ' # msg=msg.strip (' * ') # print (msg) #移除左边的 # Print (Msg.lstrip (' * ')) #移除右边的 # Print (Msg.rstrip (' * ')) #用处while true:name=input (' User: '). Strip () Password=input (' Password: '). Strip () if name = = ' Egon ' and password = = ' 123 ': print (' login successfull ') #切分split # info= ' Root:x:0:0::/root:/bin/bash ' # print (info[0 ]+INFO[1]+INFO[2]+INFO[3] # user_l=info.split (': ') # print (user_l[0]) # msg= ' Hello World Egon say Hahah ' # print ( Msg.split ()) #默认以空格作为分隔符 #cmd= ' download|xhp.mov|3000 ' # cmd_l=cmd.split (' | ') # print (cmd_l[1]) # print (cmd_l[0]) # Print (Cmd.split (' | ', 1)) #用处while true:cmd=input (' >>: '). Strip () If Len (cmd ) = = 0:continue Cmd_l=cmd.split () print (' command is:%s ' parameter is:%s '% (Cmd_l[0],cmd_l[1])) #长度len # Print (len (' Hell 123 ')) #索引 # Cut Slices: Tangent substring # msg= ' Hello World ' # print (Msg[1:3]) #1 (Msg[1:4]) #1 2 3# Mastering part oldboy_age=84While True:age=input (' >>: '). Strip () If Len (age) = = 0:continue If Age.isdigit (): Age=int (AG e) Else:print (' must be int ') #startswith, endswith# name= ' ALEX_SB ' # print (Name.endswith (' SB ')) # print (name.starts With (' Alex ')) #replace # name= ' Alex Say:i has one tesla,my name is Alex ' # Print (Name.replace (' Alex ', ' SB ', 1)) # print (' My NA Me is%s My age was%s my sex is%s '% (' Egon ', ' Male ') # print (' My name was {} my age is {} My sex is {} '. Format (' Egon ', 18 , ' Male ') # print (' My name is {0} my age is ' {1} ' my sex is {0}: {2} '. Format (' Egon ', ' Male ') ') # print (' My name is ' {name} my Age was {age} my sex is {sex} '. Format (# sex= ' Male ', # age=18,# name= ' Egon ') # name= ' goee say hello ' # # print (nam E.find (' s ', 1,3)) #顾头不顾尾, not found then return-1 does not error, found the index # # print (Name.index (' s ')) #同上, but could not find error # # print (Name.count (' s ', 1,5)) # Gu Tou Regardless of the end, if you do not specify a range then find all #join# info= ' Root:x:0:0::/root:/bin/bash ' # print (Info.split (': ')) # l=[' root ', ' x ', ' 0 ', ' 0 ', ', '/ Root ', '/bin/bash ']# print (': '. Join (L)) #lower, upper# name= ' EGon ' # print (Name.lower ()) # Print (Name.upper ()) #了解部分 #expandtabs# name= ' Egon\thello ' # print ( Name) # print (Name.expandtabs (1)) #center, ljust,rjust,zfill# name= ' Egon ' # print (Name.center (+, '-')) # print ( Name.ljust (' * ') # print (Name.rjust (+, ' * ')) # print (Name.zfill) #用0填充 #captalize,swapcase,title# name= ' EGon ' # Print (Name.capitalize ()) #首字母大写, remainder of lowercase # print (name.swapcase ()) #大小写翻转 # msg= ' Egon say hi ' # print (Msg.title ()) # Capitalize the first letter of each word # in Python3 num0= ' 4 ' num1=b ' 4 ' #bytesnum2 =u ' 4 ' #unicode, python3 without adding U is unicodenum3= ' four ' #中文数字num4 = ' Ⅳ ' #罗马数字 # isdigt:str,bytes,unicode# print (Num0.isdigit ()) # Print (Num1.isdigit ()) # Print (Num2.isdigit ()) # Print (Num3.isdigit ( ) # Print (Num4.isdigit ()) #isdecimal: str,unicode# num0= ' 4 ' # num1=b ' 4 ' #bytes # num2=u ' 4 ' #unicode, Python3 no need to add U is unicode# num3= ' four ' #中文数字 # num4= ' Ⅳ ' #罗马数字 # print (Num0.isdecimal ()) # # Print (NUM1.) # Print (Num2.isdecimal ()) # Print (Num3.isdecimal ()) # Print (Num4.isdecimal ()) #isnumeric: Str,unicode, Chinese, Roman # num0= ' 4 ' # Num1=b ' 4 ' #bytes # num2=u ' 4 ' #unicode, Python3 no need to add U is unicode# num3= ' four ' #中文数字 # num4= ' Ⅳ ' #罗马数字 # # print (Num0.isnumeric ()) # # print (NUM1) # Print ( Num2.isnumeric ()) # Print (Num3.isnumeric ()) # Print (Num4.isnumeric ()) #is其他 # name= ' egon123 ' # print (Name.isalnum ()) # String consists of letters and numbers # name= ' Asdfasdfa SDF ' # print (Name.isalpha ()) #字符串只由字母组成 # # name= ' asdfor123 ' # print (Name.isidentifier ()) Name= ' Eggon ' Print (Name.islower ()) # Print (Name.isupper ()) # Print (Name.isspace ()) name= ' Egon say ' Print (Name.istitle ( ))
List
First, List
Role: Multiple equipment, multiple hobbies, multiple courses, multiple girlfriends, etc.
Definition: [] can have multiple values of any type, comma separated
The following are common actions for a list:
1 l=[1,2,3] #l =list ([3]) 2 # Print (Type (l)) 4 #pat1 = = = "Priority" Part 5 # Index: l=[1,2,3,4,5] 6 print (l[0]) 7 # Slices 8 l=[' A ', ' B ', ' C ', ' d ', ' e ', ' F '] 9 # print (L[1:5]) One # print (L[1:5:2]) # print (L[2:5]) # print (l[-1]) 14 1 5 #了解 # Print (l[-1:-4]) # print (l[-4:]) # l=[' A ', ' B ', ' C ', ' d ', ' e ', ' F '] # print (l[-2:]) 21 22 # Append # H obbies=[' play ', ' eat ', ' sleep ', ' study '] # hobbies.append (' Girls ') # print (Hobbies) 26 27 # Delete the hobbies=[' play ', ' E At ', ' Sleep ', ' study '] # X=hobbies.pop (1) #不是单纯的删除, is to delete and return the deleted element, we can use a variable name to receive the return value of # print (x) to # Print (hobbies) 32 # x=hobbies.pop (0) # Print (x) + # X=hobbies.pop # (0) PNS # Print (x) #队列: First-come-out, queue_l=[], #入队 # que Ue_l.append (' first ') # Queue_l.append (' second ') # Queue_l.append (' third ') # print (queue_l) #出队 # print (Queu E_l.pop (0)) # print (Queue_l.pop (0)) # print (Queue_l.pop (0)) #堆栈: Advanced, last in first out, # l=[] # #入栈 # L.appen D (' first ') # L.append(' second ') # L.append (' third ') # # #出栈 # print (l) # Print (L.pop ()) # Print (L.pop ()) # Print (L.pop ()) 63 6 4 #了解 # del hobbies[1] #单纯的删除 # hobbies.remove (' eat ') #单纯的删除, and is the specified element to remove 67 68 69 # Length # hobbies=[' play ', ' eat ' , ' Sleep ', ' study '] # print (len (hobbies)) 72 73 # contains in # hobbies=[' play ', ' eat ', ' sleep ', ' study '] # print (' Sleep ' In hobbies) * msg= ' Hello World Egon ' "# Print (' Egon ' in msg) * * #pat2 = = =" Master part of the hobbies=[' play ', ' eat ', ' Sleep ', ' study ', ' eat ', ' eat '] # Hobbies.insert (1, ' Walk ') # Hobbies.insert (1,[' walk1 ', ' walk2 ', ' Walk3 ']) # print ( Hobbies) Hobbies.count # print (' eat ') # print (Hobbies) # hobbies.extend ([' Walk1 ', ' walk2 ', ' Walk3 ']) # Prin T (Hobbies) hobbies=[' play ', ' eat ', ' sleep ', ' study ', ' eat ', ' eat '] (Hobbies.index (' eat ')) 94-#pat3 = = = " Learn part of the hobbies=[' play ', ' eat ', ' sleep ', ' study ', ' eat ', ' eat '] 98 # hobbies.clear () # Print (Hobbies) # 101 # l=hobbies.co PY () 102 # Print (L) 103 104 # L=[1,2,3,4,5]105 # L.reverse () 106 # Print (l) 107 108 l=[100,9,-2,11,32]109 l.sort (reverse=true) print (L)
Python learns the 2nd article: Python data type