资讯专栏INFORMATION COLUMN

python学习笔记4---分支、循环、条件与枚举

livem / 2813人阅读

摘要:表达式表达式是运算符和操作数所构成的序列运算符优先级同级的运算符的优先级还是有区别的比如逻辑运算符里的的优先级大于两个括号同级,左结合出现赋值符号时,右结合优先级在文本文件中编写代码脚本是后缀名为的文件,通过命令行执行推荐的,大型工程适合用

表达式
表达式(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
运算符优先级

同级的运算符的优先级还是有区别的 比如逻辑运算符里的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)
1
>>> a = 1
>>> b = 2
>>> c = 2
>>> not a or b + 2 == c
False
>>> ((not a) or ((b + 2) == c))    //优先级:not > and > or
False
在文本文件中编写Python代码


python脚本是后缀名为.py的文件,通过命令行“python filename.py”执行

推荐的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]

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/44904.html

相关文章

  • c语言入门笔记

    摘要:注在语言中如何表示真假呢表示真,非表示假。用于终止本次循环,也就是本次循环中后面的代码不会再执行,而是直接跳转到语句的判断部分,进入下一次循环判断。形式参数当函数调用完成之后就自动销毁了。 目录 一、初识c语言 1、什么是C语言? 2、第一个C语言程序 3、数据类型 4、变量、常量 5、转...

    sixleaves 评论0 收藏0
  • Python基础之控制结构

    摘要:基础之控制结构学习目标代码块与缩进条件语句语句语句的嵌套断言循环循环循环中断循环控制语句综合嵌套列表解析式基础相关链接学习目标是简洁易学面向对象的编程语言。 Py...

    liaosilzu2007 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<