Day -9 while与for

while 常见用法如下:

//while 
count = 1
while count<=5:
  print(count)
  count += 1

// result 
1
2
3
4
5

//while break 
count = 1
while count != 6:
  print(count)
  count += 1
  if count > 5:
    break

//result
1
2
3
4
5


//while break continue     
count = 0
while count != 6:
  if count > 5:
    break
  count += 1
  if count % 2 == 0:
    continue
  print(count)

//result
1
3
5 

// while break continue & if else  
guess_me = 7
number = 1
while True:
  if number < guess_me:
    print('too low')
  elif number == guess_me:
    print('found it!')
    break
  else:
    print('oops')
    break
  number += 1
  
// result

too low
too low
too low
too low
too low
too low
found it!

guess_me = 7
for number in range(10):
  if number < guess_me:
    print('too low')
  elif number == guess_me:
    print('found it!')
    break
  else:
    print('oops')
    break
    
// result
too low
too low
too low
too low
too low
too low
too low
found it!

同样的 guess_me = 7 for 会多显示一次too low,其他常见for用法。

a =["A", "B", "C", "D", "E"]
for num in a:
  print(num)
  
// result
A
B
C
D
E
  
for num in range(10,0,-1):
  print(num)
 
// result
10
9
8
7
6
5
4
3
2
1

a = [0,9,2,2]
for num in a:
  print(num)
  
// result 
0
9
2
2
  • range(10) # 0 到10
0 1 2 3 4 5 6 7 8 9
  • 1, 11 # 1 到 11
1 2 3 4 5 6 7 8 9 10
  • range(0, 30, 5) # 每个值相差5
0  5 10 15 20 25

<<:  统一状态管理 + 单一资料流

>>:  GCP NAT

op.27 《全领域》-全域开发实战 - 居家植物盆栽 Mvt II (C# Broker & Mysql)

op.27 纪录时空间的情话 让我们彼此之间的记忆与美好时光 永恒纪载 今天来将 Broker 进...

[Day3] - 前端,後端是在做什麽? --续 前端後端的历史及架构

其实每个时期程序流行的架构以及写法会略有不同,每个时期前端後端负责的范畴也不尽相同,我们无法知道我们...

09 - ripgrep - 快速查找档案内容

grep 是查找档案中内容的指令,在一般的作业情境下, grep 的功能是十分足够了,但是在开发作业...

C# 入门笔记01

程序架构 Namespace (自订命名空间) 就是由自己写的程序库之名称,一个程序库只能有一个自订...

从 JavaScript 角度学 Python(16) - pip

前言 前面章节我们学习了许多 Python 的基础语法,所以接下来我想额外介绍 pip,pip 在实...