考博 计算机基础与程序设计_计算机程序设计基础

考博 计算机基础与程序设计

Computer programming is at the heart of the modern world. All technology is powered by computer programs. Apps you download to your iPhone, websites you frequent on the internet, and even Netflix, all rely on humans telling machines what to do. If your work is not directly related to technology development, you might be tempted to leave this machine communication to the experts. If you do this, you will be missing out on obtaining a power to make your life easier. What’s more, you will be missing out unnecessarily. One of the greatest inhibitors to learning to code is a fear that it will be too difficult. Computer programming does not have to be complicated. You can learn it quickly and efficiently, and it will improve your life.

计算机编程是现代世界的核心。 所有技术均由计算机程序提供支持。 您下载到iPhone的应用程序,您经常访问互联网的网站甚至Netflix都依赖于人类告诉机器该怎么做。 如果您的工作与技术开发没有直接关系,那么您可能会想让这台机器与专家交流。 如果这样做,您将失去获得使生活更轻松的力量的机会。 而且,您会不必要地错过。 学习编码的最大障碍之一是担心它太难了。 计算机编程不必很复杂。 您可以快速有效地学习它,它将改善您的生活。

应用领域 (Applications)

If you have a Mac OS, you are likely familiar with the Applications tab in the Finder window. Every application on your computer was created from a folder (directory) containing text files on a computer. These files can be opened in any text editor (e.g. TextEdit or Microsoft Word). It is written in a computer programming language that is human-readable. That is, if you were to open the file, you would recognize English words on the page. Programming languages allow humans to communicate with computers. They allow the human to give the computer a list of instructions to execute. The computer is capable of understanding this human-readable code because it has a handy English-to-machine translator called a compiler.

如果您使用的是Mac OS,则可能熟悉Finder窗口中的“应用程序”选项卡。 您计算机上的每个应用程序都是从包含计算机上文本文件的文件夹( 目录 )创建的。 可以在任何文本编辑器(例如TextEdit或Microsoft Word)中打开这些文件。 它以人类可读的计算机编程语言编写。 也就是说,如果您要打开文件,您将在页面上识别英语单词。 编程语言允许人类与计算机进行通信。 它们使人类可以向计算机提供要执行的指令列表。 计算机能够理解这种人类可读的代码,因为它有一个方便的英语到机器的翻译器,称为编译器

Image for post
Applications on a Mac.
Mac上的应用程序。

There are hundreds of programming languages. You do not need to learn all of them. This is because they are all (for the most part) capable of accomplishing the same things.

有数百种编程语言。 您不需要全部学习。 这是因为它们(大部分)都具有完成相同功能的能力。

Programming languages can be divided into two categories: high-level and low-level. High level programming languages are easier to learn and use. Python is an example of a high-level language.

编程语言可以分为两类: 高级低级 。 高级编程语言更易于学习和使用。 Python是高级语言的示例。

Low-level programming languages are the opposite — harder to learn and use. So why would anyone learn and use them? High-level programming languages are themselves written in low-level languages.

相反,低级编程语言则更难学习和使用。 那么为什么有人会学习和使用它们呢? 高级编程语言本身是用低级语言编写的。

Python (Python)

基本 (Basics)

Python is the first language you should learn. It will allow you to quickly understand what sort of tasks your computer is capable of completing.

Python是您应该学习的第一门语言。 它可以让您快速了解计算机可以完成哪些任务。

If you have a Mac OS, you likely already have Python installed. If you don’t, you can download and install it here.

如果您使用的是Mac OS,则可能已经安装了Python。 如果没有,您可以在此处下载并安装它。

Once you have Python installed, open the Terminal Application (Mac OS) or MobaXterm (Windows). If you are unfamiliar with the command line, I strongly suggest reading this article. Run the following command to create a new Python file.

安装Python后,打开终端应用程序(Mac OS)或MobaXterm (Windows)。 如果您不熟悉命令行,强烈建议阅读本文 。 运行以下命令以创建一个新的Python文件。

touch file.py

Now, open your file using the following command.

现在,使用以下命令打开文件。

vim file.py

Press “i” to insert and type the following in the file. You can put any word you want within the quotation marks. I have chosen the word “Hi”.

按“ i”插入并在文件中键入以下内容。 您可以在引号中放入任何想要的单词。 我选择了“嗨”这个词。

print(“Hi”)

This is Python for “print the word Hi to the screen”. Press Esc:wq to save and quit. Run the following command to run your code.

这是Python的“在屏幕上打印单词Hi”。 按Esc:wq保存并退出。 运行以下命令以运行您的代码。

python file.py
Image for post
Running a simple Python file called file.py that prints the word “Hi” to the terminal.
运行一个名为file.py的简单Python文件,该文件在终端上显示单词“ Hi”。

Congratulations! You have run your first computer program. You will notice the word “Hi” printed in the terminal, which is what you asked the computer to do (in Python).

恭喜你! 您已经运行了第一个计算机程序。 您会注意到终端上印有“ Hi”一词,这是您要求计算机执行的操作(使用Python)。

Jupyter笔记本 (Jupyter Notebook)

While learning to code on the command line like this is useful, I find that most people learn best using an Integrated Development Environment, or IDE. An IDE is an application (just like Google Chrome or Microsoft PowerPoint) with a text editor (similar to TextEdit or Microsoft Word) made explicitly for programmers. My favorite for coding in Python is Jupyter Notebook.

虽然在这样的命令行上学习代码很有用,但我发现大多数人都使用集成开发环境 ( IDE)学习得最好。 IDE是带有文本编辑器(类似于TextEdit或Microsoft Word)的应用程序(就像Google Chrome或Microsoft PowerPoint),是专门为程序员设计的。 我最喜欢使用Python编码的是Jupyter Notebook

Run the following command to download and install Jupyter Notebook.

运行以下命令以下载并安装Jupyter Notebook

pip install notebook

Next, run the following command to open Jupyter Notebook.

接下来,运行以下命令以打开Jupyter Notebook。

jupyter notebook

Jupyter Notebook will open in a new tab in your default browser. Select New > Python 3. At this point your screen should look something like this.

Jupyter Notebook将在默认浏览器的新选项卡中打开。 选择“ 新建”>“ Python 3” 。 此时,您的屏幕应如下所示。

Image for post
An empty Jupyter Notebook.
一个空的Jupyter笔记本。

Try typing the same Python command you typed in file.py on the command line. Then, instead of exiting and running your file on the command line, press Shift+Enter.

尝试在命令行上键入与在file.py中键入的Python命令相同的命令。 然后,按Shift + Enter,而不是在命令行上退出并运行文件。

Image for post
A line of Python code in a Jupyter Notebook to print the word “Hi” to the screen.
Jupyter笔记本中的一行Python代码可将“ Hi”一词打印到屏幕上。

You will notice that your computer program has been executed, and “Hi” is printed to the notebook, just as it was printed to the terminal when you ran it there.

您会注意到您的计算机程序已执行,并且在笔记本上打印“ Hi”,就像在终端上运行时将其打印在终端上一样。

You can give the computer other instructions too. For example, your computer can perform mathematical operations.

您也可以向计算机提供其他说明。 例如,您的计算机可以执行数学运算。

Image for post
Two lines of Python code in a Jupyter Notebook to add 2 + 3 and print the result to the screen.
在Jupyter Notebook中使用两行Python代码添加2 + 3并将结果打印到屏幕上。

功能 (Functions)

Functions are my favorite part of coding. They allow you to organize code into logical chunks for future re-use. For example, say I want to add 2 numbers together. I can write a function that will do this for me. I can call the function whatever I want (in this case, I will call it add), but Python requires a specific format for function definitions. The function add would will look like this.

函数是我最喜欢的编码部分。 它们使您可以将代码组织成逻辑块,以备将来重用。 例如,说我想将2个数字加在一起。 我可以编写一个函数来为我做这件事。 我可以随意调用函数(在这种情况下,我称其为add ),但是Python需要函数定义的特定格式。 函数add将看起来像这样。

A basic function to add 2 numbers (x and y) together and return the result.
一个基本函数,将两个数字(x和y)加在一起并返回结果。

In this function, x and y are the input arguments, and z is the return value. In English, this Python function is saying, “Add x and y, and give me the result”. We can use the function like so.

在此函数中,x和y是输入参数 ,而z是返回值。 用英语来说,这个Python函数说:“加x和y,然后给我结果”。 我们可以像这样使用函数。

Using the user-defined function add().
使用用户定义的函数add()。

Focus on the second to last line of code: result = add(1,2). This line of Python is telling the computer, “Find the function called add. Within the function add, let x = 1 and y = 2. Assign the return value to the variable result”. This is an important point. Whenever you use a function with a return value (in this case, z, the sum of x and y), you will want to set it equal to a new variable (in this case result). That new variable will then be equal to the return value of the function. That is, the variable result will be equal to z, the sum of 1 and 2.

专注于代码的倒数第二行: result = add(1,2) 。 这行Python告诉计算机“找到名为add的函数 在函数add中,x = 1y = 2 。 将返回值分配给变量结果 “”。 这是重要的一点。 每当您使用带有返回值的函数(在这种情况下, zxy的总和)时,都将其设置为等于新变量(在这种情况下为result )。 然后,该新变量将等于该函数的返回值。 也就是说,变量结果等于z ,即1和2的和。

Note that a variable in computer programming is just like a variable in algebra — the only major difference is that in computer programming, variable names are typically longer and more descriptive than letters of the alphabet.

请注意,计算机编程中的变量就像代数中的变量一样–唯一的主要区别是,在计算机编程中,变量名通常比字母更长且更具描述性。

模块,软件包和库 (Modules, Packages, and Libraries)

Python actually already includes a function that will add numbers together. It is called sum and it can be used like so.

Python实际上已经包括一个将数字加在一起的函数。 它称为sum ,可以像这样使用。

Using the built-in function sum() to add 2 numbers.
使用内置函数sum()将2个数字相加。

The sum function even allows you to add more than 2 numbers together.

求和功能甚至允许您将两个以上的数字相加。

Using the built-in function sum() to add 5 numbers.
使用内置函数sum()将5个数字相加。

Both print() and sum() are defined in the Python standard library, which is the code that you installed when you installed Python. These commands that exist in the Python standard library are called built-in functions.

在Python标准库中定义了print()sum() ,这是在安装Python时安装的代码。 Python标准库中存在的这些命令称为内置函数

Python also offers additional functionality in the standard library that is more difficult to access. This functionality is contained in Python files called modules. A module is a Python file containing function definitions. Although these modules exist in the Python standard library, you need to explicitly import them into your code at the top of your file. In order to access the functions within a module, you must type the name of the module, followed by a dot, followed by the function name. For example, a very useful Python module is the os module. A very useful function within the os module is the getcwd() function, which gets (returns) your current working directory. This module and function can be imported and used in a Python script like this.

Python在标准库中还提供了更难访问的附加功能。 此功能包含在称为模块的Python文件中。 模块是包含函数定义的Python文件。 尽管这些模块存在于Python标准库中,但是您需要将其显式导入到文件顶部的代码中。 为了访问模块中的功能,必须键入模块的名称,然后输入点号,然后输入功能名称。 例如,一个非常有用的Python模块是os模块。 os模块中的一个非常有用的函数是getcwd()函数,该函数获取(返回)当前的工作目录。 可以像这样在Python脚本中导入并使用此模块和函数。

Importing a Python module called “os”, and using a function within the module called “getcwd()”.
导入一个名为“ os”的Python模块,并在该模块中使用一个名为“ getcwd()”的函数。

There are millions of people around the world who are writing additional Python libraries that we can also use in our code. A library is a collection of packages. A package is a directory of Python modules. We can access these libraries by downloading and installing them using “pip”, Python’s built-in package installer. For example, a very useful Python library is numpy, which can be installed by running the following command.

世界各地有数百万人正在编写其他Python库,我们也可以在代码中使用它们。 是软件包的集合。 是Python模块的目录。 我们可以使用Python的内置软件包安装程序“ pip”下载并安装这些库,以访问这些库。 例如,一个非常有用的Python库是numpy,可以通过运行以下命令来安装它。

pip install numpy

Once these libraries are installed, you need to import them into your code at the top of the file if you want to use functions within them. It is possible to assign aliases to the libraries when you import them, so that you can avoid typing out the full library name every time you need to access one of its functions. The standard alias for the numpy library is np. You can import it into your file using this alias like so.

安装这些库后,如果要在其中使用函数,则需要将它们导入文件顶部的代码中。 可以在导入库时为它们分配别名,这样就可以避免在每次需要访问其功能之一时都键入完整的库名。 numpy库的标准别名是np 。 您可以像这样使用该别名将其导入到文件中。

Importing a Python library (to use alongside the Python standard library) called “numpy” using the alias “np”, and using a function within the library called “arange”.
使用别名“ np”导入名为“ numpy”的Python库(与Python标准库一起使用),并使用库中的“ arange”函数。

For the moment, ignore the printed array. Simply note that np can be used as an alias for numpy to access the function arange within the numpy library.

目前,请忽略打印的阵列。 只需注意, np可以用作numpy的别名,以访问numpy库中的arange函数。

其他图书馆 (Other Libraries)

We have only brushed the surface of Python libraries. There are hundreds of thousands of Python libraries available for you to download and install using pip, and import and use in your code. Here are a few of my favorites.

我们只刷过Python库的表面。 有成千上万的Python库可供您使用pip下载和安装,以及在代码中导入和使用。 这是我的一些最爱。

  • numpy : a useful library for creating and manipulating lists (arrays)

    numpy:用于创建和处理列表( 数组 )的有用库

  • pandas : an excellent library for working with data from Excel

    pandas:一个用于处理Excel数据的优秀库
  • matplotlib : a library for creating plots

    matplotlib:用于创建绘图的库
  • scipy : an essential library for any mathematician

    scipy:任何数学家必不可少的库
  • PySimpleGUI : a library for creating graphical user interfaces (GUIs)

    PySimpleGUI:用于创建图形用户界面( GUI )的库

下一步 (Next Steps)

The previous section was a basic introduction to Python computer programming. There is much more to know, but as with learning any new skill, it is important not to bombard yourself with too much too soon. For now, play around on Jupyter Notebook, assigning numbers to variables and creating small functions. It is possible to create a wealth of functionality with what you now already know.

上一节是对Python计算机编程的基本介绍。 还有更多要了解的内容,但是与学习任何新技能一样,重要的是不要过早地对自己进行轰炸。 现在,在Jupyter Notebook上玩耍,为变量分配数字并创建小函数。 利用您现在已经知道的功能,可以创建丰富的功能。

翻译自: https://medium.com/simple-cs/the-basics-of-computer-programming-3d76e6b2898d

考博 计算机基础与程序设计

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值