Python の基本構文

updated Jul 19, 2021created Jul 22, 2021

参考

配列

py
list = ['A', 'B', 'C'] print(list[0]) # 'A'

dict

py
d = {'k1': 1, 'k2': 2, 'k3': 3} print(d) # {'k1': 1, 'k2': 2, 'k3': 3} print(d['k1']) # 1

型出力

py
docs = websites_ref.stream() print(type(docs)) # <class 'generator'>

NOTE: イテレータとジェネレータが存在する。

条件式 or

py
og_description = "A" description = "B" answer = og_description or description or "" print(answer) # "A" og_description = "" description = "B" answer = og_description or description print(answer) # "B" og_description = None description = "B" answer = og_description or description print(answer) # "B" og_description = None description = None answer = og_description or description print(answer) # None og_description = None description = None answer = og_description or description or "" print(answer) # ""

条件式 not

py
if not is_existed:

三項演算子

py
a = {True時の処理} if {条件式} else {False時の処理}

URL取得

py
from urllib.parse import urlparse parsed_url = urlparse("https://nishimura.club/") article_id = hash_text(parsed_url.geturl())

import

py
from urllib.request import urlopen from urllib.parse import urlparse