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