当前位置:网站首页>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 .
边栏推荐
- Global and Chinese market of electrolyte analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
- WPF format datetime in TextBlock- WPF format DateTime in TextBlock?
- Use of aggregate functions
- unittest框架基本使用
- Rd file name conflict when extending a S4 method of some other package
- Microservice knowledge sorting - search technology and automatic deployment technology
- [Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University
- Leetcode 1189. Maximum number of balloons (special character count)
- Meso tetra [P - (p-n-carbazole benzylidene imino)] phenylporphyrin (tcipp) /eu (tcipp) [pc( α- 2-oc8h17) 4] and euh (tcipp) [pc (a-2-oc8h17) 4] supplied by Qiyue
- 2022 Xinjiang latest road transportation safety officer simulation examination questions and answers
猜你喜欢

Chapter 1: find the algebraic sum of odd factors, find the same decimal sum s (D, n), simplify the same code decimal sum s (D, n), expand the same code decimal sum s (D, n)

Chapter 20: y= sin (x) /x, rambling coordinate system calculation, y= sin (x) /x with profile graphics, Olympic rings, ball rolling and bouncing, water display, rectangular optimization cutting, R que

PR 2021 quick start tutorial, how to create a new sequence and set parameters?

Chapter 1: drinking soft drinks, step tariff calculation, step tariff calculation function, personal income tax, solving square root inequality, simplifying solving square root inequality, solving dem

FPGA 学习笔记:Vivado 2019.1 工程创建

Leetcode 1189. Maximum number of balloons (special character count)

第一章: 舍罕王失算
![[effective Objective-C] - block and grand central distribution](/img/09/22b979b97ea13d649b4b904637b79f.jpg)
[effective Objective-C] - block and grand central distribution
![2022-06-30 advanced network engineering (XIV) routing strategy - matching tools [ACL, IP prefix list], policy tools [filter policy]](/img/b6/5d6b946d8001e2d73c2cadbdce72fc.png)
2022-06-30 advanced network engineering (XIV) routing strategy - matching tools [ACL, IP prefix list], policy tools [filter policy]

Chapter 1: find the factorial n of n!
随机推荐
Chapter 1: extend the same code decimal sum s (D, n)
Cross compile opencv with contrib
Wechat applet quick start (including NPM package use and mobx status management)
BOC protected alanine porphyrin compound TAPP ala BOC BOC BOC protected phenylalanine porphyrin compound TAPP Phe BOC Qi Yue supply
Global and Chinese markets of cast iron diaphragm valves 2022-2028: Research Report on technology, participants, trends, market size and share
5. MVVM model
第一章: 舍罕王失算
Class loading process
04 -- QT OpenGL two sets of shaders draw two triangles
Rad+xray vulnerability scanning tool
Parental delegation mechanism
unittest框架基本使用
Micro service knowledge sorting - cache technology
Unittest framework is basically used
Today's work summary and plan: February 14, 2022
Strict data sheet of new features of SQLite 3.37.0
03 -- QT OpenGL EBO draw triangle
第二章:4位卡普雷卡数,搜索偶数位卡普雷卡数,搜索n位2段和平方数,m位不含0的巧妙平方数,指定数字组成没有重复数字的7位平方数,求指定区间内的勾股数组,求指定区间内的倒立勾股数组
[Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University
P5.js development - setting