[Dark Horse] net course notes first Python program

First Python Program

aims

  • The first HelloPythonprogram
  • Python 2.xAnd 3​​.xversion Introduction
  • The implementation of Pythonthree ways to program
    • Interpreter - python/python3
    • Interactive - ipython
    • Integrated Development Environment-- PyCharm

01. The first HelloPythonprogram

Basic Concepts 1.1 Python source code

  1. Python source code is a text file of a special format , you can use any text editor to do Pythondevelopment
  2. Python program file extensions are usually.py

Step drills 1.2

  • In the desktop, create a new 认识Pythondirectory
  • In 认识Pythonthe new directory 01-HelloPython.pyfile
  • Use gedit editor 01-HelloPython.pyand enter the following:
print("hello python")
print("hello world")
  • Enter the following command in the terminal execution 01-HelloPython.py
$ python 01-HelloPython.py

printIs the pythonfirst we learned of the function

printAction function, you can put "" the contents inside, and output to the screen

1.3 Extended Walkthrough - Mistake (BUG)

About error

  • Programs written can not be executed properly , or execution result is not what we expected
  • Commonly known BUG, is a programmer in the development of very common, common mistakes beginners reasons include:
    1. mistake
    2. Already learned knowledge to understand there are still insufficient
    3. There is need for content and promotion of language learning
  • When learning a language, not only to learn the syntax of the language , but also learn how to recognize errors and methods to resolve errors

Every programmer is growing constantly correct errors in

Common Errors first walkthrough

  • 1> wrong hands , for example,pirnt("Hello world")
NameError: name 'pirnt' is not defined

名称错误:'pirnt' 名字没有定义
  • 2> The number of printwrite in a row
SyntaxError: invalid syntax

语法错误:语法无效

Every line of code responsible for completing an action

  • 3> indentation errors
IndentationError: unexpected indent

缩进错误:不期望出现的缩进
  • Python is a very strict format programming language
  • For now, we remember the front of each line of code do not add a space
  • 4> Python 2.x default does not support Chinese

There are two versions of Python coexist on the market, respectively, Python 2.xandPython 3.x

  • Python 2.x default does not support Chinese , the specific reasons, until the introduction character encoding when to explain to
  • Python 2.x interpreter name is python
  • Python 3.x interpreter name is python3
SyntaxError: Non-ASCII character '\xe4' in file 01-HelloPython.py on line 3, 
but no encoding declared; 
see http://python.org/dev/peps/pep-0263/ for details

语法错误: 在 01-HelloPython.py 中第 3 行出现了非 ASCII 字符 '\xe4',但是没有声明文件编码
请访问 http://python.org/dev/peps/pep-0263/ 了解详细信息
  • ASCIICharacter contains only 256characters, does not support Chinese
  • Issues related to character encoding, follow-up will speak

Word list

* error 错误
* name 名字
* defined 已经定义
* syntax 语法
* invalid 无效
* Indentation 索引
* unexpected 意外的,不期望的
* character 字符
* line 行
* encoding 编码
* declared 声明
* details 细节,详细信息
* ASCII 一种字符编码

02. Python 2.xand 3​​.xversion Introduction

There are two versions of Python coexist on the market, respectively, Python 2.xandPython 3.x

The new program is recommended to use Python Python 3.0version syntax

  • Python 2.x is the last version
    • Interpreter name is python
  • Python 3.x is now mainstream and future versions
    • Interpreter name is python3
    • With respect to the Pythonearlier version, which is a major upgrade
    • In order not to bring too much burden, Python 3.0when the design does not consider backward compatible
      • Many of the early Pythonversions of the design of the program can not be in Python 3.0the normal execution on
    • Python 3.0 released in 2008
    • So far, Python version 3.0 has been stable for many years
      • Python 3.3 released in 2012
      • Python 3.4 released in 2014
      • Python 3.5 released in 2015
      • Python 3.6 released in 2016
  • In order to take care of existing programs, it provides a transition official version - Python 2.6
    • Use the basic Python 2.xsyntax and libraries
    • Taking into account the Python 3.0migration, allowing the use of part of the Python 3.0 syntax and function
    • Launched in 2010, Python 2.7it was identified as the last Python 2.x version

Tip: If the developer can not be used immediately Python 3.0 (there are very few third-party libraries do not support syntax 3.0), it is recommended

  • First use Python 3.0version developed
  • Then Python 2.6, Python 2.7to perform, and to make the processing of some compatibility

03. Python implementation of the program in three ways

3.1. Interpreter python/python3

Python interpreter

# 使用 python 2.x 解释器
$ python xxx.py

# 使用 python 3.x 解释器
$ python3 xxx.py
Other interpreters (to know)

Python interpreter now have to implement multiple languages, including:

  • CPython - the official version of the C language
  • Jython - you can run on the Java platform
  • IronPython - it can run on .NET and Mono platforms
  • PyPy - Python, support for real-time JIT compiler

3.2. The interactive program run Python

  • Run the interpreter in the terminal directly, without entering the file name to be executed
  • In Python's Shelldirect input in Python code , the program will immediately see the results

1) Interactive run Python's strengths and weaknesses

advantage
  • Suitable for learning / verification codes or locally Python syntax
Shortcoming
  • Code can not be saved
  • Not suitable for running much of the program

2) Quit official interpreter

1> Direct Input exit()
>>> exit()
2> using a hotkey to exit

In the python interpreter, press the hotkey ctrl + dto exit the interpreter

001_ short step a thousand miles

3) IPython

  • IPython in the "I" represents the interaction interactive
Feature
  • IPython is a python's interactive shell , than the default python shellmuch more useful
    • It supports auto-completion
    • Auto-indenting
    • Support bash shellCommand
    • It built a lot of features and useful functions
  • IPython is based on the BSD open source
version
  • Python 2.x interpreter is used ipython

  • Python 3.x interpreter is used ipython3

  • To exit the interpreter can have the following two ways:

1> Direct Input exit
In [1]: exit
2> using a hotkey to exit

In IPython interpreter, press the hot key ctrl + d, IPythonit will be asked whether to exit the interpreter

IPython installed

$ sudo apt install ipython

. 3.3 Python of IDE - PyCharm

1) Integrated Development Environment (IDE)

Integrated Development Environment ( IDE, Integrated Development Environment) - integrates all the tools needed to develop software generally includes the following tools:

  • GUI
  • Code Editor (supports code completion / auto-indent )
  • Compiler / interpreter
  • Debugger ( breakpoints / stepping )
  • ……

2) PyCharm Introduction

  • PyCharm The Python is a very good integrated development environment
  • PyCharmIDE necessary for addition to the general functions, but also in Windows, Linux, macOSthe use of
  • PyCharm Suitable for large-scale development projects
    • A project usually contains several files
    • Each source code line number is limited, usually within a few hundred lines of
    • Each source file their duties together to accomplish complex business functions

3) PyCharm quickly experience

  • File navigation area can browse / locate / open the project file

  • Document editing area can edit the currently open file

  • Console area to:

    • Output program execution content
    • Execution trace debugging code
  • The upper right corner of the toolbar can execute (the SHIFT + F10) / Debug (SHIFT + F9) Code

  • The console above the single-step execution button (the F8) , can step through the code

Guess you like

Origin blog.csdn.net/qq_23090489/article/details/92984845