Example of if else for a for loop in Python

Source: Internet
Author: User
Tags in python


An If statement is used to check a condition: If the condition is true (true), we run a statement block (you are an if block), otherwise (else), we execute another statement block (called else block). Else the child statement is optional.

For example (Save as if.py):

Number = 23
guess = Int (input (' Please enter an integer: ') #等待输入整数
If guess = = number:
Print (' Congratulations, you guessed right. # The new block starts here
Print (' But you didn't get any prizes! ) # The new block ends here
Elif Guess < number:
Print (' No, you guess a little bit ') # another block
Else
Print (' No, you guessed a little bigger ')
Print (' Finish ')
# If statements are executed, the final statement is always executed the output may have the following three cases:
Please enter an integer: 50

No, you're guessing a little big.
Complete

Please enter an integer: 22
No, you guessed a little bit.
Complete

Please enter an integer: 23
Congratulations, you guessed right.
(But you didn't get any prizes!) )
Complete


The For statement is a circular control statement in Python. Can be used to traverse an object and also have an optional else block attached, which is used primarily to handle the break statements contained in a for statement.

If the For loop is not terminated by a break, the statement in the else block is executed.

Break terminates the For loop when needed

Continue skips the following statement and starts the next round of loops.

The For statement is formatted as follows:

>>>for <> in < object collection:

.. if < conditions:

.. break

.. if < conditions:

... continue

... < other statements >

. else:

... <>

...

In Python, a loop statement for or a while after else indicates that the loop condition is not valid, such as:

Example


# First: Ask for prime numbers between 50-100

Import Math
For I in range (50, 100 + 1):
For j in range (2, int (math.sqrt (i)) + 1):
If I% j = 0:
Break
Else
Print I

# Second: Put else's position in the same indent as if.

Import Math
For I in range (50, 100 + 1):
For j in range (2, int (math.sqrt (i)) + 1):
If I% j = 0:
Break
Else
Print I

# Third: Add a break statement after else.

Import Math
For I in range (50, 100 + 1):
For j in range (2, int (math.sqrt (i)) + 1):
If I% j = 0:
Break
Else
Print I
Break

# # #idea ###

#-*-Coding:utf-8-*-
For I in range (10):
Print (i)

if i = = 5: # will not execute else at this time, when I is greater than or equal to 10, does not meet the for condition, executes else
Print ("Too big-i ' m giving up!")
Break
Else
Print ("Completed successfully")

This is one of the differences between Python and other programming languages, so that you can determine if the For statement condition is set up, do not set a flag=false tag outside of the for, and change the syntax of flag to true in the For loop to make the code more concise in syntax.

For x in range (1, 5):
If x = 6:
Print ("Found the number", X)
Break
Else
Print ("Not found!")
To give a simple example, the above example outputs not found, because the x==6 does not conform to the for-loop condition and runs the else's code directly.

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.