Rearrange page numbers for printing a book I didn't want my title to be too long so to clear up any confusion I'll explain better here what my code does.

It asks for the total pages of a book(PDF file in my case), and returns the page numbers in a specific order. The groups of 32 pages will each make a booklet that when all booklets are stapled together and laid one on top of the other will be a book ready for binding. 

One issue with how I wrote the program is if "pages" is not a multiple of 4, then there will be extraneous pages at the end of the list (i couldn't think of a way to pop those). Another tid-bit, print_order doesn't need to be a list, it just needs to be page numbers separated by commas and I knew list items were separated by commas so having a list of ints would give me what I needed. The formula for printing a book on a single sided printer goes like this (n, f, f+1, n-1) where n is the last page, and f is the first page of each booklet/the whole book. The reason a long book needs to be broken into booklets is because if I take a stack of 100 papers and just fold the whole stack in half the spine would be too wide and the middle pages would be poking out of the cover. So using n and f as the first and last page, respectively, of smaller booklets allows me to print a whole book into something more practical.

I'm not worried about PEP formatting on this one I'm really just curious how this could be written more efficiently. I know python is extremely powerful and I'm not taking full advantage of what it has to offer. This is how I managed to organize my thoughts into doing what I required. I know that there are many out there who could do this in a much simpler way, and I just want to look those ways over so that I can improve my coding style!

pages = float(input("How many total pages is the book?" ))
num_booklets = pages/32
if (pages &lt; 32):
    num_booklets = 1
pages_per_booklet = int(pages/num_booklets)
last_booklet = int(pages%32)


f = 1               #first page of the book
n = int(pages_per_booklet)  #last page of the first booklet
k = 1               #number of booklets completed
print_order = []


while (len(print_order) &lt; pages):
    while ((len(print_order) &lt; pages_per_booklet*k) 
    and (len(print_order) &lt; pages)):
        print_order.extend([n, f, f+1, n-1])
        n -= 2
        f += 2
    k += 1
    n = pages_per_booklet * k
    f = 1 + (pages_per_booklet*(k-1))
    if ((len(print_order)+pages_per_booklet) &gt; pages):
        n = int(pages)
        f = int(pages) - last_booklet


print(print_order)
Style

Your code is not too far from following PEP8 and you know PEP8: it would have been a nice touch for volunteer reviewers to provide a piece of code even closer to what they expect. You'll find various tools (online or not) to check more or less precisely the compliancy of your code to PEP8. For most points, once you get used to it, it doesn't take any longer to apply as as you write the code.

Float ?

The following piece of code

pages = float(input("How many total pages is the book?"))


seems to imply that pages could be a float. As far as I can tell, this value corresponds to a number of page and an int would be more appropriate. Also, to be sure, I've tried giving a 0.5 value and the code is stuck in a loop.

Code organisation

Your code is the perfect example of a piece of code that could have been written as a clear function: input and output are both well-defined. Once you're done, you can even write unit-tests for it. Then, the code calling your function could be behind an if __name__ == "__main__": guard.

You'd get something like:

def page_order(pages):
    num_booklets = pages/32
    if (pages &lt; 32):
        num_booklets = 1
    pages_per_booklet = int(pages/num_booklets)
    last_booklet = int(pages%32)

    f = 1               #first page of the book
    n = int(pages_per_booklet)  #last page of the first booklet
    k = 1               #number of booklets completed
    print_order = []

    while (len(print_order) &lt; pages):
        while ((len(print_order) &lt; pages_per_booklet*k) 
        and (len(print_order) &lt; pages)):
            print_order.extend([n, f, f+1, n-1])
            n -= 2
            f += 2
        k += 1
        n = pages_per_booklet * k
        f = 1 + (pages_per_booklet*(k-1))
        if ((len(print_order)+pages_per_booklet) &gt; pages):
            n = int(pages)
            f = int(pages) - last_booklet
    return (print_order)

tests = {
    0: [],
    1: [1, 1, 2, 0],
    2: [2, 1, 2, 1],
    3: [3, 1, 2, 2],
    4: [4, 1, 2, 3],
    5: [5, 1, 2, 4, 3, 3, 4, 2],
    6: [6, 1, 2, 5, 4, 3, 4, 3],
    7: [7, 1, 2, 6, 5, 3, 4, 4],
    8: [8, 1, 2, 7, 6, 3, 4, 5],
    9: [9, 1, 2, 8, 7, 3, 4, 6, 5, 5, 6, 4],
    10: [10, 1, 2, 9, 8, 3, 4, 7, 6, 5, 6, 5],
    11: [11, 1, 2, 10, 9, 3, 4, 8, 7, 5, 6, 6],
    12: [12, 1, 2, 11, 10, 3, 4, 9, 8, 5, 6, 7],
    13: [13, 1, 2, 12, 11, 3, 4, 10, 9, 5, 6, 8, 7, 7, 8, 6],
    14: [14, 1, 2, 13, 12, 3, 4, 11, 10, 5, 6, 9, 8, 7, 8, 7],
    15: [15, 1, 2, 14, 13, 3, 4, 12, 11, 5, 6, 10, 9, 7, 8, 8],
    16: [16, 1, 2, 15, 14, 3, 4, 13, 12, 5, 6, 11, 10, 7, 8, 9],
    17: [17, 1, 2, 16, 15, 3, 4, 14, 13, 5, 6, 12, 11, 7, 8, 10, 9, 9, 10, 8],
    18: [18, 1, 2, 17, 16, 3, 4, 15, 14, 5, 6, 13, 12, 7, 8, 11, 10, 9, 10, 9],
    19: [19, 1, 2, 18, 17, 3, 4, 16, 15, 5, 6, 14, 13, 7, 8, 12, 11, 9, 10, 10],
}

if __name__ == "__main__":
    if False:  # Interactive
        pages = int(input("How many total pages is the book?" ))
        print(page_order(pages))
    else:  # Automatic tests
        for inpu, expected_val in tests.items():
            res = page_order(inpu)
            if res != expected_val:
                print(inpu, res, expected_val)
        print("DONE")


divmod

When you perform both a division and a modulo operation with the same values, you can use the more concise and faster divmod builtin function.

def page_order(pages):
    num_booklets, last_booklet = divmod(pages, 32)
    if pages &lt; 32:
        num_booklets = 1


Then I do not understand enough what your code is trying to achieve to try to be able to help more. For the time being, the code looks like:

def page_order(pages):
    num_booklets, last_booklet = divmod(pages, 32)
    if pages &lt; 32:
        num_booklets = 1
    pages_per_booklet = pages/num_booklets

    f = 1  #first page of the book
    n = pages_per_booklet  #last page of the first booklet
    k = 1  # number of booklets completed
    print_order = []

    while len(print_order) &lt; pages:
        while len(print_order) &lt; min(pages, pages_per_booklet*k):
            print_order.extend([n, f, f+1, n-1])
            n -= 2
            f += 2
        k += 1
        n = pages_per_booklet * k
        f = 1 + (pages_per_booklet*(k-1))
        if len(print_order) + pages_per_booklet &gt; pages:
            n = pages
            f = pages - last_booklet
    return print_order