Python Tutorial - 6 - Loops and Iterations

Loops and Iterations: For - While Loops

/*

  • File: loops and iterations.md
  • Project: 6_loops-iterations
  • File Created: Sunday, 12th March 2023 9:28:08 pm
  • Author: Hanlin Gu (hg_fine_codes@163.com)

  • Last Modified: Wednesday, 15th March 2023 5:03:11 pm
  • Modified By: Hanlin Gu (hg_fine_codes@163.com>)
    */

1. for loop

for loop: print all the elements in a list

nums = [1, 2, 3, 4, 5]

for num in nums:
    print(num)

Output:

1
2
3
4
5

1.1. break

break statement breaks out of the loop. It will not continue execute the code after break in the for loop.

nums = [1, 2, 3, 4, 5]

for num in nums:
    if num == 3:
        print('Found!')
        break
    print(num)

Output:

1
2
Found!

1.2. continue

If one wants to ignore a value without break out of the loop completely, one can use continue statement. continue will skip to the next iteration of a loop.

nums = [1, 2, 3, 4, 5]

for num in nums:
    if num == 3:
        print('Found!')
        continue
    print(num)

Output:

1
2
Found!
4
5

When the program meets continue, it goes to the next iteration directly. Without continue, it will run through that iteration and then go to the next one.

nums = [1, 2, 3, 4, 5]

for num in nums:
    if num == 3:
        print('Found!')
    print(num)

Output:

1
2
Found!
3
4
5

1.3. Nested loop: loop within a loop

# Nested loop: loop within a loop
nums = [1, 2, 3, 4, 5]

for num in nums:
    for letter in 'abc':
        print(num, letter)

Output:

1 a
1 b
1 c
2 a
2 b
2 c
3 a
3 b
3 c
4 a
4 b
4 c
5 a
5 b
5 c

1.4. range(n)

Run through a loop for a certain number of times, one can use the build in function range().

for i in range(10):
    print(i)

Output:

0
1
2
3
4
5
6
7
8
9

If one wants to start from 1, and goes to 10, then use range(1, 11).

for i in range(1, 11):
    print(i)

Output:

1
2
3
4
5
6
7
8
9
10

2. while loop

while loop runs until certain condition is met or get a break.

x = 0

while x < 10:
    print(x)
    x += 1

Output:

0
1
2
3
4
5
6
7
8
9

2.1. break in while loop

x = 0

while x < 10:
    if x == 5:
        break
    print(x)
    x += 1

Output:

0
1
2
3
4

2.2. Infinite loop

Sometimes, one would like to create an infinite loop that never ends until one gets some input or finds some values. To create an infinite loop, simply to replace the conditional statement of while into True.

x = 0

while True:
    if x == 5:
        break
    print(x)
    x += 1

Output:

0
1
2
3
4

If one accidentally get stucked in a infinite loop, in general ctrl + c will terminate the process.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值