First knowledge of Python

Source: Internet
Author: User
Tags echo command

Introduction
Let's take a look at how to use python to run the "Hello World" program, which will teach you how to write, save, and run the python program.
There are two ways to run the python Program-use the interactive interpreter or execute the source file. Now let's take a look at the two methods respectively.

 

Use an interactive Interpreter
Enter"Python"To start the interaction interpreter.
If Windows users want to use idle, click Start> program> Python 3.0> idle (Python GUI.
EnterPrint ("Hello World "),Press enter and you will see the screen output Hello world.
$ Python
Python 3.0b2 (r30b2: 65106, Jul 18 2008, 18:44:17) [MSC v.1500 32 bit (Intel)] on Win32
Type "help", "Copyright", "Credits" or "License" for more information.
>>> Print ('Hello World ')
Hello World
>>>

Note that python provides the output line immediately! You just entered a python statement.
We use print to print anything that is provided to it. Here we provide Hello world and then immediately print it to the screen.
How do I exit the interaction interpreter?
Enter Ctrl-D in idle or Linux/BSD shell and exit. In Windows, enter Ctrl-Z and press Enter.

 

Select a python Editor
Before writing Python source files, we need a text editor. It is very important to select a good editor, just as you are buying a car.
A good editor allows you to easily write Python programs, allowing you to quickly and securely reach your destination on a journey.
One of the basic conditions for selection is syntax highlighting. Different parts of the python program will be given different colors, so you can easily identify the code,

It is easier to visualize the running process in the brain.
If you are a Windows system, I suggest using idle. It not only provides syntax highlighting but also has many other features, such as running your program in idle.
A special note:Do not use Notepad-This is a bad choice because it does not support syntax highlighting, and more importantly, it does not support automatic text.

Indent (you will see this function is very important later), and a good editor such as idle (or Vim) will automatically help you do this.
Linux/FreeBSD has many options. If you are a beginner in programming, you may want to use geany. It provides a graphical user interface with buttons for compilation and

Run the functions of the python program. For experienced programmers, VIM or Emacs must be used. Needless to say, this is two powerful editors,

Writing Python programs using them will certainly benefit you a lot. I personally use Vim to write most of my programs.
If you are a cainiao programmer, you can use Kata, which is one of my favorites.

If you are willing to spend some time learning vim or Emacs, I strongly suggest you study one of them. In the long run, this will be very helpful to you.
In this book, you can either use idle, integrated development environment (IDE), or your favorite editor.
Idle has been installed by default in the python installation package of Windows and Mac OS X,
Linux can also get its installation package (http://love-python.blogspot.com/2008/03/install-idle-in-linux.html)
The idle of the BSD system is in their respective databases (repositories)
We will explore idle usage in the next section. For more information, see idle document (http://www.python.org/idle/doc/idlemain.html ).
If you want to study other available editors, see the python editor list (http://www.python.org/cgi-bin/moinmoin/PythonEditors)
You can also select the python ide. For details, see the list of Python-supported ides.

Http://www.python.org/cgi-bin/moinmoin/IntegratedDevelopmentEnvironments)
First, you can write large python programs. Ide is very useful.
Finally, I will repeat it again. Please select a suitable editor that makes the compilation of Python programs more interesting and easy.

 

Use source files
Now let's go back to programming. There is a tradition. When learning a new programming language, the first program to be written and run is called "Hello World ",

All of its functions are simply to print "Hello world". As Simon cozens said, it is the magic of the programming Emperor to help people better learn languages :)
Start your editor, enter the following program and save it as a fileHelloworld. py.
The idle user clicks file → new window, then enters the following program and clicks file → save)
#! /Usr/bin/Python
# Filename: helloworld. py

Print ('Hello World ')
Start a shell (Linux terminal or DOS prompt (Note: Windows command line) and enter the commandPython helloworld. py
Click RUN> RUN module or press the keyboard shortcut key f5.

The program output is as follows.
$ Python helloworld. py
Hello World

If you get the above output, congratulations! You have successfully run your first Python program.
Otherwise, check your input according to the code above and run the program again.
In addition, it should be noted that python is case sensitive, that is, print and print are different, while the latter is case sensitive.
Make sure there are no spaces or tabs before the first character of each line. I will explain the importance of this in the future.

 

How the program works
Comments-symbols for the first two behaviors of the program#Any character on the right is treated as a comment. The main function of the comment is to read the reader's notes.
Python annotations are optional, but the first line is an exception. It is called a transaction line (shebang line )-#!The starting position followed by the running position.
In Linux/Unix, this will tell the system that the program will run under the specified Python interpreter. This will be explained in detail in the next section.
In addition, by specifying the python interpreter directly in the command line, you can always let the program run on any platform, just like the commandPython helloworld. py

Key Points
The rational use of annotations to describe the important details of the program is very beneficial to the readers of the Code, so that they can easily understand the workflow of the program.
Remember, this reader may be you six months later!
The comment is followed by a python statement. The print we call is a function, where it only prints the text "Hello World ".
In the next chapter, we will learn functions. What you need to understand now is what you provide in parentheses and what print will output on the screen.
In this example, we provide 'Hello world', which is called a string-does not understand what "string" is? This term will be studied in detail later.

Run the python Program
This part only applies to Linux/Unix users, but Windows users may be curious about the first line of the program.
First, we must use the CHMOD command to assign executable properties to the program before we can run it.
$ Chmod A + x helloworld. py
$./Helloworld. py
Hello World

Here, the CHMOD command changes the file mode so that all users in the system can run this file.
Then we run the program by specifying the location of the source file,./Indicates that our program is in the current directory.
To be more interesting, rename the file as helloworld and run it. You will find that the program can still work,

This is because the system knows that it must run the program using the interpreter specified in the first line of the source file.
What if you don't know the location of python? You can also use the Env program on Linux/Unix. Modify the first behavior of the Code:
#! /Usr/bin/ENV Python
In this way, the Env program will find the python interpreter that will run the program in sequence.
So far, we only need to specify the exact path to run our program. But what if we want the program to run anywhere?
When you run any program, the system will find the directory specified in the environment variable path and execute it. Therefore, copy the programPathThis can be done in a directory in.
$ Echo $ path
/Usr/local/bin:/usr/x11r6/bin:/home/swaroop/bin
$ CP helloworld. py/home/swaroop/bin/helloworld
$ Helloworld
Hello World

We can use the echo command to displayPath, Prefix $ tells shell that we need to display the value of path.
We can see that/Home/swaroop/binYesPathA directory in which swaroop is the user name I use in my system,

There is a similar directory in your system. Of course, the user name is yours.
As another option, you can add a new directoryPath, You can runPath = $ path:/home/swaroop/mydir.
'/Home/swaroop/mydir'Is the directory to be added.
This method is very effective if you want your program to run anywhere at any time.

This is like a command created by yourself, like a CD or other command on a Linux terminal or command line.

Prompt
In python, the concepts of program scripts or software are the same.

 

Get help
If you need to obtain quick information about any functions or statements in Python, you can use the built-in help function.
This is useful when an interpreter is used. For exampleRun (print)-The help of the built-in print function is displayed.
Prompt
Press Q to exit the help.
With help, you can obtain almost any information in Python, and enterHelp ()You can also get help!
For exampleReturnIn this case, you need to enclose it in quotation marks.Help ('Return ').

Summary
Now you should be able to easily write, save and run Python programs. Now that you have become a python user, let's learn more about Python concepts.

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.