【人工智能】python之控制语句练习

我们都知道程序等于数据结构和算法的集合、前面几节练习的都是数据结构,字符串,列表,元组,字典都是存储数据的结构。下面我们来练习算法,也就是我们经常操作的if  esle  ,for 循环的等等这类都是就是算法,正是数据结构加算法组成了程序。

# Clase en vídeo: https://youtu.be/Kp4Mvapo5kc?t=21442

### Conditionals ###

# if

my_condition = False

if my_condition:  # Es lo mismo que if my_condition == True:
    print("Se ejecuta la condición del if")

my_condition = 5 * 5

if my_condition == 10:
    print("Se ejecuta la condición del segundo if")

# if, elif, else

if my_condition > 10 and my_condition < 20:
    print("Es mayor que 10 y menor que 20")
elif my_condition == 25:
    print("Es igual a 25")
else:
    print("Es menor o igual que 10 o mayor o igual que 20 o distinto de 25")

print("La ejecución continúa")

# Condicional con ispección de valor

my_string = ""

if not my_string:
    print("Mi cadena de texto es vacía")

if my_string == "Mi cadena de textoooooo":
    print("Estas cadenas de texto coinciden")

 我们来运行一下代码

C:\Users\lenovo\AppData\Local\Programs\Python\Python39\python.exe D:\workspace\Hello-Python\Basic\08_conditionals.py 
Es igual a 25
La ejecución continúa
Mi cadena de texto es vacía

Process finished with exit code 0
 

接下来就是来分析一下条件语句的语法和使用技巧

#变量,赋予一个布尔值

my_condition = False

#条件语句,用if修饰 后面跟布尔值 和一个冒号:

下一行缩进,执行条件下的内容,显然因为布尔值是false ,所以不会打印

if my_condition:  # Es lo mismo que if my_condition == True:
    print("Se ejecuta la condición del if")

#变量

my_condition = 5 * 5

#布尔判断
if my_condition == 10:
    print("Se ejecuta la condición del segundo if")

#多个条件组合判断,多个分支,通过if  elif else 组合 

if my_condition > 10 and my_condition < 20:
    print("Es mayor que 10 y menor que 20")
elif my_condition == 25:
    print("Es igual a 25")
else:
    print("Es menor o igual que 10 o mayor o igual que 20 o distinto de 25")

#通过not 置换布尔值

my_string = ""

if not my_string:
    print("Mi cadena de texto es vacía")

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奋力向前123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值