Initial knowledge of basic types
2.1 Numeric int.
The number is mainly used for computing, not many ways to use, just remember one can:
2.2 String str stores a small amount of data
' Alex ', ' 1235443543 '
' [A.] '
3.3 Boolean bool
True False
4.4 List Lists various data types of data, a large amount of data, easy to operate.
[' name ', true,[] .....],
5.5 Yuan zu tupe Read-only list
Use () to denote (a)
6.6 Dictionary Dict stores large amounts of data, relational data. Store data in the form of key-value pairs (key-value)
{' name ': ' Old boy ',
' Name_list ': [' negative ',.....]
' Alex ': {' age ': 40,
' Hobby ': ' Old_women ',
}
},
7.7 Set
{' Wusir ', ' Alex ', ...}
The specific usage
Int
#i = 100#print (I.bit_length ()) Decimal to the number of significant digits converted to binary 1 0000 0001 2 0000 0010 3 0000 0011 100
BOOL
int <---> str mutual conversion
str---> int int (str)Condition: The string must consist entirely of numbers. # age = Int (input (' >>> ')) # Print (Age,type (age))
int---> str str (int)# S1 = str (123) # s2 = 123 # Print (S1, type (S1)) # Print (S2, type (s2))
BOOL <---> int
BOOL---> intTrue--->1 False---> 0# print (int (True)) # Print (int (False))
int---> BOOLNonzero is true and 0 is false. # print (bool) # print (bool ( -1)) # print (bool (0))
Str
i = ' python12 period '
IndexStarting from 0, the calculation is sequentially 0. 1.2. .... From right to left is-1-2-3 .....
# i1 = i[0] # print (I1) # i2 = i[4] # print (I2) # i3 =i[-1] # print (i3)
slices
(Gu Tou Disregard butt)
# i4 = I[0:6] # print (i4) # i5 = i[:6] # Print (i5) python# I6 = i[1:4]# print (I6) Yth# i7 =i[5:]# print (i7) N12 period # i8 = i[:]# print (i8) Python12 period
Step Size
# I9 = i[:5:2] front empty is the beginning to 5 steps for 2# print (I9) PTO# i10 = i[4::2] Intermediate empty is the end 4 to end Step 2# print (i10) O1 period # i11 = i[-1:-5:-1] from back to forward step 1 # print (I11) period 21n# i12 = i[-3:-1] slice from 3 to 2 (Gu Tou regardless of tail)# print ( I12)
common ways to manipulate strings
s = ' Laonanhai '
* * Capitalize first letter uppercase, the remaining letters lowercase.
# S1 = s.capitalize () # print (S1) # Laonanhai
* Center Center. The first parameter determines the size, * indicates padding left and right parts
# s2 = s.center ("*") # print (s2) #***laonanhai***
* * * Upper full capitalizationLower Full lowercase
# s3 = S.upper () # s31 er () # Print (S3) Laonanhai# print (s31) Laonanhai
PS
# code = ' Qadr '. Upper () # Your_code = input (' Please enter a verification code, not case sensitive: '). Upper () # if Your_code = = Code: # print (' Verification successful ')
StartsWith: Determine what content starts with, return bool, can slice, slice, separate
EndsWith:
StartsWith (self, prefix, Start=none, End=none)
# S4 = S.startswith (' l ') # s41 = S.startswith (' Lao ') # s42 = S.startswith (' N ', 3, 6) # print (S42) True# print (S4) true# print (s41) False
* Swapcase: Case Flip
# S5 = s.swapcase () # Print (S5)
* Title: Capitalize the first letter of each word not separated by letter
# ss = ' gdsj Wusir6taibai*ritian ' # s6 = Ss.title () # Print (S6)
* * * Index: Indexed by the element, can be sliced, unable to find an error. * * * Find: Indexed by element, can be sliced, cannot find return-1
# s7 = S.find (' a ') # s71 = s.find (' A ', 2,) 2 find # s71 = s.find (' Q ',)# print (s71)#< from the second start /c9> s72 = S.index (' Q ')# print (s72)# print (s71)# ss = ' \talex\n ' # Print (ss)
* * * Strip remove the front and back space, line break \ n, tab (tab \ t)You can also remove the specified characters before and after (must be in order of occurrence)
# ss = ' Ablabsexsba ' # S9 = Ss.strip (' a ') # S9 = Ss.strip (' abs ') # Lstrip () remove # Rstrip () starting from the left to remove # print (S9) from the right
PS prevents the user from sliding the input spaces or other whitespace characters
Username = input (' Please enter user name '). Strip ()if' Bowl rong ' : Print (' login successful ')
* * * Split str---> Listseparated by a space by default
# s = ' wusir Alex Taibai ' # st = ' Wusir,alex,taibai ' # st1 = ' Qwusirqalexqtaibai ' # s10 = S.split () # print (S10) # s101 = St.split (', ') # print (s101) # s102 = st1.split (' Q ', 2) 2 represents the first two Q to split # print (s102)
Join in some cases, List---> str
# s = ' Alex ' # S11 = ' + '. Join (s) # print (S11) # L = [' Wusir ', ' Alex ', ' Taibai '] # s111 = ". Join (L) # Print (S111,type (s111))
* * Repalce replaces specified characters
Replace (self, old, new, Count=none)
# s = ' small matte pink ghlasdfg little Pink ' # # s12 = S.replace (' Little pink ', ' big Hammer ') # S12 = S.replace (' Little pink ', ' big Hammer ', 2) number 2 means replace only the first two characters # print (s12)
Public methods
' Fdsajlskgjdsaf;jdskfsdaf '
Len () Total number
# print (len (s))
Count (') calculates the number of occurrences of some elements, which can be sliced
# C1 = S.count (' f ') # Print (c1)
Format formatted output
Three different ways
# msg = ' My name {}, this year {}, hobby {} '. Format (' Taibai ', ' ' Girl ') # Print (msg)
# msg = ' I call {0}, this year {1}, hobby {2}, I still call {0} '. Format (' Taibai ', ' ' "', ' Girl ') # Print (msg)
# msg = ' My name {name}, this year {age}, hobby {hobby} '. # format (name= ' Taibai ', hobby= ' girl ', age= ') # Print (msg)
Judgment statement
# name= ' jinxin123 ' # print (Name.isalnum ()) #字符串由字母或数字组成 True# print (Name.isalpha ()) #字符串只由字母组成 False# print (name.isdigit ()) #字符串只由数字组成 False
Ps
Python Learning D3