本文是根据以下两个博客基础上,把整个环境与开发平台搭建好之后,所学习的代码。全程是按照 牛客网上面那个初级项目所指导的那样,一步一步来。
虽然视频仅仅只有1个小时,但是实际操作的时候,我自己是花费了大概3天的时间,才掌握熟悉这样的知识,具体深入部分必须等以后进一步需求时候才能用的着这些。
整个运行环境都是基于pycharm中运行的。
Python简介
语法基础
控制流
数据结构
面向对象
异常
随机数/正则表达式
__author__ = 'wdd'
# print "hello world!"
# print "fuck u!"
import requests
import random
import re
from bs4 import BeautifulSoup
content = requests.get('https://user.qzone.qq.com/1125577106/mood').content
soup = BeautifulSoup(content, 'html.parser')
for div in soup.find_all('div', {'class': 'content'}):
print div.text.strip()
def demo_string():
stra = 'hello world'
print stra.capitalize()
print stra.replace('world', 'nowcoder')
strb = ' \n\rhello nowcoder \r\n'
print 1, strb.lstrip()
print 2, strb.rstrip()
strc = 'hello w'
print 3, strc.startswith('hel')
print 4, strc.endswith('x')
print 5, stra + strb + strc
print 6, len(strc)
print 7, '-'.join(['a', 'b', 'c'])
print 8, strc.split(' ')
def demo_operation():
print 1, 1 + 2, 5 / 2, 5 * 2, 5 - 2
print 2, True, not True
print 3, 1 < 2, 5 > 2
print 4, 2 << 3
print 5, 5 | 3, 5 & 3, 5 ^ 3
x = 2
y = 3.3
print x, y, type(x), type(y)
def demo_buildinfunction():
print 1, max(2, 1), min(5, 3)
print 2, len('xxx'), len([1, 2, 3])
print 3, abs(-2) # fabs, Math.fabs
print 4, range(1, 10, 3)
print 5, dir(list)
x = 2
print 6, eval('x + 3')
print 7, chr(65), ord('a')
print 8, divmod(11, 3)
def demo_contronflow():
score = 65
if score > 99:
print 1, 'A'
elif score > 60:
print 2, 'B'
else:
print 3, 'C'
while score < 100:
print score
score += 10
score = 65
# for (int i = 0; i < 10; ++i)
# continue, break, pass
for i in range(0, 10, 2):
if i == 0:
#print 3, i
pass #do_special
if i < 5:
continue
print 3, i
if i == 6:
break
def demo_list():
lista = [1, 2, 3] # vector Arraylist
print 1,lista
listb = ['a', 1, 'c', 1.1]
print 2, listb
lista.extend(listb)
print 3, lista
print 4, len(lista)
print 5, 'a' in listb
lista = lista + listb
print 6, lista
listb.insert(0,'www')
print 7, listb
listb.pop(1)
print 8, listb
listb.reverse()
print 9, listb
print 10, listb[0], listb[1]
listb.sort()
print 11, listb
listb.sort(reverse=True)
print 12, listb
print 13, listb * 2
print 14, [0] * 14
tuplea =(1, 2, 3)
listaa = [1, 2, 3]
listaa.append(4)
print 15, listaa
def add(a, b):
return a + b
def sub(a, b):
return a - b
def demo_dict():
dicta = {4: 16, 1: 1, 2: 4, 3: 9}
print 1, dicta
print 2, dicta.keys(), dicta.values()
print 3, dicta.has_key(1), dicta.has_key('3')
for key, value in dicta.items():
print 'key-value:', key, value
# for map(int ,int)::iterator it = x.begin(); it ! = x.end(); it++
dictb = {'+': add, '-': sub}
print 4, dictb['+'](1, 2)
print 5, dictb.get('-')(15, 3)
dictb['*'] = 'x'
print dictb
dicta.pop(4)
print 6,dicta
del dicta[1]
print 7, dicta
def demo_set():
seta = set((1, 2, 3)) # lista = [1, 2, 3] seta = set(liata)
setb = set((2, 3, 4))
print 1, seta
# seta.add(4)
# print 2, seta
print 3, seta.intersection(setb), seta & setb
print 4, seta | setb, seta.union(setb)
print 5, seta - setb
seta.add('x')
print 6, seta
print len(seta)
print seta.isdisjoint(set((1, 2)))
class User:
type = 'USER'
def __init__(self, name, uid):
self.name = name
self.uid = uid
def __repr__(self):
return 'im ' + self.name + ' ' + str(self.uid)
class Admin(User):
type = 'ADMIN'
def __init__(self, name, uid, group):
User.__init__(self, name, uid)
self.group = group
def __repr__(self):
return 'im ' + self.name + ' ' + str(self.uid) + ' ' + self.group
class Guest(User):
def __repr__(self):
return 'im guest:' + self.name + ' ' + str(self.uid)
def create_user(type):
if type == 'USER':
return User('u1', 1)
elif type == 'ADMIN':
return Admin('a1', 101, 'g1')
else:
return Guest('gu1', 201)
# raise ValueError('error')
def demo_exception():
try:
print 2 / 1
# print 2 / 0
# if type == 'c':
raise Exception('Raise Error', 'NowCoder')
except Exception as e:
print 'error:', e
finally:
print 'clean up'
def demo_random():
# 1 - 100
# random.seed(1)
# x = prex * 100007 % xxxx
# prex = x
print 1, int(random.random() * 100)
print 2, random.randint(0, 200)
print 3, random.choice(range(0, 100, 10))
print 4, random.sample(range(0, 100), 4)
a = [1,2,3,4,5]
random.shuffle(a)
print 5, a
def demo_re():
str = 'abc123def12gh15'
p1 = re.compile('[\d]+')
p2 = re.compile('\d')
print p1.findall(str)
print p2.findall(str)
str = 'a@163.com;b@gmail.com;c@qq.com;e@163.com;z@qq.com'
p3 = re.compile('[\w]+@[163|qq]+\.com')
print p3.findall(str)
str = '<html><h>title</h><body>xxx</body></html>'
p4 = re.compile('<h>[^<]+</h>')
print 4, p4.findall(str)
p4 = re.compile('<h>([^<]+)</h><body>([^<]+)</body>')
print 5, p4.findall(str)
str = 'xx2016-06-11yy'
p5 = re.compile('\d\d\d\d-\d\d-\d\d')
print p5.findall(str)
if __name__=="__main__":
'''
user1 = User('u1', 1)
print user1
admin1 = Admin('a1', 101, 'g1')
print admin1
print create_user('USERX')
'''
# print 'hello nowcder'
# comment
# demo_string()
# demo_operation()
# demo_buildinfunction()
# demo_contronflow()
# demo_list()
# demo_dict()
# demo_set()
# demo_exception()
# demo_random()
demo_re()