16 == 0b10000 == 0o20 == 0x10
a = bin(16) # 转二进制
b = oct(16) # 转八进制
c = hex(16) # 转十六进制
print(a, b, c)
0b10000 0o20 0x10
注意:上述转换结果为字符串类型
print(type(a))
d = int(a, 2)
e = int(b, 8)
f = int(c, 16)
print(d, e, f)
16 16 16
print((0.1 + 0.2) == 0.3)
False
print(0.1 + 0.2)
0.30000000000000004
计算机采用二进制小数来表示浮点数的小数部分
二进制 0.1
十进制 0.5
a = 3*0.1
print(a)
b = round(a, 1) # 第二个参数为保留小数位数
print(b)
0.30000000000000004
0.3
3+4j
2+5J
2+1j
print((1+3-4*2)/5)
-0.8
x = 1
print(-x)
-1
print(2**3)
8
print(13 // 5) # 整数商 x/y 向下取整数
print(13 % 5) # 模运算 余数 13=2*5+3
2
3
几点说明
print(abs(3 + 4j)) # 模运算
5.0
print(pow(2, 5)) # pow(x, n)x的n次方 等价于x**n
print(pow(2, 5, 3)) # 2^5%3 更快速
32
2
a = 1.618
print(round(a)) # 默认四舍五入为整数
print(round(a, 2)) # 参数2表示四舍五入保留2位有效小数
print(round(a, 5)) # 位数不足,无需补齐
2
1.62
1.618
print(divmod(13, 5)) # 较(x//y, x%y)更快,只执行了一次x/y
(2, 3)
print(max(3, 2, 3, 6, 9, 4, 5))
9
print(sum([1, 2, 3, 4, 5])) # sum括号内为列表
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")
“Python” is good
转义符可以用来换行继续输入
a = "Py\
thon"
print(a)
Python
s = "My name is Peppa Pig"
变量名[位置编号]
print(s[0])
print(s[2])
print(s[5])
M
m
print(s[-1])
print(s[-3])
print(s[-5])
g
P
a
索引只能获得一个字符,如何获得多个字符?
变量名[开始位置:结束位置:切片间隔]
s = "Python"
print(s[0:3:1])
print(s[0:3:2])
print(s[0:3])
Pyt
Pt
Pyt
print(s[0:6])
print(s[:6])
print(s[:])
print(s[-6:])
Python
Python
Python
Python
反向切片
s = "123456789"
print(s[-1:-10:-1])
print(s[:-10:-1])
print(s[::-1])
987654321
987654321
987654321
a = "I love "
b = "my wife "
print(a+b)
I love my wife
c = a + b
print(c*3)
print(5*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 I love my wife I love my wife
folk_singers = "Peter, Paul and Mary"
print("Peter" in folk_singers)
True
for s in "Python":
print(s)
P
y
t
h
o
n
s = "Python"
print(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)
print(s_join)
1,2,3,4,5
s = " I have many blanks "
print(s.strip(" "))
print(s.lstrip(" "))
print(s.rstrip(" "))
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"
print(s.upper())
PYTHON
print(s.lower())
python
print(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("太小了")
请输入一个正整数:28
太小了
请输入一个正整数:2800
你猜对啦
import numpy as np
x = np.array([[1, 3, 2, 5, 7]])
print(x > 3)
print(x[x > 3])
[[False False False True True]]
[5 7]
age = 20
name = "Ada"
print(type(age))
print(type(name))
print(isinstance(age, int)) # 承认继承
print(isinstance(age, object))
print(isinstance(name, object)) # object是老祖宗
True
True
True
字符串.isdigit()字符是否只有数字组成
age = "20"
name = "Ada"
print(age.isdigit())
print(name.isdigit())
True
False
字符串.isalpha()字符是否只有字母组成
print(name.isalpha())
True
字符串.isalnum()字符是否只有数字和字母组成
print("Ada20".isalnum())
True
age = 20
print("My age is "+str(age))
My age is 20
s1 = "20"
s2 = "10.1"
print(int(s1))
print(float(s1))
print(float(s2))
print(eval(s1))
print(eval(s2))
20
20.0
10.1
20
10.1
以上,便是第二节基本数据类型内容。
下一节为组合数据类型,深入了解列表、元组、字典和集合。