Basic Python Tutorial Series: Viii. Conditions and cycles

Source: Internet
Author: User

in fact, as a conditional judgment statement or Loop statement, we have more or less involved in the front, and now we do some additional or to pay attention to a few places to say. There are generally the following common statements: If, while, for, and else, elif, break, continue, and pass that are used with each other.

Let's just say a few simple words, a simple one to take, because everyone has learned C, so the main point of the difference with C.

1. If and else:

If expression:
Statement 1

Statement 2

...

The conditional statement (expression) does not need to be written in parentheses, and the entire pyThon language does not use curly braces as the bounds of the code block , and a colon is required after the conditional statement. This is also true in the while loop statement.

So, since Python doesn't use curly braces as a code block boundary, how does python work? The answer is to indent through the code. As a result, the formatting requirements for code indentation in Python programming are very strict. Here, by the way, emphasis on indentation, usually the most used is the Tab tab, the program of \ T, followed by the most use of space.

emphasis: First, do not allow the use of a combination of tabs and spaces; second, if cross-platform is involved in the actual work red, try not to use tabs, tab, some cases are also problematic, but unified use 4 spaces. Third, if only to do a practice or not involved in cross-platform, such as the tab should be said to be very convenient, but it is recommended that you develop a good programming habits.

when there is only one statement in the IF statement 1, this can be written as if expression: statement 1 is written in a row, this is possible, but not allowed to write, first inconvenient to read, second if you need to modify the program to add a new statement or need to move the statement 1 down, In fact, it is still the code code of the program.

In C or Java, how to determine if an else is matched with which if, is to find the closest to their own if, so sometimes there will be a match error phenomenon, the book is called "Hang Else", then in Python how to determine an else with which if collocation, Or how Python solves "hang Else", the answer is indentation. else with which if indent level is the same, which if match.

/ * dangling-else in C */
if (Balance > 0.00)//IF1
if (((Balance-amt) > Min_bal) && (atm_cashout () = = 1)//IF2
printf ("Here ' s your cash; Bills.\n ");
else//The else here, though with IF1, actually matches the IF2
printf ("Your balance is zero or negative.\n");

If Balance > 0.00: # IF1
if Balance-amt > Min_bal and Atm_cashout (): # IF2
print "Here ' s your cash; Bills. "
Else: # aligned with IF1, so match with IF1
print ' Your balance is zero or negative. '
or is:
If Balance > 0.00: # IF1
if Balance-amt > Min_bal and Atm_cashout (): # IF2
print "Here ' s your cash; Bills. "
Else: #与if2匹配
print ' Your balance is zero or negative. '

if-elseif-else in Python is if-elif-else,elseif a little shorthand.

Switch/case is not currently supported in Python, and may be supported in the future, so if-elif-else can replace switch on multiple choices, but it can be done by other means, such as using a dictionary to replace switch. Do you remember the dictionary you spoke yesterday? {1: ' Choose 1 ', 2: ' Select 2 ', ...}, this is not case 1: Select 1;case 2: Select 2 ...

The use of ternary operators, the main wording in C or Java is (c? X:Y) c is a conditional expression, when C is true, the expression is x, otherwise Y, the syntax that is integrated after Python2.5 is determined to be x if C else Y.

2. While Loop

While expression:
Statement 1

Statement 2

...

The basic syntax is the same as in C and Java, and formatting considerations are similar to If.

3. For Loop

For Iter_var in iterable:
suite_to_repeat

is the use form of an iterator. As an example:

For eachletter in ' Names ':
... print ' Current letter: ', Eachletter
    ...
Current letter:n
Current letter:a
Current letter:m
Current letter:e
Current letter:s

In fact, if you look at this example, can you think of the dictionary you talked about yesterday and how to get all the entries in the dictionary?

4, break and continue statement is arguably a key content, but the use of the same as in C, I will not say, break can jump out of the loop to the cut statement, continue end the current loop, continue the next cycle.

5, pass statement, as the name implies, what is not dry, is a purely coding logic things. Some places require code in syntax, but Python does not have a corresponding empty curly brace or semicolon (;). To mean "don't do anything" in C, and if you don't write any statements where you need a block of child statements, the interpreter will prompt you for a syntax error. As a result, Python provides a pass statement

6, Else statement, in addition to the IF condition statement, in the while and for loop can also use the Else,for loop can have else for the loop post-processing (post-processing). It is the same as the else process in the while loop. As long as the for The loop is normal (not through break), and the ELSE clause executes.

Basic Python Tutorial Series: Viii. Conditions and cycles

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.