Python for Data Analysis 2
Python for Data Analysis
第2章 python語法基礎
list.append(obj) 在列表的末尾新增新的物件,可以為字典,列表等
list.count(obj) 統計某個元素在列表中出現的次數
list.extend(*obj) 在列表末尾一次性追加另一個序列中的多個值(用新列表擴充套件原來的列表)
list.index(obj) 從列表中找出某個值第一個匹配項的索引位置
list.insert(index,obj) 將物件插入列表,第一個引數可以是位置
list.pop(obj=list[-1] 移除列表中的一個元素(預設最後一個元素),並返回該元素的值
list.remove(obj) 移除列表中某個值的第一個匹配項
list.reverse() 反向列表中的元素
list.sort[func] 對原列表進行排序,引數reverse=True時,從大到小排序
list.clear() 清空這個列表(感覺毫無卵用…)
# 自省
a = [1,2,3]
a?
Type: list String form: [1, 2, 3] Length: 3 Docstring: list() -> new empty list list(iterable) -> new list initialized from iterable’s items
print?
Docstring: print(value, …, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream. Type: builtin_function_or_method
import numpy as np
np.*load*?
np.loader np.load np.loads np.loadtxt np.pkgload
a = [1,2,3]
b = a
c=a.copy()
a.append(4)
print(a,b,c)
[1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3]
a = [1,2,3]
b = a
c=a.copy()
print(a,b,c)
print(a is b)
print(a is c)
print(a == b)
print(a == c)
[1, 2, 3] [1, 2, 3] [1, 2, 3]
True
False
True
True
print(5 / 2)
print(5 // 2) #結果取整
print(5 ** 2)
2.5
2
25
# list, array, dictionary型別可變
# tuple型別不可變
a = [1,2,3]
a[2] = 4
a
[1, 2, 4]
a[2] = (3,5)
a
[1, 2, (3, 5)]
# date time
from datetime import datetime, date, time
dt = datetime(2018, 5, 8, 19, 59, 1) # tuple不可變
print(dt.day)
print(dt.minute)
print(dt.date())
print(dt.time())
print(dt.strftime('%m/%d%Y %H:%M'))
print(datetime.strptime('20000102', '%Y%m%d'))
8
59
2018-05-08
19:59:01
05/082018 19:59
2000-01-02 00:00:00
print(range(10))
range(0, 10)
print(list(range(10)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
控制語句:if, else, elif, for, while, pass