您的当前位置:首页正文

python之几个典型的语句

2024-11-09 来源:个人技术集锦

1 while

condition=1;
while(condition<10):
    print(condition)
    condition=condition+1;
condition=1;
while condition<10:
    print(condition)
    condition=condition+1;

2 for

examle_list=[12,234,454,343,2,2,334,123,545,256,33,24,26,26]
for i in examle_list:
    print(i)
    print("in for")  这个语句还是在for循环里面
print('out of for')  这个语句由于没有四个空格 所以是在for循环外面

输出1-9
for i in range(1,10):
    print(i) 

输出1 3 5 7 9

range(,,stepsize)

for i in range(1,10,2):
    print(i)

3 if

x,y,z=1,2,3
if x<y<z:
    print('x is less than y and y is less than z') 正确
x,y,z=1,2,0
if x<y>z:
    print('x is less than y and y is more than z') 正确
x,y,z=1,2,0
if x<=y:
    print('x is less than y or eaual to y') 正确
x,y,z=2,2,0
if x==y:
    print('x is eaual to y') 正确 不能写x=y =是赋值运算 ==才表示判断是否相等
x,y,z=1,2,0
if x!=y:
    print('x is not eaual to y') 正确

4 if else

x,y,z=1,2,0
if x<y:
    print('x is less than y')
else:
    print('x is greater than y')

5 if elif else

elif=else if

x,y,z=1,2,0
if x<1:
    print('x is less than 1')
elif x>1:
    print('x is greater than y')
else:
    print('x is equal to 1')

只能打印出x<-1这个结果:

x,y,z=-3,2,0
if x>1:
    print('x is more than 1')
elif x<-1:
    print('x is less than -1')
elif x<-2:
    print('x is less than -2')
else:
    print('nothing')

 

Top