打印,三个单引号原样输出
print("Hello world")
print("Hello\
world")
print("Hello world\n","Hello world")
s = '''This is the first line
This is the second line
'''
print(s)
输出格式
#居中
s = 'I like C I like C++ I love Python'
s.center(50)
(Python可直接当计算器)
查看类型,type
num = 34
type(num)
#输出int
四则,整数除整数转为float
5//2
#2(只取整数部分)
type(1000 / 2)
#float
进制
0b10
#2 2进制
0o10
#8 8进制
0x10
#16 16进制
科学记数法
9.86755e2
#986.755
导入包,使用函数 包.方法名
import math
print(math.pi)
print(math.pow(2,3))
#2的3次方,结果为float型
2**3
#2的3次方,结果为int型
或与等运算符 or,and,==,!=
(2 < 1) or (1 < 2)
(2 < 1) and (1 < 2)
2 == 1
2 != 1
数据类型转换 数据类型(数据)
some_var = "110"
1089 + int(some_var)
True +3
#4 true --> 1
字符串 下标可为负数,[ : ]截取一部分
s = 10*"ha "
print(s)
#ha ha ha ha ha ha ha ha ha ha
letters = 'qwert'
print(letters[0])
#q
print(letters[-1])
#t
print(letters[0:3])
#qwe,左取右不取
print(letters[0:3:2])
#qe,每2个取一个
print(letters[::-1])
#trewq,倒序
切分整合替换
#按照逗号切分
s = "a,b,c,d,e"
s.split(',')
#整合
','.join(['a', 'b', 'c', 'd', 'e'])
#替换
s = 'I like C I like C++ I love Python'
s.replace('like','hate')
#'I hate C I hate C++ I love Python'
s = 'I like C I like C++ I love Python'
s.replace('like','hate',1)
#'I hate C I like C++ I love Python' 只替换1次
判断查找单词等工具
s = "I like C I like C++ I love Python"
s.startswith('I')
#true,判断是否以I开头
s.endswith('C++')
#false,判断是否以C++结尾
s.endswith('Python')
#true,判断是否以Python结尾
s.find('Python')
#27,找到起始位置
s.count("Python")
#1,出现的次数
输入输出参数
age = input("How old are you?")
height = input("How tall are you?")
print("So,you're %s years old and %s cm tall."%(age,height))
缩进代替 {}, if-else后有冒号
num = 34
if num > 30:
print("num bigger than 30 ")
else:
print("num smaller than 30 ")
交换数据
num1 = 1
num2 = 2
num1,num2 = num2,num1
print(num1,num2)
小练习:
输入一个数,代表从0点过去的分钟数,计算当前时间
minutes = input("minutes:")
if int(minutes) > 1440:
minutes = int(minutes) % 1440
hour = int(minutes)//60
minute = int(minutes)%60
if minute < 10:
print("\ntime: %s:0%s"%(hour,minute))
else:
print("\ntime: %s:%s"%(hour,minute))
农药纯文本解析
#文本
page_hero = '''<ul class="herolist clearfix"><li><a href="herodetail/522.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/522/522.jpg" width="91" height="91" alt="曜">曜</a></li><li><a href="herodetail/506.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/506/506.jpg" width="91" height="91" alt="云中君">云中君</a></li><li><a href="herodetail/505.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/505/505.jpg" width="91" height="91" alt="瑶">瑶</a></li><li><a href="herodetail/529.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/529/529.jpg" width="91" height="91" alt="盘古">盘古</a></li><li><a href="herodetail/511.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/511/511.jpg" width="91" height="91" alt="猪八戒">猪八戒</a></li><li><a href="herodetail/515.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/515/515.jpg" width="91" height="91" alt="嫦娥">嫦娥</a></li><li><a href="herodetail/513.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/513/513.jpg" width="91" height="91" alt="上官婉儿">上官婉儿</a></li><li><a href="herodetail/507.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/507/507.jpg" width="91" height="91" alt="李信">李信</a></li><li><a href="herodetail/312.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/312/312.jpg" width="91" height="91" alt="沈梦溪">沈梦溪</a></li><li><a href="herodetail/508.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/508/508.jpg" width="91" height="91" alt="伽罗">伽罗</a></li><li><a href="herodetail/509.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/509/509.jpg" width="91" height="91" alt="盾山">盾山</a></li><li><a href="herodetail/137.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/137/137.jpg" width="91" height="91" alt="司马懿">司马懿</a></li><li><a href="herodetail/510.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/510/510.jpg" width="91" height="91" alt="孙策">孙策</a></li><li><a href="herodetail/125.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/125/125.jpg" width="91" height="91" alt="元歌">元歌</a></li><li><a href="herodetail/504.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/504/504.jpg" width="91" height="91" alt="米莱狄">米莱狄</a></li><li><a href="herodetail/503.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/503/503.jpg" width="91" height="91" alt="狂铁">狂铁</a></li><li><a href="herodetail/197.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/197/197.jpg" width="91" height="91" alt="弈星">弈星</a></li><li><a href="herodetail/502.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/502/502.jpg" width="91" height="91" alt="裴擒虎">裴擒虎</a></li><li><a href="herodetail/176.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/176/176.jpg" width="91" height="91" alt="杨玉环">杨玉环</a></li><li><a href="herodetail/199.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/199/199.jpg" width="91" height="91" alt="公孙离">公孙离</a></li><li><a href="herodetail/501.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/501/501.jpg" width="91" height="91" alt="明世隐">明世隐</a></li><li><a href="herodetail/179.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/179/179.jpg" width="91" height="91" alt="女娲">女娲</a></li><li><a href="herodetail/198.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/198/198.jpg" width="91" height="91" alt="梦奇">梦奇</a></li><li><a href="herodetail/194.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/194/194.jpg" width="91" height="91" alt="苏烈">苏烈</a></li><li><a href="herodetail/195.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/195/195.jpg" width="91" height="91" alt="百里玄策">百里玄策</a></li><li><a href="herodetail/196.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/196/196.jpg" width="91" height="91" alt="百里守约">百里守约</a></li><li><a href="herodetail/193.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/193/193.jpg" width="91" height="91" alt="铠">铠</a></li><li><a href="herodetail/189.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/189/189.jpg" width="91" height="91" alt="鬼谷子">鬼谷子</a></li><li><a href="herodetail/182.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/182/182.jpg" width="91" height="91" alt="干将莫邪">干将莫邪</a></li><li><a href="herodetail/187.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/187/187.jpg" width="91" height="91" alt="东皇太一">东皇太一</a></li><li><a href="herodetail/191.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/191/191.jpg" width="91" height="91" alt="大乔">大乔</a></li><li><a href="herodetail/192.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/192/192.jpg" width="91" height="91" alt="黄忠">黄忠</a></li><li><a href="herodetail/190.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/190/190.jpg" width="91" height="91" alt="诸葛亮">诸葛亮</a></li><li><a href="herodetail/180.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/180/180.jpg" width="91" height="91" alt="哪吒">哪吒</a></li><li><a href="herodetail/186.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/186/186.jpg" width="91" height="91" alt="太乙真人">太乙真人</a></li><li><a href="herodetail/184.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/184/184.jpg" width="91" height="91" alt="蔡文姬">蔡文姬</a></li><li><a href="herodetail/183.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/183/183.jpg" width="91" height="91" alt="雅典娜">雅典娜</a></li><li><a href="herodetail/178.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/178/178.jpg" width="91" height="91" alt="杨戬">杨戬</a></li><li><a href="herodetail/177.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/177/177.jpg" width="91" height="91" alt="成吉思汗">成吉思汗</a></li><li><a href="herodetail/175.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/175/175.jpg" width="91" height="91" alt="钟馗">钟馗</a></li><li><a href="herodetail/174.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/174/174.jpg" width="91" height="91" alt="虞姬">虞姬</a></li><li><a href="herodetail/173.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/173/173.jpg" width="91" height="91" alt="李元芳">李元芳</a></li><li><a href="herodetail/171.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/171/171.jpg" width="91" height="91" alt="张飞">张飞</a></li><li><a href="herodetail/170.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/170/170.jpg" width="91" height="91" alt="刘备">刘备</a></li><li><a href="herodetail/169.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/169/169.jpg" width="91" height="91" alt="后羿">后羿</a></li><li><a href="herodetail/168.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/168/168.jpg" width="91" height="91" alt="牛魔">牛魔</a></li><li><a href="herodetail/167.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/167/167.jpg" width="91" height="91" alt="孙悟空">孙悟空</a></li><li><a href="herodetail/166.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/166/166.jpg" width="91" height="91" alt="亚瑟">亚瑟</a></li><li><a href="herodetail/163.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/163/163.jpg" width="91" height="91" alt="橘右京">橘右京</a></li><li><a href="herodetail/162.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/162/162.jpg" width="91" height="91" alt="娜可露露">娜可露露</a></li><li><a href="herodetail/157.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/157/157.jpg" width="91" height="91" alt="不知火舞">不知火舞</a></li><li><a href="herodetail/156.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/156/156.jpg" width="91" height="91" alt="张良">张良</a></li><li><a href="herodetail/154.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/154/154.jpg" width="91" height="91" alt="花木兰">花木兰</a></li><li><a href="herodetail/153.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/153/153.jpg" width="91" height="91" alt="兰陵王">兰陵王</a></li><li><a href="herodetail/152.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/152/152.jpg" width="91" height="91" alt="王昭君">王昭君</a></li><li><a href="herodetail/150.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/150/150.jpg" width="91" height="91" alt="韩信">韩信</a></li><li><a href="herodetail/149.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/149/149.jpg" width="91" height="91" alt="刘邦">刘邦</a></li><li><a href="herodetail/148.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/148/148.jpg" width="91" height="91" alt="姜子牙">姜子牙</a></li><li><a href="herodetail/146.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/146/146.jpg" width="91" height="91" alt="露娜">露娜</a></li><li><a href="herodetail/144.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/144/144.jpg" width="91" height="91" alt="程咬金">程咬金</a></li><li><a href="herodetail/142.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/142/142.jpg" width="91" height="91" alt="安琪拉">安琪拉</a></li><li><a href="herodetail/141.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/141/141.jpg" width="91" height="91" alt="貂蝉">貂蝉</a></li><li><a href="herodetail/140.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/140/140.jpg" width="91" height="91" alt="关羽">关羽</a></li><li><a href="herodetail/139.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/139/139.jpg" width="91" height="91" alt="老夫子">老夫子</a></li><li><a href="herodetail/136.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/136/136.jpg" width="91" height="91" alt="武则天">武则天</a></li><li><a href="herodetail/135.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/135/135.jpg" width="91" height="91" alt="项羽">项羽</a></li><li><a href="herodetail/134.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/134/134.jpg" width="91" height="91" alt="达摩">达摩</a></li><li><a href="herodetail/133.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/133/133.jpg" width="91" height="91" alt="狄仁杰">狄仁杰</a></li><li><a href="herodetail/132.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/132/132.jpg" width="91" height="91" alt="马可波罗">马可波罗</a></li><li><a href="herodetail/131.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/131/131.jpg" width="91" height="91" alt="李白">李白</a></li><li><a href="herodetail/130.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/130/130.jpg" width="91" height="91" alt="宫本武藏">宫本武藏</a></li><li><a href="herodetail/129.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/129/129.jpg" width="91" height="91" alt="典韦">典韦</a></li><li><a href="herodetail/128.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/128/128.jpg" width="91" height="91" alt="曹操">曹操</a></li><li><a href="herodetail/127.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/127/127.jpg" width="91" height="91" alt="甄姬">甄姬</a></li><li><a href="herodetail/126.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/126/126.jpg" width="91" height="91" alt="夏侯惇">夏侯惇</a></li><li><a href="herodetail/124.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/124/124.jpg" width="91" height="91" alt="周瑜">周瑜</a></li><li><a href="herodetail/123.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/123/123.jpg" width="91" height="91" alt="吕布">吕布</a></li><li><a href="herodetail/121.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/121/121.jpg" width="91" height="91" alt="芈月">芈月</a></li><li><a href="herodetail/120.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/120/120.jpg" width="91" height="91" alt="白起">白起</a></li><li><a href="herodetail/119.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/119/119.jpg" width="91" height="91" alt="扁鹊">扁鹊</a></li><li><a href="herodetail/118.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/118/118.jpg" width="91" height="91" alt="孙膑">孙膑</a></li><li><a href="herodetail/117.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/117/117.jpg" width="91" height="91" alt="钟无艳">钟无艳</a></li><li><a href="herodetail/116.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/116/116.jpg" width="91" height="91" alt="阿轲">阿轲</a></li><li><a href="herodetail/115.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/115/115.jpg" width="91" height="91" alt="高渐离">高渐离</a></li><li><a href="herodetail/114.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/114/114.jpg" width="91" height="91" alt="刘禅">刘禅</a></li><li><a href="herodetail/113.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/113/113.jpg" width="91" height="91" alt="庄周">庄周</a></li><li><a href="herodetail/112.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/112/112.jpg" width="91" height="91" alt="鲁班七号">鲁班七号</a></li><li><a href="herodetail/111.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/111/111.jpg" width="91" height="91" alt="孙尚香">孙尚香</a></li><li><a href="herodetail/110.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/110/110.jpg" width="91" height="91" alt="嬴政">嬴政</a></li><li><a href="herodetail/109.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/109/109.jpg" width="91" height="91" alt="妲己">妲己</a></li><li><a href="herodetail/108.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/108/108.jpg" width="91" height="91" alt="墨子">墨子</a></li><li><a href="herodetail/107.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/107/107.jpg" width="91" height="91" alt="赵云">赵云</a></li><li><a href="herodetail/106.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/106/106.jpg" width="91" height="91" alt="小乔">小乔</a></li><li><a href="herodetail/105.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/105/105.jpg" width="91" height="91" alt="廉颇">廉颇</a></li></ul>'''
#查找图片链接
start_link = page_hero.find('img src=')
print(start_link)
#图片链接开始的下标
start_quote = page_hero.find('"',start_link)
print(start_quote)
#图片链接结束的下标
end_quote = page_hero.find('"',start_quote+1)
print(end_quote
#打印链接
page_hero[start_quote+1:end_quote]
print(page_hero[start_quote+1:end_quote])
#名字开始的下标
start_bracket = page_hero.find(">",end_quote)
print(start_bracket)
#名字结束的下标
end_bracket = page_hero.find("<",start_bracket+1)
print(end_bracket)
#打印名字
page_hero[start_bracket+1:end_bracket]
print(page_hero[start_bracket+1:end_bracket])
#循环:
while page_hero.find('img src="')> 0 :
start_link = page_hero.find('img src="')
start_quote = page_hero.find('"',start_link)
end_quote = page_hero.find('"',start_quote+1)
print(page_hero[start_quote+1:end_quote])
start_bracket = page_hero.find('>',end_quote)
end_bracket = page_hero.find('<',start_bracket+1)
print(page_hero[start_bracket:end_bracket])
page_hero = page_hero[end_bracket:]