当前位置:网站首页>2.6 formula calculation
2.6 formula calculation
2022-07-03 20:00:00 【leeshuqing】
1) Let the user input the content of multiplying any two integers ( Such as “2*3”) Can output “2*3=6”, For example, the operation is :
Let's finish it step by step . Complete the input first :
strs = input()
Suppose we enter “2*3”, Now we need to split the two values before and after the asterisk . This can take advantage of the string itself split Function to implement :
strs = input()
print(strs.split('*'))
The running result is :
I see ,split Method splits the current string with the characters specified in parentheses , And get the first and second strings , Namely ‘2’ and ‘3’. Two values form a list , Therefore, we can get :
strs = input()
num = strs.split('*')
print(num[0], num[1])
The running result is :
You can also use this method to obtain the two separated values :
strs = input()
a, b = strs.split('*')
print(a, b)
The running result is :
The result is no longer a list , Is the value of two variables of string type .
Please note that , This ‘2’ and ‘3’ All character types , Therefore, direct algebraic operation is not allowed :
strs = input()
a, b = strs.split('*')
print(a * b)
There is an error in the running result , The error message is that you cannot multiply non integers :
Therefore, it is necessary to convert integers and perform algebraic operations :
strs = input()
a, b = strs.split('*')
print(int(a) * int(b))
The running result is :
This result is the tail of the result we are going to output . Therefore, the previously entered content can be spliced in front of this result , Doesn't it 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 reason is simple ,strs For the string ,int(a) Is an integer , Of course, you can't add it directly . Further improved as :
strs = input()
a, b = strs.split('*')
print(strs + str(int(a) * int(b)))
The running result is :
Careful observation shows that , The final output is missing an equals sign , It is revised again as :
strs = input()
a, b = strs.split('*')
print(strs + '=' + str(int(a) * int(b)))
The running result is :
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 . This is a bit of a hassle , But the effect is the same .
In addition to these methods , We can also use another completely different method :
strs = input()
print(strs + '=' + str(eval(strs)))
If input “2*3”, The running result is :
How to understand that ?eval Function can directly replace the parameter string , And embedded in the current code . For example, if you enter “2*3”, be eval(strs) Namely 2*3, At this time, there are no quotation marks around its content , So the last line above
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 , You can calculate the distance between two vectors . such as :
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 is : 0.9838699100999074
Although the writing seems very complicated , In fact, it's simple , It is to express various common algebraic operations according to the calculation formula , You can observe carefully .
For square sum square , You can also use 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 . among ,sqrt Is the square function ,pow Is a power function , The second parameter in parentheses represents square . Please pay attention to one detail , because fenmu The expression is very long , Used \ A newline , This symbol means that the next line is still followed by this code , Just to avoid a long line of content .
边栏推荐
- Microservice knowledge sorting - search technology and automatic deployment technology
- IP address is such an important knowledge that it's useless to listen to a younger student?
- Kubernetes cluster builds efk log collection platform
- BOC protected alanine porphyrin compound TAPP ala BOC BOC BOC protected phenylalanine porphyrin compound TAPP Phe BOC Qi Yue supply
- February 14-20, 2022 (osgear source code debugging +ue4 video +ogremain source code transcription)
- 3. Data binding
- Chapter 1: King Shehan miscalculated
- Chapitre 1: le roi de shehan a mal calculé
- Native table - scroll - merge function
- Explore the internal mechanism of modern browsers (I) (original translation)
猜你喜欢
BOC protected phenylalanine zinc porphyrin (Zn · TAPP Phe BOC) / iron porphyrin (Fe · TAPP Phe BOC) / nickel porphyrin (Ni · TAPP Phe BOC) / manganese porphyrin (Mn · TAPP Phe BOC) Qiyue Keke
Bright purple crystal meso tetra (4-aminophenyl) porphyrin tapp/tapppt/tappco/tappcd/tappzn/tapppd/tappcu/tappni/tappfe/tappmn metal complex - supplied by Qiyue
Make a simple text logo with DW
Geek Daily: the system of monitoring employees' turnover intention has been deeply convinced off the shelves; The meta universe app of wechat and QQ was actively removed from the shelves; IntelliJ pla
PR FAQ: how to set PR vertical screen sequence?
Acquisition and transmission of parameters in automatic testing of JMeter interface
2022-06-30 advanced network engineering (XIV) routing strategy - matching tools [ACL, IP prefix list], policy tools [filter policy]
05 -- QT OpenGL draw cube uniform
CMD implements the language conversion of locale non Unicode programs
Exercises of function recursion
随机推荐
Typora charges, WTF? Still need support
Chapter 2: find the number of daffodils based on decomposition, find the number of daffodils based on combination, find the conformal number in [x, y], explore the n-bit conformal number, recursively
QT tutorial: signal and slot mechanism
Global and Chinese market of high purity copper foil 2022-2028: Research Report on technology, participants, trends, market size and share
How to read the source code [debug and observe the source code]
Chapitre 1: le roi de shehan a mal calculé
Utilisation de base du cadre unitest
Chapter 1: recursively find the factorial n of n!
Global and Chinese market of two in one notebook computers 2022-2028: Research Report on technology, participants, trends, market size and share
Wechat applet quick start (including NPM package use and mobx status management)
Chapter 1: find the factorial n of n!
Day11 - my page, user information acquisition, modification and channel interface
Micro service knowledge sorting - asynchronous communication technology
2. Template syntax
2022-06-25 网工进阶(十一)IS-IS-三大表(邻居表、路由表、链路状态数据库表)、LSP、CSNP、PSNP、LSP的同步过程
Detailed and not wordy. Share the win10 tutorial of computer reinstallation system
Implementation of stack
Chapter 1: sum of three factorials, graph point scanning
Vscode reports an error according to the go plug-in go get connectex: a connection attempt failed because the connected party did not pro
JMeter connection database