Python must-see Introduction to the Basic text

Source: Internet
Author: User
about 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.

Python vs. Other languages:

C and Python, Java, C #, etc.

C Language: The code compiles the machine code, the machine code executes directly on the processor, each instruction controls the CPU to work

Other languages: Code compiles to get bytecode, virtual machines execute bytecode and convert to machine code and then execute on processor

The language of Python and C Python was developed by C.

For use with: Python's class library is complete and concise, if you want to implement the same functionality, Python 10 lines of code can be solved, C may require 100 lines or more.

For speed: Python runs faster than C, forcing it to slow down.

Python and Java, C #, etc.

For use with: Linux original Python, no other languages; The above languages have a very rich class library support
For speed: Python may be slightly slower in speed

So, there is no essential difference between Python and other languages, other differences are: good at a field, rich in talent, preconceived.

Types of Python

CPython

The official version of Python, implemented using the C language, is the most widely used, and the CPython implementation converts the source file (py file) into a bytecode file (PYc file) and then runs on the Python virtual machine.

Jyhton

Python Java implementation, Jython will dynamically compile Python code into Java bytecode, and then run on the JVM.

IronPython

In Python's C # implementation, IronPython compiles Python code into C # bytecode and then runs on the CLR. (similar to Jython)

PyPy (Special)

Python implements Python, which compiles Python bytecode bytecode into machine code.

Rubypython, Brython ...

Install Python

Windows:

1. Download the installation package

https://www.python.org/downloads/

2. Installation

Default installation path: C:\python27

3. Configure Environment variables

"Right-click Computer"-"Properties"-"Advanced system Settings"-"Advanced"-"Environment variable"-"in the second content box to find the variable named path of a row, double-click"-"Python installation directory appended to the variable value, with;

such as: the original value; C:\python27, remember that there's a semicolon in front

Linux:

No installation required, original Python environment

PS: If you bring your own 2.6, please update to 2.7

Update python

Windows:

Unloading load can be

Linux:

View the default Python version

Python-v

1, install GCC, for compiling python source code

Yum Install GCC

2, download the source package, https://www.python.org/ftp/python/

3. Unzip and enter the source file

4. Compile and install

./configure

Make all

Make install

5. View version

/usr/local/bin/python2.7-v

6. Modify the default Python version

mv/usr/bin/python/usr/bin/python2.6

Ln-s/usr/local/bin/python2.7/usr/bin/python

7. To prevent Yum from performing exceptions, modify the Python version used by Yum

Vi/usr/bin/yum

Change the head #!/usr/bin/python to #!/usr/bin/python2.6

Getting Started with Python

First, the Interpreter

If you want to execute a python script like executing a shell script, for example:./hello.py, then you need to specify the interpreter in the header of the hello.py file, as follows:

#!/usr/bin/env python  print "Hello,world"

PS: Need to give hello.py execute permission before execution, chmod 755 hello.py

Second, Content coding

The Python interpreter encodes the content when it loads the code in the. py file (default Ascill)

ASCII (American Standard Code for Information interchange, United States Standards 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 used up to 8 Bit to represent (one byte), that is: 2**8 = 256, so the ASCII code can only represent a maximum of 256 symbols.

It is clear that the ASCII code cannot represent all the words and symbols in the world, so it is necessary to create a new encoding that can represent all the characters and symbols, namely: Unicode

Unicode (Uniform Code, universal Code, single code) is a character encoding used on a 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), that is: 2 **16 = 65536,
Note: Here is a minimum of 2 bytes, possibly more

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  print "Hello, World"

Correction: It should be shown to tell the Python interpreter what code to use to execute the source code, i.e.:

#!/usr/bin/env python#-*-coding:utf-8-*-  print "Hello, World"

Third, comments

When the line stares: # is annotated content

Multiline Comment: "" "Annotated Content" ""

Iv. executing script incoming parameters

Python has a large number of modules, which makes developing Python programs very concise. The class library includes three:

Python internally supplied modules
• Industry-Open Source modules
• Modules developed by programmers themselves

Python internally provides a SYS module where SYS.ARGV is used to capture parameters passed in when executing a python script

#!/usr/bin/env python#-*-coding:utf-8-*-  import sys  print SYS.ARGV

V.. pyc file

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

PS: Code is compiled to generate bytecode, and bytecode can be obtained by decompile.

Vi. variables

1. Declaring variables

#!/usr/bin/env python#-*-coding:utf-8-*-  name = "Zhangyanlin"

The code above declares a variable named: Name, and the value of the variable name is: "Zhangyanlin"

Rules for variable definitions:

Variable names can only be letters, numbers, or underscores the first character of any combination variable name cannot be a number The following keyword cannot be declared as a variable name
[' 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 ']

VII. Input

#!/usr/bin/env python#-*-coding:utf-8-*-  # Assigns the user input to the name variable name = raw_input ("Please enter user name:")  # Print the contents of the input print name

When entering a password, if you want to be invisible, you need to take advantage of the Getpass method in the Getpass module, namely:

#!/usr/bin/env python#-*-coding:utf-8-*-  import getpass  # Assigns user input to the name variable pwd = getpass.getpass ("Please enter password:") c5/># Print the contents of the input print pwd

Viii. Process Control and indentation

Requirement One, user login verification

#!/usr/bin/env python#-*-coding:encoding-*-  # Prompt for user name and password #  verify username and password #   if error, output username or password error #   If successful, output welcome , xxx!  Import getpass    name = raw_input (' Please enter user name: ') pwd = Getpass.getpass (' Please enter password: ')  if name = = "Zhangyanlin" and pwd = = "123 456 ":  print" Welcome, zhangyanlin! "Else:  print" User name and password error "

Nine, while loop

1. Basic cycle

While condition:     # loop Body   # If the condition is true, then the loop body executes  # if the condition is false, the loop body does not execute

2. Break

Break to exit all loops

While True:  print 123   break

3, continue

Continue is used to exit the current loop and continue the next loop

While True:  print 123   continue

4. #列出100之内所有的奇数

Odd = 1while odd <=:     print (odd)  odd + = 2

5. #列出100之内所有的偶数

even = 0while even <= 100:print (even) even + = 2

6. Figure out the 1-2+3-4....+99 and

#排除99剩余49组1减2u1 =49print (u1* (1-2) +99)

Or

#列出1 99 and Start=1sum = 0while start <:  temp = start%2  If temp = = 1:     sum = sum+start  else     sum =sum-start  start+=1print (sum)

The above is a small series for everyone to bring the first Python basic knowledge of Python must see the whole content, I hope you support the script home ~

  • 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.