Python programming is a pleasant thing to do!!!
first, the characteristics of python:
It focuses on how to solve the problem rather than the syntax and structure of the programming Language.
When you write a program in the Python language, you don't have to think about how to manage the memory that your program Uses.
The Bottom-level details.
Portability
Supports object-oriented and process-oriented programming
Embeddable: you can embed C or C + + in Python code
A rich library
second, The Python code execution process:
Source code .py-> byte code. pyc-byte code executed in PVM (python virtual machine)
second, the Python IDE:
http://www.pydev.org/
third, the basic concept of python:
- Explanatory language: The source code is not translated directly into the machine language, but translated into intermediate code, and then interpreted by the interpreter to run the intermediate Code.
- Compiled language: before execution requires a specialized compilation process, The program is compiled into machine language files, the runtime does not need to re-translate, directly using the results of the Compilation.
- Number: There are 4 types of numbers in python: integers, long integers, floating-point numbers, complex numbers
- String: typically single, double, and triple quotes
- Back slash
- Natural String: A natural string is specified by prefixing the string with R or R. For example R "newlines is indicated
by \ n ".
- Unicode string:-you only need to prefix the string with u or u,
Remember to use Unicode strings when you are working with text files, especially if you know that this file contains useful
Text written in a non-english language.
- If you put two strings next to each other literally, they will be automatically connected by Python. For example, ' what\ '
' Your name? ' will be automatically converted to "what's your name?".
- There is no dedicated char data type in Python.
- remember, single and double quote strings are exactly the Same.
- Be sure to use a natural string to process the regular Expression. otherwise, you will need to use a lot of back Slashes. For example, a forward reference can
Written as ' \\1 ' or R ' \1 '.
What is the back reference backreference:\n slash plus number indicates the contents of the preceding Parenthesis. For example: \b (\d+) \b\s+\1\b This will match such as: 123 123 repeating number string
Metacharacters
Name
Matching objects
.
Point number (dot)
Single arbitrary character
\ n
Line break (newline)
Line break
\ r
Enter (return)
Enter
\ t
tab (tab)
Tabs
[...]
Character Group (Character Class)
Any of the listed characters
[^...]
Excluded character groups (negated Character Class)
Any characters that are not listed
\d
Number (digit)
Digital
\d
Non-digital
\w
Word (word)
Word characters (letters or numbers)
\w
Non-word characters
\s
White space character (whitespace)
White space characters
\s
Non-whitespace characters
^
De-char (caret)
Start position of line
$
Dollar symbol (dollar)
End position of the line
\<
Start position of the word
\>
The end position of the word
\b
Boundary (boundary)
Word boundaries
\b
Non-word boundary
(?=...)
Affirmative-order Surround (Positive Lookahead)
Success if the right can match
(?! ...)
Negative order surround (negative Lookahead)
Success if the right can't match
(? <= ...)
Affirmative reverse look (Positive Lookbehind)
Success if the left can match
(?<!...)
Negative reverse look (negative Lookbehind)
Success if the left does not match
?
The previous character may not appear, or it can appear only once
+
The characters before it can appear countless times, but at least once
*
The characters before it can appear countless times, or they may not appear
N
Its previous characters match exactly n times
{n,}
Its previous characters can appear countless times, but at least n times
{n,m}
The character before it must appear at least n times, at most times m
(...)
Parentheses (parenthese)
Grouping sub-expressions and recording the contents of the words it contains to match the expression
(?:...)
Grouping sub-expressions
\1-\9
Back to reference
|
Vertical bar (bar)
Match any expression on either side of the split
- Variables: variables are just a portion of your computer's memory that stores Information. You only need to assign a value to a variable when you use it. You do not need to declare or define a data type.
- Designator Command Rules:
The first character must be a letter in the alphabet (uppercase or Lowercase) or an underscore;
Other parts can be set by letter (uppercase or lowercase), underscore (' _ '), or number (0-9)
Into
Case sensitive;
- Data type: variables can handle different types of values, called data types.
- Object:
- The semicolon represents the end of a logical line/statement, as Follows:
To use more than one logical line in a physical line, you need to use a semicolon (;) to specifically indicate this
The Use Of.
i = 5
Print I
can be written
i = 5;
Print i;
can also be written
i = 5; Print i;
- Indentation: indentation determines the grouping of Statements. Do not use tabs and spaces to indent, as this does not work when crossing different Platforms. It is strongly recommended that you use a single tab at each indentation level.
- Methods: classes also have methods that define only the ground function for a class. You can use these features only when you have an object of that class. For example, Python provides the Append method for the list class, which lets you add an item at the end of the List. A class also has a field, which is a variable that is defined only for a class.
Iv. python format note:
Python is case-sensitive
The beginning of each line match either no spaces or tabs
V. Reasonable use of help information:
Help (str)-this will show the Str class
Help
Help you need to use often
Vi. python control Flow:
If/while/for/break/continue
#!/usr/bin/python
A=12
B=int (raw_input ("pls Input a Int:"))
If B==a:
Print "congratudations"
Elif B<a:
Print "your guess lower than it"
Else
Print "done"
#!/usr/bin/python
A=20
Flag=true
#这个地方需要注意首字母大写
While Flag:
Guess=int (raw_input ("pls Input a Int:"))
If Guess==a:
Print "good"
Flag=false
Elif Guess<a:
Print "should bigger"
Else
Print "should lower"
Print "done"
#!/usr/bin/python
# Filename:for.py
For I in range (1, 5):
Print I
Else
print ' The For loop was over '
The Else section is Optional. If an else is included, it is always executed once after the For loop ends.
#!/usr/bin/python
b= "abc"
While True:
A=raw_input ("pls Input:")
If a==b:
Break
Else
Print "the length is", len (a)
Print "done"
Vii. python module:
- Module Definition: A module is basically a file that contains all of the functions and variables that you define.
Standard library Module: SYS module
#!/usr/bin/python
# Filename:using_sys.py
Import Sys
print ' The command line arguments is: '
For I in Sys.argv:
Print I
print ' \n\nthe PYTHONPATH is ', sys.path, ' \ n '
- Bytes compiled files: these files are in. pyc as the extension
- From import statement:
- __name__ of the Module:
If we only want to run the main block when the program itself is being used, it does not run the main block when it is entered by another Module. Each Python module has its __name__, if it is ' __main__ ', which means that the module is run by the user Alone.
- To create your own module:
Remember that this module should be placed in the same directory as the program we entered it, or in the directory listed in Sys.path
One.
#!/usr/bin/env python
DEF say ():
If __name__== ' __main__ ':
print ' This program was being run by itself '
Else
print ' This module is being imported from another module '
Version= ' 0.1 '
#!/usr/lib/env python
Import Name #调用name模块
Name.say ()
print ' Version ', name.version
#!/usr/bin/python
# Filename:mymodule_demo2.py
From MyModule import say, version
# alternative:
Say ()
print ' version ', version
You can use the built-in Dir function to list the identifier for a module Definition. Identifiers have functions, classes, and Variables.
For example, the module name.py defined Above:
>>> Import Name
>>> dir (name)
[' __builtins__ ', ' __doc__ ', ' __file__ ', ' __name__ ', ' __package__ ', ' say ', ' version ']
Note that if the dir () parameter is empty:
>>> Import Name
>>> dir (name)
[' __builtins__ ', ' __doc__ ', ' __file__ ', ' __name__ ', ' __package__ ', ' say ', ' version ']
>>> dir ()
[' __builtins__ ', ' __doc__ ', ' __name__ ', ' __package__ ', ' name ']
#这里import模块名称是列表的一部分
eight, the Python standard library:
A set of modules consists of a standard library.
Ix. Python exception
X. Data structure of Python
Three built-in data structures: arrays, dictionaries, tuples
- List: separate each item with a COMMA. Items in the list should be included in square brackets [].
The list is a mutable data type.
Example: manually adding item to the list and deleting it:
#!/usr/bin/env python
def buy ():
mylist=[' potato ', ' chilli ', ' banana ', ' apple ']
print ' I has ', len (mylist), ' to buy\n '
print ' The Items are: '
For I in Mylist:
Print I,
Flag=true
While Flag:
Judge=raw_input (' You can input yes or no: ')
If judge== ' Yes ':
Goods=raw_input (' Maybe you need-buy more goods: ')
Mylist.append (goods)
Else
Flag=false
For I in Mylist:
Print I,
print ' My shop list was now: ', mylist
Mylist.sort ()
print ' The sorted shop list is ', mylist
Flag2=true
While Flag2:
Judge2=raw_input (' If you want to Delte?yes or no: ')
If judge2== ' Yes ':
print ' which itme want to delte ', mylist
print ' can choose the item from 0 to ', Len (mylist)-1
A=int (raw_input (' the item is: '))
Del mylist[a]
Else
Flag2=false
print ' The remained shop list is: ', mylist
Buy ()
A comma is used at the end of the print statement to eliminate the line break that is automatically printed for each print Statement.
Tuples and strings are immutable, i.e. you cannot modify Tuples. Tuples are defined by a comma-separated list of items in Parentheses.
An empty tuple consists of a pair of empty parentheses, such as Myempty = ().
You must follow the first (only) item followed by a comma so that python can differentiate between tuples and a Parenthesized object in an Expression.
The most common use of tuples is in print Statements.
Examples of using tuples:
!/usr/lib/env python
Def Zoo ():
zoo= (' Lion ', ' tiger ', ' rabbit ', ' dragon ', ' snake ')
print ' The number of the zoo is ', Len (zoo)
For I in range (len):
Print zoo[i],
zoo_new= (' dog ', ' Monkey ', zoo)
print ' The animal number is ', len (zoo_new)
print ' The Sencond animal is ', zoo_new[1]
print ' The animals brought from old zoo is: ', zoo_new[2]
print ' All animals: ', zoo_new
A= (123)
Print a
A= (123,)
Print a
Zoo ()
Input Result:
The number of the zoo is 5
Lion Tiger Rabbit Dragon Snake The animal number is 3
The Sencond animal is monkey
The animals brought from old zoo is: (' Lion ', ' tiger ', ' rabbit ', ' dragon ', ' snake ')
All animals: (' dog ', ' monkey ', (' lion ', ' tiger ', ' rabbit ', ' dragon ', ' snake ')
123
(123,)
Example 2:
Tuples are used for printing:
#!/usr/bin/python
# Filename:print_tuple.py
Age = 22
name = ' Swaroop '
Print '%s is%d years old '% (name, Age)
print ' Why are%s playing with that python? '% name
The dictionary is similar to the Address book where you find the address and contact details by contact name, i.e. we associate the key (name and value (details) together. Note that the key must be Unique.
Xi. python functions
- function definition: A function is a program block that can be Reused.
- Call Function: Use this name anywhere in your program to run this block of statements any number of Times.
Example:
#!/usr/bin/python
# Filename:function1.py
def SayHello ():
print ' Hello world! ' # block belonging to the function
SayHello () # Call the function
- Formal parameters and arguments for a function:
Parameters are specified internally within the parentheses of the function definition, separated by Commas.
- Local variables: declare variables within function definitions.
- Use the global statement: you can specify multiple global variables using the same global Statement. For example, global x, y, z.
#!/usr/bin/python
# Filename:func_global.py
def func ():
Global X
print ' x is ', x
x = 2
print ' Changed local x to ', X
x = 50
Func ()
print ' Value of x is ', x
- Default Parameters: Assign the assignment operator (=) and the default value after the parameter name of the function definition to specify the default parameter values for the Parameters. Only those arguments at the end of the formal parameter list can have default parameter Values.
#!/usr/bin/python
# Filename:func_default.py
Def say (message, times = 1):
Print message * times
Say (' Hello ')
Say (' world ', 5)
- Key Parameters: Assign values to these parameters by naming Them.
- Return statement:
#!/usr/bin/python
# Filename:func_return.py
def Maximum (x, y):
If x > Y:
return x
Else
Return y
Print Maximum (2, 3)
- Docstrings document String: Docstrings is an important tool because it helps to make your program documentation easier to Understand.
#!/usr/bin/python
# Filename:func_doc.py
def Printmax (x, y):
"' Prints The maximum of the Numbers.
The values must be Integers. "
x = int (x) # Convert to integers, if possible
y = int (y)
If x > Y:
Print x, ' is maximum '
Else
Print y, ' is maximum '
Printmax (3, 5)
Print printmax.__doc__
The string of the first logical line of the function is the document string for this function
The Convention for a document string is a multiline string whose first line begins with an uppercase letter and ends with a period. The second line is a blank line,
Starting with the third line is a detailed description.
。 Keep in mind that Python takes everything as an object, including this Function. This sentence is very important, through the above example from the following line of code can be withdrawal.
12. Python Variables
13. Python Operators and Expressions:
650) this.width=650; "title=" clip_image001 "style=" border-top:0px; border-right:0px; background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" clip_image001 "src=" http://s3.51cto.com/wyfs02/M00/8A/9D/wKiom1g1NtyxxI_ Haafczoaj09i254.png "" 664 "height=" "the"/>
The plus sign can do arithmetic operations, or you can concatenate 2 strings.
* * Power
650) this.width=650; "title=" clip_image002 "style=" border-top:0px; border-right:0px; background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" clip_image002 "src=" http://s3.51cto.com/wyfs02/M01/8A/9D/ Wkiom1g1nt6i3urtaahe1uhfxlg510.png "" 556 "height=" 317 "/>
650) this.width=650; "title=" clip_image003 "style=" border-top:0px; border-right:0px; background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" clip_image003 "src=" http://s3.51cto.com/wyfs02/M01/8A/99/wKioL1g1Nt_ Igqifaagm2j3evaw220.png "" 565 "height=" 289 "/>
I recommend that you use parentheses to group operators and operands so that you can clearly indicate the order of Operations.
650) this.width=650; "title=" clip_image004 "style=" border-top:0px; border-right:0px; background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" clip_image004 "src=" http://s3.51cto.com/wyfs02/M02/8A/9D/ Wkiom1g1nudcd2keaacsavf37cm869.png "" 302 "height=" 373 "/>
650) this.width=650; "title=" clip_image005 "style=" border-top:0px; border-right:0px; background-image:none; border-bottom:0px; padding-top:0px; padding-left:0px; border-left:0px; padding-right:0px "border=" 0 "alt=" clip_image005 "src=" http://s3.51cto.com/wyfs02/M02/8A/99/ Wkiol1g1nugasbc2aaeiylt-5z8373.png "" 317 "height=" 430 "/>
Reading notes for a concise Python tutorial