栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python中运算符的使用及解释

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python中运算符的使用及解释

1.访问[(1, 10), (2, 20), (3, 30)]列表中元组的每个元素
for i, j in [(1, 10), (2, 20), (3, 30)]:
    print(i)
    print(j)
# 运行结果
# 1
# 10
# 2
# 20
# 3
# 30
2.打印9 * 9乘法表
for x in range(1, 10):           # 9*9乘法表
    print()
    for y in range(1, x+1):
        print(x, '*', y, '=', x*y, end='t')
"""
运行结果
1 * 1 = 1	
2 * 1 = 2	2 * 2 = 4	
3 * 1 = 3	3 * 2 = 6	3 * 3 = 9	
4 * 1 = 4	4 * 2 = 8	4 * 3 = 12	4 * 4 = 16	
5 * 1 = 5	5 * 2 = 10	5 * 3 = 15	5 * 4 = 20	5 * 5 = 25	
6 * 1 = 6	6 * 2 = 12	6 * 3 = 18	6 * 4 = 24	6 * 5 = 30	6 * 6 = 36	
7 * 1 = 7	7 * 2 = 14	7 * 3 = 21	7 * 4 = 28	7 * 5 = 35	7 * 6 = 42	7 * 7 = 49	
8 * 1 = 8	8 * 2 = 16	8 * 3 = 24	8 * 4 = 32	8 * 5 = 40	8 * 6 = 48	8 * 7 = 56	8 * 8 = 64	
9 * 1 = 9	9 * 2 = 18	9 * 3 = 27	9 * 4 = 36	9 * 5 = 45	9 * 6 = 54	9 * 7 = 63	9 * 8 = 72	9 * 9 = 81	
"""
3.运算符的使用: 算术运算符:所有的英文,以及使用样例
"""
+: add
-: sub
*: mul
/: div
%: mod
//:floordiv
**: pow"""

a = 3
b = 11
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a ** b)

"""
运行结果

14
-8
33
0.2727272727272727
0
3
177147
"""
比较运算符:所有的英文,以及使用样例
print(2 == 3)  # equal -> eq
print(2 != 3)  # not equal -> ne
print(2 > 3)  # greater than -> gt
print(2 >= 3)  # greater than equal ->  ge
print(2 < 3)  # less than: lt
print(2 <= 3)  # less than equal -> le


"""
运行结果

False
True
False
False
True
True
"""
位运算符:&, |, ~, 使用的样例,以及二进制操作的过程
&与  符的运算
# 1&1=1, 1&0=0, 0&0=0
print(10 & 8)
# 1010
# 1100
# 1000=> 8
print(2 & 5)
# 0010
# 0101
# 0000 =>0
结果:
8
0
|或 的运算
# 1&1=1, 1&0=1, 0&0=0
print(10 | 8)
# 1010
# 1000
# 1010 =>10
print(2 | 5)
# 0010
# 0101
# 0111 => 7
结果:
10
7
~非 的运算
print(~1)
# 原码为 0 0001
# 补码为:1 1110
# 减一为反码:1 1101
# 再转化为原码符号位不变 1 0010 => -2
# 符号位0代表正数,1代表负数
print(~10)
# 原码为:0 1010
# 补码为:1 0101
# 反码为:1 0100
# 再转化为原码:1 1011 = > -11
结果:
-2
-11
4.if的三种形式,举样例说明
"""
条件控制语句的第一种格式:
if condition:
    语句1
    语句2
    ...
condition 为真, 才执行if中的语句
condition w为假, 不执行
"""
score = 100
if score < 60:
    print("D")
if score >= 60:
    if score < 70:
        print("C")
if score >= 70:
    if score < 80:
        print("B")
if score >= 80:
    if score <= 100:
        print("A")
# 结果:A
"""
if语句的第二种形式:
if condition:
    语句1
    语句2
else:
    语句1
    语句2

# else是不能单独存在的,它必须和if成对存在。
"""
score = 50
if score <= 60:
    print("D")
else:
    if score <= 70:
        print("C")
    else:
        if score <= 85:
            print("B")
        else:
            print("A")
"""
if 语句的第三种形式
if condition1:
    语句1
elif condition2:
    语句2
elif condition3:
    语句3
......
elif conditionN:
    语句N
else:(可选)
    语句N+1
"""
score = 90
if score <= 60:
    print("D")
elif score <= 70:
    print("C")
elif score <= 85:
    print("B")
else:
    print("A")

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/991373.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号