Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Which programming language to learn?

  1. Jul 2, 2007 #1
    Which programming language to learn???

    Hi there,
    I've decided to start teaching myself a programming language and it was recommended to me by two people both in the field I'm getting into (astrophysics) to learn either C or Fortran but preferably C. I was told C rather than C++ because C++ is object oriented and I don't need to worry about that for what I'll need to be doing.

    I was also talking to a fellow who does computer science and he told me that C is good and all but I shouldn't be wasting my time with learning that now and i should be learning perl and/or python because they're more efficient in some manners.

    If anyone doing physics or astronomy has any advice on this matter it would be greatly appreciated.
  2. jcsd
  3. Jul 2, 2007 #2
    It depends on why you want to learn the language. If it's to run simulations, I would think there are a zillion software packages available that run astrophysical simulations. You just need to learn them instead of a programming language. If it's to design your own software package, then i have no clue which would be easiest.
  4. Jul 2, 2007 #3


    User Avatar
    Science Advisor
    Gold Member

    I would suggest you teach yourself both C and python.

    Python is a good learning and prototyping language where you can work out the basic algorithms. It is easy to write good code with few (easily detected) bugs, develop good habits, and also get a good sense of object oriented programming if you want to learn that. Python has excellent debugging feedback when errors occur!

    It is also pretty fast for an interpreted language, has unlimited integer arithmetic (so you could e.g. calculate 100! and it will print out all the digits)
    and many useful built in data structures. It is also an open standard so you can download good useful interpreters and code is pretty well interchangable between systems. You can also download many packages which can handle big numerical calculations by loading modules written in C.

    If you actually want to crunch some numbers, you will want to compile C programs. Short of delving into assembly you'll find the fastest code is written in C. Indeed it is the standard compiled language of Unix/Linux operating systems and most all the non-assembly coded programming is done in C.

    Once you've mastered say Python for simple or objected oriented programming and C for hard-core work you will find it easier to translate code into other languages (with the language manual in hand).

    I also think the syntax of Python and C are such that you can begin learning both at the same time without confusing the two styles.

    My advice is start with Python. Then learn C once you have learn the basics of data structures. As an exercise you would try to implement in C some of the built-in data structures of Python.
  5. Jul 2, 2007 #4
    you will soon find that some languages are better suited for some tasks than others. However, whoever told you that you don't need to bother with C++ because it's an OO language is very short sighted (IMO). You will find it very difficult to get away from anything recent that isn't OO or has some OO characteristics.

    These are the ones that I would put on the list
    Shell scripting (if you plan on being in a *nix environment)

    I wouldn't bother trying to learn them all at once. Just learn them as you need them.
  6. Jul 2, 2007 #5
    as a computational science student(my supervisor is in astrophysics) I suggest starting with C...it has probably the highest learning curve out of any language(besides asm)...and IMO its best to start early one of the key things to learn in C is memory and bit manipulations ( printf("%p",pointer); is your friend)

    One question u will have to ask yourself is what do you see yourself doing in the future. IF it has anything to do with HPC(high performance computing/parallel computing) you shouhld really start with see and code anything (math and physics concepts ) that come along your way. ALso if it has anything to do with visualization or physics in games...stick to C/C++.

    Can't say much about python but i'm learning it currently(well the c component of python) but i can say that python in my app is only used as a scripting language ontop of a C/C++ driven engine. That is it allows the engine to execute runtime commands for visualization but not for the simulation itself. But as said in the above post its a nice language to write simple short code but that can be said for any language like matlab/maple.
    Even C/C++ is short code if you know where to look for the libraries. Cygwin is a good place to start but sadly my preference is MSVC# compiler and IDE.

    As for C and C++ well charm++ is written in C++ and it drives some of the most sophisticated molecular dynamics and astrophysics sims. But make sure along the way you learn some type of neetworking protocol something i didn't do till my masters(mpi is a good place to start). The only benefit C++ has to C is inheritance but i'm pretty sure you can fake inheritance through void*. I personally don't like pure OO and don't see the point to it(neither does my supervisor) so you should look to do a OO/functional hybrid.

    i would have to disagree with the post above that you should start with python to look through their data structures before converting them to C
    becuase you may still come across the many difficults of learning pointers.
    There's a good gaming book written by an 18 year old about data structures and he give explicit code examples(primatech series if i remmeber correctly).

    also your standard university textbooks like...crap i can't remmeber the author or name it comes in 5 parts in C/pascal and i think now in java.
    Last edited: Jul 2, 2007
  7. Jul 2, 2007 #6


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    I disagree with your constant assessment that Python is a "learning and prototyping language." It's as fast as C for many operations, it's extremely easy to learn and maintain, and it has an enormous standard library on par with more mainstream languages like Java.

    Well-written Python programs spend most of their time in the underlying highly-optimized C implementation. Python is at least as fast as C for most scientific applications, and is frequently faster for novice users who tend to write rather poor C code. In fact, Python has a commanding presence in the world of scientific computation, for good reasons. It's completely disingenuous to describe it as a teaching language or as being unfit for doing "real" computations.

    My advice is to learn Python and MATLAB or Mathematica thoroughly. I suggest simply learning C well enough to be "dangerous," i.e. able to read other peoples' code and make small modifications. I wouldn't even attempt to write a complete numerical code in C unless you find out that you have to, for some reason or another.

    - Warren
    Last edited: Jul 2, 2007
  8. Jul 2, 2007 #7


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

  9. Jul 2, 2007 #8

    Dr Transport

    User Avatar
    Science Advisor
    Gold Member

    I would say that if you know or have some knowledge of C, learning Matlab is a snap and vice versa. I know C and picked up Matlab in about 2 days. A friend of mine did the opposite and learned C after working with Matlab in less than a month. The major difference is that Matlab has a butt-load of intrinsic functions that C doesn't. I'd learn Matlab as fast as I could then go on and learn other laguages to add in other functionality (Matlab has the ability to read C and Fortran routines).
  10. Jul 3, 2007 #9


    User Avatar
    Homework Helper

    I'd also recommend MatLab. Think of it as a tool more than a programming language. You should also find out what the language of choice is in your inteneded environment. Fortran is still popular in some niches of the scientific world (for example the Cray computers Fortran compiler generates faster code than their C compilers, some of this due to the extensions to the language and the effort made by Cray on Fortan versus C, an apparent indication of the relative popularity of Fortran versus C on their machines).
  11. Jul 3, 2007 #10
    I'd stay away from matlab as your first language because it has too many things/styles that you could get use too when moving to another langauge like the one function per file or its array and vector notations (the array notations are the same as in fortran)

    Personally i think the pointers in C/C++ is the hardest concept to grasp in programming (besides asm coding and hardware optimization like utilizing cache) ...as a student you should take the time to learn the hardest stuff when you have the time. And as transport said you can grasp matlab faster than C when learning the other language. ALso if you plan to do any advance graphics with those numerical simulations...too my knowledge matlab isn't very good at it though they have a parallel toolbox.

    Out of curiosity for all the other posters what order have you learned these language and used them.

    edit: LOL my bad, forgot to erase what was below cuz i moved it to another thread...
    Jeff reid, if you can do the same ...its in the programming subforum. entitled something like
    "when did you learn what language..."
    Last edited: Jul 3, 2007
  12. Jul 3, 2007 #11


    User Avatar
    Homework Helper

    Depends if the goal is to use a tool to help with school, or to learn a programming language. Matlab is going to be much more useful, in a shorter period of time for quick projects.

    I've never used MatLab myself, but I have co-workers who swear by it.
    Last edited: Jul 4, 2007
  13. Jul 3, 2007 #12
    learn ksh

    Its the most useful, well to me it is... But Im a network engineer not a coder :)
  14. Jul 3, 2007 #13
    Thanks much to all for the advice. I'm not really sure to what ends I will need to be programming (I'm just starting grad school for astrophysics in the fall), but I've already started plodding through on C, and will go at that for a bit and then try out some python.

    I was also wondering how much programming skills the average physics student has when coming into grad school. Just out of curiosity. I've always had the impression that my undergrad school physics dept. didn't focus anywhere near enough on the computational side of things but I really have nothing to compare it to. There's one required computer science class but it was a while ago and I barely remember it now, and in calc 3 we use matlab a bit and that's it really.

    Thanks again.
  15. Jul 3, 2007 #14
    depends if your starting grad school look at the core courses and perhaps talk to your supervisor. It also dependson the area of astrophysics your going to.
  16. Jul 3, 2007 #15
    Although I can't claim to be able to use them with any significant amount of proficiency: (enough to cobble together small little programs that suit my needs)

    *Applesoft BASIC
    *Objective C
    *A smattering of Perl
  17. Jul 4, 2007 #16

    Dr Transport

    User Avatar
    Science Advisor
    Gold Member

    Matlab may b e a tool, but it's sintax is very much like C, matter a fact I've heard it called "C-light". Matlab has almost all of the perks of C and the functinoality to be useful right away. The best part about it is that although not a compiled language, it is optimized for running matrix computations and you don't need to program loops to do those types of operations, something you'll never see in C.
  18. Jul 4, 2007 #17
    "it is optimized for running matrix computations and you don't need to program loops to do those types of operations, something you'll never see in C"

    but can't you just make BLAS calls and avoid the loops yourself?
    syntax may be similar to both C and F but its file layout IMO is horrid...only one global function per file? And the file name has to be the same as the function name IMO is horrid for a coder.
  19. Jul 4, 2007 #18


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    MATLAB's syntax, in my opinion, bears no resemblance at all to C. Nor do its features.

    - Warren
  20. Jul 7, 2007 #19
    I think he should learn Visual Basic. It's really easy.

    Ok, that was a joke, sorry I couldn't help it.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Which programming language to learn?