#-*-coding:utf-8-*-#Fourth Chapter if for while#Boolean logicPrintTrue = =FalsePrintTrue andFalsePrintTrueorFalsePrint notTruePrintTrue! =False#if elif elseName ='Lee'ifName = ='Chen' : Print 'Yes, you are Chen'elifName = ='le': Print 'Yes, you are God'elifName = ='Shou': Print 'No, get out'#a conditional expression, similar to an expressionPrint 'Yes' if1 + 1 = = 2Else 'No'# for#RangePrintHelp (range)PrintType (range (10))PrintRange (0, 10, 2)PrintRange (10, 0, 1)#Range (Start, Stop=none, Step=none) step is each increment or decrement (-) Step forIinchRange (10, 0, 1) : PrintI forIinch[' A',' at','Lala'] : PrintI# whileName ='Chen' whileTrue:Print 'Heheda' ifName = ='le' : Breakname='le' Continue Print "no continue"
Results:
Falsefalsetruefalsetrueyeshelp on built-inchFunction rangeinchModule__builtin__: Range (...) Range (stop)-List of integers range (start, stop[, step])-List of integers Return a list containing an arithmetic progression of integers. Range (I, j) returns [I, I+1, I+2, ..., j-1]; Start (!) defaults to 0. When step isGiven, it specifies the increment (ordecrement). For example, range (4) returns [0, 1, 2, 3]. The end point isomitted! These is exactly the valid indices forA list of 4elements. None<type'List'>[0,2, 4, 6, 8][10, 9, 8, 7, 6, 5, 4, 3, 2, 1]109876543211223Lalahehedaheheda
For or while else
# While or for else # The loop ends normally (no break, return), and else is entered in the Else code snippet. for in range: print iif i = = 3 : break Else : print"forelse"
Results:
0123
Python if,for,while