Python基础语法(二):选择语句if else elif、while循环、for循环、for-else、while-else

本文介绍了Python编程中的if-else、多分支结构、while循环和for循环的基本概念及示例应用,包括单分支、双分支和多分支判断,以及循环结构在实际场景中的使用。
摘要由CSDN通过智能技术生成

双分支


if 判断语句:

    执行语句

    ...

else:

    执行语句

    ...



多分支


if 判断语句:

    执行语句

    ...

elif 判断语句:

    执行语句

    ...

else:

    执行语句

    ...



示例:


# 单分支

ageStr = input("Input your age:")

age = int(ageStr)

if age > 10:

    print("age:{}".format(age))

# -------------------------------------------



# 双分支

numStr = input("Input your data:")

num = int(numStr)

if num > 100:

    print("num > 100")

else:

    print("num <= 100")

# --------------------------------------------



# 多分支

dayStr = input("Input a number:")

day = int(dayStr)

if day == 1:

    print("Monday")

elif day == 2:

    print("Tuesday")

elif day == 3:

    print("Wednesday")

elif day == 4:

    print("Thursday")

elif day == 5:

    print("Friday")

elif day == 6:

    print("Saturday")

elif day == 7:

    print("Sunday")



二、while循环语句

======================================================================


while 判断语句:  # 当判断语句为False时结束

    执行语句1    # 缩进表示该执行语句在while结构中

    执行语句2

    ...



示例(猜拳游戏):


import random  # 导入随机数模块

count = 1

personScore, computerScore = 0, 0



while count <= 5:

    print("当前得分Person:Computer 为 {} : {}".format(personScore, computerScore))

    person = int(input('请出手:[0:石头 1:剪刀 2:布]: ')) # 数据类型转换

    computer = random.randint(0, 2)  # 取0~2的随机int型整数



    if person==0 and computer==1:

        print("你赢得一分")

        count += 1

        personScore += 1



    elif person==0 and computer==2:

        print("电脑赢得一分")

        count += 1

        computerScore += 1



    elif person==1 and computer==0:

        print("电脑赢得一分")

        count += 1

        computerScore += 1



    elif person==1 and computer==2:

        print("你赢得一分")

        count += 1

        personScore += 1



    elif person==2 and computer==0:

        print("你赢得一分")

        count += 1

        personScore += 1



    elif person==2 and computer==1:

        print("电脑赢得一分")

        count += 1

        computerScore += 1



    elif person==computer:

        print("平局,不计分")

        

print("最终比分 Person:Computer")

print("          {} :  {}".format(personScore, computerScore))



if personScore>computerScore:

    print("你胜利了!")

else:

    print("电脑获胜了!")



三、for循环语句

====================================================================

语法


for 变量名 in 数据容器(集合):

    执行语句

    ...

# 变量代表容器中的每一项数据,循环就是遍历容器中的每一项数据

# 变量名可以自定义且只能在该循环结构中使用



示例:


class = ' TaiZhou电子与信息工程学院 ' # 字符串本身就是一个容器



for item in class:

    print(item)

    pass  # 空语句,不做任何操作



range函数

生成一个数据集合列表,语法为


range(起始值,终止值,步长)

# 该集合包含初始值,最大为终止值-1,即左闭右开
**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**

**深知大多数Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**

**因此收集整理了一份《2024年Python开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**

![img](https://img-blog.csdnimg.cn/img_convert/deb5ae67efaf59139047ca38dabed656.png)

 

![img](https://img-blog.csdnimg.cn/img_convert/66ac12399c02cdd856208e99e0387f10.png)

![img](https://img-blog.csdnimg.cn/img_convert/46506ae54be168b93cf63939786134ca.png)

![img](https://img-blog.csdnimg.cn/img_convert/252731a671c1fb70aad5355a2c5eeff0.png)

![img](https://img-blog.csdnimg.cn/img_convert/6c361282296f86381401c05e862fe4e9.png)

![img](https://img-blog.csdnimg.cn/img_convert/9f49b566129f47b8a67243c1008edf79.png)

 

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!**

**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**

**如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注Python)**

/img-blog.csdnimg.cn/img_convert/6c361282296f86381401c05e862fe4e9.png)

![img](https://img-blog.csdnimg.cn/img_convert/9f49b566129f47b8a67243c1008edf79.png)

 

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!**

**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**

**如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注Python)**

<img src="https://img-community.csdnimg.cn/images/fd6ebf0d450a4dbea7428752dc7ffd34.jpg" alt="img" style="zoom:50%;" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值