if 条件
a = 1
b = 2
c = 3
d = 1
if a > b:
print(“right”)
if a >= d:
print(“right”)
if a == d:
print(“right”)
if a != b:
print(“right”)
if a < b < c:
print(“right”)
if a > b < c:
print(“right”)
if 1 > 100:
print(“right”)
else:
print(“wrong”)
if a == b:
print(“a == b”)
elif a == c: #python中使用的elseif 简写为elif
print(“a == c”)
elif a == d:
print(“a==d”)
else:
print(a)
if a == b:
print(“a == b”)
elif a == c:
print(“a == c”)
elif a == d:
pass #满足条件但是并不执行相关的的操作,直接pass就可以了
else:
print(a)
if a < b and a == d: #and 用于并列,两个条件均满足的时候,才会成立
print(“right”)
if a > b or a == d: # or 用于表示或者,只要其中一个条件满足就会成立
print(“right”)
colors = [‘red’, ‘bule’, ‘black’, ‘yellow’]
for color in colors: #这里的for循环会轮流带入列表中的元素赋予color
if color == ‘black’:
print(“color is black”)
else:
print(“color is not black”)
colors = [‘red’, ‘bule’, ‘black’, ‘yellow’]
for color in colors: #这里的for循环会轮流带入列表中的元素赋予color
if color == ‘black’:
break #跳出大循环
print(“color is black”)
else:
print(“color is not black”)
colors = [‘red’, ‘bule’, ‘black’, ‘yellow’]
for color in colors: #这里的for循环会轮流带入列表中的元素赋予color
if color == ‘black’:
continue #跳出单次循环
print(“color is black”)
else:
print(“color is not black”)
if ‘red’ in colors:
print(“red is here”) #判断列表中是否存在某个值
#函数
def function():#定义一个函数
a = 1
b = 2
c = a + b
print(‘a=’, a)
print(‘b=’, b)
print(‘c=’, c)
print(‘a + b =’, a + b)
function() #在外调用这个函数
def function2(a,b): #定义一个传参函数
c = a + b
print(‘a + b=’, c)
function2(10,20) #传递参数到函数中
a2 = 5
b2 = 6
function2(a2,b2) # 另一种传递参 数的方式
def function3(a=20,b=30): #可以对参数设定一个初始的值
print(‘a=’, a)
print(‘b=’, b)
print(‘a+b=’, a+b)
function3()
function3(15,15)#可以将设定值赋给函数
a=3
function3() #运行这条函数会发现当局部变量和全局变量重名的时候,函数默认使用局部变量
def function5(b=30): #可以对参数设定一个初始的值
global a #使用全局变量
print(‘a=’, a)
print(‘b=’, b)
print(‘a+b=’, a+b)
a=13
function3()
def add(m,n): #定义一个函数可以返回值
g = m + n
return g
add = add(1,5)
print(add)