进入python_进入Python世界——Python基础知识

#-*- coding: utf-8 -*-

importrandomimportreimportrequestsfrom bs4 importBeautifulSoup

# 爬取糗事百科的首页内容defqiushibaike():

content= requests.get('http://www.qiushibaike.com/').content

soup= BeautifulSoup(content, 'html.parser')#print(soup.get_text())

for div in soup.find_all('div', {'class': 'foot'}):print(div.text.split())#for div in soup.find_all('div', {'class': 'content'}):

# 字符串常见处理方法

defdemo_string():

stra= 'hello world'

print(stra.capitalize())print(stra.replace('world', 'victor'))

strb= '\n\rhello world \r\n'

print(0, strb)print(1, strb.lstrip())print(2, strb.rstrip(), 'xx')

strc= 'hello w'

print(3, strc.startswith('hel'))print(4, strc.endswith('w'))print(5, stra + strb +strc)print(6, len(stra), len(strb), len(strc))print(7, '-'.join(['a', 'b', 'c'])) #a-b-c

print(8, strc.split(' '))print(9, strc.find('llo'))

# 运算符操作defdemo_operation():print(1, 1 + 2, 5 / 2, 5 * 2, 5 - 2)print(2, 1 + 2, 5.0 / 2, 5 * 2, 5 - 2)print(3, True, not True, False, notFalse)print(4, 1 << 2, 88 >> 2)print(5, 1 < 2, 5 > 3)print(6, 5 & 3, 5 ^ 3, 5 | 3)

x= 3y= 5.0

print(7, x, y, type(x), type(y))

# 函数defdemo_buildinfunction():print(1, max(1, 2), min(5, 3))print(2, len('xxx'), len([3, 4, 5]))print(3, abs(-2), abs(7))print(4, range(1, 10, 2))#print(5, '\n'.join(dir(list)))

x = 2

print(6, eval('x+3'))print(7, chr(65), ord('a'))print(8, divmod(11, 3))

# 控制流defdemo_controlflow():

score= 65;if score > 99:print(1, 'A')elif score > 60 & score <= 99:print(2, 'B')else:print(3, 'C')while score < 100:print(4, score)

score+= 10

if score > 80:break

for i in range(0, 10):if i ==0:pass

if i == 3:continue

if i < 5:print(5, i*i)if i == 7:break

# 列表

defdemo_list():

lista= [1, 2, 3]print(1, lista)#print(dir(list))

listb = ['a', 1, 1.0, 4, 2]print(2, listb)

lista.extend(listb)print(3, lista)print(4, len(lista))print(5, 'a' in lista, 'b' inlistb)

lista+=listbprint(lista)

listb.insert(0,'www')print(listb)

listb.pop(1)print(listb)#listb.sort()

print(listb)print(6, listb[0], listb[1], listb[2])print(7, [0] * 10)print(8, listb * 2)

listb.reverse()print(9, listb)

t= (1, 2, 3,1)print(10, t)#print(11, t.count())

print(11, t.count(1), len(t))

# 字典defdemo_dict():

dicta= {4 : 16, 1 : 1, 2 : 4, 3 : 9, 'a' : 'b'}print(1, dicta)print(2, dicta.keys(), dicta.values())for key, value, indicta.items():print(key, value)for key indicta.keys():print(key)#2.0版本是has_keys()方法

print(3, dicta.__contains__(1), dicta.__contains__(11))

dictb= {'+': add, '-': sub}print(4, dictb.get('-')(5, 3))print(4, dictb['+'](1, 2))print(5, dictb.__contains__('+'))del dictb['+']print(5, dictb.__contains__('+'))print(5, dictb)

dictb.pop('-')print(6, dictb)

dictb['x'] = 'y'

print(7, dictb)defadd(a, b):return a +bdefsub(a, b):return a -b#集合

defdemo_set():

lista= [1, 2, 3]

lista1= (1, 2, 3)

seta=set(lista)

seta1=set(lista1)print(1, seta)print(1, seta1)

setb= set((2, 3, 4))print(2, seta.intersection(setb))print(3, seta &setb)print(4, seta |setb, seta.union(setb))print(5, seta - setb, setb -seta)

seta.add('xxx')print(6, seta)print(7, len(seta))print(8, seta.isdisjoint(set((1, 2))))print(9, 1 inseta)

# 类classUser:

type= 'USER'

def __init__(self, name, uid):

self.name=name

self.uid=uiddef __repr__(self):#toStirng()

return 'i am' + self.name + ' ' + str(self.uid) + ' ' +self.type#继承

classGuest(User):

type= 'GUEST'

def __repr__(self):return "I am guest:" + self.name + ' ' + str(self.uid) + ' ' +self.typeclassAdmin(User):

type= 'ADMIN'

def __init__(self, name, uid, group):

User.__init__(self,name, uid)

self.group=groupdef __repr__(self):return 'I am admin:' + self.name + ' ' + str(self.uid) + ' ' + self.group + ' ' +self.typedefcreate_user(type):if type == 'USER':return User('u1', 1)elif type == 'ADMIN':return Admin('a1', 1, 'shu')else:return Guest('g1', 1)

# 异常defdemo_exception():try:print(2/1)#print(2/0)

raise Exception('Raise Error', 'xxx')exceptException as e:print('error', e)finally:print('clean up')defdemo_obj():

user1= User('victor', 1)print(user1.__repr__())print(user1)

guest= Guest('xunuo', 2)print(guest)

admin= Admin('Qingde', 2, 'shanghai university')print(admin)print(create_user('ADMIN'))defdemo_random():

random.seed(11)for i in range(0, 5):print(1, random.randint(0,100))print(2, int(random.random()*100))print(3, random.choice(range(0, 100, 5))) #抽奖

print(4, random.sample(range(0, 100, 10), 4)) #抽几个

lista= [1, 2, 3, 4, 5]

random.shuffle(lista)print(lista)defdemo_regex():

str= 'abc123def12gh15'p1= re.compile('[\d]+')

p2= re.compile('\d')print(1, p1.findall(str))print(2, p2.findall(str))

str1= 'axxx@163.com, bsad@google.com, cdd@qq.com, dasd@qq.com, eda@163.com'p3= re.compile('[\w]+@[163|qq]+\.com') #不能有空格

print(3, p3.findall(str1))

str= 'title

content'p4= re.compile('[^')print(4, p4.findall(str))

p4= re.compile('[^[^')print(5, p4.findall(str))

str= 'xx2017-06-06zzz'p5= re.compile('\d{4}-\d{2}-\d{2}')print(6, p5.findall(str))if __name__ == '__main__':#print('hello world')

qiushibaike()#demo_string()

#demo_operation()

#demo_buildinfunction()

#demo_controlflow()

#demo_list()

#demo_dict()

#demo_set()

#demo_obj()

#demo_exception()

#demo_random()

#demo_regex()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值