1、数值型(Numbers)
x = 5
print type(x) #Prints "<type 'int'>"
print x #Prints "5"
print x + 1 #Prints "6" 加减乘除和平常的一样
print x**2 #Prints "25" 幂运算
x += 1 #自加
x *= 2 #自乘
y = 2.5
print type(y) #Prints "<type 'float'>"
2、布尔型(Booleans)
t = True
f = False
print type(t) #Prints "<type 'bool'>"
print t and f #Prints "False"
print t or f #Prints "True"
print not t #Prints "False"
print t != f #Prints "True"
3、字符串(String)
hello = 'hello'
world = 'world'
print hello
print len(hello)
hw = hello + ' ' + world #字符串连接
print hw
hw2017 = '%s %s %d' % (hello,world,2017)
打印结果:
s = "hello"
print s.capitalize() #首字母大写
print s.upper() #全部大写
print s.rjust(7) #以7为长度右对齐,左边不空格
print s.center(7) #居中,左右补空格
print s.replace('l','(ooo)') #字符串替换
print ' world '.strip() #去首尾空格
print len(' world '.strip())
打印结果:
4、Python容器
(1)list
nums = range(5)
print nums
print nums[2:4] #下标从2到4-1的元素
print nums[2:] #小标从2到结尾的元素
print nums[:2] #从开头到下标为2-1的元素
print nums[:] #所有元素
print nums[:-1] #-1代表倒数,也就是最后一个函数
nums[2:4] = [8,9] #对子序列赋值
print nums
打印结果:
(2)dictionary