current position:Home>Python Basics - (1) overview of Python

Python Basics - (1) overview of Python

2022-06-24 07:50:57July is your lie

Python brief introduction

        Python The English of is “ boa constrictor ” It means , But its author Guido van Rossum I'm going to call it python The reason is that he is a TV play 《Monty Python's Flying circus》.

The common saying is :python It's a scripting language , But it goes far beyond that ! In addition to being a scripting language , Or a cross platform 、 Open source free Interpretive high-level dynamic programming language , And support object-oriented Programming ideas and methods (python Everything is an object ).

         In addition to being able to interpret execution ,python Can also be .py file Pseudo compilation Run as bytecode file , So as to improve the loading and running speed , And encrypt the source code .

         To make a long story short ,python It's a door The whole scene Language ! But in terms of usage distribution and characteristics , It is used in numerical calculation 、 Artificial intelligence 、 Script development and other fields are popular .

Python Version selection

     (Welcome to Python.org)https://www.python.org/          Python Official website ( Link above ), At the same time provide Python2 . x and Python3 . x Download and install version , But in 2020 year ,python2 The series has stopped the iteration of version update .Python3 And 2 The difference is relatively large , It is embodied in grammar And code Compatibility And portability On .

        overall , The higher the version python Its function is also strengthened , Each iteration of the minor version exists as a patch .

        The popular advice on the Internet is : choice Python3

Python Coding standards

        Private opinion , Study python The most important thing is the code specification . It pays great attention to the readability of the code , There are grammatical requirements for code layout and typesetting . Let's briefly introduce some python Public norms :

1、python Strictly use code indentation to reflect logical dependencies . Specific for : In each class 、 function 、 Or use a blank line after a complete function code 、 Add a space on both sides of the operator 、 Add a space after the comma separator . In normal assignment statements ,= Add a space to the left and right of the , But when defining the default value parameters of a function and calling a function with key parameters ,= No space is added on both sides !( Such as  “def num(age=1): ”age after = There are no spaces on either side of the ).

2、 Every import Language only imports one module , according to “ Standard library “、” Expansion Library “、” Custom library “ Import in the order of . And you don't need to import the entire library , Because this will affect the running speed of the program .

3、 A single python If the sentence is too long , Then use () Branch packing , Or make continuation character : The backslash \. .

4、 To make the code readable , Use parentheses appropriately for complex expressions , And add necessary notes . Single line comment :#, Three quotes ‘’‘ For multiline comments .

5、 At running speed ,python Built-in objects > Standard library >c Language expansion Library > Third party Library . therefore , No special needs , Try to call libraries that run faster .

6、 Among all built-in data types , Try to follow : aggregate 、 Dictionaries > Tuples > list 、 The order of precedence of strings .

Python Import and use of standard library and extended library

pip

        In general use pip Tool management expansion Library , Please refer to :
python Of pip command - You know (zhihu.com)https://zhuanlan.zhihu.com/p/68715097

import

import 【 Module name 】as【 Alias 】

After importing in this way , To use related resources, you need to pass the module name . Object :

inport math
math.sin(0.5)
import math as mh
mh.cos(0.5)

from 【 Module name 】 import 【 Object name 】  as【 Alias 】 

In this way, only explicitly specified objects are imported , Reduce the number of queries to the library , Improve access speed , Reduce the size of the packed file , It is recommended to use !

from math import sin
sin(5)
from math import sin as s
s(5)

from 【 Module name 】 import  *

  This is the extreme case of the above usage , All object members in the module can be imported at one time , If not necessary, it is not recommended to use !

from math import *
abs(-3)

The above import methods can import multiple... In turn , It also supports aliasing multiple imported objects .

Python Medium “main” function

        _ _name_ _: Module name , Every python When a script runs, it will have a _ _name_ _ attribute , When the execution file is consistent with the current module ,_ _name_ _ The value is equal to the _ _main_ _, This type is a string type  . If it is imported as a module , Then the value of this attribute is automatically replaced by the module name

  python It is based on script sequence execution , No, main The concept of function , But we can use keywords  _ _name_ _ To simulate the .

   about _ _name_ _ The command-line parameters of are used in the same way as mian More about , Refer to the following article :

if __name__ == '__main__': What the hell is that? ? - cloud + Community - Tencent cloud (tencent.com)https://cloud.tencent.com/developer/article/1538553

Python main function 、 Named row parameters - lwp-boy - Blog Garden (cnblogs.com)https://www.cnblogs.com/lwp-boy/p/13415886.html

copyright notice
author[July is your lie],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/175/202206240345254236.html

Random recommended