系统相关
首页 > 系统相关> > Linux + Python 的第六天

Linux + Python 的第六天

作者:互联网

今天的实验是在vscode的环境下进行的,vscode可以像shell中一样“tab”,还可以像window简易操作。

1、for循环嵌套
## 楼层 + 房间号 嵌套循环
for i in range(1,4):
    print(f"===========Floor-{i}============")
    for j in range(1,4):
        print(f"L{i}-0{j}")
[Running] C:\Users\Administrator\AppData\Local\Programs\Python\Python310\python.exe -u "C:\Users\ADMINI~1\AppData\Local\Temp\tempCodeRunnerFile.python"        
===========Floor-1============
L1-01
L1-02
L1-03
===========Floor-2============
L2-01
L2-02
L2-03
===========Floor-3============
L3-01
L3-02
L3-03

[Done] exited with code=0 in 0.126 seconds
2、for + if 2层嵌套

for i in range(1,4):
    if i == 2 :
        continue
    print(f"Floor-{i} is ok.")
    for j in range(1,6):
        if i == 3 and j == 3:
            print("202 can not be arrived")
            break
        print(f"Floor-{i}-{j} is avaiable")
[Running] C:\Users\Administrator\AppData\Local\Programs\Python\Python310\python.exe -u "C:\Users\ADMINI~1\AppData\Local\Temp\tempCodeRunnerFile.python"
Floor-1 is ok.
Floor-1-1 is avaiable
Floor-1-2 is avaiable
Floor-1-3 is avaiable
Floor-1-4 is avaiable
Floor-1-5 is avaiable
Floor-3 is ok.
Floor-3-1 is avaiable
Floor-3-2 is avaiable
202 can not be arrived

[Done] exited with code=0 in 0.112 seconds

# 除了嵌套之外,循环中还加入了continue和break,两个的功能在这里补充下:
1、break 只要程序遇到break,就会结束当前这个循环,注意如果是多层嵌套循环,只会结束当前层的循环。
2、continue 只要程序遇到continue,本次循环就不会继续了,直接进入下次循环。
3、for循环打印等腰三角形
for i in range(10):
    if i <= 5:
        print("*" * i )
    else:
        print((10 - i ) * "*")
[Running] C:\Users\Administrator\AppData\Local\Programs\Python\Python310\python.exe -u "C:\Users\ADMINI~1\AppData\Local\Temp\tempCodeRunnerFile.python"

*
**
***
****
*****
****
***
**
*

[Done] exited with code=0 in 0.13 seconds
4、while循环
# 之前的笔记中记录过while + if 的循环了,这里补充一个计数的概念。
count = 0
while count < 3:
    count += 1
    print(f"count {count} times")
[Running] C:\Users\Administrator\AppData\Local\Programs\Python\Python310\python.exe -u "C:\Users\ADMINI~1\AppData\Local\Temp\tempCodeRunnerFile.python"
count 1 times
count 2 times
count 3 times

[Done] exited with code=0 in 0.109 seconds
4、for循环打印九九乘法表
for i in range(1,10):
    for j in range(1,i+1):
        print(f"{j}x{i}={i * j}", end=" ")
    print() 
[Running] C:\Users\Administrator\AppData\Local\Programs\Python\Python310\python.exe -u "C:\Users\ADMINI~1\AppData\Local\Temp\tempCodeRunnerFile.python"
1x1=1 
1x2=2 2x2=4 
1x3=3 2x3=6 3x3=9 
1x4=4 2x4=8 3x4=12 4x4=16 
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25 
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36 
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49 
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64 
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81 

[Done] exited with code=0 in 0.137 seconds

# 我们可以通过help查看一下,print的用法,其中print默认会有一个‘\n’,需要添加end参数;最后一个print的意义也同样是因为默认有一个‘\n’,所以会将数据打印成多列。
>>> help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
5、for循环中的九九乘法表
[tanuki@localhost python_study]$ vim loop.sh
[tanuki@localhost python_study]$ bash loop.sh
1*1=1      
2*1=2      2*2=4      
3*1=3      3*2=6      3*3=9      
4*1=4      4*2=8      4*3=12      4*4=16      
5*1=5      5*2=10      5*3=15      5*4=20      5*5=25      
6*1=6      6*2=12      6*3=18      6*4=24      6*5=30      6*6=36      
7*1=7      7*2=14      7*3=21      7*4=28      7*5=35      7*6=42      7*7=49      
8*1=8      8*2=16      8*3=24      8*4=32      8*5=40      8*6=48      8*7=56      8*8=64      
9*1=9      9*2=18      9*3=27      9*4=36      9*5=45      9*6=54      9*7=63      9*8=72      9*9=81      
[tanuki@localhost python_study]$ cat 1.sh 
#!/bin/bash
for ((  i=1;i<=9;i++  ))
do
   for ((  j=1;j<=9;j++  ))
   do
   [  $j  -le  $i  ]  &&  echo -n "${i}*${j}=$((i*j))      " 
   done
   echo ""              
done

标签:count,AppData,Floor,Python,python,第六天,Linux,print,Local
来源: https://www.cnblogs.com/tanukisama/p/16391150.html