A byte of Python: http://woodpecker.org.cn/abyteofpython_cn/chinese/index.html
The creator of python is Guido van rosum.
The official introduction to python is: "python is easy to learn and powerful.Programming LanguageIt has an efficient High-Level Data Structure and implements Object-Oriented Programming simply and effectively. Python's concise syntax and support for Dynamic Input, coupled with the essence of explanatory language, make it an ideal scripting language in many fields on most platforms, especially suitable for fast applicationsProgramDevelopment ."
Features: simple syntax, no memory management, portability, is an interpreted language, can be embedded in C/C ++ and other programs, with a large standard library, free open source.
There are four types of numbers: integer, long integer, floating point, and plural(-5 + 4j).
String: it can be a pair of single or double quotation marks. In addition, it can contain three single quotation marks, including multiple lines of strings. Any single quotation marks can be used as double quotation marks, for example, '''sam 'P' le '''. (Python does not have the char type)
Variable: directly assign values without declaring or defining the data type. Like basiC Language.
Code: A statement can end with a semicolon or save it. Press enter.
Python code hierarchies depend on Indentation rather {}.
Operator: similar to C, the difference is: ** multiplication, // division, ^ exclusive or ,~ Flip by bit (X's flip by bit is-(x + 1 ),~ 5 is 6), Boolean operations "not" "and" or "are not, and, or, and C respectively !, &, | Different.
Note: It starts with # and does not use the C/C ++ annotation symbol. It should be in order to follow the Unix script habits.
Control statement
Conditional Control: If Elif else is similar to C, but no parentheses are used, and a colon is added at the end of the sentence :. Python does not have a switch statement. You can use if Elif else or a dictionary.
While loop: While else (else is optional)
For Loop: for I in range (1, 5): Note that range (1, 5) indicates [1, 2, 3, 4], it is equivalent to the for (INT I = 1; I <5; I ++) of C ). The default step size of range is 1. Range (1, 5, 2) indicates that the step size is 2, [1, 3].
Break: Used to terminate the loop. Else will not be executed.
Function
Define with Def, for example, Def func1 (a, B = 1): The second parameter has the default value. You can call func1 (1) func1 (1, 2) func1 (B = 2, A = 3) to change the Parameter order.
Function parameters are passed in.
A variable has a scope. global variables can be declared.
Return Statement: If no return is returned, it is equivalent to return none.
Pass statement: indicates an empty statement block, for example, Def func (): Pass
Docstring: the string of the first logical line of each function (or module or class) is a document string. It is usually a multi-line string. The first line starts with an uppercase letter and ends with a full stop. The second line is empty, and the third line is detailed description. It can be printed out by func. _ Doc _.
Module: import the module through import. The module file extension must be. py. The module that has been compiled by byte is. PyC, which can accelerate the module loading speed and has nothing to do with the platform. The module is a separate. py file. Nothing special.
Data Structure: Three built-in data structures: List list, tuple, and dictionary.
List: a list that can be added, deleted, and queried. Alist = ['A', 'B', 'C']
Tuple: an unchangeable list. Atuple = ('A', 'B', 'C') Empty tuples directly (), but a member's tuples must be added with a comma atuple = ('A ',) to distinguish between a tuples and an object with parentheses in an expression. Print is the most useful place, for example, print '% s is % d' % (var0, var1)
Dictionary dict: D = {key1: value1, key2: value2} use d ['key'] to obtain the value
You can use index indexes to access members in lists and tuples, for example, alist [0]. The index value can be negative.-1 indicates the last element of the sequence.
Directly assigning values to a list does not generate a copy of the list, which is equivalent to just copying pointer. For example, list2 = list1 is the same. List2 = list1 [:] to generate a new copy.
Class: similar to other oo classes. However, the first function parameter of the class must be self. You do not need to add self to the call. Use _ init _ for initialization __. Use _ del _ to explicitly Delete objects __.
Class
Person
:
Def
_ Init __
(Self, name ):
Self. Name = Name
Def
Sayhi
(Self ):
Print
'Hello, my name is'
, Self. Name
P = person (
'Swaroop'
)
P. sayhi ()
Class inheritance: The subclass _ init _ does not automatically call the _ init _ of the base class __
Class male (person ):
Def _ init _ (self, name, salary ):
Person. _ init _ (self, name)
Self. Salary = salary
Input and Output
File
Open F = file('aaa.txt 'in Read mode ')
Read a row (including carriage return) line = f. Readline ()
Write mode to open F = file('aaa.txt ', 'w ')
Write F. Write ('abc ')
Close F. Close ()
Memory pickle, which can persistently store any Python object in the file.
Import cpickle as P
F = file ("A. dat", 'w ')
P. Dump (['A', 'B'], F)
F. Close ()
F = file ('A. dat ')
Alist = P. Load (f)
Exception Handling
Try:
Failed t eoferror:
Except t:
Custom exception type
Class
Shortinputexception
(Exception ):
'''A user-defined exception class .'''
Def
_ Init __
(Self, length, atleast ):
Exception. _ init _ (Self)
Self. Length = Length
Self. Atleast = atleast
Try
:
S =
Raw_input
(
'Enter something -->'
)
If
Len
(S) <
3
:
Raise shortinputexception (
Len
(S ),
3
)
# Other work can continue as usual here
Except
Eoferror:
Print
'\ Nwhy did you do an EOF on me? '
Except
Shortinputexception, X:
Print
'Shortinputexception: the input was of length % d ,\
Was expecting at least % d'
% (X. length, X. Atleast)
Else
:
Print
'No exception was raised .'
Lambda creates a function object
Def
Make_repeater
(N ):
Return Lambda
S: S * n
Twice = make_repeater (
2
)
Print
Twice (
'Word'
)
Print
Twice (
5
)
Output
$ Python lambda. py
Wordword
10
Python, learning notes