Python basics for. Net programmers-judgment conditions and loops [Fourth Day]
Today, I learned how to judge conditions and perform loop operations in Python. 1. boolean variables: you must understand the bool variables before learning and determining conditions. In Python, the bool variables are similar to those in C language and differ greatly from. net. In the following cases, remember the bool variables. False, '', 0, (), [], {}, None is null, and True, 12, 'Hello', [1] are generally True. in fact, it can be simply understood that none is false, and either is true> bool (True) True >>> bool (0) False >>> bool (1) true >>> bool ('') False >>>> bool ('000000') True >>> bool ([]) 2. IF usage. note: if is followed by a comma (,), which is equivalent to C # And C language. {} is used to separate included code segments with an interval sign. For example, if (age <18) Sub-method is used to print 'minor 'in the third line, a few spaces must be left in front of it. age = 18if (age <18): print 'minor elif (age <50): print 'minor else: print 'Elder 'output: 'Adult 'age = 20if (age <50): if (age> 18): print 'Adult' III. while usage: x = 1 while x <= 100: print x + = 1 4. for usage: names = ['frank', 'loch', 'Vincent ', 'cain'] for name in names: print name 5. use of assertions: assertions are mainly used for testing. If the assert fails, an error occurs.> age = 10> assert age <20> assert age <9 # The first assert passes. The second asserted fails, so an error is reported. Traceback (most recent call last): File "<pyshell #23>", line 1, in <module> assert age <9 AssertionError 6. Comprehensive application. 1. below is a list of numbers in the list: myList = ['frank', 'bob', 123,456,789] for val in myList: if (type (val) is int ): # type to obtain a value type. print val: 123456789 2. print out the Yang Hui triangle: Layer 10 Yang Hui triangle, first declare a two-dimensional array, because it cannot be like the C language or C ++ using for (int index; index <10; index ++ ), I chose the while double loop rowIndex = 1 columnIndex = 0 myList =, 0, 0], [, 0, 0, 0, 0, 0, 0, 0], [, 0, 0, 0, 0, 0, 0], [, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [, 0, 0], [, 0, 0, 0, 0, 0, 0], [, 0, 0, 0, 0, 0, 0, 0], [, 0, 0, 0, 0, 0, 0, 0, 0, 0], [,] print myListprint (myList [9] [9]) while rowIndex <10: while columnIndex <= rowIndex: if (rowIndex = 1): myList [1] [0] = 1 myList [1] [1] = 1 else: if (columnIndex = rowIndex ): myList [rowIndex] [columnIndex] = 1 else: myList [rowIndex] [columnIndex] = myList [rowIndex-1] [columnIndex-1] + myList [rowIndex-1] [columnIndex] columnIndex + = 1 columnIndex = 0 rowIndex + = 1 line = ''for row in myList: for val in row: if (val! = 0): line = line + str (val) + ''print line = ''running result: 1 1 1 1 2 1 1 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 15 15 6 1 1 7 21 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1 copy code