VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > python入门教程 >
  • python的一些运算符

复制代码
# 1.算术运算符
print('1.算术运算符')
# 1.1 + 求和
a = 10
b = 20
c = a + b
print(c)
print('a+b={}'.format(c))
print('a+b=%i' % c)
print(f'a+b={c}')

# 1.2 - 求差
print('a - b =', a - b)

# 1.3 * 乘法
print('a * b =', a * b)

# 1.4 / 除法
print('a / b =', a / b)  # 0.5

# 1.5 % 取余
print('7 % 3 =', 7 % 3)

# 1.6 ** 次方
print('2 ** 3 = ', 2 ** 3)

# 1.7 // 取整
print('7 // 3 =', 7 // 3)  # 2


# 2.比较运算符
print('2.比较运算符')
# 2.1 == 判断两边是否相等 输出bool类型
print(2.1)
print('1+1 == 2', 1+1 == 2)
print('1+1 == 3', 1+1 == 3)
print('a == a', 'a' == 'a')

# 2.2 != 判断两边是否不相等
print(2.2)
print('1+1 != 3', 1+1 != 3)

# 2.3 >
print(2.3)
print('2 > 1', 2 > 1)

# 2.4 <
print(2.4)
print('2 < 1', 2 < 1)

# 2.5 >=
print(2.5)
print('1 >= 1', 1 >= 1)

# 2.6 <=
print(2.6)
print('2 <= 2', 2 <= 2)


# 3.赋值运算符
print('3.赋值运算符')

# 3.1 =
a = 10

# 3.2 +=  c+=a == c=c+a
# -= *= /= %= **= //=
print(3.2)
a = 1
b = 2
c = a + b
print(c)
a += b
print(a)


# 4.位运算符 &与  |或  ^异或  ~取反  <<左移动  >>右移动  用得少
print('4.位运算符')

# 4.1 二进制转换为十进制
print(4.1)
a = 60
# 查看a的二进制 bin()
print(bin(a))  # 00111100
b = '0b111100'  # 0b代表可以在0b这里补位
#                 b是二进制标志
print(int(b, 2))  # 将二进制数b转换为十进制数


# 5.逻辑运算符
print('5.逻辑运算符')

# 5.1 布尔与 and 两边同时满足,结果为True,否则False
print(5.1)
print(1+1 == 2 and 2+2 == 4 and 3+3 == 6)
print(1+1 == 2 and 2+2 != 4)

# 5.2 布尔或 or 只要有一边成立,则为True,否则False
print(5.2)
print(1+1 == 2 or 2+2 != 4)

# 5.3 布尔非 not 真假倒置
print(5.3)
print(not False)
print(not 1+1 == 2)


# 6.成员运算符 in  not in
print('6.成员运算符')

# 6.1 in
print(6.1)
a = 'a'
b = 'abc'
print(a in b)  # a是否在b中

# 6.2 not in
print(6.2)
print(a not in b)


# 7.身份运算符 is  is not
print('7.身份运算符')
a = 1
b = 2
print(a is b)  # id(a) == id(b)
print(id(a) == id(b))
print(a is not b)


# 今日作业
print('今日作业')
a = 100
b = 200
c = 'abc'
print(a + b)
print(a ** 3)
print(a == 100)
print(a > 100)
print(c == 'cba')
print(a == b)
print('a' in c)
复制代码

 

 

出处:https://www.cnblogs.com/qc2012/p/16830015.html


相关教程