Monday, December 17, 2012

PYTHON 3 RANDOM MODULE

PYTHON 3 RANDOM GENERATORS



REVISED: Sunday, March 3, 2013




Python 3 Random Module.

I.  PYTHON 3 RANDOM MODULE INTRODUCTION

For non state sharing generators instantiate multiple random instances.

random.randint(a, b)

Returns a random integer N such that a <= N <= b.

random.choice(seq)

Returns a random element from the non-empty sequence seqIndexError is raised if seq is empty.

random.shuffle(x)


Shuffle the sequence x = [1, 2, 3...] in place.

II.  PYTHON 3 RANDOM MODULE EXAMPLE SOURCE CODE

from random import shuffle, choice, randint 

def main():

    # Example 1
    print('EXAMPLE 1:\n','The Python code randint(-10,10) provides a random integer between 10 and -10; e.g.,',randint(-10,10),'.\n')

    # Example 2
    pet = ('dog', 'cat', 'turtle')
    print('EXAMPLE 2:\n','Given the choice of pets is (\'dog\', \'cat\', \'turtle\') choice(s) randomly picks a pet; for example,',choice(pet),'.\n')

    # Example 3
    num = randint(1,10)
    print('EXAMPLE 3:\n','Given a random number between one and ten; e.g.,',num,'.')
    guess = int(input('Enter a number from one and ten and left mouse click OK:'))
    print('You try to determine the random number and pick',guess,'.')
    if num == guess:
        print('Your number was right!\n')
    else:
        print('Your number was wrong!\n')

    # Example 4
    print('EXAMPLE 4:\n','The total number of the two dice you roll is',randint(1,6) + randint(1,6),'.\n')

    # Example 5
    print('EXAMPLE 5:\n','You walk into a cave looking for gold.')
    if randint(1,2) == 1:
        print('You locate gold!','\n')
    else:
        print('You have fun but do not locate any gold.','\n')

     # Example 6
    cards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ,13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52]
    print('EXAMPLE 6:\n','A deck of cards comes out of the box in the order:',cards,'\n')
    shuffle(cards)
    print('After shuffling the order of the deck of cards is:',cards,'\n')

if __name__ == '__main__':
    main()

III.  PYTHON 3 RANDOM MODULE EXAMPLE OUTPUT

>>> 
EXAMPLE 1:
 The Python code randint(-10,10) produces a random integer between -10 and 10; e.g., -7 .

EXAMPLE 2:
 Given the choice of pets is ('dog', 'cat', 'turtle') choice(pet) randomly selects one of the pets; e.g., cat .

EXAMPLE 3:
 Given a random number between one and ten; e.g., 8.
You try to determine the random number and pick 6.
Your guess was wrong!

EXAMPLE 4:
 The total number of the two dice you roll is 6 .

EXAMPLE 5:
 You walk into a cave looking for gold.
You find gold! 

EXAMPLE 6:
 A deck of cards comes out of the box in the order: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52]

After shuffling the deck of cards is in the following order: [29, 25, 42, 44, 50, 6, 11, 14, 12, 30, 2, 39, 15, 49, 20, 24, 51, 1, 28, 45, 52, 9, 38, 27, 8, 41, 33, 40, 16, 26, 10, 36, 23, 21, 7, 22, 37, 19, 3, 46, 13, 31, 32, 5, 34, 17, 43, 48, 4, 35, 18, 47]
>>> 

IV.  REFERENCES

The Python Tutorial by Guido van Rossum and Fred L. Drake, Jr., editor (2003)

Style Guide for Python Code by Guido van Rossum and Barry Warsaw (2001)

Enjoy the Python 3 Random Module!


Elcric Otto Circle






-->




-->




-->

















How to Link to My Home Page

It will appear on your website as:

"Link to: ELCRIC OTTO CIRCLE's Home Page"



Sunday, December 16, 2012

PYTHON 3 PICKLE

PYTHON 3 PICKLE



REVISED: Sunday, March 3, 2013




Python 3 "Pickle".

I.  PYTHON 3 "PICKLE" INTRODUCTION

In Python to “pickle”, refers to a type of file input output (I/O) where you save a Python object to a file. Pickling stores objects on your hard drive instead of in your random access memory (RAM), short term memory, where the objects will be lost when you log out.

You can only pickle one object into one file.

First, you have to import the Python "pickle module", e.g., import pickle. We need the Python “pickle module” to get the "pickle dump" function to dump the object we want to pickle into the file; e.g., pickle.dump(). Later we can use the Python "pickle module" "pickle load" function to un-pickle the object by pulling the object from the file; e.g., pickle.load().

Second, you have to create a file output “file object” using the Python “open method”; e.g., outFile = open(‘pickleExample.txt’, ‘wb’), which we open in binary mode; e.g., ‘wb’ for write binary. I am using a text file named pickleExample; however, you can use any file name you prefer. I used outFile for the "file object"; however, any "file object" name can be used.

Third, you have to create a file input “file object” using the Python “open method”; e.g., inFile = open(‘pickleExample.txt’, ‘rb’), which we open in binary mode; e.g., ‘rb’ for read binary. I am still using the original text file named pickleExample; you must use your original file name because that is the one you wrote to. I used inFile for the "file object"; however, any "file object" name can be used.

II.  PYTHON 3 PICKLE EXAMPLE SOURCE CODE

import pickle

def main():

    # Creating objects we want to pickle, write, to a file.
    pickleObjects = ["home", "library", "garage", "garden", "pool", "car"]
    print (pickleObjects)

    # Pickling, writing, to a file.
    outFile = open('pickleExample.txt', 'wb')
    pickle.dump(pickleObjects, outFile)
    outFile.close()

    # Un-pickling, reading, from a file.
    inFile = open('pickleExample.txt', 'rb')
    unPickledObjects = pickle.load(inFile)
    print (unPickledObjects)
    inFile.close()

if __name__ == '__main__':
    main()

III.  PYTHON 3 PICKLE EXAMPLE OUTPUT

>>> 
['home', 'library', 'garage', 'garden', 'pool', 'car']
['home', 'library', 'garage', 'garden', 'pool', 'car']
>>> 

IV.  REFERENCES

The Python Tutorial by Guido van Rossum and Fred L. Drake, Jr., editor (2003)

Style Guide for Python Code by Guido van Rossum and Barry Warsaw (2001)

Enjoy Python 3 "Pickle".


Elcric Otto Circle






-->




-->




-->

















How to Link to My Home Page

It will appear on your website as:

"Link to: ELCRIC OTTO CIRCLE's Home Page"



Tuesday, December 4, 2012

PYTHON 3 SET THEORY AND LOGIC

PYTHON 3 SET THEORY AND LOIGIC



REVISED: Sunday, March 3, 2013




This tutorial discusses Python 3 "Set Theory and Logic".

I.  PYTHON 3 "SET THEORY AND LOGIC" INTRODUCTION

A set is an unordered collection of "distinct" objects; which means a set does not contain duplicate objects. Curly brackets { } are used when detailing the contents of a set. For example, the set A would be written as A = {a, b, c}. Set A has three members or elements: a, b, and c. The set A is no different from A = {b, a, c} because the ordering of the elements in a set does not matter. The only concept that really matters for any set is membership. The sign denotes set membership, and can be read as "in". If "a is a member of the set A" we write  a ∈ A. And, if "d is not a member of set A", we could write ¬∈ A.

In Python:

def main():

    A = set( ['a', 'a', 'b', 'b','c', 'c' ] )
    print('Set A =',A)     print('"a" in A =',"a" in A)
    print('"d" in A =',"d" in A)

if __name__ == '__main__':
    main()

Python display:

>>> 
Set A = {'a', 'b', 'c'}

"a" in A = True

"d" in A = False
>>> 

A. CONDITIONAL STATEMENTS

In English: In mathematics the study of logic deals with statements or propositions. A statement is "a sentence that is either true or false, but not both". A conditional statement is two simple statements connected by the words if. . .then. (Dots like this “. . .” mean continue the pattern in the obvious way.) The first statement is called the hypothesis and the second statement is called the conclusion. Conditional statements are used in proving the validity of an argument through deductive reasoning. For example, if "it is 32 degrees Celsius or colder outside" then "the water outside is frozen".

In Python:

def main():

    Celsius = 32
    if Celsius >= 32:
        print("The water outside is frozen.")

if __name__ == '__main__':
    main()

Python display:

>>> 
The water outside is frozen.
>>> 

B. INTERSECTION

In English: If A and B are two sets taken from some universe U, then the intersection of A and B written A ∩ B, or  B, is the set containing all elements common to sets A and B. An element is in the result if it is in one set and the other.

In Python:

def main():
    # Python "Set Theory and Logic" Intersection
    U = set(['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'])

    A = set( ['a', 'b', 'c'])
    B = set( ['a', 'b', 'c', 'd'])
    C = set(A).intersection(B)

    print("Consider some universe U =",U)
    print("If A and B are two sets taken from the universe U such that")
    print("A =",A)
    print("and")
    print("B =",B)
    print("then")
    print("C = set(A).intersection(B) =",C)

if __name__ == '__main__':
    main()

Python display:

>>> 
Consider some universe U = {'m', 'l', 'o', 'n', 'i', 'h', 'k', 'j', 'e', 'd', 'g', 'f', 'a', 'c', 'b', 'y', 'x', 'z', 'u', 't', 'w', 'v', 'q', 'p', 's', 'r'}
If A and B are two sets taken from the universe U such that
A = {'a', 'c', 'b'}
and
B = {'d', 'a', 'c', 'b'}
then
C = set(A).intersection(B) = {'a', 'c', 'b'}
>>> 

C. UNION

In English: If A and B are two sets taken from some universe U, then the union of A and B written U B, or  B, is the set containing all elements in either A or B. An element is in the result if it is one set or the other.

In Python:

def main():
    # Python "Set Theory and Logic" Union
    U = set(['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'])


    A = set( ['a', 'b', 'c'])

    B = set( ['d', 'e', 'f'])
    C = A.union(B)

    print("Consider some universe U =",U)

    print("If A and B are two sets taken from the universe U such that")
    print("A =",A)
    print("and")
    print("B =",B)
    print("then")
    print("C = (A | B) =",C)

if __name__ == '__main__':

    main()

Python display:

>>> 
Consider some universe U = {'k', 'j', 'i', 'h', 'o', 'n', 'm', 'l', 'c', 'b', 'a', 'g', 'f', 'e', 'd', 'z', 'y', 'x', 's', 'r', 'q', 'p', 'w', 'v', 'u', 't'}
If A and B are two sets taken from the universe U such that
A = {'c', 'b', 'a'}
and
B = {'f', 'e', 'd'}
then
C = (A | B) = {'c', 'b', 'a', 'f', 'e', 'd'}
>>> 

D. DIFFERENCE

In English: Given two sets: A = set( ['a', 'a', 'b', 'b', 'c', 'c', 'd','e', 'f' ] ) and B = set( ['a', 'b', 'c' ] ), the difference,  C = A - B, will be such that an element will be in set C if it is in set A and not in set B.

In Python:

def main():

    A = set( ['a', 'a', 'b', 'b', 'c', 'c', 'd','e', 'f' ] )

    B = set( ['a', 'b', 'c' ] )

    C = A - B

    print("C =",C)

if __name__ == '__main__':
    main()

Python display:

>>> 

C = {'f', 'd', 'e'}

>>> 

II.  PYTHON 3 SET OPERATIONS

There are a large number of set operations, including "Union" (|), "Intersection" (&), "Difference" (-), and "Symmetric Difference" (^), also called "Exclusive Or", which is all elements in set A or set B but not both. You can also test to see if A is a Subset of B and if B is a Superset of A.

In Python:

def main():

    A = set( ['a', 'a', 'b', 'b', 'c', 'c', 'd','e', 'f' ] )
    B = set( ['a', 'b', 'c', 'z' ] )
    C = set( ['a', 'b', 'c', 'w', 'z' ] )

    print('The Union A | B is a new set with elements from both A and B =', A | B, "\n")
    print('The Intersection A & B is a new set with elements common to A and B =', A & B, "\n")
    print('The Difference A - B is a new set with elements in A but not in B =', A - B, "\n")
    print('The Symmetric Difference A ^ B is a new set with elements in either A or B but not both =', A ^ B, "\n")
    print('Is B a subset of C? In other words, is every element in B also in C?',B <= C, "\n")
    print('Is C a superset of B? In other words, is every element in B also in C?',C >= B)

if __name__ == '__main__':
    main()

Python display:

>>> 
The Union A | B is a new set with elements from both A and B = {'z', 'd', 'e', 'f', 'a', 'b', 'c'}

The Intersection A & B is a new set with elements common to A and B = {'a', 'b', 'c'}

The Difference A - B is a new set with elements in A but not in B = {'d', 'e', 'f'}

The Symmetric Difference A ^ B is a new set with elements in either A or B but not both = {'z', 'd', 'e', 'f'}

Is B a subset of C? In other words, is every element in B also in C? True

Is C a superset of B? In other words, is every element in B also in C? True
>>> 

III.  REFERENCES

The Python Tutorial by Guido van Rossum and Fred L. Drake, Jr., editor (2003)

Style Guide for Python Code by Guido van Rossum and Barry Warsaw (2001)

In this tutorial, you have been presented with a basic introduction to Python 3 "Set Theory and Logic".


Elcric Otto Circle






-->




-->




-->

















How to Link to My Home Page

It will appear on your website as:

"Link to: ELCRIC OTTO CIRCLE's Home Page"