Python_ condition judgment and circulation

Source: Internet
Author: User

Conditional Judgment and circulation


Author:lxy



Conditions determine what the computer chooses to do
Loops make the computer do repetitive work

Condition Judgment:
If ... elif....else ....
If < judgment conditions;:
< statement block >
Elif < judgment conditions;:
< statement block >
Else
< statement block >

Description
Usage feel is no different from Java, except that grammar is limited in writing.
Note after each judging condition and else add:
Elif is else if is shorthand
The judging conditions can be abbreviated, such as if x: As long as X is not 0 value, flying empty string, flying empty sequence, and so on is judged to be true, responsible for false.

Loops
For...in #分别迭代出数列或元祖中的元素
For x in ...: #分别取x的值为in后面的元素

Example 1:
>>>names = [' dog ', ' cat ', ' pig ']
>>>for name in Names:
.. print (name) #依次取names中的值元素带入
' Dog '
' Cat '
' Pig '

Example 2:
>>>n = 0
>>>for x in [1,2,3,4,5,]:
... n = n + x #使用变量前要进行声明初始化
... print (n)
1
3
6
10
15

Compare the above two ways, in fact, the elements in the back of the first take-out,

Example 3:
Calculates the sum of 1-100 integers
>>>arr Range (101) #Python提供函数, generates an integer sequence of 0-100
>>>n = 0
>>>for x in arr:
... n = n + x
... if x = = Arr[-1]: #获取arr序列中的最后一个 (maximum) element
... print (n)




From group: Java user Group

Python_ condition judgment and circulation

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.