# 5-遍历、公共方法、引用

## 遍历

### 字符串遍历

```>>> a_str = "hello"
>>> for char in a_str:
...     print(char,end=' ')
...
h e l l o```

### 列表遍历

```>>> a_list = [1, 2, 3, 4, 5]
>>> for num in a_list:
...     print(num,end=' ')
...
1 2 3 4 5```

### 元组遍历

```>>> a_turple = (1, 2, 3, 4, 5)
>>> for num in a_turple:
...     print(num,end=" ")
1 2 3 4 5```

### 集合遍历

```>>> a_set = {1, 2, 3, 4, 5}
>>> for num in a_set:
...     print(num,end=" ")
1 2 3 4 5```

### 字典遍历

#### 带下标索引的遍历： enumerate()

```>>> chars = ['a', 'b', 'c', 'd']
>>> for i, chr in enumerate(chars):
...     print(i,chr)
...
0 a
1 b
2 c
3 d```

## 公共方法

### 运算符

Python 表达式

+

[1, 2] + [3, 4]

[1, 2, 3, 4]

*

'Hi!' * 4

['Hi!', 'Hi!', 'Hi!', 'Hi!']

in

3 in (1, 2, 3)

True

not in

4 not in (1, 2, 3)

True

#### +

```>>> "hello " + "itcast"
'hello itcast'
>>> [1, 2] + [3, 4]
[1, 2, 3, 4]
>>> ('a', 'b') + ('c', 'd')
('a', 'b', 'c', 'd')```

#### *

```>>> 'ab'*4
'ababab'
>>> [1, 2]*4
[1, 2, 1, 2, 1, 2, 1, 2]
>>> ('a', 'b')*4
('a', 'b', 'a', 'b', 'a', 'b', 'a', 'b')```

#### in

```>>> 'itc' in 'hello itcast'
True
>>> 3 in [1, 2]
False
>>> 4 in (1, 2, 3, 4)
True
>>> "name" in {"name":"Delron", "age":24}
True```

### python内置函数

Python包含了以下内置函数

1

cmp(item1, item2)

2

len(item)

3

max(item)

4

min(item)

5

del(item)

#### cmp

```>>> cmp("hello", "itcast")
-1
>>> cmp("itcast", "hello")
1
>>> cmp("itcast", "itcast")
0
>>> cmp([1, 2], [3, 4])
-1
>>> cmp([1, 2], [1, 1])
1
>>> cmp([1, 2], [1, 2, 3])
-1
>>> cmp({"a":1}, {"b":1})
-1
>>> cmp({"a":2}, {"a":1})
1
>>> cmp({"a":2}, {"a":2, "b":1})
-1```

#### len

```>>> len("hello itcast")
12
>>> len([1, 2, 3, 4])
4
>>> len((3,4))
2
>>> len({"a":1, "b":2})
2```

#### max

```>>> max("hello itcast")
't'
>>> max([1,4,522,3,4])
522
>>> max({"a":1, "b":2})
'b'
>>> max({"a":10, "b":2})
'b'
>>> max({"c":10, "b":2})
'c'```

#### del

del有两种用法，一种是del加空格，另一种是del()

```>>> a = 1
>>> a
1
>>> del a
>>> a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> a = ['a', 'b']
>>> del a[0]
>>> a
['b']
>>> del(a)
>>> a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined```

### 多维列表/元祖访问的示例

```>>> tuple1 = [(2,3),(4,5)]
>>> tuple1[0]
(2, 3)
>>> tuple1[0][0]
2
>>> tuple1[0][2]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
>>> tuple1[0][1]
3
>>> tuple1[2][2]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> tuple2 = tuple1+[(3)]
>>> tuple2
[(2, 3), (4, 5), 3]
>>> tuple2[2]
3
>>> tuple2[2][0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not subscriptable ```

## 引用

```>>> a = 1
>>> b = a
>>> id(a)
13033816
>>> id(b)   # 注意两个变量的id值相同
13033816
>>> a = 2
>>> id(a)   # 注意a的id值已经变了
13033792
>>> id(b)   # b的id值依旧
13033816```
```>>> a = [1, 2]
>>> b = a
>>> id(a)
139935018544808
>>> id(b)
139935018544808
>>> a.append(3)
>>> a
[1, 2, 3]
>>> id(a)
139935018544808
>>> id(b)       # 注意a与b始终指向同一个地址
139935018544808```

### 可变类型与不可变类型

• 列表 list
• 字典 dict

• 数值类型 int, long, bool, float
• 字符串 str
• 元组 tuple

## 常用的数据类型转换

int(x [,base ])

long(x [,base ])

float(x )

complex(real [,imag ])

str(x )

repr(x )

eval(str )

tuple(s )

list(s )

chr(x )

unichr(x )

ord(x )

hex(x )

oct(x )

27 篇文章13 人订阅

0 条评论

## 相关文章

### PHP字符串和数组操作函数

str_split() 函数把字符串分割到数组中。 stripslashes() 函数删除由 addslashes() 函数添加的反斜杠。 stripcslas...

42870

22550

27510

### Python字符串

# -*- coding: utf-8 -*- import re #字符串替换 str1 = 'hello world world world abc=12...

32460

23870

14530

27450

19990

253100

11020