Python Learning record One

Source: Internet
Author: User

I. Some comparisons of Python with other languages :

C and Python, Java, C #, etc.

C Language: The code compiles the machine code, the machine code executes directly on the processor, each instruction controls the CPU to work

Other languages: Code compiles to get bytecode, virtual machines execute bytecode and convert to machine code and then execute on processor

The language of Python and C Python was developed by C.

For use with: Python's class library is complete and concise, if you want to implement the same functionality, Python 10 lines of code can be solved, C may require 100 lines or more.
For speed: Python runs faster than C, forcing it to slow down.

Python and Java, C #, etc.

For use with: Linux original Python, no other languages; The above languages have a very rich class library support
For speed: Python may be slightly slower in speed

So, there is no essential difference between Python and other languages, other differences are: good at a field, rich in talent, preconceived.

Two.types of Python:

    • Cpython
      The official version of Python, implemented using the C language, is the most widely used, and the CPython implementation converts the source file (py file) into a bytecode file (PYc file) and then runs on the Python virtual machine.
    • Jyhton
      Python Java implementation, Jython will dynamically compile Python code into Java bytecode, and then run on the JVM.
    • IronPython
      In Python's C # implementation, IronPython compiles Python code into C # bytecode and then runs on the CLR. (similar to Jython)
    • PyPy (Special)
      Python implements Python, which compiles Python bytecode bytecode into machine code.
    • Rubypython, Brython ...
    • at present, we are studying CPython. PyPy is a trend.

Three. Parser:

1. The way scripts can be executed under Windows: After configuring the environment variables, the DOS interface is used to perform

2. The way scripts can be executed in a Linux environment: {

The first method:

The second method: To modify the permissions of the file, you can change to chmod 755 lixin.py ( This 755 permissions, not absolute, change to other permissions can also run, such as 744 permissions ), and then the head of the file plus #!/usr/bin/env Python This code, you can take Note:./Follow the Py file immediately thereafter, no spaces. }

Four. Code:

ASCII code: (American Standard Code for information interchange, US Standards Information Interchange Code) is a Latin alphabet based computer coding system, mainly used to display modern English and other Western European languages, which can be used up to 8 Bit to represent (one byte), that is: 2**8 = 256, so the ASCII code can only represent a maximum of 256 symbols.

Unicode (Uniform Code, universal Code, single code) is a character encoding used on a computer. Unicode is created to address the limitations of the traditional character encoding scheme, which sets a uniform and unique binary encoding for each character in each language, which specifies that characters and symbols are represented by at least 16 bits (2 bytes), that is: 2 **16 = 65536,
Note: Here is a minimum of 2 bytes, possibly more

UTF-8, which is compression and optimization of Unicode encoding, does not use a minimum of 2 bytes, but instead classifies all characters and symbols: the contents of the ASCII code are saved with 1 bytes, the characters in Europe are saved in 2 bytes, and the characters in East Asia are saved in 3 bytes ...

when the 2.x version of the Python interpreter loads the code in the. py file, the content is encoded (default Ascill), if the following code is the case:

Error: ASCII code cannot be expressed in Chinese

123 #!/usr/bin/env python print"你好,世界"

Correction: It should be shown to tell the Python interpreter what code to use to execute the source code, i.e.:

1234 #!/usr/bin/env python# -*- coding: utf-8 -*- print"你好,世界"

3.x version can not add #-*-Coding:utf-8-*-,python interpreter when the code in the. py file is loaded, the content is encoded (UTF-8), and no garbled characters appear.

Five. NOTES:

When the line stares: # is annotated content

Multiline Comment: "" "" "" "" ("" " can be misaligned " "")

Six. pyc file

When you execute Python code, if you import a different. py file, a. pyc file with the same name is automatically generated during execution, which is the bytecode generated after the Python interpreter was compiled.

PS: Code is compiled to generate bytecode, and bytecode can be obtained by decompile. ( Note: Even if you delete the Py file, the. pyc file will work as well )

Seven. Variables

The role of a variable: a nickname that refers to what is stored in an address in memory

Rules for naming variables:

    • Variable names can only be any combination of letters, numbers, or underscores
    • The first character of a variable name cannot be a number
    • The following keywords cannot be declared as variable names
    • [' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' exec ', ' finally ', ' for ', ' F ' Rom ', ' Global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' not ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' try ', ' while ', ' WI Th ', ' yield ']
    • Variable naming is short and representative ( in the actual development, such as: Name a name, cannot write a to name, can use a name to represent meaning, easy program maintenance )

Eight. Enter

1. the input of the 2.x version is raw_input ()

2. the input of the 3.x version is "Input" ()

3. When entering a password, if you want to be invisible, you need to take advantage of the Getpass method in the Getpass module, namely:

12345678910 #!/usr/bin/env python #-*-coding:utf-8-*-    import  getpass     # assigns user-entered content to the name variable pwd  =  getpass.getpass (     # print the input content print  pwd

Nine. If Else statement

Note: The print "User name and password error" is the above else, so you want to indent it.

10. If elif Else statement

Note: If elif else statement. As long as one of these is true, the rest of the statement will not be executed directly out of the way.

Eleven. While Loop

1. Basic cycle

123456 while条件:         # 循环体    # 如果条件为真,那么循环体则执行    # 如果条件为假,那么循环体不执行

2. Break

Break to exit all loops

1234 whileTrue:    print "123"    break    print"456"

3, continue

Continue is used to exit the current loop and continue the next loop

1234 whileTrue:    print "123"    continue    print"456"

Python Learning record One

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.