Python development [Article 1]: first understanding of Python and first understanding of python

Source: Internet
Author: User

Python development [Article 1]: first understanding of Python and first understanding of python

I. python Environment

1. Install Python

Windows:

1, download installation package 2 https://www.python.org/downloads/3 2, installation 4 default installation path: C: \ python275 3. Configure environment variables 6. Right-click the computer and choose Properties> advanced system Settings> advanced> environment variables. find a line with the variable name Path in the content box, double-click to append the Python installation directory to the variable value and separate it with;. For example, 7: The original value; C: \ python27. Remember that there is a semicolon before it.

Linux:

1 No installation required, original Python environment 2 ps: if it comes with 2.6, please update to 2.7

2. Update Python

Windows:

Uninstall and reinstall

Linux:

Linux yum dependency comes with Python. To prevent errors, the update here is actually to install another Python

1 check the default Python version 2 python-V 3 4 1. install gcc to compile Python source code 5 yum install gcc 6 2. Download the source code package, https://www.python.org/ftp/python/ 7 3, extract and enter the source code file 8 4, compile and install 9. /configure10 make all11 make install12 5. View version 13/usr/local/bin/python2.7-V14 6. Modify the default Python version 15 mv/usr/bin/python/usr/bin/ python2.616 ln-s/usr/local/bin/python2.7/usr/bin/python17 7. Prevent yum execution exceptions, modify Python version 18 vi/usr/bin/yum19 of yum to set the header #! /Usr/bin/python #! /Usr/bin/python2.6

 

Ii. Getting started with Python

1. Write the first program

1 #!/usr/bin/env python2 # _*_ coding:utf-8 _*_3 # Author: JiaChen4 5 print("Hello World!")
First Program

Execute Python hello. py

The internal execution process of Python is as follows:

Lexical Analysis-> compilation-> execution

2. Interpreter

When running Python hello. py in the previous step, it is clearly indicated that the script is executed by the Python interpreter.

Programming languages are classified from the following perspectives: compiler and interpretation, static and dynamic languages, strong and weak definition languages

What is the difference between compilation and interpretation?

The compiler compiles every statement in the source program into a machine language and saves it as a binary file. In this way, the computer can run the program directly in the machine language at a high speed;

The interpreter is interpreted as a machine language for execution only when the program is executed, so the running speed is not as fast as that of the compiled program.

This is because computers cannot directly understand and execute the statements we write. They can only recognize machine languages (in the form of a second-level system)

Compilation type

Advantage: the compiler usually has a pre-compilation process to optimize the code. It is thought that only one compilation task is required, and no compilation is required during the runtime. Therefore, the execution efficiency of compiled languages is high. It can run independently from the language environment.

Disadvantage: After compilation, you need to re-compile the entire module if you need to modify it. During compilation, machine code is generated based on the corresponding runtime environment. Porting between different operating systems may cause problems. You need to compile different executable files according to the operating system environment.

Interpreted type

Advantage: it has good platform compatibility and can run in any environment, provided that the interpreter (Virtual Machine) is installed ). Flexible. You can directly modify the code when you modify it. It can be deployed quickly without downtime.

Disadvantage: it must be explained every time it is run. The performance is inferior to that of compiled languages.

3. Content Encoding

In python, the default encoding is UTF-8, which has two data types: str and bytes

4. Notes

Single line comment # comment content

Multi-line comment "comment content """

5. pyc File

When you run the Python code. py file, a file with the same name is automatically generated during execution. pyc file, which is the bytecode generated after the Python interpreter is compiled.

 

Iii. Variables

1. Declare Variables

1 name = "Jack"
Declare a variable

The variable name is "name" and the value is "Jack". The value is in memory and the variable name points to the address in memory.

Rule for variable definition:

Variable names can only be any combination of letters, numbers, or underscores

The first word of the variable name cannot be a number.

Some special Python names cannot be used as variable names, such as def class.

2. Variable assignment

1 name1 = "Jack"2 name2 = "Tom"
Case 1

In this case, name1 and name2 point to their respective memory addresses.

1 name1 = "Bob"2 name2 = name1
Case 2

In this case, name1 and name2 point to the same memory address.

 

Iv. format input and output

1 username = input ("Please input your username:"). strip () 2 3 # input. strip () is standard and used to delete spaces before and after user input content
Format input
1 name = "Tom" 2 age = 183 phone = "123123123123" 4 5 print ("name: % s age: % s Tel: % s") % (name, age, phone)
Format output

The getpass module is introduced to hide password input, but cannot be used in windows.

1 import getpass2  3 password = getpass.getpass('password:')
Use getpass

 

V. Process Control and ternary Computation

1. Condition judgment

1 age = 102 3 if age > 5:4     pass5 elif age < 5:6     pass7 else:8     pass
If else

2. for Loop

1 for i in range(10):2     print(i)
For

3. while Loop

1 while True:2     ...
While

4. break and countinue

Break jumps out of the overall Loop

Countinue jumps out of the current loop and continues the next loop

5. Ternary operations

1 a = 12 B = 23 c = 34 5 # if a> B result = a = 1 if a <= B result = c = 36 result = a if a> B else c
Ternary Computation

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.