Run the Hello World Program in Python
Let's take a look at how to write and run a traditional "Hello World" program in Python. Through it, you will learn how to write, save, and run Python programs.
There are two ways to run your program using Python-using an interactive interpreter with a prompt or using a source file. We will learn these two methods.
Use an interpreter with a prompt
Type python at the shell prompt of the command line to start the interpreter. Enter print 'Hello world' and press Enter. You can see the output word Hello World.
For Windows users, as long as you set the PATH variable correctly, you should be able to start the interpreter from the command line. Or you can choose to use the IDLE program. IDLE is the abbreviation of integrated development environment. Click Start> program> Python 2.3> IDLE (Python GUI ). Linux users can also use IDLE. Note: >>> is the prompt for you to type a Python statement. Example 1 use a Python interpreter with a prompt
- $ python
- Python 2.4.3 (#1, Jul 26 2006, 16:42:40)
- [GCC 3.4.2 20050110 (Red Hat 3.4.2-6.fc3)] on linux2
- Type "help", "copyright", "credits" or "license" for more information.
- >>> print 'hello world'
- hello world
- >>>
Note that Python will output your output immediately in the next line! You just typed a Python statement. We should not be surprised to use print) to print the value you provided to it. Here we provide the text Hello World, which is quickly printed on the screen.
How to exit the Python prompt: if you are using Linux/BSD shell, press Ctrl-d to exit the prompt. If it is in Windows command line, press Ctrl-z and then press Enter.
Select an editor
Before writing a Python program as a source file, we need an editor to write the source file. Selecting an editor is really important. You can select an editor just like you can select a car you will buy. A good editor will help you easily compile Python runtime programs, make your programming journey more comfortable, and help you reach your destination more quickly and securely ). The above article runs on Python.
One of the basic requirements of the editor is the syntax brightening function. With this function, different parts of your Python program are marked with different colors, so that you can better understand your program, to visualize its operation.
If you are using Windows, we recommend that you use IDLE. IDLE has the syntax brightening function and many other functions, such as allowing you to run your program in IDLE. Note: Do not use Notepad-it is a bad choice, because it does not have the syntax brightening function, and more importantly, it does not support text indentation. We will see that text indentation is extremely important to us. A good editor, such as IDLE and VIM, will automatically help you do these tasks.
- How to better learn Python built-in objects
- Introduction to three types of Python types
- Python 3.0 beta 1 ultra-Concise syntax usage
- How to Implement element variables in the Python list
- The Python splitter teaches you how to operate on an article.
If you use Linux/FreeBSD, you have many options. If you are an experienced programmer, you must be using VIM or Emacs. Undoubtedly, they are two of the most powerful editors. You will benefit from using them to write your Python program. I personally use VIM to write most of my programs. If you are a beginner in programming, you can use Kate, which is also one of my favorite editors.
As long as you are willing to spend time learning to use VIM or Emacs, I strongly recommend that you learn either of them, because they are extremely helpful in the long run.
Use source files
Now let's start programming again. When you learn a new programming language, the first program you write and run is usually a "Hello World" program, which has become a tradition. When you run the "Hello World" program, what Python does is to say "Hello World ". As Simon Cozens, who proposed the "Hello World" program, said, "It is a traditional spell of the god of programming and can help you better learn languages ."
The above is an introduction to the actual usage of running the Hello World Program in Python.