Seven. Conditional Judgment and circulation

Source: Internet
Author: User

Conditional judgment

Computers can do a lot of automated tasks, because it can make their own conditions to judge.

For example, enter the age of the user, print different content according to age, in the Python program, with the if statement implementation:

Age =If age >=:    print'your     print'adult'

According to the indentation rules of Python, if the if statement is judged True , the indented two lines of the print statement are executed, otherwise, nothing is done.

It is also possible to if add a else statement, meaning that if the if judgment is False , do not execute if the content, go to put the else executed:

Age =If age >=:    print'your     print'adult'else:    Print ' your age is ' ,     age Print ' teenager '

Be careful not to write a colon less : .

Of course, the above judgment is very sketchy, can be used to elif do more detailed judgment:

Age = 10ifAge >= 18:    Print 'your age is', AgePrint 'Adult'elifAge <=12:    Print 'your age is', AgePrint 'Kid'Else:     Print 'teenager'

Cycle

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 = ['python','java','. NET' ,'demacia'] for in names:      Print Name

Range () function

If you want to calculate the sum of 1-100 integers, writing from 1 to 100 is a bit difficult, but fortunately Python provides a range () function that generates a sequence of integers, such as range (5), which generates a sequence that is less than 0, starting from 5:

Print range (5)

sum = 0 for in range (101):    = sum+xprint sum

Another discussion on Raw_input

Finally see a problematic condition to judge. Many students will raw_input() read the user's input, so that they can input, the program runs more interesting:

Birth = Raw_input (")if birth <    :Print ' 00 ago ' Else :     Print ' after 00 '

Input 1982 , and the results show 00后 that this simple judgment of Python can be mistaken?

Of course it's not a Python problem, print it out in Python's interactive command line birth :

Reason to find out! The original raw_input() read from the content is always returned as a string, the string and integer comparison will not get the expected results, you must first int() convert the string to the integer type we want:

Seven. Conditional Judgment and circulation

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.