Python基本语法回顾一

  主要参考《笨办法学Python》 

 1. 联系环境在3中,当使用/作为除的时候输出的数值类型为浮点,使用//作为除的时候输出的类型为int。

 2.%s, %r, %d它们是一种“格式控制工具”。它们告诉 Python 把右边的变量带到字符串中,并且把变量值放到 %s 所在的位置上。

 3.打印的经典题

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."
)
你 应 该 看 到的 结 果
$ python ex8.py
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.'

4.

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.
"""
你 应 该 看 到的 结 果
$ python ex9.py
Here are the days: Mon Tue Wed Thu Fri Sat Sun
Here are the months: Jan
Feb
Mar
Apr
May
Jun
Jul
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.

 

posted @ 2017-03-28 15:57  99度冰  阅读(147)  评论(0)    收藏  举报