条件语句和循环

条件语句
if <test expression>:
<Block(执行过程)>
else :
<Block>
定义绝对值函数
def absolute(x):
if x<0:
x=-x
return x


名字是不是D.N开头
def is_friend(x):
    
if x[0]=="D"  or  "N":
        
return True
    
else :
        
return False






def is_friend(x):
 
return x[0]=="D"  or  x[0]=="N"
通过算法。比较、程序和循环语句  可以表现出所有电脑程序的形式




求最大值
def bigger(a,b):
if a>b:
return a
else:
return b


def biggest(a,b,c):
return bigger(bigger(a,b),c)


def biggest(a,b,c):
return max(a,b,c)




循环
i=0
while i<10:
i=i+1
print i
输出1到10




消除字符串中的空格
def remove_spaces(text):
    
text_without_spaces = '' #empty string for now
    
while text != '':
        
next_character = text[0]
        
if next_character != ' ':    #that's a single space
            text_without_spaces = text_without_spaces + next_character
        
text = text[1:]
    
return text_without_spaces


print remove_spaces("hello my name is andy how are you?")




break 即使条件状态为真 也跳出循环
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值