Python教程

Python基本运算符

本文主要是介绍Python基本运算符,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Python基本运算符

一、算术运算符

算术运算符不仅有加'+'减'-'乘'*'除'/',还有整除'//',取余'%',等于'='。

print(9 // 6)
print(9 % 6)
print(9 + 6)
print(9 - 6)
print(9 * 6)
print(9 / 6)
print(9 == 6)

二、赋值运算符

  • 增量赋值
x = 100
x += 50  # :x = x + 50
print(x)
y = 100
y -= 50  # :y = y - 50
print(y)
z = 100
z *= 50  # :z = z * 50
print(z)
q = 100
q /= 50  # :q = q / 50
print(q)

  • 链式赋值
a = 999
b = a
c = a
print(a, b, c)  # :打印变量'a''b'''c'的值
a = b = c  # :简化写法
print(a, b, c)  # :打印变量'a''b'''c'的值

  • 交叉赋值
v = 3.141592653
w = 1234567890
tmp = v  # :创建一个临时变量名'tmp'
v = w
w = tmp
print(v, w)
v, w = w, v  # :简化写法
print(v, w)

  • 解压赋值
    分步解压
student_height_list = ['175cm', '180cm', '177cm', '187cm', '190cm', '188cm', '185cm']  # :创建学生身高列表
student_height1 = student_height_list[0]
print(student_height1)
student_height2 = student_height_list[1]
print(student_height2)
student_height3 = student_height_list[2]
print(student_height3)
student_height4 = student_height_list[3]
print(student_height4)
student_height5 = student_height_list[4]
print(student_height5)
student_height6 = student_height_list[5]
print(student_height6)
student_height7 = student_height_list[6]
print(student_height7)


一步到位

student_height_list = ['175cm', '180cm', '177cm', '187cm', '190cm', '188cm', '185cm']  # :创建学生身高列表
height1, height2, height3, height4, height5, height6, height7 = student_height_list
print(height1, height2, height3, height4, height5, height6, height7)

这篇关于Python基本运算符的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!