Python (v)

Source: Internet
Author: User

1. function functions

Functions are small or small programs that can implement some specific functions, and in Python there are many built-in functions, and simply put, the function is you write some statements, in order to facilitate the use of these statements, the combination of these statements, give him a name, use only to call this name, You can implement the function of the statement Group.


>>>

>>> 2**3

8

>>> POW (2,3)

8

>>>

The built-in function POW is to calculate the Exponentiation.

@ What is the built-in function and how to use the built-in Function.

Python system in some of the functions are called built-in functions, do not need our own writing, there is a third-party function, is someone else to make up some of the functions, shared out for everyone to use, the front of the two functions are can be directly used, and finally we to facilitate their work learning function, is called a custom Function.

@ Define a Function's method

Define the function to use the DEF statement, the specific function syntax format:

def function name (parameter):

code block

When defining a function, it is important to note that:

1.def, which represents the definition Function.

2.def and function to strike a space Between.

3. The function name is Followed. There is no requirement for this name, so it is easy for users to use it.

4. The function name is followed by parentheses (). Represents a defined function that can be added to a parameter.

5. Be sure to add a colon after the parentheses (): this is Important.

6. The block part of the code is made up of statements that are Indented.

7. The function should have a return value reture

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

2. Class is an abstract concept that is more abstract than functions, Python is an object-oriented programming language (oop), object-oriented objects, classes and functions are different:

Advantages of the @ class

A. The object of a class is polymorphic: that is, multiple states, which means that we can use the same method of operation with different class objects without the need for additional writing Code.

B. Encapsulation of classes: after encapsulation, you can directly invoke the object of the class to manipulate some internal methods without having to let the user see the details of the Code's Work.

C. Inheritance of classes: classes can inherit their methods from other classes or meta-classes and use them directly.

The syntax for the @ definition class (classes):

>>> class Iss:

... def FA (self,name):

... self.name =name

Class is followed by the name, class name of the first letter is generally uppercase, this kind of code for easy to distinguish the whole class, and finally must remember to add a colon:

The biggest difference between class and function is that there is a ' self ' inside of him, a parameter whose function is to refer to the object itself.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

2.import statements

The import statement is used primarily for importing modules, which can appear anywhere in the program

His syntax format is as Follows:

Import Modult

Keyword Module name

Let's say the following example:

Import Math #导入math模块

Math.floor () #调用mat模块中的floor () function

If you need to import multiple modules at the same time, simply isolate them with commas before the module Name. In general, it is less readable to import multiple modules at the same time. A single import module is Recommended. And That's when we usually start again. import the module, because the Python interpreter takes the scope into account when executing the Statement. If you just start importing the module, then his scope is global, and if you import the module in the middle, his role is local, and can not be called by other functions, if the other functions to use the same module, but also need a separate import,

It is best to follow this order when importing modules using import:,

1.python Standard library Module

2.python Third-party Modules

3. Custom Modules

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

How to use the 3.python open () Function.

Opening the file will use the open function, the standard Python opening file syntax is as Follows:

Open (name[,mode[,buffering]])

The file name of the Open function is required, and the mode and buffer parameters are optional, If you have a file a.txt text file, You can use the following command to open it from C:\Test

>>>x = Open (r ' c:\test\a.txt ')

Opens the text under the corresponding path in a read mode if the text does not Exist. The program will get an Error.

@open function File Open mode parameter what are the common Values.

There are several main modes of file Open:

' r ': read mode ' W ': write mode ' G ': append mode ' b ': binary mode ' + ' Read/write mode

How to write @python file

>>>f = open (' a.txt ', ' W ') #用写的方式打开a. txt This file, and copy to F

>>>f.write (' hello, ') The F.write method writes the contents of the parentheses inside the File.

>>>f.write (' Iplaypython ') has the same meaning as the second line, but this appends him to the existing data

>>>f.close () finally closes the file, there is open there is a close

How to read Python files

To read the file operation, only need to change the mode to ' r ', because the default is read

>>>f =open (' a.txt ', ' r ')

>>>f.read ()

Read () is a method of reading a file, filled with the number of characters to read in parentheses

There are other ways to open a file

A.reead (): reads all content

B.readline (): indicates progressive read.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

4. Exceptions

In python, whenever an error occurs in the code, whether it is a syntax error or an indentation error, an exception is thrown if such an exception is not Paved. The program will backtrack, throw an exception, and terminate the Program's Operation.

@raise

We can actively throw exceptions to the Python program, and we can use the raise statement to trigger the exception

>>>raise Exception #触发python异常类

@ Catch exception

You can use the try and except Languages.


Python (v)

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.