Conditional judgment
Python's indentation rules, if the IF statement is true, the code is executed, you can also add an else statement, meaning, if the judgment is false, do not execute if the content, to the else executed
Num=num >=: (num) (): (num) ()
Of course, the above judgment is rough, can be used elif to make more detailed judgments
Num=num >=: (num) () num >=: (): ()
Elif is the else if abbreviation, can have more than one elif, so the IF statement after the XXX type is:
< condition judgment 1>: < perform 1>< condition judgment 2>: < execution 2>< condition 3: < execution 3>: < execution 4>
If statement execution has a feature, it is judged from the top down, if it is true on a certain judgment, the corresponding statement of the judgment executed, ignoring the remaining elif and else
If you can also abbreviate
If x:
Print (' true ')
As long as X is a non-0 value, a nonempty string, a non-empty list, and so on, it evaluates to true, otherwise it is false.
Input () read as a string
Cycle:
For loop
There are two types of Python loops, one is the for...in loop, and each of the elements in the list or tuple is iterated in turn to see an example
Names=[]i names: (i)
Jack
Lucy
Tom
So for the X in ... A loop is a statement that puts each element into the variable x and then executes the indented block.
Build number function range (), use method range (start number, end number, middle span), 0 start, n-1 end
Version 3.x has generated an iterator for the range (), not the output,
Calculation of the sum of 100
Sum=i (): Sum=sum+i (SUM)
The second kind of loop
While loop, as long as the condition is satisfied, the loop will not break and exit the loop if the condition is not satisfied. For example, we want to calculate the sum of all the odd numbers within 100, using the while loop
Sum=n=n>: Sum=sum+n n=n-(SUM)
Interrupt current loop Break, demo
Sum=n=n>: sum=sum+n n = n-n <: (n) (sum)
Continue end this cycle, continue execution
n=n<: n=n+ (N)
Print only odd numbers
n=n<: n=n+n% = =: (n)
Python conditional judgment and looping