These notes can help you get started. If you know how to do something better than what is listed here, edit. I am a Python noob (with a recent Java background) just trying to keep notes on what I am learning along the way.

Table of Contents

  1. Python Installation

  2. PyCrypto Module

  3. Running code for the first time

  4. Key differences between Python and Java

    1. Summary of code differences

    2. No variable type declarations

    3. Indentation

    4. Lists

      1. Looping
      2. Slicing Power
      3. append versus extend
    5. Functions can be passed to functions

    6. Functions can return more than one value

    7. Operators

Python Installation

You must have this installed to be able to do anything else.

PyCrypto Module

Very practically, if you decompress the PyCrypto archive, there is a ReadMe.txt file inside. Follow the instructions within the ReadMe.txt file. The file is nicely laid out and highlights common errors and solutions that can occur during installation.

Running code for the first time

Copy the code from a class assignment into a text editor. Save it under a file name with the extension ".py" For example: "". Then, add a bit of code at the very end of the file so that you can run the desired function from the command line:


Make sure function call is left-aligned. Then, save your file again.

Go to your command line utility and nagivate to the directory where your file is located. Then type "python". For example:


The function that you have supplied in this code block should execute.

Key differences between Java and Python

Summary of code differences

Here is a nice chart that makes a comparison of basic operations and flow control functions in Python and Java:\~chili/CSS/pythonForJavaProgrammers.htm

No variable type declarations

One of the things that makes it challenging to read another person's code is that there are no type declarations for variables. So, if the coder did not add good comments to hint at the type and the purpose of the variables, then you must use context to figure out what kinds of things the variables are. Naturally, look at the values that are assigned to the variables, and you should be able to figure out what kinds of things the variables are. For example:

# a number
my_variable = 123

# a string
my_variable = 'Bob'

# a Boolean
my_variable = False

# a list (Lists seem to be the power of Python.  See more about lists below.)
my_variable = [456, 'Shiela', True]


Indentation is super important in Python. This is how you separate one logical code block from another. Here is a good, short article on indentation:

If you get an indentation error (for example, "Indentation Error: expected an indented block"), it probably means that you have accidentally used tabs instead of spaces to indent your code. Check the line number of the error and inspect that line to ensure that you have used spaces and not tabs (or possibly check the line before it). Apparently, 4 spaces for indentation is standard in Python land. If you are lucky, and you have an editor where you can set your tabs to a specific number of spaces, then do this. Otherwise, avoid the tab button and become friends with the space bar.


In Python, lists = power. Lists are like arrays on steroids. A list is a basic data structure in Python that allows you to put add anything that you can point to with a variable. You can make lists of numbers, you can mix numbers and Booleans, you can mix Booleans, Strings and other lists, so on and so forth. As with arrays, the index starts at 0. You can get the length by using len(name_of_list) in your code. There are a few sites that I have found when learning about the power of lists that I have found helpful:

Some things to note about lists:


You can iterate over a loop using a for construct. For example:

# create the list
plaintext = [3,7,9,9,1,9]

# loop through the list, and on each iteration, point the variable lol at the next value
for lol in plaintext:

    # print the value that lol points to
    print lol

Slicing Power

Slicing allows you to access and modify multiple elements of list directly without looping through. Slicing uses a : notation. For example, suppose I wanted to change the last to values of my plaintext list from the previous example from [9,9,1] to [8,8,0]. This could be done with one step of code:

# create the list
plaintext = [3,7,9,9,1,9]

# modify elements 2, 3 and 4 of the list
# notice that you start with the index that you want, and you end with the index+1 that you want
plaintext[2:5] = [8,8,0]

# The resulting list is [3,7,8,8,0,9]

There is a nice summary discussion of lists and slicing here:

append versus extend

You can also use function calls with lists. There are two that need to be called out. The first is append. Append will take whatever you supply as an argument and append it to a list. This is easy for primitive values like numbers and strings, but it gets a little tricky when you try to append one list to another. For example:

list1 = [7,0,7]
list2 = [0,9]

print list1
# output: [7,0,7,[0,9]]
# notice that you have 4 elements now in your list:  3 numbers and one list

If you would like to simply add the values contained in list2 to the end of list1, you can use the extend function. For example:

list1 = [7,0,7]
list2 = [0,9]

print list1
# output: [7,0,7,0,9]

Functions can be passed to functions

A polymorphic feature of Python is the ability to pass functions to other functions as arguments. This allows you to write different functions that perform variations of the same type of behavior, and then you can run those functions with the same block of code. See the example below (thank you, instructor Job):

# the following two functions define two different types of encryption behaviors

def non_encoder(block, key):
        """A basic encoder that doesn't actually do anything"""
        return pad_bits_append(block, len(key))

def xor_encoder(block, key):
        block = pad_bits_append(block, len(key))
        cipher = [b ^ k for b, k in zip(block, key)]
        return cipher

# the following is a function that will pass pointers (is there a better word for this?) to these functions
# notice that with each call to cipher_block_chaining, references to the functions
# non_encoder(...) and xor_encoder(...)
# are being made.

def test():

        #set up variables
        key = string_to_bits('4h8f.093mJo:*9#$')
        iv = string_to_bits('89JIlkj3$%0lkjdg')
        plaintext = string_to_bits("One if by land; two if by sea")

        # this is a function call that passes a reference to the non_encoder(..) function
        cipher = cipher_block_chaining(plaintext, key, iv, 128, non_encoder)
        print bits_to_string(cipher)

        # this is a function call that passes a refernce to the xor_encoder(...) function
        cipher = cipher_block_chaining(plaintext, key, iv, 128, xor_encoder)
        print bits_to_string(cipher)

# This is the function that uses the argument that points to one of our encryption function

def cipher_block_chaining(plaintext, key, block_enc):

        # get the first 128 characters from our plaintext and
        message_block = plaintext[0,128]

        # call the encryption function that is pointed to by the argument block_enc
        cipher_block = block_enc(message_block, key)

        # return the first block that has been processed by block_enc
        return cipher_block

Functions can return more than one value

The header for this section says it all. Here is an example (thank you, instructor Job) of a function that returns multiple values, and a function call that gets those values and stores them:

# our function that returns multiple values
def hash_inputs():
    block_size = 128
    block_enc = aes_encoder
    key = string_to_bits("Vs7mHNk8e39%CXeY")
    ctr = [0] * block_size
    return block_size, block_enc, key, ctr

# some other function that contains the hash_inputs() calling code
def counter_mode_hash(plaintext):

    # code that calls our multiple-return function
    block_size, block_enc, key, ctr = hash_inputs()



Operators in Python are similar, but there are some important differences. For example,

  • & is a bitwise operator!! DO NOT attempt to use it as a comparator. You will not get a syntax error, and you will get unexpected results. If you want to make a compound if statement with and "and", use "and" as your operator.
  • There don't seem to be equivalents to Java operators "++" and "--". That's a pity.
  • But, Python does have operators like "**" which is an exponent operator (rock on!).

You can read python form here:

There is a nice summary of operators and their operations here:
To know more about popular GUI framework in python :