当前位置:网站首页>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 .
边栏推荐
- Professional interpretation | how to become an SQL developer
- 04 -- QT OpenGL two sets of shaders draw two triangles
- Unittest framework is basically used
- 03 -- QT OpenGL EBO draw triangle
- Wechat applet quick start (including NPM package use and mobx status management)
- Global and Chinese market of high temperature Silver sintering paste 2022-2028: Research Report on technology, participants, trends, market size and share
- Change deepin to Alibaba image source
- NFT without IPFs and completely on the chain?
- Micro service knowledge sorting - asynchronous communication technology
- Chapter 2: find the box array, complete number in the specified interval, and improve the complete number in the specified interval
猜你喜欢
IPv6 experiment
Point cloud data denoising
Commands related to files and directories
NFT without IPFs and completely on the chain?
HCIA-USG Security Policy
[Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University
Acquisition and transmission of parameters in automatic testing of JMeter interface
2022-07-02 网工进阶(十五)路由策略-Route-Policy特性、策略路由(Policy-Based Routing)、MQC(模块化QoS命令行)
原生表格-滚动-合并功能
The 15 year old interviewer will teach you four unique skills that you must pass the interview
随机推荐
Blue Bridge Cup: the fourth preliminary - "simulated intelligent irrigation system"
Gym welcomes the first complete environmental document, which makes it easier to get started with intensive learning!
Commands related to files and directories
Detailed and not wordy. Share the win10 tutorial of computer reinstallation system
2022-06-30 網工進階(十四)路由策略-匹配工具【ACL、IP-Prefix List】、策略工具【Filter-Policy】
Chapter 1: find all factorial sums, Grand Prix site unified programming, three factorial sums, graphic point scanning, recursive factorial n of n!, Find the factorial n of n!, King Shehan miscalculate
CMD implements the language conversion of locale non Unicode programs
交叉编译Opencv带Contrib
2022-06-30 网工进阶(十四)路由策略-匹配工具【ACL、IP-Prefix List】、策略工具【Filter-Policy】
Strict data sheet of new features of SQLite 3.37.0
P5.js development - setting
Utilisation de base du cadre unitest
BOC protected tryptophan porphyrin compound (TAPP Trp BOC) Pink Solid 162.8mg supply - Qiyue supply
Rd file name conflict when extending a S4 method of some other package
Ae/pr/fcpx super visual effects plug-in package fxfactory
01 - QT OpenGL display OpenGL window
[Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University
Upgrade PIP and install Libraries
Global and Chinese market of cyanuric acid 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets of active matrix LCD 2022-2028: Research Report on technology, participants, trends, market size and share