程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Fundamentals of Python Programming

編輯:Python

Python Programming based

  • One 、 brief introduction
    • 1.Python Interpreter
    • 2. characteristic
      • (1) Grammatical simplicity
      • (2) It's not about the platform
      • (3) Viscous expansion
      • (4) Open source ideas
      • (5) Versatile and flexible
      • (6) Force readability
      • (7) Support Chinese
      • (8) Diverse models
      • (9) Rich third-party library resources
  • Two 、 grammar
    • 1. notes
    • 2. End of sentence
    • 3. identifier : Variable 、 function 、 Object etc. .
  • 3、 ... and 、 data type
    • 1. Digital
    • 2. character string (str)
    • 3. Constant
    • 4. Variable
  • Four 、 Data type conversion function
  • 5、 ... and 、 Operator
    • 1. Arithmetic operator
    • 2. Assignment operator
      • (1) Continuous assignment
      • (2) Multiple assignments
    • 3. Comparison operator
    • 4. Logical operators
    • 5. String operators
    • 6. An operator
    • 7. member operator
  • 6、 ... and 、 Combined data types
    • 1.Python Combined data types in language : Tuples 、 list 、 Dictionaries 、 aggregate
      • (1) Tuples
      • (2) list
      • (3) Dictionaries
  • 7、 ... and 、 Branch
    • 1. Conditional branch
      • (1) One branch and two branches
      • (2) Multiple branches
  • 8、 ... and 、 loop
    • 1. Traversal cycle
    • 2. condition loop
  • Nine 、 function
    • 1. Define and call
      • (1) Definition
      • (2) call
  • Ten 、 class
  • 11、 ... and 、 Summary

One 、 brief introduction

Python The language is spoken by the Dutch Guido Van Rossum On 1989 The invention of ,1991 The first public release was released in ;Python It is the mainstream language in the field of machine learning ;Python2.x It is already a legacy ,Python3.x Is the present and future of this language . Is a high-level general-purpose scripting language Compiler language , Such as C、C++、C# etc. , The source code must be converted by the compiler into the machine object code , Run the executable directly , No more source code and compilers . Explanatory language , Such as Python、MATLAB、Java Script etc. , The interpreter interprets and executes the source program line by line , That is, the runtime needs source code and interpreter .

1.Python Interpreter

Python The interpreter executes for the first time py Source program error , Will convert the source program into bytecode , The execution speed of bytecode is much faster than that of source code , And it's not about the platform . When the source program is executed again , The interpreter will automatically load the bytecode and interpret the bytecode . The interpreter will automatically check the timestamp of the source program and bytecode , If you find inconsistencies , Will convert the source program to bytecode again .
Python There is no independent compilation system , Just with some compilation features , The bytecode is still interpreted and executed item by item .

2. characteristic

(1) Grammatical simplicity

Implement the same function ,Python The number of lines of code in this language is only equivalent to that in other languages 1/10~1/5.

(2) It's not about the platform

It can run without modification in any computer environment where the interpreter is installed

(3) Viscous expansion

Is often referred to as “ Glue language ”
Excellent scalability , Can be integrated C、C++、Java And other languages , Through interfaces and function libraries, etc “ Stick it together ”. One common application is , First use Python Quickly prototype the program ( Even the final interface of the program ), Then rewrite the parts with special requirements in more appropriate languages . For example, the parts requiring high operating speed , available c/c++ Rewrite and encapsulate into Python Extension class libraries that can be invoked .

(4) Open source ideas

For advanced programmers ,Python The open source interpreter and function library of the language have a strong attraction , More importantly ,Python The open source software concept advocated by the language has laid a solid mass foundation for the development of the language .

(5) Versatile and flexible

It can be used to write applications in various fields .

(6) Force readability

Python Language by forced indentation ( Like the first line of a paragraph in an article ) To reflect the logical relationship between sentences , The readability and maintainability of the program are significantly improved

(7) Support Chinese

(8) Diverse models

Even though Python3.x The interpreter is implemented in an object-oriented manner , but Python The syntax level supports both process oriented and object-oriented programming .

(9) Rich third-party library resources

Has formed a thriving Python Machine learning ecosystem .

Two 、 grammar

1. notes

Single-line comments :#
Multiline comment : Triple quotes ( Single or double quotation marks )

2. End of sentence

Tends to identify the end of a statement by wrapping
Semicolons are also supported as statement end identifiers . If you write multiple statements on one line , You need to add a semicolon to distinguish these statements .

3. identifier : Variable 、 function 、 Object etc. .

1. The... Of the identifier 1 Characters must be letters or underscores
2. Other characters can be composed of letters 、 Underline 、 Or numbers make up , Chinese characters are not recommended
3. The identifier length is arbitrary test_data,test_data_1,y,, correct ; 1test,1_test error
4. Identifiers are case sensitive
5. Identifier cannot be associated with Python Duplicate key name .(help(“keywords”))
6. Generally, you don't start with an underscore , Because the system stipulates ,_xxx Represents the protected variable name in the class ,__xxx Represents the private variable name in the class ,__xxx__ System defined identifier .

3、 ... and 、 data type

1. Digital

(1) Integers (int): Don't worry about overflowing due to insufficient digits
(2) Floating point numbers (float):3.14,-0.28,1.392E9, 1.392e9
(3) Boolean value (bool):True,False
(4) The plural (complex):1+2i,1+2j

2. character string (str)

 Use pairs of single quotation marks 、 Double quotation marks form a single line string ,
Use pairs of triple quotes ( Single or double quotation marks Number ) Cover up , Form a multiline string .

3. Constant

 Numbers 、 character string 、 Boolean value 、 Null value, etc , for example 2,-10086,3.5, “Python” ,True、False,None
Python There are no named constants in , A constant cannot be given a name

4. Variable

 Python You don't need to declare , The data type is determined by the assigned value
Different types of numerical data operations , It will automatically perform type conversion :
bool<int<float<complex
Automatic type conversion , Only exists between numeric data

Four 、 Data type conversion function


for example :

int、float、complex Is the class name. ,a2 It is actually an instantiated object of an integer class ,
d It's actually accessing complex objects c Of imag Attribute or data member

5、 ... and 、 Operator

1. Arithmetic operator

2. Assignment operator

(1) Continuous assignment

Continuous assignment :a=b=c=1

(2) Multiple assignments

Multiple assignments :(x,y,z)=(1,2,“Python”),x,y,z=1,2,“Python”

3. Comparison operator

4. Logical operators

5. String operators

6. An operator

An operator : Operate on the binary bits of an integer

7. member operator

6、 ... and 、 Combined data types

1.Python Combined data types in language : Tuples 、 list 、 Dictionaries 、 aggregate

(1) Tuples 、 list 、 The string is a sequence type .
The elements have order , repeatable , Through square brackets and subscripts ( Indexes ) Access elements . The element index of the sequence type adopts the forward increasing or reverse decreasing sequence number . Common operators and functions are 12 individual , Commonly used :①sn or ns, The sequence of s Copy n Time ;②s[i], A single index returns sequence number i Elements ;③s[i:j], section , Back to page i To j-1 Elements ;④s[i:j:k], Step slicing , Back to page i To j-1 The first element is k For stepping ;⑤len(s)、min(s)、max(s)
(2) The dictionary is of mapping type , Each element is a key value pair , Elements in no order , Can't repeat , The corresponding values are accessed through square brackets and keys . key key Numbers or strings are often used , value value It can be any type .

(1) Tuples

Tuples (tuple [ˈtʌpl] ) Is a read-only sequence type , Element types can be different , Tuple elements cannot be reassigned after initialization . Use commas and parentheses ( Parentheses can be omitted without confusing semantics ) Express . In expressing fixed data items 、 Function returns multiple values 、 Multivariable assignment 、 Loop traversal is very useful .

(2) list

list (list) Is the sequence type , Element types can be different , Variable length and content .

(3) Dictionaries

7、 ... and 、 Branch

1. Conditional branch

keyword if、else、elif; Each condition determines ( Brackets can be omitted ) Ends with a colon ; Indent spaces to distinguish code blocks .

(1) One branch and two branches

(2) Multiple branches

8、 ... and 、 loop

1. Traversal cycle

Extract the elements from the iteratable object one by one, put them in the loop variable, and execute the loop body
for < Loop variable > in < Iteratable object >:
< The loop body >

range Function to generate an integer range :range(3) return 02;range(1,4) return 13;range(0,6,2) return 0,2,4;range(6,0,-2) return 6,4,2.
range Objects are lazy objects , Can't show directly . Built in functions zip Expandable inert objects , You can also pair the elements of multiple list objects into meta groups .

2. condition loop

while < Conditions >:
< The loop body >


The loop structure has two reserved words break and continue,break Used to jump out of its place for or while loop ,continue Used to end the current cycle .

Nine 、 function

1. Define and call

(1) Definition

The parameter can be zero 、1 One or more , Keep parentheses when there are no parameters , Multiple parameters are separated by commas ; The order of formal parameters is : General parameters ( Only formal parameter names )、 Optional parameters ( The name of the parameter = The default value )、 Variable parameters (* The name of the parameter ).
The return value can be zero 、1 One or more , Multiple return values are separated by commas and returned as tuples , When there is no return value, there can be no return sentence .
When a function is called , General parameter must be given an actual parameter value ; By default, arguments are passed in the order of formal parameters , And variable parameters can only be passed by position ; To enhance readability , You can also pass arguments by parameter name , The order of parameters can be arbitrary . If mixed , Then the person passing by name needs to pass by location

(2) call

1. Method 1

2. Method 2

Ten 、 class

1. Customize Python The parent class of a class is usually object, Multiple inheritance is possible ; Class members include function members ( Or method ) And data members ( Or attribute ).
2. When defining member functions , The first parameter must be self, Prefix can be used self. Call properties or other member functions ;
3. When instantiating a class to generate an object , The constructor will be called automatically for initialization , The name of the constructor is __init__,init There are two underscores before and after , no return value ; Omission
4. When clearing objects , The system calls the destructor automatically , Reclaim and release the resources occupied by the object. Its name is __del__. It can be omitted .
5. All member functions and properties are public by default ( That is, you can call... Through an object ), If you want to make it private , Add two underscores before the name . Use del Object name , You can delete objects .

example :

11、 ... and 、 Summary

All things can be python Later I'm going to share 8.10 piece python Topic sharing , Will expand some knowledge of machine learning direction , You are welcome to support me .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved