Python编程基础:第九节 逻辑运算Logical Operators
生活随笔
收集整理的這篇文章主要介紹了
Python编程基础:第九节 逻辑运算Logical Operators
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
第九節 邏輯運算Logical Operators
- 前言
- 實踐
前言
常用的邏輯運算共分為三種:與(and)、或(or)、非(not)。與運算就是同真才真,有假則假;或運算就是有真則真,同假才假;非運算就是真變假,假變真。我們接下來舉個例子加以分析。
實踐
我們從鍵盤獲取輸入作為今天的溫度,然后判斷若今天溫度滿足[0, 30],則輸出今天是個好天氣,適合出去玩耍:
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!我們將上述兩段代碼進行合并:
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!")取反運算的使用方法如下:
if not(True):print("False") else:print("True") >>> True以上便是邏輯運算的全部內容,感謝大家的收藏、點贊、評論。我們下一節將介紹while循環(While Loops),敬請期待~
總結
以上是生活随笔為你收集整理的Python编程基础:第九节 逻辑运算Logical Operators的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python编程基础:第八节 判断语句I
- 下一篇: Python编程基础:第十节 while