Python syntax and if, while, for statements

Source: Internet
Author: User

1. Learn Python's special indentation and syntax
Python is so simple, thanks to his indentation mechanism, the strict indentation mechanism makes the code very neat and tidy, pleasing to the eye, improves readability, and improves maintainability in a certain sense. Python does not require {} As other languages do, just using the TAB key to differentiate the logic of the code.

2. Learn about the IF condition of Python

Python has a lot of logic to judge, the first choice is our if statement, if followed by the condition to judge, if the following is true, only after the execution if the indentation code, if the condition is false, skip the IF statement directly.

Format:

If judging condition:

Execute statement ...
Else

Execute statement ...

Example:

A = 3

If a <-1

Print (' A is a negative number ')

Elif a ==0

Print (' a equals 0 ')

Else

Print (' A is a positive number ')

3. Learning the while loop of Python

Format:

While judging condition

EXECUTE statement

While the judgment condition while in the execution loop, first to determine whether the condition is true, for the true execution of the statement, execute the sentence and then judge the condition, if the condition is false, jump out of the loop.

4. Learn Python for loop

The For loop is used primarily in work to traverse lists, strings, files, and so on, and for loops by default is looped to the end of the element, with the format of the For loop as follows:

For Iterating_var in sequence

Statements (s)

5. Learn the difference between range and xrange
In the For loop, Range generates a list object directly, and xrange does not generate a list, but returns one of the values in each call. The performance of the xrange loop is better than range, when it returns very large.

6. Learn different ways to break and continue

The break statement terminates the loop, even if the loop condition does not have a false condition or if the sequence has not been fully traversed, the loop statement will stop.

The continue statement jumps out of the loop, and break jumps out of the loop.


This article is from the "brave Heart Zhao Xiao Bai" blog, please make sure to keep this source http://3024783.blog.51cto.com/3014783/1976237

Python syntax and if, while, for statements

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.