Pyslvs 使用手冊

  • Home
    • Site Map
    • reveal
    • blog
  • 簡介
    • 機構與機器
    • Pyslvs 介面
      • Mechanism
      • Inputs
      • Synthesis
  • 操作
    • 模擬操作
      • 曲柄滑塊
      • 四連桿
    • 尺寸合成操作
    • 構造合成操作
    • 由現有設計進行尺寸合成
  • 下載
  • 分析
    • 三角形表示式
  • 範例
    • 模擬範例
      • Quick return mechanism
      • Watt six-bar linkage
      • Stephenson six-bar linkage
      • Klann linkage
    • 尺寸合成範例
    • 結構合成範例
    • 產品設計範例
  • Python 教學
    • 控制流程
    • 簽章
    • 複製與參照
    • 物件導向
    • 類型註解
  • 開發
    • Windows 開發
      • 環境修正
      • PyInstaller 對 Windows 的支援
    • Ubuntu 開發
      • AppImage 包裝
    • Mac OS X 開發
      • PyInstaller 對 Mac OS X 的支援
    • 參與協同
      • 註解規則
      • 命名規則
      • 類型註解規則
    • Kmol-editor
  • 參考
    • Misc
Python 教學 << Previous Next >> 簽章

控制流程

請學員從下列圖片中的控制流程方法對應 Python 中的語法。

# Condition statement
if condition_1:
    # Do for condition 1.
elif condition_2:
    # Do for condition 2.
else:
    # Do for other case.

# Loops
for i in range(10):
    if i == 4:
        continue
    # Do for 0~9 except 4.

k = 'b'
w = ['a', 'b', 'c', 'd', 'e']
while w:  # Condition: 'w' is not empty.
    if k == w.pop():  # Remove last one and compare it with 'k'.
        break  # Stop the loop if k is equal with last one.
print(w)  # w = ['a']
# note: "for" loop is syntactic sugar based on "while" loop.
for c in ['a', 'b', 'c']:
    if c == 'b':
        continue
    # Do for 'a'~'c' except 'b'.

i = 0
c_list = ['a', 'b', 'c']
while i < len(c_list):  # 'i' is in the range of length of 'c_list'.
    c = c_list[i]  # Get the value from 'c_list'.
    if c == 'b':
        continue
    # Do for 'a'~'c' except 'b'.
    i += 1  # 'i' increase 1.

# Define a function 'func'.
def func(a, b):
    c = int(a)  # Copy 'a' and turn it to an integer, and save it to 'c'.
    if c > 30:  # If 'c' is over 30, set it to 30.
       c = 30
    return b - c  # Return a value that obtained by 'b' minus with 'c'.
    # Garbage collecting for 'a', 'b', 'c'. Except 'b' - 'c'.

# Use the function.
d = func(27.9705, 50)  # 'd' is 23.
d -= func('70', d)  # 'd' minus -7, so 'd' will be set to 30.
func(30.5, d)  # Return value is 0, but it will be discarded and deleted.
# The following statements are the same in the last line of function.
def f(w):
    w.append(w.pop(0))
    # No return statement.

def f(w):
    w.append(w.pop(0))
    return

def f():
    w.append(w.pop(0))
    return None

# The first is recommended.

延伸閱讀 - 執行緒:

Qt 中的 signal 與 slot 為透過多執行緒配置實現的功能。




Python 教學 << Previous Next >> 簽章

Copyright © All rights reserved | This template is made with by Colorlib