表达式

       表达式(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)

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

Python3(四) 分支、循环、条件与枚举的更多相关文章

  1. [剑指offer] 29. 顺时针打印矩阵 (for循环条件)

    思路: 先定义左上和右下角点坐标,打印可分为从左到右,从上到下,从右到左,从下到上.依次判断最后一圈的四个循环条件. #include "../stdafx.h" #include ...

  2. Python3 的分支与循环

    1:条件分支 if 条件 : 语句 else: 语句 2.缩写 else: if : 可以简写为 elif ,因此Python 可以有效的避免"悬挂else" 举例: #悬挂els ...

  3. 第三次实验计算分段函数 第四次计算分段函数和循环NEW 第五次分支+循环加强版 实验报告

    一.实验题目,设计思路,实现方法 第四次分支+循环 加强版 (2-2计算个人所得税,2-7 装睡,2-8计算天数) 设计思路:2-2 用if-else的语句,与计算分段函数的题类似的做法:2-7 运用 ...

  4. Python基础学习参考(四):条件与循环

    在实际的开发中,想要实现某些功能或者需求,里面必然涉及到一些逻辑,复杂的也好简单也好,那么,通过python语法如何实现呢?这就涉及到了条件与循环.很显然绝大多数的语言都有条件和循环的语法,pytho ...

  5. python的分支循环

    知识内容: 1.if-else分支结构 2.while循环 3.for循环 4.循环结构综述 5.break和continue语句 一.if-else分支结构 1.单分支选择结构 if 表达式: 语句 ...

  6. python_分支循环

    什么是分支+循环? --不同条件进行不同逻辑处理            -- 分支 --满足条件进行反复相同逻辑处理     -- 循环 分支的形式? -- if 条件:  执行体   else: 执 ...

  7. 第十九节:Java基本数据类型,循环结构与分支循环

    基本数据类型 Java中的基本数据类型,有8种,在Java中有四种类型,8种基本数据类型. 字节 boolean 布尔型为1/8 byte 字节类型为1 short 短整型为2 char 字符型为2 ...

  8. 分支&循环

    分支 单分支 if 条件: 满足条件后要执行的代码 双分支 if 条件: 满足条件执行代码 else: if条件不满足就走这段 多分支: if 条件: 满足条件执行代码 elif 条件: 上面的条件不 ...

  9. Java05-Java基础语法(四)循环结构

    Java05-Java基础语法(四)循环结构 循环结构(重复/迭代):根据条件重复执行部分语句 1.while循环结构 while(条件表达式){ 循环体语句; } 1)语法:a.while是关键字 ...

随机推荐

  1. 什么是aPaas?aPaas与低代码又是如何促进应用程序开发现代化的?

    从软件即服务(SaaS)到基础设施即服务(IaaS),云计算的兴起使“一切皆服务”(XaaS)模型得以泛滥,而aPaaS可能是这些模型中最鲜为人知的模型.随着aPaaS市场预计将从2018年的近90亿 ...

  2. Educational Codeforces Round 80 (Rated for Div. 2)

    A. Deadline 题目链接:https://codeforces.com/contest/1288/problem/A 题意: 给你一个 N 和 D,问是否存在一个 X , 使得 $x+\lce ...

  3. 关于爬虫的日常复习(14)—— 爬虫beautifulsoup的初级高级的基本用法

  4. docker创建mysql容器,并挂载数据+配置

    新建:/my/mysql/my.cnf (准备挂载配置文件用) 将以下内容拷贝进去(或者启动一个docker的mysql,并且把/etc/mysql/my.cnf中的内容拷贝出来) # Copyrig ...

  5. LOJ6053 简单的函数

    题目传送门 分析: 对于这道题来说,当\(x\)为质数时: \(~~~~f(x)=x-1+2[x=2]\) 因为除2以外的质数都是奇数,它们与1异或就是减一,然后2就是加一 然后我们先来康康怎么快速求 ...

  6. 51Nod 1238 最小公倍数之和V3

    题目传送门 分析: 现在我们需要求: \(~~~~\sum_{i=1}^{n}\sum_{j=1}^{n}lcm(i,j)\) \(=\sum_{i=1}^{n}\sum_{j=1}^{n}\frac ...

  7. atx测试框架实现手机应用UI自动化测试

    最近工作中遇到游戏APP需要实现UI自动化测试,这个app中真的是典型的混合App,有Android原生控件,有webview控件,以及游戏操作页面.研究了Appium,发现appium实现跨应用操作 ...

  8. python数据类型之字典操作

    Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型.一.创建字典字典由key和value成对组成.基本语法如下: infos = {"name&q ...

  9. 3分钟接入socket.io使用

    WebSocket 简介 传统的客户端和服务器通信协议是HTTP:客户端发起请求,服务端进行响应,服务端从不主动勾搭客户端. 这种模式有个明显软肋,就是同步状态.而实际应用中有大量需要客户端和服务器实 ...

  10. xhsell关闭jupyter仍然运行的命令

    nohup jupyter notebook & nohup 和 &都是linux的命令 1.& 当在前台运行某个作业时,终端被该作业占据:可以在命令后面加上& 实现后 ...