1. 程式人生 > >Python中while,for循環及文件操作,函數,模塊等操作

Python中while,for循環及文件操作,函數,模塊等操作

文件操作 rand 提示 don close choice line 轉發 字典

此內容本人原創,拒絕商業用途及他人轉發,嚴厲打擊有以上行為,發現後追究法律責任。
print內調用變量

>> print "tom is %d,jerry is %d" %(i,j) #%d調用 %i 第二個%d調用 %j
tom is 20,jerry is 30
#################################################
break用法 中斷所有循環
1.
#!/usr/bin/python
#coding:utf-8
#提示輸入用戶名
#直到輸入tom為止
while 1:
user=raw_input("請輸入用戶名:")

if user=="tom":
break

2.
#!/usr/bin/python
#coding:utf-8
import random
num=random.randint(1,100)
while 1:
user=int(raw_input(‘請輸入一個數‘))
if user == num:
print ‘你猜對了‘
break
elif user > num:
print ‘你猜大了‘
else:
print ‘你猜小了‘

#################################################
shell for 循環 do done
for i in 1 8 5 66

for i in {1..100}
for i in cat file
for i in ls /etc/

python for 循環
for i in 字符串:
###########################

>> x=‘hello the‘
>> len(x)
9
>> for i in x: ##for循環幾次跟下標有關,取下標的變量內容
... print i
...
h
e
l
l
o

t
h
e

for i in 字典:
#############################

>> x={‘phone‘:11,‘age‘:88,‘name‘:‘tom‘}

>> for i in x:
... print i
...
phone
age
name
############################

for i in 列表:

>> x=[11,88,‘tom‘]
>> for i in x:
... print i
...
11
88
tom

####################################

for i in xrange(8): #執行8次 從0開始 或者 for i in range(8):
兔子列表
x=[0,1]
for i in xrange(8):
tmp=x[-1]+x[-2]
x.append(tmp)
print x

##################################
偶數求和

>> range(1,10,2)
[1, 3, 5, 7, 9]
>> range(2,10,2)
[2, 4, 6, 8]

快速生成

>> [10 for i in range(5)] ###把常量10放入for循環裏面 print rang(5)循環五次
[10, 10, 10, 10, 10]
>> [‘qq‘for i in range(5)]
[‘qq‘, ‘qq‘, ‘qq‘, ‘qq‘, ‘qq‘]
>> [‘192.168.4.%d‘ %i for i in xrange(1,255)] ###把變量i放入for循環裏面print
######################################
Python對文件操作
open(‘文件名‘,權限) 默認為讀權限

x=open(‘/etc/hosts‘) ##讀權限打開,文件存在就報錯。打開文件內容把內容給變量x
x.readline() #默認讀一行
x.read() #讀所有
x.seek(0) #返回第一行
x.readline(6) #讀6個字節,一個字母一個字節
x.close() #關閉文件

for i in x: #循環一次讀一行 i取文件的行內容 自動換行

>> y=open(‘/root/new.txt‘,‘w‘) ##如果不存在就新建,如果存在就清空覆蓋,寫的權限打開
>> y.writelines("aaa\n") ##在一行中寫入aaa 再回車
>> y.writelines("bbbb\n") ##在一行中寫入bbbb 再回車
>> y.flush() ##保存
>> y.writelines("ccccc\n") ##在一行中寫入cccccc 再回車
>> y.close() ##關閉並保存
#####################################
寫一個cp程序
#!/usr/bin/python
#coding:utf-8
file1=raw_input("請輸入源文件:")
file2=raw_input("請輸入目標文件:")
x=open(file1)
y=open(file2,‘w‘)
for i in x: 循環一次取一行,包含結束符合 相當於循環一次,就readline一次
y.writelines(i) 寫一行包含\n 循環一次 ####綜合這個語句,取一行寫一行,再循環一次......
y.close()
x.close()
################################
函數
#!/usr/bin/python
#coding:utf-8
def jsp(): #定義函數
print 1+8
print 23
print 3
5
jsp() #調用函數
print ‘20
jsp() #調用函數
###################################
形式參數,實際參數,默認參數
#####################################
#!/usr/bin/python
#coding:utf-8
def jsp(x,y): #x,y為形式參數
print x+y
print xy
print x/y
jsp(3,5) #3,5為實際參數
jsp(8,3) #8,3為實際參數
#######################################
#!/usr/bin/python
#coding:utf-8
def jsp(x=3,y=2): #x=3,y=2為默認參數
print x+y
print x
y
print x/y
jsp(3,5) #實際參數和默認參數一起用
jsp()

######################################3
函數運用
思考:

>> import subprocess
>> subprocess.call(‘ping -c2 192.168.4.5 > /dev/null‘,shell=True)
1
則程序應該這麽寫
#!/usr/bin/python
#coding:utf-8
import subprocess
def myping(x):
tmp=subprocess.call(‘ping -c2 %s &> /dev/null‘ %x,shell=True)
if tmp==0:
print(‘%s is up‘ %x)
else:
print(‘%s is down‘ %x)
myping(‘192.168.4.5‘)
myping(‘172.121.205.38‘)
結果:
192.168.4.5 is down
172.121.205.38 is up

##################################
!/usr/bin/python
#coding:utf-8
import sys ##導入模塊
file1=sys.argv[1] ##相當於shell中的$1 腳本命令 第一個參數
file2=sys.argv[2] ##相當於shell中的$2 腳本命令 第二個參數
x=open(file1)
y=open(file2,‘w‘)
for i in x:
y.writelines(i)
y.close()
x.close()

################################
模塊 (/usr/lib64/python2.7/)
import 模塊名 ##導入模塊中所有函數
模塊名.函數() ##調用函數
函數名.變量

from 模塊名 import 函數 ###只導入模塊中的一個函數
from nb import star

import 模塊,模塊,模塊 ###導入多個模塊逗號隔開

>> import string
>> string.letters
‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ‘
>> string.letters+string.digits
‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789‘
>> x=string.letters+string.digits
>> import random
>> random.choice(x)
‘w‘
>> random.choice(x)
‘r‘
>> random.choice(x)
‘Y‘

####################################
隨機產生8位數密碼
#!/usr/bin/python
#coding:utf-8
import random,string ##導入string和random模塊
x=string.letters+string.digits ##產生所有字母和數字返回給變量x
passwd=‘‘ ##定義passwd變量為空
for i in range(8): ##循環八次
passwd+=random.choice(x) ##循環一次隨機產生一個數,然後累加起來
print passwd ###字符串用引號,變量不用引號

##############################
while,for循環
文件操作
函數
模塊

Python中while,for循環及文件操作,函數,模塊等操作