[python日志]2019年1月9日 21:51:27

版权声明: https://blog.csdn.net/weixin_38272395/article/details/86182658

python可以避免悬挂else

if xxx:

elif xxxx:

else:

x = a if a < b else b     #三元操作符

普通if else

a,b = 4,5

if a < b :
    x = a
else:
    x = b

断言(assert)

assert这个关键字我们称之为“断言”,当这个关键字后边的条件为假的时候,程序自动崩溃并抛出AssertionError的异常。

例如: 

assert 3 > 4

一般来说我们可以用Ta再程序中置入检查点,当 ,需要确保程序中的某个条件一定为真才能让程序正常工作的话,assert关键字就非常有用了。

循环:

  while 条件:

      循环体

for循环( py的for循环可以自动的调用迭代器的next()方法,自动补货stopIteration异常,并结束循环)

    for 目标 in 表达式:

             循环体

range()

语法: range( [strat,] stop[, step=1] )

-这个BIF有三个参数,其中用中括号括起来的两个表示这两个参数是可选的。

-step=1表示第三个参数的值默认值是1。

- range 这个BIF的作用是生成一个从start参数的值开始到stop参数的值结束的数字序列。
>>> range(5)
range(0, 5)
>>> list(range(5))
[0, 1, 2, 3, 4]
>>> for i in range(5):
	print(i)
	
0
1
2
3
4
>>> for i in range(2,9):
	print(i)
	
2
3
4
5
6
7
8
>>> for i in range(1,10,2):
	print(i)
	
1
3
5
7
9
>>> 

break  continue

猜你喜欢

转载自blog.csdn.net/weixin_38272395/article/details/86182658