每次键入命令需要摁下Enter键(REPL中)或者Run Cell按钮(IPython)
你会得到一个显示结果反馈。
In [2]:
8 * 5 + 2
Out[2]:
世间真理42,以及
In [3]:
2 ** 100
Out[3]:
超大整数,或者试试
In [3]:
"The answer to life,the universe,and everything is ?"
Out[3]:
字符串。
用print打印任意我们想要的内容:
(一个对象在REPL中直接回车和被print分别输出的是他的__repr__和__str__方法对应的字符串)
In [4]:
print "Scala|Python|C"
print 42
print repr(42),str(42)
In [2]:
lang = "Scala|Python|C"
print lang
如果需要标识注释,请以# 符号开始一段语句(这与大部分脚本语言和unix-shell语言一样)。从# 开始直到一行结束的内容都是注释。
In [4]:
#Skip these comments.
print "These lines can be run."
print '''
Whatever.
'''
Out[4]:
In [3]:
a = 42
b = 42*3
print type(a)
print id(b)
print a*b
b = "Hello,world!"
print type(b)
print id(b)
print b * 2
print b + b
help(id)
#print dir(a),'\n'*2,dir(b)
print isinstance(a,int)
In [4]:
x = y = z = 42
print x,y,z
当然你也可以为多个对象指定多个变量,例如:
In [9]:
x,y,z = "So long","and thanks for all","the fish"
print x,y,z
In [6]:
x,y = "So long",42
x,y = y,x
print x
print y
Python同样拥有增量赋值方法:
In [4]:
a = 42
a += 1
print a
a -= 1
a *= 2
print a
类似的能够赋值的操作符还有很多,包括:
等等。
注意:Python不支持x++ 或者 --x 这类操作。
In [12]:
a,b,c,d = 42,True,3.1415926,6.23+1.5j
print type(a),type(b),type(c),type(d)
print isinstance(a,int),isinstance(b,(float,int)),\
isinstance(c,float),isinstance(d,(int,bool))
# 可以分别取得复数的实部和虚部
print d.real
print d.imag
In [13]:
d = 6.23+1.5j
e = 0+1j
print d+e,d-e,d*e
print 7*3,7**2 # x**y 返回x的y次幂
print 8%3,float(10)/3,10.0/3,10//3
print 1==2, 1 != 2 #==表示判断是否相等 != 表示不相等
print 7%2 #返回除法的余数
print 1<42<100
In [1]:
print 7/4
from __future__ import division
print 7/4
普通floor除法:
In [6]:
print 7//4
布尔值:
In [16]:
print not True
print not False
print 40 < 42 <= 100
print not 40 > 30
print 40 > 30 and 40 < 42 <= 100
print 40 < 30 or 40 < 42 <=100
In [2]:
c = "Hello world"
print len(c)
print c[0],c[1:3]
print c[-1],c[:]
print c[::2],c[::-1]
复杂切片操作:[start:end:step]
In [18]:
c = "Hello world"
print c[::-1] #翻转字符串
print c[:] #原样复制字符串
print c[::2] #隔一个取一个
print c[:8] #前八个字母
print c[:8:2] #前八个字母,每两个取一个
复制:
In [11]:
c = "Hello world"
d = c[:] #复制字符串,赋值给d
del c #删除原字符串
print d #d 字符串依然可用
e = d[0:4]
print e #新构造一个截取d字符串部分所组成的串
f = e
print id(f)
print id(e)
del e
print id(f),f #仍然可用
加号(+)用于字符串连接运算,星号(*)用来重复字符串。
In [12]:
teststr,stringback="Clojure is","cool"
print '-'*20
print teststr+stringback
print teststr*3
print '-'*20
美化打印:
In [21]:
pystr,pystring="Clojure is","cool"
print '-'*20
print pystr,pystring
print '-'*20
print pystr+'\t'+pystring
print '-'*20
In [10]:
pystr = "Clojure"
pystring = "cool"
yastr = "LISP"
yastring = "wonderful "
print('Python\'s Format I : {0} is {1}'.format(pystr,pystring))
print 'Python\'s Format II: {language} is {description}'.\
format(language='Scala',description='awesome') #使用\接续换行
print 'C Style Print: %s is %s'%(yastr,yastring)
字符串复杂效果举例:
In [11]:
for i in range(0,5)+range(2,8)+range(3,12)+[2,2]:
print' '*(40-2*i-i//2)+'*'*(4*i+1+i)
尾部换行\:
In [24]:
"A:What's your favorite language?\
B:C++."
Out[24]:
\t:水平制表符:
In [25]:
print "A:What’s your favorite language?\nB:C++"
print "A:What’s your favorite language?\tB:C++"
注意其他的转义字符(反斜线+字符):
In [14]:
print 'What\'s your favorite language?'
print "What's your favorite language?"
print "What\"s your favorite language?\\"
其他操作 —— Join,Split,Strip, Upper, Lower
In [3]:
s = "a\tb\tc\td"
print s
l = s.split('\t')
print l
snew = ','.join(l)
print snew
line = '\t Blabla \t \n'
print line.strip()
print line.lstrip()
print line.rstrip()
salpha = 'Abcdefg'
print salpha.upper()
print salpha.lower()
#isupper islower isdigit isalpha
更多操作:
In [13]:
s = "string"
print type(s)
In [14]:
s[3] = "o"
In [6]:
s = "String"
sba = bytearray(s)
sba[3] = "o"
print sba
In [ ]:
In [ ]: