Small Q: who read West wind alone cool, rustling yellow leaf closed window, meditation past setting sun.
The wine is not surprised by the heavy spring, gambling books to pour tea incense, then only the road is ordinary. ----Lanrong "Wan Xi sha"
----------------------------------------------------------------------------------------------------
Each keyword is separated by a colon (:)
The module is defined by indentation spaces, generally empty four squares
Note: There are no do...while and switch statements in Python, but there are for....else and While.....else statements.
If judgment condition 1:
Execute Statement 1 ...
Elif Judgment Condition 2:
Execute Statement 2 ...
Else
Execute Statement 3 ...
#!/usr/bin/python#-*-coding:uft-8-*-#有外界交互的成绩评定器source = Int (raw_input ("Please input a num:")) #raw_input输入的是字符串, you need to go IfIf source >= 90:print ' A ' print ' very good ' elif source >= 70:print ' B ' print ' good ' else:print ' C ' print ' It's bad ' print "ending"
While judging condition:
Execute statement ...
#!/usr/bin/pythoncount = 0while Count < 5:print count, "is less than 5" Count = Count + 1else:print count, " Is isn't less than 5 ">>>>>>0 are less than are less than are less than An than 5
Note: while. else is the same as For...else and executes when the loop is running normally (not breaking out of breaks, etc.)
A Python for loop can traverse any sequence of items, such as a list or a string.
For Iterating_var in sequence:
Statements (s)
#!/usr/bin/python#-*-coding:utf-8-*-fruits = [' banana ', ' apple ', ' Mango ']for index in range (len (fruits)): print ' current Fruit: ', fruits[index]print ' good bye! ' >>>>>>>> Current fruit: Banana current fruit: Apple current fruit: Mangogood bye!
Random function:
Range (5) >>> 0,1,2,3,4
Range (1,6) >>> 1,2,3,4,5
Range (1,10,2) >>> 1,3,5,7,9
Xrange (5) >>> 0,1,2,3,4
Xrange (1,6) >>> 1,2,3,4,5
Xrange does not account for memory, and is not called until it is used, test:
A = xrange (5); Print a
Example: a prime number between nested loop output 2-30?
#!/usr/bin/python#-*-coding:uft-8-*-i = 2for I < 30:j = 2 while (j <= (i/j)): #无优先级时, brackets are optional i F Not (I%J): Break #i divided by J without remainder, then interrupts j = j + 1 if J > i/j: Print 1, ' is prime number ' i= i+ 1print ' good Bye ">>>>>>>>>2 is the prime number 3 is the prime number 5 is the prime number 7 is the prime number 11 is the prime number 13 is the prime number 17 is the prime number 19 is prime number 23 is the prime number 29 is prime
Example: Print 9*9 multiplication table?
#!/usr/bin/pythonfor i in xrange (1,10): for j in xrange (1,i+1): print "%s*%s=%s" % (j,i,j*i) print #打印换行符 >> >>>>>>> 1*1=11*2=2 2*2=41*3=3 2*3=6 3*3=91*4=4 2*4=8 3*4=12 4*4=161*5=5 2*5=10 3*5=15 4*5=20 5*5=251*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=361*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7 =491*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=641*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
----------------------------------------------------------------------------------------------------
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/80/EA/wKiom1dEUNnwyQmDAAAzGbwu928073.png "title=" statement. png "alt=" Wkiom1deunnwyqmdaaazgbwu928073.png "/>
#!/usr/bin/python# -*- coding: utf-8 -*-for letter in ' python ': # first instance of break if letter == ' H ': break print ' current letter : ', letter var = 7 # the second instance of Continuewhile var > 0: var = var -1 if var == 5: continue print ' current variable value : ', var for letter in ' ABCD ': #第三个实例pass if letter == ' B ': pass print ' This is pass block ' print ' current letter : ', letterprint "good bye!" >>>>>>>>>current letter : pcurrent letter : ycurrent letter : t current variable value : 6 current variable value : 4 current variable value : 3 current variable value : 2 current variable value : 1 current variable Value : 0 Current letter : a This is the pass block current letter : b Current letter : c Current letter : dgood bye!
Python--if and for statement