《笨办法学Python》记录-part1(习题1~6)

例题复现:

例题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.')
#!/usr/bin/env python3 '''

注1:代码块的引用,在markdown语法里只需要在键盘里输入左上角的三个半角的[`]就可以了,也就是[~]这个键哦。
注2: Python3 和 Python2 的区别之——print()在p3中是函数,需要用()的,而在p2中 其可以直接print字符串,例如print “Hello World!”
注3:括号(),有英文半角和中文全角之区别。


例题2-代码

# A comment, this is so you can read your program later.
# Anything after the # is ignored by python.
a = 2
print  ("I could have code like this.测试拷贝中文到这里。",a )# and the comment after is ignored
# Pay attention that: In Python3 ,print() is a kind of "hanshu",don't forget the (),but in python2 the print do not need the ().
# You can also use a comment to "disable" or coment out a piece of code:

# print "This won't run."
print("This will run.")
# I also try no "space" between the Print and the(),look what happend.

# Log:
# 1.1 the chinese character could in the code,but it should in the "",not the out line 。

例题3-代码

print ("I will now count my chickens:")

print ("Hens",25+30/6)# In Print,in python3 ,the result is 30.0,but in python2.0 it is 30.

# print 30/6

print ("30/6")


print (30/6)# The is a point float number

print (30.000/6.21)# The is a point float number,more zero.

print ("Roosters",100-25*3%4)# I forget the function of % ,what the usage of it?#Attention that % is "qu yu shu"

print ("Now I will count the eggs:")

print (3+2+1-5+4%2-1/4+6) # In python3 the result is 6.75,but in python it is 7.

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,tha's why it's False.")
print ("How about some more.")

print ("Is it greater?", 5 > -2 )
print("Is it greater or eaqual?" , 5 >= -2)
print ("Is it less or equal?", 5 <=- 2)

## the cod below is for plus bonus。


例题4-代码

#  设置:给变量 车(cars)赋值100,每量车的空是4.0,司机是30个,旅客是90个。
# 车数量为100
cars = 100
# 每辆车里等空位是4
space_in_a_car = 4.0 #思考:这里为什么要用4.0浮点计算而不是4?
# 司机数量是30个。
drivers = 30
# 旅客人数为90个。
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.hahaha")

print ("数学是一切科学的源头和归宿,Turtules乌龟程序“”")

例题5-代码

# -- coding: utf-8 --
## 以下代码在py3下调试通过
my_name = "Zed A.Shaw"

my_age = 35 # not a lie

my_height = 74 #inches

my_weight = 180 #lbs

my_eyes = 'Blue' #注意:字符串内容可以选择单引号或者双引号。放在print函数后面加上括号就可以被打印出来。


my_teeth = 'White'

my_hair = 'Brown'


print ("Let's talk about %s ." % my_name) 

#格式化字符%s、%d。
# 只要将格式化的变量放到字符串中,再紧跟着一个百分号%,再紧跟着变量名即可。唯一要注意的地方,是如果你想要在字符串中通过格式化字符放入多个变量的时候,你需要将变量放到()圆括号里,变量之间采用逗号,隔开。

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))#在pyhon3中将%号转移到括号之外,最左端,而且左右参数可以塞到一个括号里,右侧可以有两个括号。

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." %(my_age,my_height,my_weight,my_age+my_height + my_weight))
# 在26行受到32行的启发,知道了Py3下如何表示。
# 程序员喜欢用恼人的难度大简写来节约打字事件,我们尽早学会就可以读懂这些东西。

#Log
#本文件于20170928建立。


习题6-代码

# 将“字符串”赋值给"变量x",该字符串中有个位置是用“%d”(包含格式)来代替的。%d这个符号看起来有两个作用:第一个作用是“占位符”——正名这个位置将来会有个东西放在这里;第二个作用是“这个东西的格式”,既然其使用了%d,我认为其应该是digital——数字的。

x= "There are %d types of people." %10

# 建立了给新的变量binary,将字符串“binary”赋值给了它。一般来说,变量会用简单的写法,竟然也可以直接使用单词。
binary="binary"
 
do_not="don't"
 
y="Those who know %s and those who %s."%(binary,do_not)
 
print (x)# print() 是个函数,必须有()
 
print (y)
 
print ("I said: %r."% x)
 
print ("I also said:'%s'."%y)#Print后面每次都要加满括号。
 
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)
 
 # 只要将格式化的变量放到字符串中,再紧跟着一个百分号%,再紧跟着变量名即可。唯一要注意的地方,是如果你想要在字符串中通过格式化字符放入多个变量的时候,[你需要将变量放到()圆括号里,变量之间采用逗号,隔开。]
 #试着使用更多的格式化字符,例如%s、%d、%r。例如 %r就是非常有用的一个,它的含义是“不管什 么都打印出来”。

LOG

20170927 建立了此文件。
20170928 将前面做的几道例题(1~6)搬过来,以陈述的方式讲给大家听。本文件将持续更新。

你可能感兴趣的:(《笨办法学Python》记录-part1(习题1~6))