# Some common built-in methods and operations in Python # Python common built-in methods and operations

## integer int

1. type conversion

int (other data types), but only numeric and decimal types are supported

```>>> num1 = input('Your age>>>:')
>>> print(type(num1))
<class 'str'>  # You can see that the input type is a string type
>>> num1 = int(num1)
>>> print(type(num1))
<class 'int'>  # You can see the converted type
```

```>>> print(bin(100))  # convert to binary
0b1100100  # Converted binary data value
>>> print(oct(100))  # convert to octal
0o144  # Converted octal data value
>>> print(hex(100))  # Convert to hexadecimal
0x64  # Converted hexadecimal data value
```

Convert 2, 8, and hexadecimal numbers to decimal

```>>> print(int(0b1100100))
100
>>> print(int(0o144))
100
>>> print(int(0x64))
100
```

If the converted content has quotation marks, you need to tell the interpreter the base number

```>>> print(int('0b1100100'))# Direct output after adding quotation marks will report an error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '0b1100100'

# Tell the interpreter your base number can be output
>>> print(int('0b1100100',2))
100
>>> print(int('0o144',8))
100
>>> print(int('0x64',16))
100
```

## floating point float

1. type conversion

float (other data types), only pure numbers can appear in the converted content, plus a decimal point.

```>>> num1 = 10
>>> print(type(num1))
<class 'int'>  # data shaping as int
>>> num1 = float(num1)  # convert data type
>>> print(type(num1))
<class 'float'>  # The data type is float
>>> print(num1)
10.0  # You can see that the converted result is added with decimals
```

# string str

1. type conversion

str (other data types), can convert any data type, just add quotation marks before and after the data value.

```>>> num1 = 10
>>> print(type(num1))
<class 'int'>
>>> print(num1)
10
>>> num1 = '10'
>>> print(type(num1))
<class 'str'>	# The data type is str
>>> print(num1)
10	# Although the value is also 10, but the data type is str, it cannot be operated at this time.

>>> num2 = 5	# define a num2
>>> print(num1 - num2)	# Subtract two values, and the value of num1 at this time is 10 of type str
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'str' and 'int'  #Report an error directly
```
1. Operation and method

index value

Get the corresponding data value with an index value of 1

```>>> s1 = 'abcde'
>>> print(s1)
b
```

Get the first last data value

```>>> s1 = 'abcde'
>>> print(s1[-1])
e
```

fetch all data values

```>>> s1 = 'abcde'
>>> print(s1[:])
abcde
>>> print(s1[::])
abcde
```

slice operation

Note that the slicing operation takes values ​​from head to tail, and the default order is to take values ​​from left to right.

Get the data value corresponding to index 1 to 5 (not including 5)

```>>> s1 = 'abcde'
>>> print(s1[1:5])
bcde
```

Starting from the 5th last value, take the 2nd last value

```>>> s1 = 'abcde'
>>> print(s1[-5:-1])
abcd
```

Output all values ​​starting from index 2

```>>> s1 = 'abcde'
>>> print(s1[2:])
cde
```

Output starts at index 0 and ends at index 4

```>>> s1 = 'abcdefghijk'
>>> print(s1[:5])
abcde
```

Output starts from index 0, and then outputs data every other index (because the last value defaults to 1, so 2 just increases by 1, so it outputs every other index)

```>>> s1 = 'abcdefghijk'
>>> print(s1[::2])
acegik
```

Modify slice direction and interval value

After outputting the value of index 1 first, output a value every 2 index values.

```>>> s1 = 'abcdefghijk'
>>> print(s1[1:5:2])
bd
```

The normal value is from left to right, when the last key value is changed to -1, the value order becomes from right to left

```>>> s1 = 'abcdefghijk'
>>> print(s1[-1:-5:-1])
kjih
```

Count the number of strings

Corresponding method: len()

```>>> s1 = 'abcdefghijk'
>>> print(len(s1))
11
```

Remove the specified characters at the beginning and end of the string

Corresponding method: strip()

Usage scenario: When users enter information such as user names and verification codes, it is easy to enter more spaces. At this time, the spaces need to be removed to make the user experience better.

```username = input('username >>>:').strip()
# After using strip, if you enter 'abc' when entering the user name, it will also determine success.
print('success!!!')
```

If you want to specify to remove the specified characters at the beginning and end, you can also

```>>> '\$\$abc\$\$'.strip('\$')	# Specify the string to be removed as '\$'
'abc'
```

If you only want to remove the string at the beginning or the end of one of the places, you can use

Remove the beginning, that is, the specified string on the left: lstrip (the first letter l is actually the initial letter of left)

Remove the end, which is the specified string on the right: rstrip (the first letter r is actually the initial letter of right)

```>>> '\$\$abc\$\$'.lstrip('\$')
'abc\$\$'
>>> '\$\$abc\$\$'.rstrip('\$')
'\$\$abc'
```

Cut the specified characters in the string

Cutting strings: split()

The cut strings are displayed in the form of a list

```>>> 'jack,18,teacher'.split(',')
['jack', '18', 'teacher']
# Call this method with a variable
>>> s1 = 'jack,18,teacher'
>>> s1.split(',')
['jack', '18', 'teacher']
```

Variables can also be specified for individual data values

```>>> s1 = 'jack,18,teacher'
>>> name, age, job = s1.split(',')
>>> print(name, age, job)
jack 18 teacher
```

Specify the number of split objects, use the maxsplit parameter

```>>> s1 = 'jack,18,teacher'
>>> print(s1.split(',',maxsplit=1))
['jack', '18,teacher']	# It can be seen that it was cut into two parts
```

Specify the number of cutting objects and perform reverse cutting: rsplit()

```>>> s1 = 'jack,18,teacher'
>>> print(s1.rsplit(',',maxsplit=1))
['jack,18', 'teacher']
```

String formatted output

Formatted output: format()

The first method of use: used as a placeholder, similar to %s

```>>> res = 'The IP address is {}, Port is {}'.format('1.1.1.1','3389')
>>> print(res)
The IP address is 1.1.1.1, Port is 3389
```

The second type, according to the index value, can be used repeatedly

```>>> res = 'The IP address is {0}, Port is {1}, The{0} cannot conmunicate!'.format('1.1.1.1','3389')
>>> print(res)
The IP address is 1.1.1.1, Port is 3389, The1.1.1.1 cannot conmunicate!
```

The third type is similar to calling in the form of a dictionary, see the name and know the meaning

```>>> res = 'The IP address is {addr}, Port is {port}, The{addr} cannot conmunicate!'.format(addr='1.1.1.1',port='3389')
>>> print(res)
The IP address is 1.1.1.1, Port is 3389, The1.1.1.1 cannot conmunicate!
```

Fourth, specify the variable first, and then call it

```>>> name = input('username >>>:')	# Use with input to directly assign values ​​to variables
>>> age = input('age >>>:')	# Enter user age
age >>>:18	# age 18
>>> res = f'my name is {name},age is {age}' # Add an f at the beginning, and use quotation marks to call the content
>>> print(res)
my name is abcde,age is 18  # You can see that the call was successful
```

Additional knowledge, you can let the variable fill in the gaps to achieve the effect of alignment. When calling the variable value, add X:>N to indent N characters to the right and fill it with X. If you do not enter X, spaces will be used by default. If you add X:<N, it means that the N characters on the left are filled with X.

```# It can be seen that the ages of the output results are not aligned. Sometimes when you want to make the output look better, you can define the indentation value.
>>> jack_age = 29
>>> jason_age = 18
>>> john_green_age = 15
>>> print(f'''jack is {jack_age} years old,
... jason is {jason_age} years old
... john green is {john_green_age} years old''')
jack is 29 years old,
jason is 18 years old
john green is 15 years old

# Do not specify character padding
>>> jack_age = 29
>>> jason_age = 18
>>> john_green_age = 15
>>> print(f'''jack is {jack_age:>8} years old,
... jason is {jason_age:>7} years old,
... john green is {john_green_age:>2} years old.''')
jack is 29 years old,		# The left side of the variable value is filled with spaces
jason is18 years old,
john green is 15 years old.

# You can also specify character completion
>>> print(f'''jack is {jack_age:\$<8} years old,
... jason is {jason_age:\$<7} years old,
... john green is {john_green_age:\$<2} years old.''')
jack is 29\$\$\$\$\$\$ years old,		# The right side of the variable value is filled with the specified \$
jason is 18\$\$\$\$\$ years old,
john green is 15 years old.
```

case dependent

Change all the letters in the string to uppercase: upper()

Change all the letters in the string to lowercase: lower()

Usage scenario: When the user enters a verification code and other scenarios, case can be ignored to make the user experience better.

```>>> s1 = 'aAbBcC2333'
>>> s1.upper()
'AABBCC2333'
>>> s1.lower()
'aabbcc2333'
```

Determine whether a string is a pure number

How to use: isdigit()

```>>> s1 = 'aAbBcC2333'
>>> s1.isdigit()
False
>>> s2 = '123456'
>>> s2.isdigit()
True
>>> s3 = '123.456'	# Note that the decimal point inside the quotation marks is also a string, so it is False
>>> s3.isdigit()
False
```

replaces what is specified in the string

```>>> s1 = 'aAbBcC2333'
>>> s1.replace('a','G')
'GAbBcC2333'	# replace a with G and output
>>> print(s1)
aAbBcC2333		# Note that the original value has not changed, if you want to change, you need to reassign
```

concatenation of strings

Use print splicing

```>>> s1 = 'Hello'
>>> s2 = 'World'
>>> print(s1 + s2)
HelloWorld
>>> print(s1 * 5)	# Also supports multiplication, multiple output
HelloHelloHelloHelloHello
```

Use join splicing

```>>> print('|'.join(['Hello','World','Welcome']))
Hello|World|Welcome

# Note that the splicing content must use the str type
>>> print('|'.join(['Hello','World',12]))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sequence item 2: expected str instance, int found
```

Count the number of occurrences of a specified character

How to use: count()

```>>> s1 = 'aaaWorld'
>>> s1.count('a')
3
```

Determine the beginning or end of a string

Judge the beginning: startswith

Judging the end: endswith

```>>> s1 = 'aaaWorld'
>>> s1.startswith('a')
True	# true if successful
>>> s1.startswith('b')
False	# If the judgment fails, it is false
>>> s1.endswith('d')
True
>>> s1.endswith('l')
False
```

Capitalize the first letter and lowercase the rest

Method: title()

```>>> s1 = 'hello world'
>>> s1.title()
'Hello World'
>>> s1 = 'helloworld'
>>> s1.title()
'Helloworld'	# Pay attention to the use of spaces, punctuation, etc. for judgment
>>> s1 = 'hello world,welcome you'
>>> s1.title()
'Hello World,Welcome You'
```

Capitalize the first letter of the first word in a sentence and lowercase the rest

Method: capitalize()

```>>> s1 = 'hello world,welcome you'
>>> s1.capitalize()
'Hello world,welcome you'
```

case conversion

```>>> s1 = 'aAbBcCdD123'
>>> s1.swapcase()
'AaBbCcDd123'
```

Find the first few characters of the keyword (note that it starts from 0)

Method 1: index (if there is no value, an error will be reported)

Method 2: find (returns -1 if there is no value)

```>>> s1 = 'cccabacd'
>>> s1.index('a')	# The data value is at index 3
3
>>> s1.find('a')	# The data value is at index 3
3
>>> s1.index('e')	# There is no e in the data value, and an error is reported
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
>>> s1.find('e')	# If there is no e in the data value, return -1
-1
```

# list type

1. type conversion

list (other data types), generally speaking, anything that can be looped by for can be converted into a list.

1. Operation and method

list index value

```>>> l1 = [1, 2, 3]
>>> print(l1)
1
```

list slice operation

```>>> l1 = ['a', 'b', 'c', 'd', 'e', 'f']
>>> print(l1[0:5])
['a', 'b', 'c', 'd', 'e']
>>> print(l1[-5:-1])
['b', 'c', 'd', 'e']
```

Interval value between lists

```>>> l1 = ['a', 'b', 'c', 'd', 'e', 'f']
>>> print(l1[0:5:2])
['a', 'c', 'e']
```

Reverse value of list

```>>> l1 = ['a', 'b', 'c', 'd', 'e', 'f']
>>> print(l1[::-1])
['f', 'e', 'd', 'c', 'b', 'a']
>>> print(l1[-1:-3:-1])
['f', 'e']
```

The number of data values ​​in the statistics list of the list

How to use: len()

```>>> l1 = ['a', 'b', 'c', 'd', 'e', 'f']
>>> print(len(l1))
6
```

Modify the value in the list of the list

```>>> l1 = ['a', 'b', 'c', 'd', 'e', 'f']
>>> l1 = '666'
>>> print(l1)
['666', 'b', 'c', 'd', 'e', 'f']
```

The list adds additional data values ​​​​at the end of the list

```>>> l1 = ['a', 'b', 'c', 'd', 'e', 'f']
>>> l1.append('666')
>>> print(l1)
['a', 'b', 'c', 'd', 'e', 'f', '666']
```

Add a new data value anywhere in the list of the list

```>>> l1 = ['a', 'b', 'c', 'd', 'e', 'f']
>>> l1.insert(3,'new')  # The new character is added at the position of index 3, and the original data index value is extended.
>>> print(l1)
['a', 'b', 'c', 'new', 'd', 'e', 'f']
```

Extended List or Combined List of Lists

```>>> l1 = [1, 2, 3]
>>> l2 = [3, 4, 5]
>>> print(l1 + l2)  # Just print, do not modify the original data value
[1, 2, 3, 3, 4, 5]
>>> l1.extend(l2)	# Append the data in l2 to the l1 list
>>> print(l1)
[1, 2, 3, 3, 4, 5]
>>> print(l2)		# list l2 value unchanged
[3, 4, 5]
```

List delete table data

Method 1: del() deletes data values ​​in memory

```>>> l1 = ['a', 'b', 'c', 'd', 'e', 'f']
>>> del(l1)
>>> print(l1)
['b', 'c', 'd', 'e', 'f']
```

Method 2: remove() deletes the data value in memory, and uses the data value instead of the index.

```>>> l1 = ['a', 'b', 'c', 'd', 'e', 'f']
>>> l1.remove('a')	# Delete the data value of a
>>> print(l1)
['b', 'c', 'd', 'e', 'f']
# Question, how to delete if there are multiple a?
>>> l1 = ['a', 'b', 'c', 'd', 'a', 'e', 'f', 'a']
>>> l1.remove('a')
>>> print(l1)
['b', 'c', 'd', 'a', 'e', 'f', 'a']		# Only the first a in order from left to right is cleaned up
```

Method 3: pop() pops up the data, you can assign the popped data to other variables

```>>> l1 = ['a', 'b', 'c', 'd', 'e', 'f']
>>> l1.pop(0)
'a'		# This will output the data value you want to pop up, if you specify a variable, you can assign it
>>> print(l1)
['b', 'c', 'd', 'e', 'f']	# a in the original list is popped
```

sorting of lists of lists

How to use: sort()

```>>> l1 = ['d', 'c', 'b', 'a']
>>> l1.sort()
>>> print(l1)
['a', 'b', 'c', 'd']
>>> l2 = [5, 2, 1, 6, 9]
>>> l2.sort()
>>> print(l2)
[1, 2, 5, 6, 9]
```

Sort in reverse order, add parameter reverse (translated into Chinese: reverse, reverse, etc.)

```>>> l1 = ['a', 'b', 'c', 'd']
>>> l2 = [5, 2, 1, 6, 9]
>>> l1.sort(reverse = True)
>>> l2.sort(reverse = True)
>>> print(l1)
['d', 'c', 'b', 'a']
>>> print(l2)
[9, 6, 5, 2, 1]
```

The number of occurrences of the data value in the list statistics list

How to use: count()

```>>> l1 = ['aa', 'bc', 'cd', 'd']
>>> l1.count('d')		# Note that the entire string is matched, not a single letter
1
>>> l1.count('cd')
1
```

List of lists in reverse order output

```>>> l2 = [5, 2, 1, 6, 9]
>>> l2.reverse()
>>> print(l2)
[9, 6, 1, 2, 5]		# Note that the output is just reversed, not reverse sorted
```

# dictionary type

1. type conversion

Use dict(), but dict is very troublesome. Generally, you will not use this method for conversion. It is better to write it by hand.

```>>> print(dict([('name','pwd'),('jason',123)]))
{'name': 'pwd', 'jason': 123}
```
1. Operation and method

Dictionary lookup

Get the value by key, but there is a disadvantage that if the key does not exist in the dictionary, an error will be reported.

```>>> d1 = {'username': 'jack', 'age': 18}
jack
>>> print(d1['job'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'job'
```

How to use: get()

It is recommended to use, if the key does not exist in the dictionary, it will only return None or the specified value.

```>>> d1 = {'username': 'jack', 'age': 18}
jack
>>> print(d1.get('job'))	# Returns None by default, no error will be reported
None
>>> print(d1.get('job', 'error!!!!'))	# The second value defines the value returned
error!!!!
```

Modified value of dictionary

Modification by key, note that if the key does not exist in the dictionary, the modification will be changed to increase the key value.

```>>> d1 = {'username': 'jack', 'age': 18}
>>> d1['age'] = 99
>>> print(d1)
```

The added value of the dictionary

As long as the key does not exist in the dictionary, the method of using modification will change to increase the key value.

```>>> d1 = {'username': 'jack', 'age': 18}
>>> d1['job'] = 'Caption'
>>> print(d1)
{'username': 'jack', 'age': 18, 'job': 'Caption'}
```

Delete key value of dictionary

Method 1: del()

```>>> d1 = {'username': 'jason', 'job': 'Caption', 'age': 18}
>>> del d1['job']
>>> print(d1)
```

Method 2: pop() Note: This method is the same as the pop method in the list, you can assign the popped (deleted) value to a new variable

```>>> d1 = {'username': 'jason', 'job': 'Caption', 'age': 18}
>>> d1.pop('job')
'Caption'	# At this point you can assign this 'Caption' to a new variable
>>> print(d1)
```

Method 3: popitem Note: This method can only delete the last key-value pair added to the dictionary, and cannot specify which one to delete

```>>> d1 = {'username': 'jason', 'job': 'Caption', 'age': 18}
>>> d1.popitem()	# Delete the last key value added to the dictionary
('age', 18)
>>> print(d1)

>>> d1 = {'username': 'jason', 'job': 'Caption', 'age': 18}
>>> d1.popitem('username')		# As you can see, it is not possible to specify which key value to delete
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: popitem() takes no arguments (1 given)
```

The number of statistical key-value pairs in the dictionary

How to use: len()

```>>> d1 = {'username': 'jason', 'job': 'Caption', 'age': 18}
>>> print(len(d1))
3
```

Get all the keys in the dictionary

How to use: keys()

```>>> d1 = {'username': 'jason', 'job': 'Caption', 'age': 18}
>>> d1.keys()
```

Get all the values ​​​​in the dictionary

How to use: values()

```>>> d1 = {'username': 'jason', 'job': 'Caption', 'age': 18}
>>> d1.values()
dict_values(['jason', 'Caption', 18])
```

Dictionary to get the key-value pair data in the dictionary

How to use: items()

```>>> d1 = {'username': 'jason', 'job': 'Caption', 'age': 18}
>>> d1.items()
dict_items([('username', 'jason'), ('job', 'Caption'), ('age', 18)])
```

Quickly generate dictionaries with the same value for dictionaries

How to use: dict.fromkeys() Note: The value is shared by three keys, so no matter which key adds a value, other keys will see it.

```>>> print(dict.fromkeys(['name', 'job', 'age'], 123))
{'name': 123, 'job': 123, 'age': 123}

>>> d1 = dict.fromkeys(['name', 'job', 'age'], [])
>>> d1['name'].append('jason')		# Add a value 'jason' for name
>>> d1['job'].append('Caption')		# Add a value 'Caption' to the job
>>> d1['age'].append(18)			# Add a value of 18 to age
>>> print(d1)				# The result we want should be {'name': 'jason', 'job': 'Caption', 'age': 18}, but please see the output
{'name': ['jason', 'Caption', 18], 'job': ['jason', 'Caption', 18], 'age': ['jason', 'Caption', 18]}
```

# tuple type

1. type conversion

tuple (data value) description: all data values ​​that can be looped by for can be converted to tuple

```>>> s1 = 'abcde'
>>> tuple(s1)
('a', 'b', 'c', 'd', 'e')
```
1. Method and operation

index value

```>>> s1 = ('a', 'b', 'c', 'd', 'e')
>>> print(s1)
a
```

slice value

```>>> s1 = ('a', 'b', 'c', 'd', 'e')
>>> print(s1[0:1])
('a',)	# Note that there will be a comma when taking a value, and the same is true when specifying a variable. A comma at the end is considered a tuple, otherwise it is considered another data type
```

interval value

```>>> s1 = ('a', 'b', 'c', 'd', 'e')
>>> print(s1[0:4:2])
('a', 'c')
```

Reverse values, etc. are the same as lists

```>>> s1 = ('a', 'b', 'c', 'd', 'e')
>>> print(s1[-1:-3:-1])
('e', 'd')
```

The number of data values ​​in the tuple statistics tuple

How to use: len()

```>>> s1 = ('a', 'b', 'c', 'd', 'e')
>>> print(len(s1))
5
```

Tuple counts the number of occurrences of a data value

How to use: count()

```>>> s1 = ('a', 'b', 'c', 'd', 'e', 'a')
>>> s1.count('a')
2
```

View the index value corresponding to a data value in tuple

How to use: index() Note: If the data value is repeated, only the corresponding index value of the first data value from left to right can be seen

```>>> s1 = ('a', 'b', 'c', 'c', 'd')
>>> print(s1.index('c'))
2
```

# collection type

1. type conversion

set( data value)

Note: The data in the collection must be of immutable type (integer, float, string, tuple)

​ The data in the collection is also unordered, there is no concept of index

1. Operation and method

collection of deduplication

Remove duplicate values ​​in the data value, as long as the data type is converted to set, it will be automatically removed, but note: set deduplication cannot retain the sort order of the original data

```>>> l1 = [11, 22, 33, 11, 22, 55, 66]
>>> s1 = {11, 33, 55, 22, 99, 77, 11, 33, 22}
>>> print(set(s1))
{33, 99, 22, 55, 11, 77}
>>> print(set(l1))
{33, 66, 11, 22, 55}
```

The set takes the same value in two tuples

How to use: use '&' between two values ​​or use the method intersection (& is equivalent to intersection)

```>>> s1 = {'abc', 'ccc', 'qqq', 'ddd', 'lll'}
>>> s2 = {'cba', 'ccc', 'ddd', 'qqq', 'bbb'}
>>> print(s1 & s2)
{'qqq', 'ccc', 'ddd'}
>>> print(s1.intersection(s2))
{'qqq', 'ccc', 'ddd'}
```

The set takes the value that is in the previous set and not in the latter set

How to use: use '-' between two values ​​or use the method difference (- is equivalent to difference)

```>>> s1 = {'abc', 'ccc', 'qqq', 'ddd', 'lll'}
>>> s2 = {'cba', 'ccc', 'ddd', 'qqq', 'bbb'}
>>> print(s1 - s2)
{'lll', 'abc'}
>>> print(s1.difference(s2))
{'lll', 'abc'}
```

The set takes all the values ​​​​in the two tuples, and removes duplicate values

How to use: use '|' between two values ​​or use method union (｜ is equivalent to union)

```>>> s1 = {'abc', 'ccc', 'qqq', 'ddd', 'lll'}
>>> s2 = {'cba', 'ccc', 'ddd', 'qqq', 'bbb'}
>>> print(s1 | s2)
{'cba', 'lll', 'bbb', 'ccc', 'ddd', 'abc', 'qqq'}
>>> print(s1.union(s2))
{'cba', 'lll', 'bbb', 'ccc', 'ddd', 'abc', 'qqq'}
```

The tuple takes the unique value of two tuples (that is, removes the same part and outputs a different part)

How to use: Use '^' between two values ​​or use the method symmetric_difference (^ is equivalent to symmetric_difference)

```>>> s1 = {'abc', 'ccc', 'qqq', 'ddd', 'lll'}
>>> s2 = {'cba', 'ccc', 'ddd', 'qqq', 'bbb'}
>>> print(s1 ^ s2)
{'cba', 'abc', 'lll', 'bbb'}
>>> print(s1.symmetric_difference(s2))
{'cba', 'abc', 'lll', 'bbb'}
```

# Mutable and immutable types

1. Characteristics of mutable types:
2. What the list modifies after calling the built-in method is itself, and does not produce a new result.
3. The value of the variable changes, the memory address does not change
```>>> l1 = [1, 2]
>>> id(l1)
2242835102344
>>> l1.append(3)  # Modifies itself and does not produce a new result
>>> id(l1)
2242835102344	# memory unchanged
>>> print(l1)
[1, 2, 3]		# In the case of unchanged memory, the data value has changed
```
1. Characteristics of immutable types:
2. The string does not modify itself after calling the built-in method, but produces a new result.
3. When the value of a variable changes, the memory address will definitely change.
```>>> s1 = '\$\$\$abc\$\$\$'
>>> id(s1)
2242835110960
>>> s1.strip('\$')
'abc'	# produced a new result
>>> print(s1)
\$\$\$abc\$\$\$	# data value does not change
>>> id(s1)
2242835110960	# The memory address has not changed
# At this time, if you want to change the result, you need to reassign the variable
>>> s1 = s1.strip('\$')
>>> print(s1)
abc
>>> id(s1)
2242835112768  # Reassignment, the memory address will definitely change
```

Posted by Dvector on Tue, 29 Nov 2022 22:38:38 +1030