The basics of the Python step-up tutorial series

Source: Internet
Author: User
Tags naming convention

  1. The difference between compiled language and interpretive language

    Compiled and interpreted type, both have advantages and disadvantages, the former compilation speed, low system requirements, suitable for large-scale applications, development of operating systems, such as C, C + + and other compiled language, and Python, Java, JavaScript and other languages are interpreted language, compilation speed is slower than the former, And the compatibility between the system platform is also a certain requirement, here is mainly Python introduction.


    The internal mechanism of interpretive language

    1) Python first compiles the script into bytecode (PYO,PYC)

    2) Python virtual machine explanatory and run bytecode file

    3) return results


    The internal mechanism of a compiled language

    1) First compile the source code into machine code (machine code)
    2) Generate executable file
    3) Run the executable file

    4) return results

    When the code file is modified again, the interpreted language does not need to generate the executable file as the compiled language does before it can take effect.


  2. Python supports Chinese encoding

    #coding =utf-8 or #-*-Conding:utf-8-*-

    #上面其实你随便怎么写都可以, as long as coding[:=]\s* is met ([-\w.] +) format.


  3. The
  4. Python variable and the assignment

    are all data objects in Python; all variables are references to data (reference: When a data object is assigned to a variable, the change amount refers to the data object.)


    variable naming convention : You cannot use a keyword, you cannot start with a number, you cannot assign a value with the operator

    : Dynamic property, Determining data type When assigning values, multiple assignments, deleting


    Multiple assignments:

    >> a = "str"
    >>> a,b,c = "str", "STRB", 4
    > >> print a,b,c
    str strb 4
    >>> print a
    str
    >>> print b
    strb

    Delete & Multiple Delete variables:

    >>> a = "test"
    >>> a
    ' test '
    >>> del a
    >>> a
    Traceback (most recent):
      File "<stdin>", line 1, in <module>
    Nameerror:name ' a ' isn't Defined
    >>>
    >>> del a,b,c (see multiple assignments)

  5. Three commonly used built-in functions

    Type () View variable properties

    Help () View Document assistance information
    Dir () returns a list of module-defined names

    >>> Import Time #导入模块
    >>> Help (Time)
    >>> Help (Time.sleep)
    >>> Time.sleep (3) #停顿三秒
    >>> Dir (Time)
    [' __doc__ ', ' __file__ ', ' __name__ ', ' __package__ ', ' accept2dyear ', ' altzone ', ' asctime ', ' clock ', ' ctime ', ' Daylight ', ' Gmtime ', ' localtime ', ' mktime ', ' sleep ', ' strftime ', ' strptime ', ' struct_time ', ' time ', ' timezone ', ' tzname ', ' Tzset '

  6. First Python script Hello world!

    Typically, the Linux system installs the Python suite by default if it is not installed via Yum or rpm

    #rpm-IVH python-2.6.6-37.el6_4.x86_64 or yum-y install Python


    Currently you can see from the following example that my Python version is 2.6. This does not affect the use, if you want to use the latest version can be directly from the official website http://www.python.org/download and installation, the latest version of Python is 3.x its syntax is slightly different from 2.x, It is recommended that you use more than 2.6 of Python to learn it first.

    [[email protected] ~]# rpm-q python
    python-2.6.6-37.el6_4.x86_64 

    [[email protected] ~ ]# python
    Python 2.6.6 (r266:84292, Jul, 22:48:45)
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
    Type "Help", "copyright", "credits" or "license" for more information.
    >>>
    >>> print "Hello world!"                 #可以从屏幕闪刚看到 "Hello world! "
    Hello World!"
    >>>

    Print is a common python keyword that features output information. In Python 3.x, the syntax of print changes and is used as a function, so the above example should be written as:

    Print ("Hello world! ")

    Print function prints integer, floating point and other data

    >>> Print 10
    10
    >>> Print 3.14
    3.14
    >>> print (' June ')
    June
    >>> print ("www.51cto.com")
    Www.51cto.com

    2) through small programs

    Use the text editor vim to end the file with. py, such as write a hello.py file


    [email protected] 51cto]# cat hello.py
    #!/usr/bin/python

    print ' Hello world! ‘

    How Python scripts are executed

    [email protected] AA python hello.py
    Hello world!
    [Email protected] 51cto]# chmod +x hello.py #赋予可执行权限
    [Email protected] 51cto]#./hello.py
    Hello world!


  7. One practice per day

    (1) Remove all preceding spaces in the string. Lstrip

    (2) Remove all the blanks behind the string. Rstrip

    (3) Remove the spaces on either side of the string. Strip

    >>> a = "ABC Test"
    >>>
    >>>
    >>> Print A.rstrip ()
    ABC Test
    >>> Print A.lstrip ()
    ABC Test
    >>> Print A.strip ()
    ABC Test
    >>>


    String "ABCdef"

    (1) Please export it to uppercase

    (2) Please export it to lowercase


    >>> dir (str)
    >>> a = "abcdef"
    >>> A.swapcase ()
    ' ABCDEF '
    >>> A.lower ()
    ' ABCdef '
    >>>


The basics of the Python step-up tutorial series

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.