Brief introduction
We'll look at how to write a traditional "Hello World" program in Python. Through it, you will learn how to write, save, and run a Python program.
There are two ways to run your program using Python--using an interactive, prompt interpreter or using a source file. We are going to learn both of these methods.
Using an interpreter with a prompt
At the command line, at the shell prompt, type Python and start the interpreter. Now enter print ' Hello world ' and press the ENTER key. You should be able to see the output of the word Hello world.
For Windows users, if 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 an acronym for an integrated development environment. Click Start-> program->python 2.3->idle (Python GUI). Linux users can also use idle.
Note that,>>> is the prompt for you to type the Python statement.
Example 3.1 using the Python interpreter with a prompt
$ python
Python 2.3.4 (#1, Oct 26 2004, 16:42:40)
[GCC 3.4.2 20041017 (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 give you the output immediately on the next line! What you just typed is a python sentence. We use print (don't be surprised) 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 the Linux/bsd shell, press ctrl-d to exit the prompt. If it is on the Windows command line, press ctrl-z and then press ENTER.
Pick an editor
Before we start talking about writing a Python program as a source file, we need an editor to write the source file. It's really important to pick an editor. You pick an editor just as you pick a car that you will buy. A good editor helps you easily write Python programs that make your programming journey more comfortable and help you get to your destination more quickly and safely (to achieve your goal).
One of the basic requirements of the editor is syntax brightening, and with this feature, the different parts of your Python program are labeled with different colors so that you can better see your program and make it appear visualized.
If you use Windows, then I suggest you use idle. Idle features a syntax highlight and many other features, such as allowing you to run your program in idle. It is particularly noteworthy that you do not use notepad--it is a bad choice because it has no syntax highlights and, more importantly, does not support text indentation. And we'll see that text indentation is extremely important to us. A good editor, such as Idle (and VIM), will automatically help you do these things.
If you use Linux/freebsd, then you have a number of options. If you are an experienced programmer, you must already be using VIM or emacs. Undoubtedly, they are the two most powerful editors. Use them to write your Python program and you will benefit from it. I personally use vim to write most of my programs. If you're a beginner programmer, you can use Kate, and it's also one of my favorite editors. As long as you are willing to take the time to learn to use VIM or Emacs, I strongly suggest that you must learn one of the two, because in the long run they are extremely helpful to you.
If you're looking for an alternative editor, take a look at the detailed list of Python editors and make your choice. You can also use the Python IDE (integrated development environment). Take a look at the detailed list of supported Python ides for detailed information. Once you start writing large python programs, the IDE is really useful.
Once again, please choose an appropriate editor--it makes it much more fun and convenient to write Python programs.
Working with Source files
Now let's start programming again. When you learn a new programming language, the first program you write is usually the "Hello World" program, which has become a tradition. When you run the Hello World program, what it does is say, "Hello World." As Simon Cozens[1, who put forward the "Hello World" program, says: "It is the traditional mantra of the god of programming that can help you learn a language better." ”
Start the editor you selected, enter the following program, and save it as a helloworld.py.