python中if條件分支


 

1、條件成立執行

>>> a = 50
>>> if a > 10: print("xxx") xxx >>> if a < 10: print("yyy") >>> if a == 50: print("zzz") zzz

 

 

score = int(input("score=")) if 100 >= score >= 90: print("AAA") if 90 > score >= 80: print("BBB") if 80 > score >= 60: print("CCC") if 60 > score >= 0: print("DDD") if score > 100 or score <= 0: print("error! range: 0-100")

 

score = int(input("score=")) if 100 >= score >=90: print("AAA") else: if 90 > score >= 80: print("BBB") else: if 80 > score >= 60: print("CCC") else: if 60 > score >= 0: print("DDD") else: print("error! range:0-100")

 

score = int(input("score:")) if 100 >= score >= 90: print("AAA") elif 90 > score >= 80: print("BBB") elif 80 > score >= 60: print("CCC") elif 60 > score >= 0: print("DDD") else: print("error! range: 0-100")

 

>>> test1 = ["aaa","bbb","ccc","ddd"] >>> for i in test1: if i == "aaa": print(i.upper()) else: print(i.title()) AAA Bbb Ccc Ddd >>> for i in test1: if i == "bbb": print(i.upper()) else: print("_".join(i)) a_a_a BBB c_c_c d_d_d

 

>>> test1 = ["aaa","bbb","ccc","ddd"] >>> for i in test1: if i == "ccc": print(i.upper()) else: print(i.replace("a","xxx")) xxxxxxxxx bbb CCC ddd >>> for i in test1: if i == "aaa" or i == "bbb": print("+".join(i)) else: print(i.title()) a+a+a b+b+b Ccc Ddd

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM