1. 内建(built-in)数据类型种类

  • 数字类型:int(), float()
  • 顺序(sequence):
    • 字符串:str()
    • 元祖:tuple()
    • 列表:list()
  • 字典:dict()
  • 集合:set()
  • 文件:open()
  • 布尔:True, False
  • 空对象:None

2. 各类型示例详解

2.1. 数字类型:

数字直接量示例

>>> 123 + 222  # 整数加
345 >>> 1.5 * 4 # 浮点数乘
6.0 >>> 2 ** 100 # 2的100次幂
1267650600228229401496703205376 >>> str(2**100) # 转换为字符串 >>> 3.1415 * 2 # (Python < 2.7 和 3.1),调用类的__repr__函数
6.28300000000004 >>> print(3.1415 * 2) # 调用了类的__str__函数
6.283 >>> import math # 引入外部模块, math数学模块
>>> math.pi
3.141592653589793
>>> math.sqrt(85)
9.219544457292887 >>> import random # 引入随机数模块
>>> random.random() # 生产随机数, 小于1的正数
0.6489341324245831
>>> random.randint(1, 10) # 生产1~10之间的整数,含1,10
5
>>> random.choice([1,3,4,8]) # 1,3,4,8中随机选择一个数
4

第三方开源数字类型有:矩阵、向量、扩展精度数等。

字符串转换为数字示例

>>> help(int)
class int(object)
int(x=0) -> integer
int(x, base=10) --> integer (默认转换为10进制整数) >>> int("12a")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '12a' >>> int("a12")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'a12' >>> int("abc")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'abc' 以上示例说明,含有非数字的字符串无法调用int()函数进行转换,使用过程中需要进行异常捕捉。 >>> int('')
12 >>> int('', 2) # 字符串中的2在二进制模式内是越界的
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 2: '' >>> int('', 2)
10 >>> int('')
777
>>> int('', 8)
511
>>> int('0x1212')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '0x1212'
>>> int('0x1212', 16)
4626
>>> int('0xFF', 16)
255
>>> int('0xFG', 16) # 字符G在十六进制中越界
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '0xFG' >>> help(float)
class float(object)
float(x) -> floating point number # 只接收一个参数,只能包含点和数字
>>> float('0.1x')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not covert string to float: '0.1x') >>> float('0.1')
0.1
>>> float('.1')
0.1 >>> float('0.1.2')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not covert string to float: '0.1.2')

2.2. 顺序(sequence):

”顺序“顾名思义,有先后、有序,其中包含的类型有:字符串、列表、元祖,Python中没有数组的提法,在Python里面需要忘记数组,如果难以做到,记得元祖和列表都可以认为是数组的一种,元祖和列表间又存在区别,稍后会提到这一点。sequence有以下几方面的特点:

  • 可以切片获取新的sequence
  • 可以下标访问
  • 可len()函数获取长度

2.2.1. 字符串:

特点:

  • 直接量定义:mystr = "this is my string"
  • 工厂函数定义:mystr = str("this is my string")
  • 可切片:mystr[0:3] (不含3), mystr[3:-2] (不含-2), 索引0, -1可以省略,mystr[0:3] === mystr[:3], mystr[3:-1] === mystr[3:], mystr[0:-1] (取全部值) === mystr[:]
  • 可下标访问:mystr[0], mystr[1], mystr[-1], mystr[-2]
  • 不可变值(immutable):mystr[0] = 'a' 会抛出TypeError错误

2.2.2. 列表:

特点:

  • 直接量定义:mylist = [1, 3, 5, "this is a string"]
  • 工厂函数定义:mylist = list([1, 3, 5, "this is a string"]); mylist = list( (1, 3, 5, "this is a string") ); mylist = list("this is a list")
  • 可切片:mylist[0:3] (不含3), mylist[3:-2] (不含-2), 索引0, -1可以省略,mylist[0:3] === mylist[:3], mylist[3:-1] === mylist[3:], mylist[0:-1] (取全部值) === mylist[:]
  • 可下标访问:mylist[0], mylist[1], mylist[-1], mylist[-2]
  • 可变值(mutable): mylist[1] = 'newstring'

2.2.3. 元祖:

特点:

  • 直接量定义:mytuple = (1,3,5, "this is a string"); mytuple = 1, 3, 5, "this is a string";
  • 工厂函数定义:mytuple = tuple([1,3,5, "this is a string"]); mytuple = tuple( (1,3,5, "this is a string") ); mytuple = tuple("this is a tuple")
  • 可切片:mytuple[0:3] (不含3), mytuple[3:-2] (不含-2), 索引0, -1可以省略,mytuple[0:3] === mytuple[:3], mytuple[3:-1] === mytuple[3:], mytuple[0:-1] (取全部值) === mytuple[:]
  • 不可变值(immutable): mytuple[0] = "new one"   # 抛出TypeError错误

2.2.4. 示例

字符串示例

>>> S = 'Spam'
>>> len(S)
4 下标访问
>>> S[0]
'S'
>>> S[1]
'p'
>>> S[100] # 越界抛出 IndexError 错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range >>> S[-1]
'm'
>>> S[len(S) - 1]
'm'
>>> S[-2]
'a' 切片操作
>>> S[1:3]
'pa'
>>> S[1:]
'pam'
>>> S
'Spam'
>>> S[0:3] # 切片不包含最后的值
'Spa'
>>> S[:3] # 开头的0可以省略
"Spa'
>>> S[:-1]
'Spa'
>>> S[:] # 末尾的-1也可以省略
'Spam'
>>> S[::2] # 第三个值为步长
'Sa' 字符串拼接
>>> S + 'xyz'
'Spamxyz'
>>> S * 8 # 重复8次
'SpamSpamSpamSpamSpamSpamSpamSpam' 不可修改值
>>> S[0] = 'z' # 抛出 TypeError 错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

字符串相关函数介绍示例:

>>> my_string = "this is a string"
>>> my_string.capitalize() # 首字母大写
This is a string >>> my_string.center(50, "=") # 中对齐,总长度50,填充=
=======this is a string=======
>>> my_string.ljust(50, "=") # 左对齐,总长度50,填充=
this is a string
>>> my_string.rjust(50, "=") # 右对齐,总长度50,填充=
==============this is a string # 默认my_string为UTF-8, UTF-8是UNICODE的子集
# str--------> unicode(utf-8) ---------------->str
# "".decode("GBK") --> unicode(utf-8) --> u"".encode("GBK")
>>> my_string.encode("GBK")
b'this is a string' # 因为是因为字符,所以显示出了字节类型的具体值
>>> my_string = "中文" # 因为"中文"两个字是UTF-8编码的,所以只能执行encode方法
>>> my_string.encode("GBK")
b'\xd6\xd0\xce\xc4' # 只显示出了二进制值 >>> my_string = "this is a string"
>>> my_string.endswith('ing')
True
>>> my_string.endswith('abc')
False
>>> my_string.startswith('this')
True >>> my_string = "this is a\tstring"
>>> my_string.expandtabs(tabsize=5) # \t替换为5个空格
this is a string
>>> my_string.find('i') # 从左往右,查找第一个i的索引值,找不到返回值为-1
2
>>> my_string.rfind('i') # 从右往左,查找第一个i的索引值,找不到返回值为-1
13
>>> "My name is {}, age is {}, sex is {}".format('Tim', 28, 'man')
"My name is Tim, age is 28, sex is man # format_map 参数为字典(后面会介绍字典)
>>> "My name is {name}, age is {age}, sex is {sex}".format_map({'name': 'Tim', 'age': 28, 'sex': 'man'})
"My name is Tim, age is 28, sex is man >>> my_string.index(" is") # 获取index值,找不到抛出ValueError异常
4
>>> my_string.rindex(" is") # 从右往左获取index值,找不到抛出ValueError异常
4
>>> my_string.isalnum() # 只包含字母,数字,因为有空格所以False
False >>> my_string.isdecimal() # 只包含数字时为True
False >>> my_string.isalpha() # 只包含字母时为True
False >>> my_srting.islower() # 是纯小写
True
>>> my_srting.isupper() # 是纯大写
False
>>> ','.join(my_string) # 每个迭代项目之间插入',',返回字符串
t,h,i,s, ,i,s, ,a, ,s,t,r,i,n,g >>> my_string.replace('i', 'x', 2) # i->x, 最大替换2次
thxs xs a string >>> my_string.partition('is') # 返回三个元素的元祖:head, sep, tail
('th', 'is', ' is a string')
>>> my_string.partition('is') # 找不到sep时
('this is a string', '', '') >>> my_string.partition('is') # 返回三个元素的元祖:head, sep, tail
('th', 'is', ' is a string')
>>> my_string.rpartition('is') # 优先从右往左查找
('this ', 'is', ' a string')
>>> my_string.split('is') # 使用'is'对字符串进行切割,得到列表,切割后的内容中不在包含有'is’
['th', ' ', ' a string']
>>> my_string.split('is', 1) # 切一次
['th', ' is a string']
>>> my_string.split('xis') # 不存在
['this is a string'] >>> my_string.rsplit('is', 1) # 从右往左开始切
['this ', ' a string'] >>> "tzt this is a string ttt".strip('tz') # 开头、结尾含有't', 'z'的字符
' this is a string ' >>> my_string.swapcase() # 交换大小写
THIS IS A STRING >>> my_string.zfill(20) # 左侧填0
0000this is a string

[TimLinux] Python 类型与运算的更多相关文章

  1. 1. python 类型和运算

    类型和运算 (Types and Operations) Introducing Python Object Types 在非正式的意义上, 在 Python 中, 我们用一些东西做事情. " ...

  2. Python对象类型及其运算

    Python对象类型及其运算 基本要点: 程序中储存的所有数据都是对象(可变对象:值可以修改 不可变对象:值不可修改) 每个对象都有一个身份.一个类型.一个值 例: >>> a1 = ...

  3. 【Python】Python对象类型及其运算

    Python对象类型及其运算 基本要点: 程序中储存的所有数据都是对象(可变对象:值可以修改 不可变对象:值不可修改) 每个对象都有一个身份.一个类型.一个值 例: >>> a1 = ...

  4. python变量、类型、运算、输出

    1.变量.类型.运算.输出等 # -*- coding: utf-8 -*- a=2 b=3 c=a+b print u'结果是=%i'%c #加u显示中文 str=unicode(s,"u ...

  5. [TimLinux] Python学习内容框架

    以下内容主体来自<Python学习手册第四版>,大致整理出的方向 1. 第一部分:使用入门 介绍Python语法之前,先对Python的的各个方面进行一个比较宽广的介绍,包含对Python ...

  6. (工具类)double类型数据运算

    package com.flf.util;import java.math.BigDecimal;/** * double类型数据运算 * @author Yancy 2016-12-14 * */p ...

  7. Java基本类型与运算

    问题及答案来源自<Java程序员面试笔试宝典>第四章 Java基础知识 4.4基本类型与运算 1.Java提供了哪些基本数据类型? Java一共提高了八种原始的数据类型:byte.shor ...

  8. python类型学习

    python类型学习 标准类型 数字 Integer 整型 Boolean 布尔型 Long integer 长整型 Floating point real numer  浮点型 Complex nu ...

  9. C Python类型互换

    从Python到C的转换用PyArg_Parse*系列函数,int PyArg_ParseTuple():把Python传过来的参数转为C:int PyArg_ParseTupleAndKeyword ...

随机推荐

  1. Java nio 空轮询bug到底是什么

    编者注:Java nio 空轮询bug也就是Java nio在Linux系统下的epoll空轮询问题. epoll机制是Linux下一种高效的IO复用方式,相较于select和poll机制来说.其高效 ...

  2. html部分知识点(待完善)

    html部分知识点 跳转 在A页面某链接处`<a href="#aa" target="_blank"></a> 在B页面指定的标题处定 ...

  3. 修改 Django Administration

    只需要在django项目下的APP下的admin.py重写以下几个变量即可,不需要改django源码 from django.contrib import adminadmin.site.site_t ...

  4. spark集群搭建(三台虚拟机)——系统环境搭建(1)

    !!!该系列使用三台虚拟机搭建一个完整的spark集群,集群环境如下: virtualBox5.2.Ubuntu14.04.securecrt7.3.6_x64英文版(连接虚拟机) jdk1.7.0. ...

  5. thinkphp 6.0 在 initialize 中重定向无效

    thinkphp 6.0 在 initialize 中重定向无效 改用 header() 函数 实例: // header('location:/index.php/模块/控制器/方法'); head ...

  6. a 标签添加 onclick 事件

    a 标签添加 onclick 事件 <a href="javascript:void(0);" οnclick="js_method()">点击&l ...

  7. 领扣(LeetCode)两数之和II - 输入有序数组 个人题解

    给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值 ...

  8. 在lldb调试中调用c++函数 - 如何使用QuartzCore里面的日志消息

    承接上一篇,上一篇讲到可以在lldb调试中调用QuartzCore.framework里的CA::Render::Object::show方法来是观察CA::Render模块内的类的信息,但是在lld ...

  9. Python 函数知识点整理

  10. .NET高级特性-Emit(2)类的定义

    在上一篇博文发了一天左右的时间,就收到了博客园许多读者的评论和推荐,非常感谢,我也会及时回复读者的评论.之后我也将继续撰写博文,梳理相关.NET的知识,希望.NET的圈子能越来越大,开发者能了解/深入 ...