As we have already seen, Python has a number of built-in functions and operators: for example, del, print, len(), and type(). Tables 1.7-1.9 show some others that are useful, some of which we will discuss here.
In IDLE, or when using the Python interpreter directly, we can use the help() function to get information about an object, or to enter Python's interactive help system. For example:
Table 1.6 Selected Set Methods and Functions
Syntax x in s x not in s len(s) s.clear() s.copy() s.add(x) s.remove(x)
s.issubset(t) s <= t s.issuperset(t) s >= t s.union(t)
s | t s.intersection(t) s & t s.difference(t) s - t
Returns True if item x is in set s
Returns True if item x is not in set s
Returns the number of items in set s
Removes all the items from set s
Returns a shallow copy of set s
Adds item x to set s if it is not already in s
Removes item x from set s, or raises a KeyError exception if x is not in s
Removes item x from set s if it is in s Returns True if set s is a subset of set t
Returns True if set s is a superset of set t
Returns a new set that has all the items from set s and from set t
Returns a new set that has each item that is both in set s and in set t
Returns a new set that has every item that is in set s that is not in set t
Syntax Description all(q) Returns True if all items in q are True; q is an iterable—for jpyhpn example, a sequence such as a string or a list any(q) Returns True if any item in q is True x in q Returns True if item x is in sequence q; also works for dictionaries x not in q Returns True if item x is not in sequence q; also works for dictionaries len(q) Returns the number of items in sequence q; also works for dic tionaries max(q) Returns the maximum item of sequence q min(q) Returns the minimum item of sequence q sum(q) Returns the sum of the items in sequence q
Table 1.8 Some Useful Built-ins
isinstance(x, C) type(x)
Returns a one-character str whose ASCII value is given by int i
Returns a one-character unicode string whose Unicode code point is given by int i
Returns the int that is the byte value (0-255) if c is a one-character str string, or the int for the Unicode code point if c is a one-character unicode string
Returns a list of most of object x's attributes, including all its method names
In IDLE, prints a brief description of object x's type and a list of its attributes including all its methods
Returns True if the object x has the attribute called a
Returns the unique ID of the object that object reference x refers to
Returns True if x is an instance of class C or a subclass of class C
Returns the type of x; isinstance() is preferred since it accounts for inheritance; type() is most often used for debugging
Returns the result of evaluating the string s which can contain an arbitrary Python expression
Opens the file named in string f using mode m, and returns the file handle; covered in Chapter 6
Returns a list of int i ints numbered from 0 to i - 1; additional arguments specify start, end, and step values
This will display all the str class's methods with a brief explanation of each. Quite a lot of information is provided, so we often have to scroll up using the PageUp key or using the scrollbar.
With no arguments the help() function takes us into the interactive help system. Type quit to return to normal IDLE interaction.
Once we are familiar with Python's classes and we need just a quick reminder, we can use dir() to get a bare list of a class's methods, for example:
Table 1.9 Selected Math-Related Built-ins Syntax Description abs(n) Returns the absolute value of number n divmod(i, j) Returns a tuple containing the quotient and remainder that result from dividing i by j hex(i) Returns a hexadecimal string representing number i oct(i) Returns an octal string representing number i float(x) Returns x converted to a float; x may be a string or a num ber int(x) Returns x converted to an int; x may be a string or a num ber long(x) Returns x converted to a long; x may be a string or a num ber pow(x, y) Returns x raised to the power y; can accept a third modulo argument—the two-argument form is the same as using operator **
round(x, n) Returns float value x rounded to n digits after the decimal place
The range() function is covered in Chapter 2 when we look at looping, and the open() function is covered in Chapter 6 when we look at reading and writing files. The hasattr() and isinstance() functions are covered in Chapter 3.
For the sequence-related functions, max() and min() work on sequences that contain strings as well as those that contain numbers, but may give suprising results:
>>> x = "Zebras don't sail" >>>max(x), min(x) ('t', ' ')
The ordering is based on the byte values for str strings and on code points for unicode strings. For example, ord("Z") is 90, whereas ord("t") is 116.
Some of Python's built-in mathematical functions are shown in Table 1.9. Python is also supplied with a mathematics library that has all the standard functions we would expect. We can discover what they are by importing the math module, and using dir():
['_doc_', '_file_', '_name_', 'acos', 'asin', 'atan', 'atan2',
'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log', 'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh']
The first three items are special methods (indicated by leading and trailing double underscores); we will learn more about special methods in Chapter 3. All the rest are functions, except for math.e and math.pi, which are constants. We can find out what type an item is interactively. For example:
(<type 'float'>, <type 'builtin_function_or_method'>)
(3.1415926535897931, <built-in function sin>) >>> math.sin(math.pi) 1.2246063538223773e-16*
At first it is quite useful to explore what Python offers in this interactive way, but reading the documentation, particularly skimming the "Library Reference", will provide a broad overview of what Python's standard libraries have to offer.
Was this article helpful?
The main focus of this report is to show how to get involved in video marketing on the run, how to rank quickly on YouTube and Google using FREE semi-automatic tools and services. QUICKLY AND FREE. I will show methods and techniques I use to rank my videos, as well as free resources and tools to make video clips, to get backlinks and free traffic.