Learning to program: Why Python?

Written by, Chris Chew
Software Engineer, Udacity

Blog reader, Peter, writes:
I like udacity but it’s programming course is tailored around python and i’m interested in c#. How does Python and C# .NET differ?

We think this is great question, and it is one we hear often, so we thought we should try answering it in some detail.

Before specifically comparing C# and Python, I want to say a few words about why Python is an interesting language for coursework, which in turn will help emphasize the similarities between Python and C# that are discussed a little further below.

Udacity’s courses so far have been using Python because it is overall, the most convenient language for teaching and learning.  The natural syntax means students spend less time grokking code than with terser languages.  Beyond the clean syntax Python’s flexible nature allows it to support all the common programming paradigms and concepts, which is very handy for someone who wants to learn programming as efficiently as possible.

For example object-oriented, imperative, and even functional programming paradigms can all be introduced using Python.  The dynamic typing system makes it easy to try advanced practices like “aspect-oriented” design and “inversion of control” through “dependency injection” without complicated libraries.  Somehow, amazingly, Python handles all of these crazy ideas with the same grace it handles “print ‘hello world.’”

It is important to keep in mind that time spent learning Python doubles as time spent learning C#, Ruby, or Java.  Thinking otherwise is dangerously short-sighted, particularly in a fast-paced industry like computer science.  The similarities between these languages are as complete as the similarities between driving in Europe and America:  In the same way an experienced driver from one continent can improvise with a reasonable degree of safety on the other, an experienced Python programmer can be up-and-running with C# in a day or two.

To illustrate my point, here is a non-exhaustive list of concepts that are directly relatable from Python to C#.  I hope you’ll agree, it comprises most everything someone needs to be proficient with a language like Python or C#:

  • Backus-Naur Form grammar
  • Built-in memory management
  • “Main” method is the program’s execution starting-point
  • Basic data types:  Boolean, String, Integer, Array, Dictionary, etc.
  • Method definitions with arguments and return statements
  • Classes act as type definitions
  • Polymorphic typing
  • Standardized libraries for common tasks like file input/output, network sockets, HTTP operations, regular expression matching, etc.
  • xUnit testing tools

Python’s type system is more flexible than C#’s (for better and worse).  It is important to keep in mind a few concepts from Python that are not directly translated to C#:

  • Mixin-style multiple inheritance (although this can be accomplished in C# using libraries like PostSharp or Spring.NET).
  • Dynamic typing, a.k.a “duck-typing” (this, too, can be accomplished using libraries built around Delegate.DynamicInvoke).

Lastly, there is a short but compelling list of features gained when moving from Python to C#:

  • Static-typing ensures more problems are found during compilation, before the program is executed.
  • A stricter distinction between interfaces and implementations means potentially-stronger contracts between components.
  • Popular Web and UI frameworks are included in the “language”.
  • VisualStudio, the most popular IDE for C#, is very nice.

As a general rule, once you become initially comfortable switching between C#, Python, Java, or Ruby, etc., it is a very good idea to learn how each of the languages handle the following things in a subtly different manner which can affect advanced programs:

  • Garbage collection
  • Passing arguments to methods by value vs. reference
  • Mutable vs. immutable types
  • The hierarchy of primitive types

Peter — Thank you for your question.  I hope this addresses, to some degree, the differences and similarities between Python and C#.  I also hope I may have convinced you that the similarities between the two languages are far more important than the differences.

As Professor Evans would say, “Stay Udacious!”

30 thoughts on “Learning to program: Why Python?

  1. Nice article. For me, the most attractive reason to use python for these courses is the superior numerical computation libraries commonly used in python. Also, I'll point out that though C# is a strongly-typed, dynamic typing has become very natural in C# with the var and dynamic keywords.

  2. RogerDodger91 nobody said that Python is better than C#, but is easier to learn, powerful (is not a toy language), and its syntax is more readable and simple (few keywords and control structures). These features makes the difference for beginners in computer science.

  3. Thanks for the explanation! Nothing like a good language war to get the blood pumping. A quick note: the link for "grokking" is blank. Now back to my catatonic state…

  4. Nice summary Chris! The world needs more polyglots, fewer language specialists. I totally agree with the emphasis on conceptual mastery.

  5. Great post! I also hope Udacity could give courses in Ruby. Ruby and Python are both great language for general purpose.

  6. I agree with your analyis of Python as a teaching language, but I want to make a largely unrelated and highly pedantic point: your use of the word 'grok' is not correct, as the Wikipedia link demonstrates. To grok is 'to understand so completely it changes your understanding of the universe', not 'to attempt to understand'. Spending less time grokking something is a

  7. Only an open-minded personal could create this way of content. I recognize with your aspects and I really knowledgeable this content a lot. Excellent article!Thanks! Ta! Cheers! Thanks SO much!zara

  8. Fabulous post,enjoyed reading it. @Chris Chew I have a question, which language should one learn first, an assembly language like C++ or and interpreter language like python?Regards,Saurav:)

  9. I am in my 10th year of C# now and I found lots of concepts transfer very well from C# to Python. Now I am suddenly missing things like generator expressions in C#. There are lot of similarities between both languages and Python seems to me like a very good candidate for your first language. The only thing I miss from C# is a good working intellisense.

  10. Is there anybody who will share Google App engine Download link for Web Development course.I am confused to download about Google App Engine.That means just share a download link for Win Xp 32 bit.Thanks

  11. Thank you, I think I will try your course. I still would like to see a C++ course on here now that courses recommending C++ are popping up here and on edX. I am not discrediting Python in any way; it sounds like I can learn a lot of constructs from it and use it in the future as well. :)

  12. Python is a powerful language, and it suits the purpose of the course. If you want to get in to other languages, I would strongly suggest you start with C, rather than with C++, C#, Objective-C (for e.g. iPad and iPhone) or Java. The latter four (as well as Python) draws heavily on C and you are well advised to get a basic grounding in C after [or before if you would like a challenge]

Comments are closed.