# python Basic data types
# 1. int integer
# 2.str Character string.Doesn't save a lot of data with strings
# 3.bool Boolean value. True, False
# 4.list list(A key) Store large amounts of data
# 5.dict Dictionaries key: value Finding will be faster
# 6.set Set disorder cannot be repeated
# 7.bytes A bunch of bytes. The smallest unit of processing in our program
# 8.tuple Tuple immutable list
1. Integer (int)
In Python 3, all integers are int type. But in Python 2, if the data is compared, the long type will be made. In Python 3, there is no long type.
The operation of int, xxx.bit_length(), can be calculated, returning the binary length
1 a=8 2 print(a.bit_length()) # .bit_length() Returns the binary length
2. Boolean value (bool)
Only True, False. bool values have no operation.
Transformation:
1 # bool-int False - 0 True - 1 2 # int - bool 0 - False Nonzero - True Such as: 3 x = False 4 print(int(x)) #Output is 0. 5 6 while 1: 7 print("Circular circulation") 8 # str - bool The empty string is False It's not empty. True
3. String (str)
In python, the contents caused by the use of','','', and''are called characters.
3.1. Indexes. Indexes are subscripts. Remember, subscripts start at 0.
1 # Indexes 2 # 0 1 2 3 4 5 6 7 8 9 10 11 12 Count from 0 3 # Life is short. , I use p y t h o n 4 s = "Life is short, I use it. python" 5 print(s[10]) 6 print(s[12]) 7 print(s[-7]) # Reciprocal 8 print(s[-1]) 9 print(s[-14]) # No 14 out of range will report errors 10 print(s[14]) # Out of range will report errors
3.2. Slice. Get a new string from a string
Grammar: str[start: end] rule: regardless of the baht, intercept from start. Intercept to end position. But exclude end.
1 # s1 = "Life is short, I use it. python" 2 # print(s1[0:3]) # Grammar print(str[start:end]) ignores the baht, so there is no s1[3] 3 # print(s1[3:5]) 4 # print(s1[7:13]) # The last one is s1[12], the last one is s1[13] 5 # print(s1[8:14]) # Out of range output ython equals maximum 6 # print(s1[:]) # There is no limit from beginning to end. 7 # print(s1[1:]) # From s1[1] to the end 8 # print(s1[:5]) # From start to s1[5] 9 10 # s2 = s1[3] + s1[4] + s1[5] + s1[6] + s1[7] 11 # print(s2) 12 # print(s1[3:-1]) # You can't get the last one regardless of the baht. 13 # print(s1[:-8]) # From the beginning to the eighth penultimate 14 # print(s1[-8:-1]) # The eighth from the bottom gets the first from the bottom but not the first from the bottom. 15 # print(s1[:-1]) # Similarly sliced to reciprocal 1 but not shown 16 # print(s1[-5:]) #tctl eof
Jump interception:
1 # Jump interception 2 # s[start: end: step] step Step size is positive from left to right. If it's negative, it's right to left. 3 s3 = "RNG Fucking great, RNG Fucking great" 4 print(s3[::2]) # Get it every two times from beginning to end RG force RG Force (note that there is no limit here to the end of the force) 5 print(s3[3:9:2]) # Take a cow from three to nine at intervals of two. N 6 print(s3[-5::-2]) # Take 1 from 5 upside down to 2 upside down at the beginning. R force GR 7 print(s3[-1:-9:-1]) #Take one bull from the bottom one to the bottom nine from the bottom one GNR,Forcing cattle G)
4. String related operations
Note that strings are immutable objects, so any operator strings will have no impact!
4.1. Case-to-case conversion
Grammar: xxx.capitalize() capital letters, such as:
1 s = "rng cattle b" 2 # s.capitalize() 3 # print(s) #No change needs to be retrieved 4 # s1 = s.capitalize() 5 # print(s1) #Capital initials
xxx.lower() converts strings to lowercase.
xxx.upper() converts strings to uppercase.
1 s = "rng cattle B" 2 # toggle case 3 s2 = s.lower() # xxx.lower() Full conversion to lowercase 4 print(s2) 5 6 s3 = s.upper() # xxx.upper() Full conversion to uppercase 7 print(s3)
1 while 1: 2 content = input ("Please enter what you want to spray, enter exit:") 3 if content.upper()=="EXIT": 4 break 5 print(content)
xxx.swapcase() case-to-case conversion
1 s4 = s.swapcase() # Case-to-case conversion 2 print(s4)
Another conversion to lowercase
1 a = "Life is short, I use it. Python!" 2 a1 = a.casefold() # Convert to lowercase lower Distinguish between:lower()Not good enough support for some characters. 3 # casefold()Effective for all characters. For example, some words in Eastern Europe 4 print(a1) 5 6 a3 = "БBß" # Russian virtue 7 print(a3) 8 print(a3.lower()) # бbß 9 print(a3.casefold()) # бbss
Capital letters of words separated by special characters
1 # Capital letters for each word separated by special characters 2 s = "chinese China china" # Chinese is also a special character 3 print(s.title()) # Chinese China China 4 5 s1 = "i$can,i&up " 6 print(s1.title()) # I$Can,I&Up