Python conditional judgment and looping

Source: Internet
Author: User

Tag: SSE tin exits list usage by meaning for loop break

First, the Python if condition judgment statement

(1),Python's if--If statement followed by an expression, and then using: to represent the code start

Note: The indentation rules for Python code. indentation should be used in strict accordance with Python's customary usage:4 spaces, do not use tab, and do not mix tab and space, otherwise it is easy to cause syntax errors due to indentation.

           If you're python the interactive environment , also pay special attention to indentation, and Exit indent requires more than one line of carriage return:  
 

if age >=:      ... Print ' your age is ' , age ...      Print ' Adult '   isadult

(2), Python's if-else statement

Use if ... else ... Statement, we can execute an if code block or an else code block, depending on the value of the conditional expression, either True or False.

Note: else there is a ":" later.

if score >=:      ... Print ' passed '  else     : ... Print ' failed ' . .. failed

(3), Python's if-elif-else statement

Elif means else if. In this way, we write a series of conditional judgments that are very clear in structure.

Special attention: This series of conditional judgment will be judged from top to bottom, if a certain judgment is True, after executing the corresponding code block, the subsequent condition judgment is ignored and no longer executed.

>>> score = 85>>>ifScore >= 90:...     Print 'Excellent'... elifScore >= 80:...     Print 'Good'... elifScore >= 60:...     Print 'passed'... Else:...     Print ' failed'... good

Second, the Python loop statement

(1), Python for Loop statement

the python for loop can then iterate over each element of the list or tuple in turn:

# after the class exam, the teacher to statistical average results, 4 students are known to list the results are as follows: # L = [A.] # please use the For loop to calculate the average score. >>> L = [75,92,68,59] for in L:      ... = sum + a ... Print sum/473.5

(2), Python's while statement

# use the While loop to calculate an odd number of 100 or less. >>> sum = 0 while x <: ...      = x + sum ...      = x + 2... Print sum2500

(3), Python break exit Loop statement

when using a for loop or while loop, you can use the break statement if you want to exit the loop directly inside the loop .

# Use the while True infinite loop with the break statement to calculate 1 + 2 + 4 + 8 + 16 + ... The first 20 items of the and. >>> sum = 0 while     True: ... = sum + x ...      = x * 2...      = n + 1...      if n >: ...           Break ... Print sum1048575

(4), the continue of Python continue to loop statements

Continue skips the subsequent loop code and proceeds to the next loop.

# With the continue statement, the odd sum of 0-100 is computed:>>> sums = 0>>> x = 0 while True:. ..      = x + 1...      if x >: ...           Break ...      if x%2==0:          ... Continue ...      = sum + x ... Print sum2500

Python conditional judgment and looping

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.