元组
元组的定义
Tuple(元组)与列表相似,不同之处在于元组的元素不能修改
元组表示多个元素组成的序列
元组在python开发中,有特定的应用场景
用于存储一串信息,数据之间使用,分隔
元组用()定义
# 列表中通常保存相同类型的数据,而元组中通常保存不同类型的数据
Python 的元组与列表类似,不同之处在于元组的元素不能修改。
元组使用小括号,列表使用方括号。
元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可
元组中只包含一个元素时,需要在元素后面添加逗号
元组与字符串类似,下标索引从 0 开始,可以进行截取,组合等。
In [2]: info_tuple = (1,'he',2.2)
In [3]: type(in
%install_default_config in int
%install_ext info_tuple intern
%install_profiles input
In [3]: type(info_tuple)
Out[3]: tuple
In [4]: info_tuple[0]
Out[4]: 1
In [5]: info_tuple[1]
Out[5]: 'he'
In [6]: info_tuple[2]
Out[6]: 2.2
In [8]: empty_tuple = ()
In [9]: type(empty_tuple)
Out[9]: tuple
In [12]: empty_tuple.
empty_tuple.count empty_tuple.index
In [12]: single_tuple=()
In [13]: single_tuples=(1) 单个元素元组后面需加个逗号,不然就是int型
In [14]: type(single_t)
single_tuple single_tuples
In [14]: type(single_tuples)
Out[14]: int
In [15]: single_tuples=(1,)
In [16]: type(single_tuples)
Out[16]: tuple
列表元组可以互相转换
In [17]: num_list = [1,2,3,4]
In [18]: type(num_list)
Out[18]: list
In [19]: num_list = tuple(num_list)
In [20]: type(num_list)
Out[20]: tuple
In [21]: num_list = list(num_list)
In [22]: type(num_list)
Out[22]: list
元组运算符
与字符串一样,元组之间可以使用 + 号和 * 号进行运算。这就意味着他们可以组合和复制,运算后会生成一个新的元组。
Python表达式 结果 描述
len((1, 2, 3)) 3 计算元素个数
(1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6) 连接
('Hi!',) * 4 ('Hi!', 'Hi!', 'Hi!', 'Hi!') 复制
3 in (1, 2, 3) True 元素是否存在
for x in (1, 2, 3): print x, 1 2 3 迭代
info_tuple = ('张三',18,1.76,'李四','张三')
# 取值和索引
print info_tuple[0]
print info_tuple.index('李四')
# 统计
print info_tuple.count('张三')
# 统计元组中包含元素的个数
print len(info_tuple)
info_tuple = ('小红',18,1.80)
# 格式化字符串后面(),本质上就是一个元组
print '%s 的年龄是:%d 身高是:%.2f' % info_tuple
字典
# 字典是一个无序的数据集合,使用print输出字典时,
# 通常和定义的顺序不一样
message = {'name':'tom',
'age':18,
'height':1.85,
'weight':75.5}
print message
# 1.取值
print message['name']
# 增加/修改
# 有原元素时是修改
message['age'] = 19
print message
# 有新元素时是增加
message['sex'] = 'man'
print message
# 删除 需要定义引导名
message.pop('name')
print message
message = {'name':'tom',
'age':18,
'height':1.85,
'weight':75.5}
# 统计数量
print len(message)
# 合并字典 更新
temp_dict = {'height':1.40,'age':20}
message.update(temp_dict)
print message
# 字典的自定义键是可变得,也是唯一的
temp_dict = {'height':2.10,'sex':'man'}
message.update(temp_dict)
print message
# 清空字典
message.clear()
print message
# 循环列表
message_dict = {'name':'xiaohua',
'qq':'1234567',
'phone':'1234567'}
for q in message_dict:
print '%s - %s'% (q,message_dict[q])
card_list = [{'name':'xiaoming','age':18,'sex':'man'},
{'name': 'xiaohong', 'age': 18, 'sex': 'woman'}]
for card_info in card_list:
print card_info
字符串
Python 不支持单字符类型,单字符在 Python 中也是作为一个字符串使用。
Python 访问子字符串,可以使用方括号来截取字符串
In [1]: print 'hello'
hello
In [2]: print "hello"
hello
In [3]: print 'he's boy'
File "<ipython-input-3-c3d94099ffdb>", line 1
print 'he's boy'
^
SyntaxError: invalid syntax
In [4]: print 'he\'s boy'
he's boy
In [5]: print "he's boy"
he's boy
In [6]: str = 'hello python'
In [7]: str[0]
Out[7]: 'h'
In [8]: str[1]
Out[8]: 'e'
In [9]: str[2]
Out[9]: 'l'
In [10]: str[3]
Out[10]: 'l'
In [11]: str[4]
Out[11]: 'o'
python 字符串格式化符号:
符 号 描述
%c 格式化字符及其 ASCII 码
%s 格式化字符串
%d 格式化整数
%u 格式化无符号整型
%o 格式化无符号八进制数
%x 格式化无符号十六进制数
%X 格式化无符号十六进制数(大写)
%f 格式化浮点数字,可指定小数点后的精度
%e 用科学计数法格式化浮点数
%E 作用同 %e,用科学计数法格式化浮点数
%g %f 和 %e 的简写
%G %f 和 %E 的简写
%p 用十六进制数格式化变量的地址
str1 = 'hello python'
for char in str1:
print char
# 中文前需加 u
str2 = u'字符串字符串'
for char2 in str2:
print char2
hello_str = 'hello hello python'
# 统计字符串的长度
print len(hello_str)
# 统计某一子字符串出现的次数
print hello_str.count('llo')
# 某一子字符串出现的位置
print hello_str.index('hello')
# 若子字符串不存在,会报错
print hello_str.index('aa')
# 1.判断字符串是否只包含数字
num_str = '1'
print num_str
print num_str.isdigit()
#2 判断是否以指定的字符串开始
hello_str = 'hello python'
print hello_str.startswith('he')
# 3判断字符串是否以制定字符串结束
print hello_str.endswith('o')
# 4.查找指定的字符串,返回的是一个索引
# 如果查找的指定字符串不存在,程序不会报错,会返回-1
print hello_str.find('llo')
# 5.替换字符串,前面‘python’是被替换的
print hello_str.replace('python','world')
print hello_str
# 6.判断字符串是否含有空格
# 制表符\t 换行符\n 默认是空格
null = '\t\n'
print null.isspace()
格式
字符串 / 列表 / 元组【开始索引:结束索引:步长(有跳跃有间隔的对字符串切片)】
倒叙索引:如果一个字符串 / 列表 / 元组很长很长,使用倒叙索引很方便
最后一个索引的倒叙索引是:-1
1. 指定的区间属于左闭右开型
从起始位置开始,到结束位置的前一位(不包含结束位本身)
2. 从头开始,开始索引数字可以省略,冒号不能省略
3. 到末尾结束,结束索引数字可以省略,冒号不能省略
4. 步长默认为 1