一入Python深似海--print

先给大家来个干货^~^,学习Python的一个好站点,http://learnpythonthehardway.org/book/

经典样例

以下是几个老经典的样例喽,刚接触Python的能够敲一敲,看看结果喽!

my_name='Zed A. Shaw'
my_age=35#not a lie
my_height=74#inches
my_weight=180#1bs
my_eyes='Blue'
my_teeth='white'
my_hair='Brown'
print my_name
print "Let's talk about %r" %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 %r I get %d."%(my_age,my_height,my_weight,my_age+my_height+my_weight)
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
print "Mary had a little lamb."
print "Its fleece was white as %s."%'snow'
print "Its fleece was white as %r."%'snow'
print "And everywhere that Mary went."
print "."*10 #output . 10 times

end1="c"
end2="h"
end3="e"
end4="e"
end5="s"
end6="e"
#watch that comma at the end.
print end1+end2+end3,
print end4+end5+end6
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."
	)
days="Mon Tue Wed Thu Fri Sat Sun"
months="Jan\nFeb\nMar\nApr\nMay\nJun\nAug"

print "Here are the days:",days
print "Here are the months:",months
print "Here are the months: %r"%months
#That's how %r formatting works;
#it prints it the way you wrote it (or close to it). It's the "raw" format for debugging.
print """
There's something going on here.
whith 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.
"""
tabby_cat="\tI'm stabbed in."
persian_cat="I'm split\non a line."
backslash_cat="I'm \\ a \\ cat."

fat_fat="""
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_fat

%r与%s的差别

我的总结是这么个点:  %r与%s的差别

That‘s how %r formatting works; it prints it the way you wrote it (or close to it). It‘s the "raw" format for debugging.

Always remember this: %r is
for debugging, %s is for displaying.

时间: 2024-11-05 19:06:52

一入Python深似海--print的相关文章

一入python深似海--while-loop与for-loop

while loops 定义与实例 i = 0 numbers = [] while i < 6: print "At the top i is %d" % i numbers.append(i) i = i + 1 print "Numbers now: ", numbers print "At the bottom i is %d" % i print "The numbers: " for num in numbe

一入python深似海--list

几个实例展示python中数据结构list的魅力! list变量申明 the_count = [1, 2, 3, 4, 5] fruits = ['apples', 'oranges', 'pears', 'apricots'] change = [1, 'pennies', 2, 'dimes', 3, 'quarters'] 访问list元素 array= [1,2,5,3,6,8,4] #其实这里的顺序标识是 (0,1,2,3,4,5,6) (-7,-6,-5,-4,-3,-2,-1)#有

一入python深似海--对象的属性

Python中一切皆是对象,每个对象都可以有多个属性.Python是如何管理这些属性呢?我们来探讨一下. 属性的__dict__系统 对象的属性包含两部分:类属性和对象属性.对象的属性可能来自于其类的定义,叫做类属性.类属性可能来自于类的定义自身,也可能来自父类.一个对象的属性还可能是该对象实例定义的,叫做对象属性. 对象的属性存储在对象的__dict__属性中.__dict__为一个字典,键为属性名,对应的值为属性本身.下面是一个例子. class bird(object): feather

一入python深似海--浅拷贝与深拷贝

python中有一个模块copy,deepcopy函数用于深拷贝,copy函数用于浅拷贝.要理解浅拷贝,必须先弄清楚python中的引用. 引用 Python中一切都是对象,变量中存放的是对象的引用.这是一个普遍的法则.可以说 Python 没有赋值,只有引用.如,a=1,变量a只是整数对象1的引用. 可变对象与不可变对象及其引用 一.不可变对象 不可变对象包括:数字,字符串,元组. 由于Python中的变量存放的是对象引用,所以对于不可变对象而言,尽管对象本身不可变,但变量的对象引用是可变的.

一入python深似海--函数与文件

先上写干货,几个开源网站: github.com launchpad.net gitorious.org sourceforge.net freecode.com 今天介绍一下python函数和文件读写的知识. 函数 def print_two(*args):#That tells Python to take all the arguments to the function and then put them in args as a list arg1,arg2=args print "a

一入python深似海--range()与for

range用法 使用python的人都知道range()函数很方便,今天再用到他的时候发现了很多以前看到过但是忘记的细节. 这里记录一下: range(1,5)#代表从1到5(不包含5) [1,2,3,4] range(1,5,2)#代表从1到5,间隔2(不包含5) [1,3] range(5)#代表从0到5(不包含5) [0,1,2,3,4] 再看看list的操作: array= [1,2,5,3,6,8,4] #其实这里的顺序标识是 [1,2,5,3,6,8,4] (0,1,2,3,4,5,

一入python深似海--class

python class 分为三个部分:class and object(类与对象),inheritance(继承),overload(重载)and override(覆写). class and object 类的定义,实例化,及成员访问,顺便提一下python中类均继承于一个叫object的类. class Song(object):#definition def __init__(self, lyrics): self.lyrics = lyrics#add attribution def

一入python深似海--Dictionaries

定义及应用 定义 <span style="font-size:18px;">stuff = {'name': 'Zed', 'age': 36, 'height': 6*12+2}#key:value pairs</span> 实例 # create a mapping of state to abbreviation states = { 'Oregon': 'OR', 'Florida': 'FL', 'California': 'CA', 'New Yo

一入python深似海--dict(字典)的一种实现

下面是python中字典的一种实现,用list数据结构实现字典.具体是这样的:[[(key1,value1),(key2,value2),...],[],[],...] 内部每一个hash地址是一个list,存放hash地址相同的(key,value)对. dict代码 def Map(num_buckets=256): """Initializes a Map with the given number of buckets.""" aMap