Python编程入门学习笔记(三)

Python 学习笔记(三)

### 切片


```python
line = 'Welcome to Beijing,welcome to China!'
#取字符串的前10个字符,line[0:10],默认是0
line[:10]
```




    'Welcome to'




```python
#取第一个字符
line[1]
```




    'e'




```python
#从0开始到20个字符,每2个取一个字符
line[0:20:2]
```




    'Wloet ejn,'



### 取后10个字符


```python
line[-10:]
```




    ' to China!'



### 反转字符


```python
line[::-1]
```




    '!anihC ot emoclew,gnijieB ot emocleW'



### 单字符

### 单字符是不可以进行赋值的


```python
line[-1] = 'E'
```


    ---------------------------------------------------------------------------

    TypeError                                 Traceback (most recent call last)

    <ipython-input-38-ea6925c27361> in <module>()
    ----> 1 line[-1] = 'E'
    

    TypeError: 'str' object does not support item assignment



```python
#第一个字符大写,其他的字符小写
line.capitalize()
```




    'Welcome to beijing,welcome to china!'




```python
#居中处理,空白字符以‘#’代替
line.center(40,'#')
```




    '##Welcome to Beijing,welcome to China!##'




```python
#计数函数
line.count('e')
```




    5



### 字符串首尾判断


```python
#是否以给定的字符串结尾
line.endswith('na!')
```




    True




```python
#是否以给定的字符串开头
line.startswith('we')
```




    False




```python
#返回给定字符的第一个位置下标,如果字符不存在,返回‘-1’
line.find('l')
```




    2




```python
#从指定的位置开始,返回给定字符的位置下标,如果字符不存在,返回‘-1’
line.find('l',3)
```




    21




```python
#返回给定字符的第一个位置下标,如果字符不存在,报错
line.index('l')
```




    2




```python
#将字符串所有字符大写
line.upper()
```




    'WELCOME TO BEIJING,WELCOME TO CHINA!'




```python
#将字符串所有字符小写
line.lower()
```




    'welcome to beijing,welcome to china!'




```python
#判断是否是首字母大写,其他小写的格式
line.istitle()
```




    False




```python
#判断是否全部是大写
line.isupper()
```




    False




```python
#判断是否全是小写
line.islower()
```




    False




```python
line = '    Ls \n Kdk \n\t   '
```


```python
#去掉字符串两端的空格,制表符,中间的是无法去掉的
line.strip()
```




    'Ls \n Kdk'




```python
#去掉字符串右边的空格,制表符,
line.rstrip()
```




    '    Ls \n Kdk'




```python
#去掉字符串左边的空格,制表符,
line.lstrip()
```




    'Ls \n Kdk \n\t   '




```python
#大小写相互转换,即大写转小写,小写转大写
line.swapcase()
```




    '    lS \n kDK \n\t   '



### 【重点】上面用到的所有字符串函数,都是重新生成了一个新的字符串,原有的字符串是不变的


```python
line = 'ni hao'
id(line)
```




    2287933149848




```python
new_line = line.upper()
id(line),id(new_line)
```




    (2287933149848, 2287933519216)



### 列表


```python
# 空列表
varibals = []
varibals = list[]
```

#### 可以容纳任意类型的对象,任意数量的对象【重点】列表是可变类型的


```python
varibals = [1, 2, 3, 'ni hao', 'hello,python', [], [100,100]]
```


```python
varibals = []
varibals.append(1)
varibals.append(2)
varibals.append('ni hao')
varibals
```




    [1, 2, 'ni hao']




```python
varibals[0] = 10
varibals
```




    [10, 2, 'ni hao']



#### python是一种动态类型的语言,一个变量是什么类型,要看程序在运行过程中变量所代表的值是什么


```python
var = 10
type(var)
var = 'str'
type(var)
```




    str



### 切片


```python
varibals[-2:]
```




    [2, 'ni hao']




```python
varibals + [1,23]
```




    [10, 2, 'ni hao', 1, 23]




```python
varibals * 4
```




    [10, 2, 'ni hao', 10, 2, 'ni hao', 10, 2, 'ni hao', 10, 2, 'ni hao']



### 序列 列表是一种容器型的序列;字符串是一种扁平型的序列


```python
len(varibals)
```




    3




```python
#没有返回值,而是修改了列表对象本身
varibals.append(1)
varibals
```




    [10, 2, 'ni hao', 1]




```python
# 清空
varibals.clear()
```


```python
varibals = [1,12,23,4234,[1, 2]]
```


```python
#copy的作用是不改变原来变量的值
new_varibals = varibals.copy()
```


```python
#浅复制
new_varibals[0] = 9999
```


```python
new_varibals
```




    [9999, 12, 23, 4234]




```python
varibals
```




    [1, 12, 23, 4234]




```python
#深复制
new_varibals[-1][0] = 9999
new_varibals
```




    [1, 12, 23, 4234, [9999, 2]]




```python
varibals
```




    [1, 12, 23, 4234, [9999, 2]]




```python
id(new_varibals[-1]),id(varibals[-1])
```




    (2287932939720, 2287932939720)




```python
a = [1, 2]
b = [3 ,4]
a + b
```




    [1, 2, 3, 4]




```python
a.extend(b)
a
```




    [1, 2, 3, 4, 3, 4]




```python
#在列表指定位置插入一个值
a.insert(0,100)
a
```




    [100, 1, 2, 3, 4, 3, 4]




```python
# 把列表指定位置的元素弹出,不指定参数则弹出最后一个值
a.pop(0)
```




    100




```python
a
```




    [1, 2, 3, 4, 3, 4]




```python
#移除一个指定元素,如果元素不存在则报错
a.remove(3)
a
```




    [1, 4, 3, 4]




```python
#排序函数,reverse排序反转
a.sort(reverse=True)
a
```




    [4, 4, 3, 1]




```python
#in的用法
4 in a
```




    True



### tuple 不可变列表


```python
var = tuple()
var = ()
type(var)
```




    tuple




```python
# 只有count,index两个函数
var = (1,2,1,3,4,5,[23,34,43])
```


```python
var.count(1)
```




    2




```python
var.index(5)
```




    5




```python
a = 10, 20
a
```




    (10, 20)



### 字典类型

| 元祖变量 | 字符串变量 | 列表变量 |
| --- | --- | --- |
| t_1 = [1,2,3,4,5] | s_1 = 'ni hao' | l_1 = [1,2,3,4,5] |
| t_2 = [5,6,7,8,9] | s_2 = 'how are you' | l_2 = [6,7,8,9,10] |

| 函数 | 元祖 | 实例 | 字符串 | 实例 | 列表 | 实例 |
| :--- | :---: | :--- | :---: | :--- | :---: | :--- | :--- |
| + | &radic; | t_1 + t_2 | &radic; | s_1 + s_2 | &radic; | l_1 + l_2 |
| * | &radic; | t_1 * 2 | &radic; | s_1 * 2 | &radic; | l_1 * 2 |
| > < | &radic; | t_1 > t_2 | &radic; | s_1 > s_2 | &radic; | l_1 > l_2 |
| [index] | &radic; | t_1[0] | &radic; | s_1[0] | &radic; | l_1[0] | 列表可以索引赋值,字符串、元祖不可以 |
| [::] | &radic; | t_1[::] | &radic; | s_1[0:10:1] | &radic; | l_1[0:10:2] |列表可以切片赋值,字符串、元祖不可以|
| len | &radic; | len(t_1) | &radic; | len(s_1) | &radic; | len(l_1) |
|bool|&radic;|bool(t_1)|&radic;|bool(s_1)|&radic;|bool(l_1)|空字符串、空列表、空元祖转换为布尔型为False|
| count | &radic; | t_1.count(1) | &radic; | s_1.count('n') | &radic; | l_1.count(1) |
| index | &radic; | t_1.index(3) | &radic; | s_1.index('n') | &radic; | l_1.index(1) |
| replace |  |  | &radic; | s_1.replace('n','N') |  |  | 字符串replace函数返回一个新字符串,原来的变量不变 |
| sort |  |  |  |  | &radic; | l_1.sort(100) |
| reverse |  |  |  |  | &radic; | l_1.reverse() | 字符串不可改变,只能通过生成一个新的字符串来翻转 |
| append |  |  |  |  | &radic; | l_1.append(100) |
| extend |  |  |  |  | &radic; | l_1.extend(l_2) |
| remove |  |  |  |  | &radic; | l_1.remove(1) |
| pop |  |  |  |  | &radic; | l_1.pop() |

### 字典类型


```python
var = {}
var = dict()
type(var)
```




    dict




```python
var = {
    '中':100,
    '左':200
}
```


```python
var['中']
```




    100




```python
words = ['中','左']
location = [100,200]

location[words.index('中')]
```




    100



#### 拉锁函数


```python
new_var = list(zip(words,location))
```


```python
dict(new_var)
```




    {'中': 100, '左': 200}




```python
list(zip([1,2],[3,4],[4,5,6]))
```




    [(1, 3, 4), (2, 4, 5)]




```python
students = ['wang','li','sun','zhao','qian']
```


```python
#fromkeys为list元素赋初始值
money = dict.fromkeys(students,1000)
```

#### 访问字典中的值


```python
money['wang']
```




    1000




```python
#用get找名为ww的元素,如果找不到,返回none,如果需要初始值,可以通过参数填加,如100。
a = money.get('ww','100')
print(a)
```

    100
    


```python
money.keys()
```




    dict_keys(['wang', 'li', 'sun', 'zhao', 'qian'])




```python
money.values()
```




    dict_values([1000, 1000, 1000, 1000, 1000])




```python
money.items()
```




    dict_items([('wang', 1000), ('li', 1000), ('sun', 1000), ('zhao', 1000), ('qian', 1000)])




```python
#删除操作
money.pop('wang')
```




    1000




```python
money
```




    {'li': 1000, 'sun': 1000, 'zhao': 1000, 'qian': 1000}




```python
money['wang'] = 100
```


```python
money
```




    {'li': 1000, 'sun': 1000, 'zhao': 1000, 'qian': 1000, 'wang': 100}




```python
#setdefault如果原来存在值,则返回原来的值,如果不存在则返回参数的值
money.setdefault('li',5000)
```




    1000




```python
money.setdefault('gao',5000)
```




    5000




```python
money
```




    {'li': 1000, 'sun': 1000, 'zhao': 1000, 'qian': 1000, 'wang': 100, 'gao': 5000}


Author: allengao

发表评论

电子邮件地址不会被公开。 必填项已用*标注