Python Knowledge points

Source: Internet
Author: User

After a couple of contacts with Python, I briefly talked about my initial understanding of Python.

I. Basic knowledge

Basic knowledge of Python: Python features {explanatory scripting language; object-oriented language; Dynamic language variable type is not fixed; default encoding Utf-8}

Again his basic rules are divided into lines, indents, blank lines, and modules. The cross-line also includes the closing operator, various symbols, three quotation marks, often used for multi-line comments; indent blocks are the best 4 spaces to differentiate a block of code with indentation depth, such as a function, a class, or a blank line. The last module means that each script file is a module.

Run Python: Divided into two modes 1: Scripted programming: Source code script, one-time run and command-line mode

2: Interactive programming: Perform progressive input again.

Run script: 1:python*.py command-line mode

2:linux executable script ① script first line add: #! /user/bin/env Python (env command)

#!/user/local/bin/python (Pythhon Path)

② the command line to give permission chmod 755*.py

③ Run script *. Py

3:lpython%run*.py

Variable assignment: 1: Assigned value =

2: Incremental Assignment + =

3: Multiple assignment X=y=z=1 (assigned to x, Y, Z with a reference)

4: Multivariate assignment ①x,y,z=1,2, ' a ' {① equals to both sides of tuple

② Recommendation: (x, y, Z) = ("a")

② Variable Exchange x,y=y,x

Two • Program structure

Pythonde program structure has, continue statement, comment and break statement, range function And while loop, for loop, if elif else.

1.if Elif Else statement

①age=13

If age<18:

Print ("Oxox,work forbidder")

②name= ' Bang '

If name is ' bang ':

Print (' Bangbangbang ')

Elif name is ' Lucy ':

Print (' Hello Lucy ')

Elif name is ' linning ':

Print (' Hello linning ')

Name= ' hehe '

If name is ' bang ':

Print (' Bangbangbang ')

Elif name is ' Lucy ':

Print (' Hello Lucy ')

Elif name is ' linning ':

Print (' Hello linning ')

Else

Print ("Can not find" + name)

2.Rang functions And while loops

① For example create a loop of 0-9 loops 10 times

For x in range (10):

Print (x)

Ii

Count=5

While Count <10;

Print (count)

three · functions

functions are well-organized, reusable pieces of code that are used to implement a single, or associated function.

functions can improve the modularity of the application, and the reuse of the code. You already know that Python provides a number of built-in functions, such as print (). But you can also create your own functions, which are called user-defined functions. Define a function
You can define a function that you want to function, and here are the simple rules:

The function code block begins with a def keyword followed by the function identifier name and parentheses ().
any incoming parameters and arguments must be placed in the middle of the parentheses. Parentheses can be used to define parameters.
the first line of the function statement can optionally use the document string-for storing the function description.
The function contents begin with a colon and are indented.
return [expression] ends the function, optionally returning a value to the caller. Return without an expression is equivalent to returning None. Syntax:

Defines a function that gives the function a name, specifies the parameters contained in the function, and the code block structure.

Once the basic structure of this function is complete, you can execute it from another function call or directly from the Python prompt.

The following instance calls the Printme () function:

The result of the above example output:

as follows: 

Def oxox ():

Print (' work ')

Run result, no output

Def oxox ():

Print (' work ')

oxox () The result of running; Four • ModuleA module is a file that contains all of the functions and variables that you define, followed by a. PY name. Modules can be introduced by other programs to use functions such as functions in the module. This is also the way to use the Python standard library.
Here is an example of using a module in the Python standard library. the execution results are as follows 1. Import SYS introduces the sys.py module in the Python standard library, which is the method of introducing a module.
2. SYS.ARGV is a list that contains command-line arguments.
3. Sys.path contains a list of paths that the Python interpreter automatically finds for the required modules.

Python Knowledge points

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.