python_3 进阶(1)

表达式

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

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

运算符优先级

以下所列优先级顺序按照从低到高优先级的顺序;同行为相同优先级

Lambda  #运算优先级最低
逻辑运算符: or
逻辑运算符: and
逻辑运算符:not
成员测试: in, not in
同一性测试: is, is not
比较: <,<=,>,>=,!=,==
按位或: |
按位异或: ^
按位与: &
移位: << ,>>
加法与减法: + ,-
乘法、除法与取余: *, / ,%
正负号: +x,-x

注释

单行注释 #
多行注释 ''''''

条件判断

if...else 用来解决一些选择性的问题

mood = True
if mood:
    print('go to left!')
else:
    print('go to right!')
a = 1
b = 2
if a > b:
    print('go to left!')
else:
    print('go to right!')
# python编程规范,变量名最好用大写,if后面的冒号不要有空格,代码最后要留一行空行,每个缩进要是4个空格
ACCOUNT = 'qiyue'
PASSWORD = '123456'

print('please input account.')
USER_ACCOUNT = input()

print('please input password.')
USER_PASSWORD = input()

if USER_ACCOUNT == ACCOUNT and USER_PASSWORD == PASSWORD:
    print('success!')
else:
    print('fail')
# if可以单独使用,但else不能单独使用,pass表示空语句/占位语句(一般用来保证语句的完整性)
if True:
    pass

if False:
    pass

# if...else还可以嵌套使用
if condition:
    if condition:
        pass
    else:
        pass
else:
    if condition:
        pass
    else:
        pass
'''
elif的用法
假设 a = x
当 a = 1 print('apple')
当 a = 2 print('orange')
当 a = 3 print('banana')
否则 print('shopping')
'''
'''
a = input()
if a == '1':
    print('apple')
elif a == '2':
    print('orange')
elif a == '3':
    print('banana')
else:
    print('shopping')
'''

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

while 循环

# 死循环 while,使用场景可以理解为,给自己设定一个目标,当目标达到后则停止循环
CONDITION = True
while CONDITION:
    print('I am While')

# while 的其他用法
counter = 1
while counter:
    counter += 1
    print(counter)

# 使用语句控制 while 循环
counter = 1
while counter <= 10:
    counter += 1
    print(counter)

# while 循环结合 else
counter = 1
while counter <= 10:
    counter += 1
    print(counter)
else:
    print('EOF')

for 循环

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

# for 嵌套循环,把列表中的列表元素和元组元素都打印出来
a = [['apple', 'banana', 'orange', 'grape'], (1, 2, 3)]
for x in a:
    for i in x:
        print(i)

# 取消换行,元素横向打印
a = [['apple', 'banana', 'orange', 'grape'], (1, 2, 3)]
for x in a:
    for i in x:
        print(i, end='')

# for 和 else 搭配使用,当 for 循环运行结束后,else 的内容才会运行
a = [['apple', 'orange', 'banana', 'grape'], (1, 2, 3)]
for x in a:
    for i in x:
        print(i)
else:
    print('fruit is gone')

# 跳出整个循环 break,下面例子输出为 1
a = [1, 2, 3]
for x in a:
    if x == 2:
        break
    print(x)

# 跳出当前循环 continue,下面例子输出为 1 2 3 5 6
a = [1, 2, 3, 4, 5, 6]
for x in a:
    if x == 4:
        continue
    print(x)

# for 循环非正常结束(break),则不会运行 else 的内容,以下例子输出为 1 2 3
a = [1, 2, 3, 4, 5, 6]
for x in a:
    if x == 4:
        break
    print(x)
else:
    print('EOF')

# 下面例子输出的是 'apple' 1 2 3 'fruit is gone',因为嵌套循环,break 结束的是第一个列表的循环,对第二个元组的循环并不影响
a = [['apple', 'orange', 'banana', 'grape'], (1, 2, 3)]
for x in a:
    for i in x:
        if i == 'orange':
            break
        print(i)
else:
    print('fruit is gone')

range 函数

# for 循环 range 函数
# 从 0 开始,循环十次,以下例子输出为 0 1 2 3 4 5 6 7 8 9
for x in range(0, 10):
    print(x)

# 从 0 开始,循环十次,步长为 2,以下例子输出为 0 2 4 6 8
for x in range(0, 10, 2):
    print(x)

# 从 0 开始,循环十次,步长为 2,同时把输出整合为一行,用 | 分割
# 以下例子输出为 0 | 2 | 4 | 6 | 8
for x in range(0, 10, 2):
    print(x, end=' | ')

# 从 0 开始,循环十次,步长为 2,同时把输出整合为一行,用 | 分割,递减输出
# 以下例子输出为 8 | 6 | 4 | 2 | 0
for x in range(10, 0, -2):
    print(x, end=' | ')

# 指定一个列表,使用 for 循环结合 range
# 以下例子输出 1 | 3 | 5 | 7
a = [1, 2, 3, 4, 5, 6, 7, 8]
for x in range(0, len(a), 2):
    print(a[x], end=' | ')

# 使用切片的方式获得一样的结果
a = [1, 2, 3, 4, 5, 6, 7, 8]
b = a[0:len(a):2]
print(b)
pythonstudiedpython笔记python学习progresspython进阶

我来吐槽

*

*