python知识集合

1.list

list是一种有序的集合


例子:classmates = ['Michael', 'Bob', 'Tracy'];

方法:
1. len

len(classmates) //3

2.append

classmates.append('Adam') //['Michael', 'Bob', 'Tracy', 'Adam']


3.insert 插入元素到指定位置

classmates.insert(1, 'Jack') //['Michael', 'Jack', 'Bob', 'Tracy', 'Adam']


4.pop()

要删除list末尾的元素,用pop()方法

classmates.pop()

5.
classmates.pop(1)

要删除指定位置的元素,用pop(i)方法,其中i是索引位置:

======================================

tuple

tuple一旦初始化就不能修改

获得元素的方法同list,用下标获得


=======================================

循环

names = ['Michael', 'Bob', 'Tracy']
for name in names:
print(name)

=====================================

range和list

range()函数,可以生成一个整数序列,再通过list()函数可以转换为list。比如range(5)生成的序列是从0开始小于5的整数:

>>> list(range(5))
[0, 1, 2, 3, 4]


=====================================

dic 字典

d = {'Michael': 95, 'Bob': 75, 'Tracy': 85}

读取
d['Michael'] //95

写入
d['Adam'] = 67

判断是否存在某元素

'Thomas' in d

删除某元素

d.pop('Bob')

=======================================

set

set和dict类似,也是一组key的集合,但不存储value。由于key不能重复,所以,在set中,没有重复的key

=====================================

函数

1.强制转换,同php

2.函数的定义

def my(a):
if a>=0:
print(a)
else:
print(-a)

my(-2)


3.检查数据类型
print(isinstance(1,(int)) //true


4.可变参数


在Python函数中,还可以定义可变参数。顾名思义,可变参数就是传入的参数个数是可变的,可以是1个、2个到任意个,还可以是0个


def calc(*numbers):
sum = 0
for n in numbers:
sum = sum + n * n
return sum

====================================

切片

L = ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack']

L[0:3] //['Michael', 'Sarah', 'Tracy']

====================================

列表生成器

list(range(1, 11)) //[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


装饰器和偏函数

=======================================

模块

安装pip

http://stackoverflow.com/questions/22531360/no-module-named-setuptools


pip安装beautifulsoup
http://stackoverflow.com/questions/19957194/install-beautiful-soup-using-pip


htmlparser

http://stackoverflow.com/questions/34630669/python-importerror-no-module-named-htmlparser

原文地址:https://www.cnblogs.com/norm/p/6697415.html