5. Comment, branch structure, loop structure, pseudo & ldquo; select Structure & rdquo;, Branch

Source: Internet
Author: User

5. Comments, branch structure, loop structure, pseudo "select structure", and branch

Note:

Python uses # As the line annotator and three quotation marks as the multi-line annotator

Branch Structure: if-else:

A = int (input ("your score is:") if a> 60: print ("you are qualified! ") Else: print (" you fail! ")
If-elif-else:
A = int (input ("enter an integer") if a <0: print ("0>") elif a <10: # elif = else if print ("<10") elif a <60: print ("a <60") else: print ("a> 60 ")

 

Loop Structure::

List1 = ["apple", "banana", "pine", "super banana"] for I in list1: print (I, end = "\ t ") for I in range (10): print (I, end = "\ t") print ("\ n ------ show both subscript ------") for I, value in enumerate (['A', 'B', 'C']): print (I, value) print ("\ n ------ for-else ------") for I in range (, 3): print (I) else: # Run else print ("You jumped out of the loop ")

Result:

Apple banana pine super banana 0 1 2 3 4 5 6 7 8 9 ------ iterations show subscript ------ 0 A1 B2 C ------ for -- else ------ 0369 you jumped out of the loop

 

While:
N = 3 while n> 0: print ("hello world", n) n = n-1def while_else (count): while count> 3: print ("in while ") count = count-1 else: print ("You exited the loop") while_else (0) # Do not enter whilewhile_else (5) # enter while

 

Code result:

Hello world 3 hello world 2 hello world 1 --------------------------- you quit the loop in whilein while you quit the loop

 

 

Loop Control statement:

Break: jump out of the current loop

Continue: end the cycle ahead of schedule

While n! = 1: n = int (input ("You guess:") if n = 10: print ("right") break elif n> 10: print ("too big") else: print ("too small") else: print ("You quit the loop ")

 

num=10while(num>0):    if num %2==0:        print(num,end='')        num = num - 1    else:        print(num,end='')        print('-',end='')        num=num-1        continue    print('+',end='')

 

 

Pseudo "select structure ":

Note: Why does the switch syntax structure exist in Python? Is there any alternative solution?

 

The switch structure is compared one by one until a specified selection is found for execution. If there are many options, more search time is required (although this time does not matter in single-user processing ),

The pseudo "select structure" composed of dictionaries uses hash Lookup. hash value calculation is faster, and the query time is less than that of switch (what is the advantage of multiple users ?)

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.