Python Basics (4): Conditional statements and loop statements

Source: Internet
Author: User

Today we look at conditional statements and loop statements.

Preview:

1. Using while loop output 1 2 3 4 5 6 8 9 10

2. For all numbers of 1-100

3. All odd numbers in output 1-100

4. All even numbers in output 1-100

5, Beg 1-2+3-4+5 ... 99 of all numbers of the and

6. User Login (three chance retry)

First, conditional statements

When we write a program that requires branching, we can also say that our conditional statements are used when an event occurs in a particular situation that produces a different processing condition.

If...else statement:

Single branch:

1 " " 2 If condition: 3     code to execute after satisfying condition 4"'56 age =7if age = = 18  : 8     Print (" I am a grown man!" ")

Dual Branch :

1 " "2 If condition:3 code that executes after the condition is met4 Else5 Execute if not satisfied6 " "7 8Age = 199 ifAge <= 18 :Ten     Print("I'm not years old! ") One Else : A     Print("I'm a grown man! ")

Multi-branch:

1 " "2 If condition:3 code that executes after the condition is met4 elif conditions:5 Do not meet the above conditions6 elif conditions:7 Do not meet the above conditions8 ...9 ElseTen Do not meet the above conditions One " " A  -Age = 19 - ifAge <= 18 : the     Print("I haven't had a year! ") - elifAge >= 18 : -     Print("I'm a grown man! ") - Else : +     Print("I'm just grown up this year! ")

Indent:

In other languages, most of the code blocks are identified by {}, and Python does not have {} which is a feature of Python. How does python determine which code block to execute? This leads to a concept forced indentation , which is intended to let the program know which condition each piece of code depends on, and if it does not differentiate by indentation, the program cannot determine which code block is executing.

The Python indentation principle:

The top-level code must write the top line, i.e., if a line of code itself does not depend on any condition, it must not be indented

Same level of code, indentation must be consistent

The official recommendation is to indent with 4 spaces, and of course you can indent in the way you used to.

Second, the circular statement

While statement:

1 " "2 While condition:3 code that executes after the condition is met4 " "5 6Count =07  whileCount <= 100:#as long as count<=100 is constantly executing the following code8     Print("Loop", Count)9Count +=1#each time it is executed, the count+1 is turned into a dead loop, because count is always 0 .

While...else statement:

Unlike other languages else, which is usually only the same as if, there is a while ... else statement in Python. The else function behind the while loop is that the statement after the else is executed when the while cycle is executed properly and the middle is not interrupted by a break.

Dead Loop:

There is a cycle called death cycle, one but into the dead loop, the program will run to the everlasting forever can not quit.

The while is always executed as long as the condition behind it is always set (i.e. the condition result is always true).

For example: The above code, if there is no code count + = 1, the program will go into the dead loop. Count <= 100 is always set because count = 0.

Loop Termination statement:

If, for some reason, you do not want to continue looping in the process of looping, you will need to break or continue to terminate the statement.

Break : completely jump out of the loop and execute the code after the loop.

Continue: jump out of this cycle, do not execute continue after the code, re-enter the loop to cycle the condition of the judgment.

For loop:

1  forIinchRange (4):#i is the variable (4) value range2     Print(">>:"I#0 1 2 33 4  forIinchRange (1,5):#Gu Tou Regardless of the tail5     Print(">>:"I#1 2 3 46 7  forIinchRange (1,5,2):#Step 2 Take one value per two8     Print(">>:"I#1 3

99 Multiplication Table Exercises:

1  for  in range (1,10):2for in      Range (1,i+1):3         Print("%s*%s=%s" % (j,i,i*j), end=""  )4     print()

Results:

Preview Answer:

1 #using while loop Output 1 2 3 4 5 6 8 92Count = 13  whileCount <= 10 :4     Print(count)5Count + = 16     ifCount = = 7 :7Count + = 18 9 #count = 0Ten #While count <: One #count + = 1 A #if Count = = 7: - #Continue - #print (count)
1 # all numbers of the 1-100 are calculated and 2 count = 13 sum = 04 while Count <= :5 C15/>sum + = count6     count + = 17print(sum)
 1  #   output All odd  2  count = 13  while  count <= 100:  4  print   (count)  5  count + = 2 
 1  #   output All even  2  count = 23  while  count <= 100:  4  print   (count)  5  count + = 2 
  1  #   Beg 1-2+3-4+5 ... 99 of all numbers and   2  count = 1 3  sum = 0   4  while  Count < 100:   5   If  count% 2 = = 1:   6  sum += count   7  else  :   8  sum-= count   9  count + = 110  print  (sum) 
1 #User Login (three chance retry)2Username ="Oldbody"3Password = 100864Count = 15 Print("Please enter your account password for a total of three attempts! ")6  whileCount <= 3 :7Name = input ("Please enter your account:")8pswd = Int (input ("Please enter your password:"))9     ifName = = Username andPSWD = =Password:Ten         Print("Enter right! ") One          Break A     Else : -         Print("Section", Count,"input Error Please re-enter! ") -Count + = 1

Small Knowledge Points:

Print () comes with a newline character.

If you want to cancel the default newline multibyte End (""), please refer to the code of the 99 multiplication table.

Python Basics (4): Conditional statements and loop statements

Related Article

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.