python基础(9)

这篇博客详细介绍了Python中的条件语句,包括if、else和elif,以及循环结构如while和for循环,并探讨了range()函数和与序列相关的内置函数。文章还包含了多个练习题,涵盖从打印星号到求质数的各种编程练习。
摘要由CSDN通过智能技术生成
条件和循环
1、if语句
>>> if exception:
...     expr_true_suite
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'exception' is not defined
>>> if not warn  and (system_load >= 10):
...     print("warning:losing  resources")
...     warn += 1
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'warn' is not defined
2、else语句
>>> if expression:
...     expr_true_suite
... else:
...     expr_false_suite
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'expression' is not defined
>>> if passwad == user.passwad:
...     ret_str = "password accepted"
...     id = user.id
...     valid = True
... else:
...     ret_str = "invalid password entered  try again!"
...     valid = False
...
Traceback (most recent call last)
  File "<stdin>", line 1, in <module>
NameError: name 'passwad' is not defined
3、elif语句

If 语句后最多只能有一个else语句,但是可以有任意数量的elif语句

4、条件表达式
>>> x,y = 4,3
>>> if x < y:
...     smaller = x
... else:
...     smaller = y
...
>>> smaller
3

在这里插入图片描述

5、while语句
>>> count = 0
>>> while(count < 9):
...     print("the index is:",cou
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值