元组的定义:
Tuple(元组)与列表相似,不同之处在于元组的元素不能修改
元组表示多个元素组成的序列
元组在python开发中,有特定的应用场景
用于存储一串信息,数据之间使用,分隔
元组用()定义
# 列表中通常保存相同类型的数据,而元组中通常保存不同类型的数据
初始元组:
In [1]: xfl_tuple = ('xuefeilong',21,175) 定义一个简单的元组
In [2]: type(xfl_tuple) 查看类型为元组类型
Out[2]: tuple
In [3]: xfl_tuple[0] 从零索引取值
Out[3]: 'xuefeilong'
In [4]: xfl_tuple[1]
Out[4]: 21
In [5]: xfl_tuple[2]
Out[5]: 175
In [6]: empty_tuple = () 定义一个空元组
In [7]: type(empty_tuple) 查看类型为元组类型
Out[7]: tuple
In [8]: empty_tuple. 查看元组的功能
empty_tuple.count empty_tuple.index
In [8]: single_tuple=()
In [9]: single_tuple=(1) 定义一个数据的元组必须加,号
In [10]: type(single_tuple) 查看类型为整形,这是初学者容易犯错
Out[10]: int
In [11]: single_tuple=(1,) 定义一个只有一个元素的元组,
必须加,号声明元组类型,加,表示其是一个元组,不然是整型,说明,是元组的标志
In [12]: type(single_tuple)
Out[12]: tuple
取值和索引以及统计元素个数:
"""
file: tuple01.py
date:2018-07-15 10:16 AM
author: xfl
desc:
"""
xfl_tuple = ('薛飞龙', 21, 175, '薛飞龙')
print xfl_tuple[0]
print xfl_tuple.index('薛飞龙')
print xfl_tuple.count('薛飞龙')
print len(xfl_tuple)
元组的输出:
"""
file: tuple02.py
date:2018-07-15 10:34 AM
author: xfl
desc:
"""
xfl_tuple = ('薛飞龙', 21, 175)
for my_info in xfl_tuple:
print my_info
xfl_tuple = ('薛飞龙', 21, 175)
print '%s的年龄是%d,身高是%d' %xfl_tuple
元组列表的类型转换:
In [13]: num_list = [1,2,3,4] 定义一个列表
In [14]: type(num_list) 查看类型为列表
Out[14]: list
In [15]: num = tuple(num_list) 转换成元组类型
In [16]: type(nu)
num num_list
In [16]: type(num) 查看已经转换为元组类型
Out[16]: tuple
In [17]: num1 = list(num) 再次转换成列表
In [18]: type(num1) 查看已经转换为列表类型
Out[18]: list
python中单双引号用法的区别:
In [19]: print 'hello' 单双引号均可以正常输出
hello
In [20]: print "hello"
hello
In [21]: print 'he's boy' 单引号输出报错
File "<ipython-input-21-c3d94099ffdb>", line 1
print 'he's boy'
^
SyntaxError: invalid syntax
In [22]: print "he's boy" 用""进行输出
he's boy
In [23]: print 'he\'s boy' 加入\进行转义
he's boy
什么是字符串:
字符串或串(String)是由数字、字母、下划线组成的一串字符。
一般记为 s=“a1a2···an”(n>=0)。它是编程语言中表示文本
的数据类型。在程序设计中,字符串(string)为符号或数值
的一个连续序列,如符号串(一串字符)或二进制数字串(一串二进制数字)。
字符串的输出:
"""
file: str_01.py
date:2018-07-15 11:53 AM
author: xfl
desc:
"""
str1 = 'hi'
for char in str1:
print char
str2 = u'我的宠物叫粉条'
for char in str2:
print char
统计字符串长度以及索引:
"""
file: str_02.py
date:2018-07-15 1:55 PM
author: xfl
desc:
"""
hello_str = 'hello python hello'
print len(hello_str)
print hello_str.count('llo')
print hello_str.count('o')
print hello_str.count('ww')
print hello_str.index('llo')
print hello_str.index('o')
字符串的常用功能01:
"""
file: str_03.py
date:2018-07-15 2:03 PM
author: xfl
desc:
"""
num_str = '1'
print num_str
print num_str.isdigit()
hello_str = 'hello python'
print hello_str.startswith('h')
print hello_str.startswith('hello')
print hello_str.startswith('ww')
print hello_str.endswith('n')
print hello_str.endswith('python')
print hello_str.endswith('ww')
字符串的常用功能02:
"""
file: str_04.py
date: 2018-07-21 8:33 PM
author: xuefeilong
desc:
"""
hello_str = 'hello python'
print hello_str.find('p')
print hello_str.find('llo')
print hello_str.find('ww')
print hello_str
print hello_str.replace('python','world')
print hello_str
null = ''
print null.isspace()
null3 = '\t'
print null3.isspace()
null2 = '\n'
print null2.isspace()