for循环,文件管理

1、for循环

break结束循环 continue 终止本次循环,并开启下一次循环 控制循环结构执行

pass 占位 让程序什么都不做 #while True:

                                               if 1 == 1:

                                              pass

for循环 作用 用来遍历可迭代对象的数据元素

遍历是指经历且只经历一遍

可迭代对象 是指能够依次获取数据元素的对象    
比如列表、元组  、字符 、 字典

语法
for 变量列表 in 可迭代对象:
       语句块
else:
       语句块

#使用循环生成列表元素 x = 1 L1=[]

                                      while x <= 6: name = input("请输入的名字")

                                      L1.append(name) x +=1 #使用for循环输出列表元素

                                      for i in L1: print(i) #print("tarena")

#使用while循环输出列表元素 #y = 0 #while y < len(L1):

print(L1[y])

y += 1

else可以省略

for 变量列表 in 可迭代对象:
       语句块

s = 'ABCDE' for x in s: print('x=', x) else: print("遍历结束")

 

2、range()函数的使用

格式 range(起始数字 , 结束数字 ,步长) 

使用for循环把 192.168.4.0/24 网段所有主机ip地址输出

for i in range(1,256):

print("192.168.4.",i,sep="") x = 1

while x <= 255: print("192.168.4.",x,sep="") x += 1

假设一个用户信息如下: 用户名是:root 密码是: 123456 写一个身份验证的程序,让用户输入用户名和密码登录, 用户名和密码全部匹配,提示登录成功。 否则继续,最多尝试3次。3次不匹配以后提示登录失败.

times = 0 # 用来记录循环次数 while times < 3:

name = input('请输入用户名:') passwd = input('请输入密码: ')

if name != 'root' or passwd != '123456':

print('输入有错请重新输入')

times += 1 continue print("登陆成功!")

break else: print('登陆失败!')

循环嵌套 输出乘法表

vim cf.py

j = 1

while j <= 9:

i = 1

while i <= j: k=ij

print(i,"",j,"=",k,end=" ")

i += 1

j += 1

print()

[root@dc code]# python3 cf.py

1 * 1 = 1

1 * 2 = 2 2 * 2 = 4

1 * 3 = 3 2 * 3 = 6 3 * 3 = 9

1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16

1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25

1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36

1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49

1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64

1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81

斐波那契数列 (后边的数 总是前2个数相加的和)

[root@teacher code]# cat fb.py

a = 0 b = 1

for i in range(10):

print(a) a,b = b,a+b

[root@dc code]# python3 fb.py

0

1

1

2

3

5

8

13

21

34

数据数据模块的使用

import random

player = int(input("请输入 0剪刀 1石头 2布 :"))

computer = random.randint(0,2)

print("电脑的选择是:",computer) if (player ==0 and computer ==2) or (player ==1 and computer ==0) or (player==2 and computer ==1): print("赢了")

elif player==computer: print("平局")

else : print("输了")

[root@dc code]# python3 game.py 请输入 0剪刀 1石头 2布 :0 电脑的选择是: 2 赢了 [root@dc code]# python3 game.py 请输入 0剪刀 1石头 2布 :1 电脑的选择是: 0 赢了

文件管理

文件迭代**

逐行处理文件,可以结合for循环

*read读取字节到字符串中,不指定默认读到文件末尾,一次读取完放到变量中*

f.read()读取所有

readline读取文件第一行,依次输出,一次读取一行

f.readline()读取一行

f.close() 关闭文件

readlines读取所有行,放到列表中

f.readlines() [............]读取所有,放入列表

[root@dc code]# head -5 /etc/passwd > a.txt

f = open("/root/code/a.txt",'r')

data = f.readlines() for x in data: print(x)

[root@dc code]# python3 3.1.py root​:x:​0:0:root:/root:/bin/bash

bin​x:​1:1:bin:/bin:/sbin/nologin

daemon​x:​2:2:daemon:/sbin:/sbin/nologin

adm:x3:4:adm:/var/adm:/sbin/nologin

lp:x4:7:lp:/var/spool/lpd:/sbin/nologin

**

文件输出

write()向文件中写入内容

w 如果没有源文件会自动创建,如果有清空内容在写入

python3

f=open('/root/code/a.txt','w') f.write('12345678\n') 9 f.close()

12345678

writelines()项文件写入内容

python3

f=open('/root/code/a.txt','a') f.writelines(['abc\n','bnjk\n'])

f.writelines(['abc\n','bnjk\n']) f.writelines(['abc\n','bnjk\n']) f.close()

12345678 abc bnjk abc bnjk abc bnjk

文件内移动

seek(offset[,whence]:)移动指针到不同位置

#(偏移量,光标位置)0代表开头,2代表末尾,1代表当前

f.seek(0,0) 光标回到开头

f.seek(0,2) 光标回到末尾**

tell()返回当前文件指针位置

f=open('/root/code/a.txt','r+')

f.tell() 0

f.seek(0,2) 36

f.tell() 36 f.read() '' f.seek(0,0) 0 f.read(5) '12345'

with子句

读完文件,自动关闭文件

》》》with open('/root/code/a.txt','r') as asdf: ... data = asdf.readlines() ...

》》》asdf.closed True

用Python实现对文件拷贝

[root@dc code]# vim cp.py

[root@dc code]# python3 cp.py

f1 = open('/bin/ls','rb')

f2 = open('/root/ls','wb')

data = f1.read() f2.write(data)

f1.close()

f2.close()

[root@dc code]# ls /root/ls

/root/ls

 

 

 

 

 

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值