Python for &while Loop

Source: Internet
Author: User

1        Loops1.1   For inLoops

[email protected] python]# cat for.py

#!/usr/bin/python

#-*-Coding:utf-8-*-

list=[1,2,3,4,5]

For I in list:

Print (i)

List= (1,2,3,4,[3,4], (3,4))

For I in list:

Print (i)

[email protected] python]# python!$

Python for.py

1

2

3

4

5

1

2

3

4

[3, 4]

(3, 4)

[email protected] python]# cat sum100.py

#!/usr/bin/python

#-*-Coding:utf-8-*-

Sum=0

L= list (range (101)) List (range (101)) generates An ordered integer of 0-100

For I in L:

Sum=sum+i

Print (sum)

l=Tuple (range (101))

For I in L:

Sum=sum+i

Print (sum)

[email protected] python]# python sum100.py

5050

10100

1.2   WhileLoops

As long as the conditions are met, the cycle is repeated, and the condition is not satisfied when exiting the loop.

[email protected] python]# cat listpop.py

#!/usr/bin/python

#-*-Coding:utf-8-*-

l=[1,2,3,4]

Num=0

While L:

L.pop ()

Num=num+1

Print (num)

Delete the elements in L until you erase the

[email protected] python]# python listpop.py

4--4 is the number of elements in l


This article is from the "90SirDB" blog, be sure to keep this source http://90sirdb.blog.51cto.com/8713279/1795434

Python for &while Loop

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.