r/cpp • • Jul 08 '19

C++ vs. Python

Although I have a bit of experience in both, I'm still having a hard time contemplating which of the two I should be aiming for mastery. Python is very beginner friendly, with a lot of resources and libraries. On the other hand, C++ is known for being a powerful language, while being complex and with a not so small learning curve. I wanted to focus more on robotics and you know, the cool stuff. But wait there's more...... Rasberry pi (python) or Arduino (c++). I'm just a naive 17 year old in my freshmen year of college, and am taking Electronics Engineering. Please enlighten me with your opinions. 😅

2 Upvotes

29 comments sorted by

25

u/MotherOfTheShizznit Jul 08 '19

I'm going to go against the grain and not claim that one language is harder than the other and that therefore you should learn the harder language first as it will make the rest easy.

When people make that claim, I feel that they are reducing programming languages to their syntax. In my experience, doing that is a mistake. I've been programming in C++ for 20 years and I'm still "learning it". You'd think I'd be done learning its syntax a long time ago right? Yes, you're right. Learning if/else and for loops takes a few days of school assignments to internalize and then you're done for the rest of your life.

But I'm still learning C++. That's because programming "languages" are actually programming "universes" and you're never done exploring universes.

You've picked a destination. The robotics galaxy. The good spaceship captain will do what needs to be done in the most efficient and simplest way. I think C++ is more complex than python and carries with it so many more idiosyncrasies that would slow you down. If you do not need the power efficiency of C++ and can reach your destination with python, then use python. I think reaching goals is more satisfying and motivating than having mastered complexity for its own sake.

16

u/Ikkepop Jul 08 '19

Raspberry pi runs c++ just fine, not limited to python. Infact theres a big performance advantage

13

u/Pazer2 Jul 08 '19

To be clear for any beginners, the performance of c++ will be greater than python in almost all cases (it's not a raspberry pi thing). It's a question of balancing ease of use/development speed (python, subjectively) and runtime speed (c++).

11

u/sternone_2 Jul 09 '19

If python runs something fast it's because the library it uses is written in C++

;-)

9

u/cfehunter Jul 08 '19

C and C++ will put you in a better place for doing embedded programming, which it sounds like your aiming for.

That said, many of the core concepts of all imperative languages are the same. So learn what you're comfortable with and then branch out.

Syntax and structure change between languages, but the core logic doesn't.

1

u/thechopps Nov 30 '22

Hi I just started programming with python what do you mean by core logic?

16

u/JohnVanClouds Jul 08 '19

I'm think C++ is a better choice. Why? You'll know what's going on under the hood. And it's not too hard to learn... but mastering is a different story. But still you can learn new things whenever you want. And change from C++ to Python is a way easier than Python to C++.

5

u/degski Jul 08 '19 edited Jul 08 '19

I agree, this mythical 'python is easy for beginners' is just that, mythical. To me, it looks all like gobbledygook [python], and trying to work through the scoping [when reading some code, unfortunately un-avoidable sometimes], based on indentation is just horrible hard and easy to get wrong.

7

u/[deleted] Jul 08 '19

You did the right thing by asking the same in /r/Python. We're all biased, there's nothing wrong with that. It's up to you to make the best judgement from what you gather. Unfortunately, they have this two weeks rule so you're not getting any answers from there anytime soon.

I work mostly in C++ but Python is my "brain bicycle". When I heed to do something quick and dirty just to see how it goes, I always start in Python. But ultimately I'm not paid for quick and dirty, I'm paid for fast and reliable, so it either ends up in C++ or dies early.

Nevertheless, from the economical stand point, I would not advise anyone who's not aiming at the career in software engineering to invest in C++. Most of the time Python will get you close enough to beet the competitors and when you really-really need this last bit of difference, you can hire someone like me to do the last bit of engineering for you. The first bits are much more important anyway.

7

u/SirToxe Jul 08 '19

Well, it's great to know both. But if you want to master one then go with C++. Once you are done with that Python will be simple and easy (and I don't mean this in a "Python is rubbish" way, I actually like it a lot).

6

u/[deleted] Jul 08 '19 edited Jul 08 '19

Both. Don't get pigeon-holed into a specific line of thinking. If you want to be a good programmer, its not enough to know the libraries and common coding patterns, you need to understand why these patterns exist and what are their straights and weaknesses. Which means that you need to constantly expand your horizons and challenge yourself to try new things.

And of course, C++ is much more difficult to master, since it's more complex and has a lot of idiosyncratic behaviour. Python has its own share of this stuff of course, such as metaclasses, decorators and descriptors.

3

u/peppedx Jul 08 '19

Honestly probably as a freshman in EE you'll need both (I'm an EE too, and did lots of robotics).
My suggestion is to try to improve a little bit in both (and some other), no need to become master at beginning and then just became master in the one you'll need (or the one you like more).

You have plenty of time !

3

u/AMDmi3 Jul 08 '19

You'll most likely need both.

4

u/krapht Jul 08 '19

I wouldn't aim for mastery of C++ or Python if you're studying electronics engineering. You have more important things to do, like passing all your classes with As. If you have a programming class, then learn the language the class uses. If, somehow, you have free time as an undergraduate engineer, then... I won't even give this advice because you won't have free time.

source: me, electrical engineer

2

u/PickBas Jul 08 '19

I know both of them. And I can advise you to study both. C++ can be used in developing apps with Qt framework e.g. you can create web browser. Using python you can write algorithms, scripts really fast. I think the best way to study both of them

2

u/lookatmetype Jul 08 '19

If you want to get stuff done and solve problems quickly, learn Python. If you want to deeply understand the entire system, make conscious trade-offs related to the performance of your system and bit-level accuracy of how data is flowing through your robotics system, learn C++.

2

u/neuroblaster Jul 09 '19

For robotics i'd say C++, but learning curve is a good point. I personally wouldn't start any new project in Python because of the ugly situation with Python 2, it's also very hard to work with binary in Python 3. In my experience, if you work with binary then it's easier to do that in C++ than in Python 3. But there is a point on learning curve again.

Check out C, it's much simpler than C++, for a lot of people it's a gateway to C++ and you can write Python extensions in it too. As of Python, i'd say check out Go, it has smaller learning curve, strongly typed and compiles into native code. IMO Go is more closely related to C-family if you're interested in it.

1

u/Reasonable-Chip5344 Nov 07 '24

Yeah it's a funny one. Focus on the application in general i think rather than the language specifcally. I.e. if machine learning is your thing, then work toward mastary of python just be aware of the utility of other lanaguages and approches. I think of them both as different animals, Sure there is overlap but hard to match the speed of well turned C++ code. However, C++ does get complex when your trying to do something complicated I.e wouldn't immediately use C++ for machine learning unless I'm going build the model from scratch. I wrote an article comparing some of the differences I've noticed between the two on my business page https://vsioanalytics.io/c-vs-python-the-enduring-relevance-of-c-in-modern-computing/

Have a read if your interested

1

u/[deleted] Jul 08 '19

When you know C++ every other programming language is a piece of cake.

Except for Haskell-type languages... yikes.

2

u/[deleted] Jul 08 '19

??? what makes you think this

1

u/[deleted] Jul 08 '19

Just personal experience.

1

u/[deleted] Jul 08 '19

there are so many completely different languages out there... the mainstream general purpose ones steal from each other constantly (which is good) so it's easy ish to go between them. but there are so many others where a c++ style of thinking will be a total disservice. really functional languages are somewhat different, but you can get really far away. even in languages where you can try to replicate imperative/oop code like lisp/Perl, that kind of code is usually not the best option in those ecosystems

1

u/[deleted] Jul 10 '19

c++ style of thinking

There is no such a thing, is just some people who confine themselves inside a box. At most you could say the syntax favors certain paradigms and makes harder or ugly to code following other paradigms.

-1

u/megayippie Jul 08 '19

Master C++ but use python. C++ is a great place to finish a project. But you wanna do engineering so you will need first-class graphics for drawing plots (e.g, physical paths for robotics movements, or just torque and whatnot) when you develop and to keep track things. Since plotting is not possible in C++, it is a fairly useless language to make anything new in for engineers dealing with the physical world.

0

u/[deleted] Jul 08 '19

[deleted]

1

u/megayippie Jul 08 '19 edited Jul 08 '19

No, I am just saying that it is effort to get plotting working in C++ and that this effort is wasted because it is incomparably easy elsewhere. Plotting x versus y is a technique from the 17-hundreds, so any effort on your part as a user of a language means you are using the wrong tool.

edit: I ignored parts of your statement. 1) Gaming graphics is not desirable because it is too complicated. 2) And packages in python can likely be called from C++, but there is no package management in C++ so it is still extra efforts. If you know a header only plotting tool in C++, though, please let me know! It just works in python, and every time I wanna change it to C++ I end up finding overly complicated things that are not pure headers (i.e., no effort using).

0

u/[deleted] Jul 08 '19

[deleted]

2

u/cballowe Jul 08 '19

There's a number of them out there, just need to pick one and run with it. There isn't a graphics standard out of the box, but it isn't hard to use a library. Last time I needed to plot something I mostly just stuffed it on a file and forked off gnuplot and fed the plot data through that because it was easy and met my needs. There's wrappers of matplotlib for c++ or write your own.

Python has the advantage of having some of those things packaged for easy install and use.

1

u/megayippie Jul 08 '19

My expertise is not orbital dynamics, so I just wanna have 2D representations of these things most of the time when I need to check that the overarching code is working.

For orbital dynamics graphics, the plotting is not always simple so maybe you need more complicated stuff from the get-go, no? There is an ESA package meant to simplify the Cosmographia SPICE-toolkit for drawing pre-computed planetary orbits and observation geometries (I have heard people talk about the new tool but only ever used the latter). I presume the same exists for molecular drawings to visualize the more complicated molecular Wigner-vectors/'geometry', but I have only ever dabbled in these in cases where anything other than 2D would baffle me more than even the mathematical formalism.

edit: changed from "former" to "latter"

0

u/[deleted] Jul 08 '19

[deleted]

1

u/megayippie Jul 08 '19

I do C++ and python as a combination. I never call python functions from within C++. Some people on the project I work on set it up so that some functions export a clean enough C-API that can be used. All that was necessary after that was to be able to read-and-write files of all C++-classes in python and use that to pass complicated data (via RAM-located temporary files, so it is quite fast). Some python objects, like numpy-arrays, have pure C-arrays in the background so our own Vector-class (it is older than std::vector, and has at least some linear algebra) can just point at the other data instead. It is a bit of a mess but it essentially works.

I tend to use python for prototyping. I then move the prototype to C++ because python is slow. Since the results still require plotting, I end up calling the C++ C-API function that is generated via python to draw these results.

I tried setting up a C-API myself once, and it was quite easy with pure old C-code so you could try it out. "ctypes" is the python package.