学习python记录第四天 (1)

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值