Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I've read here on P.SE more questions in which beginner programmers are advised to pick Python as their first programming language.

Don't get me wrong, I like Python. I like it a lot! But its philosophy revolves around "We are all consenting adults here".

From an experience/knowledge point of view, a beginner programmer is not an adult. Which then kinda means it is easier to shoot yourself in the foot and pick up some bad habits even if you survive the wound.

I'm thinking that in a "more static" language it would be harder to shoot yourself in the foot as it will be more restrictive.

Back to my question. Why is Python recommended as an entry level programming language?

What are the points that make it good for teaching a programming language. Or... is it personal preference of the adviser?

share|improve this question
14  
I'm thinking that in a "more static" language it would be harder to shoot yourself in the foot - well, C and C++ are statically typed and you would be badly, badly surprised to see how many different ways you can blow your foot off using them... –  Péter Török Dec 13 '11 at 12:30
    
@Péter Török: Yes, and it takes your entire foot away (c++ :D). I said "more static" trying to avoid flame wars for something like... I don't know... Java?!?!... which everybody seems to hate as it is thought at school/universities and people have some scares from it... –  JohnDoDo Dec 13 '11 at 12:35
1  
I think C++ is a better starting language, because it has helped me to better understand things in other lanaguages. –  systemovich Dec 13 '11 at 13:29
1  
I have my beefs with python as a starting language after TAing a first semester programming class that used it. Students oft didn't understand 'types' and the whitespace dependencies killed them. They also had a hard time understanding objects with that 'self' referencing kludged OO business. It has it's merits but it definitely has it's detractors too. –  Rig Dec 13 '11 at 16:49
2  
@MahmoudHossam I agree. I was just highlighting some of the sticking points from my experience. Java, C#, Lisp, etc all have their issues. Those were just the major issues I encountered teaching python to first time programmers. –  Rig Dec 13 '11 at 18:48

11 Answers 11

up vote 28 down vote accepted

IMO, the most prominent points that speak for Python as an entry-level language are these:

  • it has a shallow learning curve - going from nothing to "Hello world" is much faster than in most other languages
  • it is intuitive - the syntax was designed to follow the principle of least surprise, and it is very consistent overall (unfortunately, the standard libraries don't always follow this consistency)
  • it requires very little boilerplate: a typical "Hello world" is one line of code, and simple programs can be written without any additional background noise that needs to be explained (such as function declaration keywords, import statements, class constructs, preprocessor directives, etc.)
  • there are excellent, straightforward tools to work with python code, especially the interactive interpreter; you don't need to learn a build system, IDE, special text editor, or anything else to start using python - a command prompt, the interactive editor, and a simple text editor, are all you need
  • it uses dynamic typing, but unlike many other dynamically-typed languages, types are transparent, and type-related pitfalls are rare
share|improve this answer
    
Just want to emphasize the intuitiveness of the language. I've seen people with 0 programming experience quickly learn Python. This is largely due to the syntax/grammar being very close to spoken English. –  Stephen Gross Dec 13 '11 at 16:25
11  
At you last point: people often mix up dinamic typing with weak typing. Python is dynamically typed (types associated to values instead of variables) but is also strongly typed (so programs crash instead of silently swallowing conversion errors) –  hugomg Dec 13 '11 at 18:46
    
@missingo I second - Eagle eyes you have ;) –  yati sagade Dec 15 '11 at 18:01
    
@missingno: I know the difference. An example of non-transparent types is PHP's array keys - arrays behave differently for integer keys than for any other type; if you add two sufficiently large integers, you may end up with a float. And that's just one example - especially PHP and javascript (the two most popular dynamic languages in web development) are full of obscure typing edge cases. –  tdammers Dec 16 '11 at 7:29
    
Judging from your comment, by "types are transparent" you mean the rules that govern types are easy to understand? What exactly did you intend to say? –  phant0m Mar 11 '13 at 19:35

The hello world in python:

 print "Hi there"

hello world in Java:

 class HelloWorldApp {
  public static void main(String[] args) {
    System.out.println("Hello World!"); // Display the string.
    }
  }

hello world in C:

  #include<stdio.h>
  int main(int argc, char** argv)
  {
    printf("Hello World");
   }

Throw in having to talk about compilation for the other two, and python is much simpler. I only have to talk about the single idea I am interested in looking at, all of the machinery that complicates the matter just disappears in python. I can wait until they are ready to talk about modules, no need to rush it to get stdio.h for simple programs. I can wait until they are ready for topics like classes no need to rush them for their first programs. It has a REPL for fooling around at the command line. Python is very good at being conceptually minimal. This is a good thing because it helps beginners focus on the task at hand.

share|improve this answer
12  
You forgot the "return 0;" statement in your C implementation :) Compiler warning! –  Stephen Gross Dec 13 '11 at 16:29
2  
This is why BASIC existed in the first place. 10 PRINT "Hello World!" 20 END For extra credit, the Python program fails the "Did it say 'Hello World!'?" unit test :-) –  Ross Patterson Dec 14 '11 at 1:34
2  
Same in F#: printfn "Hello World!". Yay, F# is good for beginners! –  Den Mar 12 '13 at 9:55

That's an unusual viewpoint.

The consenting adults thing is a very small part of Python's philosophy. It certainly doesn't "revolve" around that. It's not even part of the "Zen of Python" that you get if you do import this in the interpreter.

Other things, like "explicit is better than implicit", "Simple is better than complex", and "Readability counts" are much much more central to Python, and show why Python is a good first language.

Anyway, the "consenting adults" is really only about the lack of private objects in Python. Which itself is, in my opinion, another indication of why it's good for beginners: you don't need to worry about things like interfaces when you're just starting out.

share|improve this answer
1  
Yes, but about those "private" parts, since you have access to them, you might be tempted to hack some workarounds/shortcuts around them not knowing better. –  JohnDoDo Dec 13 '11 at 11:52
    
@JohnDoDo What difference does it make if, in C# or Java, I decided to convert private fields to public to hack around? BTW, in Python, you can prefix the property with an underscore to make it private (though not really hidden) –  OnesimusUnbound Mar 12 '13 at 6:45

There are two different few points. When raising a child, should you put them in a bubble that only has safe things in it or let them play in the yard where they might trip and fall?

Being inside the bubble is very restrictive. You have to write your code in a very specific way in order for it to run. When people are first starting out, anything that isn't there to do what the person wants is seen as wasteful. This can also lead to "I don't know why that is there, but someone told me it's necessary."

In Python, that doesn't exist. If you want to say "Hello World", you just print "Hello World". Using Python as an entry point allows someone to hit the ground running and just write the code they need to make what they want happen. A first time programmer can not understand the value of encapsulation before they understand the basics of programming.

Also, in small programs, type safety is not a large problem. Any code a beginning developer writes is going to be small and contained. It is not until you have a larger code-base, with lots of sections that you use as abstraction layers and don't actively think about, when type safety shows it's benefits of ensuring you do what you need to. When all the code is in the same file, it is easier to just go look at the function and see what you did wrong.

Other benefits:

  • Lots of libraries that do lots of things
  • Flexibility to teach both Object Oriented and Functional programing techniques without making it seem like one was forced to fit into a language designed to do the other.
share|improve this answer
3  
Counter analogy: Teaching your kid about not playing with fire. 1) You can talk to him, explain what fire is and what fire does, show PowerPoint slides whatever.. or 2) let him play with fire and get burned. The second method is far more efficient indeed. What I'm interested about is the possibility that he might set the house on fire or even like it and become a pyromaniac because no safety measures were taken. Kind of an extreme situation but can't really express it in words. –  JohnDoDo Dec 13 '11 at 12:33
1  
@JohnDoDo - So let him get burned ... it's the best school and who didn't get burned on a match as a kid? It's not gonna kill him ... –  ldigas Dec 13 '11 at 13:07
1  
@JohnDoDo: Just because the language lets you play with fire doesn't mean the teacher doesn't point out that fire is very dangerous and explain some way to avoid it. I think the other thing you are missing is that someone is not tied to a language. After gaining a basic understanding of programing, they can use a different language to learn bigger concepts (like not burning down the building). –  unholysampler Dec 13 '11 at 14:24

I see Python as a good choice. Typically for "entry" level programming you want something simple yet productive. Receiving quick feedbacks supports both motivation and speed of learning. I dare to say it's not about learning the "right way" but rather "getting hooked and falling in love with programming for life". The rest will come along nicely later through experience, secondary education, whatever.

share|improve this answer

Python is a great language for beginners because it makes simple things simple while making enough complicated things possible that it isn't be regarded as a useless toy language and even advanced programmers find it to be the right tool for some jobs. Specifically, simple things include:

  • An absolute minimum of boilerplate code for simple programs.

  • Garbage collection.

  • Dynamic typing.

  • Clean, simple syntax.

Compare that to e.g. Java or C++ and the difficulty of explaining all the concepts involved in a "Hello, world" program. Complicated things include:

  • Introspection (Python's equivalent to reflection).

  • Monkey patching.

  • The things that can be done with the huge standard library.

That said, noone who only knows Python is a good programmer because noone who only knows any one language or one style of language is a good programmer. Anyone serious about programming should eventually learn something lower-level, just not while he/she is still trying to grok flow control, recursion and other basics.

share|improve this answer

IMHO, teaching people to code in "safe" languages is a very bad idea. People assume everything is easy, and only think about adding layers and layers of code. And loosing any chance of being able to debug/fix problems when they arise.

Good programmers absolutely must know C/C++ or even assembly. And their mindset has to come from that side as well. And not - "Oh, GC, it does Magic(tm) we are cool, and why C++ doesn't have GC, it's stupid." attitude.

share|improve this answer

For a beginner programmer, lack of static types is a feature, not a bug!

When learning to program even the most obvious things can be hard to understand. Dynamic type systems are very simple and and get out of the way of the more urgent topics in an introductory course, like modularization, control flow, etc.

There are also some conceptual advantages to dynamic typing in an educational setting:

  1. Types are associated with values, not variables. This is arguably more intuitive since it reflects the reason programs actually crash.

  2. Error messages are much more immediate and concrete. In Python you get a lovely stack trace and a message indicating what actually went wrong. In a statically typed language you get a compiler warning saying what would go wrong.

    Students working with a compiler can only work on programs they fully understand, lest they face a compilation error they cannot solve.

  3. Runtime error checking allows you to run incomplete programs. This allows for more incremental programs where the student can first worry about making the start of the program correct and only then see what happens.

  4. Some important concepts, like parametric polymorphism and duck typing, come for free in a dynamic language but require more complex type systems in a static language.

Finally, while you still have to handle the same complexity, you don't get all the benefits of static typing when starting to program:

  • Beginner programs are small and don't have many code paths (so you don't have to worry that much about type bugs in rarely accessed bits of code)

  • Begginer programs don't use high level interfaces and functions that much so there is also not much to gain in the "use the type system to design the program" department.

share|improve this answer

There's one very, very, very important thing about Python for beginning programmers that everyone seems to have overlooked: Forcing valid indentation.

Like some of the other commenters, I taught beginners for a few years. It was unnervingly common to see stuff like this:

#include <stdio.h>

int main(char *args[])
                                                                     {
   int A = 1, B = 1, C;
for (int X = 0;X < 20;X++)
{
   printf("%d\n", A);
C = B + A;
      A = B;  B = C;
}
        }

Now imagine this horrid inconsistency over about a hundred lines of code. For some students, the entirety of all their projects looked like this.

I have no clue how they managed to do it, and it never bothered them. Every time I asked, their response was something like "Well, it doesn't matter because the compiler will figure it out." Even when I show them an obvious bug by fixing the indenting, they still never got a clue.

In Python, this type of indentation is just plain invalid. They're forced to use something that's at least readable, even if it's a little inconsistent. It gives them a habit that will, hopefully, continue when they eventually learn some other language.

share|improve this answer

Python is a great first language for most of the reasons stated above. In particular, one requirement of a first language is a gentle learning curve, which Python has. You don't have to know all about classes, for instance, to get started. In this respect it's a little like the Basic many of us started on.

One thing that hasn't been highlighted so far is that many languages have a gentle learning curve, but quickly run into a brick wall when trying to do more advanced stuff. Old-school Basic is a good example of this. With Python you can get into some very advanced stuff before feeling that some other language might be a better choice.

By that time a beginner is able to understand more about choices and trade-offs, and is ready for C/C++, Java, Assembler, Prolog, Lisp, etc. Actually, Lisp might be a good first language too, though it's rather austere!

share|improve this answer

The key issue with Python as a first language are the dynamic typing and lack of variable declaration. IMHO they are the only major issue with the language.

The simple addition of a line saying I am only going to store a string in the variable foo allows the programmer, the development environment, the language (and for "professional code" the poor fool who has to come in cold and support it afterwards) to work together. For normal developers and beginners especially it doesn't make sense to use a variable for more than one type. It is quite OK on the rare occasion when it does make sense to use an explicit var/object type that allows for dynamic typing.

share|improve this answer

protected by maple_shaft Mar 11 '13 at 22:34

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.