Python Study the third day

Source: Internet
Author: User

Python Study the third day

1. python file types

1) Source code

Python source file with 'py' as extension, interpreted by Python program, no need to compile

Example 1: vim 1.py

#!/usr/bin/python As with the shell script, you need to indicate which program is explaining it)

Print ' HelloWorld '

Chmod a+x 1.py

./1.py

Run the result: Hello World

2) byte code

Python source code files are compiled to generate a file with the extension 'pyc'

The compilation method is import py_compile

py_compile.compile ("python source code file name")

Example 2:vim 2.py

#!/usr/bin/python

Import Py_compile

Py_compile.compile (' 1.py ')

chmod a+x 2.py

./2.py

Run Result: A file with PYc suffix is generated in this directory (binary file)

3) optimized code

Optimized source file with the extension 'pyo'

python–o–m py_compile python Source code file name

Example 3: python–o–m py_compile 1.py

The result is: A binary file with Pyo suffix is generated under this directory. That is, the optimized source code file

Note: 1. Files with the Py,ypc,pyo suffix in the above can be run.

Python 1.py or Python 1.pyc or python1.pyo

2. Since 1.PYC and 1.pyo do not indicate that the program is processed in it, they are not allowed to use it even if they have given execute permission./File name execution

3. Use compilation and optimization to get code to be executed faster

2. variables in python

1) variable definition: variable is an area of computer memory, variable can store the value within the specified range, and the value can be changed.

2) naming rules for variables

Variable names consist of letters, numbers, underscores

Numbers can not start

You cannot use keywords

3) Assignment of variables

is actually the process of declaring and defining variables.

Example 4:a=1 print a execution result of 1 (can be verified in interactive mode)

Attention:

1. A=12 ID (a)

a=345 ID (a) where ID (a) is not the same

The equivalent of changing the label to a place

2. a=123 b=123 See both ID (a) and ID (b) Find the same (I found it doesn't seem to support this in my Linux version)

The equivalent of two labels affixed to one memory data.

Abnormal phenomenon:

1. syntaxerror syntax error

2. Nameerror The name of the variable, function, or class is wrong.


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.