1.if else 流程控制
-
/*1.py*/
-
#!/usr/bin/python
-
if 1<2: /*前面有冒号*/
-
print "ok" /*统一空四个空格*/
-
print "ok" /*统一空四个空格,在同一个运算条件中*/
-
/*相当于C 语言中的{ }将两行,扩起来*/
-
/*************************************************
-
打印出两个ok
-
****************************************************/
-
执行: python 1.py
-
ok
-
ok
-
***************************************************
/*2.py*/
-
#!/usr/bin/python
-
if True: /*以冒号结束 ,首字母大写 True False*/
-
print "ok" /*四个空格*/
-
print "false" /* 当条件不成立时执行,print "false"*/
/*Flase.py*/
-
#!/usr/bin/python
-
if False:
-
print "ok"
-
print "false"
2。增加函数
-
#!/usr/bin/python
-
def fun():
-
return 1
-
if fun():
-
print "ok"
-
/********************/
-
python 1.py
-
ok
3.else
-
#!/usr/bin/python
-
def fun():
-
return 1
-
if 0:
-
print "ok"
-
else: /*冒号*/
-
print "bad"
4.elif的用法
-
#!/usr/bin/python
-
def fun():
-
return 0
-
x=int(raw_input("please input:"))
-
if x>=90:
-
print "A"
-
elif x>=80:
-
print "B"
-
elif x>=70:
-
print "C"
-
else :
-
print "bad"
5逻辑关系.and or not 相当于 and 就是&
-
#!/usr/bin/python
-
x=int(raw_input("please input:"))
-
y=int(raw_input("please input:"))
-
if x>=90 and y>=90:
-
print "A"
-
elif x>=80:
-
print "B"
-
elif x>=70:
-
print "C"
-
else :
-
print "bad"
-
-
*************************
-
python 1.py
-
please input:90
-
please input:90
-
A
6.for循环
-
#!/usr/bin/python
-
for x in "abcd":
-
print x,"hello world"
-
**********************
-
python 1.py
-
a,hello world
-
b,hello world
-
c,hello world
-
d,hello world
7.range(10)
-
#!/usr/bin/python
-
for x in range(10):
-
print x, "hello world"
-
*************************
-
python 1.py
-
0 hello world
-
1 hello world
-
2 hello world
-
3 hello world
-
4 hello world
-
5 hello world
-
6 hello world
-
7 hello world
-
8 hello world
-
9 hello world
8.range(1,11,2) //第一个为初始值,第二个为结束值,第三个为步长
-
#!/usr/bin/python
-
for x in range(1,11,2):
-
print x, "hello world"
-
python 1.py
1 hello world
3 hello world
5 hello world
7 hello world
9 hello world
9.range(1,101)
-
#!/usr/bin/python
-
num=0
-
for x in range(1,101):
-
num+=x
-
print num
-
-
-
************
-
5050
-
#!/usr/bin/python
-
num=0
-
for x in range(1,101):
-
num+=x
-
print num
-
-
-
************
-
5050
10.for遍历列表中的值
-
#!/usr/bin/python
-
fruits=['bana','apple','man','str']
-
for x in range(len(fruits)):
-
print 'Current fruit:',fruits[x]
-
-
-
-
***********************
-
python 1.py
-
Current fruit: bana
-
Current fruit: apple
-
Current fruit: man
-
Current fruit: str
阅读(1156) | 评论(0) | 转发(0) |