for ( Parametric )

  • The for code block can create a loop that iterates over a particular range until no more elements in that range are available. A for code block consists of a " for ... : " line and line(s) of code indented beneath the " for ... : " line; the indented code provides a course of action for each iteration of the expressed range.
  • See (on this page):

Also see :


Iteration with a for loop :

Strings "..." , lists [x, 2, z] and tuples (1, z, 3) are sequences. To iterate over the items in a sequence you can use a for loop. Iteration proceeds consecutively, following the order in which the items appear within the sequence.

SDS2 Python Prompt

>>> list1 = ['a', 'b', 'c', 'd', 'e', 'f'] >>> for var in list 1: ... print(var,) # Trailing comma in causes the contents of the list to be output to a single line. ... a b c d e f

The keyword break in this next example ends the loop after the first iteration. As a result, the value assigned to var is the first item in list1 .

SDS2 Python Prompt

>>> list1 ['a', 'b', 'c', 'd', 'e', 'f'] >>> for var in list 1: ... break ... >>> var 'a'

The variable var is assigned the value 'a' because 'break' ends the loop after the first iteration.

The keyword continue in this next example causes the loop to continue to the next iteration. The loop ends when there are no more items to iterate over in the list. The final value assigned to var is the final item in list1 .

SDS2 Python Prompt

>>> list1 ['a', 'b', 'c', 'd', 'e', 'f'] >>> for var in list 1: ... continue ... >>> var 'f'

The variable var is now assigned the value 'f' . These same results would have been achieved if the keyword pass had been substituted for continue .

Boolean operators such as < or > can be used to compare strings as well as numbers . This example uses an if condition to print only those items in list1 that are greater than the item 'c' . These items are 'd' , 'e' , and 'f' .

SDS2 Python Prompt

>>> 'd' > 'c' True >>> list1 ['a', 'b', 'c', 'd', 'e', 'f'] >>> for var in list1: ... if var > 'c': ... print(var,) ... ... d e f >>> var 'f'

The final value of var is 'f' since 'f' was the value assigned in the last iteration.

The keyword break is used in this example to stop the iteration when var is greater than 'c' .

SDS2 Python Prompt

>>> list1 ['a', 'b', 'c', 'd', 'e', 'f'] >>> for var in list1: ... if var > 'c': ... print(var) ... break ... >>> var 'd'

Now the value of var is 'd' since 'd' was the value assigned at the time that break ended the loop.

Here is an example that shows something a bit more useful, though not much more. Two lists are compared to determine their common elements.

SDS2 Python Prompt

>>> list1 ['a', 'b', 'c', 'd', 'e', 'f'] >>> vowels = [ 'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U' ] >>> for var in list1: ... if var in vowels: ... print(var,) ... a e

A for statement may have an optional else clause, which executes when the sequence being iterated over is exhausted.

SDS2 Python Prompt

>>> word1 = "djjkjp" >>> vowels [ 'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U' ] >>> for ltr in word1: ... if ltr in vowels: ... print(ltr,) ... else: ... print("The word", word1, " has no vowels.") ... The word djjkjp has no vowels.

However, there is a problem with the above example. Namely, if word1 were a word with vowels in it, then its vowels would be printed and, when the sequence was exhausted, The word ... has no vowels would also be printed.

SDS2 Python Prompt

>>> word2 = "banana" >>> for ltr in word2: ... if ltr in vowels: ... print(ltr,) ... else: ... print("The word", word2, " has no vowels.") ... The word banana has no vowels.

A more useful approach would be to set up a situation where the outcomes described in the print statements are mutually exclusive. The keyword break in this next example ends the inner for loop after the first vowel is found. The outer for loop continues to its next iteration. If a for loop is terminated with break , its else clause is not executed. The else clause executes only when the loop ends because no vowels have been found. Notice that the else clause in this example is indented so that it aligns with the second for statement.

SDS2 Python Prompt

>>> word1 'djjkjp' >>> word2 'banana' >>> for wrd in [word1, word2]: # iterates through the list ... for ltr in wrd: # iterates through letters in a word ... if ltr in vowels: # compares letters to the vowel list ... print("The first vowel in", wrd, "is:", ltr) ... break ... else: ... print("has no vowels.") ... The word djjkjp has no vowels. The first vowel in banana is: a

Examples :

In this example, mem1.LeftEnd.Nodes returns a list -- e.g., [22, 0, 0, 0] -- of member numbers which is printed by the line that reads print(mem1.LeftEnd.Nodes) . Lines that are grouped (indented) under the for...: statement print the piecemark (mem2.Piecemark ) and member number ( mem2.MemberNumber ) for those members in the list that are not zero [0] . The if statement ( if mem==0: ) that is indented under the for statement handles the values that are zero in the mem1.LeftEnd.Nodes list.

# Prints piecemarks [nums] supporting the left end of mem1.
from param import ClearSelection
from member import MemberLocate
mem1 = MemberLocate("Select a member")
print(mem1.LeftEnd.Nodes)
for mem_num in mem1.LeftEnd.Nodes:
    if mem_num == 0:
        continue
    mem2 = Member(mem_num)
    print(mem2.Piecemark, "[", mem2.MemberNumber, "]")
ClearSelection()

Tip: See Copying and Pasting scripts from Help for information on how to paste these scripts into blank .py text files that you can Run as parametrics in Modeling .

Here's a script that clears the " Date held " date for all members in the Job in which this parametric is Run .

# Clears "Date held" for all members in Job.
from member import MemberAllocated, Member
for mem_num in range(MemberAllocated() + 1):
    try:
        m = Member(mem_num)
        m.MarkedForHold = "Not held"
        m.DateMemberWasHeld = "NOT SET"
        m.Update()
    except:
        pass

The following script creates a grid of equally spaced construction lines in any view. When the script is Run , the user left-clicks ( Locate ) to define a point, then enters the number of construction lines and the distance that the construction lines are to be apart. The grid of construction lines is then generated.

# Creates a grid of equally spaced construction lines. 
from param import Prompt, Units 
from point import PointLocate 
from cons_line import ConsLine Units("feet") 
pt1 = PointLocate("Locate a point") 
num = Prompt(5, "How many horz/vert construction lines?") 
spc = Prompt("5-0", "How far apart do you want the con lines?") 
for ii in range(0, num): 
    cl1 = ConsLine()     # blue, horizontal con lines 
    cl1.Point1 = (pt1.x, pt1.y+ii*spc, pt1.Z) 
    cl1.Rotation = 0 
    cl1.PenNumber = "Blue" 
    cl1.Add() 
    cl2 = ConsLine()     # green, vertical con lines 
    cl2.Point1 = (pt1.x+ii*spc, pt1.Y, pt1.Z) 
    cl2.Rotation = 90 
    cl2.PenNumber = "Green" 
    cl2.Add()