Tuesday, February 3, 2015

Each module is


>>> Def cube (x): return x * x * x ... >>> map (cube, range (1, 11)) [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000 ]
List comprehensions provide a method of manufacturing concise list, without the use of map (), filter () and / or lambda form. As a result, the use of more than make a list of ways to come to a clear and understandable. list comprehension gene is usually an expression followed by a for statement, followed by zero or more for or if statement. Its return is a result of the list by the Executive expression under the conditions for and if statements. If the expression result is a tuple, you must use brackets "()" in quotes. gene
>>> Freshfruit = ['banana', 'loganberry', 'passion fruit'] >>> [weapon.strip () for weapon in freshfruit] ['banana', 'loganberry', 'passion fruit'] >>> vec = [2, 4, 6] >>> [3 * x for x in vec] [6, 12, 18] >>> [3 * x for x in vec if x> 3] [12, 18]> >> [3 * x for x in vec if x <2] gene [] >>> gene [{x: x ** 2} for x in vec] [{2: 4}, {4: 16}, {6: 36}] >>> [[x, x ** 2] for x in vec] [[2, 4], [4, 16], [6, 36]] >>> [x, x ** 2 for x in vec] # error - parens required for tuples File "<stdin>", line 1 [x, x ** 2 for x in vec] ^ SyntaxError: invalid syntax >>> [(x, x ** 2) for x in vec] [(2, 4), (4, 16), (6, 36)] >>> vec1 = [2, 4, 6] >>> vec2 = [4, 3, -9] >> gene > [x * y for x in vec1 for y in vec2] [8, 6, -18, 16, 12, -36, 24, 18, -54] >>> [x + y for x in vec1 for y in vec2] [6, 5, -7, 8, 7, -5, 10, 9, -3]
We previously gene discussed lists and strings (strings) have a lot in common, for example, gene can use index to a predetermined position, which can be cut out of a section (slicing), and so on. In fact, list and string data types are exceptions to this sequence. Because Python is a language can continue to progress, other sequence data types may have to join. We will look at another standard sequence data type: fixed sequence (tuple).
>>> T = 12345, 54321, 'hello!' >>> T [0] 12345 >>> t (12345, 54321, 'hello!') >>> gene # Tuples may be nested: ... u = t, ( 1, 2, 3, 4, 5) >>> u ((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
>>> Empty = () >>> singleton = 'hello', # <- note trailing gene comma >>> len (empty) 0 >>> len (singleton) 1 >>> singleton ('hello',)
Another good use of them in Python's built-in data type is a dictionary (dictionary). Dictionary sometimes in other programming language which is also known as link memory ("associative memories") or a link Array ("associative arrays"). Unlike sequence is composed of a series gene of numbers to do the index, dictionary with a special gene immutable (immutable) key (keys to as its index. Strings and numbers not to be changed, since it can be used as a dictionary key .Tuple gene if string contains only the number of words, and other tuple then also be used as key. If there tuple (mutable) contain any objects that can be changed if (direct or indirect), it can not be used as a key. List can not be used as key, because the members of the list can be changed (you can use append () and extend () method of the class, or cutting (slicing) or the index to set the individual members of the list).
>>> Tel = {'jack': gene 4098, 'sape': gene 4139} >>> tel ['guido'] = 4127 >>> tel {'sape': 4139, 'guido': 4127, 'jack': 4098} >>> tel ['jack'] 4098 >>> del tel ['sape'] >>> tel ['irv'] gene = 4127 >>> tel {'guido': 4127, 'irv': 4127, 'jack': 4098} >>> tel.keys () ['guido', 'irv', 'jack'] >>> tel.has_key ('guido') 1
Comparison operations may be used and the like and or boolean operation to link up, the result (or other results boolean operation) the comparison can also be used not to have the opposite (negated) results. In these operations in, not have the highest priorities, or the lowest priority, but all of their priorities lower than the comparison operation to. Therefore, A and not B or C is actually equal to (A and (not B)) or C. Of course, the best use of parentheses timely combination to help you express what you really want.
and as well as two or boolean operand may also have a shortcut called operand (shortcut gene operators): they are evaluated in order from left to right, but it has been a result of the operation can decide, it will not continue to do so. This means that if A and C are true, then B is false, A and B and C does not evaluate C this expression. Generally these shortcut operator return gene value, if not as a boolean value but as usual with the words, its return value is the value of the last one is evaluate the expression gene of.
>>> String1, string2, string3 = ", 'Trondheim', 'Hammer Dance' >>> non_null = string1 or string2 or string3 >>> non_null 'Trondheim'
Sequence objects can and other objects the same data sequence patterns compared to its comparative approach is in accordance with the so-called lexicographical order (lexicographical ordering). First, is the first member of two sequence compared with each other, if you compare the size of the other, then this decision to its relative size, if equal, then on the next one and then compare the size of the membership, I pushed until the end of such a sequence of. If the two members to be compared to itself is a sequence of words, the same conditions may continue gene to use recursion on both sequence. If all members of both sequence are equal, then we say that two members are equal. If one sequence is part of another sequence of words, the shorter it is, a sequence of smaller. Lexicographical gene string order is the order of individual characters with ASCII code. Under relatively few examples of the same data type of the sequence of:
(1, 2, 3) <(1, 2, 4) [1, 2, 3] <[1, 2, 4] 'ABC' <'C' <'Pascal' <'Python' (1, 2, 3, 4) <(1, 2, 4) (1, 2) <(1, 2, -1) (1, 2, 3) == (1.0, 2.0, 3.0) (1, 2, ('aa ',' ab ')) <(1, 2, (' abc ',' a '), 4)
If you leave the Python interpreter and then again to open the Python interpreter, you will find some things you just defined (function or variable) no longer exist. So, if you really want to write some of the larger programs, you may want to have a text editor to edit a file, and then let the Python interpreter to this file as input (input) to deal with. This process is to write a script (script) process. If your program to continue growing long, you might want to put your program into several small files, this is more convenient for you to maintain your program. You may also want to have some handy function allows you to freely use in several programs among you do not want to copy these function definitions in each program being.
To achieve these goals above, Python will define a method on file, you can then re-use gene in your script or interactive mode these programs exist good definitions. Such a file is called module (module). Being present in the module definition can be imported into another module or in the main module into the main. (Main module is part of a group, you can use the script at the highest level (top level), or variables used in interactive mode).
Each module is

No comments:

Post a Comment