Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the for loop in Python

Shulou Source: shulou.com Published: 2022-06-02 22:15:49 10月04日 Update

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

Tags: Loops grades statements between even instance input factor character string number type content classmate odd sequence knowledge article grammar UTF-8 Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Shulou Information MariaDB macOS Shulou Technology