Select Page

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!”