[Learn python with me] Start from Python and start from python

Source: Internet
Author: User

[Learn python with me] Start from Python and start from python

In other words, the high-rise buildings on the ground will not climb before they go. This is especially true for me. No matter how you code first, please test first when the wings of me are hard.

1. How to Run python?

Download the python installation file from python.org first. If you are slower than org, you are not careful about it. Of course, there is also a Chinese download link ...! Then you can click next, next, and finish. When you know that this is a windows platform (of course, activepython is also excellent ). In Linux, you may have already prepared for it. I used Ubuntu10.04.3, And the mo-man 2.6 is enough for the time being. If you want to be updated, download the source code, cmms (./configuration, make, make install ). If everything goes smoothly, you can open the python IDLE under my indows. in linux, you can simply run the command directly, python. (If it doesn't work, try the path)

2. if the environment is ready, start code.

(1) give full play to the explain command line environment.

(2) Of course, 1. It cannot satisfy all our needs. But if you want to write a decent program, save the file. Save it, filename. py, and then cd it to your python source file in the command line. python filename. py and the cute python will start to work.

(3) The script is bound to our environment.

Specify the location of python in the python environment variables, such as in UT, python =>/usr/bin/python. In windows, c: \ python2.7 \ python.exe is displayed, edit the python source file and bind the python command at the beginning of the file.

At the beginning of the file #! /Usr/bin/python, or #! C: \ python2.7 \ python.exe, and then save the source file (you don't need to end with. py !),

If you still have the permission to modify the source file in linux, sudo chmod 755 filename and./filename will work!

3. python Module

When we or others have completed a set of functions, we can share them with each other. How can we call others' modules? For example, hello_world.py (saved in/home

/Tom/python/hello_world.py)

Python code
  1. Title = "I love python"

Suppose we want to use the title variable:

Open the python Interpreter:

Python code
  1. >>> Import sys
  2. >>> Sys. path
  3. ['', '/Usr/local/lib/python2.6/dist-packages/web. py-0.36-py2.6.egg ','/usr/local/lib/python2.6/dist-packages/virtualenv-1.7-py2.6.egg ','/usr/local/lib/python2.6/dist-packages/Django-1.3.1-py2.6.egg ', '/usr/local/lib/python2.6/dist-packages/tornado-2.1.1-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/PasteScript-1.7.5-py2.6.egg ', '/usr/local/lib/python2.6/dist-packages/PasteDeploy-1.5.0-py2.6.egg', '/usr/local/lib/python2.6/dist-packages/Paste-1.7.5.1-py2.6.egg ', '/usr/local/lib/python2.6/dist-packages/CherryPy-3.2.2-py2.6.egg', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2 ', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old','/usr/lib/python2.6/lib-dynload ', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL ', '/usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6 ', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0 ', '/usr/lib/python2.6/dist-packages/ages', '/usr/local/lib/python2.6/dist-packages']

We checked the path of the python system to load the module, but obviously there is no file path for hello_world.py. As a result, hello_world.py cannot be found, so we need to add the hello_world path as long:

Python code
  1. Sys. path. append ('/home/tom/python ')

Then we can import the python module,

Python code
  1. Import hello_world
  2. Print hello_world.title
  3. From hello_world import title
  4. Print title

Two methods.

When the module File is imported into the python session for the first time, the python interpreter will fully execute the code in one module. If you import the same module to the python interaction environment again, python will not execute any more. All the code for changing the module during this period, and the python interaction environment is still the old code. To load new code, use reload

For example:

Python code
  1. Reload (hello_world)

Have you learned the Python source code, that is, the Python implementation?

Let's take a look at Python source code analysis.

Which version of python is used in this book to learn programming with children?

The book uses 2.5.1, which is evidenced by the pictures on the second page of the first chapter of the text (I see the English version ). 2.6 and 2.7 are also compatible.
The Python3 syntax has changed. The previous program must be modified before it can run. 2.7 is recommended for learning.

Www.python.org/ftp/python/2.7/python-2.7.msi

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.