This content I original, refused commercial use and others forwarded, severe crackdown has the above behavior, found after the legal responsibility.
Call Variables in print
>> print "Tom is%d,jerry is%d"% (i,j) #%d call%i Second%d call%j
Tom is 20,jerry is 30
#################################################
Break usage interrupts all loops
1.
#!/usr/bin/python
#coding: Utf-8
#提示输入用户名
#直到输入tom为止
While 1:
User=raw_input ("Please enter user name:")
If user== "Tom":
Break
2.
#!/usr/bin/python
#coding: Utf-8
Import Random
Num=random.randint (1,100)
While 1:
User=int (raw_input (' Please enter a number '))
if user = = num:
print ' You guessed it right '
Break
elif User > num:
print ' you guessed big. '
Else
print ' You guessed small '
#################################################
Shell for Loop do-DONE
For I in 1 8 5 66
For i in {1..100}
For I incat file
For I inls /etc/
Python for Loop
For I in string:
###########################
>> x= ' Hello the '
>> Len (x)
9
>> for I in x: # #for循环几次跟下标有关, remove the contents of the underlying variable
... print I
...
H
E
L
L
O
T
H
E
For I in Dictionary:
#############################
>> x={' phone ': one, ' age ': A, ' name ': ' Tom '}
>> for I in x:
... print I
...
Phone
Age
Name
############################
For I in list:
>> x=[11,88, ' Tom ']
>> for I in x:
... print I
...
11
88
Tom
####################################
For I in Xrange (8): #执行8次 starting from 0 or for I in range (8):
Rabbit List
x=[0,1]
For I in Xrange (8):
Tmp=x[-1]+x[-2]
X.append (TMP)
Print X
##################################
Even sums
>> Range (1,10,2)
[1, 3, 5, 7, 9]
>> Range (2,10,2)
[2, 4, 6, 8]
Quick Build
>> [Ten for I in range (5)] # # #把常量10放入for循环里面 Print rang (5) loop five times
[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 Action on files
Open (' File name ', permissions) default to read permissions
X=open ('/etc/hosts ') # #读权限打开, the file exists on the error. Open the contents of the file to the variable x
X.readline () #默认读一行
X.read () #读所有
X.seek (0) #返回第一行
X.readline (6) #读6个字节, one letter at a byte
X.close () #关闭文件
For i in x: #循环一次读一行 I take the line contents of the file Wrap
>> y=open ('/root/new.txt ', ' W ') # #如果不存在就新建, if present, empty overwrite, write permission open
>> y.writelines ("aaa\n") # #在一行中写入aaa and return
>> y.writelines ("bbbb\n") # #在一行中写入bbbb and return
>> Y.flush () # #保存
>> y.writelines ("ccccc\n") # #在一行中写入cccccc and return
>> y.close () # #关闭并保存
#####################################
Write a CP program
#!/usr/bin/python
#coding: Utf-8
File1=raw_input ("Please enter source file:")
File2=raw_input ("Please enter target file:")
X=open (File1)
Y=open (File2, ' W ')
For I in x: The loop takes one row at a time, contains the end of the equivalent loop once, ReadLine once
Y.writelines (i) write a line containing \ n Loop once # # # #综合这个语句, take one line to write a line, and then cycle once ...
Y.close ()
X.close ()
################################
Function
#!/usr/bin/python
#coding: Utf-8
Def JSP (): #定义函数
Print 1+8
Print 23
Print 35
JSP () #调用函数
print ''20
JSP () #调用函数
###################################
Formal parameters, actual parameters, default parameters
#####################################
#!/usr/bin/python
#coding: Utf-8
def jsp (x, y): #x, Y is the formal parameter
Print X+y
Print Xy
Print x/y
JSP (3,5) #3, 5 is the actual parameter
JSP (8,3) #8, 3 is the actual parameter
#######################################
#!/usr/bin/python
#coding: Utf-8
def JSP (x=3,y=2): #x =3,y=2 as default parameter
Print X+y
Print xY
Print x/y
JSP (3,5) #实际参数和默认参数一起用
JSP ()
##################################### #3
function application
Thinking:
>> Import subprocess
>> subprocess.call (' ping-c2 192.168.4.5 >/dev/null ', shell=true)
1
The program should write this
#!/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 ')
Results:
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中的 The first parameter of the script command
FILE2=SYS.ARGV[2] # #相当于shell中的 The second argument of the $ $ script command
X=open (File1)
Y=open (File2, ' W ')
For I in x:
Y.writelines (i)
Y.close ()
X.close ()
################################
Module (/usr/lib64/python2.7/)
Import Module Name # #导入模块中所有函数
Module name. function () # #调用函数
The name of the function. Variable
From Module name Import function # # #只导入模块中的一个函数
From NB Import Star
Import module, module, module # # #导入多个模块逗号隔开
>> 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
####################################
Randomly generate 8-digit passwords
#!/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) # #循环一次随机产生一个数, then add up
Print passwd # # #字符串用引号, variable without quotation marks
##############################
While,for Cycle
File operations
Function
Module
While,for loops and file operations, functions, modules, etc. in Python