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