Python Basics (i)

Source: Internet
Author: User
Tags python script

first, the Python environment

Windows:

1. Download the installation package    HTTPS://WWW.PYTHON.ORG/DOWNLOADS/2, install the    default installation path: C:\python273, configure    the environment variable "right-click Computer"-"Properties"-"Advanced system Settings" --"Advanced"-"Environment variable"-"in the Second content box find a row of the variable named path, double-click the"-"Python installation directory appended to the variable value, use; split"    such as: the original value; C:\python27, remember that there's a semicolon in front

Linux:

No installation, original Python environment   PS: If you bring your own 2.6, please update to 2.7

Second, update python

Windows:

Unloading load can be

Linux:

Linux Yum relies on its own python, in order to prevent errors, the update here is to install another python! view the default Python version python-v   1, install gcc for compiling python source    yum Install Gcc2 , 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    install5, view version    /usr/local/bin/python2.7-v6, modify the default Python version    mv/usr/bin/python/usr/bin/python2.6    ln-s/usr/local/bin/ Python2.7/usr/bin/python7, 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 basics

One, the first sentence of life python file

Vim hell.py (Create)

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

Perform:

Python hello.py can or increase execution permissions: chmod 755 hello.py./hello.py

Explain:

1 #!//usr/bin/env python   #脚本用env启动的原因 because the script interpreter may be installed in different directories in Linux, and Env can be found in the system's path directory. At the same time, Env also prescribes some system environment variables. ENV is: envirnment environment variable) 2 #-*-Coding:utf-8-*-Specify the encoding type Utf-8 (read on for more)

Second, the Python version of the difference between:

    • 2.x vs. 3.x comparison:
     One, print difference                 2.x       : Print  "" or print () printing can output 3.x normally       : Only print  () This form, otherwise it will appear    SyntaxError Input   raw_inut difference                 2.x: input   : Output The raw data type, enter what type of value, the output what type                   :   raw_ Input: All output as a string         3.x       : The   Raw_input method is canceled, only input ()              three, class difference                    2.x        :  support the new class, and classic classes, when using modern classes, the class inheritance order affects the result of the resulting inheritance        3.x        :  You must use the modern class to solve the problem of inheritance order between classes        four,/            2.x       :  For example: 1/ The value of the 2,2.x output is 0        3.x       :  For example, the value of 3.x output is 0.5

  

Iii. python basic data type

int has symbolic shaping, such as: 1,11,111,-1,-111, 2.x range between -2**31~2**31-1, depending on the operating system, will not be less than this range float float type, such as: 1.11.1.111,-1.111 ... str string, such as: ' Hello ', ' python ', ' 1 ', ' string ' ..., note that the string needs to be enclosed in quotation marks (') or          double quotation marks (""), bool Boolean type, only two values, true: True  false: false, any non-zero data type , the result is true,          when the result is int 0 o'clock, then the Falselong type only exists in the 2.x version, the range of values is infinitely large, depending on the available virtual memory complex complex number, such as: 3 tuples (tuple)  such as (' A ', ' Hello ', ' Python ', ' 1 ') lists (list) such as [' A ', ' hello ', ' python ', ' 1 '] dictionary (dict)    as {' name ': ' Zcy ', ' age ': ' Job ', ' IT '}

Iv. python annotations

Python has two ways of commenting, a single-line comment, and a # symbol in front of the statement, or you can use a multiline comment, use a continuous three single quotation marks on either side of the content range that needs to be commented on, or use a continuous three double quotation mark. For example:

"' Here is

Comments

‘‘‘

"""

This is

Comments

"""

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.

Vi. Script Incoming parameters

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

    • Python-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")

Seven, variable

    1. The role of a variable: a nickname that refers to what is stored in an address in memory
    2. Rules for variable definitions:
    • 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 ']

3. Variable assignment operation

A = 5print (ID (a)) #打印结果为 140723441682376

  

 

Python Basics (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.