Python入门系列(五)一篇搞懂python语句
2023-06-13 09:11:56 时间
If语句
elif关键字是pythons表示“如果前面的条件不为真,那么试试这个条件”。
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
如果只有一条语句要执行,则可以将其与If语句放在同一行。
if a > b: print("a is greater than b")
如果只有一条语句要执行,一条用于If,另一条用于else,则可以将所有语句放在同一行中
a = 2
b = 330
print("A") if a > b else print("B")
and关键字是一个逻辑运算符,用于组合条件语句
a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")
or关键字是一个逻辑运算符,用于组合条件语句
a = 200
b = 33
c = 500
if a > b or a > c:
print("At least one of the conditions is True")
循环语言
while语句
使用while循环,只要条件为true,我们就可以执行一组语句。
i = 1
while i < 6:
print(i)
i += 1
使用break语句,即使while条件为true,我们也可以停止循环
i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
使用continue语句,我们可以停止当前迭代,然后继续下一个迭代
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)
使用else语句,当条件不再为真时,我们可以运行一段代码
i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
for语句
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
for循环中的else关键字指定循环完成时要执行的代码块
for x in range(6):
print(x)
else:
print("Finally finished!")
for x in range(6):
if x == 3: break
print(x)
else:
print("Finally finished!")
#If the loop breaks, the else block is not executed.
相关文章
- Python该怎么入门?Python入门教程(非常详细)「建议收藏」
- 如何为pycharm配置Python解释器_pycharm选择python解释器
- 踩坑容易躲坑难!写在你开始学Python之前,帮你顺利入门
- Python入门系列(十)一篇学会python文件处理
- Python入门系列(十一)一篇搞定python操作MySQL数据库
- python中dtype的使用规范_Python numpy.dtype() 使用实例
- 0基础小白想学Python不知道怎么入门从何学起?十分钟带你快速入门 Python(初学者必看,收藏必备!!!)
- Python全栈工程师(字符串/序列)[通俗易懂]
- Python爬虫——爬取王者荣耀全英雄台词语音
- 【说站】python如何实现自定义异常类
- [Python从零到壹] 十八.可视化分析之Basemap地图包入门详解
- Python的特点是什么_python具有的特点
- python中if判断语句的用法_Python if判断语句的用法详细介绍[通俗易懂]
- Python熟练使用字典和了解元组与列表的区别
- 软件测试|如何用Python绘制雷达图
- Python-基础-if语句快速入门
- Python-基础-循环语句快速入门
- python入门之后须掌握的知识点(模块化编程、时间模块)【一】
- Python的应用领域
- Python标识符的命名规则有哪些?Python标识符命名规范及常见问题
- python-Django-Django 视图层简介(一)
- 提升Linux环境:升级Python(linux升级python)
- Python如何使用MySQL构建立连接(python怎么连接mysql)
- 从零学Python之入门(五)缩进和选择