How to use while,if,for statements in Python

Source: Internet
Author: User
This article mainly introduces the use of while,if,for statements in Python, small series feel very good, and now share to everyone, but also to make a reference. Let's take a look at it with a little knitting.

One, if condition statement

Basic form:

If condition judgment:

EXECUTE statement

Elif Condition Judgment:

EXECUTE statement

Else

EXECUTE statement

Example:

Number = Int (input ("Enter a digit:"))
If number > 0:
Print ("positive number")
elif Number = = 0:
Print ("0")
Else
Print ("negative number")
Enter a number: 2
Positive

Parse: As above, after running, the user enters a number 2, starts to enter if condition judgment, 2 > 0 is established, then enters the internal execution code block, output "positive".

If the user entered a number of-1, start to enter if condition judgment, -1 > 0 is not established, continue to execute, "elif" for the continuation of the condition judgment, -1==0 not set up,

execution down, "Else" is: if the above conditions are not satisfied, then directly execute the Else internal code block, printing "negative numbers."

Two, while Loop statement

(1) while using

The while statement is used to loop the execution of a program that, under certain conditions, loops through a program to handle the same tasks that require repeated processing. Its basic form is:

While judging condition:

Looping statements ...

The execution statement can be a single statement or a block of statements. The judging condition can be any expression, and any value other than 0, or non-null (NULL), is true.

The loop ends when the condition false false is judged.

Example:

A = 1while a < 3:    print (a)
A + = 1
The result is: 1
2
Parse: A value of 1, enter while condition to judge, A is less than 3, satisfies the condition, then enters the loop inside,
Execute code block: Print the value of a "1" and then make a+1. At this point A is 2 and goes to the next cycle,
Start condition judgment, 2 less than 3, condition satisfied, enter loop inside execution code block: Print A's value "2",
Then a+1. A has a value of 3 and goes into the next loop. Start condition judgment, at which point a value is 3, the condition is a<3,
Visible does not satisfy the condition, so end the loop.

(2) Continue,break jump out of the loop

Continue is used to skip the cycle, break is used to exit the loop, in addition to the "judging condition" can also be a constant value, indicating that the loop must be set up, the following specific usage:

Continue
A = 0
While a < 3:
A + = 1
If a = = 2:
Continue
Print (a)
The result is: 1
3
Loop process: A value of 0, the while condition to judge A<3 set up, execute code block, a+1, at this time a
A value of 1, enter if condition to judge, do not meet the condition, so do not enter if inside, continue to execute, print
The value of A is "1". Start into the next loop, a<3 set up, into the loop, a+1, at this point a value of 2, enter
If the condition is judged, a equals 2, the condition is established, enter the if internal execution code block, continue is used to skip the
The next cycle, no code blocks below will continue to execute, directly end the cycle,
Into the next loop. Start condition judgment, a<3, set, enter Loop, a+1, at this time a value of 3, enter
If condition is judged, does not satisfy the condition, so does not enter if inside, continues to execute, prints A's value "3".
Start into the next loop, a<3 not set, end loop.
Breaka = 0while A < 4:    A + = 1        if a = = 2: Break     Print (a)


The result is: 1

Replace the "continue" in the above example with "break", the loop is the same as the process, but only when the trigger to "break" is different,

Break is the direct end loop, so only "1" of the running result is printed.

Three, for Loop statement

1, how to use

A For loop can traverse any sequence of items, such as a list or a string. And while are loops, the for loop stops at the end of the sequence,

The while loop is stopped when the condition is not established.

Syntax format:

For Iterating_var in sequence:   statements (s)


Example: Iterate through each character of a string.

str1 = "Hello"
For I in STR1:
Print (i)
The result is: H
E
L
L
O

The 2,range () function generates an integer sequence that is open and closed, that is, the number behind cannot be printed.

Such as:

   Print

The result is: 1 2

The 3,xrange () usage is exactly the same as range, and the difference is that not an array is generated, but a generator.

>>> list (xrange (1,5))
[1, 2, 3, 4]

4,enumerate () is used to combine a data object that can be traversed (such as a list, tuple, or string) into an index sequence, listing both the data and the data subscript.

Such as:

A = ["A", "B", "C"]
For I in Enumerate (a):
Print (i)
The result is: (0, ' a ')
(1, ' B ')
(2, ' C ')

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.