[Introduction to Python Tutorial] Why Beginners Should Learn Python

You may have noticed from my other articles that I'm a huge Node.js fan, but despite that, and Node.js has been my programming language of choice for a while, I still wouldn't recommend it to everyone.

Learning computer technology and programming can be difficult at first: which programming language to choose? Which IDE to use? And more importantly, why choose it?

In my opinion, the most important thing when programming is to choose the best tool for solving the problem. The second is to choose the tool that you are best at. If I tell you that you should use C++ because it's one of the fastest programming languages, but you don't have any experience dealing with memory management or designing your own data structures, that's obviously not a good idea. You might struggle and feel bad about programming.

And Python solves many of these aspects. It runs much slower than C++, but is also much easier to write. As a beginner, you may not care too much about how fast the program runs. All you care about is doing some cool stuff and learning some basic concepts of programming at the same time.

So the first decision you need to make is which programming language to learn. Among hundreds of programming languages, why should beginners learn Python? Let me analyze it for you.

concise syntax

The core philosophy of Python (see "The Zen of Python" summarized in PEP20) includes the following points:

Beautiful is better than ugly

Simple is better than complex

readability matters

This shows that Python has been designed with simplicity in mind from the very beginning. This was refreshing when Python was born, because C/C++, the dominant programming language at the time, was not particularly user-friendly.

Let's compare the syntax of C++ and Python with a simple "Hello, World" example:

#include stdout

int main() {

std::cout << “Hello, world!\n”;

}

Python:

print(‘Hello, world!’)

The purpose of the two pieces of code is to print a string to the terminal, and it can be seen that there is a big difference between the two. In order to better reflect the difference, let's do another comparison, this time using another language PHP for comparison:

Python:

x=1

while x <=5:

print ‘x is less than 5:’ + str(x)

x += 1

PHP:

<?php

$x=1;

while($x<=5) {

echo “x is less than 5: $x”;

x++;

}

?>

Python tries its best to avoid unnecessary things and keep only necessary elements, which is a big reason why Python does not use curly braces and semicolons as line endings. Take a closer look at the difference these features make (I promise this is the last piece of code to compare):

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

}

}

No more comparisons with other languages ​​are made here. The languages ​​mentioned above are great and widely used, but they are not very nice for beginners.

For Python with keywords like is, not, and with, well-designed programs are as easy to read as English text. This is especially true for the conditional declaration of an if conditional statement, which can be difficult to read if the conditional of the if statement is huge:

a = None

b = None

if a is not None and b is not None:

print ‘Foo!’

else:

print ‘Bar!’

The above conditional statement is much more readable than the usual if((a != null) && (b != null)).

Easy to configure and easy to run

Many beginners fail when trying to learn a language without even writing a single line of code. For some languages ​​like Java, you have to install and configure complex project directories before compiling the code to run.

For Python, you just need to download and install Python, then use 'python .py' to run the program, there is no complicated directory structure to create or compile to run.

Although it may be increasingly rare for modern languages, compiling code is still more difficult than you might think (but it's still unavoidable). Let's take a look at the following simple C program

makefile:

CC = gcc

CFLAGS = -g -Wall

TARGET = myprog

all: $(TARGET)

$(TARGET): $(TARGET).c

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

clean:

$(RM) $(TARGET)

While I think it's a simple makefile, I would prefer Python over it at all times.

Python allows you to focus on learning programming concepts at the beginning of programming instead of getting bogged down in the tedious details of how a high-level language compiles to machine code. Those things need to be learned, of course, but shouldn't be learned at the beginning.

Massive standard library

One of Python's most widely praised advantages is its powerful standard library. More than 400 modules are shipped with Python 2.7, ranging from the minimal HTTP server module BaseHTTPServer, to the database module sqlite3, to the file compression library gzip.

Most of what you want to implement in Python is usually already included in the standard library. So you can create cool things like machine learning applications with very little work.

I often remind myself to browse the standard library to see what's there so I don't reinvent the wheel. So before you start writing a url parsing module, see if there is a similar module in the standard library.

The added benefit of not having to reinvent the wheel is that things in the standard library are usually well-tested and bug-free. Much of the code in the standard library has been around for a while and is probably used by top companies (discussed later), so it's safe to use it.

Community

A large and active community means two things:

1. Many third-party libraries

2. Many experts can help you

Regardless of your programming level, these two points are probably the most important reasons why you should use Python. This means that you have access to endless documentation, guides, and Python source code for learning.

Python ranks high in a variety of different programming language rankings: Redmonk ranks 4th and Tiobe ranks 5th.

Even more important than language popularity rankings are hiring needs. As you can see from the figure below (quoted from Indeed), Python ranks second among the most in-demand programming languages ​​for employers, which means that if you want to become a programmer after learning Python, you will have a better chance.

image

easy to debug

Often the most difficult skill for beginners to master is debugging code. When you learn to debug code, you really start to understand programming languages ​​and their inner workings. Sometimes your bug is very simple, just a syntax problem, more often your bug is hidden deep and hard to find, maybe you run the program 100 times and the bug only appears once.

At this time, you need to understand the debugger and the common error prompts in the language. And thankfully, Python's error handling and reporting mechanisms are great compared to many other languages.

For example in C++ if something goes wrong (like dereferencing an invalid pointer, accessing an element beyond the bounds of an array, etc.) you are lucky if the program crashes, because then you know for sure that there is a problem in the program, but maybe you Not sure where the exact problem is (debuggers are not always intuitive for beginners). If you're unfortunate enough that the program doesn't crash (or just crashes randomly), then the bug is hidden and harder to find.

Where Python sucks

I don't think it's right to write an article describing how Python shines without saying a word about its shortcomings. Like other programming languages, Python is far from perfect and there are many times when we shouldn't use Python.

I've mentioned more than once that Python is slow, especially compared to compiled languages ​​like C/C++ or Go. This is because many features of Python can slow things down, such as dynamic typing, garbage collection, etc.

This means that you should not use pure Python when dealing with large amounts of data, but should call C++ functions in Python (which we will discuss later).

In addition, Python is not suitable for real-time systems due to the existence of garbage collection mechanism. Because garbage collection means that the running time of your program is indeterminate, you don't know if your function will take 1ms or 100ms. Just because there are so many uncertainties, for real-time systems, you should choose a language that manages memory manually such as C or C++.

Likewise, since Python relies on many system resources and comes with an interpreter, Python code usually (but there are exceptions) can only run on hardware systems with an operating system (not microcontrollers or other embedded systems).

Summarize

Here are just a few reasons why Python is good for beginners. With so many resources out there for learning Python, you can start learning Python programming in a fraction of the time.

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

Welcome to exchange and study.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326840249&siteId=291194637