Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the for loop in Python

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use the for loop in Python. The introduction in the article is very detailed and has certain reference value. Interested friends must read it!

The for loop is one of Python's loop statements, which is often used to traverse sequence types such as strings, lists, tuples, dictionaries, and collections to obtain each element of the sequence one by one. Xiaobian will summarize the learning knowledge points of the for loop for everyone, mainly including: for loop syntax, example display, loop use else statement and for loop statement application.

For loop syntax

for iterating_var in sequence:

statements(s)

II. Examples

#!/ usr/bin/python

# -*- coding: UTF-8 -*-

for letter in 'Python':#first instance

print 'current letter:', letter

fruits = ['banana', 'apple', 'mango']

for fruit in fruits: #second instance

print 'current fruit:', fruit

print "Good bye! "

Third, the circular use of else statements

In python, for … else means that the statements in for are no different from ordinary ones, and the statements in else will be executed when the loop finishes normally (i.e., for does not break by breaking), and the same is true for while … else.

examples

#!/ usr/bin/python

# -*- coding: UTF-8 -*-

for num in range(10,20): #Iterate numbers between 10 and 20

for i in range(2,num): #Iterate by factor

if num%i == 0: #Determine the first factor

j=num/i #Calculate the second factor

print '%d equals %d * %d' % (num,i,j)

break #Jump out of the current loop

else: #else part of the loop

print num, 'is a prime number'

IV. Application of for loop statements

1. String

str = "hello" #string hello

for n in str:

print(n,end=" ")

2. Print 1-100

for num in range(1,101): #1-100

print(num)

3. Print triangles

n = int(input("Please enter the number of rows:"))

for trigon in range(n): #range(0,n)

for tri in range(trigon+1):

print("*",end=" ")

print()

4. Print rectangles

n = int(input("Please enter the number of rows:"))

for rectangle in range(n):

for rec in range(n):

print("*",end=" ")

print()

Print even numbers between 1 and 100

for num in range(1,101):

if num%2 == 0: #even

print(num)

Print odd numbers between 5 and 88 and be divisible by 3

for num in range(5,89):

if num%2 == 1 and num%3 == 0: #odd and divisible by 3

print(num)

7. Print the multiplication table

for i in range(1,10):

for j in range(1,i-1):

print(j,"*",i,"=",(i*j),end="\t")

print()

Print the first 20 even numbers between 33 and 88

count = 0

for num in range(33,89):

if num%2 == 0:

count += 1 #Quantity +1

print(num)

if count == 20:

break

print(num)

9. Print even numbers between 1-50, if even numbers are greater than 40, stop the cycle

for num in range(1,51):

if num%2 == 0 and num

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report