Python基础知识学习笔记
学习资源——Python123
1.1 变量
遵循C++的命名规则,注意可读性,少用I和O
>>>newVariable = "Hello,world!"
>>>print(newVariable)
Hello,world!
1.2 数字
整数
print或者直接输入计算即可
>>>print(2**3)
8
浮点数
二进制的表示导致运算后结果不一定与原结果相同
Decimal 模块
>>>from decimal import Decimal, getcontext
>>>getcontext().prec = 17
>>>result = 3 * Decimal(0.1)
>>>print(type(result))
<class 'decimal.Decimal'>
>>>print(3 * Decimal(0.1))
0.30000000000000002
>>>print(3 * 0.1)
0.30000000000000004
1.3 字符串
单引号和双引号
字符串可以包含在单引号或双引号中。于是字符串中可以包含引号。
>>>my_string = "123'456'"
>>>print(my_string)
123'456'
>>>my_string = '123'
>>>print(my_string)
123
多行字符串
使用三个单引号
改变大小写
>>>a_name = 'aaron'
>>>print(a_name)
aaron
>>>print(a_name.title())
Aaron
>>>print(a_name.upper())
AARON
>>>print(a_name.lower())
aaron
连接字符串
加号连接
格式化字符串
>>>string_template = 'The result of the calculation of {calc} is {res}'
>>>print("String Template: ", string_template)
String Template: The result of the calculation of {calc} is {res}
>>>print(string_template.format(calc='(3*4)+2', res=(3*4)+2))
The result of the calculation of (3*4)+2 is 14
空白符
\t \n
去除空白符
>>>a = '\ns\n'
>>>print(a.strip())
s
>>>print(a.lstrip())
s
>>>print(a.rstrip())
s
2.1 列表
>>>students = ['aa', 'bb', 'cc']
>>>for student in students:
... print("Hello, " + student.title() + "!")
...
Hello, Aa!
Hello, Bb!
Hello, Cc!
列表的访问
数组下标,从零开始,可以为负数
2.2 列表和循环
列表遍历
>>>dogs = ['a','b','c']
>>>for dog in dogs:
... print(dog)
...
a
b
c
穷举列表
>>>dogs = ['border collie', 'australian cattle dog', 'labrador retriever']
>>>> for dog in dogs:
... print(dogs.index(dog))
...
0
1
2
>>>for index, dog in enumerate(dogs):
... place = str(index)
... print("Place: " + place + " Dog: " + dog.title())
...
Place: 0 Dog: Border Collie
Place: 1 Dog: Australian Cattle Dog
Place: 2 Dog: Labrador Retriever
注意enumerate()会生成一个元组,使用一个占位符,每次循环此占位符0位置为坐标,1位置为列表元素,使用两个占位符,第一个为坐标,第二个为元素
>>> lis = {1,2,3,1, 'aaa'}
>>> lis
{'aaa', 1, 2, 3}
>>> a, b, c, d = lis
>>> a
'aaa'
>>> b
1
>>> c
2
>>> d
3
列表,元组,集合元素可以类型不同,但输入时注意遵守pep8规则
2.3 列表常用操作
修改元素
下标修改
查找元素
用index()定位
>>>dogs = ['border collie', 'australian cattle dog', 'labrador retriever']
>>>print(dogs.index('australian cattle dog'))
1
检查元素是否在列表中
in list
>>> dogs = ['border collie', 'australian cattle dog', 'labrador retriever']
>>> print('australian cattle dog' in dogs)
True
>>> print('poodle' in dogs)
False
添加元素
在尾部 list.append(str)
在某位置 list.insert(place,str)
列表排序
升序 list.sort()
降序 list.sort(reverse=True)
不真正改变的升序 sorted(list)
不真正改变的降序 sorted(list, reverse=True)
顺序反转 list.reverse()
列表长度
- len(list)
2.4 列表元素移除操作
通过位置移除元素
- del list[i]
通过值移除元素
- list.remove(str)
从列表中 popping 元素(栈与队列)
- list.pop(i)
不写i弹出最后一个,为栈,i = 0弹出第一个为队列,i可以为不大于列表长度-1的任意整数
2.5 列表切割操作
newlist = list[i:j] 左闭右开
i不写默认为0,j不写默认为列表长度,都不写复制列表
2.6 数字列表
range()生成
range(i,j,k)
左闭右开,k为步长
range()为一个类,可以用list()转换为列表
各种函数
min(list),max(list),sum(list)
2.7 列表推导式
数字列表
>>> squares = [number**2 for number in range(1,11)]
>>> for square in squares:
... print(square)
...
1
4
9
16
25
36
49
64
81
100
非数字列表
>>> students = ['bernice', 'aaron', 'cody']
>>> great_students = [student.title() + " the great!" for student in students]
>>> for great_student in great_students:
... print("Hello, " + great_student)
...
Hello, Bernice the great!
Hello, Aaron the great!
Hello, Cody the great!
2.8 字符串列表
作为字符列表的字符串
words='Hello!'
>>> for word in words:
... print(word)
...
H
e
l
l
o
!
切割字符串
- 下标访问
- list[i:j]
查找子串
- 返回是否存在 str in words
- 返回首次出现的位置 list.find(str)
- 返回最后一次出现的位置 list.rfind(str)
替换子串
list.replace(strA, strB)//用B替换A
子串计数
list.count(str)
分裂字符串
new_list = list.split(str)
根据str位置将字符串分裂成列表,不包含str
2.9 元组
定义和访问元组
- 圆括号定义
- 下标访问
- 不可添加和删除
元组生成字符串
- 类似于C的printf
- 注意%
>>> animals = ['dog', 1, 'bear']
>>> print("I have a %s, a %d, and a %s." % (animals[0], animals[1], animals[2]))
I have a dog, a 1, and a bear.
2.10 集合
- 花括号定义
- 存在唯一,非输入顺序
- in做存在性检查
常用操作
取集合的交,并,对称差,以及列表转换为集合
>>> set_of_shapes = {'polygon', 'triangle'}
>>> favourites_shapes = set(['circle','triangle','hexagon'])
>>>
>>> set_of_shapes.intersection(favourites_shapes)
{'triangle'}
>>> set_of_shapes.union(favourites_shapes)
{'polygon', 'hexagon', 'triangle', 'circle'}
>>> set_of_shapes.difference(favourites_shapes)
{'polygon'}
3 if结构
- 值的判断符号与C++相同
- if - elif - else
- 注意":"
- 遵守pep8规则加Tab
- 0, '', None都是False
- 1, -1都是True
4 循环迭代和输入
for循环与while循环(不需要赘述)
用户输入
input()