Why Beginners Should Learn Python

python_tutorials

From some of my other posts you’ve probably noticed that I’m a big fan of Node.js. While this is true, and has been my go-to language for a while now, I don’t always recommend it to everyone.

Just starting out in programming and computer science can be a bit daunting. Which language should you pick? Which IDE should you use? And more importantly, why?

In my opinion, the most important thing you should do when programming is choose the right tool for the job. The second most important thing is to choose the tool that you’re most comfortable with. If I were to tell you that you should be using C++ since it’s one of the fastest languages out there, that may not be good advice if you’ve never had to deal with memory management or written your own data structures. You’d probably struggle through it and have a bad experience.

Python, on the other hand, solves a lot of these problems for you. It runs much slower than C++, but it’s also much easier to write. And as a beginner you probably don’t care how fast it is, you just want to make something cool and learn the basic concepts.

So, the first decision you have to make is which language you want to learn. Of the hundreds of languages out there, why should beginners learn Python? Well, there are a few reasons…

Simple Syntax

Part of the core philosophy of the language (as summarized by PEP 20, “The Zen of Python”), includes the following:

  • Beautiful is better than ugly
  • Simple is better than complex
  • Readability counts

So, as you can see, Python was designed from the beginning with simplicity in mind. This was a breath of fresh air at the time of its creation since some of the more dominant languages at the time were C and C++, which aren’t very user friendly.

Let’s compare the syntax of Python to C++ using a simple ‘Hello, World’ example:

C++:

#include stdout
 
int main() {
	std::cout << "Hello, world!n";
}

Python:

print('Hello, world!')

There's a pretty big difference there, and all we did was print a string to the console. For good measure let's do another syntax comparison, but this time with PHP:

Python:

x=1
while x <=5:
    print 'x is less than 5:' + str(x)
    x += 1

PHP:


Python really tries to get rid of the 'fluff' and only requires what's really necessary, which is a big reason why it was designed to be used without curly braces and line-ending semicolons. Take a look at the difference it makes (last syntax comparison, I promise):

Python

def foo(x):
    if x == 0:
        bar()
        baz()
    else:
        qux(x)
        foo(x - 1)

C:

void foo(int x)
{
    if (x == 0) {
        bar();
        baz();
    } else {
        qux(x);
        foo(x - 1);
    }
}

I'm really not trying to bash on other languages here. All of these other languages mentioned really are great and have tons of uses, but they just aren't very good for beginners.

With keywords like is, not, and with, a well-written Python script can almost be read like plain English. This is especially true for if statement conditions, which can be hard to read if they get big enough:

a = None
b = None

if a is not None and b is not None:
    print 'Foo!'
else:
    print 'Bar!'

The conditional statement above is much cleaner than the typical if ((a != null) && (b != null)).

Easy to Set up and Run

Many beginners trying to learn a language fail before they even write a single line of code. With some languages, like Java, you have to install and set up complicated project directories and then compile your code.

With Python, all you have to do to get started is download and run the installer, and run python .py. No complicated directory structure to create or compiling to do.

Although it's becoming increasingly rare in modern languages, compiling your code can be much harder than you'd think (although, it is a necessary evil). Just take a look at this small makefile for C:

CC = gcc
CFLAGS  = -g -Wall

TARGET = myprog

all: $(TARGET)

$(TARGET): $(TARGET).c
	$(CC) $(CFLAGS) -o $(TARGET) $(TARGET).c

clean:
	$(RM) $(TARGET)

And I consider this to be a simple makefile. I'd choose Python over this any day.

Python allows you to learn the concepts of programming first before getting in to the dirty details of how high-level code is translated in to machine-level code, which you should absolutely learn, just not when you're first starting off.

Huge Standard Library

One of Python's most-touted strengths is its standard library, and for good reason. It comes with over 300 modules (in version 3.5), ranging from a minimal HTTP server (BaseHTTPServer) to databases (sqlite3), to compression libraries (gzip).

A vast majority of the things you'll want to do with Python is usually already done for you in these standard libraries. So you can start creating cool things with little effort, like apps with machine learning.

Every now and then I'll have to remind myself to look through the modules and see what all is available to avoid rewriting the code myself.

So before you go off and try to write a url parsing library, check to see if it already exists first!

One of the best parts about not having to write all this code yourself is knowing it's been thoroughly tested and bug-free. Much of this code has been around for a while and used at top companies (which we'll talk about later), so you know it's been put through the ringer.

A large, active community means two things:

  • Lots of third-party libraries
  • Lots of people to help you

These points are arguably some of the most important reasons why you should use Python, regardless of your skill level. This means you'll have tons more documentation, tutorials, and code to look through to better learn the language.

Python has consistently ranked highly as a top programming language by various sources, like by Redmonk (#4) and Tiobe (#5).

Even more important than language popularity is demand by employers. You can see from the graph below (by Indeed) that Python is the second most in-demand language by employers, which means you have a better chance of using your programming skills as a career.

Language job demand

Easy to Debug

As a beginner, one of the hardest skills to master is debugging. This is where you really get to know a language and its inner-workings. Occasionally you'll have easy bugs that are just syntax mistakes, and other times you'll have really hard-to-find bugs that only show up 1 out of the 100 times you run your program.

This is where you'll really get to know your debugger and common errors in a language.

Lucky for you, Python has very good error handling and reporting, while many other languages don't.

For example, in C++, if something goes wrong (like derefencing an invalid pointer, accessing an array element out of bounds, etc) you'll be lucky if the program crashes. That way you know there is a problem somewhere in your program, but you likely won't know where (and debuggers aren't always straight-forward for a beginner). If you're unlucky the program won't crash (or just crash at random times) and instead will give you obscure bugs that aren't very obvious.

What Python Sucks At

Okay, I didn't think it would be right to write up this glowing article on Python and not talk about its downsides. Like any other language out there it's far from perfect, there are plenty of things you shouldn't use it for.

As I've mentioned a few times, Python is slow. Like really slow when compared to compiled languages like C/C++ or Go. This is because it has quite a few features that slow it down, like being dynamically typed and having garbage collection.

What this means for you is you shouldn't use pure Python for processing lots of data, but instead you'll have to add C++ hooks (which we'll talk about another day).

And thanks to Python's garbage collection you can't use it for real-time systems. This is because garbage collection makes code run in a non-deterministic length of time, so you won't know if your function will take 1ms or 100ms to run. There are just too many unknowns. Instead, for these real-time programs you'll usually have to use a language with manual memory management like C or C++.

Along those same lines, since Python relies on so many system resources and has an interpreter, you can usually (I say 'usually' because there are other options) only run Python code on top of a system with an operating system (meaning no microcontrollers or other embedded systems).

Conclusion

These are just a few of the reasons why Python is great for beginners. There are so many resources these days to get started that it'll be a small investment of time to start programming with Python.

Which language did you learn first, and why? Let us know in the comments!

Visit source site

Leave a Reply