Python conditional judgment and looping

Source: Internet
Author: User

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

Related Article

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.