Python基礎練習——forサイクル(九九乗算表、図案)
74590 ワード
Forサイクル
for ... inサイクル for変数inシーケンス: for変数1,変数2.in多層シーケンス: for..Inループはelse文を使用できます.
In [1]:
In [7]:
range紹介は、デジタルシーケンス を生成する.具体的な範囲は を設定することができる.注意、pythonでは、一般に数字の範囲を表す2つの数である場合、一般に左の数字を含む右の数字を含まない が含まれる. randintは特例で、彼の左右は を含んでいます. range関数はpython 2とpython 3で深刻な違いがある In [10]:
ループのbreak,contineu,pass break:無条件にサイクル全体を終了し、略称サイクル急死 continue:無条件に今回のサイクルを終了し、新たに次のサイクルに入る pass:省略を表し、通常はステーション に用いられる.
In [12]:
In [18]:
In [20]:
九九乗算表
In [26]:
印刷パターン
In [27]:
In [70]:
In [40]:
In [56]:
In [63]:
In [42]:
文字の印刷
In [67]:
In [69]:
In [21]:
In [1]:
for ... inサイクル
In [1]:
#
list1 = ['aaa',' ',' ',' ']
for i in list1 :
print(i)
aaa
In [7]:
list1 = ['aaa',' ',' ',' ']
for name in list1 :
print(name,end = '')
if name == ' ' :
print(' ')
elif name == ' ':
print(' ')
else :
print(' ')
else :
print(' ')
aaa
range紹介
for i in range(1,5) :
print(i)
1
2
3
4
ループのbreak,contineu,pass
In [12]:
# break
for i in range(0,10) :
print(i)
if i == 4 :
break
0
1
2
3
4
In [18]:
# continue
for i in range(0,10) :
if i % 2 ==1 :
continue
else :
print('{0} '.format(i))
0
2
4
6
8
In [20]:
#pass
for i in range(0,10) :
pass
print('*',end = '')
**********
九九乗算表
In [26]:
#
for i in range(1,10) :
for j in range(1,i+1) :
print('%dX%d=%-2d '%(j,i,i * j),end = ' ')
print()
1X1=1
1X2=2 2X2=4
1X3=3 2X3=6 3X3=9
1X4=4 2X4=8 3X4=12 4X4=16
1X5=5 2X5=10 3X5=15 4X5=20 5X5=25
1X6=6 2X6=12 3X6=18 4X6=24 5X6=30 6X6=36
1X7=7 2X7=14 3X7=21 4X7=28 5X7=35 6X7=42 7X7=49
1X8=8 2X8=16 3X8=24 4X8=32 5X8=40 6X8=48 7X8=56 8X8=64
1X9=9 2X9=18 3X9=27 4X9=36 5X9=45 6X9=54 7X9=63 8X9=72 9X9=81
印刷パターン
In [27]:
# for
for i in range(0,4) :
print('* ' * 5)
* * * * *
* * * * *
* * * * *
* * * * *
In [70]:
# for
for i in range(0,4) :
for j in range(0,5) :
print('* ',end = '')
print()
* * * * *
* * * * *
* * * * *
* * * * *
In [40]:
# for
for i in range(0,4) :
for j in range(0,5) :
if (i == 1 and j ==1) or (i == 1 and j ==2) or (i == 1 and j ==3) or (i == 2 and j ==1) or (i == 2 and j ==2) or (i == 2 and j ==3) :
print(' ',end='')
else :
print('* ',end = '')
print()
* * * * *
* *
* *
* * * * *
In [56]:
# for
# :
for i in range(0,4) :
for j in range(0,5) :
if i ==1 or i == 2 :
if j==1 or j == 2 or j ==3 :
print(' ',end='')
else :
print('* ',end = '')
else :
print('* ',end = '')
print()
#
* * * * *
* *
* *
* * * * *
In [63]:
# for
# :
for i in range(0,4) :
for j in range(0,5) :
if i ==0 or i == 3 or j==0 or j == 4:
print('* ',end='')
else :
print(' ',end = '')
print()
# “*”
* * * * *
* *
* *
* * * * *
In [42]:
#
for i in range(1,6):
for j in range(6-i,6) :
print('* ',end = '')
print()
print()
#
for i in range(1,6):
for j in range(6-i,6) :
if i == 1 or i == 5 or j == 5 or j == 6 - i :
print('* ',end = '')
else :
print(end = ' ')
print()
print()
print('* '*20)
print()
#######################################################
for i in range(1,6):
for j in range(i,6) :
print('* ',end = '')
print()
print()
#
for i in range(1,6):
for j in range(i,6) :
if i == 1 or i == 5 or j == i or j == 5:
print('* ',end = '')
else :
print(end = ' ')
print()
print()
print('* '*20)
print()
#######################################################
for i in range(1,6):
for j in range(1,6) :
if (i ==2 and j ==1) or (i ==3 and j ==1) or (i ==3 and j ==2) or (i ==4 and j ==1) or (i ==4 and j ==2) or (i ==4 and j ==3) or (i ==5 and j ==1) or (i ==5 and j ==2) or (i ==5 and j ==3) or (i ==5 and j ==4) :
print(' ',end = '')
else :
print('* ',end = '')
print()
print()
# :
for i in range(1,6):
for j in range(1,i) :
print(end = ' ')
for k in range(i,6) :
print('* ',end = '')
print()
print()
for i in range(1,6):
for j in range(1,i) :
print(end = ' ')
for k in range(i,6) :
if i == 1 or i == 5 or k == i or k == 5 :
print('* ',end = '')
else :
print(end = ' ')
print()
print()
print('* '*20)
print()
#######################################################
for i in range(1,6):
for j in range(i+1,6) :
print(end = ' ')
for k in range(1,i+1) :
print('* ',end = '')
print()
print()
for i in range(1,6):
for j in range(i+1,6) :
print(end = ' ')
for k in range(1,i+1) :
if i == 1 or i == 5 or k == 1 or k == i :
print('* ',end = '')
else :
print(end = ' ')
print()
print()
print('* '*20)
print()
#######################################################
*
* *
* * *
* * * *
* * * * *
*
* *
* *
* *
* * * * *
* * * * * * * * * * * * * * * * * * * *
* * * * *
* * * *
* * *
* *
*
* * * * *
* *
* *
* *
*
* * * * * * * * * * * * * * * * * * * *
* * * * *
* * * *
* * *
* *
*
* * * * *
* * * *
* * *
* *
*
* * * * *
* *
* *
* *
*
* * * * * * * * * * * * * * * * * * * *
*
* *
* * *
* * * *
* * * * *
*
* *
* *
* *
* * * * *
* * * * * * * * * * * * * * * * * * * *
文字の印刷
In [67]:
#
for i in range(5):
for j in range(0,5 - i) :
print(end = ' ')
for k in range(5 - i,6) :
print('* ',end = '')
print()
print()
print('* '*20)
print()
#
for i in range(5):
for j in range(0,5 - i) :
print(end = ' ')
for k in range(5 - i,6) :
if i == 0 or i == 4 or k == 5 - i or k == 5 :
print('* ',end = '')
else :
print(end = ' ')
print()
print()
*
* *
* * *
* * * *
* * * * *
* * * * * * * * * * * * * * * * * * * *
*
* *
* *
* *
* * * * *
In [69]:
# A
for i in range(5):
for j in range(0,5 - i) :
print(end = ' ')
for k in range(5 - i,6) :
if i == 0 or i == 2 or k == 5 - i or k == 5 :
print('* ',end = '')
else :
print(end = ' ')
print()
*
* *
* * *
* *
* *
In [21]:
# B
for i in range(0,7) :
for j in range(0,3) :
if j == 0 :
print('* ',end = '')
elif i == 0 or i == 3 or i == 6:
if j == 2 :
print(' ',end = '')
else :
print('* ',end = '')
elif i == 1 or i == 2 or i == 4 or i == 5 :
if j == 1:
print(' ',end = '')
else :
print('* ',end = '')
print()
* *
* *
* *
* *
* *
* *
* *
In [1]:
# D
for i in range(0,4) :
for j in range(0,3) :
if j == 0 :
print('* ',end = '')
elif i == 0 or i == 3 :
if j == 2 :
print(' ',end = '')
else :
print('* ',end = '')
elif i == 1 or i == 2 :
if j == 1 :
print(' ',end = '')
else :
print('* ',end = '')
print()
* *
* *
* *
* *