码迷,mamicode.com
首页 > 编程语言 > 详细

4.Python:基本运算符

时间:2021-06-25 17:11:14      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:变量   指定   基本运算符   pre   算数   链式   算数运算   运算符   赋值   

# 1.算数运算符
import b as b

x = 10
y = 3.1
z = 3
print(x + y)
print(x*z)
print(x/z) # 结果带小数
print(x//z) # 结果只保留整数
print(x % z) # 取模,取余数
print(x**z) # 取幂

# 2.比较运算
print(x > y)
print(x < y)
print(x == y)
print(x != y)
print(x >= y)
print(x <= y)

# 3.赋值运算
a = 1 # 变量赋值

# 增量赋值
bb = 10
bb += 1 # bb = bb + 1
bb *= 2 # bb = bb * 2
bb /= 2 # bb = bb / 2
bb %= 3 # bb = bb % 3
bb **= 4 # bb = bb ** 4
print(bb)

# 链式赋值
a1 = b1 = c1 = 10
print(a1, b1, c1)
print(id(a1), id(b1), id(c1))

# 交叉赋值
m = 10
n = 20
print(m, n)
# temp = m
# m = n
# n = temp
# print(m, n)
m, n = n, m
print(m, n)

# 解压赋值

# 列表
salaries = [111, 222, 333, 444]
# 取前面n个值
x, y, z, *_ = salaries # *_:存储列表中所有未被指定的值
print(x)
print(y)
print(z)
# 取后面n个值
*_, x, y, z = salaries # *_:存储列表中所有未被指定的值
print(x)
print(y)
print(z)

# 字典
dic = {‘a‘: 1, ‘b‘: 2, ‘c‘: 3}
x, y, z = dic # 默认解压出来的是key
print(x, y, z)

4.Python:基本运算符

标签:变量   指定   基本运算符   pre   算数   链式   算数运算   运算符   赋值   

原文地址:https://www.cnblogs.com/wyless/p/14930132.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!