Process Control of Python learning

Source: Internet
Author: User

1. If Else

Syntax: if expression1:

Statement1 (s)

Elif expression2:

Statement2 (s)

Else

    Statement3 (s)

2. For

Syntax: for Iterating_var in sequence:

Statements (s)

Where: sequence is a sequence (string, tuple, any one in the list) or dictionary

Example 1:

#!/usr/bin/pythonFruits=['Banana','Apple','Mango'] forIndexinchrange (len (fruits)):Print 'Current fruit:', Furits[index]Print "Good bye!"

Example 2:1 to 100 of cumulative

# !/usr/bin/python num=0 for in range (1,101): num+ =xprint num

extension : Range to quickly generate a sequence

Format: Range (i,j[, step value]) where I default value is 0, stepping value defaults to 1

Example 3: Traversing a dictionary

d={1:111,2:222,5:555,3:333} for in D:  print  d[x] for  in D.items ():    #d.items () returns the Key,value in the dictionary as tuples [(1,111), (2,222), (3,333), (5,555)]  print  k  print V      

(1) The contents of Else:else in the For loop are executed only when the For loop is terminated normally, not when the for loop is terminated abnormally

Cases:

# !/usr/bin/python Import  Time  for  in range:  print  x time.sleep (1)  else: 
   
    print
    "
    ending
    "  
   

(2) Break,continue,pass (code pile, play a role in a placeholder)

3. While

Syntax: while expression:

Statement (s)

Cases:

#!/usr/bin/pythonx="" whilex!="Q":    Print "Hello"x= Raw_input ("Please input something,q for quit:")    if  notX:#if the input is empty, not X is true, jumping out of the loop         BreakElse:    Print "ending ..."  #input q, Loop end, will print this sentence; input blank, not print

Process Control of Python learning

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.