Python Learning Note 2

Source: Internet
Author: User

Conditional Judgment and circulation

1. Conditional judgment

If < condition judgment 1>:
< Executive 1>
Elif < condition judgment 2>:
< Executive 2>
Elif < condition judgment 3>:
< Executive 3>
Else
< Executive 4>

2. Cycle

There are two types of Python loops, one is the for...in loop and the other is the while loop.

2.1 for...in Cycle

A for x in ... Loop is a statement that puts each element into a variable and x then executes the indented block.

names = [' A ', ' B ', ' C ']
For name in Names:
Print (name)

The code prints all the elements of the names, in turn, A,b,c.

sum = 0
For x in [1,2,3,4]:
sum = sum + x
Print (sum)

The code calculates the 1+2+3+4 and promises the final result, that is, 55.

sum = 0
For x in range (5):
sum = sum + x
Print (sum)

This code is the same as the previous section of code, where range (n) refers to a sequence of integers starting from 0 and less than n .

2.2while Cycle

sum = 0
n = 1
While n < 5:
sum = SUM + N
n = n+1
Print (sum)

This code is similar to the for...in loop.

Python Learning Note 2

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.