[Section III] Python Process Control Statements-if, while, for, break, continue

Source: Internet
Author: User

Directory:

I. Conditional JUDGMENT Statement-if

Two. Looping statements-while & for

Three. Break & Continue statement.

Four. Exercises


Body:

One, conditional judgment statement-if:
1) The first type of syntax:

If condition: code block

2) The second type of syntax:

If condition: content One content two else: content three content four

3) The third type of syntax:

If condition 1: code block elif Condition 2: code block elif Condition 3: code block else: code block

Note:
= and = =: An equal sign is an assignment, and two equals is a comparison.
True and false:

If True: #此处的结果如果为真, the first print is executed forever, and if the result here is False, the second print is executed. Print ("xxx") else:print ("xxx")

If judging condition:
1) True, False
2) 1 > 2 n1 > N2 N1 = = N2
3) name = = "Jack" or "name" = "Jenny"
4) Name! = "Jack"
5) name = = "Jack" and pwd = = "123"

Example:

#!/usr/bin/env python #! _*_ coding:utf-8 _*_ #import getpassusername = Raw_input ("username:") password = getpass.getpass ("Password:") if username = = "Yangbin" and Password = = "Yangbin": Print ("" "******info******: Username:%s Password:%s" ""% (US Ername,password)) else:print ("Input error.")

Second, loop statement-while & for
1, while loop statement:
Grammar:

While condition: code block # Executes the loop body if the condition is true. # If the condition is false, the loop body is not executed.

Example 1:

# The While Process Control statement is a dead loop and will not automatically exit unless manually intervened. Import time # Importing timer while True:print ("1") Time.sleep (1) # Set the timer to enter print once per second ( "End")

Example 2:

Import timen1 = truewhile N1: # N1 is true. Print ("1") time.sleep (1) n1 = False//n1 The second assignment is false, which jumps out of the while loop. Print ("End")

2, for Loop statement
Grammar:

For variable name in "Looping content": # fixed collocation. Xxx

Example 1:
#打印l, I,n,u,x.
Method 1:

N1 = "Linux" for I in Range (len (n1)): temp = n1[i] Print (temp)

Method 2:

N1 = "Linux" for I in N1:print (i)

Three, break & continue statements.

Break is used to terminate the loop, which jumps out of the current loop, and the code below the break statement is no longer executed;

Continue is used to jump out of this cycle and continue the next cycle;

Example 1:
# Print the number 1-10 and skip the number 7.

i = 0j = Truewhile J:i = i + 1 if i = = 7:continue elif i = = 11:break print (i)

Example 2:
#打印字母l, i,n,u,x, remove N,u.

N1 = "Linux" for i in n1:if i = = "N" or i = = "U": # Note here with or, not with and, because I cannot be both N and U. Continue # If it is break here, only print out l,i print (i)

Four, exercises
1, use while loop and for loop Output 1 2 3 4 5 6 8 9 10 respectively.
# while loop.

i = 0while I < 10:i + = 1 if i = = 7:continue print (i)

# for Loop.

For I in range (one): # range (11), which is 0-10 of these 11 numbers. If i = = 0 or i = = 7:continue print (i)

2. The and of all numbers of 1-100.
# while loop.

i = 1sum = 0while I <= 100:sum + = i i + = 1print ("1+2+3+4+...+100=%d"% sum)
i = 1sum = 0while True:sum + = i i + = 1 if i = = 101:breakprint (sum)

# for Loop.

sum = 0for i in range (101): if i = = 0:continue sum + = IPrint ("1+2+3+4+...+100=%d"% sum)

3. All odd numbers in output 1-100
# while loop.

i = 0sum = 0while I < 100:i + = 1 if I% 2! = 0:sum + iprint ("1+3+5+...+99=%d"% sum)

# for Loop.

sum = 0for I in range: if i = = 0:continue elif I% 2! = 0:sum + iprint ("1+3+5+...+99=%d"% sum )

4. All even numbers in output 1-100
# while loop.

i = 1sum = 0while I <= 100:if I% 2 = = 0:sum + I i + = 1print ("2+4+6+...+100=%d"% sum)

# for Loop.

sum = 0for i in range (101): if i = = 0:continue Elif I 2 = = 0:sum + iprint ("2+4+6+...+100=%d"% su M

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

i = 0s = 0y = 0while I < 99:i + = 1 if I% 2! = 0:s + = i elif i% 2 = = 0:y = Isum = s + ypri NT ("1-2+3-4+5-6...-98+99=%d"% sum)

# for Loop.

s = 0y = 0for I in range: if i = = 0:continue elif I% 2! = 0:s + = i elif i% 2 = = 0: Y-= Isum = s + yprint ("1-2+3-4+5-6...-98+99=%d"% sum)

6. User Login (three chance retry)
# while loop.

Import Getpassi = 0while True:name = input ("Please input your name:") Password = getpass.getpass ("Please input your        Password: ") i + = 1 if name = =" Yangbin "and Password = =" Yangbin ": Print (" Login succeed! ") Break Else:print ("Your only input%d count"% (3-i)) if i = = 3:break

# for Loop.

Import getpassfor I in range (3): name = input ("Please input your name:") Password = getpass.getpass ("Please input y        Our password: ") if name = =" Yangbin "and Password = =" Yangbin ": Print (" Login succeed! ") Break Else:print ("Your only input%d count"% (2-i)) if i = = 2:break


The third section is complete!

This article is from the "Yangbin" blog, make sure to keep this source http://13683137989.blog.51cto.com/9636221/1896118

[Section III] Python Process Control Statements-if, while, for, break, continue

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.