1. Data type:
Digital (Integer, Long Integer, Floating Point, Complex)
String: Introducing bytes when introducing character encoding
List
Yuan Zu
Dictionary
Set
2. integer int
Function: Integer numbers such as grade/grade/ID number are correlated
Definition: age=10. Essentially age=int(10)
The decimal system is converted to... Binary system Print (bin (13)# Converts integer to binary Print (oct (13)# Converts integer to octal Print (hex (13)# Converts integer to hexadecimal
Common Operations + Built-in Methods
# Save a value # Immutable # x=10 # print(id(x)) # x=11 # print(id(x))
3. Floating-point float
Role: Wage/height/weight floating point correlation
salary=3000.3 #essence salary=float(3000.3)
Type conversion
print(float(10)) print(float(1.1)) print(float('1.1'))
4. String type str
Role: Record the status of descriptive values, such as name/gender, etc.
msg='hello world' #msg=str('hello world')
Type Conversion: Any type can be converted to string type
res1=str(10) res2=str(10.3) res3=str([1,2,3]) res4=str({'x':1}) #res4="{'x':1}"
Common Operations + Built-in Approaches
1. Value by index (forward + reverse): only
msg='hello world' print(type(msg[0])) print(msg[-1]) msg[0]='H'
2. Slices (head without tail, step length)
msg='hello world' print(msg[0]+msg[1]+msg[2]) print(msg[0:5]) print(msg[0:5:2]) print(msg[0:]) print(msg[:]) print(msg[-1:-5:-1]) #-1 -2 -3 -4 print(msg[::-1]) #-1 -2 -3 -4
3. Length len: The number of characters counted
4. Membership operations in and not in: Determine whether a subcharacter exists in a large string
# msg='hello world' # print('ho' in msg) # print('ho' not in msg)
5. Remove the blank strip: Remove some characters on the left and right sides of the string
msg=' hello ' print(msg.strip(' ')) print(msg.strip()) print(msg) name=input('name>>>: ').strip() #name='egon' pwd=input('password>>>: ').strip() if name == 'egon' and pwd == '123': print('login successfull') else: print('username or password error') msg='***h**ello**********' print(msg.strip('*')) msg='*-=+h/ello*(_+__' print(msg.strip('*-=+/(_'))
6. Segmentation split: To cut regular strings into lists to facilitate the selection of values
info='egon:18:180:150' res=info.split(':',1) print(res) print(res[1]) info='egon:18:180:150' res=info.split(':') print(res) s1=res[0]+':'+res[1]+':'+res[2]+':'+res[3] s1='' for item in res: s1+=item print(s1) s1=':'.join(res) print(s1) ':'.join([1,2,3,4,5])
7. cycle
for i in 'hello': print(i)
****** Operations to be mastered
1.strip , lstrip , rstrip
msg='*****hello****' print(msg.strip('*')) print(msg.lstrip('*')) print(msg.rstrip('*'))
2.lower , upper
msg='AaBbCc123123123' print(msg.lower()) print(msg.upper()) #After execution #aabbcc123123123 #AABBCC123123123
3. startswith , endswith
msg='alex is dsb' print(msg.startswith('alex')) print(msg.endswith('sb')) #After execution #True #True
4. Three Plays of Form
msg='my name is %s my age is %s' %('egon',18) print(msg) #After execution #my name is egon my age is 18
msg='my name is {name} my age is {age}'.format(age=18,name='egon') print(msg) #After execution #my name is egon my age is 18
msg='my name is {} my age is {}'.format(18,'egon') msg='my name is {0}{0} my age is {1}{1}{1}'.format(18,'egon') print(msg) #After execution #my name is 1818 my age is egonegonegon
5.split , rsplit
cmd='get|a.txt|33333' print(cmd.split('|',1)) print(cmd.rsplit('|',1)) #After execution #['get', 'a.txt|33333'] #['get|a.txt', '33333']
6. replace
msg='kevin is sb kevin kevin' print(msg.replace('kevin','sb',2)) #sb is sb sb kevin
7. isdigit (True when the string is a pure number)
res='11111' print(res.isdigit()) int(res) #True
age_of_bk=18 inp_age=input('your age: ').strip() if inp_age.isdigit(): inp_age=int(inp_age) #int('asdfasdfadfasdf') if inp_age > 18: print('too big') elif inp_age < 18: print('to small') else: print('you got it') else: print('Pure numbers must be entered')
* * (understand)
1.find, rfind, index, rindex, count
find and index are similar in usage. find is more powerful than index. find is preferred.
print('xxxkevin is sb kevin'.find('kevin')) print('xxxkevin is sb kevin'.index('kevin')) print('xxxkevin is sb kevin'.rfind('kevin')) print('xxxkevin is sb kevin'.rindex('kevin')) #After execution 3 3 15 15
print('kevin is kevin is kevin is sb'.count('kevin')) #After execution # 3
2. center, ljust ,rjust , zfill
print('egon'.center(50,'*')) print('egon'.ljust(50,'*')) print('egon'.rjust(50,'*')) print('egon'.zfill(50)) #After execution ***********************egon*********************** egon********************************************** **********************************************egon 0000000000000000000000000000000000000000000000egon
3. captalize , swapcase , title
print('my name is kevin'.capitalize()) print('AaBbCc'.swapcase()) print('my name is kevin'.title()) #After execution My name is kevin aAbBcC My Name Is Kevin
4.is other
name='egon123' print(name.isalnum()) #Strings consist of letters or numbers print(name.isalpha()) #Strings consist of only letters print(name.islower()) print(name.isupper()) name=' ' print(name.isspace()) msg='I Am Egon' print(msg.istitle()) #After execution True False True False True True