python基础语法小案例(一)

版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/GXSeveryday/article/details/83036748

While循环

a = 1
while a<10:
    print(a)
    a = a +1;
    
结果:
1
2
3
4
5
6
7
8
9

for循环

list = [1,3,5,7,9]
for i in list:
    print(i)
    
结果:
1
3
5
7
9


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

结果:
1
2
3
4
5
6
7
8
9


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

结果:
1
3
5
7
9

if判断

x = 1
y = 2

if x<y:
    print("x 小于 y")
    
结果:
x 小于 y


x = 4
y = 2

if x<y:
    print("x 小于 y")
else:
    print("x 大于 y")
    
结果:x 大于 y


x = 1
y = 2
z = 3

if x>1:
    print("x>1");

elif x<1:
    print("x<1")

else:
    print("x=1")
    
结果:x=1

函数

def function():
    print("函数")
    a  = 1+2
    print(a)


function()

结果:
函数
3

带参数的函数

def fun(a,b):
    c = a*b
    print(c)


fun(2,3)

结果:
6



def sale(price,color,brand ,second,length,hight):
    print('price:',price,
          'brand',brand,
          'color',color,
          'second',second,
          'length',length,
          'hight:',hight)

sale(1000,'black','GXS','Ture','2.5','1200')

结果:
price: 1000 brand GXS color black second Ture length 2.5 hight: 1200

猜你喜欢

转载自blog.csdn.net/GXSeveryday/article/details/83036748