I started to think how to create up and down style loop, and what I ended up was this. Wanted to do it little unconventional way though to demonstrate the property of range as list.

for n in range(8):
     for i in range(n)+range(n-2,-1,-1):
	  print i,
     print
""" Output:
0
0 1 0
0 1 2 1 0
0 1 2 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 4 5 4 3 2 1 0
0 1 2 3 4 5 6 5 4 3 2 1 0 """
jlm699 commented: This doesn't warrant a code snippet - also this would not work in Python 3.0 +0

Somebody has campaign against me. Please private message me or give at least reputation comment if you down or upvote me.

Here is a more complete use case context of the general version of this principle: ( I left out collecting the result in list of strings and centering it. Here I only print it)

## here more complete example of real world use, see the original case for basic show of principle

def yoyo(seq):
    """yoyo sequences by reflecting the sequence backwards so that the last
    element becomes the center element of the returned sequence"""
    return seq+seq[-2::-1] ## forward sequence and backwards (step -1) from second last


yoyol=[range(6),['a','b','c'],'-+|*']

for i in yoyol:
    for j in range(7):
        ## if j goes over length of sequence, i[:j] is copy of whole sequece i[:]
        for x in yoyo(i[:j]):
            print x,
        print
""" Output:
0
0 1 0
0 1 2 1 0
0 1 2 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 4 5 4 3 2 1 0

a
a b a
a b c b a
a b c b a
a b c b a
a b c b a

-
- + -
- + | + -
- + | * | + -
- + | * | + -
- + | * | + -
>>> 
"""

I am coding for Python 2.6, usually you can easily convert simple code snippets by using 2to3. Usually difference is the print statement without braces only (even I do not know how the print function handles all variations of print statement, I think generally by keyword arguments)

Here is code I produced for Python 3 by using __future__ import in Python 2.6 and document
http://docs.python.org/dev/3.0/whatsnew/3.0.html

## effort on Python 3 compatibility
from __future__ import print_function

def yoyo(seq):
    """yoyo sequences by reflecting the sequence backwards so that the last
    element becomes the center element of the returned sequence"""
    return seq+seq[-2::-1] ## forward sequence and backwards (step -1) from second last

yoyol=[range(8),['a','b','c'],'-+|*']

for i in yoyol:
    for j,_ in enumerate(i):
        for x in yoyo(i[:j]):
            print(x,end='')
        print()
    
""" Output:

0
010
01210
0123210
012343210
01234543210
0123456543210

a
aba

-
-+-
-+|+-
>>> """

Very nice idea.

Pretty cool idea, i will save this one hoping to come up with a use for it sometimes :D

Main use for me would be to do pattern of colored square with items in the sequence as colors or squares with color where colors form overlapping squares instead of simple diagonal lines as my diagonal stripes function (generelized checkered board routine) which does not look so special with many colors as stripes.

def diag_stripe(top,colors,sqsize,cols,rows=None):
    """ top=object to draw in, colors=list of color names, sqsize=sqsize of the square
        cols,rows = number of cols and rows
    """
    if not rows: rows=cols ## can not put rows=cols in parameter list
    cl=len(colors) 
    col=0
    
    ## diag stripes for any sqsize, any number of colors, rectangle
    for j in range(0,rows*sqsize,sqsize):
        for i in range (0,cols*sqsize,sqsize):
            fill=colors[col]
            top.create_rectangle(i,j,i+sqsize, j+sqsize,fill=fill)
            col=(col+1)%cl
        ## make to start with different color next line,
        ## if number of colors divides the number of cols
        if not cols % cl: col=(col+1)%cl

This was the motivation for the sequence style, but I did not come around to improve this pattern maker wiith use of it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.