Python Basics (Getting started)

Source: Internet
Author: User

First, Python introduction, features, Applications

Python is a computer programming language, and Python is a computer programming language that was written by the Dutch Guido van Rossum during Christmas 1989 to get rid of the boring Christmas, as the ABC language inherits

Characteristics:

    • Object-oriented, interpretive, dynamic, advanced computer programming language, the official definition of it is elegant, clear, simple.
    • Open source free, cross-platform, portable, and can be used on a variety of systems
    • Said to be easy to learn, simple and easy to study, powerful.
    • Standard libraries, third-party libraries that cover a variety of functions. Batteries included (called Built-in battery)
    • Let developers focus on solving problems without being too concerned about the underlying

Application:

Python is very popular in China in recent years, and is generally used to do things: automated operations, automated testing, data analysis, crawler development, operational scripting, WEB development, and more. China has a large number of knowledge, watercress and other applications of Python development, Anyway, a lot of people are learning, they're doing it.

Types of Python:

    • CPython, the C language implementation of Python, the default interpreter
    • Jpython, Java implementation of Python
    • Ipython
    • PyPy, a special Python interpreter for Python implementations
    • Rubypthon\ipython ... Wait a minute
second, start Python

1, installation, environment

Python can be cross-platform, can be installed on the win and Linux systems, Python currently has 2.x and 3.x versions, 2 is the mainstream version of the use, 3 is the trend

    • Windows platform installation, download version from official website, is basically the next step
    • Linux Platform Installation
Tar XF python-2.7.11.tgz         #解压, compile and install CD python-2.7.11./configure--prefix=/usr/local/make-j2 && make INSTALLM v/usr/bin/python/usr/bin/python2.6.6                #重命名系统默认的pythonln-S/usr/local/bin/python  /usr/bin/python           #  Create a soft connection as a new Python version of the system Vim/usr/bin/yum                      #修改python为python2.6 to prevent Yum from using exceptions # python                                  #查看python版本Python 2.7.11 (Default, Dec 17 2015, 01:08:56) [GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on Linux2type ' help ', ' copyright ', ' credits ' or ' license ' for more information.

For convenience, Add tab Auto-completion

Vim ~/.pythonstartup     #添加如下 #!/usr/bin/python import sysimport readlineimport rlcompleterimport atexitimport os# tab Completion readline.parse_and_bind (' Tab:complete ') # history file Histfile = Os.path.join (os.environ[' HOME '), '. Pythonhistory ') Try:    readline.read_history_file (histfile) except IOError:    passatexit.register ( Readline.write_history_file, Histfile) del os, Histfile, ReadLine, Rlcompleter

Add to ~/.BASHRC

echo ' Export pythonstartup= ' ~/.pythonstartup '  >> ~/.bashrcsource ~/.BASHRC    

Test

[Email protected] ~]# Pythonpython 2.7.11 (default, Mar, 23:35:44) [GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on Lin Ux2type "Help", "copyright", "credits" or "license" for more information.>>> import os>>> os.t                            #tab Can complement Os.tcgetpgrp (  os.tcsetpgrp (  os.tempnam (    os.times     (Os.tmpfile (Os.tmpnam ( Os.ttyname (    

2. Eggs

>>> import thisthe Zen of Python, by Tim peters<br>beautiful is better than ugly.               #美丽比丑陋好Explicit is better than implicit.                  #显示比隐式好Simple is better than complex.             #简单比复杂好Complex is better than complicated.                     #复杂比更复杂好Flat is better than nested.                    #扁平比嵌套好Sparse is better than dense.                              #分开比密集好Readability counts.           #可读性很重要Special cases aren ' t special enough to break the rules.                             #即使特殊情况也不要打破规则Although practicality beats purity.                             #即使讲究实用性Errors should never pass silently.                                        #错误不能默默地通过 (precise catch anomaly) unless explicitly silenced.                 #除非明确要这样做In the face of ambiguity, refuse the temptation to guess.      #面对歧义, don't guess (try) there should be one--and preferably just one--obvious way to do it. #应该有一个而且是最好的方法Although that is obvious at first unless you ' re Dutch.                                                 #虽然这并不容易Now is better than never. #现在开始做比从来不做好, do think before you know although never is often better than *right* now.            If the implementation is hard to explain, it's a bad idea.      #如果代码很难解释, bad If the implementation is easy to explain, it could be a good idea.     #如果代码很容易解释, namespaces is one honking great idea – let's do more than those! #命名空间是个极好的想法, it should be advocated

3. Simple code

>>> print ' Hello word ' hello Word

When the calculator is used, the math is the same

>>> 1 + 11>>> 10-19>>> 1 * 22>>> 2 * 12>>> 2/21>>> (2+3) * 420

Of course, you can write the code in the write to file execution, with. py as the file suffix

#!/usr/bin/env python      #coding: UTF8               #指定内容编码print ' hello,word ' Print 1 + 10print 10-1print 2 * 2print 2/2print ( 3) * 4

5. Code Execution process

From the top down, the code reads into memory---parsed by the interpreter---lexical analysis---syntax-bytecode---PVM

6. Comments

    • #, the code behind ' # ' will be ignored
    • "" "" "" "" "" ", the code will be ignored, generally used to make comments
    • Empty line or holding, will be interpreted by the interpreter ignored
#print ' Hello,word ' #代码被忽略 "Print 1 +" #代码被注释 "" "Print" ""     #代码被注释

7. Grammar rules

Force indentation, to indent, colon to determine the attribution of the code, the boundary, Python will automatically check, the advantage is that the code looks like a unified style, habit is good

Indentation Error

>>>  print ' hello,python! '               #多了个空格, indentation error  File "<stdin>", line 1    print ' hello,python! '    ^indentationerror:unexpected Indent

Syntax error

#coding: utf8if true:    print ' true ' else                                   #少了: number, syntax error    print ' False ' Result: File "test.py", line                  Else       ^syntaxerror:invalid Syntax

iii. variables, objects, references

Variable, the amount of change at any time. Variables are used to hold objects in memory, to reference specific objects at specific times, and to have constants.

1. Variable rules, definitions

    • The object of all certainty
    • Variable names can be uppercase and lowercase letters, numbers, underscores, and the first character cannot be a number
    • Variable name (left) = Object (right), only object is created, variable can be referenced
    • Variables do not conflict with reserved fields
    • Variables are created at initial assignment, and subsequent assignments override the value of the variable
    • Try to use meaningful, clearly understandable variable names
>>> var1 =                #定义一个int类型的对象100赋给变量var1 >>> var2 = ' string '           #定义一个str类型的对象 ' string ' assigned to the variable var2 >>> var1100>>> var2 ' string '

Procedure: First create object 100 and string in memory, then the variables point to these objects, the variables and the object to establish a reference relationship

>>> ID (VAR1), ID (var2) (25917344, 140258567970032)

The memory address of an object can be viewed through the ID function

Depth:

    • Variable-to-object connection relation reference, more like a pointer
    • The left side can be a variable name or an object element, the right can be an object or an expression that gets an object
    • The Type Property object, not the variable. Variable-to-object just points to the relationship

2. How to assign values to variables

Basic assignment, Sequence assignment, multi-objective assignment, enhanced assignment, multi-variable assignment

    • Basic Assignment
var1 = 200var2 = ' Strings '
    • Multi-objective assignment (shared reference, multiplexing)
var = var1 = Var2 = ' Zhang ' ID (name), ID (name1), id (name2)              #通过id命令可以发现引用同一对象 (139847989733872, 139847989733872, 139847989733872)

is to assign the string object ' Zhang ' to the var2,var2 assignment to Var1, to Var, which is the shared reference of the object (multiplexing)

    • Sequence assignment, unpacking, correspondence by position
>>> a,b,c,d = ' abcd ' >>> a,b,c,d (' A ', ' B ', ' C ', ' d ')
    • Enhanced Assignment
A + = 1        #等同于 a = a + 1a = 1        #等同于 a = a-1a *= 2        #等同于 a = a * 2
    • Multi-variable assignment, unpacking
User1,user2 = ' Zhang ', ' Li ' >>> user1,user2 (' Zhang ', ' Li ')

Python Basics (Getting started)

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.