运算符和操作所构成的序列
>>> a = 1
>>> b = 2
>>> c = 3
>>> print(a+b*c)
7
>>> print(a and b or c) #先做and运算返回b,再做or运算,返回b,not > and (取后面的值)>or(取前面的值)
2优先级高到底执行,同级从左到右执行。
括号的优先级最高,多个括号从左到右执行。
左结合。
>>> not (a or b) +2 == c
Truea or b -> a
a + 2 ->3
not 3 == c -> True
python靠缩进来区分代码块
选择性问题可以用if else
step = True
if step:
print("strp step step")
输出: strp step step
step = True
if step:
print("strp step step")
print("strp step step")
print("-------------")
输出:
strp step step
strp step step
-------------step = False
if step:
print("strp step step")
print("strp step step")
else:
print("no step")
输出: no step
if 后面可以加上条件python 建议使用下划线分割两个单词
step = []
if step:
print("strp step s
else:
print("no step")
输出:no step账号密码的实例ctrl + alt + l 代码整理
ctl + shift + F10 运行脚本
一个py文件就是模块,模块开头建议加上三引号,写上模块说明
程序末尾需要空一行,冒号前面不能有空格,比较运算符左右空一格
account = "a"
password = "1"
u_account = input("请输入账号")
u_password = input("请输入密码")
if u_account == account and u_password == password:
print("登录成功")
else:
print("账号或者密码错误")
输出:
请输入账号a
请输入密码1
登录成功
pass 空语句/占位语句
if True:
pass
if False:
pass嵌套使用 if else过多的层次,代码看起来不优雅。可以把具体的逻辑写在函数里面
if condition1:
code1
if condition2:
code3
else:
code4
if condition3:
code5
else:
code6
else:
code2根据变量的值做不同的事情,实战dict_1 = {"1": "苹果", "2": "沃柑"}
user_input = input("请输入值")
if user_input in dict_1:
print(dict_1[user_input])
else:
print("输入的值不正确")if elif elseinput()得到的值是字符串类型
if user_input == 1 动态语言不会报错,所以在写代码的时候要注意类型
user_input = input("请输入值")
if user_input == "1":
print("苹果")
elif user_input == "2":
print("沃柑")
else:
print("输入的值不正确")返回真的值,实战a = 0
b = 1
# 方法一
print(a or b)
# 方法二
if a == True:
print(a)
elif b == True:
print(b)名字全部大写
密码破解:穷举
while condition:
code基本的wile语句容易出现无限循环,需要写改变条件的语句
condition = 1
while condition < 10:
print(condition)
condition += 1
else:
print(">=10")
输出:
1
2
3
4
5
6
7
8
9
>=10递归用whilefor target_list in expression_list:
passexpression_list = ["a", "b", "c", "d"]
for x in expression_list:
print(x)
输出:
a
b
c
d嵌套expression_list = [["a", "b"], {"c", "d"}, (1, 2, 3)]
for x in expression_list:
for y in x:
print(y, end="")
print()
输出:
ab
dc
123
for elseexpression_list = [["a", "b"], {"c", "d"}, (1, 2, 3)]
for x in expression_list:
for y in x:
print(y, end="")
print()
else:
print("列表元素遍历完成,执行else语句")
输出:
ab
dc
123
列表元素遍历完成,执行else语句 break 中止循环,continue 继续,不执行本次循环了,开始下一次的循环expression_list = [1, 2, 3, 4, 5]
for x in expression_list:
if x == 2:
continue
elif x == 4:
break
print(x)
输出:
1
3
通过break结束,如果不是for循环的正常结束,不会执行else的语句expression_list = [1, 2, 3, 4, 5]
for x in expression_list:
if x == 2:
continue
elif x == 4:
break
print(x)
else:
print("else else else")
输出:
1
3多维度循环两个嵌套的循环,break只是跳出来内部的循环,不影响外部循环的执行。
外部else 与外部的循环是一个代码块,内部的break不影响外部else执行
expression_list = [[1, 2, 3, 4, 5, 6, 7]]
for y in expression_list:
for x in y:
if x == 2:
continue
elif x == 4:
break
print(x)
else:
print("内部的else")
else:
print("else else else")
输出:
1
3
else else elseexpression_list = [[1, 2, 3, 4, 5, 5, 7], ["a", "b", "c"]]
for y in expression_list:
for x in y:
if x == 2:
continue
elif x == 4:
break
print(x)
else:
print("内部的else")
else:
print("else else else")
输出:
1
3
a
b
c
内部的else
else else else一个代码执行10次
for x in range(0,10):
print(x)
输出:
0
1
2
3
4
5
6
7
8
9递增和递减序列
for x in range(0, 10, 2):
print(x, end="|")
print()
for x in range(10, 0, -2):
print(x, end="|")
输出:
0|2|4|6|8|
10|8|6|4|2|打印出1357
a = [1, 2, 3, 4, 5, 6, 7, 8]
print(a[0:len(a):2])
for i in range(0, len(a), 2):
print(a[i], end="|")
输出:
[1, 3, 5, 7]
1|3|5|7| 版权声明:我们致力于保护作者版权,注重分享,被刊用文章【r语言for循环语句案例(python编程)】因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!;
工作时间:8:00-18:00
客服电话
电子邮件
beimuxi@protonmail.com
扫码二维码
获取最新动态
