以下為 Jupyter notebook 檔案內容:
In [1]:
# print 函式可以列印字串
# 用雙引號或單引號或三引號圈起的內容就是字串
# 利用 print 列印單一 *
print("*")
In [2]:
# 利用 print 列印3個 *
print("***")
In [3]:
# 利用 print 列印空白與 *
print("*")
print(" *")
print(" *")
In [4]:
# 可以用乘號列印五個 *
print("*"*5)
In [5]:
# for 迴圈可以透過 iterator 逐一執行迴圈內容
for i in range(5):
print(i)
In [6]:
for i in range(1, 5):
print(i)
In [7]:
for i in range(1, 5):
# 只列印不等於 3 的數值
if i != 3:
print(i)
In [8]:
for i in range(1, 5):
# 只列印用 2 除可以整除的偶數
if i %2 == 0:
print(i)
In [9]:
for i in range(1, 5):
# 只列印大於 3 的數值
if i > 3:
print(i)
In [10]:
for i in range(1, 5):
# 只列印大於 3 或者小於 2 的數值
if i > 3 or i < 2:
print(i)
In [11]:
for i in range(1, 7):
# 只列印大於 2 且小於 5 的數值
if i < 5 and i > 2:
print(i)
In [12]:
for i in range(1, 5):
print("*"*i)
In [13]:
for i in range(5, 0, -1):
print("*"*i)
In [14]:
for i in range(1, 5):
# 請利用 help(print) 檢視 print 函式用法
print(" "*(i-1), end="")
print("*")
In [15]:
for i in range(5, 0, -1):
print(" "*(i-1), end="")
print("*")
In [16]:
for i in range(5):
print((5-i-1)*" ", end="")
print("*", end="")
print((2*i-1)*" ", end="")
if i != 0:
print("*", end="")
print((5-i-1)*" ", end="")
print()
for i in range(5-2, -1, -1):
print((5-i-1)*" ", end="")
print("*", end="")
print((2*i-1)*" ", end="")
if i != 0:
print("*", end="")
print((5-i-1)*" ", end="")
print()
In [17]:
def diamond(s):
for i in range(s):
print((s-i-1)*" ", end="")
print("*", end="")
print((2*i-1)*" ", end="")
if i != 0:
print("*", end="")
print((s-i-1)*" ", end="")
print()
for i in range(s-2, -1, -1):
print((s-i-1)*" ", end="")
print("*", end="")
print((2*i-1)*" ", end="")
if i != 0:
print("*", end="")
print((s-i-1)*" ", end="")
print()
diamond(10)
In [18]:
def diamond(s):
for i in range(s):
print((s-i-1)*" ", end="")
print("*", end="")
print((2*i-1)*"*", end="")
if i != 0:
print("*", end="")
print((s-i-1)*" ", end="")
print()
for i in range(s-2, -1, -1):
print((s-i-1)*" ", end="")
print("*", end="")
print((2*i-1)*"*", end="")
if i != 0:
print("*", end="")
print((s-i-1)*" ", end="")
print()
diamond(10)
In [19]:
def diamond(s, p):
for i in range(s):
print((s-i-1)*" ", end="")
print(p, end="")
print((2*i-1)*" ", end="")
if i != 0:
print(p, end="")
print((s-i-1)*" ", end="")
print()
for i in range(s-2, -1, -1):
print((s-i-1)*" ", end="")
print(p, end="")
print((2*i-1)*" ", end="")
if i != 0:
print(p, end="")
print((s-i-1)*" ", end="")
print()
diamond(10,"*")
diamond(5, "a")
In [20]:
def diamond(s, p):
for i in range(s):
print((s-i-1)*" ", end="")
print(p, end="")
print((2*i-1)*p, end="")
if i != 0:
print(p, end="")
print((s-i-1)*" ", end="")
print()
for i in range(s-2, -1, -1):
print((s-i-1)*" ", end="")
print(p, end="")
print((2*i-1)*p, end="")
if i != 0:
print(p, end="")
print((s-i-1)*" ", end="")
print()
diamond(10,"*")
diamond(5, "a")
diamond(5, "s")
In [21]:
def 菱形(n):
數列1 = [x+n for x in range(0, n)]
數列2 = list(range(n, 0, -1))
#print(數列1)
#print(數列2)
數列3 = zip(數列1, 數列2)
for i in 數列3:
for j in range(2*n):
if j == i[0] or j == i[1]:
print("*", end="")
else:
print(" ", end="")
print()
數列4 = [x for x in range(2, n+1)]
數列5 = [x+n-2 for x in range(n, 0, -1)]
#print(數列4)
#print(數列5)
數列6 = zip(數列4, 數列5)
for i in 數列6:
for j in range(2*n):
if j == i[0] or j == i[1]:
print("*", end="")
else:
print(" ", end="")
print()
n = 10
菱形(n)
In [22]:
'''請設法利用上述程式片段, 列印下列字串圖案:
*
**
***
****
*****
****
***
**
*
以及
*
*
*
*
*
*
*
*
*
以及
*
* *
* *
* *
* *
* *
* *
* *
*
* *
* *
* *
* *
* *
* *
* *
*
以及
* *
* *
* *
* *
*
* *
* *
* *
* *
以及
* *
* *
* *
* *
*
* *
* *
* *
* *
* *
* *
* *
*
* *
* *
* *
* *
'''
Out[22]:
Comments
comments powered by Disqus