- 2.1 Program Output, the print Statement, and Hello World!
- 2.2 Program Input and the raw_input()Built-in Function
- 2.3 Comments
- 2.4 Operators
- 2.5 Variables and Assignment
- 2.6 Numbers
- 2.7 Strings
- 2.8 Lists and Tuples
- 2.9 Dictionaries
- 2.10 Code Blocks Use Indentation
- 2.11 if Statement
- 2.12 while Loop
- 2.13 for Loop and the range() Built-in Function
- 2.14 List Comprehensions
- 2.15 Files and the open() and file() Built-in Functions
- 2.16 Errors and Exceptions
- 2.17 Functions
- 2.18 Classes
- 2.19 Modules
- 2.20 Useful Functions
- 2.21 Exercises
2.20 Useful Functions
In this chapter, we have seen some useful built-in functions. We summarize them in Table 2.1 and present a few other useful ones (note that these may not be the full syntax, only what we feel would be useful for you now).
Table 2.1. Useful Built-In Functions for New Python Programmers
Function |
Description |
dir([obj]) |
Display attributes of object or the names of global variables if no parameter given |
help([obj]) |
Display object’s documentation string in a pretty-printed format or enters interactive help if no parameter given |
int(obj) |
Convert object to an integer |
len(obj) |
Return length of object |
open(fn, mode) |
Open file fn with mode (’r’ = read, ‘w’ = write) |
range([[start, ]stop[,step]) |
Return a list of integers that begin at start up to but not including stop in increments of step; start defaults to 0, and step defaults to 1 |
raw_input(str) |
Wait for text input from the user, optional prompt string can be provided |
str(obj) |
Convert object to a string |
type(obj) |
Return type of object (a type object itself!) |