Python Self-Study notes – Basic Overview
Print () function
The usual print function, similar to the C language's printf function. And can be used in conjunction with the string format operator (%) to implement the string substitution function
>>> print '%s is number%d! '% ("Python", 1)
python is number 1!
Raw_input () function
A data entry function that is typically used to read data from a standard screen and assign it to the corresponding variable python is a dynamic language, so you do not need to declare the type of the variable beforehand. The value and type of a variable are determined by the compiler when the variable is initialized. In addition, the Python language does not support the C language's self amplification and self operation. Python interprets –n as-(-N) to get N. Python supports the use of pairs of single or double quotes, triple quotes (three consecutive single quotes or double quotes) that can be used to contain special characters. You can get substrings by using the index operator ([]) and the Slice operator ([:]). A string has its own index rule: the first character is indexed by 0, and the last character is indexed to-1.
The plus sign (+) is used for string concatenation operations, and the asterisk (*) is used for string duplication.
Use format for IF statements
If expression1:
if_suite
elif expression2:/*elif equivalent else-if*/
elif_suite
else:
else_suite
If the value of an expression is not 0 or is a Boolean value True, then the code group If_suite is executed; Otherwise, go to the next
Statement. A code group is a Python term that consists of one or more statements representing a child code block.
Unlike other languages, conditional bars do not need to be enclosed in parentheses
Error and exception handling
To add error detection and exception handling to your code, simply encapsulate them in the try-except statement. Try
The code group that follows is the code you intend to manage. The code group after except is the code that you handle the error. You can also intentionally throw an exception by using the Raise statement.
Try:
filename = raw_input (' Enter file name: ')
fobj = open (filename, ' R ') for
eachline in fobj:
print each Line, Fobj.close ()
except IOError, E:
print ' File open error: ', E
function definition
The syntax of a function is defined by the DEF keyword and the function name that follows it, plus the number of parameter groups that the function requires
Yes. function arguments (compared to the arguments in the example above) are optional, which is why they are placed in brackets
The reason. This statement ends with a colon (:) (with the IF and while
Statement, followed by the code group that represents the body of the function.
def function_name ([arguments]):
"Optional documentation string"
Function_suite
The parameters of a function can have a default value (the default parameter), and if a default value is provided, in the function definition, the argument is evaluated
Form provided. In fact, this is just a syntax that provides the default parameter, which means that if this argument is not supplied when the function is called, it
Take this value as the default value. For example:
>>> def FNC (debug=true):
Definition of Class
Use the class keyword to define a class. You can provide an optional parent class (base class); If there is no suitable base class,
Then use object as the base class. The class line is followed by an optional document string, static member definition, and the determination of the method
Yi.
Class ClassName (Base_class[es]):
"Optional documentation string"
static_member_declarations
method_ Declarations
For example
Self, nm= ' kill_root '): "" "
Constructor" ""
self.name = NM # class instance (data) attribute
print ' Created a class instance for ', NM
When a class instance is created, the __init__ () method executes automatically after the class instance is created, and the class
Like a build function. __INIT__ () can be used as a build function, but unlike a build function in other languages, it does not create
To build an instance, it is just the first way to execute your object after it is created. Its purpose is to execute some of the necessary initial
Start the work. By creating your own __init__ () method, you can override the default __init__ () method (the default
method does nothing, so that you can decorate the object you just created. In this case, we initialize a name named
class instance properties (or members). This variable exists only in the class instance, and it is not part of the actual class itself.
__INIT__ () requires a default parameter, as described in the previous section. There is no doubt that you have noticed that every method has
, self It is a reference to the class instance itself. Other languages typically use an identifier named this
The value of the self.__class__.__name__ variable. For an instance, this variable represents the instantiation of its
The name of the class. (self.__class__ refers to the actual class)
Module
A module is a form of organization that organizes Python code that is related to each other into separate files.
A module can contain executable code, functions and classes or combinations of these things.
When you create a Python source file, the name of the module is the filename without the. py suffix. A module to create
Once built, you can import the module using the import statement from another module to use.
Once the import is complete, a module's properties (functions and variables) can be accessed through the familiar. Period Property Identification method
Import Module_name
Summary
| function |
Description |
| Dir ([obj]) |
Displays the properties of the object, and displays the name of the global variable if no argument is supplied |
| Help ([obj]) |
Displays the object's document string in a neat and beautiful form, and if no arguments are supplied, the interactive help is entered. |
| int (obj) |
Converts an object to an integer |
| Len (obj) |
Returns the length of an object |
| Open (FN, mode) |
Open a file named fn with mode (' r ' = Read, ' W ' = Write) |
| Range ([[Start,]stop[,step]) |
Returns a list of integers. The starting value is start, and the ending value is stop-1; The start default value is 0, and the step default is 1. |
| Raw_input (str) |
Waiting for the user to enter a string, you can provide an optional parameter, str, as the cue message. |
| STR (obj) |
Converts an object to a string |
| Type (obj) |
Returns the type of the object (the return value itself is a type object.) ) |
Document strings with dir.__doc__ access to DIR () built-in functions