Introduction to the first knowledge of PYthon1.1 python
The founder of Python is Guido van Rossum (Guido van Rossum). During the Christmas of 1989, Guido van Rossum to spend time in Amsterdam, determined to develop a new script interpreter, as an inheritance of the ABC language.
The latest Tiobe leaderboard, Python overtook Php occupy five, Python advocating beautiful, clear, simple, is a good and widely used language.
1.2Python Application Areas
- Cloud computing : The hottest language in cloud computing, typically using OpenStack
- Web Development : A number of excellent web frameworks, many large sites are Python development, Youtube, Dropbox, watercress ... , a typical web framework has Django
- Scientific Computing, artificial intelligence : Typical library numpy, SciPy, matplotlib, Enthought Librarys,pandas
- system Operation and maintenance: the necessary language for operation and maintenance personnel
- Finance : Quantitative trading, financial analysis, in the field of financial engineering, Python is not only used, but also used the most, and the importance of the year. Reason: As a dynamic language Python, the language structure is clear and simple, the library is rich, mature and stable, scientific calculation and statistical analysis are very good, production efficiency is much higher than c,c++,java, especially good at strategy backtesting
- graphical GUI: PyQT, Wxpython,tkinter
Advantages and disadvantages of 1.3Python 1.3.1Python
- Python's positioning is "elegant", "clear", "simple", so the Python program looks always easy to understand, beginners learn python, not only easy to get started, but also in the future, you can write those very very complex programs.
- Development efficiency is very high, Python has a very powerful third-party library, basically you want to achieve any function through the computer, the Python official library has the corresponding modules to support, directly download the call, on the basis of the base library to develop, greatly reduce the development cycle, to avoid repeating the wheel.
- High-level language ———— when you write programs in the Python language, you don't have to consider the underlying details such as how to manage the memory used by your program
- Portability ———— because of its open source nature, Python has been ported on many platforms (modified to make it work on different platforms). If you are careful to avoid using system-dependent features, all your Python programs can run on almost any system platform on the market without modification
- Scalability ———— If you need a piece of your critical code to run faster or you want some algorithms to be private, you can write some of your programs in C or C + + and then use them in your Python program.
- Embeddable ———— You can embed python into your C + + program to provide scripting functionality to your program users.
Disadvantages of 1.3.2Python
- Slow, Python runs faster than the C language, and slower than Java, so this is the main reason why many so-called Daniel disdain to use Python, but in fact, this refers to the speed of slow in most cases the user is not directly aware of, Must rely on the use of testing tools to reflect, such as you use C a program to spend 0.1s, with Python is 0.01s, so c directly than Python faster 10s, is very exaggerated, but you can not directly perceive through the naked eye, Because a normal person can perceive the smallest unit of time is 0.15-0.4s around, haha. In fact, in most cases python has been fully able to meet your requirements for the speed of the program, unless you want to write to the speed of the most demanding search engine, in this case, of course, it is recommended that you use C to achieve.
- Code can not be encrypted, because Python is an explanatory language, its source code is stored in the form of a name, but I do not think this is a disadvantage, if your project requires that the source codes must be encrypted, then you should not use Python in the beginning to implement.
- Threads do not take advantage of multi-CPU problems, which is one of the most common drawbacks of Python, the Gil, the Global Interpreter lock (interpreter lock), is a tool that the computer programming language interpreter uses to synchronize threads so that only one thread executes at any moment, The python thread is the native thread of the operating system. On Linux for Pthread, on Windows for win thread, the execution of threads is fully dispatched by the operating system. A Python interpreter process has a main thread and the execution thread for multiple user programs. Multi-threaded parallel execution is prohibited even on multicore CPU platforms due to the existence of the Gil.
1.4Python installation deployment on 1.4.1Windows side
- Download the installation package https://www.python.org/downloads/
- Install default installation path: C:\python3.5.2
- Configure the environment variable (original value; C:\python27, remember that there is a semicolon in front) right-click Computer –> Property –> Advanced system settings –> environment variable –> find a row with the variable named path double-clicking the –>python installation directory appends to the variable value. To use a semicolon; split.
Deployment at the 1.4.2Linux end
The Linux side will come with a Python client by default, with the centos6.x python2.6,ubuntu16.04 comes with Python2.7 and 3.5 two versions.
1.5 First Python program
- #!/usr/bin/env python
- Print ("hello,world!")
Execute this Python program
- [email protected] python]# python hello.py
- hello,world!
1.6Python Comment 1.6.1 Single-line comment: Same as the shell annotation method
- #print ("hello,world!")
1.6. More than 2 lines comments: Bulk annotations using three quotation marks
- #每行最多不能超过80个字符
- ”’
- This is a note!
- This is a note!
- This is a note!
- ”’
1.6. Another use of 33 quotation marks: formatted output
- #!/usr/bin/env python
- A= "' Hello,everybody,i am lizhong\n
- The second line
- haha
- 4
- 5
- Ok
- ;;;
- ”’
- Print a
Execute the above Python script and get the following results:
- Hello,everybody,i am Lizhong
- The second line
- haha
- 4
- 5
- Ok
- ;;;
1.7Python variable 1.7.1 declaration variable
- #_ *_coding:utf-8_*_
- Name = "Lizhong"
1.7.2 variables define the rules:
- Variable names can only be any combination of letters, numbers, or underscores
- The first character of a variable name cannot be a number
- The following keywords cannot be declared as variable names
[' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' exec ', ' finally ', ' for ', ' F ' Rom ', ' Global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' not ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' try ', ' while ', ' WI Th ', ' yield ']
Assignment of the 1.7.3 variable
- #!/usr/bin/env python
- #-*-Coding:utf-8-*-
- name1 = "Lizhong"
- name2 = "Alex"
- Name1 = "Me"
- Print name1
At this point name1 is me, not lizhong,name2 for Alex.
Operation of 1.7.4 variables
Variables can be directly calculated
- #!/usr/bin/env python
- User_name= ' Alex '
- Age=27
- Next_year_age=age+1
- Print Next_year_age
Then the value of the output should be 28, and the variable can be evaluated directly.
1.8Python character encoding
When the Python interpreter loads the code in the. py file, the content is encoded (the default ASCILLASCII (American standard code for Informationinterchange, The American Standard Information Interchange Code) is a set of computer coding systems based on the Latin alphabet, mainly used to display modern English and other Western European languages, which can be represented at most 8 bits (one byte), i.e. 2**8 = 256-1, so that ASCII codes can represent up to 255 symbols.
It is obvious that the ASCII code cannot represent all the words and symbols in the world, so a new encoding that can represent all the characters and symbols is needed, i.e. Unicode Unicode (universal code) is a character encoding used on the computer. Unicode is created to address the limitations of the traditional character encoding scheme, which sets a uniform and unique binary encoding for each character in each language, which specifies that characters and symbols are represented by at least 16 bits (2 bytes), i.e. 2 **16 = 65536.
UTF-8, which is compression and optimization of Unicode encoding, does not use a minimum of 2 bytes, but instead classifies all characters and symbols: the contents of the ASCII code are saved with 1 bytes, the characters in Europe are saved in 2 bytes, and the characters in East Asia are saved in 3 bytes ... Therefore, when the Python interpreter loads the code in the. py file, it encodes the content (the default ascill), if it is the following code:
Error: ASCII code cannot be expressed in Chinese
- #!/usr/bin/env python
- # name = "Hello, world"
- # print (name)
Correct: You should tell the Python interpreter what code to use to execute the source code (Chinese is supported by default in Python3)
- #!/usr/bin/env python
- #-*-Coding:utf-8-*-
- # name = "Hello, world"
- # print (name)
1.9Python user Input
- #!/usr/bin/env python
- #将用户输入的内容赋值给name变量
- Name = input ("Please enter user name:")
- #打印输入的内容
- Print (name)
When entering a password, you can use the Getpass method in the Getpass module if you want to be invisible.
- #!/usr/bin/env python
- #导入getpass库
- Import Getpass
- #将用户输入的内容赋值给name变量
- Name = input ("Please enter user name:")
- PWD = Getpass.getpass ("Please enter password:") #在Pycharm下不支持此模块
- #打印输入的内容
- Print ("Username:", name, "Password:", pwd)
1.10Python formatted output
Python formatted input is divided into the following three ways. A third approach is recommended, and some cases must be used in a third way
1.10.1. Method One: the string is%s; integer%d; floating-point number%f
- #!/usr/bin/env python
- Name = input ("Name:")
- age = Int (input ("Age:"))
- Job = input ("Job:")
- Salary = input ("Salary:")
- info = "'
- ——-Info of%s--
- name:%s
- age:%d
- job:%s
- salary:%s
- "'% (name,name,age,job,salary)
- Print (info)
1.10.2. Method Two:
- #!/usr/bin/env python
- Name = input ("Name:")
- age = Int (input ("Age:"))
- Job = input ("Job:")
- Salary = input ("Salary:")
- Info2 = "'
- ——-info of {0}--
- NAME:{0}
- Age:{1}
- JOB:{2}
- SALARY:{3}
- "'. Format (name,age,job,salary)
- Print (Info2)
1.10.3. Method Three:
- #!/usr/bin/env python
- # print (name)
- Name = input ("Name:")
- age = Int (input ("Age:"))
- Job = input ("Job:")
- Salary = input ("Salary:")
- Info3 = "'
- ——-info of {_name_}--
- Name:{_name_}
- Age:{_age_}
- Job:{_job_}
- Salary:{_salary_}
- "'. Format (_name_=name,
- _age_=age,
- _job_=job,
- _salary_=salary)
- Print (INFO3)
The results of the implementation are as follows:
- Name:lizhong
- Age:24
- Job:engineer
- salary:20000
- ——-Info of lizhong--
- Name:lizhong
- Age:24
- Job:engineer
- salary:20000
1.11 Process Control 1.11.1 If....else
- #!/usr/bin/env python
- # Prompt for user name and password
- # Verify user name and password
- # If an error occurs, the output user name or password is incorrect
- # If successful, the output is welcome, xxx!
- Import Getpass
- user = "Lizhong"
- passwd = "123"
- Username = input ("Please enter User:")
- #pwd = input ("Please enter password:") #明文
- PWD = Getpass.getpass ("Please enter password:") #密文
- If user = = Username and pwd = = passwd:
- Print ("Welcome, lizhong!")
- Else
- Print ("User name and password error")
1.11.2 If...elif...else
- #!/usr/bin/env python
- Name = input (' Name: ')
- Age =int (Input (' Age: '))
- Job = input (' job: ')
- If Age >40:
- msg = ' You are too old! '
- Elif Age >30:
- msg = ' You had a few years to having fun '
- Else
- msg = ' You're still quite young. '
- Info= "' \tname:%s
- \tage:%d
- \tjob:%s
- ——————————————–
- %s
- "'% (name,age,job,msg)
- Print (info)
The path of Python (i)