【Python爬虫】-20170721 第一周小涌的Python 作业ex1-10

一、作业内容

Mr_Cxy发布作业很久了,出了个差感觉已经被甩十万八千里了,希望组织不要抛弃我,我也不放弃自己,开始追赶,本部分为第一周作业 (笨办法学 Python 习题0-10),希望>Mr_Cxy给个鼓励 。

二、作业代码

  • 习题0:安装程序已完成
  • 习题1:第一个程序
# 习题1:第一个程序
print("Hello World!")
print("Hello Again")
print("I like typing this.")
print("This is fun.")
print("Yay! Printing.")
print("I'd much rather you 'not'.")
print('I "said" do not touch this.\n')  # 最后再换行
'''
Summary:
1、使用英文输入状态下单引号和双引号输出文本
2、文本中出现单引号则用双引号输出,反之亦然
3、文本中同时出现单引号和双引号,则分别标出如print("I'd much rather you"'"not"''.')
'''
# 加分题1:让你的脚本再多打印一行(我理解每一句后面多一行)
print('Hello World!\n')
print('Hello Again\t')
print('I like typing this.\n')
print('This is fun.\n')
print('Yay! Printing.\r')
print("I'd much rather you 'not'.\n")
print('I "said" do not touch this.\n')
'''
Summary:
1、文本输出中\n表示回车后再多空出一行
2、文本输出中\r和\t则只回车换行并不多空出一行
'''
# 加分题2:让你的脚本只打印一行(我理解所有句子间无换行符)
print('Hello World!', end=' ')
print('Hello Again', end=' ')
print('I like typing this.', end=' ')
print('This is fun.', end=' ')
print('Yay! Printing.', end=' ')
print("I'd much rather you 'not'.", end=' ')
print('I "said" do not touch this.\n')  # 最后再换行
'''
Summary:
1、end=' '表示结尾不换行(因为默认换行的)
'''
# 加分题3:如上所示,#号为单行注释
'''
Summary:
1、单行注释用#
2、多行注释用三个单引号和三个双引号均可
'''
  • 习题2:注释和井号
# 习题2:注释和井号
# A comment,this is so you can read your program later.
# Anything after the # is ignored by python.
print("I could have code like this.") # and the comment after is ignored
# You can also use a comment to "disable" or comment out a piece of code:
# print("This won't run.")
print("This will run.")
'''
Summary:
1、#号为单行注释符,英文称作octothorpe或者pound character
2、使用#号时容易忽略的一点就是后面有一个空格
'''
  • 习题3:数字和数学计算
# 习题3:数字和数学计算
print("I will now count my chickens:")
print("Hens",25+30/6)
print("Roosters",100-25*3%4)
print("Now I will count the eggs:")
print(3+2+1-5+4%2-1/4+6) # 里面不能有引号否则是输出为文本而不是运算结果
print("Is it true that 3+2<5-7?")
print(3+2<5-7)
print("What is 3+2?",3+2)
print("What is 5-7?",5-7)
print("Oh,that's why it's False.")
print("How about some more.")
print("Is it greater?",5>=-2)
print("Is it less or equal?",5<=-2)
'''
Summary:
1、运算符含义:/整除、%取余数、*乘法
2、我这边似乎就是浮点运算,部分结果与书中不一致
3、浮点数挺复杂的,现在有点一知半解
4、犯错点:做运算时print里面加了引号发现输出文本无法运算
5、做判断运算时输出的为ture或者false
'''
  • 习题4:变量(variable)和命名
# 习题4:变量(variable)和命名
cars=100
space_in_a_car=4.0
drivers=30
passengers=90
cars_not_driven=cars-drivers
cars_driven=drivers
carpool_capacity=cars_driven*space_in_a_car
average_passengers_per_car=passengers/cars_driven
print("There are",cars,"cars available.")
print("There are only",drivers,"drivers available,")
print("There will be",cars_not_driven,"empty cars today.")
print("We can transport",carpool_capacity,"people today.")
print("We have",passengers,"to carpool today.")
print("We need to put about",average_passengers_per_car,"in each car.")
'''
Summary:
1、变量定义一定是一个整体的单词,多个词组要用下划线连接成一个整体
2、变量的运行要按逻辑走,里面有些指针的思想
3、做了几次作业已经感受到Python的Visualization做得很不错,细细总结不同文本颜色的差异
4、输出中注意文本与变量的关系和表示方法
5、比起以前C++好多了,Python中的变量只要定义过可以下拉框选,免得写错
'''
# 加分习题:即这个car_pool_capacity未定义,细细看car和pool之间多了一个下划线
  • 习题5:更多的变量和打印
# 习题5:更多的变量和打印
my_name="Zed A. Shaw"
my_age=35 # not a lie
my_height=74 # inches
my_weight=180 # lbs
my_eyes="Blue"
my_teeth="White"
my_hair="Brown"
print("Let's talk about %s."% my_name)
print("He's %d inches tall."% my_height)
print("He's %d pounds heavy."%my_weight)
print("Actually that's not too heavy.")
print("He's got %s eyes and %s hair."%(my_eyes,my_hair))
print("His teeth are usually %s depending on the coffee."% my_teeth)
# this line is tricky, try to get it exactly right
print("If I add %d, %d, and %d I get %d.\n"%(my_age,my_height,my_weight ,my_age+my_height+my_weight))
# 加分习题1:去掉“my_"
name="Zed A. Shaw"
age=35 # not a lie
height=74 # inches
weight=180 # lbs
eyes="Blue"
teeth="White"
hair="Brown"
print("Let's talk about %s."% name)
print("He's %d inches tall."% height)
print("He's %d pounds heavy."%weight)
print("Actually that's not too heavy.")
print("He's got %s eyes and %s hair."%(eyes,hair))
print("His teeth are usually %s depending on the coffee."% teeth)
print("If I add %d, %d, and %d I get %d.\n"%(age,height,weight ,age+height+weight))
'''
Summary:
1、使用这个%叫格式化字符;
2、如上面代码所示,常用的%s表示字符串,%f表示浮点数,%d表示十进制整数,%r所有字符串,当然还有更多
3、格式化字符的使用格式当然还可以更复杂,远不止上面那么简单,如%04d表示宽度为4
'''
# 加分习题4:英寸和磅转换成厘米和千克,其中1英寸=2.54厘米,1磅=0.45392千克
inches_height=74 # inches
lbs_weight=180 # lbs
cm_height=inches_height*2.54
kg_weight=lbs_weight*0.45392
print("%s's height is %f, and weight is %r"%(name,cm_height,kg_weight))
# 尝试了下%f和%r,height输出结果%r为187.96,而%f输出结果为187.960000
  • 习题6:字符串(string)和文本
# 习题6:字符串(string)和文本
x="There are %d types of people."%10
binary="binary"
do_not="don't"
y="Those who know %s and those who %s."%(binary,do_not)
print(x)
print(y)
print("I said: %r."%x)
print("I also said: '%s'."%y)
hilarious=False
joke_evaluation="Isn't that joke so funny?!%r"
print(joke_evaluation % hilarious)
w="This is the left side of..."
e="a string with a right side."
print(w+e)
'''
Summary:
1、习题4和习题5、6都有数字,两种表达方式都可以,但注意输出时格式的不一样
2、定义变量里也可以出现格式化字符
3、字符串运算(+、*)很神奇,减法和除法不好用,比如截取前面几个字符的操作怎么搞
'''
  • 习题7:更多打印
# 习题7:更多打印
print("Mary had a little lamb.")
print("Its fleece was white as %s." % 'snow')
print("And everywhere that Mary went.")
print("."*10) # what'd that do?
end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"
# watch that comma at the end. try removing it to see what happens
print(end1+end2+end3+end4+end5+end6,end7+end8+end9+end10+end11+end12) # 逗号充当了空格类似与end=''
print(end1+end2+end3+end4+end5+end6), print(end7+end8+end9+end10+end11+end12)
'''
Summary:
1、print(end1+end2+end3+end4+end5+end6,end7+end8+end9+end10+end11+end12)输出为Cheese Burger
2、print(end1+end2+end3+end4+end5+end6), print(end7+end8+end9+end10+end11+end12)输出结果中两个单词之间有回车符,print间默认会有回车符
3、因为版本问题显示结果不太一样
4、哦,今天还有一个收获,就是空格的输入,要注意看提示,前面练习都没有注意
'''
  • 习题8:打印,打印
# 习题8:打印,打印
formatter="%r %r %r %r"
print(formatter % (1, 2, 3, 4))
print(formatter % ("one", "two", "three", "four"))
print(formatter % (True, False, False, True))
print(formatter % (formatter, formatter, formatter, formatter))
print(formatter % (
    "I had this thing.",
    "That you could type up right.",
    "But it didn't sing.",
    "So I said goodnight."
))
'''
Summary:
1、第四个print倒是没想到的,开始以为是一个死循环
2、段落输入这个与以前C++不一样,Python去一个个对齐括号时发现PEP8波浪警告
'''
  • 习题9:打印,打印,打印
# 习题9:打印,打印,打印
# Here's some new strange stuff, remember type it exactly.
days="Mon Tue Wed Thu Fri Sat Sun"
months="Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"
print("Here are the days:",days)
print("Here are the months:",months)
print("""
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
""")
'''
Sumary:
1、"""段落输出,注意对比ex8中是每一段都需要引号标出来
'''
  • 习题10:那是什么?
# 习题10:那是什么?
print("I am 6'2\"tall.") # 将字符串中的双引号转义
tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split\non a line."
backslash_cat = "I'm \\ a \\ cat."
fat_cat = """
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
"""
print(tabby_cat)
print(persian_cat)
print(backslash_cat)
print(fat_cat)
'''
Summary:
1、"""段落输出用\'''效果是一样的
2、\\输出为\
3、其他常用转义字符\n换行,\t横向制表符,\r回车,\f换页
'''
# 加分习题4 将转义序列和格式化字符串放一起,并比较%r和%s
x="I like using \"Python\"!"
y="Today I finished %d homeworks."%5
print("output:%r"%x) # %r输出的内容多了两个单引号
print("output:%s"%y)

三、学习总结

  • 每一个小知识点的总结都在代码里用段落注释标出来“Summary”所示标出来
  • 每一个小的知识点其实学深入了需要掌握的东西还是很多的
  • 跟着抄题做了这么多练习有的却是需要好好总结,虽然代码中Summary也做了部分总结,比如段落字符串输出可以为每一段输出加引号,也可以整体用三个引号表示;比如字符串输出需要涉及到变量时,可以“文本”+变量+“文本”这种模式,也可以直接用格式化字符“文本%r文本”这种模式,要注意总结差异
  • 注意从一开始养成好的习惯和追求完美的细节,比如空格这一细节在前面几道题并没有注意,后面发现虽不报错但会提醒才意识到,标准化的理念要一开始就要树立
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容