First, Python introduction, variables, input and output

Source: Internet
Author: User
Tags python script

Start with a light:

  Good memory is worse than bad writing, it is really this truth!!

First, understand the programming

The purpose of programming:

    Programming languages are used to develop programs that let computers work and liberate your hands.

Programming Languages:

    Just like human language, programming language is the language in which computers communicate with people.

  Program:

    A program is a code file written in a programming language.

II. Classification of programming languages

    Machine language:

Machine language is a set of machine instructions that can be directly identified and executed by a computer using binary code.      

Advantages: flexible, direct execution and fast speed.      

Cons: Different models of computers whose machine language is not interlinked, according to a computer machine instructions compiled program, can not be executed on another computer.

    Assembly:

The language of assembly language is the same as that of machine, which is directly to the hardware, but the instruction is abbreviated in English, and the identifier is easier to recognize and remember. It also requires programmers to write each step of a specific action in the form of a command. Advantages: can accomplish the general high-level languages can not achieve the operation, and the source program generated by the compilation of the executable file is relatively small, and execution speed quickly.      

Disadvantage: The source program is verbose, complex, error prone, and the use of assembly language programming requires more computer expertise.

    Advanced Language:

Specifically, high-level languages are spoken, and programmed with human-readable (such as English) characters. High-level languages are the choice of most programmers. Compared with assembly language, it not only synthesizes many related machine instructions as a single instruction, but also removes details related to the operation but not relevant to the completion of the work, such as the use of stacks, registers, etc.      

Advantages: greatly simplifies the instructions in the program. At the same time, because of the omission of a lot of details, programmers do not need to have too much expertise.      

disadvantage: High-level language programming program can not be directly recognized by the computer, must be converted to be executed.

    Conversion Mode classification

explanation class: The application source code is "translated" by the interpreter of the corresponding language into the target code while executing        

Advantages: This method is more flexible, debugging program is very convenient, once the program error, immediately debugging can test the results immediately        

Cons: inefficient, and cannot generate executable files that can be executed independently, the application cannot detach itself from its interpreter. Only in the execution of the program, only one interpretation of the machine language to the computer to execute, so the speed is not as fast as the compiled program to run.

      Compile class: compile means to "translate" the program source code into a machine instruction and save it as a binary file before the program executes.        

Advantages: The compiled code can be run directly on the machine and run at a higher speed than the interpreted type.        

Disadvantage: Debugging Program Trouble, once the program needs to be modified, you must first modify the source code, and then recompile to get the binaries to execute.

 

Programming language experience: Machine language--------> assembly language------------> Advanced language

Since high-level language execution is inefficient, why use it????

High-level language certainly no machine and assembly fast, but now the hardware technology developed, mainly focus on development efficiency!! Time is Money!!

Start python! (based on 3.6.1)

  the founder of Python is Guido van Rossum (Guido van Rossum), which was developed during the Christmas of 1989, as an inheritance of ABC (crouching trough, look at others)

Python's application involves every aspect! Don't say it, too much.  

  Python is interpreted as a language:

Python generates bytecode files during execution, but bytecode files are not saved and the next time they are re-interpreted to execute

Python bytecode files are also retained, only when the module is imported

Pyhon code cannot encrypt (now all popular open source, add what secret!) )

Types of Python interpreters:   

      • Cpython (most commonly used version)
        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
        Pytho's AVA implementation, Jython will dynamically compile Python code into Java bytecode and run it 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 ...

Download the installation:

Online search, a lot, very easy, don't forget to configure environment variables

Note: Linux is self-contained, if you want to change the new version, you need to install a Pyhon

Multi-version coexistence:

Windows

Add two python versions of the path to the environment variable and the system will start looking for the interpreter from scratch, so entering Pyhon on the command line will execute the first version of the Python.exe file;

We have changed the program file name to Python3.exe (version 3) and Python2.exe (version 2) so that we are free to choose (the PIP Installation tool is the same)

Linux

1. Install the new version of the source code (user/local)

2. Add the new Bin directory to the BASHRC behind etc (avoid yum error)

The Yum tool is written in Python and the system installs Python by default

To permanently modify environment variables:

Vim/etc/profile

At the end of the file, add and Save:

            

Input: Source/etc/profile immediately (or re-open terminal)

Change the Python interpreter for the bin directory to Python3

MV Python Python3

            

This way Python uses the 3 version by default using Python2,python3

Note the Yum command error, which defaults to finding Python under User/bin

The first Python program!!

First, interactive mode

      Enter python into interpreter

  

     Knocks one row at a line result for debugging

After exiting (exit ()) The code is gone and the code cannot be saved permanently

  Second, the file mode

Windows

Save code with text

      

Perform

      

Note: The file name is then changed to. py end, used to differentiate

      In Linux

      

can use./execute, to add a file header to the file specify interpreter (only in Linux)

      

Plus Execute permissions

      

      

  Specify encoding format 

Python Execution Program phase:

1. Start the Interpreter

2. The interpreter reads the contents of the file into memory

3. Interpreter Interpretation Execution Code

Python Comments:

Solo

Multiple lines of "content" "" "" Content "" "

   

Python script wear parameters (for a moment to understand)

Want to wear parameters as follows

      

      Import Modules First

        

     parameter is passed in.

       

PYc file (import hold, start with say)

Direct execution, no PYc file in directory

    

   Execute as import (-M)

   So dir view directory, there will be PYC files, this pyc can run directly

 

Variable

  The quantity that can be changed, the state of record

The variable can be any type

    Variable name specification:      

can only be any combination of letters, numbers, or underscores
The first character 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 ']

declarations and references

  Statement

Variable name  =  Variable Value name = Shuai # Two is a binding relationship

  Reference

Print (name)                # Shuai

  The principle of defining variables

Define a variable, open a "room" in memory and store the value, then the variable name points to the room;

    Such as:

A points to 1, and when a changes to point 2 o'clock, the relationship to 1 is broken, but 1 is still present (multiple variables can point to a value (same block of address))

        

    Memory Reclamation:

The variable takes up memory, and the value of a variable with a reference count of zero (memory value without binding variable name) is automatically cleaned up

    

  View variable memory address (ID):

     

There is no new space to store the values of N and MSG2???? This is not the same as the previous variable definition??

Python is optimized, and for this smaller value, there is a direct point in memory, eliminating some memory overhead

Input/Output

Input:

Input () receives user input

Type () view types

s = input (' input: ')                       #  Input: 1print (s)                                   # 1print (type (s))                        #<class ' str ' >

 Input converts all input content to a string

In the 2 release:

Input () What data type is entered and what data type

Raw_input () as in version 3 of input ()

s = input (' input: ')                       #  Input: 1print (s)                                   # 1print (type (s))                        #<class ' int ' >

  

Liezi:

Import Getpass
Name = input (' User name: ')
PWD = Getpass.getpass (' Password: ')
Print (NAME,PWD)

Getpass Change Password to not display input

Note: Now this getpass only works on the command line

Output:

Print ()

Print to screen, multiple elements with, separate

%s format symbol, comma can only be followed by printing,%s may be anywhere

Note: Print is a function in version 3, parentheses are required, and version 2 does not require

First, Python introduction, variables, input and output

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.