前言
在假期由于数模学习了python基础知识,在这里进行一个整理。
一、python简介
Python 是一种解释型语言,不需要编译和链接,可以节省大量开发时间。它的解释器实现了交互式操作,轻而易举地就能试用各种语言功能,编写临时程序,或在自底向上的程序开发中测试功能。同时,它还是一个超好用的计算器。
Python 程序简洁、易读,通常比实现同种功能的 C、C++、Java 代码短很多,原因如下:
- 高级数据类型允许在单一语句中表述复杂操作;
- 使用缩进,而不是括号实现代码块分组;
- 无需预声明变量或参数。
Python “可以扩展”:会开发 C 语言程序,就能快速上手为解释器增加新的内置函数或模块,不论是让核心程序以最高速度运行,还是把 Python 程序链接到只提供预编译程序的库(比如,硬件图形库)。
二、python基本计算方法
2.1注释
Python 注释以 # 开头,直到该物理行结束。注释可以在行开头,或空白符与代码之后。注释用于阐明代码
PS:不能在字符串里面。字符串中的 # 号就是 # 号。
spam = 1 # and this is the second comment
# ... and now a third!
text = "# This is not a comment because it's inside quotes."
2.2基本运算
1、符号说明
符号 | 运算 |
---|---|
+ | 加法 |
— | 减法 |
* | 乘法 |
/ | 除法 |
// | 整除 |
% | 取余 |
** | 乘方 |
注意:
/为除法运算;//为整除运算。
混合类型运算数的运算会把整数转换为浮点数。
2、代码示例:
>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 8 / 5 # division always returns a floating point number
1.6
>>> 17 / 3 # classic division returns a float
5.666666666666667
>>>
>>> 17 // 3 # floor division discards the fractional part
5
>>> 17 % 3 # the % operator returns the remainder of the division
2
>>> 5 ** 2 # 5 squared
25
>>> 2 ** 7 # 2 to the power of 7
128
3、_符号
交互模式下,上次输出的表达式会赋给变量 _。把 Python 当作计算器时,用该变量实现下一步计算更简单,例如:
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
注意:最好把该变量当作只读类型。不要为它显式赋值,否则会创建一个同名独立局部变量,该变量会屏蔽内置变量。
4、其他数据类型(未补充)
除了 int 和 float,Python 还支持其他数字类型,例如 Decimal 或 Fraction。Python 还内置支持 复数,后缀 j 或 J 用于表示虚数
2.3字符串
1、表示方法:、字符串有多种表现形式,用单引号(’……’)或双引号("……")标注的结果相同
>>> 'doesn\'t' # use \' to escape the single quote...
"doesn't"
>>> "doesn't" # ...or use double quotes instead
"doesn't"
2、反斜杠 \ 用于转义
>>> "\"Yes,\" they said."
'"Yes," they said.'
3、原始字符串:不希望前置 \ 的字符转义成特殊字符,可以使用 原始字符串,在引号前添加 r 即可:
>>> print('C:\some\name') # here \n means newline!
C:\some
ame
>>> print(r'C:\some\name') # note the r before the quote
C:\some\name
4、跨行连续输入:实现方式是用三引号:"""…""" 或 ‘’’…’’’,字符串行尾会自动加上回车换行,如果不需要回车换行,在行尾添加 \ 即可。示例如下:
print("""\
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
""")
#输出结果:
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
5、字符串拼接与相加:
字符串可以用 + 合并(粘到一起),也可以用 * 重复:
>>> # 3 times 'un', followed by 'ium'
>>> 3 * 'un' + 'ium'
'unununium'
相邻的两个或多个字符串字面值 (引号标注的字符)会自动合并:
注意:这项功能只能用于两个字面值,不能用于变量或表达式
>>> 'Py' 'thon'
'Python'
6、字符串索引与切片:
(1)索引:索引可以提取单个字符,第一个字符的索引是 0。单字符没有专用的类型,就是长度为一的字符串;索引还支持负数,用负数索引时,从右边开始计数。
>>> word = 'Python'
>>> word[0] # character in position 0
'P'
>>> word[5] # character in position 5
'n'
>>> word[-1] # last character
'n'
>>> word[-2] # second-last character
'o'
>>> word[-6]
'P'
(2)切片:
切片用于提取子字符串。格式[i:j]:从索引为i到索引为j的字符串(输出结果包含切片开始,但不包含切片结束,因此,s[:i] + s[i:] 总是等于 s)
+---+---+---+---+---+---+
| P | y | t | h | o | n |
+---+---+---+---+---+---+
0 1 2 3 4 5 6
-6 -5 -4 -3 -2 -1
>>> word = 'Python'
>>> word[0:2] # characters from position 0 (included) to 2 (excluded)
'Py'
>>> word[2:5] # characters from position 2 (included) to 5 (excluded)
'tho'
>>> word[:2] + word[2:]
'Python'
切片索引默认值:省略开始索引时,默认值为 0,省略结束索引时,默认为到字符串的结尾。
>>> word[:2] # character from the beginning to position 2 (excluded)
'Py'
>>> word[4:] # characters from position 4 (included) to the end
'on'
>>> word[-2:] # characters from the second-last (included) to the end
'on'
(3)越界问题:
- 索引越界会报错
- 切片越界会自动调整
>>> word[42] # the word only has 6 characters
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> word[4:42]
'on'
>>> word[42:]
''
7、字符串无法修改
Python 字符串不能修改。因此,为字符串中某个索引位置赋值会报错。要生成不同的字符串,应新建一个字符串
8、字符串长度计算
len():内置函数 len() 返回字符串的长度
>>> s = 'supercalifragilisticexpialidocious'
>>> len(s)
34
9、字符串方法(待补充)
2.4列表
1、列表 ,是用方括号标注,逗号分隔的一组值。列表可以包含不同类型的元素,但一般情况下,各个元素的类型相同。
>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
2、列表支持索引和切片。
>>> squares[0] # indexing returns the item
1
>>> squares[-1]
25
>>> squares[-3:] # slicing returns a new list
[9, 16, 25]
>>> squares[:]
[1, 4, 9, 16, 25]
切片赋值可以改变列表大小
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> # replace some values
>>> letters[2:5] = ['C', 'D', 'E']
>>> letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
>>> # now remove them
>>> letters[2:5] = []
>>> letters
['a', 'b', 'f', 'g']
切片赋值可以清空列表
>>> # clear the list by replacing all the elements with an empty list
>>> letters[:] = []
>>> letters
[]
注意:squares[:]切片操作会返回列表的浅拷贝!
- 浅层复制
构造一个新的复合对象,然后(在尽可能的范围内)将原始对象中找到的对象的 引用 插入其中。 - 深层复制
构造一个新的复合对象,然后,递归地将在原始对象里找到的对象的 副本 插入其中。
(1)浅拷贝:
a=[1,2,[3,4],5]
b=copy.copy(a)
print(b)
# 结果为 [1,2,[3,4],5]
# 接下来我们更改a的数据
a.append(6)
print(a)
print(b)
# 结果为 [1,2,[3,4],5,6]和[1,2,[3,4],5]
# 可以发现浅层的数据更改(第一层)并没有让b发生变化
# 接下来进行子对象数据(深层数据)的更改
a[2].append(7)
print(a)
print(b)
# 结果为 [1,2,[3,4,7],5,6]和[1,2,[3,4,7],5]
# 可以发现b发生了改变
(2)深拷贝:
a=[1,2,[3,4],5]
b=copy.deepcopy(a)
print(b)
# 结果为 [1,2,[3,4],5]
# 接下来我们更改a的数据
a.append(6)
print(a)
print(b)
# 结果为 [1,2,[3,4],5,6]和[1,2,[3,4],5]
# 可以发现浅层的数据更改(第一层)并没有让b发生变化
# 接下来进行子对象数据(深层数据)的更改
a[2].append(7)
print(a)
print(b)
# 结果为 [1,2,[3,4,7],5,6]和[1,2,[3,4],5]
3、列表支持合并操作
>>> squares + [36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
4、列表可修改
>>> cubes = [1, 8, 27, 65, 125] # something's wrong here
>>> 4 ** 3 # the cube of 4 is 64, not 65!
64
>>> cubes[3] = 64 # replace the wrong value
>>> cubes
[1, 8, 27, 64, 125]
5、append() 方法 可以在列表结尾添加新元素
>>> cubes.append(216) # add the cube of 6
>>> cubes.append(7 ** 3) # and the cube of 7
>>> cubes
[1, 8, 27, 64, 125, 216, 343]
6、内置函数len():可以求列表长度
>>> letters = ['a', 'b', 'c', 'd']
>>> len(letters)
4