Home  >  Article  >  Backend Development  >  What is for in python

What is for in python

(*-*)浩
(*-*)浩Original
2019-06-26 16:29:2010318browse

Python for loop can iterate through any sequence of items, such as a list or a string.

What is for in python

Grammar: (Recommended learning: Python video tutorial)

The syntax format of the for loop is as follows:

for iterating_var in sequence:
   statements(s)

Flow chart:

What is for in python

Example:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
for letter in 'Python':     # 第一个实例
   print '当前字母 :', letter
 
fruits = ['banana', 'apple',  'mango']
for fruit in fruits:        # 第二个实例
   print '当前水果 :', fruit
 
print "Good bye!"

Output result of the above example:

 当前字母 : P
当前字母 : y
当前字母 : t
当前字母 : h
当前字母 : o
当前字母 : n
当前水果 : banana
当前水果 : apple
当前水果 : mango
Good bye!

For more Python related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of What is for in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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