如何挑选你的第一门编程语言
编程语言
想学编程的原因有很多，你也许是想要做一个程序，又或者你只是想投身于这个行业，所以，在选择你的第一门编程语言之前，问问你自己：你想要在哪里运行程序？你想要程序来完成什么工作？


![](/data/attachment/album/201704/06/162637h3c1l3c1zqx17h3z.jpg)


你学习编程的原因将会决定你的第一门编程语言的选择。


在这篇文章里，我会交替使用<ruby> “编码” <rp>  （ </rp> <rt>  code </rt> <rp>  ） </rp></ruby>、<ruby> “编程” <rp>  （ </rp> <rt>  program </rt> <rp>  ） </rp></ruby>、<ruby> “开发” <rp>  （ </rp> <rt>  develop </rt> <rp>  ） </rp></ruby> 等动词，<ruby> “代码” <rp>  （ </rp> <rt>  code </rt> <rp>  ） </rp></ruby>、<ruby> “程序” <rp>  （ </rp> <rt>  program </rt> <rp>  ） </rp></ruby>、<ruby> “应用程序” <rp>  （ </rp> <rt>  application </rt> <rp>  ） </rp></ruby>、<ruby> “应用” <rp>  （ </rp> <rt>  app </rt> <rp>  ） </rp></ruby>等名词。这是考虑到了你可能听过的语言用法。


### 了解你的设备


在你编程语言的选择上，你的程序将运行在何处是个决定性因素。


桌面应用是运行在台式机或者笔记本电脑上的传统软件程序。这样你编写的代码在同一时间内只能在一台电脑上运行。移动应用，也就是我们所熟知的“app”，运行在使用 IOS 、Android 或者其他操作系统的移动设备上。网页应用是功能像应用的网页。


按互联网的 客户-服务器（C/S）架构分，网页开发者经常被分为两类：


* 前端开发，就是编写运行在浏览器里面的代码。这是个面对用户的部分，或者说是程序的前脸。有时候被称为“客户端编程”，因为浏览器是网站的客户-服务器架构的客户端部分。浏览器会运行在你本地的电脑或者设备上。
* 后台开发，也就是大家所熟知的“服务器端开发”，编写的代码运行在你无法实际接触的服务器上。


### 创造什么


编程是一门广泛的学科，能应用在不同的领域。常见的应用有：


* 数据科学
* 网页开发
* 游戏开发，以及
* 不同类型的工作自动化


现在我们已经讨论了为什么你要编程，你要程序运行在哪里，让我们看一下两门对于新手来说不错的编程语言吧。


### Python


[Python](https://www.python.org/about/) 是对于第一次编程的人来说是最为流行的编程语言之一，而且这不是巧合。Python 是一门通用的编程语言。这意味着它能应用在广泛的编程任务上。几乎**没有**你不能用 Python 完成的工作。这一点使得很多新手能在实际中应用这门编程语言。另外， Python 有两个重要的设计特征，使得其对于新手更友好：清晰、类似于英语的[语法](https://en.wikipedia.org/wiki/Python_syntax_and_semantics)，和强调代码的[可读性](https://en.wikipedia.org/wiki/Python_syntax_and_semantics#Indentation)。


从本质上讲，一门编程语言的语法就是你所输入的能让这编程语言执行的内容。这包括单词，特殊字符（例如 `;`、`$`、`%` 或者 `{}`），空格或者以上任意的组合。Python 尽可能地使用英语，不像其他编程语言那样经常使用标点符号或者特殊的字符。所以，Python 阅读起来更自然、更像是人类语言。这一点帮助新的编程人员可以聚焦于解决问题，而能花费更少的时间纠结于语言自身的特性上。


清晰语法的同时注重于可读性。在编写代码的时候，你所创造的代码的逻辑“块”，就是一些为了相关联目标而共同工作的代码。在许多编程语言里，这些块用特殊字符所标记（或限定）。它们或许被 `{}` 或者其他字符所包住。块分割字符和你写代码的能力，这两者不管怎么结合起来都会降低可读性。让我们来看一个例子。


这有个被称为 `fun` 的简短函数。它要求输入一个数字，`x` 就是它的输入。如果 `x` 等于 `0`，它将会运行另一个被称为`no_fun` 的函数（这函数做了些很无趣的事情）。新函数不需要输入。反之，简短函数将会运行一个使用输入 `x` 的名为 `big_fun` 的函数。


这个函数用 [C 语言](https://en.wikipedia.org/wiki/C_(programming_language) 将会是这样写的：



```
void fun(int x)
{
    if (x == 0) {
        no_fun();
    } else {
        big_fun(x);
    }
}

```

或者，像是这样：



```
void fun(int x) { if (x == 0) {no_fun(); } else {big_fun(x); }}

```

功能上两者等价，而且都能运行。`{}` 和 `;` 告诉我们哪里是代码块的不同部分。然而，第一个对于人们来说*明显*更容易阅读。相比之下完成相同功能的 Python 是这样的：



```
def fun(x):
    if x == 0:
        no_fun()
    else:
        big_fun(x)

```

在这里，只有一个选择。如果代码不是这样排列的，它将无法工作。如果你编写了可以工作的代码，你就有了可阅读的代码。同样也留意一下两者在语法上的差异。不同的是 `def` ，在 Python 代码中这个词是英语单词，大家都很熟悉这单词的含义（LCTT 译注：def 是 definition 的缩写，定义的意思）。在 C 语言的例子中 `void` 和 `int` 就没有那么直观。


Python 也有个优秀的生态系统。这有两层意思，第一，你有一个使用该语言的庞大、活跃的社区，当你需要帮助指导的时候，你能向他们求助。第二，它有大量早已存在的库，库是指完成特定功能的代码集合。从高级数学运算、图形到计算机视觉，甚至是你能想象到的任何事情。


Python 作为你第一门编程语言有两个缺点。第一是它有时候安装起来很复杂，特别是在运行着 Windows 的电脑上。（如果你有一台 Mac 或者 Linux 的电脑，Python 默认已经安装好了。）虽然这问题不是不能克服，而且情况总在改善，但是这对于一些人来说还是个阻碍。第二个缺点是，对于那些明确想要建设网站的人来讲，虽然有很多用 Python 写的项目（例如 [Django](https://www.djangoproject.com/) 和[Flask](http://flask.pocoo.org/) ），但是编写运行在浏览器上的 Python 代码却没有什么好的选择。它主要是后台或者服务器端语言。


### JavaScript


如果你知道你学习编程的主要原因是建设网站的话，[JavaScript](https://en.wikipedia.org/wiki/JavaScript) 或许是你的最佳选择。 JavaScript 是关于网页的编程语言。除了是网页的默认编程语言之外， JavaScript 作为初学的语言有几点优点。


第一，无须安装任何东西。你可以打开文本编辑器（例如 Windows 上的记事本，但不是一个文字处理软件，例如 Microsoft Word）然后开始输入 JavaScript 。代码将在你的浏览器中运行。最顶尖的浏览器内置了JavaScript 引擎，所以你的代码将可以运行在几乎所有的电脑和很多的移动设备上。事实上，能马上在浏览器中运行代码为编程人员提供了一个非常*快*的反馈，这对于新手来说是很好的。你能尝试一些事情然后很快地看到结果。


开始 JavaScript 是作为前端语言的，不过一个名为 [Node.js](https://nodejs.org/en/) 的环境能让你编写运行在浏览器或者服务器上的代码。现在 JavaScript 能当作前端或者后台语言使用。这增加了它的使用人数。JavaScript 也有大量能提供除核心功能外的额外功能的包，这使得它能当作一门通用语言来使用。JavaScript 不只是网页开发语言，就像 Python 那样，它也有个充满生气的、活跃的生态系统。


尽管有这些优点，但是 JavaScript 对于新手来说并非十全十美。[JavaScript 的语法](https://en.wikipedia.org/wiki/JavaScript_syntax#Basics)并不像 Python 那样清晰，也不怎么像英语。更像是之前例子里提到的 C 语言。它并不是把可读性当作主要的设计特性。


### 做出选择


选 Python 或者 JavaScript 作为入门语言都没有问题。关键是你打算做什么。为什么你要学习编程？你的回答很大程度上影响你的决定。如果你是想为开源做贡献，你将会找到*大量*用这两门语言编写的项目。另外，许多主要不是用 JavaScript 写的项目仍使用 JavaScript 用作前端组件。当你做决定时，别忘了你本地的社区。你有在使用其中一门语言的朋友或者同事吗？对于一个新手来说，有实时的帮助是非常重要的。


祝好运，开心编程。




---


作者简介：


Kojo Idrissa - 我是一个新晋的软件开发者（1 年），从会计和大学教学转型而来。自从有开源软件以来，我就是它的一个粉丝。但是在我之前的事业中并不需要做很多的编程工作。技术上，我专注于 Python ，自动化测试和学习 Django 。我希望我能尽快地学更多的 JavaScript 。话题上，我专注于帮助刚开始学习编程或想参与为开源项目做贡献的人们。我也关注在技术领域的包容文化。




---


via: <https://opensource.com/article/17/1/choosing-your-first-programming-language>


作者：[Kojo Idrissa](https://opensource.com/users/transitionkojo) 译者：[ypingcn](https://github.com/ypingcn) 校对：[bestony](https://github.com/bestony)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
The reasons for learning to program are as a varied as the people who want to learn. You might have a program you want to make, or maybe you just want to jump in. So, before choosing your first programming language, ask yourself: Where do you want that program to run? What do you want that program to do?

Your reasons for learning to code should inform your choice of a first language.

*In this article, I use "code," "program," and "develop" interchangeably as verbs, while "code," "program," "application," and "app" interchangeably as nouns. This is to reflect language usage you may hear.*

## Know your device

Where your programs will run is a defining factor in your choice of language.

Desktop applications are the traditional software programs that run on a desktop or laptop computer. For these you'll be writing code that only runs on a single computer at a time. Mobile applications, known as apps, run on portable communications devices using iOS, Android, or other operating systems. Web applications are websites that function like applications.

Web development is often broken into two subcategories, based on the web's client-server architecture:

-
Front-end programming, which is writing code that runs in the web browser itself. This is the part that faces the user, or the "front end" of the program. It's sometimes called "client-side" programming, because the web browser is the client half of the web's client-server architecture. The web browser runs on your local computer or device.

-
Back-end programming, which is also known as "server-side" programming, the code written runs on a server, which is a computer you don't have physical access to.


## What to create

Programming is a broad discipline and can be used in a variety of fields. Common examples include:

- data science,
- web development,
- game development, and
- work automation of various types.

Now that we've looked at why and where you want to program, let's look at two great languages for beginners.

## Python

[Python](https://www.python.org/about/) is one of the most popular languages for first-time programmers, and that is not by accident. Python is a general-purpose language. This means it can be used for a wide range of programming tasks. There's almost *nothing* you can't do with Python. This lets a wide range of beginners make practical use of the language. Additionally, Python has two key design features that make it great for new programmers: a clear, English-like [syntax](https://en.wikipedia.org/wiki/Python_syntax_and_semantics) and an emphasis on code [readability](https://en.wikipedia.org/wiki/Python_syntax_and_semantics#Indentation).

A language's syntax is essentially what you type to make the language perform. This can include words, special characters (like `;`

, `$`

, `%`

, or `{}`

), white space, or any combination. Python uses English for as much of this as possible, unlike other languages, which often use punctuation or special characters. As a result, Python reads much more like a natural, human language. This helps new programmers focus on solving problems, and they spend less time struggling with the specifics of the language itself.

Combined with that clear syntax is a focus on readability. When writing code, you'll create logical "blocks" of code, sections of code that work together for some related purpose. In many languages, those blocks are marked (or delimited) by special characters. They may be enclosed in `{}`

or some other character. The combination of block-delimiting characters and your ability to write your code in almost any fashion can decrease readability. Let's look at an example.

Here's a small function, called "fun," which takes a number, `x`

as its input. If `x`

equals **0**, it runs another function called `no_fun`

(which does something that's no fun). That function takes no input. Otherwise, it runs the function `big_fun`

, using the same input, `x`

.

This function defined in the ["C" language](https://en.wikipedia.org/wiki/C_(programming_language) could be written like this:

```
``````
void fun(int x)
{
if (x == 0) {
no_fun();
} else {
big_fun(x);
}
}
```


or, like this:

```
````void fun(int x) { if (x == 0) {no_fun(); } else {big_fun(x); }}`


Both are functionally equivalent and both will run. The `{}`

and `;`

tell us where different parts of the block are; however, one is *clearly* more readable to a human. Contrast that with the same function in Python:

```
``````
def fun(x):
if x == 0:
no_fun()
else:
big_fun(x)
```


In this case, there's only one option. If the code isn't structured this way, it won't work, so if you have code that works, you have code that's readable. Also, notice the difference in syntax. Other than `def`

, the words in the Python code are English and would be clear to a broad audience. In the C language example `void`

and `int`

are less intuitive.

Python also has an excellent ecosystem. This means two things. First, you have a large, active community of people using the language you can turn to when you need help and guidance. Second, it has a large number of preexisiting libraries, which are chunks of code that perform special functions. These range from advanced mathematical processing to graphics to computer vision to almost anything you can imagine.

Python has two drawbacks to it being your first language. The first is that it can sometimes be tricky to install, especially on computers running Windows. (If you have a Mac or a Linux computer, Python is already installed.) Although this issue isn't insurmountable, and the situation is improving all the time, it can be a deterrent for some people. The second drawback is for people who specifically want to build websites. While there are projects written in Python (like [Django](https://www.djangoproject.com/) and [Flask](http://flask.pocoo.org/)) that let you build websites, there aren't many options for writing Python that will run in a web browser. It is primarily a back-end or server-side language.

## JavaScript

If you know your primary reason for learning to program is to build websites, [JavaScript](https://en.wikipedia.org/wiki/JavaScript) may be the best choice for you. JavaScript is *the* language of the web. Besides being the default language of the web, JavaScript has a few advantages as a beginner language.

First, there's nothing to install. You can open any text editor (like Notepad on Windows, but not a word processor like Microsoft Word) and start typing JavaScript. The code will run in your web browser. Most modern web browsers have a JavaScript engine built in, so your code will run on almost any computer and a lot of mobile devices. The fact that you can run your code immediately in a web browser provides a *very* fast feedback loop, which is good for new coders. You can try something and see the results very quickly.

While JavaScript started life as a front-end language, an environment called [Node.js](https://nodejs.org/en/) lets you write code that runs in a web browser or on a server. Now JavaScript can be used as a front-end or back-end language. This has led to an increase in its popularity. JavaScript also has a huge number of packages that provide added functionality to the core language, allowing it to be used as a general-purpose language, and not just as the language of web development. Like Python, JavaScript has a vibrant, active ecosystem.

Despite these strengths, JavaScript is not without its drawbacks for new programmers. The [syntax of JavaScript](https://en.wikipedia.org/wiki/JavaScript_syntax#Basics) is not as clear or English-like as Python. It's much more like the C example above. It also doesn't have readability as a key design principle.

## Making a choice

It's hard to go wrong with either Python or JavaScript as your first language. The key factor is what you intend to do. Why are you learning to code? Your answer should influence your decision most heavily. If you're looking to make contributions to open source, you will find a *huge* number of projects written in both languages. In addition, many projects that aren't primarily written in JavaScript still make use of it for their front-end component. As you're making a choice, don't forget about your local community. Do you have friends or co-workers who use either of these languages? For a new coder, having live support is very important.

Good luck and happy coding.

## 5 Comments