SoFunction
Updated on 2024-11-15

Learning Python with Laoqi: Starting Real Programming

Through the study of the four operations, has been initially exposed to the contents of Python, if the reader is a zero-based learners, may be a little confused. Is it possible to knock a few commands in the IDE and see the results, even if the program is programmed? This is not one of those programs that can run automatically?

Indeed. So far, it's not quite programming, only a bit of simple work with some instructions (or commands, as they're called). And the IDE interface that you're looking at is also for entering commands.

Please be at ease, we are going to learn how to write a real program. The tool is still IDLE, but please keep in mind that for a real program, it doesn't matter what tool you use, as long as you can write the instructions in it, for example, you can use Notepad.

I'm going to go get a cup of tea, listed as first read the following paragraph carefully, about the concept of program, from Wikipedia:

First read a paragraph in English: computer program and source code, it does not matter if you do not understand, you can skip it and read the next article directly.

A computer program, or just a program, is a sequence of instructions, written to perform a specified task with a computer.1 A computer requires programs to function, typically executing the program's instructions in a central processor.2 The program has an executable form that the computer can use directly to execute the instructions. The same program in its human-readable source code form, from which executable programs are derived (., compiled), enables a programmer to study and develop its algorithms. A collection of computer programs and related data is referred to as the software.

Computer source code is typically written by computer programmers.3 Source code is written in a programming language that usually follows one of two main paradigms: imperative or declarative programming. Source code may be converted into an executable file (sometimes called an executable program or a binary) by a compiler and later executed by a central processing unit. Alternatively, computer programs may be executed with the aid of an interpreter, or may be embedded directly into hardware.

Computer programs may be ranked along functional lines: system software and application software. Two or more computer programs may run simultaneously on one computer from the perspective of the user, this process being known as multitasking.

computer program

A computer program (Computer Program) is a set of instructions that instructs a computer or other information-processing device to perform each step of an action, usually written in some programming language and running on some target architecture. To make an analogy, a program is like a recipe (program) for braised pork written in Chinese (programming language), which is used to instruct someone who knows Chinese and cooking techniques (architecture) to make the dish. Typically, a computer program is compiled and linked into a format that is not readily visible to people but can be interpreted by a computer, and then run. Programs that run without being compiled are often called scripts.

Bigelow, it's my favorite. Anyone wishing to send me a gift, please don't forget it. Do I expect to be listed as a watcher who will send it? Hahaha.

Without further ado, let's get to the program. A program, in short, is a collection of instructions. However, some programs need to be compiled and some don't. Programs written in python don't, so they are also called scripted programs. Special note to all of you, do not think that compiled is good, not compiled is not good; also do not think that compiled is "high-end", not compiled is "low-end". Some programmers who have been working on programs for many years, or others, may have such an idea, which is unfounded.

No arguments. Used wonderfully is good.

Programming environment with IDLE

Operation: File->New window

In this way, a new interface appears, in which the prompt used to enter commands is not visible: >>>, the interface is a bit like a notepad. That's right, it's essentially a notepad, you can only enter text, you can't paste pictures directly into it.

Write two big words: Hello, World.

Hello,World. is a world-oriented logo, so write any program, the first sentence must write this, because the programmer is world-oriented, absolutely not cowering in a local area network, so, so look at the officer to be able to scientific Internet access, in order to really Hello with the world.

Just go straight to the code, just one line.

print "Hello,World"

The style as shown below

As I said before, a program is a collection of instructions, and now, inside this program, there is just one instruction. A single instruction can also be a collection.

Observe that there is a RUN on the menu, click on this menu and select Run Moudle in the drop down.

A dialog box will pop up asking to save this file, which is relatively simple, save it to a location, the viewer must remember the location, and take a file name, the file name is with a .py extension.

After all done, click the OK button, you will find in another interface with >>> > automatically come out of the Hello,World two big words.

Did you succeed? Don't get excited if you succeeded, because it's not time to celebrate.

In this case, we are still in the IDLE environment to achieve the automatic execution of the program just now, if we are out of this environment?

The following is to close the IDLE, open the shell (if you are using Apple's ms xos operating system or some kind of linux distribution operating system, for example, I am using ubuntu), or open the cmd (windows operating system users, special reminder of the use of windows users, the use of windows is not your fault, the fault is in your fault! Only use the mouse to click around, but do not want to and will not use commands, not to mention do not want to and will not use linux commands, but also dream of becoming a good programmer.) , by way of commands, go to the directory where you saved the file you just made.

Below is the address where I saved that file, I named that file and saved it in a folder.

Then inside this shell, type: python

The meaning of the above sentence is to tell the computer to run me a program written in the python language, and the name of that program file is

I'm the boss of my computer. So it dutifully executed this command for me. It is shown below:

Still silent? It's time to cheer, Germany won 7:1 against Brazil pair, listed as one of the watchers, no matter if you are a fan of Germany or Brazil, you can cheer, because you took a great second step on the programmer's path. By the way, predict that the ultimate winner of this World Cup should be: the Chinese team. (Is there such a thing as bullshit?)

solve a puzzle

Please calculate: 19+2*4-8/2

The code is as follows:

#coding:utf-8

"""
Please calculate: 19+2*4-8/2
"""

a = 19+2*4-8/2
print a

Reminder to beginners, don't copy this code, but rather type it in word by word. Then save it (I saved it with the filename :).

In the shell or cmd, execute: python (filename.py)

The result of the execution is shown below:

The above code, the first line, can not be less, this document is able to input Chinese characters, otherwise Chinese characters such as law input.

It seems to be simpler still.

Take it easy. Complications are ahead.