# 1. The theory of built-in methods of data types

• What are data types built-in

Built-in methods are functions that come with each data type

General class of data types:
1. Integer 2. Float 3. List 4. Character
5. Character 6. Boolean 7. Tuple 8. Set

The built-in methods of using data types uniformly use the period character (.)

```'rain'.String built-in methods
The variable name of the bind string.String built-in methods
str.String built-in methods
"""
There are many built-in methods of data types. If we want to master it, we don’t need to memorize it by rote, and more often we rely on practice to make perfect.
"""
```

# 2. Built-in methods and operations of integer int

## 1. Type conversion

Type conversion is to convert other data into an integer

int (other data type)

Note: Only floating point type and string can be converted to integer type, floating point type can be converted directly, and the string must be pure numbers inside.

## 2. Binary conversion

```Convert decimal to other bases
print(bin(100))  #bin() converts decimal to binary 0b is the identity of binary 0b1100100
print(oct(100))  #oct() converts decimal to octal 0o is the identification of octal 0o144
print(hex(100))  #hex() converts decimal to hexadecimal 0x is the hexadecimal identifier 0x64
#If there is no preceding identifier, it defaults to decimal
Convert other bases to decimal
print(int(0b1100100))
print(int(0b1100100))
print(int(0b1100100))
or
print(int("0b1100100", 2))
print(int("0o144", 8))
print(int("0x64", 16))

```

ps:python itself has low sensitivity to numbers (low precision), if you need accurate calculations, you need to use the module numpy.... # 3. Floating-point built-in methods and operations

## 1. Type conversion

​ float (other data types)

​ A decimal point can be allowed in the string, and the others must be pure numbers (python itself has low sensitivity to numbers (low precision), if you need accurate calculations, you need to use the module numpy....)

## 2. Special case for boolean values

```print(folat(True)) # 1.0
print(folat(False)) # 0.0
print(int(True))   # 1
print(int(Flase))  # 0
```

When the converted value is a boolean value, the value corresponding to the boolean value is converted (True:1,False:0)

# 4. Built-in methods and operations of string float

## 1. Type conversion

​ str (other data types)

​ Can convert any data type (just add quotes around it)

## 2. The methods that must be mastered

### 1. Index value

```s1 = 'hello word'
print(s1)  # h
print(s1[-1])  # d
"""
1.Start from the starting position 0, and report an error directly beyond the range.
2.Negative numbers are supported, and the printed content is read from right to left
"""
```

### 2. Slicing operation

```s1 = 'hello word'
print(s1[1:5])  # The bones are cut from index 1 to index 4 regardless of the tail
print(s1[-1:-5])  # Since the default order is from left to right, the implemented value cannot be printed
print(s1[-5:-1])  # The default order is left to right worl
```

### 3. Modify the slice direction

```s1 = 'hello world'
print(s1[1:5:1])  # Default is 1 ello
print(s1[1:5:2])  # Default is 1 el
print(s1[-1:5:-1])  # Change direction from right to left dlrow
print(s1[:])  # If you don't write a number, it is the default.
print(s1[2:])  # That is, starting from index 2 and going forward
print(s1[:5])  # Start at index 0 and go to 4
print(s1[::2])  # Take every other one starting at index 0
```

### 4. Count the number of strings

```len:string length
s1 = 'hello world'
print(len(s1))  # 10
```

### 5. Remove the specified character from the first character of the string

```strip:remove
print('Landed successfully')
res = '  jason  '
print(len(res))  # Since there are two spaces at the beginning and the end, the length of the string is 9
print(len(res.strip()))  # Do not write in parentheses Remove leading and trailing spaces by default String length is 5

res1 = '\$\$jason\$\$'
print(res1.strip('\$'))  # Remove the first and last jason by default
print(res1.lstrip('\$'))  # Remove the left (leftstrip) that is the first jason\$\$
print(res1.rstrip('\$'))  # Remove the right aka trailing \$\$jason
```

### 6. Cut the specified characters in the string

```split:cut
print(res.split('|'))  # ['jason', '123', 'read'] The result of this method is a list
name, pwd, hobby = res.split('|')
print(res.split('|', maxsplit=1))  # ['jason', '123|read'] Cut the specified number from left to right maxsplit: the maximum number of cuts
print(res.rsplit('|',maxsplit=1))  # ['jason|123', 'read'] Cut the specified number from right to left

```

### 7. String formatted output

```format How to play 1:Equivalent to placeholder
res = 'my name is {} my age is {}'.format('jason',123)
print(res)  # my name is jason my age is 123

format Play 2:The index takes the value and uses it repeatedly
res = 'my name is {0} my age is {1} {0} {0} {1}'.format('jason',123)
print(res)  # my name is jason my age is 123 jason jason 123

format Play 3:placeholder name
res = 'my name is {name1} my age is {age1} {name1} {age1} {name1} '.format(name1='jason', age1=123)
print(res)

format Play 4:Recommended Use
age = input('age>>>:')
res = f'my name is {name} my age is {age}'
print(res)  # my name is jason my age is 18

```

## 3. Methods that strings need to know

### 1. Case related

```'''upper:convert to uppercase  lower: convert to lowercase'''
res = 'HeLlO wOrlD 123'
print(res.upper())  # HELLO WORLD 123
print(res.lower())  # hello world 123
eg:
'''Image verification code:Generate a verification code without unified capitalization and show it to the user
Get the verification code entered by the user Convert the verification code entered by the user and the verification code originally generated to uppercase or lowercase and then compare
'''
code = '8Ja6Cc'
print('The image verification code displayed to the user', code)
confirm_code = input('please enter verification code').strip()
if confirm_code.upper() == code.upper():
print('The verification code is correct')
res = 'hello world'
print(res.isupper())  # Determines whether the string is pure uppercase False
print(res.islower())  # Check if the string is pure lowercase True
```

### 2. Determine whether the string is a pure number

```isdight:determine whether it is a number
res = ''
print(res.isdight())
guess_age = input('guess_age>>>:').strip
if guess_age.isdight():
guess_age = int(guess_age)
else:
print('Age is a number!')
```

### 3. Replace the content specified in the string

```replace:replace
res = 'my name is jason jason jason'
print(res.replace('jason', 'tony'))  # my name is tony tony tony
print(res.replace('jason', 'tony', 1))  # my name is tony jason jason replace the specified number of contents from left to right

```

### 4. String concatenation

```l1 = 'hello'
l2 = 'world'
print(l1 + '\$\$' + l2)  # hello\$\$world
print(l1 * 3)  # hellohellohello
print('|'.join(['jason', 123]))  # Error The data value involved in splicing must be a string
```

### 5. Count the number of occurrences of the specified character

```res = 'hello world'
print(res.count('l'))  # 3
```

### 6. Determine the beginning or end of a string

```starswith:beginning  endswith:end
res = 'jason say hello'
print(res.startswith('jason'))  # True
print(res.startswith('j'))  # True
print(res.startswith('jas'))  # True
print(res.startswith('a'))  # False
print(res.startswith('son'))  # False
print(res.startswith('say'))  # False
print(res.endswith('o'))  # True
print(res.endswith('llo'))  # True
print(res.endswith('hello'))  # True
```

### 7. Other ways to supplement

```res = 'helLO wORld hELlo worLD'
print(res.title())  # Hello World Hello World Capitalize the first letter of each word
print(res.capitalize())  # Hello world hello world capitalize the first letter of the first word in the string
print(res.swapcase())  # HELlo WorLD HelLO WORld case conversion
print(res.index('O'))  # The meaning of the index, the disadvantage is that it will only find the first match from left to right
print(res.find('O'))  # 4
print(res.index('c'))  # Can't find direct error
print(res.find('c'))  # cannot find default return -1
print(res.find('LO'))  # 3
``` # 5. Built-in methods and operations of list list

## 1. Type conversion

```list(other data types)
ps:can be for The data type of the loop can be converted into a list
print(list('hello'))
print(list({'name': 'jason', 'pwd': 123}))
print(list((1, 2, 3, 4)))
print(list({1, 2, 3, 4, 5}))
```

## 2. Methods to be mastered

### 1. Index value

```11 = [111, 222, 333, 444, 555, 666, 777, 888]
print(l1)  # 111
print(l1[-1])  # 888 supports negative numbers
```

### 2. Slicing operation (consistent with string explanation operation)

```l1 = [111, 222, 333, 444, 555, 666, 777, 888]
print(l1[0:5])  # [111, 222, 333, 444, 555] Bone regardless of tail Cut from index 1 to index 4
print(l1[:])  # [111, 222, 333, 444, 555, 666, 777, 888] Use all numbers without writing them
```

3. Interval number and direction (consistent with the string explanation operation)

```l1 = [111, 222, 333, 444, 555, 666, 777, 888]
print(l1[::-1])  # [888, 777, 666, 555, 444, 333, 222, 111]
```

### 4. Count the number of data in the list

```l1 = [111, 222, 333, 444, 555, 666, 777, 888]
print(len(l1))  #8
```

5. Data value modification

```l1 = [111, 222, 333, 444, 555, 666, 777, 888]
l1 = 123
print(l1)  # [123, 222, 333, 444, 555, 666, 777, 888]
```

### 6. Add data values ​​to the list

Method 1: Append data value at the end

```l1.append('cooked rice')
print(l1)  # [111, 222, 333, 444, 555, 666, 777, 888, 'dry rice']
l1.append(['jason', 'kevin', 'jerry'])
print(l1)  # [111, 222, 333, 444, 555, 666, 777, 888, ['jason', 'kevin', 'jerry']]
```

2. Method 2: Insert data value at any position

```l1.insert(0, 'jason')
print(l1)
l1.insert(1, [11, 22, 33, 44])
print(l1)  # [111, [11, 22, 33, 44], 222, 333, 444, 555, 666, 777, 888]
```

3. Method 3: Expand the list and merge the list

```ll1 = [11, 22, 33]
ll2 = [44, 55, 66]
print(ll1 + ll2)  # [11, 22, 33, 44, 55, 66]
ll1.extend(ll2)  # for loop + append
print(ll1)  # [11, 22, 33, 44, 55, 66]
for i in ll2:
ll1.append(i)
print(ll1)
```

### 7. Delete list data

```l1 = [111, 222, 333, 444, 555, 666, 777, 888]
way 1:Generic delete keyword del
del l1
print(l1)

way 2:remove
l1.remove(444)  # Fill in data values ​​in parentheses
print(l1)

way 3:pop
l1.pop(3)  # Fill in the index value in parentheses
print(l1)
l1.pop()  # Default tail pop data value
print(l1)
res = l1.pop(3)
print(res)  # 444
res1 = l1.remove(444)
print(res1)  # None
```

### 8. Sort

```ss = [54, 99, 55, 76, 12, 43, 76, 88, 99, 100, 33]
ss.sort()  # The default is ascending order
print(ss)  # [12, 33, 43, 54, 55, 76, 76, 88, 99, 99, 100]
ss.sort(reverse=True)
print(ss)  # Change to descending order [12, 33, 43, 54, 55, 76, 76, 88, 99, 99, 100]
```

### 9. Count the number of occurrences of a value in a list

```l1 = [111, 222, 333, 444, 555, 666, 777, 888]
print(l1.count(111))  # 1
```

### 10. Reverse list order

```l1 = [111, 222, 333, 444, 555, 666, 777, 888]
l1.reverse()
print(l1)  # [888, 777, 666, 555, 444, 333, 222, 111]
``` # 6. Mutable and Immutable Types

```s1 = '\$\$jason\$\$'
l1 = [11, 22, 33]
s1.strip('\$')
print(s1)  # \$\$jason\$\$
'''The string does not modify itself after calling the built-in method, but produces a new result
How to check whether there is a new result after calling a method? You can add variable names and assignment symbols to the left side of the code that calls the method
res = s1.strip('\$')
'''
ret = l1.append(44)
print(l1)  # [11, 22, 33, 44]
print(ret)  # None
'''The list modifies itself after calling the built-in method and does not produce a new result'''

mutable type:The value changes, the memory address does not change
l1 = [11, 22, 33]
print(l1)
print(id(l1))
l1.append(44)
print(l1)
print(id(l1))

immutable type:The value changes, the memory address must change
res = '\$\$hello world\$\$'
print(res)
print(id(res))
res.strip('\$')
print(res)
print(id(res))
```

Tags: Python

Posted by XaeroDegreaz on Thu, 29 Sep 2022 15:32:28 +0530