python冒泡循环示例

Python for loop is used for iterating over a sequence. The for loop is present in almost all programming languages.

Python for循环用于迭代序列。 for循环几乎存在于所有编程语言中。

Python for循环 (Python for loop)

We can use for loop to iterate over a list, tuple or strings. The syntax of for loop is:

我们可以使用for循环遍历列表,元组或字符串。 for循环的语法为:

for itarator_variable in sequence_name:Statements. . .Statements

Let’s look at some examples of using for loop in Python programs.

让我们看一些在Python程序中使用for循环的示例。

使用for循环打印字符串的每个字母 (Using for loop to print each letter of a string)

Python string is a sequence of characters. We can use for loop to iterate over the characters and print it.

Python字符串是字符序列。 我们可以使用for循环遍历字符并打印出来。

word="anaconda"
for letter in word:print (letter)

Output:

输出

a
n
a
c
o
n
d
a

遍历列表,元组 (Iterating over a List, Tuple)

List and Tuple are iterable objects. We can use for loop to iterate over their elements.

List和Tuple是可迭代的对象。 我们可以使用for循环遍历它们的元素。

words= ["Apple", "Banana", "Car", "Dolphin" ]
for word in words:print (word)

Output:

输出

Apple
Banana
Car
Dolphin

Let’s look at an example to find the sum of numbers in a tuple.

让我们看一个示例,查找元组中的数字总和。

nums = (1, 2, 3, 4)sum_nums = 0for num in nums:sum_nums = sum_nums + numprint(f'Sum of numbers is {sum_nums}')# Output
# Sum of numbers is 10

Python嵌套循环 (Python Nested For Loop)

When we have a for loop inside another for loop, it’s called nested for loop. The following code shows nested for loops in Python.

当我们在另一个for循环中有一个for循环时,称为嵌套for循环。 以下代码显示了Python中嵌套的for循环。

words= ["Apple", "Banana", "Car", "Dolphin" ]
for word in words:#This loop is fetching word from the listprint ("The following lines will print each letters of "+word)for letter in word:#This loop is fetching letter for the wordprint (letter)print("") #This print is used to print a blank line

Output

输出量

带有range()函数的Python for循环 (Python for loop with range() function)

Python range() is one of the built-in functions. It is used with for loop to run a block of code specific number of times.

Python range()是内置函数之一。 它与for循环一起使用,以运行特定次数的代码块。

for x in range(3):print("Printing:", x)# Output# Printing: 0
# Printing: 1
# Printing: 2

We can also specify start, stop, and step parameters for range function.

我们还可以为范围功能指定开始,停止和步进参数。

for n in range(1, 10, 3):print("Printing with step:", n)# Output# Printing with step: 1
# Printing with step: 4
# Printing with step: 7

带for循环的break语句 (break statement with for loop)

The break statement is used to exit the for loop prematurely. It’s used to break the for loop when a specific condition is met.

break语句用于过早退出for循环。 当满足特定条件时,它用于中断for循环。

Let’s say we have a list of numbers and we want to check if a number is present or not. We can iterate over the list of numbers and if the number is found, break out of the loop because we don’t need to keep iterating over the remaining elements.

假设我们有一个数字列表,我们想检查一个数字是否存在。 我们可以遍历数字列表,如果找到了该数字,请跳出循环,因为我们不需要继续遍历其余元素。

nums = [1, 2, 3, 4, 5, 6]n = 2found = False
for num in nums:if n == num:found = Truebreakprint(f'List contains {n}: {found}')# Output
# List contains 2: True

用for循环继续语句 (continue statement with for loop)

We can use continue statements inside a for loop to skip the execution of the for loop body for a specific condition.

我们可以在for循环内使用continue语句,以跳过特定条件下for循环主体的执行。

Let’s say we have a list of numbers and we want to print the sum of positive numbers. We can use the continue statements to skip the for loop for negative numbers.

假设我们有一个数字列表,我们想打印正数之和。 我们可以使用continue语句跳过for循环中的负数。

nums = [1, 2, -3, 4, -5, 6]sum_positives = 0for num in nums:if num < 0:continuesum_positives += numprint(f'Sum of Positive Numbers: {sum_positives}')

带有可选else块的Python for循环 (Python for loop with optional else block)

We can use else clause with for loop in Python. The else block is executed only when the for loop is not terminated by a break statement.

我们可以在Python中将else子句与for循环一起使用。 仅当for循环未由break语句终止时,才会执行else块。

Let’s say we have a function to print the sum of numbers if and only if all the numbers are even. We can use break statement to terminate the for loop if an odd number is present. We can print the sum in the else part so that it gets printed only when the for loop is executed normally.

假设我们有一个函数,当且仅当所有数字均为偶数时,才打印数字之和。 如果存在奇数,我们可以使用break语句终止for循环。 我们可以在else部分中打印总和,以便仅在正常执行for循环时才打印总和。

def print_sum_even_nums(even_nums):total = 0for x in even_nums:if x % 2 != 0:breaktotal += xelse:print("For loop executed normally")print(f'Sum of numbers {total}')# this will print the sum
print_sum_even_nums([2, 4, 6, 8])# this won't print the sum because of an odd number in the sequence
print_sum_even_nums([2, 4, 5, 8])# Output# For loop executed normally
# Sum of numbers 20

结论 (Conclusion)

The for loop in Python is very similar to other programming languages. We can use break and continue statements with for loop to alter the execution. However, in Python, we can have optional else block in for loop too.

Python中的for循环与其他编程语言非常相似。 我们可以使用带有for循环的break和Continue语句来更改执行。 但是,在Python中,我们也可以在for循环中使用可选的else块。

翻译自: https://www.journaldev.com/14136/python-for-loop-example

python冒泡循环示例

python冒泡循环示例_Python for循环示例相关推荐

  1. python循环程序_python的循环

    python的循环 编写程序时经常有代码需要重复运行,python提供了while和for进行循环操作. 一.while循环 1.while循环可以根据条件进行判断,决定是否要循环执行语句块,语法如下 ...

  2. python中for循环流程图_Python While循环语句实例演示及原理解析

    这篇文章主要介绍了Python While循环语句实例演示及原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Python 编程中 while ...

  3. python中for循环流程图_Python while 循环

    Python while 循环 循环在编程中用于重复特定的代码块.在本文中,您将学习如何在Python中创建while循环. 什么是Python中的while循环? 只要测试表达式(条件)为真,Pyt ...

  4. python退出循环快捷_python退出循环的方法

    break 语句 Python break语句,就像在C语言中,打破了最小封闭for或while循环. break语句用来终止循环语句,即循环条件没有False条件或者序列还没被完全递归完,也会停止执 ...

  5. python 示例_Python中带有示例的关键字除外

    python 示例 Python关键字除外 (Python except keyword) except is a keyword (case-sensitive) in python, it is ...

  6. python循环迭代_Python中循环迭代的重做

    不,Python不直接支持redo.有一个选项可能会让嵌套循环变得非常糟糕,比如:for x in mylist: while True: ... if shouldredo: continue # ...

  7. python for循环换行_python for循环换行_day08-循环之for循环

    for循环-前言 '''1.什么是for循环 循环就是重复做某件事,for循环是python提供第二种循环机制2.为何要有for循环 理论上for循环能做的事情,while循环都可以做 之所以要有fo ...

  8. python如何实现隔行_Python编写循环的两个建议 | 鹅厂实战

    作者 | piglei(腾讯高级工程师) 转载自腾讯技术工程知乎专栏 循环是一种常用的程序控制结构.我们常说,机器相比人类的最大优点之一,就是机器可以不眠不休的重复做某件事情,但人却不行.而" ...

  9. python画魔法阵_Python编写循环的两个建议 | 鹅厂实战!

    本文系 "Python 工匠"系列的第 7 篇文章,已取得作者授权. 循环是一种常用的程序控制结构.我们常说,机器相比人类的最大优点之一,就是机器可以不眠不休的重复做某件事情,但人 ...

最新文章

  1. ASP.NET网站建设基本常用代码
  2. 一个 零差评的 Python 内置库
  3. PHP (20140508)
  4. php联想输入,自动联想搜索提示功能
  5. php用session制作网站仿恶意刷新计数器
  6. 在Ubuntu系统下如何将chrome浏览器的bookmarks导出到本地
  7. dubbo provider异步_Dubbo学习(六) Dubbo面试问题
  8. 5寸屏,智能之外也可以当数码相框
  9. findstr()与strfind()的区别
  10. java判断光标位置_Java如何知道光标的当前位置?
  11. 全国网上统考计算机试题及答案,2017 年研究生全国统考计算机试题操作系统题目及答案...
  12. Skype和LibFetion无法输入中文的解决方法
  13. 同义词词林或哈工大词林扩展的词类.
  14. [产品设计]如何绘制业务流程图(下)
  15. UINO优锘:【万物可视系列之四】可视化“疏通”智慧港口数据“阻塞”
  16. android 国际化之Locale
  17. maxscript rollout
  18. MM 固定汇率的使用在MIRO的问题
  19. instr,left,mid 定位、取内容(字符串)函数
  20. 计算机多媒体培训总结,多媒体培训心得体会

热门文章

  1. 上传并解析XML文件
  2. SharePoint 2010 technology stack
  3. 【已解决】python远程连接数据库问题
  4. [转载] python中bool啥意思_Python中的bool类型
  5. [转载] python缩进报错_python缩进报错
  6. [转载] python中list与string的转换
  7. [转载] python中if嵌套语句_Python的if语句怎么嵌套
  8. [转载] python中异常处理的四个句子_Python学习笔记总结(四)异常处理
  9. vivado中FIFO IP核的Standard FIFO和First-word-Fall-Through模式的仿真比较
  10. 序列的修改、散列和切片