First Python Program
aims
- The first
HelloPython
program Python 2.x
And3.x
version Introduction- The implementation of
Python
three ways to program- Interpreter -
python
/python3
- Interactive -
ipython
- Integrated Development Environment--
PyCharm
- Interpreter -
01. The first HelloPython
program
Basic Concepts 1.1 Python source code
- Python source code is a text file of a special format , you can use any text editor to do
Python
development - Python program file extensions are usually
.py
Step drills 1.2
- In the desktop, create a new
认识Python
directory - In
认识Python
the new directory01-HelloPython.py
file - Use gedit editor
01-HelloPython.py
and enter the following:
print("hello python")
print("hello world")
- Enter the following command in the terminal execution
01-HelloPython.py
$ python 01-HelloPython.py
python
first we learned of the function
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:- mistake
- Already learned knowledge to understand there are still insufficient
- 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
print
write 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.x
andPython 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/ 了解详细信息
ASCII
Character contains only256
characters, 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.x
and 3.x
version Introduction
There are two versions of Python coexist on the market, respectively, Python 2.x
andPython 3.x
The new program is recommended to use Python
Python 3.0
version 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
Python
earlier version, which is a major upgrade - In order not to bring too much burden,
Python 3.0
when the design does not consider backward compatible- Many of the early
Python
versions of the design of the program can not be inPython 3.0
the normal execution on
- Many of the early
- 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.x
syntax and libraries - Taking into account the
Python 3.0
migration, allowing the use of part of thePython 3.0
syntax and function - Launched in 2010,
Python 2.7
it was identified as the last Python 2.x version
- Use the basic
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.0
version developed- Then
Python 2.6
,Python 2.7
to 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 languageJython
- you can run on the Java platformIronPython
- it can run on .NET and Mono platformsPyPy
- 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
Shell
direct 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 + d
to exit the interpreter
3) IPython
- IPython in the "I" represents the interaction interactive
Feature
- IPython is a python's interactive shell , than the default
python shell
much more useful- It supports auto-completion
- Auto-indenting
- Support
bash shell
Command - 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
, IPython
it 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 environmentPyCharm
IDE necessary for addition to the general functions, but also inWindows
,Linux
,macOS
the use ofPyCharm
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