注:本人写出来是为了让自己记得深刻,顺便可以给和我一样都是小白的人看(想学好还是看权威的大佬写的),至于大佬,请多多指教。
目录
1、数据类型转换
函数 | 描述 |
---|---|
将x转换为一个整数 | |
将x转换到一个浮点数 | |
创建一个复数 | |
将对象 x 转换为字符串 | |
将对象 x 转换为表达式字符串 | |
用来计算在字符串中的有效Python表达式,并返回一个对象 | |
将序列 s 转换为一个元组 | |
将序列 s 转换为一个列表 | |
转换为可变集合 | |
创建一个字典。d 必须是一个 (key, value)元组序列。 | |
转换为不可变集合 | |
将一个整数转换为一个字符 | |
将一个字符转换为它的整数值 | |
将一个整数转换为一个十六进制字符串 | |
将一个整数转换为一个八进制字符串 |
Python 数据类型转换可以分为两种:
- 隐式类型转换 - 自动完成
- 显式类型转换 - 需要使用类型函数来转换
1、隐式类型转换
在隐式类型转换中,Python 会自动将一种数据类型转换为另一种数据类型,不需要我们去干预。
>>>num_int=123
>>>num_fioat=1.23 #本来想写float,但是打错字了
>>>num_new=num_int+num_float
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
>>>num_new=num_int+num_float
NameError: name 'num_float' is not defined. Did you mean: 'num_fioat'?
>>>print("datatype of num_int":type(num_int))
SyntaxError: invalid syntax
>>>print("datatype of num_int":,type(num_int))
SyntaxError: invalid syntax
>>>print("datatype of num_int:",type(num_int))
datatype of num_int: <class 'int'>
>>>print("datatype num_float:",type(num_float))
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
>>>print("datatype num_float:",type(num_float))
NameError: name 'num_float' is not defined. Did you mean: 'num_fioat'?
>>>print("datatype of num_float:",type(num_float))
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
>>>print("datatype of num_float:",type(num_float))
NameError: name 'num_float' is not defined. Did you mean: 'num_fioat'?
>>>print("datatype of num_filoat:",type(num_filoat))
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
>>>print("datatype of num_filoat:",type(num_filoat))
NameError: name 'num_filoat' is not defined. Did you mean: 'num_fioat'?
>>>print("datatype of num_fioat:",type(num_fioat))
datatype of num_fioat: <class 'float'>
>>>print("value of num_new:",num_new)
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
>>>print("value of num_new:",num_new)
NameError: name 'num_new' is not defined
>>>print("Value of num_new:",num_new)
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
>>>print("Value of num_new:",num_new)
NameError: name 'num_new' is not defined
num_new = num_int+num_fioat
>>>print("Value of num_new:",num_new)
Value of num_new: 124.23
>>>print("datatype of num_new:",type(num_new))
datatype of num_new: <class 'float'>
num_string="234"
>>>print(num_int+num_string) #这个出错是因为他不是隐式的,需要特定转换
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
2、显式类型转换
在显式类型转换中,将对象的数据类型转换为所需的数据类型。 我们使用 int()、float()、str() 等预定义函数来执行显式类型转换。
>>>x=int(1)
>>>print("x")
x
>>>print("x是整数:“,x)
SyntaxError: incomplete input
>>>print("x是整数:",x)
x是整数: 1
>>>y=int(1.23)
>>>print("y")
y
>>>print("y:",y)
y: 1
>>>print(y)
1
>>>z=int("123")
>>>print(z)
123
>>>num_str=int(num_str)
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
num_str=int(num_str)
NameError: name 'num_str' is not defined
>>>num_int=123
>>>num_str="234"
>>>num_str=int(num_str)
>>>num_sum=num_int+num_str
>>>print("num_str转换后的类型:",type(num_str))
num_str转换后的类型: <class 'int'>
>>>print("num_sum的结果是:",num_sum)
num_sum的结果是: 357
>>>print("sun的数据类型:",type(num_sum))
sun的数据类型: <class 'int'>
2、运算符
1、Python算术运算符
运算符 | 描述 |
---|---|
+ | 加 - 两个对象相加 |
- | 减 - 得到负数或是一个数减去另一个数 |
* | 乘 - 两个数相乘或是返回一个被重复若干次的字符串 |
/ | 除 - x 除以 y |
% | 取模 - 返回除法的余数 |
** | 幂 - 返回x的y次幂 |
// | 取整除 - 往小的方向取整数 |
>>>a=1
>>>b=2
>>>print(a+b)
3
>>>print(b-a)
1
>>>a=12
>>>b=20
>>>print(a+b)
32
>>>print(a-b)
-8
>>>print(a/b)
0.6
>>>print(a//b)
0
>>>print(a%b)
12
2、比较运算符
运算符 | 描述 |
---|---|
== | 等于 - 比较对象是否相等 |
!= | 不等于 - 比较两个对象是否不相等 |
> | 大于 - 返回x是否大于y |
< | 小于 - 返回x是否小于y。所有比较运算符返回1表示真,返回0表示假。这分别与特殊的变量True和False等价。注意,这些变量名的大写。 |
>= | 大于等于 - 返回x是否大于等于y。 |
<= | 小于等于 - 返回x是否小于等于y。 |
>>>a=12
>>>b=20
>>>if(a==b)
SyntaxError: incomplete input
>>>if(a==b):
...print(a=b)
>>>else
SyntaxError: incomplete input
>>>else:
SyntaxError: invalid syntax
>>>if(a==b)
SyntaxError: incomplete input
>>>if(a==b):
...print(a等于b)
>>>else:
SyntaxError: invalid syntax
>>>if(a==b):
... print(a等于b)
... else:
SyntaxError: invalid syntax
>>>if ( a == b ):
... print ("1 - a 等于 b")
...else:
... print ("1 - a 不等于 b")
1 - a 不等于 b
>>>if(a!=b):
...print(a不等于b)
...else:
...print(a等于b)
Traceback (most recent call last):
File "<pyshell#57>", line 2, in <module>
print(a不等于b)
NameError: name 'a不等于b' is not defined
>>>if(a!=b):
...print("a不等于b")
...else:
...print("a等于b")
a不等于b
>>>if(a==b):
...print("a等于b")
...elif:
SyntaxError: incomplete input
>>>if(a==b):
...print("a等于b")
...elif
SyntaxError: incomplete input
>>>if(a==b):
...print("a等于b")
...elif(a>b): #相当于C语言的elseif
...print("a大于b")
...else:
...print("a小于b")
a小于b
3、赋值运算符
运算符 | 描述 | 实例 |
---|---|---|
= | 简单的赋值运算符 | c = a + b 将 a + b 的运算结果赋值为 c |
+= | 加法赋值运算符 | c += a 等效于 c = c + a |
-= | 减法赋值运算符 | c -= a 等效于 c = c - a |
*= | 乘法赋值运算符 | c *= a 等效于 c = c * a |
/= | 除法赋值运算符 | c /= a 等效于 c = c / a |
%= | 取模赋值运算符 | c %= a 等效于 c = c % a |
**= | 幂赋值运算符 | c **= a 等效于 c = c ** a |
//= | 取整除赋值运算符 | c //= a 等效于 c = c // a |
:= | 海象运算符,可在表达式内部为变量赋值。 | 在这个示例中,赋值表达式可以避免调用 len() 两次: if (n := len(a)) > 10: print(f"List is too long ({n} elements, expected <= 10)") |
不敲了,干饭