Python数据类型转换及运算符

一、数据类型的转换
int(x) 转整数;float(x) 转浮点数; str(x) 转字符串;
tuple(s) 转元组 ;list(s) 转列表;

# 浮点型转整数
x=12.34
print(int(x))
# 序列转列表
x=1,2,3,4
print(list(x))
# 数字转字符串
x=12.2
print(type(str(x)))
# 字符串转整型数字,前面的0省略
x='00989'
print(int(x))

运行结果
在这里插入图片描述
二、运算符
1、算数运算符
(1)+、-、*

print('加{}'.format(1+1))
print('减{}'.format(2-1))
print('乘{}'.format(2*4))

运行结果:
在这里插入图片描述
(2)/、//、%

# 不管整除还是整除不了,结果类型都为浮点型
print('除{}'.format(8/4))
print('除{}'.format(10/3))
# 取整 // ,不管余数,结果为整数部分
print('除{}'.format(20//3))
# 取余 % ,结果为余数
print('除{}'.format(10%3))

运行结果:
在这里插入图片描述
(3)幂

print('幂次方{}'.format(2**3))

运行结果:
在这里插入图片描述(4)优先级

print('结果{}'.format((1+2)*3**2/9+2))

运行结果:
在这里插入图片描述
2、赋值运算符

# 多个变量赋同一值
a=b=c=2
print(a,b,c)
# 多个变量赋不同值
a,b,c=2,'dd',12.33
print(a,b,c)

运行结果:
在这里插入图片描述
3、复合赋值运算符
+=、-=、/=、//=、%=、**=

#+=表示a=a+(1+2)
a=10
a+=1+2
print(a)
# -=表示b=b-1
b=4
b-=1
print(b)

运行结果:
在这里插入图片描述
4、比较运算符
== 相等、!=不等于、>、< 、>= 、<=,返回值为布尔类型
5、逻辑运算符
and、or、not,返回值为布尔类型

a=50
b=81
c=98
# 判断是否都大于90
print((a>90)and(b>90)and(c>90))
# 判断a或b或c是否大于90
print((a>90)or(b>90)or(c>90))

运行结果:
在这里插入图片描述

### Python 中类型转换运算符 #### 类型转换Python中,可以使用内置函数来进行不同类型之间的转换。以下是几种常见的类型转换方式: - **int()**: 将其他类型的值转换成整数。 - **float()**: 把数值或字符串转换成浮点数。 - **str()**: 转换成字符串形式。 ```python # 整形转浮点型 integer_value = 10 floating_point_value = float(integer_value) print(f"Integer to Float: {floating_point_value}") # Integer to Float: 10.0 ``` - **bool()**: 可以把任何数据类型转化为布尔值,通常情况下非零数值、非空序列(列表、元组、字典等)会被视为`True`;而相反的情况则为`False`。 ```python empty_list = [] non_empty_string = "hello" boolean_from_empty_list = bool(empty_list) boolean_from_non_empty_string = bool(non_empty_string) print(f"Empty List to Bool: {boolean_from_empty_list}, Non-empty String to Bool: {boolean_from_non_empty_string}") # Empty List to Bool: False, Non-empty String to Bool: True ``` #### 运算符及其应用实例 ##### 算术运算符 算术运算符用于执行常规数学计算,如加法、减法、乘法、除法等[^4]。 ```python number_a = 7 number_b = 3 addition_result = number_a + number_b subtraction_result = number_a - number_b multiplication_result = number_a * number_b division_result = number_a / number_b floor_division_result = number_a // number_b modulus_result = number_a % number_b exponentiation_result = number_a ** number_b results = { 'Add': addition_result, 'Subtract': subtraction_result, 'Multiply': multiplication_result, 'Divide': division_result, 'Floor Divide': floor_division_result, 'Modulo': modulus_result, 'Exponentiate': exponentiation_result } for operation, result in results.items(): print(f"{operation}: {result}") # Add: 10 # Subtract: 4 # Multiply: 21 # Divide: 2.3333333333333335 # Floor Divide: 2 # Modulo: 1 # Exponentiate: 343 ``` ##### 按位运算符 按位运算符作用于二进制表示的数据上,允许程序员直接操控比特位来完成特定的任务[^1]。 ```python bitwise_and = 6 & 3 # Binary AND bitwise_or = 6 | 3 # Binary OR bitwise_xor = 6 ^ 3 # Binary XOR bitwise_not = ~6 # Binary Ones Complement right_shift = 6 >> 1 # Right Shift by one position left_shift = 6 << 1 # Left Shift by one position operations = ['AND', 'OR', 'XOR', 'NOT', 'Right Shift', 'Left Shift'] values = [bitwise_and, bitwise_or, bitwise_xor, bitwise_not, right_shift, left_shift] for op, val in zip(operations, values): print(f"Bitwise {op} Result: {val}") # Bitwise AND Result: 2 # Bitwise OR Result: 7 # Bitwise XOR Result: 5 # Bitwise NOT Result: -7 # Bitwise Right Shift Result: 3 # Bitwise Left Shift Result: 12 ``` ##### 逻辑运算符 逻辑运算符主要用于处理条件表达式的组合,支持`and`, `or`, `not`三种操作[^2]。 ```python condition_1 = True condition_2 = False logical_and = condition_1 and condition_2 logical_or = condition_1 or condition_2 logical_not_condition_1 = not condition_1 logical_not_condition_2 = not condition_2 conditions = ['Logical AND', 'Logical OR', 'Not Condition 1', 'Not Condition 2'] outcomes = [logical_and, logical_or, logical_not_condition_1, logical_not_condition_2] for cond, outcome in zip(conditions, outcomes): print(f"{cond}: {outcome}") # Logical AND: False # Logical OR: True # Not Condition 1: False # Not Condition 2: True ``` 通过上述例子可以看出,在实际编程过程中合理运用不同种类的运算符以及适当进行类型转换能够极大地提高代码效率并简化复杂度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值