【pythonベース】6-制御構造
5827 ワード
じょうけんさ
>>> num = 5
>>> num > 2
True
>>> num > 3 and num <= 5
True
>>> 3 < num <= 5
True
>>> num % 3 == 0 or num % 5 == 0
True
>>> fav_fiction = 'Harry Potter'
>>> fav_detective = 'Sherlock Holmes'
>>> fav_fiction == fav_detective
False
>>> fav_fiction == "Harry Potter"
True
>>> bool(num)
True
>>> bool(fav_detective)
True
>>> bool(3)
True
>>> bool(0)
False
>>> bool("")
False
>>> bool(None)
False
>>> if -1:
... print("-1 evaluates to True in condition checking")
...
-1 evaluates to True in condition checking
in
オペレータの使用>>> def num_chk(n):
... if n == 10 or n == 21 or n == 33:
... print("Number passes condition")
... else:
... print("Number fails condition")
...
>>> num_chk(10)
Number passes condition
>>> num_chk(12)
Number fails condition
もう1つの
>>> def num_chk(n):
... if n in (10, 21, 33):
... print("Number passes condition")
... else:
... print("Number fails condition")
...
>>> num_chk(12)
Number fails condition
>>> num_chk(10)
Number passes condition
(10, 21, 33)
はメタグループデータ型であり、後述する章でif
#!/usr/bin/python3
num = 45
# if
if num > 25:
print("Hurray! {} is greater than 25".format(num))
# if-else
if num % 2 == 0:
print("{} is an even number".format(num))
else:
print("{} is an odd number".format(num))
# if-elif-else
# elif
if num < 0:
print("{} is a negative number".format(num))
elif num > 0:
print("{} is a positive number".format(num))
else:
print("{} is neither postive nor a negative number".format(num))
:
()
はオプションの$ ./if_elif_else.py
Hurray! 45 is greater than 25
45 is an odd number
45 is a positive number
if-elseを条件オペレータとして
#!/usr/bin/python3
num = 42
num_type = 'even' if num % 2 == 0 else 'odd'
print("{} is an {} number".format(num, num_type))
?:
条件オペレータがありません.if-else
の使用は、トランスミッション方法$ ./if_else_oneliner.py
42 is an even number
for
#!/usr/bin/python3
number = 9
for i in range(1, 5):
mul_table = number * i
print("{} * {} = {}".format(number, i, mul_table))
range
関数を用いて実現することができるstart=0
、step=1
、stop
の値$ ./for_loop.py
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
while
#!/usr/bin/python3
#
usr_string = 'not a number'
while not usr_string.isnumeric():
usr_string = input("Enter a positive integer: ")
$ ./while_loop.py
Enter a positive integer: abc
Enter a positive integer: 1.2
Enter a positive integer: 23
$
continueとbreak
continue
およびbreak
キーワードは、いくつかの条件下で通常のループ動作を変更するために使用されるcontinue-ループコードブロックの残りの文をスキップし、次の反復に進みます.
#!/usr/bin/python3
prev_num = 0
curr_num = 0
print("The first ten numbers in fibonacci sequence: ", end='')
for num in range(10):
print(curr_num, end=' ')
if num == 0:
curr_num = 1
continue
temp = curr_num
curr_num = curr_num + prev_num
prev_num = temp
print("")
continue
は、複雑なコードストリームcontinue
の使用のみを示し、ここでよりPython化された操作方法$ ./loop_with_continue.py
The first ten numbers in fibonacci sequence: 0 1 1 2 3 5 8 13 21 34
break-ループコードブロックの残りの文(ある場合)をスキップし、ループコードブロックを終了します.
#!/usr/bin/python3
import random
while True:
# range 500
random_int = random.randrange(500)
if random_int % 4 == 0 and random_int % 6 == 0:
break
print("Random number divisible by 4 and 6: {}".format(random_int))
while True:
は、無限ループstart, stop, step
パラメータ$ ./loop_with_break.py
Random number divisible by 4 and 6: 168
$ ./loop_with_break.py
Random number divisible by 4 and 6: 216
$ ./loop_with_break.py
Random number divisible by 4 and 6: 24
このwhile_loop.py例は
break
文で書き換えることができる>>> while True:
usr_string = input("Enter a positive integer: ")
if usr_string.isnumeric():
break
Enter a positive integer: a
Enter a positive integer: 3.14
Enter a positive integer: 1
>>>
continue
およびbreak
は、中間層の対応するサイクル