Conditional judgment and looping on the basis of Python

Source: Internet
Author: User
1. Conditional judgment
age = 3if age >= 18:    print(‘adult‘)elif age >= 6:    print(‘teenager‘)else:    print(‘kid‘)

Elif is an else if abbreviation and can have more than one elif.

2. Cycle

There are two types of Python loops,

One is the for...in loop, which sequentially iterates through each element of the list or tuple.
names = ["geg","gege","geege"];for name in names:    print(name)

If you want to calculate the sum of 1-100 integers, it would be tiring to write a list using the exhaustive method, but if Python provides a range () function, you can generate an integer sequence and then convert the sequence to a list by using the list () function.

list(range(5));[0,1,2,3,4]//注意这里是0开始到4.
The other is a while loop, which, as long as the condition is satisfied, loops continuously and the condition does not satisfy the exit loop.
sum = 0;n = 99;while n > 0:    sum = sum + n    n = n - 2print(sum)

In a loop, you can use break to exit the loop early, or you can use continue to skip the current loop and start the next loop directly during the loop.

Conditional judgment and looping on the basis of Python

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.