Basic python tutorial-simple branch and loop usage, basic python tutorial

Source: Internet
Author: User

Basic python tutorial-simple branch and loop usage, basic python tutorial

This example describes the simple usage of python branches and loops. We will share this with you for your reference. The details are as follows:

Speaking of program design, we have to talk about sequence, branches, and loops.

The order is to run the code from top to bottom. This is very simple.

When talking about branches and loops, pay special attention to the forced indentation in python code.

Let's first look at the branch:

(1) simple if-else

Python code:

A = '1' if a = 1: # note that there is a colon. Where "=" is equal to determine print 1 # note that there is a tab key before the print function, which is the forced indent else of python: # note the colon print 0 after else # note the indentation if (a = 1): # You can add garden brackets print 1 else: print 0

The output is:

1
1

(2) and logical judgment

Python code:

A = 1b = 0if a = 1 and B = 1: # and is the logical "and" operation. The natural "or" is the logical "or" Operation print 1 else: print 0

The output is:

0

(3) branch if-else if

Take a closer look:

Python code:

# Else ifa = 1b = 0if a <1: print 1 elif B <1: # note that this is not else if, but elif. Print 0

The output is:

0

The above three cases are about Branch judgment. The following describes the loop.

(1) Start With A for loop:

The for loop is essentially the traversal of elements:

For example:

Python code:

For I in range (0, 5): # note that range is a function print I

The output is:

0
1
2
3
4

Range is a function that generates a sequence of [0, 5. Here, the mathematical expression "[)" is used to indicate that it is greater than or equal to 0 and less than 5. Is a semi-open and semi-closed interval. Note that in python, semi-open and semi-closed intervals are used (I have never seen any other forms, but I can implement them myself ).

The meaning of "for I in range (0, 5):" is that from the sequence ", 4", each time an element is retrieved and assigned a variable I, print the value of element I every time you execute the print function.

There is a colon at the end of the if and for statements to tell the compiler that the current row is over and the next row should be explained.

With this colon, we can directly execute the print function without line breaks.

Python code:

for i in range(0, 5):print i

(2) while Loop

While LOOP, when the while condition is true, execute the while internal program segment.

Python code:

I = 10 while I> 0: print I-= 1 # note that python does not support I --, I ++, -- I, ++ I and other operations.

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.