# Python and C++

This topic is 4004 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ive been deciding between learning Python or C++ as a first ever language and first time programming. From what I am reading, Python is easier to learn but doesnt have the same abilities as C++. On the other hand C++ is alot harder to learn but is more extensive. Which do you think would really be best for a beginner hoping to eventually program games? I read somewhere that some consider Python to be minimalist because the program does away with many problems associated with other languages. I just dont want to take shortcuts in my learning, It seems like learning C++ introduces you to almost every aspect of programming while Python kind of sugar coats some things. This is just how it seems to me based on my reading, I might be totally wrong. So please suggest which language would benefit me more. I also need to know what I need to be able to write code and turn it into a program? Can these languages be dealt with totally in Windows or do I need a Unix system?

##### Share on other sites
You can do Python or C on either Win/Linux systems.

There are concepts in C which make it difficult for people learning to code. Pointers, references & C sytle arrays[] seem to take people a long time to fully understand, and then you also have to understand the class concept. Python uses classes similar to C and you can actually port data between Python & C with some of the python headers and libs for C. Ron Penton did some neat stuff integrating Python w/ C in his book Mud Game Programming. He wrote a bunch of wrappers which are free to DL which would make integrating them much easier.

You want to make games, I would say start with C since you can add Python as a scripting to it later.

##### Share on other sites
Quote:
 Original post by biggjoee5790On the other hand C++ is alot harder to learn but is more extensive.
It depends on what you mean by "extensive". There's a lot more of the basic language to learn than there is with Python. At the same time, though, Python offers all the functionality of C++ and more.
Quote:
 Which do you think would really be best for a beginner hoping to eventually program games?
Python. Hands down.
Quote:
 It seems like learning C++ introduces you to almost every aspect of programming while Python kind of sugar coats some things.
Learning C++ introduces you to almost every aspect of C++. (There are major aspects of programming in general which it omits.) It has the advantage of being more widely used in the industry than Python. As a first language, however, it's a very poor choice; and in my experience, people who try to learn C++ as their first language frequently either burn out or end up less proficient as programmers than those who start with a more user-friendly language and then learn C++ later on.

##### Share on other sites
I absolutely, definatly suggest Python. There are many things to learn when starting to program that are more important than knowing how to work with "C++ specific problems". Algorithms, good program design etc, and Python is going to let you focus on learning that stuff much better than C++. C++ is probably one of the worst possible languages to start learning, and while you probably will end up learning it in the end its not a good place to start.

Think of it like this: Python is that awsome pedagogic teacher you remember from your early school years that made learning fun, and C++ is the army drill-sergeant making you crawl through mud with a backpack full of rocks, while he's bellowing orders and firing machine-gun bullets around your ass.

so its, "learn while having fun" vs "learn or get you ass shot full of hot lead"

##### Share on other sites
Quote:
 From what I am reading, Python is easier to learn but doesnt have the same abilities as C++.

Define "abilities". If we're talking about the speed of number-crunching programs, C++ is superior. If we talk about which language is more effective and elegant in expressing algorithms and patterns, Python is of course superior(since it is a much higher level language).

Quote:
 I read somewhere that some consider Python to be minimalist because the program does away with many problems associated with other languages. I just dont want to take shortcuts in my learning, It seems like learning C++ introduces you to almost every aspect of programming while Python kind of sugar coats some things.

C++ is also take "shortcuts" in comparison to,say, assembly. The whole notion of programming languages is to take shortcuts, otherwise we would still write our programs in hex editors. If you think chasing memory leaks and segmentation faults is an "aspect of programming" you want to get in touch with from the beginning of your learning, then I suppose you'll like C++. Otherwise, you would be wise of choosing another higher-level language like C#,Python or Ruby.

##### Share on other sites
Quote:
 Original post by biggjoee5790From what I am reading, Python is easier to learn but doesnt have the same abilities as C++.

That's correct. It has more.

C++ doesn't natively support introspection, or reflection, or metaclass hacking. C++ doesn't support tuples as a language intrinsic (even with the modern Standard C++ Library). C++ doesn't natively possess the ability to compile itself, or spawn instances of its compiler/interpreter. Among others. (And let's not even get into expressivity.)

Quote:
 On the other hand C++ is alot harder to learn but is more extensive.

No. It's just faster at run time, in specific cases.

Quote:
 Which do you think would really be best for a beginner hoping to eventually program games?

Python, because you'll focus on making the game, rather than on getting the source to compile, or link, or locate resources, or add a new library, or the thousands of other environmental failures common in beginner usage of C++.

Quote:
 I read somewhere that some consider Python to be minimalist because the program does away with many problems associated with other languages.

That's an interesting definition of "minimalist." Bookmark that source, and make sure you never visit it again.

Quote:
 I just dont want to take shortcuts in my learning, It seems like learning C++ introduces you to almost every aspect of programming while Python kind of sugar coats some things.

In short, no.

In greater length, programming effectively in C++ requires a greater understanding of the underlying platform and its mechanisms. This knowledge is useful, eventually, but not in all cases (in fact, arguably not in the majority of cases). A language like Python lets you take advantage of such knowledge as you come to possess it; a language like C++ will often stall you until you possess it - which, by definition, you don't as a beginner.

Quote:
 I also need to know what I need to be able to write code and turn it into a program?

Generally, C++ must be compiled into a native binary for your specific platform while Python is interpreted. One advantage of the interpreter is that the same Python code will often run, unaltered, on multiple platforms. The same C++ code may compile and may link, but once you stray from the Standard Library, it almost certainly will not run.

If you're a Windows user, I recommend Microsoft Visual C++ 2005 Express Edition for C++ and ActiveState ActivePython for Python. Both are free. The latter (ActivePython) is a much smaller download with much lower system requirements.

Here's the thing a lot of people advocating C++ as first language forget: you will need more than one language, eventually. As much as I love Python, and it's my default language to do anything in, at work we use C++ and Perl on both Windows and Linux exclusively. I have PHP, Ruby and Tcl interpreters on this machine, too, in case I need to do something quick and dirty. And I don't count markup and "web technologies" (though I should install SpiderMonkey and play around with JavaScript...)

Learning Python first lets you focus on learning programming, after which it becomes easier to learn other languages - they become an exercise in mapping concepts you are already familiar with onto new syntax.

Happy hacking! [smile]

##### Share on other sites
Wow you guys really cleared alot up for me. Im sure that Im going to start with Python now, I dont want to find myself too frustrated and decide to quit getting caught up in C++ problems. It seems like once you know a language well, learning others becomes easier. Im going to try to get everything I need to begin learning Python, hopefully I can get some good books. Now I do want to know, can you use Python to program games well? both 2d and 3d? I know that at first I wont be making any games. Im guessing Id start with simple text based apps and then move up from there

##### Share on other sites
Have a look at PyGame

##### Share on other sites
I wanted to ask something else about Python. I wanted to know if its an "all purpose language". What I mean by that is, can it be used for making all sorts of programs efficiently. I know that certain languages are suited for certain tasks. Ive read that C++ can be used for programming almost anything which is why its used so extensively. Im asking this because, even though I said I want to program games, I want to have the ability to create all types of programs not just games. I just want to learn a first language that will allow me to be a versatile programmer and learn all the most important aspects of programming. In other words I want to start with the most general ideas and skills and then begin to specialize. I hope that makes sense :)

##### Share on other sites
Python is most certainly NOT a domain-specific language. In terms of applicability to diverse areas of development, Python and C++ are just about equal.

##### Share on other sites
Awsome, then it appears that theres really no reason for me to begin with C++. If I can do everything with Python that I can do with C++, and Python will help a beginner learn better and get a better understanding of things, Python seems like the way to go. Anyway, I can always learn a new language once I can use Python fluently and create programs with it. Im going to download that Active Python program once I get a good book that teaches it. Any suggestions on a really good Python book, something that caters to a beginner?

##### Share on other sites
I'm not going to tell you to choose C++ over Python but I am going to make this discussion less one-sided

C++ has some trick areas, for example someone above listed:
"Pointers, references & C sytle arrays[]"

Now you see pointers and C style arrays are advanced features (references are easy). A beginner wouldn't start out with them. So they shouldn't be an obstacle to learning. I say shouldn't but a lot of C++ tutorials are really C tutorials in disguise. So if you were my neighbor you would have no problem learning C++ because I would guide you through the topics in an order that makes them easy to learn. However if you try to learn using the internet you are likely to stray.

Ok but now here is the catch: pointers are tricky and yet so called easy languages (C#, java) expose you to them early. I don't remember whether Python does but I'm almost certain that it does. Pointers are tricky and so I think that these high level languages can be harder to learn. They might not seem harder but that often just means that you are ignorant of how your language actually works. I was trying to explain C++ references to my brother (who only knows pascal and java, he's in highschool) and to give him and example I asked him to write a swap function in java. He actually thought that he could but it isn't possible (maybe with some advanced trickery it is but I am talking about a simple swap function).

With C++ you use pointers less frequently and when you do use them it is your choice. With other languages you use pointers more frequently and usually the language choses when you do.

Ok enough about pointers. My biggest objection to Python is that it does not have compile-time type checking. Compile-time type checking allows the compiler to automatically find a whole category of errors for you. You are a human, and humans make mistakes. I had to use Python in Robotics II at college and it drove me insane. I cursed Python every day. I really don't like the language because of the suffering that it has caused me. When I program in C++ I never have to deal with that category of error. The compiler just finds it for me and I simply fix it. So now the errors that frusterate me most are those relating to string literals. There are techniques to minimize those but it would be even nicer if the compiler could just do it for me.

So I would go with C++ if I were you. It really isn't that hard if you learn it correctly. This website has a series on learning with the book Accelerated C++. My wife bought my brother a copy for \$5.

##### Share on other sites
Hehe Im really bad when faced with multiple options :) Im always worried that I will make a choice that makes my learning harder. Sometimes I wish there was 1 path to everything haha. Well I can see where C++ and Python have their strong points, I just dont have enough knowledge to make an educated decision. I did order 2 books from amazon that are an intro to programming. They do not deal with any specific languages, just lay down the foundations of programming and whats involved (pointers, etc) Im going to decide on a language definetely after I read through the books. They should help me understand more about how programming works, after I know all that, maybe Ill be able to decide on a language easier. As of now, Python seems to make the most sense to me, It seems very approachable to a newbie, and like everyone said, I can always learn more languages later

##### Share on other sites
Quote:
 Original post by biggjoee5790Awsome, then it appears that theres really no reason for me to begin with C++. If I can do everything with Python that I can do with C++, and Python will help a beginner learn better and get a better understanding of things, Python seems like the way to go. Anyway, I can always learn a new language once I can use Python fluently and create programs with it. Im going to download that Active Python program once I get a good book that teaches it. Any suggestions on a really good Python book, something that caters to a beginner?

I'm going through this book right now and it's pretty good so far and fun since it's game programming oriented.

##### Share on other sites
Try here for python related resources and books!

scroll down to the third post

##### Share on other sites

Glak, I can't imagine what you're referring to saying that C# and Java "throw pointers at you early". First, C++ certainly does that, as does C, as you have to use pointers to do anything useful at all. Second, Java does not have pointers. Its variables holding references to objects are just that -- references. The syntax is transparent and for all practical purposes it's about the same as using Python object references.

##### Share on other sites
Quote:
 Original post by biggjoee5790It seems like once you know a language well, learning others becomes easier.
A lot easier; your first language will be by far the hardest to learn. You don't need to learn a language but rather need to learn programming, for which a more elegent language such as Python is an excellent choice.

Quote:
 I'm going to try to get everything I need to begin learning Python, hopefully I can get some good books.
Fruny listed some good Python tutorials/references in the 'noteworthy threads' sticky (linked above by parawizard). As an added bonus, many of the materials are available for free online unless you'd prefer to invest the cash in a hardcopy book. I personally liked How to Think Like a Computer Scientist -- Learning with Python when I was first getting started with the language, but take a look at a few of the different options and pick something that you think may suit you.

Quote:
 Now I do want to know, can you use Python to program games well? both 2d and 3d?
Python has been used to varying degrees in Toontown Online, Eve Online and Civilization 4 amongst others. You can also see some examples of (mostly hobbyist) games developed with PyGame at the PyGame website.

Quote:
 I know that at first I wont be making any games. Im guessing Id start with simple text based apps and then move up from there
Correct. The good news though is that those who begin with Python generally get to start playing with some graphics and making small "real games" a lot sooner than many of those who decide to start with C or C++.

Quote:
 I just dont have enough knowledge to make an educated decision.
The important thing is not so much making the right decision, but simply making a decision at all. Gather whatever information you think will help you (you're already doing that right now!), then pick a language and get started with it. Remember that even (perhaps especially) failed attempts at programming are a great learning experience as long as you choose to learn from your experience and stick with it rather than allowing it to frustrate you and turn you off programming altogether. Reading through your books and then making a decision is a good idea, but also keep in mind that reading a book is no substitute for actually trying things out. [smile]

Hope that helps.

##### Share on other sites
A quick word... If you wish to change from python to C++ later on, I'm guessing that going from weakly-typed Python to strongly (occasionally annoyingly strong) typed C++ might make you go a little insane. Just a thought. (I personally started C++ and went to python, and found the weakly-type-ness-thinger of it to be a pure annoyance, so I'm only guessing at the reverse being more or less the same).

G'luck with Python!

http://www.vex.net/parnassus/

##### Share on other sites
Quote:
 Original post by AtashA quick word... If you wish to change from python to C++ later on, I'm guessing that going from weakly-typed Python to strongly (occasionally annoyingly strong) typed C++ might make you go a little insane. Just a thought. (I personally started C++ and went to python, and found the weakly-type-ness-thinger of it to be a pure annoyance, so I'm only guessing at the reverse being more or less the same).

Just to get the terminology right:

Python: dynamically and strongly typed
C++: statically and weakly typed

##### Share on other sites
Quote:
 Original post by GlakI'm not going to tell you to choose C++ over Python but I am going to make this discussion less one-sided

Ooh! Goody!

Quote:
 Now you see pointers and C style arrays are advanced features (references are easy).

Actually, arrays are such a fundamental concept in programming that your calling them "advanced features" is quite mirth-inducing. The problem is, C really doesn't have any high level data structures (struct is basically a contiguous memory aggregation, with a few offset optimizations that came along later), so it turns the whole thing into a tragic mess. Arrays, for example, really don't exist in C; all you have is a pointer (ie, a memory address) and an offset. There is no bounds checking, and the array is completely oblivious to its own extents.

So, no, you're wrong. Pointers and C-style arrays are primitive features, not advanced.

Quote:
 A beginner wouldn't start out with them.

Wrong again, at least about arrays. A language that makes a simple array - a linear list! - a complex, "advanced" feature is an obstacle to learning. Incidentally, C++ introduces std::vector and other container types because they are such data structures.

Quote:
 Ok but now here is the catch: pointers are tricky and yet so called easy languages (C#, java) expose you to them early. I don't remember whether Python does but I'm almost certain that it does.

Wrong and wrong. Java has no pointer types; everything is a reference. C# requires you to use unsafe blocks - an advanced feature - to access pointers. Python only has references to values, too.

Quote:
 With C++ you use pointers less frequently and when you do use them it is your choice. With other languages you use pointers more frequently and usually the language choses when you do.

And this confirms that you don't know what pointers are, because they are intrinsic to C++ programming. In fact, the interface of container iterators was designed to match pointers - technically, pointers conform to the iterator concept, but I digress.

Quote:
 My biggest objection to Python is that it does not have compile-time type checking. Compile-time type checking allows the compiler to automatically find a whole category of errors for you.

a.) Not really. Compile time type checking in a language like C++ is undermined by the fact that type coercion is trivial.
b.) There are dynamic languages (Python is a dynamic language) that have static checking.

Quote:
 When I program in C++ I never have to deal with that category of error. The compiler just finds it for me and I simply fix it.

The compiler is useful to determine syntax errors, but it doesn't help in the slightest with logic errors.

Quote:
 So now the errors that frusterate me most are those relating to string literals. There are techniques to minimize those but it would be even nicer if the compiler could just do it for me.

Did you know that you never have string literal errors in Python? For two reasons. First, strings are constant. All strings, no matter what. They are never, ever mutable. Ever. Second, strings are a language intrinsic type, so they always behave as expected. You can call all string methods on literals without having to obtain a reference to an instance.

Quote:
 So I would go with C++ if I were you. It really isn't that hard if you learn it correctly.

Don't take this the wrong way, but I'm not sure you've learned it correctly.

##### Share on other sites
Eventually, you'll learn both. A good programmer knows a wide variety of languages. I'd start with Python first and move onto C++ once you've tackled a number of decent-sized projects in Python.

##### Share on other sites
Quote:
Original post by Oluseyi
Quote:
 Original post by GlakOk but now here is the catch: pointers are tricky and yet so called easy languages (C#, java) expose you to them early. I don't remember whether Python does but I'm almost certain that it does.

Wrong and wrong. Java has no pointer types; everything is a reference.

I never understood why people think references are so much safer than pointers.
Pointers and references share the same feature: data->modify() may have effects you did not intend.
Sure, C/C++ pointers offer a whole other category of problems, but it's been several years since they hit me.
That is, unless you include memory leaks and double deletes, but that falls under the category of memory management, not so much pointers.

Btw, "everything"? Event basic types such as int?

Quote:

Quote:
 My biggest objection to Python is that it does not have compile-time type checking. Compile-time type checking allows the compiler to automatically find a whole category of errors for you.

a.) Not really. Compile time type checking in a language like C++ is undermined by the fact that type coercion is trivial.
b.) There are dynamic languages (Python is a dynamic language) that have static checking.

Re a.) Yet the C++ compiler will consistently find lots of "thinkos" in my code thanks to the "strict" typing system. A write strict in quotes because clearly C++ allows you to work-around the typing system, but that does not mean its typing system is completely useless.
Re b.) How does that help the programmer who's using Python? The fact there is a static type checker for e.g. Erlang is of little help if Python does not have it, and you are using Python.

Quote:

Quote:
 When I program in C++ I never have to deal with that category of error. The compiler just finds it for me and I simply fix it.

The compiler is useful to determine syntax errors, but it doesn't help in the slightest with logic errors.

There is a whole category of errors between syntax errors and logic errors that a static typing system will catch. I mean: my_vector_of_ints.push_back(a_string) is syntactically correct, but the compiler will flag it as an error, which it should do. I would consider that a language without a compiler catching this kind of errors (e.g. /bin/sh) is restricted to very small programs.

Quote:

Quote:
 So I would go with C++ if I were you. It really isn't that hard if you learn it correctly.

Don't take this the wrong way, but I'm not sure you've learned it correctly.

Even though I've defended C++, I would advise against using it. The reason: When you program fails to do what you want (and that will happen whatever language you use), if you are using C++ you will have to use the debugger to find out what's wrong, which requires detailed knowledge of all C features you don't want to know about. What's worse, there is not a single debugger that I know of that offers decent support for C++.
If you are using an interpreted language, a bunch of "print my_data" is all you need.

##### Share on other sites
C++ is more better

##### Share on other sites
Quote:
 Sure, C/C++ pointers offer a whole other category of problems, but it's been several years since they hit me.That is, unless you include memory leaks and double deletes, but that falls under the category of memory management, not so much pointers.

Quote:
 here is a whole category of errors between syntax errors and logic errors that a static typing system will catch. I mean: my_vector_of_ints.push_back(a_string) is syntactically correct, but the compiler will flag it as an error, which it should do. I would consider that a language without a compiler catching this kind of errors (e.g. /bin/sh) is restricted to very small programs.

Ok, so let me get this straight. The category of problems about pointers, it's been several years since they hit you. But the category of notoriously stupid mistakes(I won't even call them problems) like "my_vector_of_ints.push_back(a_string)", that you worry about. Well, let me, in turn, honestly say that these kind of mistakes have been years since they hit me.

See, we better drop this language comparison because it will lead nowhere. In the same vein, I could say that a language that doesn't natively support such an important structure like arrays with proper bounds checking is "restricted to very small programs". After all, which happens more often? Trying to put a string into an array of ints, or going out of bounds?(and after all, Python does have type-safe arrays, using the array module, just like C++ does support bounds checking with std::vector).

Obviously, every language has its cons and pros. And everyone will try to "hide" the cons of his favourite language, and make the cons of the "opponent" seem huge. It's kind of "if language A doesn't have feature X,it's useless". For example,static/dynamic typing. If one wants to make dynamic typing look bad, he will give all sorts of examples with similar mistakes like the above, trying to give the impression of a large project collapsing because of wrong types passed into functions everywhere like crazy. The truth is, it rarely if even happen with smart programmers(The few times it happened to me, I corrected it on the spot, and I don't claim to be that smart). On the other hand, if the programmer is not smart, then he will do even more dangerous things in C++.

I just want to say that Python, like C++, is of course proven to not be restricted to "very small programs", and the general concensus(and my own experience with both Python and C++) is that the time needed to write and debug a Python program is much smaller than the time needed to write and debug a similar C++ program. That's why Python is used after all.

[Edited by - mikeman on March 3, 2007 4:16:10 AM]

##### Share on other sites
Original post by Oluseyi

"So, no, you're wrong. Pointers and C-style arrays are primitive features, not advanced."

You are right that they are primative features. I am right that they are advanced features. In C++ the proper way to learn is to start with higher level features and works down towards the primative features.

"A language that makes a simple array - a linear list! - a complex, "advanced" feature is an obstacle to learning. Incidentally, C++ introduces std::vector and other container types because they are such data structures."

It is unfortunate that arrays in C++ are complex and that vector is so poorly named. I have said that C++ was without flaws.

"Wrong and wrong. Java has no pointer types; everything is a reference. C# requires you to use unsafe blocks - an advanced feature - to access pointers. Python only has references to values, too."

You know exactly what I mean. Java references are not aliases. They are values. Compare with C++, where references are aliases and pointers are values. The java reference has far more in common with the C++ pointer than it does with the C++ reference

"And this confirms that you don't know what pointers are, because they are intrinsic to C++ programming."

And this confirms that you don't know how to read.

"Not really. Compile time type checking in a language like C++ is undermined by the fact that type coercion is trivial."

I suppose that it can be a problem for some; it has never bothered me.

"The compiler is useful to determine syntax errors, but it doesn't help in the slightest with logic errors."

I make few logic errors and those that I do make I quickly find with testing. I suppose different people make different mistakes.

"Did you know that you never have string literal errors in Python? For two reasons. First, strings are constant. All strings, no matter what. They are never, ever mutable. Ever. Second, strings are a language intrinsic type, so they always behave as expected. You can call all string methods on literals without having to obtain a reference to an instance."

no that is not the kind that I make. I make typographical errors. In an expression a typographical error is usually a syntax error. A typographical error in a string can be hard to find. For example in my current project (a compiler for a language of my own design) I had the following code:

Token BOX("special","[]");
Token GROUP("special","{}");
Token BLOCK("special","{}");

See how the second and third are both the same? The thing is that originally I didn't have these tokens defined so I was doing like this:

if(e->token==Token("special","())

then I decided to pull all of those out to this:

if(e->token==GROUP)

So that I would have fewer string literals in my code so that I would have fewer errors. However when I pulled them all out there was no error at first and it was hard to track it down to the typo in the constructor for GROUP.

"Don't take this the wrong way, but I'm not sure you've learned it correctly."

ummm... let's see my current project is a compiler for a new programming language. This involves manipulating a syntax tree which means that I am doing a lot of pointer stuff. Also I have a directed graph for each function that I'll be using for code generation. Here is some code where I post process the parse tree before moving on to semantic analysis. There is now way that I could have written this if I did not understand pointers.

void RestructureFunctions(Expression* e){	if(!e)		return;	if //standard function	(		e->token==ADJ &&		e->left->token==ADJ &&		e->left->left->token==ARROW &&		e->right->token==BLOCK	)	{		e->right->left=e->left->left->left;		e->right->right=e->left->left->right;		Expression* p;		p=e->left->left->left;		e->left->left->left=ExtractType(p);		p=e->left->left->right;		e->left->left->right=ExtractType(p);		RestructureFunctions(e->right->contents);		return;	}	if //implicit function (block)	(		e->token==BLOCK &&		!(e->left) &&		!(e->right)	)	{		Expression* e2 = new Expression(MAKE_EQUAL);		e2->right=e->contents;		e2->left= new Expression(Token("identifier","__return_value"));		e->contents=e2;		e->left=new Expression(EMPTYGROUP);		e->right= new Expression(ADJACENCY);		e->right->right = new Expression(Token("identifier","__return_value"));		e->right->left = new Expression(TYPEOF); //creates cycle!		e->right->left->contents=e->contents->right;		RestructureFunctions(e->contents);		return;	}	RestructureFunctions(e->left);	RestructureFunctions(e->right);	RestructureFunctions(e->contents);}