以下為 Jupyter notebook 檔案內容:
In [24]:
import IPython
IPython.version_info
Out[24]:
In [25]:
%install_ext https://raw.github.com/kikocorreoso/brythonmagic/master/brythonmagic.py
In [26]:
%load_ext brythonmagic
In [27]:
%%HTML
<script type="text/javascript" src="http://brython.info/src/brython_dist.js"></script>
In [29]:
%%brython -c my_container
# 假如要列出所產生的 html 則使用 -p
from browser import doc, html
# This will be printed in the js console of your browser
print('Hello world!')
# This will be printed in the container div on the output below
doc["my_container"] <= html.P("文字位於 div 標註內", 
                              style = {"backgroundColor": "cyan"})
In [30]:
%%brython -c canvas_example
from browser.timer import request_animation_frame as raf
from browser.timer import cancel_animation_frame as caf
from browser import doc, html
from time import time
import math
# First we create a table to insert the elements
table = html.TABLE(cellpadding = 10)
btn_anim = html.BUTTON('Animate', Id="btn-anim", type="button")
btn_stop = html.BUTTON('Stop', Id="btn-stop", type="button")
cnvs = html.CANVAS(Id="raf-canvas", width=256, height=256)
table <= html.TR(html.TD(btn_anim + btn_stop) +
                 html.TD(cnvs))
doc['canvas_example'] <= table
# Now we access the canvas context
ctx = doc['raf-canvas'].getContext( '2d' ) 
# And we create several functions in charge to animate and stop the draw animation
toggle = True
def draw():
    t = time() * 3
    x = math.sin(t) * 96 + 128
    y = math.cos(t * 0.9) * 96 + 128
    global toggle
    if toggle:
        toggle = False
    else:
        toggle = True
    ctx.fillStyle = 'rgb(200,200,20)' if toggle else 'rgb(20,20,200)'
    ctx.beginPath()
    ctx.arc( x, y, 6, 0, math.pi * 2, True)
    ctx.closePath()
    ctx.fill()
def animate(i):
    global id
    id = raf(animate)
    draw()
def stop(i):
    global id
    print(id)
    caf(id)
doc["btn-anim"].bind("click", animate)
doc["btn-stop"].bind("click", stop)
In [ ]:
 
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]: