In [ ]:
for 索引 in range(6):
print(索引)
In [ ]:
from IPython.lib import passwd
passw
In [ ]:
字串資料 = "這是一個字串資料"
print("字串資料的長度為:", len(字串資料))
print("-"*30)
位址 = 0
for 索引 in 字串資料:
print(索引)
print("--- 以上為列印索引 ---")
print(字串資料[位址])
print("--- 以上為列印字串資料數列 ---")
位址 = 位址 + 1
In [ ]:
# 本程式在 demo
# print()
# def
# range()
# list()
# list 內容變化
# reversed()
# zip()
# if elif else
def 列印星號(輸入變數):
數列1 = list(range(輸入變數))
數列2 = [x+輸入變數-1 for x in 數列1]
反數列2 = reversed(數列1)
集合 = zip(數列2, 反數列2)
for 索引 in 集合:
for 數 in range(輸入變數*2):
if 數 == 索引[0] or 數 == 索引[1]:
print("*", end="")
else:
print(" ", end="")
print()
def 列印星號2(輸入變數):
數列1 = list(range(輸入變數))
數列2 = [x+輸入變數 for x in 數列1]
數列3 = [x+1 for x in 數列1]
反數列2 = reversed(數列2)
集合 = zip(數列3, 反數列2)
for 索引 in 集合:
for 數 in range(輸入變數*2):
if 數 == 索引[0] or 數 == 索引[1]:
print("*", end="")
else:
print(" ", end="")
print()
def 列印星號3(輸入變數):
數列1 = list(range(輸入變數))
數列2 = [x+輸入變數-1 for x in 數列1]
反數列2 = reversed(數列1)
集合 = zip(數列2, 反數列2)
for 索引 in 集合:
for 數 in range(輸入變數*2-1):
if 數 <= 索引[0] and 數 >= 索引[1]:
print("*", end="")
else:
print(" ", end="")
print()
def 列印星號4(輸入變數):
數列1 = list(range(輸入變數))
數列2 = [x+輸入變數 for x in 數列1]
數列3 = [x+1 for x in 數列1]
反數列2 = reversed(數列2)
集合 = zip(數列3, 反數列2)
for 索引 in 集合:
for 數 in range(輸入變數*2+1):
if 數 >= 索引[0] and 數 <= 索引[1]:
print("2", end="")
else:
print("1", end="")
print()
def 列印菱形(輸入變數):
列印星號3(輸入變數)
列印星號4(輸入變數-1)
列印菱形(11)
# 請將本程式改為菱形中間也要列印星號
In [ ]:
def 增量列印(行數):
# 基本的 print() 函式列印
# 只要將 4 改為 11, 就可以重複列印 10 行
#for 列印行數 in range(1, 4):
for 列印行數 in range(1, 行數+1):
print("Welcome to Python3 ", end="")
for 列印個數 in range(列印行數):
print("*", end="")
# 完成多星號列印後, 必須額外要求跳行, 否則各行會列在同一行
print()
增量列印(10)
In [ ]:
def 菱形(n):
數列1 = [x+n for x in range(0, n)]
數列2 = list(range(n, 0, -1))
數列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:https://120.113.76.47:8888/notebooks/intro_to_python.ipynb#
print(" ", end="")
print()
數列4 = [x for x in range(2, n+1)]
數列5 = [x+n-2 for x in range(n, 0, -1)]
數列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 = 20
菱形(n)
In [ ]:
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.colors import LogNorm
import matplotlib.pyplot as plt
try:
import numpy as np
except:
exit()
from deap import benchmarks
def bohachevsky_arg0(sol):
return benchmarks.bohachevsky(sol)[0]
fig = plt.figure()
ax = Axes3D(fig, azim = -29, elev = 50)
# ax = Axes3D(fig)
X = np.arange(-15, 15, 0.5)
Y = np.arange(-15, 15, 0.5)
X, Y = np.meshgrid(X, Y)
Z = np.zeros(X.shape)
for i in range(X.shape[0]):
for j in range(X.shape[1]):
Z[i,j] = bohachevsky_arg0((X[i,j],Y[i,j]))
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, norm=LogNorm(), cmap=cm.jet, linewidth=0.2)
plt.xlabel("x")
plt.ylabel("y")
plt.show()
In [ ]:
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.colors import LogNorm
import matplotlib.pyplot as plt
try:
import numpy as np
except:
exit()
from deap import benchmarks
def untuple(sol):
return benchmarks.himmelblau(sol)[0]
fig = plt.figure()
ax = Axes3D(fig, azim = -29, elev = 49)
X = np.arange(-6, 6, 0.1)
Y = np.arange(-6, 6, 0.1)
X, Y = np.meshgrid(X, Y)
Z = np.array(list(map(untuple, zip(X,Y))))
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, norm=LogNorm(), cmap=cm.jet, linewidth=0.2)
plt.xlabel("x")
plt.ylabel("y")
plt.show()