Python教程

4.Python:基本运算符

本文主要是介绍4.Python:基本运算符,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
# 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:基本运算符的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!