#題目:分別作 y=2^x , y=(1/2)^x 的函數圖形
#Python 程式碼如下
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
X = np.linspace(-3, 3, 100, endpoint=True)
y1 = np.power(2,X)
y2 = np.power(0.5,X)
plt.figure(figsize=(8,6),dpi=80)
ax = plt.gca() # gca stands for 'get current axis'
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))
plt.plot(X, y1, color="red", linewidth=1.0, linestyle="-", label="$y=2^x$")
plt.plot(X, y2, color="green", linewidth=1.0, linestyle="-", label="$y=(1/2)^x$")
plt.legend(loc='upper left')
plt.show()
# 輸出結果如下:
#題目:分別作 y=2^x , y=(1/2)^x 的函數圖形
# 作 y=2x and y=x^3 的函數圖形
#題目:作 y=2x and y=x^3的函數圖形
#Python 程式碼如下
import numpy as np
import matplotlib.pyplot as plt
# Create a figure of size 8x6 inches, 80 dots per inch
plt.figure(figsize=(8, 6), dpi=80)
# Create a new subplot from a grid of 1x1
plt.subplot(1, 1, 1)
X = np.linspace(-2,2, 256, endpoint=True)
y1=2*X
y2=X**3
plt.xlim(X.min() * 1.2, X.max() * 1.2)
plt.ylim(y2.min() * 1.2, y2.max() * 1.2)
ax = plt.gca() # gca stands for 'get current axis'
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))
# Plot y=2x with a blue continuous line of width 2.5 (pixels)
plt.plot(X, y1, color="blue", linewidth=2.5, linestyle="-", label="y=2x")
# Plot y=x^3 with a green continuous line of width 2.5 (pixels)
plt.plot(X, y2, color="green", linewidth=2.5, linestyle="-", label="y=x^3")
plt.legend(loc='upper right')
plt.show()
# 輸出結果如下:
# 簡單數學運算
#題目:簡單數學運算
#Python 程式碼如下
A=7
B=3
print("A+B=",A+B)
print("A-B=",A-B)
print("A*B=",A*B)
print("A/B=",A/B) #浮點除法
print("A//B=",A//B) #整數(捨去)除法
print("A%B=",A%B) #餘數
print("A**B=",A**B) #乘冪
# 輸出結果如下:
A+B= 10 A-B= 4 A*B= 21 A/B= 2.3333333333333335 A//B= 2 A%B= 1 A**B= 343
# 列出三位數的“水仙花數”
#題目:列出三位數的“水仙花數”,
#所謂“水仙花數”是指一個三位數,其各位數字立方和等於該數本身
#例如:153是一個“水仙花數”,因為153= 1 的三次方+5的三次方+3的三次方。
#Python 程式碼如下
for x in range(10):
for y in range(10):
for z in range(10):
w = x*100 + y*10 + z
if (w>100) and (w == x**3 + y**3 + z**3):
print (w)
# 輸出結果如下:
153 370 371 407
# 輸出一個隨機數
#Python 程式碼如下
import random
print (random.random()) #输入0-1之间的随机数
print (random.uniform(10,20)) #输出10-20之间的随机数
print (random.randint(10,20)) #输出10-20之间的随机整数
print (random.choice([x for x in range(1,100)])) #输出1-99间的随机数
# 輸出結果如下:
0.9481565281460353 13.202517030705241 20 82
訂閱:
文章 (Atom)