Python BASICS (4): Conditions and loops, python Basics

Source: Internet
Author: User
Tags element groups

Python BASICS (4): Conditions and loops, python Basics

In actual development, if you want to implement certain functions or requirements, some logic is involved, complicated or simple. How can you implement the python syntax? This involves conditions and loops. Obviously, most languages have conditional and cyclic syntaxes, and python syntaxes are similar to them. It is easy to learn, and students with basic knowledge are more familiar with the car.

I. Condition judgment:

Conditional interpretation in Python includes the if statement and if... Elif... Else statement

First, let's take a look at the if statement, which consists of the keyword if + condition expression +: + code block (line feed indent)

Let's look at an example:

If 2> 0: print ("print when the if condition is True ")

Where: 2> 0 is actually a conditional expression. Only when the conditional expression is True, that is, True, the code block following the execution will be executed.

In the above example, only one conditional expression is used. What should I do when multiple conditions need to be judged? It is implemented through and, or, not, that is, "and" or ". And indicates that "and" is true only when all the conditions connected by and are true. That is, when one condition is not true, the entire result is not true. Or indicates "or". When one of the conditions connected by or is true, the entire result is true. Not indicates "not". In fact, it indicates the inverse. If the expression after not is true, the returned result is false, and vice versa.

For example:

A = 5b = 'C' c = Trueif not a> 0 or B = 'C' and c = True: print ("print if multiple conditions are True ")

The multi-condition of if is true, and the print statement is executed. I wonder if the students can analyze and understand the multi-condition, from left to right, a> 0 is true, but the preceding not condition is false. B = 'C' after or is easily known as true, but false is indicated by the preceding or condition, therefore, there is a true or false condition. According to the previous instructions, we know that the or condition is true, and the whole is true. Now, the condition before and is true, the following conditions are obviously true. According to the preceding instructions, and must be true only when they are true. Therefore, the true condition is true. After the above analysis, the if loop is still very simple, which is inseparable from future learning.

Let's take a look at if... Elif... Else statements are used in the same way as if statements. When multiple conditions are judged, it is obvious that one if statement cannot be implemented. Therefore, if... Elif... Else, where elif can be omitted. When there are only two conditions, you can use if... Else is enough. When there are many conditions, elif can be used multiple times. Example:

A = True if a: print ("execute when a is True") else: print ("execute when a is not True ")

Let's look at the situation of multiple elif instances.

A = int (input ("enter a number:") if a = 1: print ("print when a is 1") elif a = 2: print ("print when a is 2") elif a = 3: print ("print when a is 3") else: print ("print when none of the above conditions are met ")

I will not give the results of this example. You can give it a try.

In addition, the conditional expression also involves a ternary operator, for example:

value = x if x>y else y

The final value of value is x or y depends on the expression after if. if x> y is true or true, the value of value is x; otherwise, it is y.

Ii. Cyclic Conditions

1. while condition Loop

Format: the keyword while + condition expression +: + code block (line feed indent) is similar to the if expression. In fact, the usage is similar. When the condition expression is true, the circular code block starts, the loop ends only when the conditional expression is not true (the code block is no longer executed. Example:

A = 1 while! = 5: print ("loop % d times" % a) a = a + 1

Every loop will judge and execute the conditional expression. When it is not true, the entire while loop will end.

2. for iteration Loop

Previously, the for loop was used in range. The for loop is actually used to traverse the sequence members or access the iteratable objects in sequence. What are these items? Because the list and element groups are not introduced yet, write down them here, wait until the content is described.

3. while... Else statement

Want to know while... Else must first know break. What is break used? It is used to stop the entire loop. It is known that the while loop ends when the conditional expression is not met. In fact, the while loop ends when the break is encountered during the loop process.

Let's take a look at the following example:

A = 1 while! = 5: print ("loop % d times" % a) a = a + 1 break

In the above example, only one break keyword is added. The running result shows that print is printed once, and the program ends. This indicates that the entire loop stops when break is encountered. Is break a obstacle.

I just mentioned that the while loop ends in two cases: one is that the condition does not meet the loop end, the other is to encounter break, while the while... In else, the execution of the code block after else must meet the first case above. The content of the code block after else will be executed only when the while loop is executed until the conditions are not met, in the case of break, the code block after else will not be executed. For example:

A = 1 while! = 5: print ("loop % d times" % a) a = a + 1 else: print ("after the while LOOP condition is not met, start executing this Code ")

The running result is as follows:

 

You can try adding a break in the while loop to check whether the content after else is printed. In fact, the for loop can also be added with else. Its usage and principle are the same as that of while. After thoroughly introducing the for loop, we will discuss it again.

4. pass

The usage of pass is also very simple. When writing a program, we don't know the content. For example, we can use pass to indicate an empty statement to complete the structure of a program. For example:

a = 1while a!=5:

When the program writes it here and does not know how to write the while LOOP code block, if it does not write any content, the program will certainly report an error because it does not conform to the syntax, then add a pass, as shown below:

a = 1while a!=5:    pass

If the program does not report an error, the pass here is an empty statement, which is equivalent to the empty content.

There are so many introductions about judgment and loop, and we will learn a lot later. I hope everyone can grasp it, learn a little bit every day, and make a better life.

Benefits: Follow the public account and replyPython booksTo obtain some well-recognized e-books learned by python.

Tips: The Code involved above. Beginners must think carefully and think carefully about it. I have also provided all the running results in the article. Do not look down. For beginners, the results achieved by themselves are often different from those imagined.

It is hereby stated that: The author's content is original and reprinted to indicate the source. Due to my limited ability, please contact the author for any errors or omissions. Thank you!
If the software is installed using python or other content that cannot be clearly written, you can join the QQ group: 476581018, or follow the Public Account (quick scan (● 'shanghai' ● )) the series of articles will be released on the public account, and there will be a lot of benefits for you!

 

 

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.