r/AskEngineers • • Mar 16 '19

Mechanical Is it worth learning Python as a Mechanical Engineer?

Greetings, I am a freshman in Mechanical engineering and I've already finished my MATLAB course, and I find coding fun, so I was wondering if I should learn python? But of course I won't waste my time learning it if it isn't beneficial to me. So my fellow MEs, how useful would it be learning python? And what are these benefits. Thank you!

296 Upvotes

165 comments sorted by

View all comments

Show parent comment

10

u/Wetmelon Mechatronics Mar 16 '19

And if you do use MATLAB on a regular basis like me... it comes with the "Matlab Engine for Python" , which lets you call matlab functions directly from Python... quite efficiently, I might add. A++

3

u/Willingo Mar 17 '19

Wow, really? Could you explain more? I am going from hundreds of hours from Matlab to Python.

6

u/Wetmelon Mechatronics Mar 17 '19

https://www.mathworks.com/help/matlab/matlab-engine-for-python.html

Basically it allows you to write like this:

import matlab.engine
eng = matlab.engine.connect_matlab

Now eng is connected to a shared matlab session, or a headless session if it doesn’t find a shared one. Then you can make calls. For example...

Check if a number is prime:

res = eng.isprime(37)
print(res)

Plot some data (no output arguments):

x = [1, 2, 3, 4]
y = [2, 4, 5, 8]
eng.plot(x, y, nargout=0)

Your more complex tasks are also possible, like code generation.

1

u/Willingo Mar 17 '19

lol that's pretty cool thanks!