Home > Backend Development > Python Tutorial > How to use for loop in Python

How to use for loop in Python

WBOY
Release: 2023-10-25 12:18:25
Original
1486 people have browsed it

How to use for loop in Python

How to use the for loop in Python

Python is a simple and easy-to-use programming language, and the for loop is one of the most commonly used tools. By using for loops, we can loop through a series of data, perform effective processing and operations, and improve the efficiency of the code.

Below, I will introduce how to use the for loop in Python through specific code examples.

  1. Basic for loop syntax

In Python, the syntax of the for loop is as follows:

for 变量 in 可迭代对象:
    # 循环体代码
Copy after login

The variable refers to the current loop execution time The value of an iterable object refers to a collection of data that can be traversed, such as a list, tuple, string, etc.

  1. Loop through lists

Lists are one of the most common data types in Python. We can use a for loop to iterate through each element in the list and perform related processing.

fruits = ["apple", "banana", "orange"]
for fruit in fruits:
    print(fruit)
Copy after login

Run the above code, the output result is:

apple
banana
orange
Copy after login
  1. Loop through the string

String is also a common data type in Python. We can use a for loop to iterate through each character in the string and perform related processing.

message = "Hello, World!"
for char in message:
    print(char)
Copy after login

Run the above code, the output result is:

H
e
l
l
o
,
 
W
o
r
l
d
!
Copy after login
  1. Loop through tuples

Tuples are immutable data types in Python, you can Contains multiple elements at the same time. We can use a for loop to iterate through each element in the tuple and perform related processing.

person = ("John", 25, "Male")
for value in person:
    print(value)
Copy after login

Run the above code, the output result is:

John
25
Male
Copy after login
  1. Loop through the dictionary

The dictionary is a very useful data type in Python, which consists of keys - consists of value pairs. We can use a for loop to iterate through each key or value in the dictionary and perform related processing.

student = {"name": "John", "age": 25, "gender": "Male"}
for key in student:
    print(key, ":", student[key])
Copy after login

Run the above code, the output result is:

name : John
age : 25
gender : Male
Copy after login
  1. Loop through the range

In Python, we can also use the range() function to Generate a range of numbers and then use a for loop to iterate over it.

for i in range(5):
    print(i)
Copy after login

Run the above code, the output result is:

0
1
2
3
4
Copy after login

The above is a specific code example of how to use the for loop in Python. By mastering the basic syntax and common usage of for loops, we can process data more flexibly and achieve more efficient code when writing Python programs. Hope this article can help you!

The above is the detailed content of How to use for loop in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template