Python------条件文とループ文

6657 ワード

Python               :
        1.    :     、         ,if-elif-else
        2.    :while             
        3.    :for        、  、      

               、           ,         .(           )
                 ,       (    )        (    )     .         tab              .           ( begin {)           ,         ( end })         .
         Python     (:)         ,            (     ).                  ,          .

 .     if

        if                   :
        1.     
               :
                                  if condition:
                                          statement
                                          statement
              Ptthon if           (),          ,      {}    TAB    .  condition            (True|False 0- |1-   0  )、     (>= <= == !=)        (and or not).
        2.     
               :
                                  if condition:
                                          statement
                                          statement
                                  else:
                                          statement
                                          statement
        3.     
        if    if-elif-else  ,  elif   else if,         if   .        :

[python] view plain copy
 
#   if-else  
count = input("please input:")  
print 'count=',count  
if count>80:  
    print 'lager than 80'  
else:  
    print 'lower than 80'  
print 'End if-else'  
  
#   if-elif-else  
number = input("please input:")  
print 'number=',number  
if number>=90:  
    print 'A'  
elif number>=80:  
    print 'B'  
elif number>=70:  
    print 'C'  
elif number>=60:  
    print 'D'  
else:  
    print 'No pass'  
print 'End if-elif-else'  
  
#      
sex = raw_input("plz input your sex:")  
if sex=='male' or sex=='m' or sex=='man':  
    print 'Man'  
else:  
    print 'Woman'  
 .     while

        while           :
                                                         while condition:
                                                                 statement
                                                                 statement
                                                         else:
                                                                 statement
                                                                 statement
                condition        、           ,else    (      C     ).    :

[python] view plain copy
 
#  while  1+2+..+100  
i = 1  
s = 0  
while i <= 100:  
    s = s+i  
    i = i+1  
else:  
    print 'exit while'  
print 'sum = ',s  
  
''''' 
     :exit while 
          sum = 5050 
'''  
               5050,   i  101 ,  i>100   else  .
              Python     (#)     ,     ('''...''')      .   C/C++ //    /**/    .
                     ,        :

[python] view plain copy
 
import webbrowser as web  
import time  
import os  
i=0  
while i<5:  
    web.open_new_tab('http://andy111.blog.sohu.com/46684846.html')  
    i=i+1  
    time.sleep(0.8)  
else:  
    os.system('taskkill /F /IM iexplore.exe')  
print 'close IE'  
                                     ,              webbrowser    open_new_tab      , CSDN  (       ip  ).
             windoes  taskkill          IE   , DOS   "taskkill /F /IM iexplore.exe"          (chrome.exe qq.exe),  /F        ,/IM    .               ,              ;      import os system()    , Linux  kill  (kill -pid killall)    .


           time.sleep(seconds)  "Delay execution for a given number of seconds.",           .
                              ,    5         100 ,                     ,     import random count=random.randint(20,40)  20 40          .      ,          Python     .      IE                .why?

 .     for

                   :
                                                 for target in sequences:
                                                         statements
        target     ,sequences    ,     list(  )、tuple(  )、strings(   ) files(  ).
        Python for          ,  C   for(i=0;i<10;i++) i    ,Python for      sequences          target ,     ,         .  in        ,           .      break continue    .
        1.     

[python] view plain copy
 
s1 = 'Eastmount of CSDN'  
for c in s1:  
    print c,  
          :   print      ,                    .         .
        2.    

[python] view plain copy
 
list1 = [1,3,4,5,'x',12.5]  
i = 0  
for val in list1:  
    print format(i,'2d'),val  
    i = i+1  
else:  
    print 'out for'  
          :  List          ,     ,              .format(i,'2d')       ,      .   0-9   " 0",   10-99   "10"      .      :

[python] view plain copy
 
1 3  
2 4  
3 5  
4 x  
5 12.5  
ut for  
            (       )           ,           range   .   for n in [1,2,3,4,5,6,7,8]   listNum=range(1,9).   "range(start, stop[, step]) -> list of integers",           ,     (  range(1,9)  1),      (   9),       0,        range(4)=[0,1,2,3].
          1 100   range(1,101),  1 100   range(1,101,2),  1 100   range(2,101,2).
        3.    

[python] view plain copy
 
tup = (1,2,3,4,5)  
for n in tup:  
    print n  
else:  
    print 'End for'  
          tuple         ,   ,   list[1,2,3,4]    .
        4.    
        help(file.read)       ."read([size]) -> read at most size bytes, returned as a string."
        help(file.readlines)      ."readlines([size]) -> list of strings, each a line from the file."    n , n readline  ,          .
        help(file.readline)        ."readline([size]) -> next line from the file, as a string."

[python] view plain copy
 
#            
for n in open('for.py','r').read():  
    print n,  
print 'End'  
  
for n in open('for.py','r').readlines():  
    print n,  
print 'End'  
  
for n in open('for.py','r').readline():  
    print n,  
print 'End'  
            :

[python] view plain copy
 
#   read()  :           
s 1   =   ' E a s t m o u n t   o f   C S D N '   
f o r   c   i n   s 1 :  
....  
End  
#   readlines()  :        
s1 = 'Eastmount of CSDN'  
for c in s1:  
....  
End  
#   readline()  :  for.py          
s 1   =   ' E a s t m o u n t   o f   C S D N '  
End  
        
 

  
転載先:https://www.cnblogs.com/zxf123/p/8011450.html