首页 > 代码库 > if语句,for循环,while循环

if语句,for循环,while循环

pycharm必须用英文半角  注意空格使用  
if 循环语句:  if sequence:                                                                          if 1>2(True包括:表示非零数的所有值)   False:表示0,None,空的量等
                   print                                                                                           print "ok"         
           只能控制下边带缩进的代码,一定要缩进,一般用空格缩进四个,冒号不能丢    print "main ok"    \不受if控制\
           多个if同时使用应用:进行分隔,可以使用elif
           x = int(raw_input("please input x:"))
#coding:utf-8

x = int(raw_input("please input:"))
if x>=90:
    print "A"                           \三个if都执行且else只与第三个if有关系\                          

if x>=80:
    print "B"
if x>=70:
    print "C"
else:
    print "bad"

 

D:\Python安装程序\python.exe C:/Users/欢/PycharmProjects/untitled2/if.py
please input:90
A
B
C

Process finished with exit code 0

 

#coding:utf-8

x = int(raw_input("please input:"))                \if-elif分开来判断,互不相关且else是三个判断都不成立才执行的\
if x>=90:
    print "A"
elif x>=80:
    print "B"
elif x>=70:
    print "C"
else:
    print "bad"

D:\Python安装程序\python.exe C:/Users/欢/PycharmProjects/untitled2/if.py
please input:99
A

Process finished with exit code 0

嵌套模式
#coding:utf-8

x = int(raw_input("please input x:"))              \x和y同时成立才会输出\
y = int(raw_input("please input y:"))
if x>=90:
    if y>90:
        print "A"
elif x>=80:
    print "B"
elif x>=70:
    print "C"
else:
    print "bad"

 

#coding:utf-8

x = int(raw_input("please input x:"))
y = int(raw_input("please input y:"))
if x>=90:                                                    可以使用and,or,not
    if y>90:              \相当于if x>=90 and y>90:\
        print "A"    \ify<90成立则输出\
    print "y>=90"     \if y>90不成立则输出\
elif x>=80:
    print "B"
elif x>=70:
    print "C"
else:
    print "bad"

 

 


for循环(有次数的循环)
   循环语句:  for inerating_var in sequence:
                          print
             s=()元组print s[x]     s = "hello"                                         l=[]列表print l
                                               for x in range(len(s)):
                                                   print  s[x]     
            迭代:range(i,j ,[进步值])类似于切片 显示具体的数字             xrange(10)只显示xrange(10)不显示具体数字
              range(10)输出是十次且是从零到九   range(1,11)输出是十次且是从一到十   range(1,11,2)输出1,3,5,7,9


#coding:utf-8
s = "hello"
l = [1,2,3,‘a‘,‘b‘]
t = (7,8,89,‘x‘,‘f‘)
d = {1:111,2:222,5:555,3:3333}
for  x in l:      #在列表中取值的格式
    if x>=2:
        print x
for x in range(len(s)):       #字符串取值的方法
    print  s[x]
for x in d:
    print x      #只取出的是k
    print d[x]     #只取出v的值                   \一个for下边有多个print,三个print会同时执行多次\
    print d.items()   #取出(k,v)的键值对
for k,v in d.items():
    print k
    print v

D:\Python安装程序\python.exe C:/Users/欢/PycharmProjects/untitled2/if.py
2
3
a
b
h
e
l
l
o
1
111
[(1, 111), (2, 222), (3, 3333), (5, 555)]
2
222
[(1, 111), (2, 222), (3, 3333), (5, 555)]
3
3333
[(1, 111), (2, 222), (3, 3333), (5, 555)]
5
555
[(1, 111), (2, 222), (3, 3333), (5, 555)]
1
111
2
222
3
3333
5
555

Process finished with exit code 0

字典胡拆分  for k,v in d:                      for k,v in d.items():
                   print k                                     print k
                   print v 类型错误                         print v类型正确

循环控制

#coding:utf-8
import time    #时间模块,两个元素之间出现的时间间隔
for  x in range(3):         #range(3)表示从0到2
    print x
    time.sleep(1)  #括号中数据表示间隔的时间
else:
    print "ending"   #只有正常结束才会ending      出现KeyboardInterrupt(终止信号)表示强制终止了,用Ctrl+c 此时不出现ending

D:\Python安装程序\python.exe C:/Users/欢/PycharmProjects/untitled2/if.py
0
1
2
ending

 


   
# encoding:utf-8                             
for x in range(35):                            
    print x
    if x==3:
        pass          pass #代码庄起到站位作用
    if x==2:
        print "hello22"
        continue           #跳过当次循环的余下语句
    if x==5:
        exit()             #exit()跳出整个程序
    if x ==6:
        break                #break跳出循环,不影响下边循环执行,也不执行else
    print"#"*50
else:
    print "ending"
for x in range(1,11):
    print "------>",x            # print "---->",x  逗号不可丢输出为---->x


加了exit()之后的运行结果
0
##################################################
1
##################################################
2
hello22
3
##################################################
4
##################################################
5

 

D:\Python安装程序\python.exe C:/Users/欢/PycharmProjects/untitled2/if.py
0
##################################################
1
##################################################
2
hello22
3
##################################################
4
##################################################
5
##################################################
6
------> 1
------> 2
------> 3
------> 4
------> 5
------> 6
------> 7
------> 8
------> 9
------> 10


while循环
while  True:
    print "hello"      #死循环


# encoding:utf-8
x=""  #设置一个全局变量,空字符
while x != "q":       #x!=q则为真
    print "hello"
    x = raw_input("please input something,q for quit:")
    if not x :    #逻辑否,即回车键
        break
    if x=="c":
        continue              #输入c则不会出现one more time...
    print "one more time...."
else:
    print "ending...."           #ending....出现是正常程序正常结束即输入q,不是按回车键或Ctrl+c


输入q的结果
D:\Python安装程序\python.exe C:/Users/欢/PycharmProjects/untitled2/if.py
hello
please input something,q for quit:q
one more time....
ending....

 

if语句,for循环,while循环