Python study NOTE 2: Process Control and python study note Process
1. if else:
#!/usr/bin/pythonx = int(raw_input('please input:'))if x >= 90: if x >= 95: print 'a+' else: print 'a'elif x >= 80: if x >= 85: print 'b+' else: print 'b'elif x >= 70: if x >= 75: print 'c+' else: print 'c'else: if x >= 60: print 'd+' else: print 'bad'Ii. logical operators and or not
#!/usr/bin/pythonx = int(raw_input('please input x:'))y = int(raw_input('please input y:'))if x >= 90 and y >= 90: print 'a'elif x >= 80 or y >= 80: print 'b'elif not x < 60 and (not y < 60): print 'c'else: print 'bad'3.
Sequence:
#!/usr/bin/pythons = "hello python"for x in s: print xfor index in range(len(s)): print s[index]
Dictionary:
#!/usr/bin/pythondic = {'a':1,'b':2,'c':3}for x in dic: print x,dic[x]for k,v in dic.items(): print k,v
Control
Else: after normal execution of for, the content in else will be executed; otherwise, no (press Ctrl + c for the following code execution process)
#!/usr/bin/pythonimport timefor x in range(10): print x time.sleep(1)else: print 'end'
Break: jump out of the current loop
#!/usr/bin/pythonfor x in range(10): print x if x == 6: breakelse: print 'end'
Content in else will not be executed here
Pass: placeholder
Exit: exit
#!/usr/bin/pythonfor x in range(10): print x if x == 2: print 'hello',x continue if x == 4: pass if x == 5: exit() if x == 6: break print '*'*10else: print 'end'
4. while
If the condition fails, it will be executed after it ends normally. If break is executed, it will not be executed in else.
#!/usr/bin/pythonx = 'hello'while x != "q": print x x = raw_input('please input something,q for quit:') if not x: breakelse: print 'ending'