Integer type int
Decimal → default base
Binary → starts with 0b (number 0 and lowercase letter b)
Octal → starts with 0o (digit 0 and lowercase letter O)
Hexadecimal → beginning with 0x (number 0 and lowercase letter X)
Floating point type
Floating point numbers are composed of integer parts and decimal parts. There will be a problem when involving the decimal part. Because the computer uses binary representation, there will be some errors when representing the decimal, resulting in inaccurate storage of floating point numbers
For example:
print(1.1+2.2) #3.30000000003 print(1.1+2.1) #3.2
It can be seen that the result of 1.1 + 2.2 has a minimum error, which can be solved by importing the module decimal
from decimal import Decimal print(Decimal(1.1)+Decimal(2.2)) #3.3
Boolean type
boolean is the abbreviation of boolean and the homonym of bool
True is true and False is False
Unlike other languages, python Boolean types can be converted to integers for calculation, where True can represent 1 and False can represent 0
print(True+1) #2 print(False+1) #1
String type
1, Definition
Strings can be defined by single quotation marks' ', double quotation marks'', three quotation marks' ', or' '' '. The difference is that strings defined by single quotation marks and double quotation marks must be displayed on one line, while strings defined by three quotation marks can be displayed on multiple consecutive lines.
str1 = 'Life is short, I use it python' str2 = "Life is short, I use it python" str3 = '''Life is short, I use python'''
2, Resident mechanism
Strings are also called immutable character sequences (tuples are also immutable sequences). zhe involves the resident mechanism of strings
1. The dwell mechanism is "a method to save only one copy of the same and immutable string". Different values are stored in the dwell pool of the string. Python's dwell mechanism only keeps one copy of the same string. When creating the same string later, it will not open up a new space, but assign the address of the string to the newly created variable
It can be seen that no matter how the same string is created successively, the string in the mainstream pool is assigned to each variable.
a = 'python' b = "python" c = '''python''' print(id(a)) #1874508798960 print(id(b)) #1874508798960 print(id(c)) #1874508798960
2. What kind of string has a resident mechanism? (interactive mode only)
- When the length of the string is 0 or 1 (even with a non identifier)
s1 = '' s2 = '' print(s1 is s2) #True str1 = '%' str2 = '%' print(str1 is str2) #True
- A string that is all symbolic identifiers
s1 = 'abc%' s2 = 'abc%' print(s1 == s2) #True print(s1 is s2) #False str1 = 'abcx' str2 = 'abcx' print(str1 is str2) #True
- Strings reside only at compile time, not at run time
s1 = 'abc' s2 = 'ab+c' s3 = ''.join(['ab' , 'c']) print(s1) #abc print(s2) #abc print(s3) #abc print(type(s1)) #<class 'str'> print(type(s2)) #<class 'str'> print(type(s3)) #<class 'str'> print(s1 is s2) #True print(s1 is s3) #False
The reason why the addresses of s1 and s3 are different here is that the assignment of s1 and the string splicing of s2 are completed before running, while the ". join" method is executed at run time, which will open up a new space to store the value of abc during the running of the program.
- Integer number between [- 5, 256]
s1 = -5 s2 = -5' print(s1 is s2) #True str1 = -6 str2 = -6 print(str1 is str2) #False
3. Force dwell
- Of course, python can use the intern method in sys to force two strings to point to the same object, that is, to force the string to reside, and the two variables to point to the same space.
import sys s1 = 'abc%' s2 = 'abc%' print(s1 is s2) #False s1 = sys.intern(s2) print(s1 is s2) #True
4. Why does the above say that these situations only give interaction mode, so in pycharm? In fact, pycharm optimizes the string, but it does not reside in pycharm
#pycharm environment: import sys s1 = 'abc%' s2 = 'abc%' print(s1 is s2) #True
5. Advantages and disadvantages of string resident mechanism
- When strings with the same value are required, they can be directly used from the string pool to avoid frequent creation and destruction, improve efficiency and save memory. Therefore, splicing strings and modifying strings will affect performance.
- When string splicing is required, it is recommended to use the str type join method instead of +. Because the join() method first calculates the length of all characters and then copies them. The object is only new once, which is more efficient than "+".
3, Common operations of string
1. Inquiry
- Note that the index starts at 0
2. Case conversion
- Note that after case conversion, a new string object will be generated, even if the string content before and after conversion is the same (non resident)
3. Align
- Center alignment fills both sides, left alignment fills right, and right alignment fills left
s = 'Hello,Python' #Twelve characters #If the width is set to 20, the string will be extended to 20 characters, that is, eight more. These eight positions are filled with * to center the string s. print(s.center(20,'*')) #****Hello,Python**** print(s.ljust(20,'*')) #Hello,Python******** print(s.rjust(20,'*')) #********Hello,Python
- It should be noted that when the first string is' + 'or' - ', zfill will put' + 'or' - 'in the first place and use it as an extended character.
print('-8910',zfill(8)) #-0008910
4. Split
- split() and rsplit() differ only when Max split times maxplit is specified.
5. Judge
- Note that when isalpha() and isalnum judge strings, Chinese is also considered as letters
print('Zhang San'.isalpha()) #True print('Zhang san123'.isalnum()) #True
- The difference between isdecimal() and isnumeric()
print('123'.isdecimal()) #True print('123 four'.isdecimal()) #False print('ⅡⅠ'.isdecimal()) #False print('123'.isnumeric()) #True print('123 four'.isnumeric()) #True print('ⅡⅠ'.isnumeric()) #True
6. Replace and merge
7. Compare
- Operators: >, > =, <, < =, = = (compare value),! =, Is (compare id)
- Comparison rule: first compare the first character in the two strings. If they are equal, continue to compare the next character and compare them successively until the characters in the two strings are not equal, the comparison result is the comparison result of the two strings, and all subsequent characters in the two strings will no longer be compared
- Comparison principle: when two characters are compared, their ordinal value (i.e. original value, or Unicode encoding) is compared. The ordinal value of the specified character can be obtained by calling the built-in function ord. The built-in function chr corresponds to the built-in function ord. When calling the built-in function chr, the corresponding characters can be obtained by specifying the ordinal value
8. Slice
- String is an immutable type and does not have operations such as adding, deleting, and modifying
- The slice operation produces a new object
- [start: end: step] is closed on the left and open on the right. Step defaults to 1. If start is not defined, it starts from 0, and if end is not defined, it goes to the last element (- 1) of the string. Step can also be a negative number, such as [:: - 1]. By default, it starts from the last element of the string and ends at the first element of the string
9. Format
String formatting can also be completed by splicing, but splicing takes up a lot of memory, so there is generally formatting.
- Three common formatting methods
name = 'Zhang San' age = 20 #(1) % placeholder% s is a string,% d is an integer, and% f is a floating point number print('My name is%s,this year%d year' % (name, age)) #My name is Zhang San. I'm 20 years old #(2){}.format print('My name is{0},this year{1}Years old, I really call{0}'.format(name, age)) #My name is Zhang San. I'm 20 years old. My real name is Zhang San #(3)f-string print(f'My name is{name},this year{age}year') #My name is Zhang San. I'm 20 years old
- The width and precision can be represented by placeholders or {}
#10 indicates that the width is 10 (8 empty bits in front) print('%10d' % 99) # 10 #. 3 means three decimal places are reserved print('%.3f' % 3.1415926) #3.142 #Both width and precision are represented (5 empty digits in front) print('%10.3f' % 3.1415926) # 3.142
#0 represents the first placeholder and. 3 represents a total of three digits print('{0:.3}'.format(3.1415926)) #3.14 #. 3f indicates that a total of three decimal places are displayed print('{0:.3f}'.format(3.1415926)) #3.142 #10.3f indicates 10 digits in total, of which three digits are decimals (the first five spaces) print('{0:10.3f}'.format(3.1415926)) # 3.142
10. Encoding and decoding
The reason for coding is that the data transmission between computers is based on binary data.
- Encoding: convert string to binary data (bytes)
str.encode(encoding='UTF-8') - Decoding: convert data of bytes type to string type
byte.decode(encoding='UTF-8') - It should be noted that the encoding format used for encoding and decoding should be the same, such as UTF-8 for encoding and UTF-8 for decoding
Data type conversion