七月算法课程《python爬虫》第二课: Python基础

课上记录的一些python基础应用,都是很简单的。

syntax基本语法

a = 1234
print(a)
a = 'abcd'
print(a)

try:
    print(b)
except Exception as e:
    print(e)

a = [1, 2, 3 , 4]

def func(a):
    a[0] = 2

func(a)
print(a)

try:
    # Python 2.x 支持
    print(100, 200, 300)
except Exception as e:
    print(e)

1234
abcd
name 'b' is not defined
[2, 2, 3, 4]
100 200 300

condition_and_loop 循环判断

score = 80
if score > 90:
    print('A')
elif score > 70:
    print('B')
elif score >= 60:
    print('C')
else:
    print('D')

total = 0
i = 1
while i <= 100:
    total += i
    i += 1  # 没有++i或者--i
print(total)

'''
for循环只作用于容器!!!
没有这种写法:
  for (i = 0; i < 100; ++i):
      # TODO
上面这种循环只能用while实现
'''

i = 0
while i < 3:
    j = 0 
    while j <= 3:
        if j == 2:
            j += 1
            continue  # 又去了while j <= 3
        print(i, j)
        j += 1
    i += 1

B
5050
0 0
0 1
0 3
1 0
1 1
1 3
2 0
2 1
2 3

func函数

def hello(who = 'world'):
    print('hello %s!' % (who))

hello()
hello('sea')

# f(x) = x * 5 + 100
# g(x) = x * 5; f(x) = x + 100
# => f(g(x)) = x * 5 + 100
def g(x):
    return x * 5
def f(gf, x):
    return gf(x) + 100
print(f(g, 100))
print(f(lambda x: x * 5, 100))

def f(gf, x, y):
    return gf(x, y) + 100
print(f(lambda x, y: x * y, 100, 200))
hello world!
hello sea!
600
600
20100

ct_list数组

# list就是数组
li = [1, 2, 3, 4, 5]
# 遍历
for i in li:
    # print(i)
    pass
# 用range模拟for (i = 0; i < x; ++i)
# range(x) => [0, x - 1]
# range(x, y) => [x, y - 1]
# range(x, y, z) => [x, x + z,..., < y]
for i in range(len(li)):
    # print(li[i])
    pass

for i in range(1, 10, 2):
    print(i)
1
3
5
7
9
# 负数索引
print(li[-1])
print(li[-2])

# 负数step的range => [x, x - z, ..., > z]
for i in range(3, -1, -1):
    print(i)
5
4
3
2
1
0
# 添加元素
li = []
li.append(1)
li.append(2)
li.append('abc')
li.append(['d', 'e', 'f'])
print(li)

# 按元素添加数组
li = [1, 2]
li_2 = [3, 4, 5]
# 我们想要[1, 2, 3, 4, 5]
# li.append(li_2) => [1, 2, [3, 4, 5]]
li.extend(li_2)
print(li)

# 删除元素
li.pop()    # => [1, 2, 3, 4]
print(li)
li.pop(2)   # => [1, 2, 4]
print(li)
[1, 2, 'abc', ['d', 'e', 'f']]
[1, 2, 3, 4, 5]
[1, 2, 3, 4]
[1, 2, 4]
li = [5, 8, 7, 4, 2, 3]
li.sort()
print(li)
# lambda帮助排序
li = [[5, 2], [3, 8], [2, 11], [7, 6]]
# li.sort(key = lambda x: x[0]) # 参数名字
# 与lamda等价写法
def item_key(x):
    return x[0]
li.sort(key = item_key)
print(li)
[2, 3, 4, 5, 7, 8]
[[2, 11], [3, 8], [5, 2], [7, 6]]

ct_tuple只读数组

# 只读数组
tp = (1, 2, 3)
try:
    tp[0] = 100
except Exception as e:
    print(e)

ct_set没有重复元素的数组

s = set([1, 2, 2, 3, 3, 4, 5])
print(s)
s = set((2, 3, 4, 5, 6, 2, 1, 9))
print(s)
{1, 2, 3, 4, 5}
{1, 2, 3, 4, 5, 6, 9}

ct_dict 字典(哈希表)

# key<->value对应的hash表
di = {'k1': 'v1', 'k2': 'v2'}
di['k3'] = 'v3'
di['k4'] = 'v4'

for k in di:
    print(di[k])

for k, v in di.items():
    print(k, v)
v3
v4
v2
v1
k3 v3
k4 v4
k2 v2
k1 v1

slice数组切片

# [1, 2, 3, 4, 5]
#  => [1, 2, 3]
#  => [3, 4]
li = [1, 2, 3, 4, 5]
li_0_2 =li[0:3] # 0 <= ? < 3
# 等价li[:3]
print(li_0_2)
# [start, end, step] => [start, start + step, ..., < end]
# start默认是0,end默认-1,step默认1
li_last_3 = li[-1:-4:-1]
print(li_last_3)

# 直接用切片反转数组
print(li[::-1])
print(li[-2::-1])

# 切片是复制
li_0_2[-1] = 100
print(li)
[1, 2, 3]
[5, 4, 3]
[5, 4, 3, 2, 1]
[4, 3, 2, 1]
[1, 2, 3, 4, 5]

list_string字符串与数组的关系

s = 'abcdefg'
try:
    str[0] = 'x'
except Exception as e:
    print(e)

# 修改字符串
li = list(s)
# print(li)
li[0] = 'x'
s = ''.join(li)
print(s)
s = '-'.join(li)
print(s)

# 切割
s = 'abc,def,ghi'
p1, p2, p3 = s.split(',')
print(p1, p2, p3)

# 下标访问和切片
s = 'abcdefg'
print(s[0], s[-1])
print(s[2:5])
'type' object does not support item assignment
xbcdefg
x-b-c-d-e-f-g
abc def ghi
a g
cde

obj面向对象

# 用type查看对象类型
print(type([1, 2, 3, 4]))
print(type('abcd'))
print(type({1:2, 2:3}))

# 用dir查看属性和方法
print(dir(list))



['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
class Clazz(object):
    # self参考C++的this指针!
    def __init__(self, x, y):
        self.x = x
        self.y = y

    # 声明成员函数的时候,第一个参数一定是self,不要忘记!
    def display(self):
        print(self.x, self.y)

print(type(Clazz))
clz = Clazz(100, 200)
clz.display()  # => display(clz)

class Base:
    def run(self):
        print('Base::run')

class Tom(Base):
    def run(self):
        print('Tom::run')

t = Tom()
print(isinstance(t, Base))
t.run()

100 200
True
Tom::run
def run(runner):
    runner.run()

class R1:
    def run(self):
        print('R1::run')

class R2:
    def run(self):
        print('R2::run')

run(R1())
run(R2())
R1::run
R2::run

file_rw文件读写

# 打开文件操作可能失败,异常处理后不会造成资源泄露等情况
with open('text.txt', 'w') as f:
    f.write('''1234
    abcd
    nefgh''')

with open('text.txt', 'rb') as f:
    print(f.read())

with open('text.txt') as f:
    for line in f.readlines():
        print(line.strip())
b'1234\r\n    abcd\r\n    nefgh'
1234
abcd
nefgh

threads多线程

import threading

def thread_func(x):
    # 自己加sleep和其它复杂操作看效果
    print('%d\n' % (x * 100))

threads = []
for i in range(5):
    threads.append(threading.Thread(target = thread_func, args = (100, )))

for thread in threads:
    thread.start()

for thread in threads:
    thread.join()
10000

10000

10000

10000

10000

error_handling错误和异常处理

# 错误处理

try:
    r = 10 / 0
except ZeroDivisionError as e:
    print(type(e))
    print(e)
finally:
    # 主要防止资源泄露(服务端。客户端一般不会资源泄露)!
    print('Always come here.')

division by zero
Always come here.
import logging
logging.basicConfig(filename='example.log',level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')

你可能感兴趣的:(Python)