一、作业内容
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文本”这种模式,要注意总结差异
- 注意从一开始养成好的习惯和追求完美的细节,比如空格这一细节在前面几道题并没有注意,后面发现虽不报错但会提醒才意识到,标准化的理念要一开始就要树立