Python study NOTE 2: Process Control and python study note Process

Source: Internet
Author: User

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'

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.