4.pythonの制御フロー

◼︎◼︎コメント

'''
複数行なら「'''」と入力する
'''

# 一行なら「#」と入力する

◼︎◼︎if文

条件文を()で括ることもできるが、IDEA側で警告が出ている。
なので、pythonでは条件文を()に括らなくていいと理解しまいた。

# 10だよ
x: int = 10
if x < 0:
    print('0以下だよ')
elif x == 0:
    print('0だよ')
elif x == 10:
    print('10だよ')
else:
    print('10以上だよ')

'''
a is over 0
b is over 0
'''
a: int = 5
b: int = 10
if a > 0:
    print('a is over 0')
    if (b > 0):
        print('b is over 0')

◼︎◼︎論理演算子

a == b  # aとbが一致
a != b  # aとbが不一致
a < b   # aがbよりも小さい
a > b   # aがbよりも大きい
a <= b  # aがb以下である
a >= b  # aがb以上である


a: int = 5
b: int = 10

# a and b is over 0 (下の2つの式は同じ出力となる)
if a > 0:
    if b > 0:
        print('a and b is over 0')

if a > 0 and b > 0:
    print('a and b is over 0')

# a or b is over 0 (下の2つの式は同じ出力となる)
if a > 0:
    print('a or b is over 0')
elif b > 0:
    print('a or b is over 0')

if a > 0 or b > 0:
    print('a or b is over 0')

◼︎◼︎inとNot

not inの使い方は自然言語っぽいので、理解しやすくて好き。

y = [1, 2, 3]
x = 1

# 1
if x in y:
    print(x)

# not in
if 100 not in y:
    print('not in')

# aとbは不一致
a: int = 1
b: int = 5
if not a == b:
    print('aとbは不一致')

◼︎◼︎ショートハンド

JavaScriptでは普通にショートハンドで条件文を書くので使えて嬉しい。

# True判定 - False判定以外
# False判定 - 0, 0.0, '', [], (), {}, set()

# listは空です
is_not_empty_list: list = []
if is_not_empty_list:
    print('listに何かいます')
else:
    print('listは空です')

◼︎◼︎None

Javaのnullに近いのかな。入力チェックとかでは必須の考えなので、簡単に比較方法をまとめときます。is notを利用する。

is_empty = None

# NoneType
print(type(is_empty))

# None!!!
if is_empty is None:
    print('None!!!')

# 出力なし
if is_empty is not None:
    print('None!!!')

# True (ショートハンドと考えは同じ)
print(1 == True)
# False (isはオブジェクト同士が一致するならTrueと判定する為)
print(1 is True)
# True (isはオブジェクト同士が一致するならTrueと判定する為)
print(None is None)

◼︎◼︎while文

continueだと、残りの処理をせずwhileの最初に戻る
breakだと、残りの処理をせずwhileから抜ける

whileとelseの組み合わせはやってこなかったので、かなり新鮮でした。

’’’
0
1
3
4
’’’
count: int = 0
while True:
    if count >= 5:
        break
    if count == 2:
        count += 1
        continue
    print(count)
    count += 1

’’’
0
1
2
3
4
done
’’’
count: int = 0
while count < 5:
    print(count)
    count += 1
else:
    print('done')

’’’
0
’’’
count: int = 0
while count < 5:
    if count == 1:
        break
    print(count)
    count += 1
else:
    print('done')

◼︎◼︎input関数

簡単な入力と入力チェックの例

'''
Enter Number:1 <- 1と入力
next Number
Enter Number:100 <- 100と入力
'''
while True:
    word: str = input('Enter Number:')
    num: int = int(word)
    if num == 100:
        break
    print('next Number')

◼︎◼︎for

for..inは、Javaの拡張for構文と同じように使えるので使いやすい。
forにもelseを使えるので、少し慣れが必要かも。
enumerate関数を使うことで、indexを取得することができる。

'''
1
2
3
4
5
'''
sapmpleList: list = [1, 2, 3, 4, 5]
for i in sampleList:
    print(i)

'''
a
b
c
d
e
'''
for i in 'abcde':
    print(i)

'''
my
'''
for word in ['my', 'name', 'is','...']:
    if word == 'name':
        break
    print(word)

'''
apple
banana
orange
I ate all.
'''
for fruit in ['apple', 'banana', 'orange']:
    print(fruit)
else:
    print('I ate all.')

'''
apple
stop eating
'''
for fruit in ['apple', 'banana', 'orange']:
    if fruit == 'banana':
        print('stop eating')
        break
    print(fruit)
else:
    print('I ate all.')
'''
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
'''
for _ in range(10):
    print('hello')

'''
0  -  apple
1  -  banana
2  -  orange
'''
for i, fruit in enumerate(['apple', 'banana', 'orange']):
    print(i, ' - ', fruit)
days: list = ['Mon', 'Tue', 'Wed']
fruits: list = ['apple', 'banana', 'orange']
drinks: list = ['coffee', 'tea', 'green tea']

'''
同じ結果になるが、zip関数を使うと綺麗に書ける(読みやすい)
Mon  -  apple  -  coffee
Tue  -  banana  -  tea
Wed  -  orange  -  green tea
'''
for i in range(len(days)):
    print(days[i], fruits[i], drinks[i])

for day, fruit, drink in zip(days, fruits, drinks):
    print(day, ' - ', fruit, ' - ', drink)
d: dict = {'x': 100, 'y': 200}

# dict_items([('x', 100), ('y', 200)])
print(d.items())

'''
x  :  100
y  :  200
'''
for k, v in d.items():
    print(k, ' : ', v)



この記事が気に入ったらサポートをしてみませんか?