搜尋
首頁後端開發Python教學Guibs 的 Python學習_If

Guibs 的 Python學習_If

Jan 20, 2017 pm 04:20 PM

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(&#39;没一个对的...&#39;)# 检查特定值是否包含在列表里# [in]languages = [&#39;Swift&#39;, &#39;Python&#39;, &#39;Objective-C&#39;]
print(&#39;Java&#39; 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 = [&#39;Swift&#39;, &#39;Python&#39;, &#39;Objective-C&#39;, &#39;Java&#39;]for language in languages:   
 if language == &#39;Swift&#39; or language == &#39;Python&#39;:
        print("非常乐意学习和使用" + language)
 # 确定列表不是空的order_books = []if order_books:    for book in order_books:
        print(book)else:
    print("订购清单为空, 你还没确定要订购的图书")# 使用多个列表wanna_books = [&#39;DN&#39;, &#39;Swift&#39;, &#39;Python&#39;]
sale_books = [&#39;Swift&#39;, &#39;Python&#39;, &#39;Java&#39;, &#39;PHP&#39;]for sale_book in sale_books:    if sale_book in wanna_books:
        print(sale_book + " 恰好是我想买的")

以上就是Guibs 的 Python學習_If的內容,更多相關內容請關注PHP中文網(www.php.cn)!


陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
Python:編譯器還是解釋器?Python:編譯器還是解釋器?May 13, 2025 am 12:10 AM

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

python用於循環與循環時:何時使用哪個?python用於循環與循環時:何時使用哪個?May 13, 2025 am 12:07 AM

UseeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.forloopsareIdealForkNownsences,而WhileLeleLeleLeleLeleLoopSituationSituationsItuationsItuationSuationSituationswithUndEtermentersitations。

Python循環:最常見的錯誤Python循環:最常見的錯誤May 13, 2025 am 12:07 AM

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

對於循環和python中的循環時:每個循環的優點是什麼?對於循環和python中的循環時:每個循環的優點是什麼?May 13, 2025 am 12:01 AM

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

Python:深入研究彙編和解釋Python:深入研究彙編和解釋May 12, 2025 am 12:14 AM

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

Python是一種解釋或編譯語言,為什麼重要?Python是一種解釋或編譯語言,為什麼重要?May 12, 2025 am 12:09 AM

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

對於python中的循環時循環與循環:解釋了關鍵差異對於python中的循環時循環與循環:解釋了關鍵差異May 12, 2025 am 12:08 AM

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

循環時:實用指南循環時:實用指南May 12, 2025 am 12:07 AM

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

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱門文章

熱工具

MantisBT

MantisBT

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

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具