Python列表和操作符
列表List是Python最通用的数据类型,一个列表包含的项用逗号分开,用两个方括号[ ]括起来。在一某些方面,List有点向C语言的数组,一个不同的地方是:List包含的项可以是不同的数据类型。
存储在List中的值可以使用分片操作符([ ] 和 [:])利用索引Index来访问,索引的值从列表的起始位置0开始,结束标志是-1。+号是列表连接符,星号*是重复操作符。
举例:
#!/usr/bin/python
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tinylist = [123, 'john']
print list # Prints complete list
print list[0] # Prints first element of the list
print list[1:3] # Prints elements starting from 2nd till 3rd
print list[2:] # Prints elements starting from 3rd element
print tinylist * 2 # Prints list two times
print list + tinylist # Prints concatenated lists
执行结果如下:
['abcd', 786, 2.23, 'john', 70.200000000000003]
abcd
[786, 2.23]
[2.23, 'john', 70.200000000000003]
[123, 'john', 123, 'john']
['abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'John']
Python元组和操作符
元组Tuples是与列表类似的另一种序列数据类型。元组由若干个由逗号分隔的值组成。然而,与列表不同的是,元组被括在括号中。
列表和元组之间的主要区别是:列表被括在括号([])中,它们的元素和大小可以更改,而元组则被括在圆括号(())中,不能更新。元组可以被视为只读列表。
举例:
#!/usr/bin/python
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
tinytuple = (123, 'john')
print tuple # Prints complete list
print tuple[0] # Prints first element of the list
print tuple[1:3] # Prints elements starting from 2nd till 3rd
print tuple[2:] # Prints elements starting from 3rd element
print tinytuple * 2 # Prints list two times
print tuple + tinytuple # Prints concatenated lists
执行结果如下:
('abcd', 786, 2.23, 'john', 70.200000000000003)
abcd
(786, 2.23)
(2.23, 'john', 70.200000000000003)
(123, 'john', 123, 'john')
('abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'John')
下来代码对元组来说是无效的,因为我们试图更新元组,这是不允许的。而对于列表来说,是允许的。
#!/usr/bin/python
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tuple[2] = 1000 # Invalid syntax with tuple
list[2] = 1000 # Valid syntax with list
Python 字典
Python的字典Dictionary是一种哈希表类型。他们的工作像关联数组,或Perl语言中的含键-值对的散列。字典键几乎可以是任何Python类型,但通常是数字或字符串。另一方面,值可以是任意的Python对象。
字典由大括号({ })括起来,可以使用方括号([])分配和访问值。
例如:
#!/usr/bin/python
dict = {}
dict['one'] = "This is one"
dict[2] = "This is two"
tinydict = {'name': 'john','code':6734, 'dept': 'sales'}
print dict['one'] # Prints value for 'one' key
print dict[2] # Prints value for 2 key
print tinydict # Prints complete dictionary
print tinydict.keys() # Prints all the keys
print tinydict.values() # Prints all the values
执行结果如下:
This is one
This is two
{'dept': 'sales', 'code': 6734, 'name': 'john'}
['dept', 'code', 'name']
['sales', 6734, 'john']
字典中没有元素顺序的概念。说元素是“无序的”是不正确的,它们只是无序的。
数据类型转换
有时,您可能需要在内置类型之间执行转换。要在类型之间转换,只需使用类型名作为函数。
有几个内置函数可以执行从一种数据类型到另一种数据类型的转换。这些函数返回一个表示转换值的新对象。
Sr.No. |
Function & Description |
1 |
int(x [,base]) Converts x to an integer. base specifies the base if x is a string. |
2 |
long(x [,base] ) Converts x to a long integer. base specifies the base if x is a string. |
3 |
float(x) Converts x to a floating-point number. |
4 |
complex(real [,imag]) Creates a complex number. |
5 |
str(x) Converts object x to a string representation. |
6 |
repr(x) Converts object x to an expression string. |
7 |
eval(str) Evaluates a string and returns an object. |
8 |
tuple(s) Converts s to a tuple. |
9 |
list(s) Converts s to a list. |
10 |
set(s) Converts s to a set. |
11 |
dict(d) Creates a dictionary. d must be a sequence of (key,value) tuples. |
12 |
frozenset(s) Converts s to a frozen set. |
13 |
chr(x) Converts an integer to a character. |
14 |
unichr(x) Converts an integer to a Unicode character. |
15 |
ord(x) Converts a single character to its integer value. |
16 |
hex(x) Converts an integer to a hexadecimal string. |
17 |
oct(x) Converts an integer to an octal string. |