
上QQ阅读APP看书,第一时间看更新
Conventions
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The functions of the random module are bound methods of a hidden instance of the random.Random
class."
A block of code is set as follows:
import random print random.random() print random.uniform(1,9) print random.randrange(20) print random.randrange(0, 99, 3) print random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZ') # Output 'P' items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] random.shuffle(items) print items print random.sample([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 5) weighted_choices = [('Three', 3), ('Two', 2), ('One', 1), ('Four', 4)] population = [val for val, cnt in weighted_choices for i in range(cnt)] print random.choice(population)