Python基本类型

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值