0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

演算子について:python

Last updated at Posted at 2024-02-23

※このQiitaで記事を書くことに慣れていない為、誤字・脱字や読みづらい部分もあるかと思いますが、ご理解ください。

閲覧いただき、ありがとうございます!
私は、現在医療業界で働く医療従事者になります。
とある経緯からpythonを勉強中です。
ここでは自分が勉強した事を復習したり、忘れないようにするため記載いたしますので、もしもっとこうした方が良いよ、とかこれはちょっと。。。というものがあれば教えていただけますと幸いです。
※記事内容は随時更新します

否定を演算(not)

※ブール型で出力

記述
a = 2 <= 5

print(not a)  #aの結果がTrueならFalse、FalseならTrueとなる。

結果

False

※not演算子には使い分けがある。

not in 演算子がおこなう演算と同等の演算を、not 演算子と in 演算子を使ってもおこなうことは出来るが、not in 演算子を使う事が推奨されている。

記述
a = "python"
print("p" not in a) #推奨
print(not "p" in a) #非推奨

結果

False
False

ブール演算の優先順位

 not >> and >> or の順で評価されます。

記述
a, b, c = 10, 20, 30

#notとandについて
print(not a == 10 and c == 30) #notから評価されるため False and c == 30となる

#notとorについて
print(not a == 20 or b == 30) #notから評価されるため True or b == 30となる

#andと orについて
print(a == 20 and c == 30 or b == 10) ##andから評価されるため False or b == 10となる

結果

False
True
False

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?