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

Python notes (1) getting started with Python

編輯:Python

Catalog

 

Chapter one :Python introduction

1.Python Introduce

1.1 brief introduction

1.2 Python2. Version and Python3.x Differences in versions

1.3 Python Advantages and disadvantages

 1.4 Basic program format

1.5 Python Application scenarios of


Chapter one :Python introduction

1.Python Introduce

1.1 brief introduction

Python It's a kind of interpreted , object-oriented Language . By Guido · Van rosum (Guido van Rossum) On 1989 The invention of ,1991 Officially announced in . Official website :www.python.org

Python The word is “ boa constrictor ” It means . But Uncle tortoise didn't get the name because he liked boa constrictor, but he was chasing a play : British TV comedy 《 Monti · Parson's Flying Circus 》

Programming languages are classified from the following aspects : Compiler and interpretive 、 Static language and dynamic language 、 Strong type definition language and weak type definition language .

What's the difference between compilation and interpretation ?

compiler It is to compile every statement of the source program into machine language 、 And save as binary file , In this way, the runtime computer can directly execute this program in machine language , fast ;

Interpreter When executing a program , One by one, it's interpreted as machine language for the computer to execute , So the running speed is not as fast as the compiled program .

This is because computers can't directly recognize and execute the statements we write , It can only understand machine language ( It's in binary form )

A compiled Explanatory mixed type CJavaScriptJavaC++PythonC#GoRuby SwiftPHP Object-CPerl PascalErlang 

A compiled

advantage : Compilers usually have precompiled procedures to optimize the code .

Explanatory

advantage : Good platform compatibility , Run in any environment , If the interpreter is installed ( virtual machine ), Flexible code modification

Python The inheritance of is multi - inheritance

1.2 Python2. Version and Python3.x Differences in versions

  • print function

Python2 Medium print It's a statement , A statement is an executable code . If the return value is also multiple , Then return a tuple .

Python3 Medium print For a function , It must be enclosed in brackets ; The function receives a return value from a parameter , Multiple parameters can be received .

#Python2
1 print 'Python', python_version()
2 print 'Hello, World!'
3 print('Hello, World!')
4 print "text", ; print 'print more text on the same line'
```
run result:
Python 2.7.6
Hello, World!
Hello, World!
text print more text on the same line
```
#Python3
1 print('Python', python_version())
2 print('Hello, World!')
3 print("some text,", end="")
4 print(' print more text on the same line')
```
run result:
Python 3.4.1
Hello, World!
some text, print more text on the same line
```
  • Divide an integer by

Python 2 in , Integer division defaults to integer .Python 3 in , Integer division defaults to floating point .

Python 2:
>>>print("1/2",1/2)
('1/2',0)    # A tuple is returned
Python 3:
>>>print("1/2",1/2)
1/2 0.5
  • code

Python 2 in , The default encoding is asscii ,asscii Coding is a coding system applied to Latin alphabets , Using Chinese in programs often leads to coding problems . stay Python 3 Have adopted the UTF-8 As default encoding ,UTF-8 Belong to Unicode code , Support for most languages , And variable length , Space saving . stay Python 3 No need to declare when writing code in # conding:utf-8

  • character string

Python 2 The type of Chinese characters :

str : The sequence of bytes that have been encoded

Unicode : Text characters before encoding

Python 3 The type of Chinese characters :

str : Code of Unicode Text character

bytes : Sequence of bytes before encoding

  Binary bytes Unicode character  Python 2 str type Unicode type Python 3bytes type str type

In order to avoid mistakes , stay Python 2 Should precede the text string with u.

  • xrang

Python 2 in range Return a list , xrange Returns an iterator .

Python 3 There is no xrange .range The method is equivalent to Python 2 Medium xrange Method , At the same time like map Functions, etc ,Python 3 Instead of returning a list, the function returns an iterator .

  • adopt input() Parsing user input

Python3 in input What you get is str;Python2 Of input What you get is int type ,Python2 Of raw_input What you get is str type ; Unify :Python3 In Chinese input and Python2 In Chinese row_input, All entered as str.

Fortunately, , stay Python 3 Storage of user input as a str Object problem . In order to avoid Python 2 Dangerous behavior of reading non string type in , We have to apply  raw_input()  Instead of .

  • Module merge

python2 Medium StringIO and cStringIO Merge into python3 Medium io

python2 Medium pickle and cPickle Merge into python3 Medium pickle.

python2 Medium urllib、urllib2 and urlparse Merge into python3 Medium urllib

  • Dictionaries

python2 Medium dict Class keys、values and items All return to list object ,iterkeys、itervalues and iteritems Returns the generator object .

python3 Removed from list、 Return only one generator object , Keep view only ( generator ), But the method name is :keys、values and items.

  • class

python2 The default class in is the legacy class , Need to explicitly inherit new classes (object) To create new classes .

python3 Completely remove legacy classes from , All classes are new , But you can still explicitly inherit object class .

  • Import

python2 Import order of packages in : Standard library — Relative pouring ( The current directory )— Absolutely import (sys.path)

python3 Import order of packages in : Standard library — Absolutely import ( If you want to import relatively , Use from .moudel)

  • Rename module
Python 3Python 2ConfigparserConfigParserfilteritertools.ifilterinputraw_inputmapitertools.imaprangexrangefunctools.reducereducesocketserverSocketServerzipitertools.izip

 

1.3 Python Advantages and disadvantages

Python The design philosophy of “ grace ”、“ clear ”、“ Simple ”.

advantage

  • Simple -----Python It's the language of simplicity . Read a good one Python The program feels like reading English , Although this English is very strict !Python The pseudo code nature of is one of its greatest advantages . It allows you to focus on solving problems rather than understanding the language itself .
  • studies of the Book of Changes -----Python Extremely easy to use .Python Although it's for C Written language , But it abandoned C Very complex pointers in , To simplify the Python The grammar of .
  • free , Open source -----Python yes FLOSS( free / Open source software ) One of . Popular said , Is that you can freely distribute copies of this software 、 Read its source code 、 Make changes to it 、 Use part of it in new free software .
  • High level language ----- When you use Python When programming in language , You don't have to think about the underlying details of how to manage the memory your program uses .
  • Portability ----- Because of its open source nature ,Python Has been ported to many platforms ( Changes have been made to make it work on different platforms ). If you are not careful to avoid using system dependent features , So all your Python The program can run on any of the following platforms without modification . These platforms include Linux、Windows、FreeBSD、Macintosh、Solaris、OS/2、Amiga、AROS、AS/400、BeOS、OS/390、z/OS、Palm OS、QNX、VMS、Psion、Acom RISC OS、VxWorks、PlayStation、Sharp Zaurus、Windows CE、PocketPC、Symbian as well as Google be based on linux Developed android platform .
  • interpreted ----- This needs some explanation . and Python A program written in a language does not need to be compiled into binary code . You can run programs directly from source code . Inside the computer ,Python The interpreter converts the source code into an intermediate form called bytecode , Then translate it into the machine language used by the computer and run it . in fact , Because you no longer need to worry about how to compile programs , How to ensure the correct reprint . You just need to put your own Python Copy the program to another computer , It's ready to work , It also makes Python Programs are easier to port .
  • object-oriented -----Python Supports both procedural and object-oriented programming . stay “ Process oriented ” The language of , Programs are built from procedures or functions that are simply reusable code . stay “ object-oriented ” The language of , Programs are built from objects that combine data and functionality . With other major languages such as C++ What Java comparison ,Python A very powerful and simple way to implement object-oriented programming
  • Extensibility ------
  • Rich library ------Python Our standard library is huge . It can help you with all kinds of work , Including regular expressions 、
  • Standard code ------Python Code that uses forced indentation is extremely readable .( This is also one of the eight original sins that a recent Daniel roast )

shortcoming :

  • Slow running speed , If there is a speed requirement , use C++ Rewrite the key parts . The domestic market is smaller ( At home with python To do the main development of , There are only a few web2.0 company ). But over time , At present, many domestic software companies , Especially game companies , He also began to use it on a large scale .【 douban You know 】
  • Lack of Chinese materials ( well python There are only a few Chinese materials ). Thanks to the community , Several excellent textbooks have been translated , But there are many entry-level textbooks , Advanced content or only English version .
  • There are too many framework principles ( Didn't like C# Such an official .net frame ,)

 1.4 Basic program format

1 . Appropriate spaces , Indentation problem

(1) The first line of logic is blank ( Spaces and tabs ) Used to determine the indentation level of logical lines , This is used to determine the grouping of statements

(2) The statement starts with the first column of the new row .

(3) The indentation style is uniform :

① Each indent level uses a single tab or four spaces (IDE The tab is automatically set to 4 A space );

②Python Indent instead of {} Represents a program block .

2 . Python Case sensitive

3 . notes

(1) Line notes

Add... Before each line of comment “#” Number . When the interpreter sees “#” , Ignore this line “#” Later

(2) Paragraph notes

Use three consecutive single quotes (```). When the interpreter sees (```), Will scan to the next (```), Then ignore the content between them .

1.5 Python Application scenarios of

  • web application development ( Now? go The development efficiency of the language is more ok ,javaee Development is still the mainstream ),Python Mobilization is used for web Development
  • Operating system management , Automatic script of server operation and maintenance ( Automatic operation and maintenance )
  • Scientific Computing , Data analysis
  • Desktop software (GUI)
  • Server software
  • game ------ Under normal circumstances, it cannot be done
  • Reptiles
  • Artificial intelligence 【 Machine learning and deep learning 】 machine learning : Algorithm ; Deep learning : Machine vision 、 Autopilot

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