“笨办法”学Python笔记 Lesson1—5

 

一、作业内容

笨办法学 Python 习题0-10以及加分题。


二、作业代码:

# -*- coding: utf-8 -*-
#习题一:第一个程序
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' #单引号中包括双引号

print '''I'd much rather you 'not'.''' #三引号
# 加分习题
# 一、让你的脚本再多打印一行。
# 使用 \n 换单行
print('I "said"  do not\ntouch this.')

# 使用三引号'''...''' 换多行
print('''I
"said"
do
not
touch
this.''''')

# 二、让你的脚本只打印一行。
print("Hello World!")
print("Hello Again")
print("I like typing this.")

# 删除其他的print,将多个字符串合并为一个字符串,用反斜杠表示字符串将在下一行继续,并只打印一行
print("Hello World! \
Hello Again. \
I like typing this.")

# 三、在一行的起始位置放一个 ‘#’ (octothorpe) 符号。它的作用是什么?自己研究一下。

# print("Hello World!")

‘#’ 的作用是注释。从运行的结果来看,如果不想让代码起到作用,又不想删除代码,也可以使用注释符号。

你应该在你的程序中尽可能多地使用有用的注释:

  • 解释假设
  • 说明重要的决定
  • 解释重要的细节
  • 说明你想要解决的问题
  • 说明你想要在程序中克服的问题

# -*- coding: utf-8 -*-
#习题二:注释和#号
# 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.'
加分习题
# 一、弄清楚”#”符号的作用。而且记住它的名字。(中文为井号,英文为 octothorpe 或者 pound character)。
# 二、打开你的 ex2.py 文件,从后往前逐行检查。从最后一行开始,倒着逐个单词单词检查回去。
# 三、有没有发现什么错误呢?有的话就改正过来.
# 四、朗读你写的习题,把每个字符都读出来。有没有发现更多的错误呢?有的话也一样改正过来。

# -*- coding: utf-8 -*-
#习题三:数字和数学计算
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 greater or equal?", 5 >= -2
print "Is it less or equal?", 5 <= -2
# 加分习题

# 一、使用 # 在代码每一行的前一行为自己写一个注解,说明一下这一行的作用。
# 注解已在上方的输入内容中。

# 二、记得开始时的 <练习 0> 吧?用里边的方法把 Python 运行起来,然后使用刚才学到的运算符号,把Python当做计算器玩玩。
>>> 10 / 3
3.3333333333333335
>>> 9 / 3
3.0
>>> 10 // 3
3
>>> 10 % 3
1

# 三、自己找个想要计算的东西,写一个 .py 文件把它计算出来。
# 略。


# 四、有没有发现计算结果是”错”的呢?计算结果只有整数,没有小数部分。研究一下这是为什么,搜索一下“浮点数(floating point number)”是什么东西。
# 浮点数也就是小数,之所以称为浮点数。
# 是因为按照科学记数法表示时,一个浮点数的小数点位置是可变的。

# 五、使用浮点数重写一遍 ex3.py,让它的计算结果更准确(提示: 20.0 是一个浮点数)。

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

print("Hens", 25 + 30 / 6)
print("Roosters", 100 - 25 * 3.0 % 4)

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

print(3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6)

print("Is it true that 3.0 + 2.0 < 5.0 - 7.0?")

print(3.0 + 2.0 < 5.0 - 7.0)

print("What is 3.0 + 2.0?", 3.0 + 2)
print("What is 5.0 - 7.0?", 5.0 -7.0)

print("Oh, that's why it's False.")

print("How about some more.")
print("Is it greater?", 5.0 > -2.0)
print("Is it greater or equal?", 5.0 >= -2.0)
print("Is it less or equal?", 5.0 <= -2.0)

# -*- coding: utf-8 -*-
#习题四:变量和命名
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."
# 更多的加分习题:
# 一、我在程序里用了 4.0 作为 space_in_a_car 的值,这样做有必要吗?如果只用 4 会有什么问题?

# 修改了编辑器的代码,观察运行的结果并得出结论:有必要。如果只用4,得到的结果就不是浮点数了。因为4.0是一个浮点数,4是一个整数。

# 二、记住 4.0 是一个“浮点数”,自己研究一下这是什么意思。

# 浮点数就是小数,按照科学计数法来表示时,一个浮点数的小数点位置是可变的。

# 三、在每一个变量赋值的上一行加上一行注解。

# 每一个变量赋值的注解已添加在上方的输入内容中。

# 四、记住 = 的名字是等于(equal),它的作用是为东西取名。
# 五、记住 _ 是下划线字符(underscore)。

# 六、将 python 作为计算器运行起来,就跟以前一样,不过这一次在计算过程中使用变量名来做计算,常见的变量名有 i, x, j 等等。

>>> i = 100
>>> x = 4.0
>>> j = 30
>>> r = 90
>>> cars_not_driven = i - j
>>> cars_driven = j
>>> carpool_capacity = cars_driven * x
>>> average_passengers_per_car = r / cars_driven
>>>
>>>
>>> print("There are", i, "cars available.")
There are 100 cars available.
>>> print("There are only", j, "drivers available.")
There are only 30 drivers available.
>>> print("There will be", cars_not_driven, "empty cars today.")
There will be 70 empty cars today.
>>> print("We can transport", carpool_capacity, "people today.")
We can transport 120.0 people today.
>>> print("We have", 90, "to carpool today.")
We have 90 to carpool today.
>>> print("We need to put about", average_passengers_per_car, "in each car.")
We need to put about 3.0 in each car.

# -*- coding: utf-8 -*-
#习题五: 更多的变量和打印
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." % (
    my_age, my_height, my_weight, my_age + my_height + my_weight)
# 加分习题

# 一、修改所有的变量名字,把它们前面的``my_``去掉。确认将每一个地方的都改掉,不只是你使用``=``赋值过的地方。
# 二、试着使用更多的格式化字符。例如`` %r ``就是是非常有用的一个,它的含义是“不管什么都打印出来”。

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 %r." % name)
print("He's %r inches tall." % height)
print("He's %r pounds heavy." % weight)
print("Actually that's not too heavy.")
print("He's got %r eyes and %r hair." % (eyes, hair))
print("His teeth are usually %r depending on the coffee." % teeth)

# this line is tricky, try to get it exactly right
print("If I add %r, %r, and %r I get %r." % (
    age, height, weight, age + height + weight))



# 三、在网上搜索所有的 Python 格式化字符。

# Python 格式化字符:
# 整数
%d
# 浮点数
%f
# 字符串
%s
# 十六进制整数
%x

# 四、试着使用变量将英寸和磅转换成厘米和千克。不要直接键入答案。使用 Python 的计算功能来完成。

height = 74 # inches
weight = 180 # lbs
transfer_height = 2.54 * height # cm
transfer_weight = 0.4535924 * weight # kg

print("He's %r inches tall, %r cm." % (height, transfer_height))
print("He's %r pounds heavy, %r kg." % (weight, transfer_weight))

猜你喜欢

转载自blog.csdn.net/qq_18893805/article/details/54983890
今日推荐