An overview of python and an introduction to basic syntax elements

overview

introduce

Python is a cross-platform computer programming language, a substitute for the ABC language, and an object-oriented dynamic type language. Originally designed for writing automation scripts (shell), as the version is continuously updated and new language features are added, it is more and more used for the development of independent and large-scale projects.

Python supports several programming paradigms, including functional, imperative, structured, object-oriented, and reflective programming. The Python interpreter is easily extensible, and can be extended with new functions and data types using C or C++ (or other languages ​​that can be called from C). Python is also used as an extension programming language in customizable software. Python has a dynamic type system and garbage collection, can automatically manage memory usage, and itself has a huge and extensive standard library, providing source or machine code for each major system platform.

Python is a high-level scripting language that combines interpretability, compilation, interactivity and object-oriented.

  • Python is an interpreted language: this means that there is no compilation part of the development process. Similar to PHP and Perl languages.
  • Python is an interactive language: this means that developers can execute code directly after a Python prompt >>>.
  • Python is an object-oriented language: This means that Python supports an object-oriented style or programming technique in which code is encapsulated in objects.
  • **Python is a Beginner's Language: **Python is a great language for beginning programmers that supports a wide range of application development, from simple word processing to WWW browsers to games.

Advantages and disadvantages of Python

Advantages of Python:

  • high software quality

    Python upholds a concise, clear syntax, and a highly consistent programming model. A consistent design style can guarantee the development of fairly standardized code.

    For errors, Python provides a "safe and reasonable" exit mechanism. Python supports exception handling, which can effectively capture and handle errors that occur when the program is running, and can monitor and handle these errors.

  • fast development

    Python is committed to the optimization of development speed: concise syntax, dynamic types, no need to compile, rich library support and other features allow programmers to quickly carry out project development. Python often requires only a few dozen lines of code to develop functions that require hundreds of lines of C code.

  • Powerful

    Python is powerful enough, and has many interfaces to other systems, that it is entirely possible to prototype entire systems in Python.

    In order to complete more specific tasks, Python has built-in many pre-coded library tools, ranging from regular expressions to network programming, to database programming. In the field of web, data analysis, etc., Python also has a powerful framework that can help rapid development of services. For example: Django, TruboGears, Pylons, etc.

  • easy to expand

    Python is easily extensible, and (in the case of CPython) functionally extensible via modules written in C or C++, making it a flexible glue language for scripting the behavior of other systems and components.

  • Cross-platform

    Python is cross-platform. Python can be found on various operating systems (Linux, windows, MacOS, Unix, etc.). Because Python is written in C, and because of the portability of C, Python can run on any platform with an ANSI C compiler. Although there are some specific modules developed for different platforms, general-purpose software developed in Python on any platform can be slightly modified or run on other platforms intact. This portability applies both to different architectures and to different operating systems.


Disadvantages of Python

  • Python runs very slowly compared to C/C++ programs

    Because Python is an interpreted language, the code will be translated line by line into machine code that the CPU can understand when it is executed. This translation process is very time-consuming, so it is very slow. The C program is directly compiled into machine code that the CPU can execute before running, so it is very fast. However, according to the 28th law, most programs do not require high speed. In some situations that require high running speed, Python designers tend to use JIT technology, or rewrite this part of the program in C/C++ language.

    And development time is a precious resource, more precious than CPU time. The benefits of Python development speed often outweigh the losses of execution speed, especially given the high processing power of modern computers. Some improvements may be too difficult to implement, or may affect the portability or maintainability of the code. Not all performance improvements are worth the effort.

  • Code cannot be encrypted

    If you're distributing a Python program, you're actually distributing the source code. This is different from the C language. The C language does not need to release the source code, but only the compiled machine code. It is impossible to completely deduce C code from machine code.


The development direction and application scenarios of Python

  • web development

    Python provides a wealth of modules to support sockets programming, multi-thread programming, and can easily and quickly develop network service programs. It supports the latest XML technology, json language, database programming, and Python's ORM framework, making it very convenient to operate the database.

    Python also has excellent web development frameworks such as Django, Tornado, and Flask, and is supported by numerous open source plug-ins, which are sufficient to meet various web development needs.

  • Automated operation and maintenance

    Python's built-in interface to operating system services makes it ideal for writing portable management tools and widgets for maintaining operating systems. Python programs can search files and directory trees, run other programs, and use processes and threads for parallel processing.

  • Web Crawler

    In terms of text processing, the re module provided by python can support regular expressions, and also provides SGML and XML analysis modules. Many programmers use python to develop XML programs.

  • graphics processing

    Supported by graphics libraries such as PIL and Tkinter, it is convenient for graphics processing. For multimedia applications, Python's PyOpenGL module encapsulates the "OpenGL application programming interface", which can perform two- and three-dimensional image processing. The PyGame module can be used to write game software

  • In addition, Python is also used in games, artificial intelligence, big data analysis, robotics and other fields. Python is the preferred programming language for artificial intelligence.


Types of Python

  • Cpython

    Execution process: program----(C interpreter)----(bytecode)----(machine code)----cpu

    The official version of Python, the original and best-maintained implementation of Python, written in C. New language features generally appear here first.

    The CPython implementation converts source files (py files) into bytecode files (pyc files), which are then run on the Python virtual machine.

  • Jyhton

    Execution process: program----(java interpreter)----(bytecode)----(machine code)----cpu

    The Java implementation of Python that can be used as a scripting language for Java applications, or to create applications using Java class libraries, and is often used to create tests for Java libraries. Jython dynamically compiles Python code into Java bytecode, and then runs it on the JVM.

  • PyPy

    Execution process: program----(bytecode)----(machine code)----cpu

    A Python implementation written entirely in Python. It supports several advanced features not found in other implementations, such as stackless support and a Just in Time compiler. PyPy translates Python source code into bytecode and then compiles it into machine code.

  • Other categories: Python for .NET, IronPython, RubyPython, Brython…


Python version (python 2, python 3)

There are two versions of python, python 2.X and python 3:

  • The last version of python 2.x is python 2.7 (released in 2010), which will be officially supported until 2020.

  • The first version of python 3.X python 3.0 (released in 2008)

    Python 3.X is not compatible with Python 2.X, and python2.6 is officially released as a transition version to Python 3.0, and all versions after 2.6 are transition versions.

Why develop Python 3.X version? Because python has been developed for more than 20 years, there are many repeated functions and modules, and many codes have become less concise, so Guido decided to carry out a thorough upgrade. X version.


basic grammar

basic grammatical elements

indentation and comments

  • indentation

    In java and c/c++ languages, "{ }" is used to represent a code block.

    Python uses indentation (spaces, preferably 4 spaces) to represent code blocks, no need to use curly braces {}, and multiple consecutive statements with the same indentation amount constitute a code block.

    The number of indented spaces is variable, but statements in the same code block must contain the same number of indented spaces .

    For example: statements such as if, for, while, def, and class all use code blocks. Usually, a colon at the end of a statement represents the beginning of a code block, as in the following example:

    if x>0:
        y=1
    else:
        y=-1    # 注意:同一个代码块中的语句,其缩进量应相同,否则会出现 indentationError (缩进错误)异常。
    
  • note

    Add descriptive statements to programs to help programmers read and understand code.

    • Single-line comment: # comment content
    • Multi-line comment: """ comment content """ or ''' comment content'''

Statement continuation character ( \ ) and statement separator ( ; )

  • Statement continuation symbol ( \ )

    Usually a statement in Python occupies one line, there is no end-of-statement symbol. When the statement is long, you can use backslash ( \ ) to achieve multi-line statement

    Note: After the symbol ( \ ), there cannot be other symbols, including spaces and comments.

  • statement separator ( ; )

    Multiple statements separated by a statement delimiter can be regarded as a compound statement, and Python allows individual statements or compound statements to be written after the colon.


Reserved words and keywords

  • Reserved words are words reserved in the programming language for use after version upgrades.

    Keywords are words in a programming language that are commands or constants.

  • Notice:

    • Reserved words and keywords are not allowed as variables or other identifiers.
    • Python is case-sensitive for identifiers, and reserved words and keywords must be strictly case-sensitive.

The meaning and function of the 35 reserved words in Python 3.7.2

serial number reserved word illustrate
1 and Logical AND operation, used for expression operation
2 as Used to convert data types
3 assert Used to judge the result of a variable or conditional expression
4 async Used to enable asynchronous operations
5 await Used in asynchronous operations to wait for coroutines to return
6 break Interrupt execution of loop statement
7 class define class
8 continue continue to the next loop
9 def define a function or method
10 of the remove the value of a variable or sequence
11 elif Conditional statement, used in conjunction with if and else
12 else Conditional statement, used in conjunction with if, else; can also be used with exceptions or looping statements
13 except Contains the processing code block after catching the exception, used in combination with try and finally
14 False logical value meaning "false"
15 finally Contains a block of code that is always called after catching an exception, used in conjunction with try and except
16 for loop statement
17 from Used to import modules, used in conjunction with import
18 global For using global variables in functions or other local scopes
19 if Conditional statement, used in combination with elif and else
20 import Import modules, used in conjunction with from
21 in Determine whether a variable is in a sequence
22 is Determine whether a variable is an instance of a class
23 lambda define anonymous function
24 None Represents an empty object or a special null value
25 nonlocal Used to use outer (non-global) variables in functions or other scopes
26 not Logical NOT operation, used for expression operation
27 or Logical or operation, used for expression operation
28 pass placeholder for an empty class, method, or function
29 raise used to throw an exception
30 return returns the result of a calculation from a function
31 True A logical value meaning "true"
32 try Test execution code that may be abnormal, used in combination with except, finally
33 while loop statement
34 with Simplify Python statements
35 yield return values ​​from functions in turn

Guess you like

Origin blog.csdn.net/footless_bird/article/details/132663890
Recommended