html for循环给href传递变量_标题: [Python Basic] 什么是循环(2)

本文详细介绍了Python中的For循环和range()函数。For循环根据range()函数指定的次数重复执行代码块,range()函数可以创建数字列表,接受起始、结束和递增值参数。此外,还讨论了嵌套循环和For循环在迭代列表、元组、字典和集合中的应用。
摘要由CSDN通过智能技术生成

e0db8e567236a74b67e0f1c4d58fa335.png

副标题: 20. What are Loops (2)

这个专栏的文字, 不仅仅是学习 Python 的基础知识, 同时, 也按照 2/8 规律学习关键知识的关键部分 - python 核心英语词汇. 我们开始这一节的正文.

For loops

什么是For循环, 下面将举例说明, 除了while循环,您还可以使用For循环。 英文: So, what are For loops. Besides the while loop, you can also use the For loop.

第三次强调: 注意For语句末尾的冒号。 正如在条件语句视频中提到的,任何缩进代码块之前的行都必须以冒号结束。 Notice the colon at the end of the For statement. As mentioned in the video on Conditionals, the line preceding any indented block of codes must end with a colon.

概念: range()函数称为可迭代函数,这意味着它是可以重复并且可以循环多次的东东。 英文:The range() function is called the iterable, which means that it is something that can be repeated and looped through a number of times.

功能: range()函数可让你方便地创建数字列表,然后用它们迭代次数, (具体次数)由传递给函数的参数指定。 The range() function allows you to create a list of numbers easily, and then use them for iterating the number of times, as specified by the argument passed to the function.

range() 函数

range(10) 循环举例分析

例子 1: 留意 count 在这里是变量

for count in range(10):
    print(count+1)
    # output: 
              1
              2
              3
              4
              5
              6
              7
              8
              9
              10
    ## 具体到当前的例子:请注意,range(10)的值范围为:从0到9,因为它始终从0而不是1开始。
COUNT 变量 将接收每个迭代范围 (10) 内的每个值,
从0到9,这意味着它将不包括10。所以,为了打印数字1到10,我们 print count加1。

For 循环按照 range() 函数中指定的次数重复运行。缩进的代码块将在每一个重复的循环中执行。 英文: The For loop repeats itself the number of times as specified in the range() function. The indented block of codes will be executed for every loop that is repeated.

range() 函数设置数字区域举例:

(例如)要将起始数传递给range()函数: 传递两个参数,起始值 2 和结束值 15. 英文: To pass a starting number to the range() function you pass 2 arguments, the starting value 2 and the ending value 15.

传递开始数字给 range()函数举例:
for count in range(2, 15):
    print(count)                 ### 注意: Mac 下 terminal 中如果不输入之前的 4 个空格,会报错.
        # output: 
                  2
                  3
                  4
                  5
                  6
                  7
                  8
                  9
                  10
                  11
                  12
                  13
                  14
        ## 对于范围2逗号15中的计数, print(count), 将导致 count 接收值 2,3,4,5,6,7,8,9,10,11,12,13 和 14,因为它不会包含大于或等于15的任何内容,  并且每个值在每个循环中都默认增加1. 
        ## 英文: the starting value 2 and the ending value 15, So, for count in range 2 comma 15, print count will result in count receiving the values 2,3,4,5,6,7,8,9,10,11,12,13,14, because it will not include anything greater than or equal to 15, and each value is incremented by the default 1 on each loop.

range() 函数中递增和递减举例:

递增例子:

可以更进一步,(range 函数中)除了起始值和结束值之外,还可以包含一个递增值。 英文: You can go a step further by including an increment value, besides the starting value, and the ending value.

range()函数递增例子

for count in range(2, 14, 4):  ## 起始范围是 2 - 14; 4 是递增值.
    print(count) 
        # output:
                  2
                  6
                  10

递减例子:

range()函数递减例子

for count in range(20, 7, -5):    ## 20是开始值,7是结束值, -5是减量值
    print(count)
        # output: 
                  20
                  15
                  10

        ## print(count) will result in count receiving the values 20,15,10, because it will not include anything less than or equal to 7, and each value is DECREMENTED by 5 on each loop.

Nested loops 嵌套循环

(类似嵌套条件语句 netsed if)

接下来,可以像嵌套 if (嵌套条件语句 netsed if)一样使用嵌套循环。 循环用于通过多次重复执行一段代码来执行迭代。 你还可以在循环中执行另一个循环。 你可以在For循环中执行while循环,或者在while和For循环之间的任意组合中执行。 当需要处理多维数据时,我们使用嵌套循环。

英文: Next, you can have nested loops just as you have nested if. Loops are used to perform iterations by repeating the execution of a piece of code multipie times. You can also perform another loop within a loop. And you can perform a while loop within a For loop, or for that matter, in any combination, between while and For loops. We use nested loops, when we need to handle multidimensional data.
例子:

for count_a in range(1, 4):
    for count_b in range(1, 4):     ## 4 空格缩进
        for count_c in range(1, 4): ## 4 + 4 空格缩进
                print(count_a, count_b, count_c)    ## 4  + 4 + 4 共 12 空格缩进

                ## 注意,每个for循环的缩进更深,因为每个for循环都是上面for循环的缩进代码块。
                ## 英文: Note the indentation goes deeper for each For loop, because, each For loop is an indented block of code for the For loop above it.

                    # output:
                          1 1 1
                          1 1 2
                          1 1 3
                          1 2 1
                          1 2 2
                          1 2 3
                          1 3 1
                          1 3 2
                          1 3 3
                          2 1 1
                          2 1 2
                          2 1 3
                          2 2 1
                          2 2 2
                          2 2 3
                          2 3 1
                          2 3 2
                          2 3 3
                          3 1 1
                          3 1 2
                          3 1 3
                          3 2 1
                          3 2 2
                          3 2 3
                          3 3 1
                          3 3 2
                          3 3 3

for循环 迭代列表:

我们可以使用For循环来迭代列表, We can iterate a list using the For loop

student_list = ["Mary", "John", "Mike", "Alice"]
for student_name in student_list: 
    print(student_name)

    # output: 
              Mary
              John
              Mike
              Alice

student_list被称为每个for循环的可迭代表; student_list将被存储到变量student_name中并打印出来。 英文: student_list is called the iterable for each For Loop, each item inside the student_list, will be stored into the variable student_name and printed out.

while 循环迭代列表

我们还可以使用While循环来迭代列表

student_list = ["Mary", "John", "Mike", "Alice"]
index = 0    ## 
while index < len(student_list)   格式有问题, 待查原因.
    print(student_list[index])    ## student_list[index] 函数用于取学生名字的 index 号.
    index += 1

        # output:
              Mary
              John
              Mike
              Alice        

    ## 只要学生列表中仍有项目,就会打印出包含该索引位置的项目 英文:As long as there is still items in the student list, the item holding that index position will be printed out
python index = 0 while index < len(student_list)
代码解释: 在这里,我们首先通过while循环将每个迭代的索引初始化为0,检查索引是否小于学生列表的长度,即学生列表中的项数。 英文:here, we first initialise the index to 0 for each iteration through the while loop, we check if index is less than the length of the student list, which is the number of items in the student list.

While 和 For 循环迭代多种数据

同样,使用While和For循环,您可以迭代元组、字典和集合。这些叫做iterables。 Iterables本身有许多值,因此循环控制结构可以迭代列表中的每一项。

英文: Original Sinmilarly, using While and For loops,you can iterate tuples, dictionaries, and sets.These are called iterables.Iterables has many values within itself,so a loop control structure can iterate through every item inside the iterable.

词汇:

a decreasing value 递减值 an increment value 递增值

while loop, for loop. len()函数 index函数.

发布时间: 2020 年 3 月 1 日 知乎链接: [Python Basic] 什么是循环(2)当前可任意转载,转载请保存以上信息即可。无需获得授权.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值