Python contains a considerable number of built-in functions, which may be used without importing any external modules.
Function | Meaning |
---|---|
chr(i) | string of char whose ASCII code is i |
float(x) | float equivalent to string or int x |
int(x) | int equivalent to number or string x |
list(sequence) | list equivalent to a sequence |
long(x) | long int equivalent to x |
ord(s) | ASCII code of one-char string s |
str(x) | string equivalent of object x |
Function | Meaning |
---|---|
abs(x) | absolute value of number x |
max(x) | largest member of string, tuple, or list x |
min(x) | smallest member of string, tuple, or list x |
round(x, n=0) | float x rounded n places after decimal point. Zero is default for n. |
Python file functions are member functions of the file class. A file object is first created by using the open( ) call.
Function | Meaning |
---|---|
fo = open(fn_string, mode) | Creates file object fo. fn_string names the physical file. mode is "r" for reading, "w" for writing. |
fo.read(bytes) | Returns bytes characters from fo. |
fo.readline( ) | Returns next line from fn, including newline at end. |
fo.readlines( ) | Returns all remaining lines from fo, as a list of strings. |
fo.write(str) | Writes string str into file fo. |
fo.writelines(list) | Writes all strings in list into file fo, without adding newlines. |
fo.close( ) | Closes fo. |
Function | Meaning |
---|---|
execfile(file) | Execute commands from a file. |
input(prompt_str) | Prints prompt_str, then returns numeric input from keyboard. Multiple entries cause input( ) to return a structure, such as a tuple. |
raw_input(prompt_str) | Prints prompt_str, then returns string input from keyboard. |
len(x) | Returns number of elements of structure x. |
map(funct, seq) | Apply funct to all members of seq; return list of results. |
type(x) | Return type of object x. |
str(x) | string equivalent of object x |
As in most other programming languages, in Python you can define your own functions. The syntax is:
def <function_name>(<parameter_list>):
<statements>
[return <value>]
One thing to be aware of with Python functions is that the function does not modify the original copy of its parameters. When the parameters are references to objects, the referred-to objects may be modified, but simple variables such as integers cannot. One way around this is to return a structure as the value of the function, as in the next example.
This is a program containing a function to sort three numerical parameters into ascending order. Note that, in Python, names must be created before they can be used. Thus, the function must be defined earlier in the program than the place where it is called.
#!/usr/bin/env python #Read three numbers and print them in ascending order. def sort(a, b, c): #This function sorts its parameters so that a <= b <= c. if a > b: temp = a a = b b = temp if b > c: temp = b b = c c = temp if a > b: temp = a a = b b = temp #Since the originals of a, b, c, have not been modified, #we must return the sorted versions as a tuple. return a, b, c #Main program starts here. num_A = input("Enter first number: ") num_B = input("Enter second number: ") num_C = input("Enter third number: ") num_A, num_B, num_C = sort(num_A, num_B, num_C) print "The sorted numbers are %d, %d, and %d." % (num_A, num_B, num_C)
Python has a built-in help() function that provides syntactic and semantic help with functions, classes, and packages. Launch Python from the command line, and type help(<function-or-class-name>) and Python will print the help. (Type <control>-D to quit the Python interpreter.)
For example, here is the help obtained from the Python command, help(chr):
Help on built-in function chr in module __builtin__: chr(...) chr(i) -> character Return a string of one character with ordinal i; 0 <= i < 256.
For classes or packages, the help is much more extensive, indicating all of the member data and functions of the class or package.