id:hirokiky ã§ãã PyQ ã®ã¹ã¿ã³ãã¼ããã©ã³ã§ããã¤ãé¢ç½ã質åãããã ããã®ã§ãããã°ã§ãç´¹ä»ãã¾ãã
ä»åã¯ãPythonã®andã¨orã¯ifæ以å¤ã§ã使ããï¼ãã¨ãã話ã¨ããandã¨ãorã¯å®ã¯æã£ã以ä¸ã«é¢ç½ãåä½ããã¦ãããã¨ãã話ããã¾ãã
ç¥ã£ã¦ããã¨æ··ä¹±ãæ¸ã£ããã便å©ã«ä½¿ããå ´åããã£ãããã¾ãã ä½ããããã°ã©ãã³ã°è¨èªã®ç¾ãããæããããã®ã§ãã²èªãã§ã¿ã¦ãã ããã
- ã¿ã¼ã²ããã«ãªã人
- andã¨orã®åä½ã解説ãã¾ã
- andã¯ã©ããªãï¼
- ãã¼ã«å¤ä»¥å¤ã§ãandãorã¯ä½¿ãã
- ifæã®ä¸ã§ãªãã¦ãandãorã¯ä½¿ãã
- and, orã3ã¤ä»¥ä¸ã§ä½¿ã
- ãã®åä½ã®å©ç¹
- ã¾ã¨ã
ã¿ã¼ã²ããã«ãªã人
å 容ã¨ãã¦ã¯Pythonãå§ããã°ãã ã æ £ãã¦ããä¸ç´ãããã®äººã対象ã«æ¸ãã¦ãã¾ãã
ãããªãæ·±ã話ããã¦åããã«ãããªããªãããã«ããããã«ãææ§ãªè¡¨ç¾ã«ãªã£ã¦ããé¨åãããã¾ãã 詳ãã人ã«ã¨ã£ã¦ã¯ææ§ãªè¡¨ç¾ã«èªããã¨ãããããããããã¾ãããããäºæ¿ãã ããã
ééãã¦ããå ´æãããã°æãã¦ãã ããã
andã¨orã®åä½ã解説ãã¾ã
Pythonã® ifæãªã©ã§ä½¿ã and
㨠or
ããã®ãã¼ã«æ¼ç®åã®åä½ã«ã¤ãã¦è§£èª¬ãã¾ãã
ã¾ããPythonã® or
ã¯ããã¾ãã¯ãã¨ããæ¡ä»¶ã§ä½¿ãæ¼ç®åã§ãããã
A | B | A or B |
---|---|---|
False | False | False |
False | True | True |
True | False | True |
True | True | True |
ãçå´ã ãã§ããçããªããçããã¨è¦ãããã¾ãã
以ä¸ã®ä¾ã§ã¯ True or False
=> True
ã¨ãããã¨ã§ãå¿
ãå®è¡ãããifæã®å¦çãæ¸ãã¦ãã¾ãã
a = True b = False if a or b: print("a or b was True") # ãã®printé¢æ°ã¯å®è¡ããã
orã®æ£ä½ã¯
ã§ããå®ã¯ã or
æ¼ç®åã¯åã«ãã¾ãã¯ããæ±ãæ¼ç®åã§ãªãã以ä¸ã®ãããªåä½ãããæ¼ç®åã§ãã
or
ã®å·¦ããçãã®å ´åãå·¦ã®å¤ãè¿ãor
ã®å·¦ããå½ãã®å ´åãå³ã®å¤ãè¿ã
ä¾ãã° True or False
ã®å ´åã or
ã®å·¦ããçããªã®ã§ãå·¦ã®å¤ True
ãè¿ããã¨ããåä½ã«ãªãã¾ã
ï¼True
ã¯çã¨ãã¦ã False
ã¯å½ã¨ãã¦å¤å®ããã¾ãï¼ã
ä»æ§ã¨ãã¦ç解ãã¦ãããããªãå·¦ã True
ã§å³ã False
ã ãã True
ãè¿ããã¨ããåä½ã§ã¯ããã¾ããã
å°ããããããã§ããããããã以ä¸ã®ããã«ãªãã¾ãã
True or True
: çµæã¯Trueãorã®å·¦ããçãã ãããå·¦ã®å¤Trueãè¿ãã¨åä½ãã¦ãã¾ãTrue or False
: çµæã¯Trueãorã®å·¦ããçãã ãããå·¦ã®å¤Trueãè¿ãFalse or True
: çµæã¯Trueãorã®å·¦ããå½ãã ãããå³ã®å¤Trueãè¿ãFalse or False
: çµæã¯Falseãorã®å·¦ããå½ãã ãããå³ã®å¤Falseãè¿ã
ãã®ããã«åä½ãããã¨ã§ã or
ã¯ãã¼ã«æ¼ç®ã¨ãã¦ã®ãã¾ãã¯ãã¨ããæ©è½ãæãããã¦ãã¾ãã
Pythonå ¬å¼ããã¥ã¡ã³ãã§ã¯ä»¥ä¸ã«æ¸ããã¦ãã¾ãã
https://docs.python.org/ja/3/library/stdtypes.html#boolean-operations-and-or-not
ãçãã¨ããå½ãã£ã¦ä½ï¼ã¨ããæ¹ã¯ãã¡ããåèã«ãã¦ãã ãã
andã¯ã©ããªãï¼
and
㯠True and True
ã®å ´å True
ã§ããã以å¤ã¯ False
ã¨ãããã¼ã«æ¼ç®åã§ããã
A | B | A or B |
---|---|---|
False | False | False |
False | True | False |
True | False | False |
True | True | True |
ãã® and
ã¯ä»¥ä¸ã®ãããªåä½ã«ãªãã¾ãã
and
ã®å·¦ããçãã®å ´åãå³ã®å¤ãè¿ãand
ã®å·¦ããå½ãã®å ´åãå·¦ã®å¤ãè¿ã
ãããå解ãã¦èãã¦ããã¨ããã®åä½ã§ãã¼ã«æ¼ç®ã¨ãã¦ã® and
ã®æ©è½ãæããã¦ããã¾ãã
ãã¼ã«å¤ä»¥å¤ã§ãandãorã¯ä½¿ãã
ããã§å¤ã®ãçãããå½ã㯠True ã False ã§ãªããæååãªã©ã§ãå¤å®ã§ãã¾ãã Pythonã§ã¯æååã®å ´åã1æå以ä¸ã®æååã§ããã°ãçãã0æåã§ããã°ãå½ãã¨æ±ããã¾ãã
"Hello" or "Goodbye"
:"Hello"
ï¼orã®å·¦ããçãã ãããå·¦ã®å¤ãè¿ãï¼"Hello" or ""
:"Hello"
ï¼orã®å·¦ããçãã ãããå·¦ã®å¤ãè¿ãï¼"" or "Goodbye"
:"Goodbye"
ï¼orã®å·¦ããå½ãã ãããå³ã®å¤ãè¿ãï¼"" or ""
:""
ï¼orã®å·¦ããå½ãã ãããå³ã®å¤ãè¿ãï¼
ifæã®ä¸ã§ãªãã¦ãandãorã¯ä½¿ãã
and
ã or
ã®ãã¼ã«æ¼ç®åã¯ifæå
ã§ãã使ãã¾ããããã以å¤ã®å ´æã§ã使ãã¾ãã
ä¾ãã°ä»¥ä¸ã®å¦çã§ã¯ãé¢æ°ã®å¼æ° l
ããªãå ´åã¯ç©ºãªã¹ããè¨å®ããã¨ããå¦çããã¦ãã¾ãã
def print_list(l=None): l = l or [] print(l)
l = l or []
ã¨ããå¦çã§ã l
ãå½ã®å ´åï¼ä¾ãã° None
ã®å ´åï¼ã« []
ãè¨å®ããã¨ããå¦çãã§ãã¾ãã
or
æ¼ç®åã¯ãå·¦ãå½ã§ããã°å³ã®å¤ãè¿ãã¨ããåä½ãããããã§ãã
and, orã3ã¤ä»¥ä¸ã§ä½¿ã
ããã«ãçºå±ãã¦ãã¼ã«æ¼ç®ã3ã¤ç¶ããå ´åãèãã¾ãããã 3ã¤ä»¥ä¸ã®å ´åããé ã«å解ãã¦ããã¨åãããããã§ãããã
string1 = '' string2 = 'Trondheim' string3 = 'Hamer Dance' non_null = string1 or string2 or string3 print(non_null) # çã㯠Trondheim ã«ãªãã¾ã
ã¾ãã string1 or string2
ã®é¨åãã以ä¸ã®ããã«è©ä¾¡ããã¾ã
'' or 'Trondheim'
ãã㧠or
ã®å·¦ãå½ãªã®ã§ãçµæã¯å³ã® 'Trondheim'
ã¨ãªãã¾ãã
ããã«ãã® 'Trondheim'
ã¨ãæ®ãã® or
ãè©ä¾¡ããã¾ã
'Trondheim' or 'Hamer Dance'
ãã㧠or
ã®å·¦ãçãªã®ã§ãçµæã¯å¤ããã 'Trondheim'
ã¨ãªãã¾ãã
ãªã®ã§ã '' or 'Trondheim' or 'Hamer Dance'
ã®çµæ㯠'Trondheim'
ã¨ãªãã¾ãã
ãã®åä½ã®å©ç¹
ãã®ããã«å·¦ã®çµæãè¦ã¦åä½ãããã¨ã§ãç¡é§ãªå¦çãæé¤ã§ããå©ç¹ãããã¾ãã
ä¾ãã°ä»¥ä¸ã®ãããªããã°ã©ã ãããå ´åã or
ã®å·¦ã® can_edit()
é¢æ°ã®çµæã True
ã§ããã°å³ã®é¢æ° can_publish()
㯠å®è¡ãããªããªãã¾ã ã
if can_edit() or can_publish(): ...
or
ã¯ãå·¦ã®çµæãçã®å ´åã«ãã®çµæãè¿ãã®ã§ã or
ã®å³ã®å¦çãå®è¡ããã¾ããã
ãªã®ã§ãä¸ç¨æã« can_publish()
é¢æ°ãå®è¡ãããªãã¨ããå©ç¹ãããã¾ãã
ççé¢ç®ã« or
ã®å·¦ã¨å³ãè©ä¾¡ãã¦ãã True
ã False
ã決ãã¦ããã¨ãç¡é§ãªå¦çãå®è¡ãã¦ãã¾ãå¯è½æ§ãããããã§ã
ï¼å·¦ãçã®æç¹ã§ãå³ãä½ã§ããã㨠or
ã®çµæã¯çãªã®ã§ï¼ã
ã¾ã¨ã
ä»åç´¹ä»ãããã¤ã³ãã¯3ã¤ã§ãã
and
ãor
ã¯çå½ãå ã«å·¦ãå³ã®å¤ãè¿ãå¦çãããand
ãor
ã¯ãã¼ã«å¤ä»¥å¤ã«ã使ããand
ãor
ã¯ifæ以å¤ã§ã使ãã
ç¥ã£ã¦ããã° and
ã or
ããã¾ã使ã£ãå¦çãè¦ãã¨ãã«ãå°ããã¨ã¯ãªãã§ãããã
ããã°ã©ãã³ã°è¨èªã¨ããã®ã¯ãããã§ãã¦ãã¦ãããç¾ãããã®ã ãªãã¨æãã¾ãã