条件语句
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 即使条件状态为真 也跳出循环
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 即使条件状态为真 也跳出循环