1) Let the user input the content of multiplying any two integers (such as "2 * 3") to output "2 * 3 = 6". For example, the operation condition is:

Let's finish it step by step. Complete the input first:

strs = input()

Assuming that we input "2 * 3", we need to split the two values before and after the asterisk. This can be achieved by using the split function of the string:

strs = input() print(strs.split('*'))

The operation result is:

It can be seen that the split method will split the current string with the characters specified in parentheses, and get the first and second strings, which are '2' and '3' respectively. The two values form a list, so they can be obtained respectively through the list elements:

strs = input() num = strs.split('*') print(num[0], num[1])

The operation result is:

You can also use this method to obtain the two separated values:

strs = input() a, b = strs.split('*') print(a, b)

The operation result is:

At this time, the result is no longer a list, but the variable values of two string types.

Please note that both '2' and '3' are character types, so direct algebraic operation is not allowed:

strs = input() a, b = strs.split('*') print(a * b)

There is an error in the operation result. The error message means that the product operation of non integers cannot be carried out:

Therefore, it is necessary to perform algebraic operation after converting integers:

strs = input() a, b = strs.split('*') print(int(a) * int(b))

The operation result is:

This result is the tail of the result we are ready to output. Therefore, the previously entered content can be spliced in front of this result, which will form the final appearance?

strs = input() a, b = strs.split('*') print(strs + int(a) * int(b))

There is an error in the running result. The error message means that only characters can be connected to characters:

The simple reason is that the string (STR, s) cannot be added directly. Further improved as follows:

strs = input() a, b = strs.split('*') print(strs + str(int(a) * int(b)))

The operation result is:

After careful observation, it can be found that the final output is missing an equal sign, which is modified to:

strs = input() a, b = strs.split('*') print(strs + '=' + str(int(a) * int(b)))

The operation result is:

Of course, it can also be written as:

strs = input() a, b = strs.split('*') print(a + '*' + b + '=' + str(int(a) * int(b)))

The output content is the same as above. This is a little troublesome, but the effect is the same.

In addition to these methods, we can use another completely different method:

strs = input() print(strs + '=' + str(eval(strs)))

If you enter "2 * 3", the operation result is:

How to understand? The eval function can directly replace the parameter string and embed it in the current code. For example, if you enter "2 * 3", eval(strs) is 2 * 3. At this time, there are no quotation marks in its content, so the last line of the above statement

print(strs + '=' + str(eval(strs)))

Replace with:

print(strs + '=' + str(2 * 3))

Just the right statement that can be executed directly!

2) Calculate similarity:

This is the famous cosine similarity, which can calculate the distance between two vectors. For example:

We wrote the first code:

v11 = 1 v12 = 2 v21 = 3 v22 = 4 fenzi = v11 * v21 + v12 * v22 fenmu = (v11 ** 2 + v12 ** 2) ** 0.5 * (v21 ** 2 + v22 ** 2) ** 0.5 print(fenzi / fenmu)

Output: 0.983869910099974

Although the writing method seems very complex, it is actually simple. It is to express various common algebraic operations according to the calculation formula. You can observe it carefully.

For square sum and square root, you can also use the functions in math module to complete:

import math v11 = 1 v12 = 2 v21 = 3 v22 = 4 fenzi = v11 * v21 + v12 * v22 fenmu = math.sqrt(math.pow(v11, 2) + math.pow(v12, 2)) * \ math.sqrt(math.pow(v21, 2) + math.pow(v22, 2)) print(fenzi / fenmu)

The output content is the same as above. Where sqrt is the square function, pow is the power function, and the second parameter in parentheses represents square. For details, please note that because the fenmu expression is very long, the \ newline character is used. This symbol indicates that the next line is still followed by this code, just to avoid displaying too long content in one line.