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

  • http://www.blogger.com/profile/05264161000959091415 Muhammad Sohail

    Thanks Chris

  • http://www.blogger.com/profile/14089375087644662609 qntmfred

    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.

  • http://www.blogger.com/profile/14152513090644515202 Anler Hernánd

    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.

  • http://www.blogger.com/profile/13555600512373693909 Laurence Favrot

    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…

  • http://www.blogger.com/profile/06934291784799753325 kindageeky

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

  • http://www.blogger.com/profile/18090883271681844012 Unknown

    Nice summary! Note that Microsoft has free & OSS (Apache) version of Visual Studio for Python as well. Yes, Microsoft! http://pytools.codeplex.com

  • http://www.blogger.com/profile/07973860563047323140 Juanito

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

  • http://www.blogger.com/profile/03241932742086615240 Unknown

    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

  • http://www.blogger.com/profile/18420764833074264708 Jake

    The text is unreadable in IE8 running on XP.

  • http://www.blogger.com/profile/01169581670273063873 Leonardo Santagada

    The text is unreadable in Netscape 6.2 running on commodore 64.

  • http://www.blogger.com/profile/05843623517229863881 Juampa

    Plus you can target the .NET platform using Python with the help of IronPython!

  • http://www.blogger.com/profile/18299821222976957571 Mary@Udacity

    Thanks for all your comments! I'll look into the text issues. Do you have trouble viewing any other Blogger blogs?

  • http://www.blogger.com/profile/14089375087644662609 qntmfred

    Looks perfectly fine on my XP/IE8 VM.

  • http://www.blogger.com/profile/04291890326458397020 Ringo66

    thank you for explaining.

  • http://www.blogger.com/profile/00089387590802472111 zara

    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

  • http://www.blogger.com/profile/11153706481805853783 RogerDodger91

    My post was deleted. :( Censorship is lame.

  • http://www.blogger.com/profile/18299821222976957571 Mary@Udacity

    Oh no! What did you post?

  • http://www.blogger.com/profile/14262580975847564822 Saurav Tomar

    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:)

  • http://www.blogger.com/profile/11073607946860541783 Unknown

    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.

  • http://www.blogger.com/profile/02985148509697135302 yengeç

    I am not agree with you! I ask "Why not Java?". It's the most popular programming language, and its syntax familiar to all C/C++ and C#(obviously(!)) programmers.Here in this link, you can compare it with Python and others: />Top three languages are 1.Java, 2.C, 3.C++ (ok,

  • http://www.blogger.com/profile/08320732163799237390 Ninja
  • http://www.blogger.com/profile/12085355624192832617 Gazi Md Rezaul Karim

    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

  • http://www.blogger.com/profile/11728426799646711219 wasjack

    😛 the text is unreadable in my Palm Pilot.

  • http://www.blogger.com/profile/16403903993053012492 Oliver
  • http://www.blogger.com/profile/16669325856035471788 aaron waters

    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. :)

  • http://www.blogger.com/profile/01644020980489530150 Tim

    But as a rubyist, I have to admit the graphing and scientific packages in python appear much more developed. :(

  • http://www.blogger.com/profile/09290854717351365245 sudqi omar

    that was great

  • http://www.blogger.com/profile/16487670034212702870 Aleksander Hansen

    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]

  • http://www.blogger.com/profile/05647366144298623724 Sarfaraj Alam

    Thanks a lotI was confused that which language should I start learning.Bit after reading this post, I am sure and confident.Now I am going to start Learning PythonMy Blog

  • http://www.blogger.com/profile/06105315080608556455 shipra sagar

    Hello all,I am new and I would like to ask that what are the benefits of sql training,What all topics should be covered in it?And has anyone studied from this course http://www.wiziq.com/course/2118-learn-how-to-pro… of C tutorial online?? or tell me any other guidance…would really appreciate help