Python implements three lines of code to solve a simple one-dimensional equation and three lines of python.

Source: Internet
Author: User
Tags integer division

Python implements three lines of code to solve a simple one-dimensional equation and three lines of python.

The example described in this article is that Python uses three lines of code to solve the one-dimensional equation. The code is concise and efficient. The specific usage is as follows:

>>> solve("x - 2*x + 5*x - 46*(235-24) = x + 2")3236.0

The function code is as follows:

def solve(eq,var='x'):  eq1 = eq.replace("=","-(")+")"  c = eval(eq1,{var:1j})  return -c.real/c.imag

Let's explain the code below.

The first line is the first line, which transforms the equation and generates a formula "x-2 * x + 5 * x-46 * (235-24)" with the result 0) -(x + 2 )".
The second row uses eval to execute this formula, and x = 1j is substituted into the formula. The result is-9708 + 3j.
Note that x = 1j, so this equation is reduced to "-9708 + 3x = 0". You only need to convert-(-9708)/3 to get x.
-9708 is the real part of the complex number, and 3 is the virtual part of the complex number, so the result is "-c. real/c. imag ".
Obviously, this function cannot solve the complex number equation.
By the way, the/operation of Python 2.x uses integer division, leading to the loss of the fractional part. Therefore, Python 3.xshould be used to obtain the correct result.

I hope this example will help you learn Python.


Python solves the one-dimensional equation

Def solve (eq, var = 'X'): eq1 = eq. replace ("=", "-(") + ")" c = eval (eq1, {var: 1j}) return-c. real/c. imag >>> solve ('2 * x = 5', 'x') 2.5

Give me 90 super simple one-dimensional equation Application Questions (including answers)

Column the equation and obtain the solution of the equation
8 times of a number, 6.4 More than the product of 4.5 and 2.8.

A number is 5 times less than the product of 3.5 and 6.3. What is this number?

The number of A is more than four of the 10 scores of B. It is known that the number of A is 2, and the number of B is what?

What number is multiplied by 0.32?

The number is 15 times less than 5.6. What is the number?

Use the column equation to solve the problem. ,
1. Two water inlet pipes a and B inject water into a pool containing 8 tons of water. Each water inlet pipe a injects 400 kg water per minute. How many kilograms of water can be injected into the pool within 8 minutes?

2. The two regions are 249 km apart. A train departs from ground a to ground B, with 55.5 km lines per hour. How many hours are there 27 km lines away from location B?

3. Buy five books and three pencils for a total of 10.4 yuan. We know that every 0.9 yuan is worth a pencil. How much is it worth?

4. The garment factory has already made 984 sets of clothing and 120 sets of clothing. How many sets of clothing are completed on average every day in the remaining 12 days?

5. The motor plant plans to produce 1980 electric motors. It has been produced for four days and 45 units are produced every day. Thanks to the improved technology, it will increase production by 15 units a day. How many days does it take to complete the task?

6. A total of 135 table tennis and basketball balls are bought at school. The bought table tennis is eight times that of basketball. How many balls are there?

7. The library has bought 235 books of literature and art, and 25 books of literature and art are more than twice as many as 25 books of Science and Technology. How many books have they bought?

8. A, B, and C donated a total of 270 yuan to the disaster area. A donated 3 times that of B, and B donated 2 times that of C. How much did the three donate?

9. The two terminals A and B are 379.4 km apart. Ship A is 3.6 km faster than ship B every hour. The two terminals are simultaneously opposite each other, after three hours, the two ships are still 48.2 km apart. What is the speed of the two ships?

Additional questions:
1. Vehicles A and B are driving from City A to City B at an hourly rate of 100 km and 120 km respectively. Car A Leaves City A one hour earlier than Car B, but the two cars arrive at City B at the same time. What is the journey between the two cities? (Do not use equations to answer: follow-up questions)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.