r语言for循环语句案例(python编程)

 2025-08-28 16:27:01  阅读 583  评论 0

摘要:(一) 表达式运算符和操作所构成的序列(二) 表达式优先级>>> 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优先级高到底执行,同级从左到右执行。括号的优先级最高

(一) 表达式

运算符和操作所构成的序列

(二) 表达式优先级

>>> 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 
True

a or b -> a

a + 2 ->3

not 3 == c -> True

(三) 在文本文件中编写python代码

新建文件输入print("hello world")另存为 hello.pycmd+r 打开terminalcd 进入文件路径输入 python + 文件名即可运行文件

(四) 流程控制语句值条件控制if else

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 else

input()得到的值是字符串类型

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循环和使用场景

密码破解:穷举

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
递归用while

(七) for与for else循环

主要用来遍历/循环 序列或者集合、字典。
for target_list in expression_list:
    pass
expression_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 else
expression_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 else
expression_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

(八) for与 range

一个代码执行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进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!;

原文链接:https://www.yxiso.com/zhishi/2112361.html

发表评论:

关于我们
院校搜的目标不仅是为用户提供数据和信息,更是成为每一位学子梦想实现的桥梁。我们相信,通过准确的信息与专业的指导,每一位学子都能找到属于自己的教育之路,迈向成功的未来。助力每一个梦想,实现更美好的未来!
联系方式
电话:
地址:广东省中山市
Email:beimuxi@protonmail.com

Copyright © 2022 院校搜 Inc. 保留所有权利。 Powered by BEIMUCMS 3.0.3

页面耗时0.0408秒, 内存占用1.93 MB, 访问数据库24次

陕ICP备14005772号-15