Python Learning Note four: Process Control

Source: Internet
Author: User

Conditional statements:

#以缩进来区分表示同一范围.

1 #Coding=utf82 #Example 1:if basic usage3 4Flag =False5Name ='Luren'6 ifName = ='python':#determine whether the variable is ' python '7Flag = True#set flag to True when condition is established8     Print 'Welcome Boss'    #and output Welcome information9 Else:Ten     PrintName#output variable name when condition is not valid

Multiple judgments:

1 #Coding=utf82 #Example 2:elif usage3 4num = 55 ifnum = = 3:#determine the value of Num6     Print 'boss'        7 elifnum = = 2:8     Print 'User'9 elifnum = = 1:Ten     Print 'worker' One elifNum < 0:#value is less than zero output A     Print 'Error' - Else: -     Print 'Roadman'     #output when conditions are not true

Python does not support switch statements

1 #Coding=utf82 #example 3:IF statement multiple conditions3 4num = 95 ifNum >= 0 andNum <= 10:#determine if the value is between 0~106     Print 'Hello'7>>> Hello#Output Results8 9num = 10Ten ifNum < 0ornum > 10:#determine if the value is less than 0 or greater than One     Print 'Hello' A Else: -     Print 'undefine' ->>> undefine#Output Results the  -num = 8 - #determine if the value is between 0~5 or 10~15 - if(Num >= 0 andNum <= 5)or(Num >= 10 andNum <= 15):     +     Print 'Hello' - Else: +     Print 'undefine' A>>> undefine#Output Results

Same line, similar to IDL

1 #  2  34  5if (var  print" Value of Expression is " 6  7 Print " Good bye! "

Loop statement:

While

 1  #   2  3  Count = 0  4  while  (Count < 9 5  print   '  

while ... else

 1  #   2  3  Count = 0  4  while  (Count < 9 5  print   '  
1 # !/usr/bin/python 2 3 flag = 145while theprint'Given flag is really true! ' 6 7 Print " Good bye! "

While loop nesting

1 #Coding=utf-82 #!/usr/bin/python3 4i = 25  while(I < 100):6j = 27     while(J <= (i/j)):8       if  not(I%J): Break9j = j + 1Ten    if(J > i/j):PrintI"is prime" Onei = i + 1 A  - Print "Good bye!"

For

traverse Any sequence of items, lists, or strings

For I in sequence

Statements (s)

1 #!/usr/bin/python2 3  forLetterinch 'Python':#First Example4    Print 'Current Letter :', Letter5 6Fruits = ['Banana','Apple','Mango']7  forFruitinchFruits#Second Example8    Print 'Current fruit:', Fruit9 Ten Print "Good bye!"

>
Current letter:pcurrent letter:ycurrent letter:tcurrent letter:hcurrent letter:ocurrent letter:ncurrent Fruit : Bananacurrent fruit:applecurrent Fruit:mangogood bye!

Index

1 #!/usr/bin/python2 3Fruits = ['Banana','Apple','Mango']4  forIndexinchrange (len (fruits)):5    Print 'Current fruit:', Fruits[index]6 7 Print "Good bye!"

The built-in function Len () and range (), function Len () returns the length of the list, which is the number of elements. Range returns the number of a sequence.

For .... else ... (For loop nesting)

1 #!/usr/bin/python2 3  forNuminchRange (10,20):#To iterate between4     forIinchRange (2,num):#To iterate on the factors of the number5       ifNum%i = = 0:#To determine the first factor6j=num/i#To calculate the second factor7          Print '%d equals%d *%d'%(NUM,I,J)8           Break #to move to the next number, the #first for9    Else:#else part of the loopTen       PrintNum'Is a prime number'

Break jumps out of the loop continue out of this loop pass empty statement, is to maintain the integrity of the program structure.

1 #!/usr/bin/python2 3  forLetterinch 'Python': 4    ifLetter = ='h':5       Pass6       Print 'This is pass block'7    Print 'Current Letter :', Letter8 9 Print "Good bye!"

Python Learning Note four: Process Control

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.