Here is a collection of all the statements and functions in Python that this book covers, along with some examples of their use. The examples all take place in the interactive shell, so you can experiment with them yourself by following the examples in the interactive shell.
If you would like to learn more functions and methods, the official Python documentation covers every function that comes with Python. It can be found at http://docs.python.org/
Doug Hellmann also has a blog called "Python Module of the Week" which covers the various modules that come with Python in good detail with plenty of examples. His blog can be found at http://www.doughellmann.com/PyMOTW/
The assignment statement is used to store a value inside a variable. The statement is a variable name, followed by the = operator, followed by a value or expression. The expression is evaluated to the value to be stored in the variable.
break statements will immediately make the execution jump out of the innermost loop that it is placed in. Notice in the following example that the print() function call is never executed because the break statement moves execution out of the while loop.
The break statement only breaks out of the innermost loop. Notice that the print('Goodbye!') call is never executed, and that even though print('Hello!') is in a for loop that should iterate three times, it is only called once.
continue statements will immediately make the execution jump to the start of the innermost loop that it is placed in. Notice in the following example that the print('Hello!') call is executed by print('Goodbye!') is never executed because the continue statement moves execution back to the start of the loop for the next iteration.
continue statements only work on the innermost loop. Notice in the following example that the print('Goodbye!') call is skipped by the continue, but the continue does not skip the print('End of outer #%s' % (i)) call.
def statements are used to define a new function. The def statement is the def keyword, followed by the function name, the parentheses which contain any parameters, a colon, and a block that contains the function's code. A function cannot be called until after it has been defined.
del statements will delete, or undefine, a variable. This is useful for removing items in lists and dicts. The del statement is the del keyword followed by the variable or item to be deleted.
for loops set a variable to each value in a list or each key in a dict. A for loop can be exited early if it encounters a break statement. A continue statment will cause execution to immediately move to the next iteration at the start of the loop.
import statements allow you to use functions and variables located inside other Python scripts (called modules) in your program. There are many modules that come with Python already (such as time, sys, or random). The import statement is the import keyword, followed by the module name. To use a function in a module, put the module name and a period before the function call.
The if, elif, else statements can decide whether or not to execute a block of code. The if and elif statements start with the if or elif keyword, followed by a condition (that is, an expression that evaluates to a boolean value True or False), followed by a colon and a block of code.
If the condition that follows the if statement is True, then the block of code that comes after the if statement is executed. If the condition that follows the if statement is False and the elif's condition is True, then the block that follows the elif statement is executed. If all the if and elif statements are False, then the block that follows the else statement is executed.
Only one of the blocks that follows an if, elif, or else statement will be executed.
The if statement is always first. An if statement does not need to have an elif or else statement after it. If the if statement has both an elif and else statement, the else statement must come after all the elif statements. There can be any number of elif statements.
return statements immediately send execution out of a function and back to the line where the function was called. They can be followed by a value or expression to be used as the function's return value. return statements can only be used inside of functions (that is, inside a def statement's block).
If the return keyword is by itself, then the function returns the None value.
while loops will execute the block of code over and over until their condition (that is, an expression that evaluates to a boolean True or False value) is False. The condition is checked at the beginning of each iteration.
A while loop can be exited early if it encounters a break statement. A continue statement will cause execution to go back to the start of the while loop and recheck the while loop's condition.
The abs(n) function returns the absolute value of n. The absolute value of a number is the positive form of the number.
The bool(x) function returns the boolean value of x. If x is an empty list, string, dict, or the numeric values 0 or 0.0, the bool(x) evaluates to False. Otherwise, bool(x) will return True.
The chr(n) function takes an integer ASCII value n and returns a string with that ASCII value's character. A table of all ASCII values can be found in Chapter 13, The Caesar Cipher.
The float(x) function takes an integer or string x and returns the floating point version of x.
The input() function pauses the execution of the program and lets the user type in a string from the keyboard. When the user presses Enter, the keystrokes (except for the Enter) are returned as a string.
The int(x) function takes a floating point number or string x and returns the integer version of x. If x is a floating point number, then the integer returned is x rounded down.
The list(x) function returns the list form of x. Usually, the "range object" returned by the range() function is passed to list() in order to get a list object of the range object's values.
The ord(c) function takes a single character string c and returns the integer ASCII value of c. A table with all the ASCII values can be found in Chapter 14, The Caesar Cipher.
The print(x) function takes a value x and displays it on the screen with a newline automatically added to the end. The print() function can take any number of multiple arguments and will display them with a single space placed in between the arguments. A print() function call with no arguments will display just a newline on the screen.
The print() function has two optional keyword arguments. The end keyword argument can change the newline automatically added to the end of the string to any other string. The sep keyword argument can change the single space autoamtically inserted in between multiple arguments to any other string.
The range(n) function returns a range object with the values 0 to n-1. This range object can be used in for loops to iterate over the integer values 0 to n-1. If you want an actual list value with these integer values, pass the range object to the list() function.
The range(start, stop) form can provide a different start value other than 0. The range(start, stop, step) form can provide a different start value than 0 and also increment a different value than 1.
The round(f) function will round a floating point number argument either up or down (depending on which is closer), and return the result as an integer value.
The str(x) function will take a value (commonly an integer or float) and return the string form of x.
The type(x) function takes a value x of any data type and returns what data type the value is. The return value itself is of a data type called type.
The random.choice(li) function returns a random item from the list li. It is equivalent to li[random.randint(0, len(li)-1)].
The random.randint(a, b) function returns a random integer between (and including) the arguments a and b.
The random.shuffle(li) function will shuffle the order the items in the list li. Note that this function does not return anything: it shuffles the items in the list in place.
The sys.exit() function causes your Python program to terminate and stop executing instructions.
The time.sleep(n) function causes the Python program to pause execution for n seconds. The argument n can either be an integer or a floating point value.
The keys() dict method returns a list where each item is a key in the dictionary. Because dictionaries are not sorted, the items in the returned list will be in an uncertain order.
The values() dict method returns a list where each item is a value in the dictionary. Because dictionaries are not sorted, the items in the returned list will be in an uncertain order.
The append(x) list method adds the value x to the end of the items in the list.
The reverse() list method reverse the order of the items in the list in place. Note that because the items are reversed in place, this method does not return anything.
The sort() list method puts all the items in the list in sorted order. This is done in place, so the sort() method does not return anything. The sort is alphanumeric, which means that numbers come before letters.
The endswith(s) string method returns True if the string ends with the string s, otherwise it returns False.
The isalpha() string method returns True if the string is not blank and only contains letters. If the string has numbers, spaces, or any other non-letter characters, the method returns False.
The isdigit() string method returns True if the string is not blank and only contains numbers. If the string has letters, spaces, or any other non-number characters, the method returns False. Note that periods will also make the method return False.
The islower() string method will return True if the all the letters in it are lowercase and the string has at least one lowercase letter.
The isupper() string method will return True if the all the letters in it are uppercase and the string has at least one uppercase letter.
The join(li) string method returns a string that contains all of the string items in the list li, with the string in between each of the items. This is most commonly used on a single space string to combine several word strings in a list together into one string.
The lower() string method returns a lowercase string version of the string.
The split() string method returns a list of strings, where each string in the list is from splitting the string by whitespace. Whitespace characters include spaces, tabs, and newlines. The whitespace is not included in the strings in the returned list.
The split(s) version can specify a string s to split the string on.
The startswith() string method returns True if the string ends with the string s, otherwise it returns False.
The upper() string method returns an uppercase string version of the string.