Learn about Python's judging conditions and looping operations today.
A. Boolean variable:
The bool variable must be understood before learning the criteria, and in Python the bool variable is similar to the C language, with a larger difference from. NET, where the following focus needs to be remembered.
False, ' ', 0, (), [],{},none is empty, while true,12, ' Hello ', [1] These are all true. In fact, it can be simply understood as, none is false, there is both true
BOOL (True) True BOOL (0) False BOOL (1) True BOOL (') False BOOL ('123') True BOOL ([]) False
Two. Use of IF.
The
Note:if is separated by the: number, which is equivalent to the C # and C language {} separated by the interval number for the included code snippet. For example, the third line print ' minor is a sub-method of if (age<18), then you need to empty a few spaces in front.
- if (age<): ' Young People ' elif (Age <): ' Adult ' Else : ' elderly ' Output: ' Adult '
- if (age<): if (age>): ' Adult '
Three. Use of the while:
1 while x<=: print x x+ =1
Four. For usage:
names = ['Frank','Loch','Vincent' ,'Cain'] for in names: print name
Five. Assertion usage: When the assertion is used primarily for testing. If the assertion does not pass, it will be an error.
>>> assert age< >>>assert age<9 # The first assertion passed. The second assertion did not pass, so the error was made. Traceback (recent):"<pyshell#23>"1 in <module> assertAge <9assertionerror
Six. Comprehensive use.
1. Below is a list of the numbers that are taken out of the list:
myList = [ frank , " bob ,
2. Print out the Yang Hui triangle: 10-layer Yang Hui triangle, first declare a two-dimensional array, because you cannot use the for (int index;index<10;index++) as C or C + +, I chose a double loop with while
RowIndex =1columnindex=0myList= [[1,0,0,0,0,0,0,0,0,0],[1,0,0,0,0,0,0,0,0,0],[1,0,0,0,0,0,0,0,0,0],[1,0,0,0,0,0,0,0,0,0],[1,0,0,0,0,0,0,0,0,0],[1,0,0,0,0,0,0,0,0,0],[1,0,0,0,0,0,0,0,0,0],[1,0,0,0,0,0,0,0,0,0],[1,0,0,0,0,0,0,0,0,0],[1,0,0,0,0,0,0,0,0,0]]print mylistprint (mylist[9][9]) whileRowIndex <Ten: whileColumnIndex <=RowIndex:if(rowindex==1): mylist[1][0]=1mylist[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+=1columnindex=0RowIndex+=1 Line="' forRowinchmyList: forValinchrow:if(val!=0): Line= line + str (val) +' 'Print Line line="'
Operation Result:
1
1 1
1 2 1
1 3 3 1
1 4 6) 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 126 126 9 1
. NET Programmer's Python basic tutorial learning----judging conditions and loops [fourth day]