【python基础】03 - python数据类型的转换、运算符、数字之间的逻辑运算

一、python数据类型的转换

  1. 目的:python中用于用户输入的input()所接收的都会变成字符串类型,故产生了数据类型转换的需求。
  2. 常用的数据类型转换函数
  • int(x):将x转换为一个整数
  • float(x):将x转换为一个浮点数
  • str(x):将对象x转换为一个字符串
  • tuple(s):将序列s转换为一个元组
  • list(s):将序列s转换为一个列表
  • eval(str):字符串str是什么类型,就返回什么类型的对象
num1 = 1
str1 = '10'

# int():将数据转换成整形
num = input('请输入一个数字:')
print(type(num))    # <class 'str'>
print(type(int(num)))   # <class 'int'>
# float():将数据转换成浮点型
print(type(float(num1)))    # <class 'float'>
print(float(num1))  # 1.0
print(float(str1))  # 10.0
# str():将数据转换成字符串类型
print(type(str(num1)))    # <class 'str'>
print(str(num1))  # 1
# tuple():将一个序列转换成元组
list1 = [10, 20, 30]    # 列表序列
print(tuple(list1)) # (10, 20, 30)
# list():将一个序列转换成列表
t1 = (45, 50, 60)   # 元组序列
print(list(t1)) # [45, 50, 60]
# eval():计算在字符串中的有效python表达式,并返回一个对象
str2 = '1'  # 字符串中是整形,那么会转成int
str3 = '1.5'    # 字符串中是浮点型,那么会转换成浮点型
str4 = '(10, 20, 30)'   # 字符串中是元组,那么会转换成元组
str5 = '[10, 20, 30]'   # 字符串中是...,那么会转换成...
print(type(eval(str2))) # <class 'int'>
print(type(eval(str3))) # <class 'float'>
print(type(eval(str4))) # <class 'tuple'>
print(type(eval(str5))) # <class 'list'>

二、python运算符

运算符包含以下类型:

  • 算数运算符:加+ 减- 乘* 除/ 整除// 取余% 指数** 小括号()
1 + 1 = 2
2 - 1 = 1
2 * 2 = 4
2 * 0.5 = 1.0(乘法 如参与运算的有浮点数,那么结果也是浮点数)
10 / 2 = 5.0(除法 无论参与运算的数据类型是什么,结果都是浮点数)
10.1 / 2 = 5.05
9 // 4 = 2(整除 只取商)
9 % 4 = 1(取余数)
2 ** 4 = 2^4 = 16(求n次方)
(1+2)*3 (小括号用来表示计算优先级)

优先级:() 高于 ** 高于 * / // % 高 于 + -
  • 赋值运算符:= (先计算等号右边的结果,再将结果赋值到左边)
# (1) 单个变量赋值
num = 1
print(num)	# 1
# (2) 多个变量赋值:变量个数要匹配,按顺序赋值
a, b, c = 10, 0.5, 'hello'
print(a)	# 10
print(b)	# 0.5
print(c)	# hello
# (3) 多个变量赋相同值
a = b = 10
print(a)	# 10
print(b)	# 10
  • 复合赋值运算符
    • 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
d = 10
d *= 1+2	# d *= 3, 10*3=30
# 注意:先算复合赋值运算符右边的表达式,再算复合赋值运算
  • 比较运算符
    • a == b a=b才True
    • a != b a≠b才True
    • a > b a>b才True
    • a < b a<b才True
    • a >= b a>=b才True
    • a <= b a<=b才True
  • 逻辑运算符
    • a and b “与” 都True才True
    • a or b “或” 有True就True
    • not a “非” True取反返回False,False取反返回True
print((1 > 2) and (2 > 1))  # false and true = False
print((1 > 2) or (2 > 1))   # false or true = True
print(not 2 > 1)        # not true = False
# 小括号加不加虽然不影响结果,但是最好加上,明确优先级避免歧义

三、数字之间的逻辑运算

数字之间的逻辑运算,常用的是and\or运算符,主要判断条件是‘0’

  • and运算符:有0就0,否则结果为 最后一个 非0数字
  • or运算符:全0才0,否则结果为 第一个 非0数字
    需要注意,这和数字逻辑课程中涉及的只有0和1的逻辑运算是有所区别的。
# and运算符,有0就0,否则结果为'最后一个'非0数字
print(0 and 1) # 0
print(1 and 0) # 0
print(3 and 1) # 1
print(0 and 2) # 0
print(2 and 0) # 0
print(1 and 2) # 2
print(2 and 1) # 1
# or运算符,全0才0,否则结果为'第一个'非0数字
print(0 or 1) # 1
print(0 or 2) # 2
print(1 or 2) # 1
print(0 or 0) # 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值