Guibs 的 Python學習_If
# if# if 通过缩进来控制代码块是否属于 if# if conditional_test:# do somethingcars = ['audi', 'bmw', 'subaru', 'toyota']for car in cars: if car == 'bmw': print(car.upper()) else: print(car.title())# 条件测试# 每条 if 语句的核心都是一个值为 True 或 False 的表达式, 这种表达式被称之为条件测试. # 检查是否相等# [==] 在两边的值相等时返回 Trueprint("bmw" == "bmw") # 检查是否不相等# [!=] 在两边的值不相等时返回 Trueprint("bmw" != "bmw")# 比较数字print(1 > 2) print(1 == 2) print(1 < 2) print(1 != 2) print(1 >= 2) print(1 <= 2)# 检查多个条件# 使用 and 检查多个条件# 必须几个条件都为 True 才返回 Trueif 1 > 0 and 2 > 1: print("说的没错")else: print("有问题啊")# 使用 or 检查多个条件# 有一个条件为 True 就返回 Trueif 1 > 0 or 2 < 1: print("可以的")else: print('没一个对的...')# 检查特定值是否包含在列表里# [in]languages = ['Swift', 'Python', 'Objective-C'] print('Java' in languages)# 检查特定值是否不包含在列表里# [not in]prices = [1, 3, 10, 6] print(2 not in prices)# 布尔表达式# 布尔表达式的结果要么为 True 要么为 FalseisExist = Truegame_active = False # if 语句if False: print("这是 if 体的语句, 所以不会显示") print("这不是 if 体的语句, 所以会显示")# if-else 语句age = 20if age > 18: print("你已经成年了")else: print("你还未成年")# if-elif-else# elif 可以有多个money = 20if money > 18: print("你可以买鸡排咖喱饭")elif money > 15: print("你可以买猪排咖喱饭")elif money > 12: print("你可以买咖喱饭")# else 可以省略else: print("你买不了咖喱饭")# 用多个 if 语句执行一系列满足条件的判断num = 3if num > 0: print("你的数字大于0")if num > 1: print("你的数字大于1")if num > 2: print("你的数字大于2")# if 处理列表# 检查特殊元素 languages = ['Swift', 'Python', 'Objective-C', 'Java']for language in languages: if language == 'Swift' or language == 'Python': print("非常乐意学习和使用" + language) # 确定列表不是空的order_books = []if order_books: for book in order_books: print(book)else: print("订购清单为空, 你还没确定要订购的图书")# 使用多个列表wanna_books = ['DN', 'Swift', 'Python'] sale_books = ['Swift', 'Python', 'Java', 'PHP']for sale_book in sale_books: if sale_book in wanna_books: print(sale_book + " 恰好是我想买的")
以上就是Guibs 的 Python學習_If的內容,更多相關內容請關注PHP中文網(www.php.cn)!

Python是解釋型語言,但也包含編譯過程。 1)Python代碼先編譯成字節碼。 2)字節碼由Python虛擬機解釋執行。 3)這種混合機制使Python既靈活又高效,但執行速度不如完全編譯型語言。

UseeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.forloopsareIdealForkNownsences,而WhileLeleLeleLeleLeleLoopSituationSituationsItuationsItuationSuationSituationswithUndEtermentersitations。

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐個偏置,零indexingissues,andnestedloopineflinefficiencies

forloopsareadvantageousforknowniterations and sequests,供應模擬性和可讀性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具