1. 接收用戶輸入
    • 使用input方法
      1
      temp = input('Guess a number')

字符串語法

  1. 原始字符串r
    • 在字符串的前面加上一個r,則表示原始字符串,轉譯字符則不再有效
      1
      print(r"Hello World \n")    # Hello World \n
  1. 長字符串
    • 專門用於實現跨行字符串
      1
      2
      3
      4
      5
      6
      x = """
      Hello World 1
      Hello World 2
      Hello World 3
      Hello World 4
      """
  1. 字符串乘法複製
    • 將字符串重複指定次數
      1
      print('HelloWorld ' * 3)    # HelloWorld HelloWorld HelloWorld 

random module

  1. 生成隨機整數:
    • random.randint(a,b)隨機生成 a - b 的函數
      1
      random.randint(1, 10)
  1. 獲取偽隨機數攻擊:
    • 使用random.getstate()獲取隨機數種子加工後隨機數生成器內部狀態
    • 使用random.setstate()設置隨機數生成器的內部狀態
    • 具體編碼:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      x = random.getstate()
      random.randint(1, 10) # 2
      random.randint(1, 10) # 3
      random.randint(1, 10) # 4
      random.randint(1, 10) # 10

      random.setstate(x)
      random.randint(1, 10) # 2
      random.randint(1, 10) # 3
      random.randint(1, 10) # 4
      random.randint(1, 10) # 10

數字類型 Number

  1. 整數型:
    • python int類型沒有大小限制
  1. 浮點型:
    • 基於IEEE-745算法,因此會出現誤差
      1
      print(0.1 + 0.2)    # 0.30000000000000004
  1. decimal module 精確計算:
    • 使用decimal.Decimal('x')聲明
      1
      2
      3
      a = decimal.Decimal('0.1')
      b = decimal.Decimal('0.2')
      print(a + b) # 0.3
  1. 科學計數法:
    • python會使用科學計算法來儲存小數點數字
      1
      print(0.00005)  # 5e-05
  1. 複數 Complex Number:
    • 以浮點型儲存
      1
      2
      3
      x = 1 + 2j
      x.real # 1.0
      x.imag # 2.0
  1. Calculation
    | Operations | Descriptions |
    | :—-: | :—-: |
    | x + y | Addition |
    | x - y | Subtraction |
    | x * y | Multiplication |
    | x / y | Division |
    | x // y | Quotient |
    | x % y | Remainder |
    | -x | x reverses |
    | +x | x itself |
    | abs(x) | Absolute value |
    | int(x) | Turn x into an integer |
    | float(x) | Turn x into a float |
    | complex(re, im) | Return a complex number |
    | x.conjugate() | x’s conjugate |
    | divmod(x, y) | Return (Quotient, Remainder) |
    | pow(x, y) | x ^ y |
    | x ** y | x ^ y |

布爾值 Boolean

  1. False的情況:
    1. 定義為False的對象:None, False
    2. 數值為0的數字類型:0, 0.0, 0j, Decimal(0), Fraction(0, 1)
    3. 空的序列和集合:"", (), [], {}, set(), range(0)
    • 具體編碼:
      1
      2
      3
      4
      5
      bool(123)   # True
      bool(' ') # True
      bool('') # False
      bool(0) # False
      bool(False) # False
  1. 邏輯運算符:
    • and, or, not
      1
      2
      3
      3 < 4 and 4 < 5     # True
      3 < 4 or 6 < 5 # True
      not 3 < 4 # False
  1. 短路求值 Short-Circuit Evaluation:
    • 從左往右,只當第一個操作數的值無法確定邏輯運算結果時,才對第二個操作數進行求值
      1
      2
      3
      4
      3 and 4     # 4
      3 or 4 # 3
      (not 1) or (1 and 0) or (3 and 4) or (5 and 6) or (7 and 8 and 9) # False or 0 or 4 or 6 or 9 == 4
      not 1 or 0 and 1 or 3 and 4 or 5 and 6 or 7 and 8 and 9 # False or 0 or 4 or 6 or 9 == 4
  1. 運算符優先級 Precedence of Operators:
    | Operator | Meaning |
    | :—-: | :—-: |
    | () | Parentheses |
    | ** | Exponent |
    | +x, -x, ~x | Unary plus, Unary minus, Bitwise NOT |
    | *, /, //, % | Multiplication, Division, Floor division, Modulus |
    | +, - | Addition, Subtraction |
    | <<, >> | Bitwise shift operators |
    | & | Bitwise AND |
    | ^ | Bitwise XOR |
    | \| | Bitwise OR |
    | ==, !=, >, >=, <, <=, is, is not, in, not in | Comparisons, Identity, Membership operators |
    | not | Logical NOT |
    | and | Logical AND |
    | or | Logical OR |

條件語句

  1. 比較運算符:
    | Symbol | Meaning |
    | :—-: | :—-: |
    | < | smaller than |
    | <= | smaller than or equal to |
    | > | greater than |
    | >= | greater than or equal to |
    | == | equal to |
    | != | not equal |
    | is | Objects’ id are equal |
    | is not | Objects’ id are not equal |
  1. if-else語句:
    • 使用縮進調整執行的語句
      1
      2
      3
      4
      5
      6
      if x < 8:
      print('smaller than 8')
      elif x == 8:
      print('equal to 8')
      else:
      print('greater than 8')
  1. if-else表達式:
    • 將if-else語句縮減成一行的表達式
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      # if-else 語句
      if 0 <= score < 60:
      level = 'D'
      elif 60 <= score < 80:
      level = 'C'
      elif 80 <= score < 90:
      level = 'B'
      else:
      level = 'A'

      # if-else 表達式
      level = ('D' if 0 <= score < 60 else
      'C' if 60 <= score < 80 else
      'B' if 80 <= score < 90 else
      'D')

循環結構

while循環

  1. while循環語句:
    • 語法:while condition:
      1
      2
      3
      4
      count = 10
      while count > 0:
      print(count)
      count -= 1
    • break語句:跳出此次循環
    • continue語句:跳到下次循環
  1. while-else循環語句:
    • 當不滿足while條件時執行else語句
      1
      2
      3
      4
      5
      6
      count = 10
      while count > 0:
      print(count)
      count -= 1
      else:
      print('count is not greater than 0')
    • 由於else是位於while語句中,因此break跳出while循環並不會執行else語句
  1. 循環嵌套 nested loop
    • while循環中嵌套第二組while循環
      1
      2
      3
      4
      5
      6
      7
      8
      i = 1
      while i <= 9:
      j = 1
      while j <= i:
      print(j, ' * ', i, ' = ', i*j, end='\t') # end 表示末尾字符
      j += 1
      print()
      i += 1
    • break和continue只能作用於一層循環體

for循環

  1. for循環語句:
    • 語法:for variable in iterableObject
    • 可迭代對象 Iterable Object: String, Array, Object, range()
  1. 遍歷字符串String:
    • variable表示字符串中每一個字符
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      str = 'Hello World'

      # for實現
      for char in str:
      print(char, end = ' ') # H e l l o W o r l d

      # while實現
      i = 0
      while i < len(str):
      print(str[i])
      i += 1
  1. 遍歷次數:
    • 使用range(start, stop, step)函數返回一個可迭代的對象
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      # 指定結束 stop
      for i in range(5):
      print(i, end = ' ') # 0 1 2 3 4

      # 指定開始於結束 start & stop
      for i in range(5, 10):
      print(i, end = ' ') # 5 6 7 8 9

      # 指定跨度 step
      for i in range(5, 10, 2):
      print(i, end = ' ') # 5 7 9
  1. 實用效果:
    • 遍歷所有質數
      1
      2
      3
      4
      5
      6
      7
      for n in range(2, 100):
      for x in range(2, n):
      # 如果其中有可被整除則不是質數,跳出循環
      if n % x == 0:
      break
      else:
      print(n, end = ' ')