SI100b期末複習

2021-10-08 01:19:39 字數 3841 閱讀 1684

1.print函式

完整語法:print(*objects, sep=' ', end='\n', file=sys.stdout, flush=false)

解釋:

2. escape character    

escape character   

meaning    

example (print())  

output  \''

don\'t do this

don't do this\""

penny said \"hi\"

penny said "hi"\\\

backslash: \\b

backslash: \

\nlinebreak

a\nbab

\ttab

a\tb

a    b

\bbackspace

ab\bc

ac\r

overriding

123456\rxx_xx

xx_xx6

3. 變數命名規則

4.標量

1.int:only signed int;在32位系統裡佔32bits,64位系統裡則佔64bits。 

2.float:有科學計數法e.g.

3.complex:兩種表示方法:+ j

+ j4.type()函式返回變數的型別

5.strings 字串

using raw strings by addingrbefore the first quote without interpretation of escape characters

e.g.print(r'don\'t fo this')

don\'t do this

string 切片[a:b] means [a,b)

string methods

some of the daily used built-in string operations

len()

lower()returns a string with lower case

upper()returns a string with upper case

wd = 'python'

wd.lower()

wd.upper()

replace(old, new)

6.一些等價關係

>>> 1 == 1.0 

true

>>> 1 is 1.0

false

>>> 1 !=1.0

false

>>> 1 is not 1.0

true

>>> none == false

false

7.legb原則

查詢變數名時的順序:local->enclosing->global->built-in

8.函式引數

def example( required_parameter,default_parameter = '',*args,**kw):

pass

引數種類:必選引數,預設引數,可選引數,關鍵字引數

note:預設引數有多個的話,可以不按順序傳參。

可選引數本質上傳進去的是乙個tuple;關鍵字引數本質上傳進去的是乙個dict。

9.call by object

個人認為可以這麼理解,傳進去的是物件的位址的複製,所以在list,dict這樣的mutable物件上進行操作,實際上是會改變其本來的值的;而在str,tuple,int這樣的物件上進行操作,因為其本身immutable的屬性,所以大多進行的是賦值的操作,這個時候,引數不再指向原來的物件,而是指向乙個新的物件,原先物件的值沒有發生任何改變,所以這相當於call by value,不會改變其本身的值。

10. working with files

1.使用open()函式  

note:檔名要保留字尾

f.open('text.txt','w')

f.write('hello')

f.close()

讀取檔案

using thereadline()function to read line by line

usingreadlines()to get a list of remaining lines of the file

note:readline()和read()函式會讀取換行符

11.class 

最好通讀lecture7,都蠻重要的。

12.list.__mul__(self,n)

__mul__(self, n): implementation of * operator

lis.__mul__(n), or lis * n
lis1 = [[1, 2], [3, 4]]

lis2 = lis1 * 2

print(lis2)

#output:[[1, 2], [3, 4], [1, 2], [3, 4]]

13.map(function,iterable,......)

簡單說,就是把function應用到每個iterable上,返回乙個iterator

x = map(lambda x : x ** 2, range(3))

next(x)

>>> 0

next(x)

>>> 1

next(x)

>>> 4

next(x)

>>> stop iteration

14 generator

def foo(): 

print("begin")

for i in range(3):

print("before yield", i)

yield i

print("after yield", i)

print("end")

每次yield之後,generator都會儲存yield之前的資料。

>>> import os 

>>> os.chdir("d:\\test")

>>> from foo import foo

>>> f = foo()

>>> print(f)

>>> next(f)

begin

before yield 0

0 >>> next(f)

after yield 0

before yield 1

1

當generator終止後,會自動呼叫stopiteration

generator有更緊湊的表達方式

x = (i+1 for i in range(5))
(注意與列表生成式區分!!!!)

x = [i+1 for i in range(5)]#列表生成式
15.預設引數的值

lecture9 p41-51

SI100B學習筆記

si100b,即我校的資訊科學與技術 導論 在本學期成為了一門超級加速課程。鑑於我校第一學期程設內容使用 rm python 我覺得還是要來歸納一下。另外吐槽一下我校的迴圈課件,為啥可以在概念b沒講之前就可以說概念a是概念b,太離譜了。以下內容中,rm invoke 統一翻譯為 啟用 rm call...

博文100 第一期

bowen100.top 歡迎你的到來。談談留學生話題 劍橋隱私洩密案 臉書鉅額罰款過關,使用者仍是待宰羔羊 到底什麼是資料中臺?你,技術,自信嗎?周杰倫和蔡徐坤的流量邏輯 tags 社會 觀點 內容簡要 1 從我自己公司業務來說,我們是非常願意僱傭留學生的。發展海外市場,當然希望找到又能中文自由溝...

CCF201509 2 日期計算(100分)

試題編號 201509 2 試題名稱 日期計算 時間限制 1.0s 記憶體限制 256.0mb 問題描述 問題描述 給定乙個年份y和乙個整數d,問這一年的第d天是幾月幾日?注意閏年的2月有29天。滿足下面條件之一的是閏年 1 年份是4的整數倍,而且不是100的整數倍 2 年份是400的整數倍。輸入格...