First Step
Next we will see how to run a traditional "Hello World" program in Python. Python tutorial This chapter will teach you how to write, save and run Python programs.
There are two ways to run your program through
Python-use the interactive interpreter prompt or directly run a source code file. We will learn how to use the functions of both of them.
Use interpreter prompt
Open the Terminal program in your operating system (as we discussed earlier in the Installation chapter) and then open the Python Prompt by typing python3 and pressing the [enter] key.
When you start Python, you will see that >>> appears where you can start typing. This is called the
Python Interpreter Prompt.
At the Python interpreter prompt, enter:
print("Hello World")
After entering, press the [enter] key. You will see Hello World printed on the screen.
Below is an example of the results you can see on a Mac OS X computer. The details of the Python software will be different depending on the computer you use, but the beginning of the prompt (such as >>>) should be the same, without being affected by the operating system.
> python3
Python 3.5.1 (default, Jan 14 2016, 06:54:11)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello World")
Hello World
You will naturally notice that
Python will immediately output a line of results to you! What you just entered is an independent Python statement. We use the print (don't be too surprised) command to print the information you provide. Here, we provided the text Hello World and it was quickly printed on the screen.
How to exit the interpreter prompt
If you are using a Shell program on GNU/Linux or OS X, you can press [ctrl + d] or type exit() (note: remember to include brackets ()) and type [enter] to exit the interpreter prompt.
If you are using the Windows command prompt, you can press [ctrl + z] and press [enter] to exit.
Choose an editor
When we want to run certain programs, we cannot always enter our program at the interpreter prompt every time. So we need to save them as files so that we can run these programs multiple times.
To create our Python source code files, we need an editor software that allows you to enter and save code. An excellent programmer-oriented editor can help you write source code files much easier. So choosing an editor is really important. You have to choose your editor just like the car you want to buy. An excellent editor can help you write Python programs more easily, make your programming journey more comfortable, and help you find a safer and faster way to reach your destination (achieve your goals).
One of the most basic requirements for the editor is syntax highlighting. This feature can help you distinguish different parts of the Python program by marking it with different colors, so that you can better see your program and make it run. The model is more visual.
If you have no idea where to start, I recommend you to use PyCharm Education Edition software, which can run on Windows, Mac OS X, GNU/Linux. You can learn more in the next section.
If you are using a Windows system, do not use Notepad-this is a very bad choice, because it does not have syntax highlighting function, another important reason is that it does not support text indentation function, this function we will later You will understand how important it is. And a good editor can help you complete this work automatically.
If you are already an experienced programmer, then you must be using Vim or Emacs. Needless to say, they are one of the most powerful editors, and using them to write your Python programs benefits a lot. I personally used them to write most of my programs, but also wrote a book "Entire Book on Vim".
Maybe you intend to spend time learning Vim or Emacs, then I strongly recommend you to learn one of them, they will benefit you in the long run. Of course, as I recommended earlier, beginners can start with PyCharm, thereby focusing on learning Python rather than the editor at this moment.
Again, please choose a suitable editor-it can make writing Python programs more interesting and easy.
PyCharm
PyCharm Education Edition is a free editor that can help you write Python programs.
When you open PyCharm, you will see the following interface, click Create New Project
Choose Pure Python
Change the untitled in your project path location to helloworld, the interface details you see should be similar to the one below
Click the Create button.
Right-click helloworld in the sidebar to select it, and select New -> Python File
You will be asked to enter the name, now enter hello
Now you can see a new file has been opened for you
Delete the existing content, now you enter the following code yourself:
print("hello world")
Now right-click what you have entered (no need to select text), and then click Run'hello'.
At this moment you will see what your program outputs (what it prints out)
Although it is just the first few steps, from now on, whenever we ask you to create a new file, remember to just right-click on helloworld and select -> New -> Python File and continue the steps as described above Generally enter the content and run.
You can find more information about PyCharm on the PyCharm Quickstart page.
Vim
Install Vim.
Mac OS X should install the macvim package through HomeBrew.
Windows users should download the "self-installing executable file" through the official Vim website.
GNU/Linux users should obtain Vim through the software repository of the distribution they use. For example, Debian and Ubuntu users can install the vim package.
Install jedi-vim plugin to add auto-complete function to Vim.
Install the corresponding jedi Python package: pip install -U jedi
Emacs
Install Emacs 24+.
Mac OS X users should obtain Emacs from http://emacsformacosx.com.
Windows users should obtain Emacs from http://ftp.gnu.org/gnu/emacs/windows/.
GNU/Linux users should obtain Emacs from the software repository of the distribution they use. For example, Debian and Ubuntu users can install emacs24 package.
Install ELPY.
Use a source code file
Now let's return to programming. There is a tradition when you learn a new programming language. The first program you write and run should be the "Hello World" program-all it does is declare the "Hello World" you run. This sentence. As Simon Cozens (Simon Cozens) 1 said, this is "traditional mantras praised by the gods of programming. May he help and bless you in learning this language better".
Start the editor of your choice, enter the following program and save it as hello.py.
If you are using PyCharm, we have discussed how to run it from the source file.
For other editors, open a new file name and name it hello.py and enter the following:
print("hello world")
Where should you save the file? Save to any folder where you know its location and path. If you don’t understand what this sentence means, then create a new folder and use this path to save and run all your Python programs:
/Tmp/py on Mac OS X.
/Tmp/py on GNU/Linux.
C:\\py on Windows.
To create the above folder (on the operating system you are using), you can use the mkdir command on the terminal, such as mkdir /tmp/py.
Important note: You need to verify and make sure that you give the file a .py extension, such as foo.py
To run your Python program:
Open a terminal window (you can refer to the previous installation chapter to understand what to do).
Use the cd command to change the directory to where you saved the file, for example cd /tmp/py.
Run the program by entering the command python hello.py. The output of the program should be as follows:
$ python hello.py
hello world
If you get an output similar to the one above, congratulations! ——You have successfully run your first Python program. You have also successfully crossed the most difficult part of learning to program, that is, start writing your first program!
If you encounter any errors, please confirm that you have entered the contents listed above correctly and try to run the program again. It should be noted that Python is case-sensitive, such as print and Print are different-note that the former p is lowercase, while the latter P is uppercase. In addition, you need to make sure that the first character of each line is not preceded by any spaces or tabulations-we will understand why this matter is so important later.
How it works
A Python program is composed of statements. In our first program, we have only one statement. In this statement, we call the print statement to match the text "hello world" provided by us.
Get help
If you need quick information about any Python function or statement in
Python, you can use its built-in help function. This is very useful when using interpreter prompts. For example, run the help('len') command-this will show you help for the "="https://www.yuanrenxue.com/python/python-function.html">Python len function and understand its use for calculation The number of items.
Tips: Press the q key to exit the help.
Similarly, you can get almost all information about Python in this way. Use the help() command to learn more about help itself!
If you need help with operators such as return, all you need to do is put them in quotation marks, just like help('return'), so that Python does not confuse what we are trying to do.
to sum up
You should now be able to write, save, and run Python programs easily.
Since then you have become a Python user, now let's learn more about Python concepts.