Python--Conditional judgment and looping

Source: Internet
Author: User

Python's judgment

Like other languages, Python also has conditional judgment and looping operations, such as we can write a simple judgment operation: using the IF keyword can be used to achieve the effect of judgment, the following example:

1>>> test_if =raw_input ()2503>>>ifTest_if < 50:4...Print "You is so yamg"5...Else:6...Print "You is so old!"7 ... 8 You is so old!9>>>

In this simple judgment, first, determine if the condition of the IF statement is not True, if yes then execute print "You is so yamg", otherwise, execute else clause content: print "You is so old!"

Attention:

When using the If execution judgment, do not forget to write ":" or Python compilation is no way to pass:

1>>>iftest_if2File"<stdin>", Line 13     iftest_if4^5 syntaxerror:invalid Syntax6>>>iftest_if:7...Print("hello!")8...Else9File"<stdin>", Line 3Ten     Else One^ A syntaxerror:invalid Syntax ->>>

The above judgments are written only for demonstration purposes, usually in the development process will write more complex judgments, this time we need to use "elif" to help us complete.

1 if < condition judgment 1>:2     < execute 1>3elif < condition judgment 2>:  4     < execute 2>5elif < condition 3>:6     < execute 3>7  Else:8     < execute 4>
1>>>ifTest_elif < 5:2...Print "a"3...elifTest_elif > 5 andTest_elif<10:4...Print "b"5...elifTest_elif > 10:6...Print "C"7...Else:8...Print "d!"9 ... Ten C One>>>

ifStatement execution has a characteristic, it is to judge from the top, if in a certain judgment True , the corresponding statement of the judgment executed, then ignore the remaining elif and else .

Shorthand for the if statement:

1>>> test1 =raw_input ()2233>>>iftest1:4...Print("yes!")5...Else:6...Print("false!") 7 ... 8 yes!9>>>

As long as the test1 is non-0 numeric value, non-empty string, non True -empty list, etc., it is judged, otherwiseFalse

Note: raw_input() The read content is always returned as a string, and the string and integer comparisons do not get the desired result, and the string must first be int() converted to the integer type we want:

1>>> h = Int (Raw_input ('H:'))2H:123>>>h4125>>>#The above shows a numeric type of type int, but it is really a string type example with Raw_input ():6 ... 7>>> Test_char =raw_input ()8129>>>Test_charTen ' A' One>>>

The cycle of Python:

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:

1>>>#Loop Print List2 ... 3>>> name = ['a','b','C','D','e','F']4>>>5>>>name6['a','b','C','D','e','F']7>>> forNinchName:8...PrintN9 ... Ten a One b A C - D - e the F ->>>

So for x in ... the loop is to take each element into the variable and n then execute the indented block of the statement. For example, if we want to calculate the sum of 1-10 integers, we can use a sum variable to accumulate:

1>>> sum =02>>> forXinch[1,2,3,4,5,6,7,8,9,10]:3... sum + =x4...Printsum5 ... 617386910Ten15 One21st A28 -36 -45 the55

You can use range to simply implement the same sample:

  1  >>> for  x in  Range (11  ... sum += x   3  ... print     ...   1 6  3 7  6 8  10 9  1510  2111  2812  3613  4514  5515  >>> 

The second loop is the while loop, which, as long as the condition is satisfied, loops continuously, exiting the loop when the condition is not satisfied. For example, we want to calculate the sum of all the odd numbers within 100, which can be implemented with a while loop:

1>>> sum =02>>>sum3 04>>> n = 995>>> whilen >0:6... sum + =N7... n-= 28...Printsum9 ... Ten99 One196 A291 -384 -475 the564 -651 -736 -819 +900 -979 +1056 A1131 at1204 - .  - .  -。

The python conditional judgment and looping explanation ends here!

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.