2. Python Start 2.1 Print
1 Print " Hello world! "
2.2
_: The value of the last expression
>>: Output redirection
%: Prints the formatted string and values for the style, and the value after% is a tuple object, with "()" containing the
Print "%s is a number%d."% ("Python", 6)ImportSYSPrint>> Sys.stderr,"msg"file= Open ("filename","a")Print>> file,"File MSG."file.close () Hello= Raw_input ("Hello:")Print "Num:%d."% (int (hello))
2.3
#: Comment Symbol
2.4 operator
+-*///% * *
< <= > >= = = = = <>
And Or not
* *: exponentiation
2.5 Variables and Assignments
2.6 Numbers
int long bool Float complex
Decimal
Import Decimal
Decimal. Decimal ("1.1")
1.1-1.1000001 (usually counted as such)
2.7 string
Support for [],[:] Operations
2.8 Lists and tuples
Index starting from 0, support slicing operation ': '
List: [] can change values
Tuple: () cannot change value
2.9 Dictionaries
{' key ': ' Value '}
2.10 Code blocks aligned with indentation
2.11 If
if condition: something
2.12 while
while condition: something
2.13 for
for iteration: something
Range (): extent
Enumerate (): Enumeration
2.14
for inch if not x% 2]
2.15 Open () file ()
Fobj = open ("Filename""R") for in fobj: print Eachline, # ', ' causes print to not print line breaks fobj.close ()
2.16 Exceptions
Try : somethingexcept IOError, E: except
2.17 Functions
def function_name (): something
Class 2.18
class ClassName (baseclassname): something
2.19 Modules
Import Module_name
2.20 Utility Functions
Dir ([obj]) help ([obj]) int () Len () Open () range () raw_input () str () type ()
[Learn] Python Core Programming Learning Notes (ii)