《Python编程从入门到实践》——学习python的第五天

Python学习第五天

前言

今天是学习Python的第五天,在学好编程的同时我们也需要控制好自己编程的格式。

程序员的个人修养之设置代码格式:

1. 使用缩进,每次缩进四个空格符,但是python程序员使用的是  \t  缩进符
2. 控制python每行的长度 每行不要超过80个字符
3. 要学会将程序不同部分的用空行分隔开

学习还没结束,咱们直接开肝。

if语句的使用

if表示如果如果if的中的条件符合那么就可以执行if语句中的命令。

其实相当于c语言中的 if 但是if和else 后面都需要加上冒号“:”,咱们直接看代码吧

cars = ['audi','bmw','subaru','toyota']#这里先是创建了一个列表

for car in cars:#使用for循环进行遍历
    if car == 'bmw':#当遍历到了‘bmw’,就将bwm以大写的形式打印出来
        print(car.upper())
    else:
        print(car.title())#其它的以首字母大写的形式显示出来

结果:
Audi
BMW
Subaru
Toyota

除开‘bmw’外其它的输出全是首字母大写,而‘bmw’是每个字母都大写

条件测试(检验条件是否正确)
每个if语句的核心就是 True or False
检查是否相等

>>> car = 'bwn'
>>> car == 'bwn'
True
>>> car == 'aduio'
False

检查相等时是否忽略大小写

> car == 'BWN'
False
>>> car = 'Bwm'
>>> car.upper() == 'BWM'
True
>>> car
'Bwm'

不忽略大小写,这样的好处是能够很好的区分不同的用户名,保证用户的唯一性,说明大小写是会影响条件判断的

检查是否不相等
此处的不相等 通c语言中的!= 一样

requested_topping = 'mushroom'
if requested_topping != 'anchovies':
    print("Hold the anchovies")

结果:
Hold the anchovies

因为不相等所以为True,所以成功的打印出print

数值比较

>>> age = 19
>>> age < 21
True
>>> age <=21
True
>>> age >21
False
>>> if answer != 21:
    print("That is not the correct answer.Please try again!")

>>> if age != 21 :
    print("That is not the correct answer.Please try again!")

结果:
That is not the correct answer.Please try again!

检查多个条件
这里使用的是and,可使两个条件并起来

>>> age_0 = 18
>>> age_1 = 19
>>> age_0 >=21 and age_1>=21 #为了代码的可读性 建议修改为(age_0 >=18)and (age_1>=18)使用括号括起来
False
>>> age_1 =19
>>> age_0 >=18 and age_1 >=18
True

使用or来检测多个条件,只要其中一个条件满足就能够输出True

>>> age_0 >=18 or age_1>19
True
>>> age_0 >19 or age_1 >19
False

检查特定值是否在列表中
相当于检查某个元素是否存在列表中

>>> request_topping = ['mushroom','onions','pineapple']
>>> 'mushroom' in request_topping
True

检查特定值是否不包含在列表中

banned_users = ['andrew','carolina','david']
user = 'marie'

if user not in banned_users:#该处使用了语句 if 变量 not in 列表
    print(f"{user.title()},you can post a response if you wish.")

结果:
Marie,you can post a response if you wish.

布尔表达式
布尔表达式只是条件检测的别名,布尔表达式的结果要不是 True 要不是 False

简单的if语句
最简单的if语句只有一个测试和一个操作
if+条件测试:
操作
这是 最简单if语句的格式

age = 19
if age>= 18:
    print('You have enough to vote!')
    print('Have you registered to vote yet?')

结果:
You have enough to vote!
Have you registered to vote yet?

如果不符合条件将不会有任何输出

if_else语句
缺点是只能从两个条件中判断

age = 17
if age>= 18:
    print('You have enough to vote!')
    print('Have you registered to vote yet?')
else:
    print("You are too young to vote")
    print("Please register to vote as soon as you turn 18!")

结果:
You are too young to votePlease register to vote as soon as you turn 18!

if_elif_else结构
if_elif_else 在这里可实现三个条件的判断

age = 20
if age<4:
    print("Your admisson is free")
elif age<18:request_toppings = ['mushroom','extra cheese']#对多个条件进行检测时,我们使用的是单个if语句而不是if_elif_else结构来进行检测的

else:#用于最后一个条件判断
    print("Your admission cost is $50")

结果:
Your admission cost is $50

今天的主要是围绕if语句来讲的,不多但是值得我们仔细消化的,加油。
有什么错误的地方或者可以更改的地方欢迎评论区指出,谢谢!!!

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值