Python基本类型
来源:互联网 发布:娶网络女主播 编辑:程序博客网 时间:2024/06/05 23:03
1、数值型(Numbers)
x = 5print 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.5print type(y) #Prints "<type 'float'>"
2、布尔型(Booleans)
t = Truef = Falseprint 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 helloprint len(hello)hw = hello + ' ' + world #字符串连接print hwhw2017 = '%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 0
- Python 基本类型操作
- Python基本类型
- python基本类型
- python基本类型
- Python基本类型
- python 基本类型转换
- Python的基本内置类型
- Python的基本内置类型
- 学习python(一) 基本类型
- python中的内置基本类型
- python基本数值类型简介
- Python基本类型与引用类型
- Python自学3:Python的基本类型
- python基础要点(1)-基本类型
- Python学习(1)基本类型
- 十分钟学会Python的基本类型
- python基本类型、操作及相互转换
- python 基本类型list、tuple、dict学习
- php array_push 与 $arr[]=$value 性能比较
- (spring全家桶十)Spring Statemachine有限状态机与地址分析
- cookie安全配置相关漏洞修复
- tarantool知识整理(1)
- 机器学习(周志华西瓜书)参考答案总目录
- Python基本类型
- 驱动中获取PsActiveProcessHead变量地址的五种方法
- 网络与多媒体基础知识--软考
- HashSet
- 网页登录以及单点登录的一些概念
- Ajax -- 发送 POST 请求
- printf和sprintf的小知识
- 如何做网课才可以更好地变现?
- 笔记:Linux环境下lua脚本层使用protobuf