Python _ condition judgment and loop, and python judgment Loop

Source: Internet
Author: User

Python _ condition judgment and loop, and python judgment Loop

Conditional judgment and loop


Author: lxy



Determine what the computer chooses to do
Loop allows the computer to do repetitive work

Condition determination:
If... elif... else ....
If <condition>:
<Statement block>
Elif <condition>:
<Statement block>
Else:
<Statement block>

Note:
The usage is similar to that of Java, except for the limited syntax.
Note that each condition is added to else:
Elif is else if is short
The condition can be abbreviated as "if x". For example, if x is a non-zero value, a flying Null String, a flying null series, and so on, the condition is True. if x is responsible, the condition is False.

Loop
For... in # iterations of elements in the sequence or ancestor respectively
For x in...: # Take the values of x as the elements after in.

Example 1:
>>> Names = ['Dog', 'cat', 'pig']
>>> For name in names:
... Print (name) # import the value elements in names in sequence
'Dog'
'Cat'
'Pig'

Example 2:
>>> N = 0
>>> For x in [1, 2, 3, 4, 5,]:
... N = n + x # declare the initialization before using the variable
... Print (n)
1
3
6
10
15

The above two methods are used to extract the elements following in order,

Example 3:
Calculate the sum of integers from 1
>>> Arr range (101) # functions provided by Python to generate a 0-100 integer Sequence
>>> N = 0
>>> For x in arr:
... N = n + x
... If x = arr [-1]: # obtain the last (maximum) element in the arr sequence.
... Print (n)




From group: Java User Group

2-9 python exercises: raw

While 0 <n <100:
.. N = int (raw_input ('Please input a number :'))
.. Print 'wrong number .'
Point represents a space. Baidu knows that it will delete the space that it considers "useless.

Python uses % to determine the loop

For I in range (100 ):
... If not I % 4:
... Print "*" * 25

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.