Home‎ > ‎

Rust Library Loudoun Codes Python Lecture

posted Sep 17, 2016, 10:51 AM by Brian P   [ updated Sep 18, 2016, 10:48 AM ]
Beginner Python Programing Book
http://amzn.to/2cHxhuy

Python Anywhere ( where you can play around with coding python right in your web browser )



Code Snippets

# open the dictionary on linux/mac and convert it to a list
>>> with open('/usr/dict/share/words') as fin:
...    dict_list = fin.read().split('\n')
>>> len(dict_list)
99134
>>>

# create a random set of characters

>>> import random

>>> L = list('abcdefghijklmnopqrstuvwxyz')

>>> L

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

>>> random.choice(L)

'h'

>>> buffer = ''.join([random.choice(L) for _ in xrange(20)])

'rffyptuurdosiyjrlled'



# find out how many english words are in your random buffer

>>> counter = 0

>>> for word in dict_list:

...    if word in buffer:

...        print word

...        counter += 1 # this is the same as saying counter = counter + 1

>>> print counter

3

>>>




Python