python笔记 习题1-9

输出:
print"dsfgh"
print'dfghyj'
(print""或print'')

 


注释:
#注释(单行注释)

 


简单计算:
1.(输入:)print"Hens",25 + 30 / 6
(输出:)Hens 30

2.(输入:)print 3 + 2 + 1 - 5 + 4 % 2 -1 / 4 + 6
(输出:)7
(%为取余符号)

3.(输入:)3 + 2 < 5 - 7 或者 print 3 + 2 < 5 - 7
(输出:)False
(若判断正确则为Ture)

4.(输入:)print"What is 5 - 7 ?",5 - 7
(输出:)What is 5 - 7 -2

 


变量(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."
(上文输出:)
There are 100 cars available.
There are only 30 drivers available.
There will be 70 empty cars today.
We can transport 120.0 people today.
We have 90 to carpool today.
We need to put about 3 in each car.
(注:print "There are", cars, "cars available." 在there are后面和cars available前面无需加空格,输出的句子会自动加空格)

 


更多变量和打印:
(输入:)
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)

(输出:)
Let's talk about Zed A. Shaw.
He's 74 inches tall.
He's 180 pounds heavy.
Actually that's not too heavy.
He's got Blue eyes and Brown hair.
His teeth are usually White depending on the coffee.
If I add 35, 74, and 180 I get 289.
(注:%s为字符串,%r为不管什么都打印出来;注意打印一个句子中打印一个和多个字符的 区别)

 


字符串(string)和文本:

(输入:)

hilarious = False

joke_evaluation = "Isn't that joke so funny?! %r"

print joke_evaluation % hilarious 

(输出:)

Isn't that joke so funny?! False

(输入:)

w = "This is the left side of..."

e = "a string with a right side."

print w + e

(输出:)

This is the left side of...a string with a right side.

%r 和 %s 有什么不同?   

 %r 用来做 debug 比较好,因为它会显示变量的原始数据(raw data),而其它的符号则是用来向用户显示输出的。 

 

 

$ 更多打印:

(输入:)

print "Its fleece was white as %s." % 'snow' 

(输出:)

Its fleece was white as snow. 

(输入:)

print "." * 10 

(输出:)

..........

(输入:)

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."

)

(输出:)

1 2 3 4

'one' 'two' 'three' 'four'

True False False True

'%r %r %r %r' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r'

'I had this thing.'  'That you could type up right.'  "But it didn't sing."  'So I said goodnight.' 

为什么 “one” 要用引号,而 True 和 False 不需要? 

因为 True 和 False 是 Python 的关键字,用来表示真假的意义。如果你加了引号,它们就变成 了字符串,也就无法实现它们本来的功能了。

(输入:)

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.

"""

(输出:)

Here are the days:  Mon Tue Wed Thu Fri Sat Sun

Here are the months:  Jan

Feb

Mar

Apr

May

Jun

Aug

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. 

注意:必须写成 """ 而不是 " " ",引号之间不能有空格。 

(+)的目的是将两个字符串连接起来,组建成一个新的字符串。print 里的逗号(,) 其实是分隔参数的一种方式。 

 

 

 

 

posted @ 2019-09-26 15:53  hotboxx  阅读(148)  评论(0)    收藏  举报