Python simple tutorial, python simple tutorial

Source: Internet
Author: User

Python simple tutorial, python simple tutorial
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, all it does is say "Hello World ". As Simon Cozens [1], who proposed the "Hello World" program, said: "It is a traditional spell of the god of programming and can help you better learn languages ."

Start the editor you selected, enter the following program, and save ithelloworld.py。

Example 3.2 use source files

#!/usr/bin/python
# Filename : helloworld.py

print 'Hello World'

(Source file: code/helloworld. py)

To run this program, open the shell (Linux terminal or DOS prompt), and then type the commandPythonHelloworld. py. If you use IDLE, use the menu Edit-> Run Script or the keyboard shortcut.Ctrl-F5. The output is as follows.

Output

$ python helloworld.py
Hello World

If the output you get is the same as the one shown above, congratulations! -- You have successfully run your first Python program.

In case you get an error, make sure that the program you typedAccurateAnd then run the program. Note that Python is case sensitive, that isprintAndPrintDifferent -- note that the first one is in lower casepThe last one is in uppercase.P. In addition, make sure there are no spaces or tabs before the start character of each line-we will discuss why this is important later.

How it works

Let's think about the first two lines of this program. They are calledNote-- Any content on the right of the # symbol is annotated. Annotations are mainly used as notes provided to program readers.

Python should have at least the special form of comments in the first line. It is calledOrganization row-- The first two characters of the source file are #!, Followed by a program. This line tells your Linux/Unix system when youRunWhich interpreter should be run for your program. This will be explained in detail in the next section. Note: You can always specify the interpreter directly on the command line to run your program on any platform. Just like a commandPythonHelloworld. pySame.

Important
Rational use of comments in your program to explain some important details-this will help readers of your program easily understand what the program is doing. Remember, this reader may be you six months later!

After the annotation, a PythonStatement-- It only prints the text "Hello World ".printIt is actually an operator, and "Hello World" is called a string-don't worry, we will explain these terms in detail later.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.