You can find at a short 2-page printable reference card for python 2.7.
A long and more complete reference page for python 2.7 is at http://rgruet.free.fr/PQR27/PQR2.7.html
For a reference manual of python 3.3, look at https://docs.python.org/3.3/index.html.

Diffie Hellman key exchange method

The Diffie-Hellman is a method for two users to exchange information that is encrypted. The method was proposed in 1976. It is the base for many methods used in VPN, SSH, PGP and other Public Key Infrastructure (PKI) systems.
The method is called asymmetric key exchange, because anyone can encrypt messages using the public key of the recipient but only the holder of the private key used to generate the public key can decrypt. The security depends on the secrecy of the private key and the use of large numbers that make brute force decryption very difficult.
Look at the page diffie.html for details and for a googledocs page you can use to share your public keys.
You can explore other methods security keys, such as RSA.

Compute the value of π

Let's start by looking at a circle inscribed in a square, as in the figure.


If the circle in centered at (0,0) and has a radius if 1, the length of the side of the square is 2. The area of the circle is π r2= π and the area of the square is 2 r2=4. The ratio of the area of the circle to the area of the square is π/4.

This suggests a method to compute π. If we compute the ratio of the area of the circle to the area of the square and multiply the result by 4 we have the value of π.

To compute this ratio we use a Monte Carlo method, which is method that uses a sequence of random numbers. Suppose we select a random point (x,y) with [-1 ≤ x ≤ 1] and [-1 ≤ y ≤ 1] and we check if the point is in the circle or in the part of the square outside the circle. If we repeat this geneartion of points many times, the ratio of the number of points that are outside the circle to the number of points that are inside the circle is a good approximation of π/4. The accuracy of the results depends on the number of points used. More points produce a more accurate value.

To use the Monte Carlo method described above, you need to generate points by generating random numbers in the [-1, 1] range. For each point, you need to check if the point is inside the circle or not (recall that the parametric form of a circle is x2 +y2 = r) and determine if the point falls within the circle or not. Try various sample sizes, including 100, 1,000, 100,000 and 1,000,000 and observe how the sample size affects the accuracy of the result. Compare your result with the value that python has for π , which is defined in the math module

from math import *
pi

To generate random numbers in python, you need to import the random module. Use

uniform(-1,1)
to generate a randonm number in the [-1,1] range.

Play Blackjack

BlackJack is a popular casino card game. The objective is to continue adding cards to your hand without its value exceeding 21. The value of the hand is the sum of the individual card values as follows: the cards 2 to 10 count at face value (2-10 points), the Jack, Queen and King count as 10 points each, and the Ace counts as either 1 or 11 depending on the most advantageous count.

Start by writing a program to determine the score of a BlackJack hand. First you need to decide how to represent the values of the cards (as characters or integers). Next, how to represent a hand (as a listi, for instance). Given a hand of cards compute its value if less than 21. Since the Ace Ace can count as either 1 or 11. you should initially value any Ace as 11 unless the total of the hand is greater than 21. If the value of the hand is greater than 21, you should recompute it assuming the Ace(s) is (are) valued as 1.

Next create a deck of cards, each with value and suite (for instance, using a list). You need to shuffle the deck, which you can do using another function from the random module, shuffle(x), where x is a sequence.

DNA

The DNA molecule employs a 4-element code consisting of sequences of the following nucleotides: Adenine, Guanine, Cytosine and Thymine. Biologists use a letter sequence to represent the genome, in which each letter stands for one of the four nucleotides. For example: . . . AGTCTATGTATCTCGTT . . .
Individual genes are substrings of a genome delineated by 3-element start and stop codons. The majority of genes begin with the start codon: ATG and end with one of the following 3 stop codons: TAG, TAA or TGA. Start codons can appear anywhere in the string, followed by a series of 3-element codons and ending with a stop codon. Note that genes are multiples of 3 in length and do not contain any of the triples ATG, TAG, TAA or TGA.

Write a Python program that reads a genome as a string and displays all the genes in the genome. Note that that finding a substring matching an end codon but which overlaps two codons would not be an actual end codon (i.e., the resulting gene string should be divisible by three).
Example:

Enter a DNA sequence: TCATGTGCCCAAGCTGACTATGGCCCAATAGCG
Gene 1 TGCCCAAGC
Gene 2 GCCCAA
This is just the beginning to having fun with DNA problems.
(Example from Mark LeBlanc, Wheaton College DNA course)


Copyright: © 2016 by the Regents of the University of Minnesota
Department of Computer Science and Engineering. All rights reserved.
Comments to: Maria Gini