Sign in

f = open(檔名, mode) (mode = r/w/a)

f.close()

file.read(size) or file.read() [空括號是檔案內所有內容, size入數字便只讀取檔案內容首N字)

f.readline() (某行,只能一行)

f.readlines() (檔案內的所有內容,回傳一個list)

f.write()

file.writelines()

print(list1, file = f)

file.seek() 把讀取指標返回原位

file.tell() 回傳指標最後的位置

def 函式名稱(參數):
程式碼

Example:

def check_leap_year(year):
if (year % 400 == 0) or (year % 100 != 0 and year % 4 == 0):
print(“閏年”)
else:
print(“平年”)

year = int(input())
check_leap_year(year)

Normally return 1 value, if want to get 2 or more, use tuple or change the main code is_leap, a = check_leap_year(year)

list = []

list2=list(list1)

my_list[start(開始的index):end(結束的index):sep(間隔)]

  • list[start:end:sep]
  • list[start:]
  • list[:end]
  • list[]
  • list[-1] (-1 = last)

Add/remove/expend element into list

  • my_list.append(object)
  • my_list.insert(position, object)
  • my_list1.extend(my_list2)
  • my_list.remove(object)
  • my_list.pop() (remove the last element)

Sort the item on the list

  • list1.sort() (預設遞增) or list1.sort(reverse=False)
  • list1.sort(reverse=True) (遞減排序)
  • list1 = sorted(list2) (create the new sorted list)
  • list1.reverse() (反轉現有次序)
  • max(), min(), sum()

Find/Search the item on list

  • list_name.index(element)
  • element in list_name (找list內有沒有這個元素, return True or False)
  • for index, item in enumerate(list_name): (output會是(index, item) e.g. (1 Edward) (2 John)
  • enumerate(list_name, start = 1)
  • my_list1 = ‘,’.join(my_list2) (a, b, c, d, e…)
  • text.split() (把字串分割為list e.g. new_list = str.split() 括孤留空會似空白為分割點)
  • input().split() (把輸入分割為list e.g. new_list = input().split())

List Comprehension

  • list = [int(num) for num in input().split()] (將input的文字分割並轉為數字)

for … in … range():

  • for i in range(10):
  • for i in list1: (會列印所有)

while …<>= …:

# standard import (最基本的方式)
import time

# import and give alias (引入模組後,用as幫他取暱稱)
import random as rd

# precise import (使用import … from …的語法,從某套件中拿出特定的功能)
from pathlib import Path

# useless statement
from datetime import *

1) Range()

  • range(stop)
  • range(start:stop)
  • range(start:stop:step)

2) Tuple

3) List[]

4) String’’

5) Dict{}

6) Set

  1. Update pip for Windows
  2. Install requests for Windows
  3. Install Facebook SDK for Windows

python -m pip install — — upgrade pip

python -m pip install requests

python -m pip install facebook-sdk

*Confirm facebook.GraphAPI version in installation message.

  1. print()
  2. print(end=’\n’)
  3. input(‘text’) / int(input(‘text’))
  4. for i in range(start,end+1):
  5. while i == x:
  6. if x==b: / elif x < y: / else:
  7. list = [1, 2, 3, 4, 5] / list = [] / x = list(y)
  8. dict = {key1:value1, key2:value2…} / key will be a string, integer… value is integer only.

Water GKK

Human Leaning

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store