表达式                                 

       表达式(Expression)是运算符(operator)和操作数(operand)所构成的序列

>>> 1 + 1
2
>>> a = [1,2,3]
>>> 1 + 1 + 1 + 1
4
>>> 1 + 2 * 3
7
>>> 1 * 2 + 3
5
>>> a = 1 + 2 * 3
>>> a = 1
>>> b = 2
>>> c = a and b or c
>>> c = int('1') + 2

运算符优先级

  Python3(四) 分支、循环、条件与枚举 Python

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

同级的运算符的优先级还是有区别的 比如逻辑运算符里的and的优先级大于or

 

>>> a = 1
>>> b = 2
>>> c = 3
>>> a + b * c
7
>>> 1 or 2
1
>>> 1 and 3
3
>>> a or b and c
1
>>> a or (b and c)
1
>>> a or 3
1
>>> (a or b) and c
3
>>> (a or b) and (c + 1)   //两个括号同级,左结合
4
>>> a = 1
>>> b = 2
>>> c = a + b    //出现赋值符号时,右结合
>>> print(c)    
3
>>> c = a or b
>>> print(c)

 

推荐的IDE:PyCharm、vsCode,大型工程适合用PyCharm,学习适合用vsCode,vsCode中推荐的插件:python、Terminal、Vim、vscode-icons

注释

 单行注释用#
多行注释用 ''' 或者  """

 

流程控制语句

主要有条件控制(if else)、循环控制(for while)、分支

条件控制(if else)

 

# encoding: utf-8

mood = False

if mood :
    print('go to left')
    # print('back away')
# print('back away')
else :
    print('go to right')
    
a = 1
b = 2
c = 2
# if后面不仅可以是布尔值,还可以是表达式
if a or b + 1 == c :
    print('go to left')
    # print('back away')
# print('back away')
else :
    print('go to right')
# encoding: utf-8
"""
    一段小程序
"""
# constant 常量  建议全大写
ACCOUNT = 'hughie'
PASSWORD = '123456'
# python变量建议都用小写,用下划线分隔单词,不用驼峰命名
print('please input account')
user_account = input()

print('please input password')
user_password = input()

if ACCOUNT == user_account and PASSWORD == user_password:
    print('success')
else:
    print('fail')
# encoding: utf-8
# snippet 片段

if condition:
    pass
else:
    pass


a = True
if a:
    # pass 空语句/占位语句
    pass
else:
    print('')


if True:
    pass
if False:
    pass

# 嵌套分支
if condition:
    if condition:
        pass
    else:
        pass
else:
    if condition:
        pass
    else:
        pass

# 代码块
if condition:
    code1
        code11
        code22
            code333
            code444
                code5555
                code6666
    code2
    code3
else:
    code1
    code2
    code3

改写为elif

 

# encoding: utf-8
"""
a = x

a = 1 print('apple')
a = 2 print('orange')
a = 3 print('banana')

print('shopping')
"""

a = input()
print('a is' + a)
if a == 1:
    print('apple')
else:
    if a == 2:
        print('orange')
    else:
        if a == 3:
            print('banana')
        else:
            print('shopping')


# 改写为elif
a = input()
print(type(a))
print('a is ' + a)
a = int(a)
if a == 1:
    print('apple')
elif a == 2:
    print('orange')
elif a == 3:
    print('banana')
else:
    print('shopping')

循环(while for)

 

# encoding: utf-8
# 循环

# 循环语句

# while     for
# CONDITION = True
# while CONDITION:
#     print('I am while')

counter = 1
# 递归常用while
while counter <= 10:
    counter += 1
    print(counter)
else:
    print('EOF')
# encoding: utf-8

# 主要是用来遍历/循环 序列或者集合、字典
# a = ['apple', 'orange', 'banana', 'grape']
# for x in a:
#     print(x)

# a = [['apple', 'orange', 'banana', 'grape'], (1, 2, 3)]
# for x in a:
#     for y in x:
#         # print(y, end='')
#         print(y)
# else:
#     print('fruit is gone')


# a = [1, 2, 3]

# for x in a:
#     if x == 2:
#         # break 遇到x==2的时候终止,打印出1
#         # break
#         # continue 遇到x==2的时候跳过,打印出1,3
#         continue
#     print(x)
# else:
#     print('EOF')


a = [['apple', 'orange', 'banana', 'grape'], (1, 2, 3)]
for x in a:
    # if 'banana' in x:
    #     break
    for y in x:
        if y == 'orange':
            # 内部循环跳出后,外部循环还在执行
            break
        print(y)
else:
    print('fruit is gone')
# encoding: utf-8
# for (i=0; i<10; i++){}

# 以上的for循环用python实现
# for x in range(0, 10):
#     # range(0,10) 表示从0开始的10个数字,并不包含10
#     print(x)


# for x in range(0, 10, 2):
#     # range(0,10,2) 2表示步长
#     print(x, end=' | ')
#     # 打印结果:0 | 2 | 4 | 6 | 8 |


for x in range(10, 0, -2):
    print(x, end=' | ')
    # 打印结果:10 | 8 | 6 | 4 | 2 |
# encoding: utf-8

a = [1, 2, 3, 4, 5, 6, 7, 8]

# for i in range(0, len(a), 2):
#     print(a[i], end=' | ')
    # 1 | 3 | 5 | 7 | 

b = a[0:len(a):2]
print(b)
    # [1, 3, 5, 7]

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄