Codes for quilting

From XPUB & Lens-Based wiki
Revision as of 17:36, 15 October 2020 by Naaami (talk | contribs)

loops
for x in range(10):

   print ("hello python")
   print (x)

x = 1 while x<10:

   print (f"x is {x}")
   x = x + 1

.random
words.sort() words = ['weaving', 'with', 'words', 'and', 'code']

  1. First a simple loop through the list

for word in words:

   print(word)
  1. Then, a loop in which we start to play with random again

import random for word in words:

   print(random.choice(words), random.choice(words), random.choice(words), random.choice(words), random.choice(words))
  1. How to work with more iterations of the loop?
  2. For example 100?
  3. You can use "range"

range?

  1. Make a loop that starts at 0 and ends at 99 (100 iterations)

import random for number in range(100):

   print(random.choice(words), random.choice(words), random.choice(words), random.choice(words), random.choice(words))
  1. You can use range also differently, for example to loop in between two numbers ...

for number in range(50, 60):

   print(number, 'x'* number)
  1. ... or with bigger steps:

for number in range(0,100,10):

   print(number, '*'* number)
  1. Now... write a loop in a loop

for x in range(1,6):

   for y in range(1,6):
       print('x'* x, 'y'* y)