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

Beginner Language

  1. Jul 8, 2015 #1

    EJC

    User Avatar

    I'm a physics major and would like to pick up a programming language or two.

    Any insight as to what language(s) would be most applicable, best compilers (for Windows), or any other general tips?

    I'm very new to programming (just used MATLAB really), so please explain your answers as much as possible for a complete beginner to understand them.
     
  2. jcsd
  3. Jul 8, 2015 #2

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    1) Don't underestimate MATLAB as a computer language. A lot of engineers do all their analysis in MATLAB.
    2) A lot of very good programmers love Python, especially for scientific / physics computations. And it is free.
    3) C and C++ are the most prevalent languages. And they are the jumping-off point for a lot of current languages (Java, C#, etc.)
     
  4. Jul 9, 2015 #3

    Boing3000

    User Avatar
    Gold Member

    That depends a lot of what you would like to do. Will this programming be be scientific related ? Will it be distributed in small circle or cross platform ? Is performance going to be an issue or not ?
    Any hints would be welcomed :wink:
     
  5. Jul 9, 2015 #4

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    Agree. For instance, there is a big speed difference for the ones targeted for physics calculations. MATLAB is SLOW (maybe 100 times slower than C). Python is slow (maybe 15 times slower than C). C and C++ are about as fast. as you can get.
     
  6. Jul 9, 2015 #5
    I agree with the Python suggestion very much...it is a great whole ecosystem with modules for whatever you want.

    AND, I am, of course, going to bring up (modern) Fortran...if you could pick up Fortran 90, that would be very handy...in your field, I am thinking you are going to run into Fortran sooner than later. Fortran is great for keeping you from memory leaks, it good for high performance computing, parallel processing, etc. It handles arrays a-la-matlab, has native complex numbers, etc. Fortran continues to be relevant for a bunch of reasons.
     
  7. Jul 9, 2015 #6

    EJC

    User Avatar

    Yeah, I read about the varying computational speeds recently. MATLAB is pretty straightforward with it's syntax, and I've heard C is too, but not quite as easy as MATLAB.

    In your opinions, is the tradeoff between learning C and the fact that it is probably slightly more difficult at first than MATLAB worth converting entirely to C?

    Thanks. Can you elaborate on how Fortran and Python differ from languages like C and MATLAB. I am very green on the subject.
     
  8. Jul 9, 2015 #7

    EJC

    User Avatar

    What I believe would be really useful to me is a description of the popular languages and an explanation of the differences between them, and what each language handles best/worst.
     
  9. Jul 9, 2015 #8

    jtbell

    User Avatar

    Staff: Mentor

    The question "which programming language?" gets discussed rather often here, and there are a lot of opinions. I suggest you try some of the "Similar discussions" listed at the bottom of this page, and likewise at the bottom of those pages.

    A Google search for "comparison of programming languages" turns up some pages which look promising for general comparisons of features etc.

    My personal opinion is that so long as you choose a general-purpose language that is widely used and that you can easily find resources for (compilers, tutorials, textbooks, etc.), and you don't have a specific application in mind initially, it doesn't make a lot of difference which language you start with. Just pick one and focus on general programming concepts: variables, data types, loops, selection (if-statements), functions, subroutines, objects, etc.; and on general principles of program design. If you do a lot of programming, you will learn several different languages during your school and working career. The general concepts and principles apply to all of them.

    I think either C++ or Python would be a reasonable start. I taught introductory programming in C++ for several years. I've never used Python, but I've read a lot of good things about it, and it's on my list of things to learn after I retire in a couple of years. Others will make reasonable cases for other languages.
     
    Last edited: Jul 9, 2015
  10. Jul 9, 2015 #9

    OldEngr63

    User Avatar
    Gold Member

    At the risk of being thrown out of polite society, I'm going to strongly recommend BASIC (Beginners All-purpose Symbolic Instruction Code). There is an excellent BASIC available from True BASIC, a revision of the original by the authors of the original, Kemmeny and Kurtz. It is easy to write fully formatted code (no GOTOs, unless you want to use them, no line numbers, unless you want to use them).

    People worry much about how fast a program is, and from time to time this is relevant, but often it is not. Do you really care whether the results appear on your screen instantly, or an eye blink later? Does this really matter to you?

    I'll tell you what matters to me: How much effort do I have to put into developing the code in order to get the computer to do the heavy lifting? I have found over the past 30 years that I can generate correct, working code by far faster in BASIC than in any other language. Sure it is SLOW, but not nearly as slow as I, a mere human, am. It is so SLOW, that sometimes, I actually have time for sip of soda before I see results; just completely intolerable. I have used BASIC to solve literally thousands of engineering problems including elaborate simulations and eigensolutions.

    I never use GOTOs, but I do use line number infrequently. I use them mostly when I intend to publish a block of code, and want to be able to discuss the code. Line numbers (which can be generated automatically with any interval you want) are very handy for this purpose.

    I do not recommend BASIC for real time applications where speed is truly important, but for anything else, it is BASICally very simple.
     
  11. Jul 9, 2015 #10
    Well, I am not going to throw OldEngr63 out, but I will certainly vote against Basic...it just does not have the ecosystem, development and popularity that Python has. Python has risen past several other early 90's languages (tcl, Java, Perl) to become not only the scripting language of choice for many open source programs but also for commercial ones and even the language many open source programs are written in.

    If you learn Python, you will also know how to develop and do scripting for many other programs.

    Like Matlab, Python (numpy, scipy, matplotlib) has just about every toolbox (module) you may need for specific tasks like physics, linear algebra, graph theory, networking, molecular biology, optimization, visualization, etc.

    As far as Python's similarity to Matlab is concerned, there are introductory Python pages out there for Matlab users:
    http://sebastianraschka.com/Articles/2014_matlab_vs_numpy.html [Broken]
    Python for Matlab Users
    Python vs Matlab
    Numpy for Matlab Users

    (I have been using Fortran for sometime and not C/C++, thus, I will not comment on the latter, needless to say, they are also very popular and powerful choices)

    Modern Fortran (90 and beyond) has taken quite a leap from Fortran77...it is very friendly, handles arrays a-la-matlab, has modules, derived types, interfaces, overloading, encapsulation, (even object oriented paradigm starting on Fortran2003) and a huge amount of Fortran code out there that you may inherit.

    Fortran is also very compatible and easily called from Python (f2py)...when Python is used as a "glue" language. Here is a page comparing Python and Fortran.
     
    Last edited by a moderator: May 7, 2017
  12. Jul 9, 2015 #11
    Excel has an embedded Basis called VBA. It might be a good place to start, as you learn the basic programming structures common to all (or most?) languages. It's already loaded if you have Excel.
     
  13. Jul 9, 2015 #12

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    C and MATLAB make a good combination. It's nice to know both. You can develop algorithms in MATLAB and rewrite in C when there is a reason to. There are often reasons to rewrite in C. It's harder to get all the calculations correct in C, so you can check it by comparing with MATLAB results. I also think it's a good idea to put comments in the C code that are the MATLAB lines I am trying to implement. Almost half the people I work with are primarily MATLAB programmers and the other half are primarily C programmers (but that is not a typical situation)
     
    Last edited: Jul 9, 2015
  14. Jul 9, 2015 #13
    Python is what we teach as a first language in Computer Science and Electrical Engineering here. Yes it is slower in execution than the compiled languages but the development time is usually much shorter. It also depends on your aim, proof of concept problems like shorter development times. Matlab is next in Engineering, although Python with Numpy, Scipy and Sympy are pretty powerful.
     
  15. Jul 11, 2015 #14
    I give a second vote for basic as a beginner language, but just enable IIS web server, embed it in a .asp file and pull it up with your web browser. Unless you really need the speed for heavy computations, it is so much easier to work with a language that does not need to be compiled. What I like about basic is that the routines you learn are very portable between different languages such as PHP, BASH, ect.
     
  16. Jul 11, 2015 #15

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    I second the comments that recommend a scripting language like Python or Basic (I like Perl). I earlier recommended the combination of MATLAB and C/C++. You are already familiar with MATLAB, but licenses are expensive and many computers will not have it. C/C++ are established standards, but are harder to develop in. So in between MATLAB and C/C++, it is nice to know an easily scripted language that is free and/or universally available. Python and Basic are like that. So I recommend 3 across the spectrum: MATLAB, Python/Basic, C/C++. Sorry if that seems like a cop-out, but that's the way a programmer's life is: more languages than you know what to do with. All of them good for something.
     
  17. Jul 11, 2015 #16

    NascentOxygen

    User Avatar

    Staff: Mentor

    If you were planning to start as a beginner and build up to devising your own programs from scratch, I think Pascal is a good first language because it enforces good habits. But does anyone teach Pascal any more??

    Most likely, in the work setting you will find yourself taking someone else's program and just modifying it a bit to fit your purposes, and for this I recommend Python. It is easy to comprehend a Python program, to spot what section needs changing in order to accomplish what you seek, and the range of functions available in Python and its specialised add-on libraries is awesome. (Daunting, almost.)
     
  18. Jul 11, 2015 #17

    wle

    User Avatar

    That's difficult to do fairly and comprehensively. There are a lot of popular (and perfectly good less popular) programming languages and a lot of things that differentiate them. Preference for certain programming languages can also be a matter of opinion, which can vary wildly (e.g. C++ can be an extremely powerful and flexible super language or a brittle, fractally complicated swamp of misfeatures, depending on who you ask). Your own preferences, interest, and motivation also matter, e.g. do you want to learn something immediately useful for a project, or do you think programming is interesting to learn for its own sake and you're willing to develop it as a skill over several years?

    That said, many people here are suggesting you start with Python. I think that's probably as good a first language as any. I don't know Python terribly well personally, but from what I've seen there are several things that make it a good default choice as a first language, particularly for a physics student:
    • It's interpreted and, more importantly, supports an interactive interpreter. This is great for learning by experimentation. You can type example code into the REPL and see immediately what it does. Some people use Python (with additional libraries) as a Matlab replacement this way.
    • It's got good support for scientific and mathematical computing. Arbitrary size integers, exact rationals, real, and complex numbers are built into the language. There are addon, commonly used libraries for numerical coding, linear algebra, etc. (NumPy and SciPy), plotting (matplotlib), and symbolic computing (SymPy), among others.
    • It's got a large, friendly community of users and developers. This means lots of support in the form of online help, tutorials, and libraries.
    • It's free and maintained as an open source project. You don't have to worry about a commercial company developing it going out of business. You're not dependent on your university or company paying expensive license fees and your code won't become useless if your student/work environment changes.
    • It's multi-platform. If you buy a Mac or switch to Linux in 2 years, you can take your Python code with you. You can share code with friends and colleagues who use different operating systems (this is quite important in a field like physics where not everyone uses Windows).
    • It's multi-paradigm. It supports at least imperative, object-oriented, and to some extent functional-style programming, without being ideological about them. So you can start learning in the usual imperative style and (optionally) later learn the other styles Python supports (and what they're useful for) when you're ready.
    • Python is implemented in C so, naturally, it provides support for interoperating with C code. (May be useful and educational when you reach a certain level of experience.)
    • Overall, Python is a good, well-designed, relatively modern, general-purpose programming language with good built-in types and data structures (strings, lists, hash tables). As long as you don't have some specific requirement (like efficiency), you can probably get whatever you want done reasonably easily in Python.

    I don't mean for this to read like an endorsement for Python over all other languages (like I said, I haven't even used Python myself that much). Most of these points aren't unique to Python, and there are sometimes good reasons for some of them not to apply to another language. But if you just want to start coding and don't want to worry too much about language choice, it seems like a reasonable place to start.
     
    Last edited: Jul 11, 2015
  19. Jul 12, 2015 #18
  20. Jul 14, 2015 #19
    I use Cran R & C for what ever I need. R & C will load in anything. Then R will take care of loading anything else it needs. Neither one are too sharp on user interfaces. I can hack together something in Excel or Open Office Spread Sheet if I have to and export it as a CSV or XLS file to R or get some one to write something for me in Java, C++, Visual Basic or something and we can interface it to R.

    R will use code from Fortran, Java, C, C++, BASH Shell, PEARL, Python Windows OS, its native language LISP and more. It's LISP like in it requires more parentheses in mathematical expressions than most languages.

    I like it because the same code is portable among Windows, Linux and Unix. The calls to the OS are smoothly integrated in the program. In fact all the calls are smoothly integrated once the libraries are called.

    GC.
     
  21. Jul 14, 2015 #20

    EJC

    User Avatar

    I think based on the sheer number of people vouching for C and Python, and based on my specific needs, those will be where I start. With that being said, more input on compilers and their pros and cons would be useful and appreciated :oldsmile:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook