Python编程基础:第九节 逻辑运算Logical Operators
生活随笔
收集整理的這篇文章主要介紹了
Python编程基础:第九节 逻辑运算Logical Operators
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
第九節(jié) 邏輯運(yùn)算Logical Operators
- 前言
- 實(shí)踐
前言
常用的邏輯運(yùn)算共分為三種:與(and)、或(or)、非(not)。與運(yùn)算就是同真才真,有假則假;或運(yùn)算就是有真則真,同假才假;非運(yùn)算就是真變假,假變真。我們接下來舉個(gè)例子加以分析。
實(shí)踐
我們從鍵盤獲取輸入作為今天的溫度,然后判斷若今天溫度滿足[0, 30],則輸出今天是個(gè)好天氣,適合出去玩耍:
temp = int(input("What is the temperature outside?: ")) if temp>=0 and temp<=30:print("the temperature is good today!")print("go outside!") >>> What is the temperature outside?: 15 >>> the temperature is good today! >>> go outside!若今天溫度低于0°或高于30°我們就呆在家里:
temp = int(input("What is the temperature outside?: ")) if temp<0 or temp>30:print("the temperature is bad today!")print("stay inside!") >>> What is the temperature outside?: -2 >>> the temperature is bad today! >>> stay inside!我們將上述兩段代碼進(jìn)行合并:
temp = int(input("What is the temperature outside?: ")) if temp>=0 and temp<=30:print("the temperature is good today!")print("go outside!") elif temp<0 or temp>30:print("the temperature is bad today!")print("stay inside!")取反運(yùn)算的使用方法如下:
if not(True):print("False") else:print("True") >>> True以上便是邏輯運(yùn)算的全部?jī)?nèi)容,感謝大家的收藏、點(diǎn)贊、評(píng)論。我們下一節(jié)將介紹while循環(huán)(While Loops),敬請(qǐng)期待~
總結(jié)
以上是生活随笔為你收集整理的Python编程基础:第九节 逻辑运算Logical Operators的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python编程基础:第八节 判断语句I
- 下一篇: Python编程基础:第十节 while