The path of Python (i)

Source: Internet
Author: User

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
    1. 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.
    2. 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.
    3. 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
    4. 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
    5. 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.
    6. Embeddable ———— You can embed python into your C + + program to provide scripting functionality to your program users.
Disadvantages of 1.3.2Python
    1. 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.
    2. 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.
    3. 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
    1. Download the installation package https://www.python.org/downloads/
    2. Install default installation path: C:\python3.5.2
    3. 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
    1. #!/usr/bin/env python
    2. Print ("hello,world!")

Execute this Python program

    1. [email protected] python]# python hello.py
    2. hello,world!
1.6Python Comment 1.6.1 Single-line comment: Same as the shell annotation method
    1. #print ("hello,world!")
1.6. More than 2 lines comments: Bulk annotations using three quotation marks
    1. #每行最多不能超过80个字符
    2. ”’
    3. This is a note!
    4. This is a note!
    5. This is a note!
    6. ”’
1.6. Another use of 33 quotation marks: formatted output
    1. #!/usr/bin/env python
    2. A= "' Hello,everybody,i am lizhong\n
    3. The second line
    4. haha
    5. 4
    6. 5
    7. Ok
    8. ;;;
    9. ”’
    10. Print a

Execute the above Python script and get the following results:

    1. Hello,everybody,i am Lizhong
    2. The second line
    3. haha
    4. 4
    5. 5
    6. Ok
    7. ;;;
1.7Python variable 1.7.1 declaration variable
    1. #_ *_coding:utf-8_*_
    2. 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
    1. #!/usr/bin/env python
    2. #-*-Coding:utf-8-*-
    3. name1 = "Lizhong"
    4. name2 = "Alex"
    5. Name1 = "Me"
    6. Print name1

At this point name1 is me, not lizhong,name2 for Alex.

Operation of 1.7.4 variables

Variables can be directly calculated

    1. #!/usr/bin/env python
    2. User_name= ' Alex '
    3. Age=27
    4. Next_year_age=age+1
    5. 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

    1. #!/usr/bin/env python
    2. # name = "Hello, world"
    3. # 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)

    1. #!/usr/bin/env python
    2. #-*-Coding:utf-8-*-
    3. # name = "Hello, world"
    4. # print (name)
1.9Python user Input
    1. #!/usr/bin/env python
    2. #将用户输入的内容赋值给name变量
    3. Name = input ("Please enter user name:")
    4. #打印输入的内容
    5. Print (name)

When entering a password, you can use the Getpass method in the Getpass module if you want to be invisible.

    1. #!/usr/bin/env python
    2. #导入getpass库
    3. Import Getpass
    4. #将用户输入的内容赋值给name变量
    5. Name = input ("Please enter user name:")
    6. PWD = Getpass.getpass ("Please enter password:") #在Pycharm下不支持此模块
    7. #打印输入的内容
    8. 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
    1. #!/usr/bin/env python
    2. Name = input ("Name:")
    3. age = Int (input ("Age:"))
    4. Job = input ("Job:")
    5. Salary = input ("Salary:")
    6. info = "'
    7. ——-Info of%s--
    8. name:%s
    9. age:%d
    10. job:%s
    11. salary:%s
    12. "'% (name,name,age,job,salary)
    13. Print (info)
1.10.2. Method Two:
    1. #!/usr/bin/env python
    2. Name = input ("Name:")
    3. age = Int (input ("Age:"))
    4. Job = input ("Job:")
    5. Salary = input ("Salary:")
    6. Info2 = "'
    7. ——-info of {0}--
    8. NAME:{0}
    9. Age:{1}
    10. JOB:{2}
    11. SALARY:{3}
    12. "'. Format (name,age,job,salary)
    13. Print (Info2)
1.10.3. Method Three:
    1. #!/usr/bin/env python
    2. # print (name)
    3. Name = input ("Name:")
    4. age = Int (input ("Age:"))
    5. Job = input ("Job:")
    6. Salary = input ("Salary:")
    7. Info3 = "'
    8. ——-info of {_name_}--
    9. Name:{_name_}
    10. Age:{_age_}
    11. Job:{_job_}
    12. Salary:{_salary_}
    13. "'. Format (_name_=name,
    14. _age_=age,
    15. _job_=job,
    16. _salary_=salary)
    17. Print (INFO3)
The results of the implementation are as follows:
    1. Name:lizhong
    2. Age:24
    3. Job:engineer
    4. salary:20000
    5. ——-Info of lizhong--
    6. Name:lizhong
    7. Age:24
    8. Job:engineer
    9. salary:20000
1.11 Process Control 1.11.1 If....else
    1. #!/usr/bin/env python
    2. # Prompt for user name and password
    3. # Verify user name and password
    4. # If an error occurs, the output user name or password is incorrect
    5. # If successful, the output is welcome, xxx!
    6. Import Getpass
    7. user = "Lizhong"
    8. passwd = "123"
    9. Username = input ("Please enter User:")
    10. #pwd = input ("Please enter password:") #明文
    11. PWD = Getpass.getpass ("Please enter password:") #密文
    12. If user = = Username and pwd = = passwd:
    13. Print ("Welcome, lizhong!")
    14. Else
    15. Print ("User name and password error")
1.11.2 If...elif...else
      1. #!/usr/bin/env python
      2. Name = input (' Name: ')
      3. Age =int (Input (' Age: '))
      4. Job = input (' job: ')
      5. If Age >40:
      6. msg = ' You are too old! '
      7. Elif Age >30:
      8. msg = ' You had a few years to having fun '
      9. Else
      10. msg = ' You're still quite young. '
      11. Info= "' \tname:%s
      12. \tage:%d
      13. \tjob:%s
      14. ——————————————–
      15. %s
      16. "'% (name,age,job,msg)
      17. Print (info)

The path of Python (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.