11""" Demonstrate fundamental programming elements
2- by creating a checker board, 8 by 8 fields,
2+ by creating a checker board, N by N fields,
33field size is configurable, same width and height
4+
5+ The following programming constructs are demonstrated:
6+
7+ Comment
8+ # The "#" starts a comment until the end of the line
9+ # We can write whatever we want
10+
11+ Variable
12+ Normally, we have to remember values sometime in order to perform
13+ operations on or with them later on. A variable is an object which
14+ has a name and a value, like so:
15+ var1 = "some text"
16+ or
17+ var2 = 123
18+ or
19+ var3 = True
20+ We can change the value of variables any time
21+
22+ Constant
23+ A constant is basically a variable were the value cannot be changed
24+ (immutable). We don't have this here but we use some variables in this
25+ style
26+
27+ Operation
28+ We have a large number of operator available to do math, logic and other
29+ things like:
30+ a = 5 + 3
31+ b = a * 4
32+ c = a % 3 # The is the modulo operation (reminder)
33+ d = 5 == 6 # d is False. The double "=" means "has equal value"
34+
35+ Branch
36+ We test a condition and do one thing when it is true and another thing
37+ when it is false using IF ... ELSE ... statements like:
38+ if a == 4 then:
39+ do thing 1
40+ else:
41+ do thing 2
42+
43+ Iteration (loop)
44+ We can repeat sections of the code several times in different ways, for
45+ example for a (fixed) number of times using a
46+ FOR LOOP like so:
47+ for i in range(10):
48+ statements ...
49+ of while a condition is true like so:
50+ while a < b:
51+ statements ...
52+
53+ Block
54+ Every language has it's own syntax to form group of statements. Python
55+ uses indentation. All statements in sequence with the same indent level
56+ are in the same block, like so:
57+ if a == 4 then:
58+ thing1a() # call function thing1a in the IF branch
59+ thing1b() # same identation => same block => do also thing1b
60+ else: # same indent as the "if", previous block terminated
61+ thing2() # new block, do thing2 in the ELSE branch
62+ thing3() # not indented, previous block terminated. Thing3 is
63+ # outside the IF-ELSE block
64+
65+ Function
66+ we use functions to execute code which is (re)used several times
67+ we can provide parameters to the functions, so the same code can
68+ actually perform (slightly) different depending on the parameters
69+ Function can return "objects" (we don't know what exactly that is yet,
70+ but in Python more or less everything is an object)
71+ Functions have to be declared before we can use them like so:
72+ def <name>([paramters]):
73+ ... function code ...
74+ [return]
75+
476"""
577
78+ ########################################################################
79+ # this is our first function declaration
80+ # We have to decalre the function before we use it
681# function to make one linear segment of a pattern
782def segment (width , color ):
883 """ print <width> items of a color """
@@ -11,39 +86,39 @@ def segment(width, color):
1186 # print(color,end="") # simple verion
1287 print ("{0:2d}" .format (color ),end = "" ) # this looks better
1388
14- # the main function
89+ ########################################################################
90+ # our second function is the main function
1591def main ():
1692 """create a checker board pattern using a function"""
1793
18- # constants
94+ # constants (actually just variables we do not change any more)
1995 fields = 8
2096 fieldWidth = 4
21-
97+
2298 # variables
2399 row = 0
24100 column = 0
25101 # set color to 0
26102 color = 0
103+ # we iterate until we are done ...
27104 while row < fields * fieldWidth and column < fields * fieldWidth :
28105 # print segment
29106 segment (fieldWidth ,color )
30- # invert color
107+ # invert color after every segment
31108 color = 1 if color == 0 else 0
32- # increment column
109+ # increment column. go back to 0 after 8 fields
33110 column = column + fieldWidth if column < fieldWidth * (fields - 1 ) else 0
34111 # new line and increment row if column = 0
35112 if column == 0 :
36113 print ("" ) # prints a new line
37- # reset line
114+ # next row
38115 row = row + 1
39116 # invert color again if new row and end of field
40117 if column == 0 and row % fieldWidth == 0 :
41118 color = 1 if color == 0 else 0
42-
43119
120+
121+ ########################################################################
44122# call the main function
45123if __name__ == "__main__" :
46124 main ()
47-
48-
49-
0 commit comments