Chapter 2 of python Learning (conditions, loops, and other statements) and Chapter 2 of python

Source: Internet
Author: User

Chapter 2 of python Learning (conditions, loops, and other statements) and Chapter 2 of python
1. Simple conditional execution statement (if)

Num = int (input ("enter a number \ n "))

If num = 0:

Print ("num = 0 \ n ")

Else:

Print ("num! = 0 \ n ")

A slightly more complex execution statement:

Num = int (input ("enter a number \ n "))

If num = 0:

Print ("num = 0 \ n ")

Elif num> 0:

Print ("num> 0 \ n ")

Else:

Print ("num <0 \ n ")

From the above example, we can see that the conditional sentence is to determine whether to execute the statements in the sentence based on the bool value of the result of an execution statement,

If judgment Statement (if true, execute the following sentence; otherwise, execute the statement after else or elif ),

Remember if, elif, or else. You cannot forget the colon: two vertices. Otherwise, they are all incorrect;

2. Simple loop execution statement (while) 4.1 a simple loop output

X = 1

While x <= 5:

Print (x)

X + = 1

1

2

3

4

5

4.2 A slightly more complex while loop:

Name = input ("please enter your name: \ n ")

While name not in ('aiyq195 ', 'wgj', 'yb '):

Print ("the name is err \ n ")

Name = input ("please enter your name, too: \ n ")

Please enter your name:

J

The name is err

Please enter your name, too:

Ni

The name is err

Please enter your name, too:

Aiyq195

4.3 determine how to exit the while LOOP

X = int (input ("Enter a number to exit the loop! \ N "))

While x:

Print ("The number unable to exit the loop! \ N ")

X = int (input ("Enter a number to exit the loop! \ N "))

 

Run the preceding statement in the IDE version to obtain the following result:

 

Enter a number to exit the loop!

8

The number unable to exit the loop!

Enter a number to exit the loop!

-1

The number unable to exit the loop!

Enter a number to exit the loop!

0

 

From the above results, we found the condition for exiting the while LOOP, that is, the exit condition of the while loop is only possible, that is, the execution result of your judgment condition is 0.

Especially when we execute a while loop and use the judgment condition as a number, it will not quit because your value is negative;

 

 

3. for Loop Example 5.1 simple for loop description

The while loop is very flexible. It can be used to repeatedly execute a code block when any condition is true. However, sometimes you need to tailor your clothes. For example, you need to execute a code block for each element of a set. In this case, you can use the for statement:

Words = ['on', 'is ', 'any', 'x', 'Parrot']

For word in words:

Print (word)

 

================================== RESTART: E:/python/pyth/. py =

This

Is

An

Ex

Parrot

It can be seen from the above sentence that a for loop has a basis, that is, it may be based on the elements in a list or a tuples or dictionary that you have defined;

5.2 for Loop a keyword range ()

For number in range (0, 10 ):

Print (number)

 

For number in range (0, 10, 2 ):

Print (number)

The execution result on IDE is as follows:

0

1

2

3

4

5

6

7

8

9

 

0

2

4

6

8

Looking at the above results, we can see that the range () function can iterate some numbers and let the for loop execute this as a judgment condition. It also includes the step size, that is, the third parameter, you can determine this;

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.