⭐本专栏旨在对Python的基础语法进行详解,精炼地总结语法中的重点,详解难点,面向零基础及入门的学习者,通过专栏的学习可以熟练掌握python编程,同时为后续的数据分析,机器学习及深度学习的代码能力打下坚实的基础。
本文已收录于Python基础系列专栏: Python基础系列教程 欢迎订阅,持续更新。
16 == 0b10000 == 0o20 == 0x10
True
a = bin(16) # 转二进制
b = oct(16) # 转八进制
c = hex(16) # 转十六进制
print(a, b, c)
0b10000 0o20 0x10
注意:上述转换后结果为字符串类型,因此如果进行相等比较的话,输出的是False结果
a == b == c
False
type(a)
str
d = int(a, 2) # 二进制转十进制
e = int(b, 8) # 八进制转十进制
f = int(c, 16) # 十六进制转十进制
print(d, e, f)
16 16 16
(0.1+0.2) == 0.3
False
0.1+0.2
0.30000000000000004
计算机采用二进制小数来表示浮点数的小数部分
原因:部分小数不能用二进制小数完全表示
二进制 十进制
0.00011001100110011001 0.09999942779541016
0.0011001100110011 0.1999969482421875
0.01001100110011001 0.29999542236328125
0.01100110011001101 0.40000152587890625
0.1 === 1 ∗ 2 − 1 1*2^{-1} 1∗2−1 === 0.5
通常情况下不会影响计算精度
0.1 + 0.7
0.7999999999999999
a = 3*0.1
print(a)
0.30000000000000004
b = round(a, 1)
print(b)
b == 0.3
0.3
True
3+4j
2+5J
(2+5j)
2+1j
(1+3-4*2)/5
-0.8
x = 1
-x
-1
2**3
8
13//5 # 整数商 x/y 向下取整数
2
13 % 5 # 模运算 余数 13=2*5+3
几点说明
1+1.5
2.5
2/5
0.4
8/4
2.0
abs(-5)
5
abs(3+4j) # 对复数a+bj 执行的是求模运算(a^2+b^2)^0.5
5.0
pow(2, 5) # pow(x,n) x的n次方 等价于x**n
32
pow(2, 5, 3) # 2^5 % 3 更快速
2
a = 1.618
print(round(a)) # 默认四舍五入为整数
2
print(round(a, 2)) # 参数2表示四舍五入后保留2位小数
1.62
print(round(a, 5)) # 位数不足,无需补齐
1.618
等价于返回二元元组(x//y,x % y)
divmod(13, 5) # 较(x//y,x % y)更快,只执行了一次x/y
(2, 3)
max(3, 2, 3, 6, 9, 4, 5)
9
a = [3, 2, 3, 6, 9, 4, 5]
print("max:", max(a))
print("min:", min(a))
max: 9
min: 2
注意:sum里面需要填入一个序列数据
sum((1, 2, 3, 4, 5))
15
import math # 导入库
print(math.exp(1)) # 指数运算 e^x
print(math.log2(2)) # 对数运算
print(math.sqrt(4)) # 开平方运算 等价于4^0.5
2.718281828459045
1.0
2.0
import numpy as np
a = [1, 2, 3, 4, 5]
print(np.mean(a)) # 求均值
print(np.median(a)) # 求中位数
print(np.std(a)) # 求标准差
3.0
3.0
1.4142135623730951
print("Python")
print('Python')
Python
Python
双中有单
print("I'm 18 years old")
I'm 18 years old
单中有双
print('"Python" is good')
"Python" is good
双中有双,单中有单——转义符 \
# print(""Python" is good")
print("\"Python\" is good") # \ 字符
"Python" is good
转义符可以用来换行继续输入
s = "py\
thon"
print(s)
python
s = "My name is Peppa Pig"
变量名[位置编号]
print(s[0])
print(s[2])
print(s[5])
M
m
s = "My name is Peppa Pig"
print(s[-1])
print(s[-3])
print(s[-5])
g
P
a
索引只能获得一个字符,如何获得多个字符?
变量名[开始位置:结束位置:切片间隔]
s = "Python"
print(s[0:3:1])
Pyt
print(s[0:3])
Pyt
print(s[0:3:2])
Pt
s = "Python"
print(s[0:6])
Python
print(s[:6])
Python
print(s[:])
Python
print(s[-6:])
Python
反向切片
s = "123456789"
print(s[-1:-10:-1])
987654321
print(s[:-10:-1])
987654321
print(s[::-1])
987654321
a = "I love "
b = "my wife "
a+b
'I love my wife '
c = a+b
print(c*3)
print(3*c)
I love my wife I love my wife I love my wife
I love my wife I love my wife I love my wife
folk_singers = "Peter, Paul and Mary"
"Peter" in folk_singers
True
"PPM" in folk_singers
False
for s in "Python":
print(s)
P
y
t
h
o
n
s = "python"
len(s)
6
将中文字库,英文字母、数字、特殊字符等转化成计算机可识别的二进制数
将字符转化为Unicode码——ord(字符)
print(ord("1"))
print(ord("a"))
print(ord("*"))
print(ord("中"))
print(ord("国"))
49
97
42
20013
22269
将Unicode码转化为字符——chr(Unicode码)
print(chr(1010))
print(chr(10000))
print(chr(12345))
print(chr(23456))
ϲ
✐
〹
宠
上述特性适合以下所有字符串处理方法
languages = "Python C C++ Java PHP R"
languages_list = languages.split(" ")#括号里的参数就是我们希望对目标字符串进行分割的标记
print(languages_list)
print(languages)
['Python', 'C', 'C++', 'Java', 'PHP', 'R']
Python C C++ Java PHP R
s = "12345"
s_join = ",".join(s) #把可迭代的对象每一个都取出来,相邻两个之间加上聚合字符
s_join
'1,2,3,4,5'
# s = [1, 2, 3, 4, 5] 无法使用聚合
s = ["1", "2", "3", "4", "5"]
"*".join(s)
'1*2*3*4*5'
s = " I have many blanks "
print(s.strip(" ")) #从两端进行搜索,遇到指定字符后删除空格,然后停止
print(s.lstrip(" "))
print(s.rstrip(" "))
print(s)
I have many blanks
I have many blanks
I have many blanks
I have many blanks
s = "Python is coming"
s1 = s.replace("Python","Py")
print(s1)
Py is coming
s = "Python is an excellent language"
print("an:", s.count("an"))
print("e:", s.count("e"))
an: 2
e: 4
s = "Python"
s.upper()
'PYTHON'
print(s.lower())
print(s)
python
Python
s.title()
'Python'
a = 10
print(a > 8)
print(a == 12)
print(a < 5)
True
False
False
print(any([False,1,0,None])) # 0 False None 都是无
print(all([False,1,0,None]))
True
False
n = 2800
while True:
m = eval(input("请输入一个正整数:"))
if m == n:
print("正确")
break
elif m > n:
print("太大了")
else:
print("太小了")
请输入一个正整数:280
太小了
请输入一个正整数:2800
正确
import numpy as np
x = np.array([[1, 3, 2, 5, 7]]) # 定义 numpy数组
print(x > 3)
x[x > 3]
[[False False False True True]]
array([5, 7])
age = 20
name = "Ada"
print(type(age))
print(type(name))
print(isinstance(age, int)) # 承认继承 这里的int就相当于是一个类
True
print(isinstance(age, object))
print(isinstance(name, object)) # object 是所有类的老祖宗
True
True
字符串.isdigit()字符是否只有数字组成
age = "20"
name = "Ada"
age.isdigit()
True
name.isdigit()
False
字符串.isalpha()字符是否只有字母组成
name.isalpha()
True
age.isalpha()
False
字符串.isalnum()字符是否只有数字和字母组成
"Ada20".isalnum() # 比如可用于判断用户名是否合法
True
age = 20
print("My age is "+str(age))
My age is 20
s1 = "20"
s2 = "10.1"
int(s1) # 仅整型
# int(s2) 会错误
20
float(s1)
20.0
float(s2)
10.1
eval(s1)
20
eval(s2)
10.1