How to Code in Python: A guide for C#/.NET Programmers and Developers (Part 1)

Sometimes being thrown in the proverbial “deep end” is the best way to learn — you’ll either sink or swim. In most cases, you’ll still flail around a lot at first not sure what you’re doing or which way to go.

This post (and hopefully many to follow) will be an on-going observation of learning Python from the perspective of a general Computer Science background with the foundations of an experienced C# programmer/developer.

The roots of Python and C#

When looking into Python, I’d heard lots of good things about the language. People rave about it and according to the TIOBE Programming Index, C# and Python are equally popular languages. (Java, C, and C++ being the most popular.) So that’s saying something, I think.

As many C#’ers know, the C# language was created by Anders Hejlsberg in 2000 (for Microsoft), while Python was created by Guido van Rossum in 1991. With those 2 dates in mind, it would be easy to conclude that Python is a better language because it has had the time to become a better language.

The truth is that C# is a more evolved language than Python, despite having less time to develop.

Part of this is because it has the backing of an 800-lbs gorilla (Microsoft) and all the smart people that come with such backing, but also Anders Hejlsberg has had far more experience with computer languages than Guido van Rossum. If you read their respective Wikipedia pages, you’ll find that Anders implemented a compiler for Pascal in 1980 and then went on to create Turbo Pascal, Delphi, C#, and TypeScript.

In comparison, Guido implemented the first version of Python in December of 1989. A full 10 years after Anders wrote his first Pascal compiler. Guido wrote Python because he was “looking for a “hobby” programming project that would keep me occupied during the week around Christmas“.

Ambitious, no?

Despite this apparent disadvantage, Python has become almost a defacto standard in the open source world.

The differences between Python and C#

Python and C# are both strongly-typed languages. And they’re both object-oriented languages. But that’s about where the similarities end.

The most important difference to know about Python (in contrast to C#) is that Python is a dynamically-typed language. As most C# programmers know, C# is statically typed.

If you’ve been living in the statically typed world for a long time, dynamically typed languages will seem very “loosey goosey” or “undisciplined”.

What do I mean by this?

Say you define a method in C# with a return value. In C#,  you know even before compile time exactly what type of value is getting returned. In Python, you can write the following method:

Not only do we not know what type “x” is that we’re passing in, but the method could return an Int or a Bool. Absurd, right?

And that’s what dynamic typing allows: Flexibility. There are cases where this could be useful. It allows more flexibility at the cost of sometimes not knowing what you’re getting back. So do your type checking and prepare for run-time errors.

Fortunately, Python relies heavily on Duck Typing to ease some of the discomfort of pure dynamic typing. As an aside, C# also utilizes Duck Typing to ease some of the discomfort of pure static typing. (Ha! Isn’t that ironic?)

Seriously, come in, the water is fine.

It will be okay. Python is a fun language. If you’ve been programming in C# for a while, you’ll run into plenty of seemingly “strange” situations, but I’m writing this guide to get you through those weird parts.

Here’s a short list of things to know:

1) Get the PyCharm IDE

While it is possible to use Visual Studio to write Python code with the Python Tools add-on to VS, I’m going to recommend skipping it in favor of the Jetbrains PyCharm IDE.

That’s right. The same company that makes Resharper for Visual Studio also makes an IDE for Python called PyCharm. And the best part is that the Community Edition (think: Visual Studio Express) is FREE: https://www.jetbrains.com/pycharm/download/

It’s a solid IDE. It’s not quite as rich as Visual Studio, but it’s pretty darn good. It’s made by JetBrains. It’s gotta be good!

2) Take the Codecademy course on Python

In order to get quickly up to speed on Python, take the free Codecademy course on Python: https://www.codecademy.com/learn/python

It’s about 12 hours of work. I managed to complete it in about 1 week doing a couple hours a day. The exercises are kinda lame, but they’ll familiarize you with the basic language syntax quickly.

3) Be aware that there are 2 versions of Python

This is one of those cases where I look at Python and shake my head in disappointment.

Python has 2 versions of the language: Python 2.x (currently 2.7) and Python 3.x (currently 3.5) — In 2008, Python 3.0 was released to fix a bunch of “flaws” in the 2.0 language. The thing about 3.x is that it has some “breaking changes”.

Unfortunately, 7 years later, the community is still largely stuck on version 2.x because a lot of the new features of 3.x were back-ported to 2.x — yet the two languages are still different enough that they’re not quite compatible.

To make matters worse, 2.x still has better library support than 3.x — The Python homepage claims otherwise, but the reality is that 2.x has better library support.

Language-wise, 3.x really IS superior, so my advice is learn and code to that if you can. Just be prepared to go back to 2.x if things don’t work out in 3.x.

4) Prepare yourself to let go of certain C# things

As an example, one of the things that I had gotten used to in C# was coding to Interfaces.

Interfaces don’t really exist in Python. I’ve seen a library that implements it, but it is not implemented natively. Conversely, Python allows for multiple inheritance (something also available in C++ that I’ve always missed about C#.)

Well, that’s all for this first post. Next post will be on tricks I’ve learned to make Python function more like C#.

If you liked this article, please help spread the word using one of the social networking sites below. Thanks!
Facebooktwittergoogle_plusredditpinterestFacebooktwittergoogle_plusredditpinterest

One thought on “How to Code in Python: A guide for C#/.NET Programmers and Developers (Part 1)

Leave a Reply

Your email address will not be published. Required fields are marked *