1. The print statement also supports redirecting input to a file, as an example:
1 logfile = open ('/tmp/mylog.txt'a') 2print'Fatal error:invalid input! ' 3 logfile.close ()
2. Use logical operators to concatenate arbitrary expressions together and get a Boolean value:
1 and 2 = = 42False3or 2 < 44True5not 6.2 <= 66true7 >>> 3 < 4 < 58 True
The last example is often illegal in other languages, but Python supports expressions that are both concise and
Beautiful. It is actually an abbreviation for the following expression:
and 4 < 5
3, Python does not support the C language of the self-increment 1 and the decrement 1 operator, because the + and-is also a single-mesh operator, Python will interpret--n as-(-N) to get N, the same ++n result is n. 4. Cyclic indexes and elements at the same time, enumerate (), Example:
1 for inch Enumerate (foo): 2 Print ' (%d) ' % i3 ... 4 A (0) 5 B (1)6 C (2)
5, List Resolution: You can use a for loop in a row to put all the values in a list
1>>> squared = [x * * 2 forXinchRange (4)]2>>> forIinchSquared:3...PrintI4 05164798 list parsing can even do more complex things, such as picking up the values that fit the requirements into the list:9>>> Sqdevens = [x * * 2 forXinchRange (8)if notX% 2]Ten>>> One>>> forIinchSqdevens: A...PrintI - 0 -4 the16 -36
6. What is a property
Properties are data-related items, which can be simple data values or executable objects, such as functions and methods. Which objects have properties? A lot. Classes, modules, files and complex numbers, and so on, have properties. How do I Access object properties? Use the period property to identify the method. This means adding a period between the object name and the property name: Object.attribute
7. Table 2.1 useful built-in functions for new Python programmers
Function |
Describe |
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 document string of an object in a neat and beautiful form, if no parameters are provided , the interactive help will be entered. |
int (obj) |
Convert an object to an integer |
Len (obj) |
Returns the length of an object |
Open (FN, mode) |
Open a file named fn in 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; Start The default value is 0, and the step default is 1. |
Raw_input (str) |
Waits for the user to enter a string that can provide an optional parameter str as a cue letter Interest. |
STR (obj) |
Convert an object to a string |
Type (obj) |
Returns the type of the object (the return value itself is a type Object!) ) |
"0" python core programming, chapter II