python基礎編(3-9)-Pythonフロー制御文:if、while、forのループを判断

10376 ワード

文書ディレクトリ
  • 1判定(if)文
  • 2演算子の優先度:
  • 3ループ文(while)
  • 1判定(if)文
    1.1判断の定義:
  • 条件が満たされれば、何かをすることができます.
  • 判定文は「分岐文」
  • とも呼ばれる.
    1.2 if文の基本構文:1)単一のif文:
    if     :
    	    ,    
    

    2)if...elseの組み合わせ:
    #           ,     
    if     :
    	    ,     
    else:
    	     ,     
    

    1.3論理演算:
  • pythonの論理演算子には、and,or,notの3つの
  • があります.
    age = 100
    # and       :
    if age >= 0 and age <=120:
    	print("    ")
    else:
    	print("     ")
    

    1.4 if文のステップ:1)if...elif...elif...elseの使用
    # python   switch  ,if      switch   
    if   1:
    	  1       
    elif   2:
    	  2       
    else :
    	             
    
  • if文のネスト
  • # if   ,      
    if   11       
    	...
    	if   1      2 :
    		  2        
    	else :
    		  2         
    else1         
    		
    

    1.5 if文の総合応用例:
    #            ——   (1)/  (2)/ (3)
    player = int(input("      (1)/  (2)/ (3):"))
    
    #          -          
    computer = 1
    
    #     
    #            ,                
    #          ,    ,PyCharm        8    
    if ((player == 1 and computer == 2) or
            (player == 2 and computer == 3) or
            (player == 3 and computer == 1)):
    
        print("  !!!     !!!")
    elif player == computer:
        print("    ,    !")
    else:
        print("  ,         !")
    
    

    2演算子の優先度:
    演算子
    説明
    **
    べき乗(最優先順位)
    */%//
    乗ずる
    + -
    加算、減算
    <= < >>=
    比較演算子
    == !=
    イコール演算子
    = %=/=//= -= += *=
    代入演算子
    not of and
    論理演算子
    3ループ文(while)
    3.1 while文の基本構文1)while文の実行手順
           ——             
    #      (    1  )
    #      (    0  )
    while   (                ):123
        ...(  )...
    
            (    + 1)
    

    2)while文適用例(sum(100))
    result = 0 	#         
    i = 0 		# 1.   
    # 2.    
    while i <= 100 :
    	# 3.    ,    
    	result += 1 
    	# 4.     
    	i += 1
    

    3.2 breakとcontinue:
  • break:ある条件が満たされ、現在のループを終了し、後続コード
  • を実行しない
  • continue:ある条件が満たされ、今回のサイクルを終了し、後続コード
  • を実行しない
    3.3 whileループネスト:
    while    1:1
        ...(  )...
    
        while    2:1
            ...(  )...
    
                 2
    
             1
    

    3.4 while文総合運用(9*9乗算表)
    row = 1 #      
    while row <= 9 :
    	col = 1 #      
    	
    	#   row :
    	while col <= row :
    		print("%d * %d = %d" % (col, row, col*row),end="\t")
    		col  += 1 #     2
    	print("")	#   
    	row += 1 #     1