銆怭ython鍏ラ棬鏁欑▼銆� 瓒呰缁嗚瑙o紝1灏忔椂瀛︿細Python锛堜笉浼氭潵鎵炬垜!锛�

1銆佷负浠�涔堜娇鐢≒ython

鍋囪鎴戜滑鏈夎繖涔堜竴椤逛换鍔�:绠�鍗曟祴璇曞眬鍩熺綉涓殑鐢佃剳鏄惁杩為��.杩欎簺鐢佃剳鐨刬p鑼冨洿浠�192.168.0.101鍒�192.168.0.200.

鎬濊矾锛�鐢╯hell缂栫▼.(Linux閫氬父鏄痓ash鑰學indows鏄壒澶勭悊鑴氭湰).渚嬪,鍦╓indows涓婄敤ping ip 鐨勫懡浠や緷娆℃祴璇曞悇涓満鍣ㄥ苟寰楀埌鎺у埗鍙拌緭鍑�.鐢变簬ping閫氱殑鏃跺�欐帶鍒跺彴鏂囨湰閫氬父鏄�"Reply from ... " 鑰屼笉閫氱殑鏃跺�欐枃鏈槸"time out ... " ,鎵�浠�,鍦ㄧ粨鏋滀腑杩涜瀛楃涓叉煡鎵�,鍗冲彲鐭ラ亾璇ユ満鍣ㄦ槸鍚﹁繛閫�.

瀹炵幇:Java浠g爜濡備笅锛�

String cmd="cmd.exe ping ";

String ipprefix="192.168.10.";

int begin=101;

int end=200;

Process p=null;

for(int i=begin;i

聽 聽 p= Runtime.getRuntime().exec(cmd+i);

聽 聽 String line = null;

聽 聽 BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

聽 聽 while((line = reader.readLine()) != null)

聽 聽 {

聽 聽 聽 聽 //Handling line , may logs it.

聽 聽 }

聽 聽 reader.close();

聽 聽 p.destroy();

}

杩欐浠g爜杩愯寰楀緢濂�,闂鏄负浜嗚繍琛岃繖娈典唬鐮�,浣犺繕闇�瑕佸仛涓�浜涢澶栫殑宸ヤ綔.杩欎簺棰濆鐨勫伐浣滃寘鎷�

缂栧啓涓�涓被鏂囦欢

缂栧啓涓�涓猰ain鏂规硶

灏嗕箣缂栬瘧鎴愬瓧鑺備唬鐮�

鐢变簬瀛楄妭浠g爜涓嶈兘鐩存帴杩愯,浣犻渶瑕佸啀鍐欎釜灏忓皬鐨刡at鎴栬�卋ash鑴氭湰鏉ヨ繍琛�.

褰撶劧,鐢–/C++鍚屾牱鑳藉畬鎴愯繖椤瑰伐浣�.浣咰/C++涓嶆槸璺ㄥ钩鍙拌瑷�.鍦ㄨ繖涓冻澶熺畝鍗曠殑渚嬪瓙涓篃璁哥湅涓嶅嚭C/C++鍜孞ava瀹炵幇鐨勫尯鍒�,浣嗗湪涓�浜涙洿涓哄鏉傜殑鍦烘櫙,姣斿瑕佸皢杩為�氫笌鍚︾殑淇℃伅璁板綍鍒扮綉缁滄暟鎹簱.鐢变簬Linux鍜學indows鐨勭綉缁滄帴鍙e疄鐜版柟寮忎笉鍚�,浣犱笉寰椾笉鍐欎袱涓嚱鏁扮殑鐗堟湰.鐢↗ava灏辨病鏈夎繖鏍风殑椤捐檻.

鍚屾牱鐨勫伐浣滅敤Python瀹炵幇濡備笅锛�

import subprocess

cmd="cmd.exe"

begin=101

end=200

while begin

聽 聽 p=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,

聽 聽 聽 聽 聽 聽 聽 聽 聽 stdin=subprocess.PIPE,

聽 聽 聽 聽 聽 聽 聽 聽 聽 stderr=subprocess.PIPE)

聽 聽 p.stdin.write("ping 192.168.1."+str(begin)+"\n")

聽 聽 p.stdin.close()

聽 聽 p.wait()

聽 聽 print "execution result: %s"%p.stdout.read()

瀵规瘮Java,Python鐨勫疄鐜版洿涓虹畝娲�,浣犵紪鍐欑殑鏃堕棿鏇村揩.浣犱笉闇�瑕佸啓main鍑芥暟,骞朵笖杩欎釜绋嬪簭淇濆瓨涔嬪悗鍙互鐩存帴杩愯.鍙﹀,鍜孞ava涓�鏍�,Python涔熸槸璺ㄥ钩鍙扮殑.

鏈夌粡楠岀殑C/Java绋嬪簭鍛樺彲鑳戒細浜夎璇寸敤C/Java鍐欎細姣擯ython鍐欏緱蹇�.杩欎釜瑙傜偣瑙佷粊瑙佹櫤.鎴戠殑鎯虫硶鏄綋浣犲悓鏃舵帉鎻ava鍜孭ython涔嬪悗,浣犱細鍙戠幇鐢≒ython鍐欒繖绫荤▼搴忕殑閫熷害浼氭瘮Java蹇笂璁稿.渚嬪鎿嶄綔鏈湴鏂囦欢鏃朵綘浠呴渶瑕佷竴琛屼唬鐮佽�屼笉闇�瑕丣ava鐨勮澶氭祦鍖呰绫�.鍚勭璇█鏈夊叾澶╃劧鐨勯�傚悎鐨勫簲鐢ㄨ寖鍥�.鐢≒ython澶勭悊涓�浜涚畝鐭▼搴忕被浼间笌鎿嶄綔绯荤粺鐨勪氦浜掔紪绋嬪伐浣滄渶鐪佹椂鐪佸姏.

Python搴旂敤鍦哄悎

瓒冲绠�鍗曠殑浠诲姟,渚嬪涓�浜泂hell缂栫▼.濡傛灉浣犲枩娆㈢敤Python璁捐澶у瀷鍟嗕笟缃戠珯鎴栬�呰璁″鏉傜殑娓告垙.

2銆佸揩閫熷叆闂�

2.1 Hello world

瀹夎瀹孭ython涔嬪悗(鎴戞湰鏈虹殑鐗堟湰鏄�2.5.4),鎵撳紑IDLE(Python GUI) , 璇ョ▼搴忔槸Python璇█瑙i噴鍣�,浣犲啓鐨勮鍙ヨ兘澶熺珛鍗宠繍琛�.鎴戜滑鍐欎笅涓�鍙ヨ憲鍚嶇殑绋嬪簭璇彞:

print "Hello,world!"

骞舵寜鍥炶溅.浣犲氨鑳界湅鍒拌繖鍙ヨK&R寮曞叆鍒扮▼搴忎笘鐣岀殑鍚嶈█.

鍦ㄨВ閲婂櫒涓�夋嫨"File"--"New Window" 鎴栧揩鎹烽敭 Ctrl+N , 鎵撳紑涓�涓柊鐨勭紪杈戝櫒.鍐欎笅濡備笅璇彞:

print "Hello,world!"

raw_input("Press enter key to close this window...");

淇濆瓨涓篴.py鏂囦欢.鎸塅5,浣犲氨鍙互鐪嬪埌绋嬪簭鐨勮繍琛岀粨鏋滀簡.杩欐槸Python鐨勭浜岀杩愯鏂瑰紡.

鎵惧埌浣犱繚瀛樼殑a.py鏂囦欢,鍙屽嚮.涔熷彲浠ョ湅鍒扮▼搴忕粨鏋�.Python鐨勭▼搴忚兘澶熺洿鎺ヨ繍琛�,瀵规瘮Java,杩欐槸涓�涓紭鍔�.

2.2 鍥介檯鍖栨敮鎸�

鎴戜滑鎹竴绉嶆柟寮忔潵闂�欎笘鐣�.鏂板缓涓�涓紪杈戝櫒骞跺啓濡備笅浠g爜:

print "娆㈣繋鏉ュ埌濂ヨ繍涓浗!"

raw_input("Press enter key to close this window...");

鍦ㄤ綘淇濆瓨浠g爜鐨勬椂鍊�,Python浼氭彁绀轰綘鏄惁鏀瑰彉鏂囦欢鐨勫瓧绗﹂泦,缁撴灉濡備笅:

-*- coding: cp936 -*-

print "娆㈣繋鏉ュ埌濂ヨ繍涓浗!"

raw_input("Press enter key to close this window");

灏嗚瀛楃闆嗘敼涓烘垜浠洿鐔熸倝鐨勫舰寮�:

# -*- coding: GBK -*-

print "娆㈣繋鏉ュ埌濂ヨ繍涓浗!" # 浣跨敤涓枃鐨勪緥瀛�

raw_input("Press enter key to close this window");

绋嬪簭涓�鏍疯繍琛岃壇濂�.

2.3 鏂逛究鏄撶敤鐨勮绠楀櫒

鐢ㄥ井杞檮甯︾殑璁$畻鍣ㄦ潵璁℃暟瀹炲湪澶夯鐑︿簡.鎵撳紑Python瑙i噴鍣�,鐩存帴杩涜璁$畻:

a=100.0

b=201.1

c=2343

print (a+b+c)/c

2.4 瀛楃涓�,ASCII鍜孶NICODE

鍙互濡備笅鎵撳嵃鍑洪瀹氫箟杈撳嚭鏍煎紡鐨勫瓧绗︿覆:

print """

Usage: thingy [OPTIONS]

聽 聽 -h聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 Display this usage message

聽 聽 -H hostname聽 聽 聽 聽 聽 聽 聽 Hostname to connect to

"""

瀛楃涓叉槸鎬庝箞璁块棶鐨�?璇风湅杩欎釜渚嬪瓙:

word="abcdefg"

a=word[2]

print "a is: "+a

b=word[1:3]

print "b is: "+b # index 1 and 2 elements of word.

c=word[:2]

print "c is: "+c # index 0 and 1 elements of word.

d=word[0:]

print "d is: "+d # All elements of word.

e=word[:2]+word[2:]

print "e is: "+e # All elements of word.

f=word[-1]

print "f is: "+f # The last elements of word.

g=word[-4:-2]

print "g is: "+g # index 3 and 4 elements of word.

h=word[-2:]

print "h is: "+h # The last two elements.

i=word[:-2]

print "i is: "+i # Everything except the last two characters

l=len(word)

print "Length of word is: "+ str(l)

璇锋敞鎰廇SCII鍜孶NICODE瀛楃涓茬殑鍖哄埆:

print "Input your Chinese name:"

s=raw_input("Press enter to be continued...");

print "Your name is ... : " +s;

l=len(s)

print "Length of your Chinese name in asc codes is:"+str(l);

a=unicode(s,"GBK")

l=len(a)

print "I'm sorry we should use unicode char!Characters number of your Chinese \

name in unicode is:"+str(l);

2.5 浣跨敤List

绫讳技Java閲岀殑List,杩欐槸涓�绉嶆柟渚挎槗鐢ㄧ殑鏁版嵁绫诲瀷:

word=['a','b','c','d','e','f','g']

a=word[2]

print "a is: "+a

b=word[1:3]

print "b is: "

print b # index 1 and 2 elements of word.

c=word[:2]

print "c is: "

print c # index 0 and 1 elements of word.

d=word[0:]

print "d is: "

print d # All elements of word.

e=word[:2]+word[2:]

print "e is: "

print e # All elements of word.

f=word[-1]

print "f is: "

print f # The last elements of word.

g=word[-4:-2]

print "g is: "

print g # index 3 and 4 elements of word.

h=word[-2:]

print "h is: "

print h # The last two elements.

i=word[:-2]

print "i is: "

print i # Everything except the last two characters

l=len(word)

print "Length of word is: "+ str(l)

print "Adds new element..."

word.append('h')

print word

2.6 鏉′欢鍜屽惊鐜鍙�

# Multi-way decision

x=int(raw_input("Please enter an integer:"))

if x<0:

聽 聽 x=0

聽 聽 print "Negative changed to zero"

elif x==0:

聽 聽 print "Zero"

else:

聽 聽 print "More"

# Loops List

a = ['cat', 'window', 'defenestrate']

for x in a:

聽 聽 print x, len(x)

2.7 濡備綍瀹氫箟鍑芥暟

# Define and invoke function.

def sum(a,b):

聽 聽 return a+b

func = sum

r = func(5,6)

print r

# Defines function with default argument

def add(a,b=2):

聽 聽 return a+b

r=add(1)

print r

r=add(1,5)

print r

骞朵笖,浠嬬粛涓�涓柟渚垮ソ鐢ㄧ殑鍑芥暟:

# The range() function

a =range(5,10)

print a

a = range(-2,-7)

print a

a = range(-7,-2)

print a

a = range(-2,-11,-3) # The 3rd parameter stands for step

print a

2.8 鏂囦欢I/O

spath="D:/download/baa.txt"

f=open(spath,"w") # Opens file for writing.Creates this file doesn't exist.

f.write("First line 1.\n")

f.writelines("First line 2.")

f.close()

f=open(spath,"r") # Opens file for reading

for line in f:

聽 聽 print line

f.close()

2.9 寮傚父澶勭悊

s=raw_input("Input your age:")

if s =="":

聽 聽 raise Exception("Input must no be empty.")

try:

聽 聽 i=int(s)

except ValueError:

聽 聽 print "Could not convert data to an integer."

except:

聽 聽 print "Unknown exception!"

else: # It is useful for code that must be executed if the try clause does not raise an exception

聽 聽 print "You are %d" % i," years old"

finally: # Clean up action

聽 聽 print "Goodbye!"

2.10 绫诲拰缁ф壙

class Base:

聽 聽 def __init__(self):

聽 聽 聽 聽 self.data = []

聽 聽 def add(self, x):

聽 聽 聽 聽 self.data.append(x)

聽 聽 def addtwice(self, x):

聽 聽 聽 聽 self.add(x)

聽 聽 聽 聽 self.add(x)

# Child extends Base

class Child(Base):

聽 聽 def plus(self,a,b):

聽 聽 聽 聽 return a+b

oChild =Child()

oChild.add("str1")

print oChild.data

print oChild.plus(2,3)

2.11 鍖呮満鍒�

姣忎竴涓�.py鏂囦欢绉颁负涓�涓猰odule,module涔嬮棿鍙互浜掔浉瀵煎叆.璇峰弬鐪嬩互涓嬩緥瀛�:

# a.py

def add_func(a,b):

聽 聽 return a+b

# b.py

from a import add_func # Also can be : import a

print "Import add_func from module a"

print "Result of 1 plus 2 is: "

print add_func(1,2)聽 聽 # If using "import a" , then here should be "a.add_func"

module鍙互瀹氫箟鍦ㄥ寘閲岄潰.Python瀹氫箟鍖呯殑鏂瑰紡绋嶅井鏈夌偣鍙ゆ��,鍋囪鎴戜滑鏈変竴涓猵arent鏂囦欢澶�,璇ユ枃浠跺す鏈変竴涓猚hild瀛愭枃浠跺す.child涓湁涓�涓猰odule a.py . 濡備綍璁㏄ython鐭ラ亾杩欎釜鏂囦欢灞傛缁撴瀯?寰堢畝鍗�,姣忎釜鐩綍閮芥斁涓�涓悕涓篲init_.py 鐨勬枃浠�.璇ユ枃浠跺唴瀹瑰彲浠ヤ负绌�.杩欎釜灞傛缁撴瀯濡備笅鎵�绀�:

parent

聽 --__init_.py

聽 --child

聽 聽 -- __init_.py

聽 聽 --a.py

b.py

閭d箞Python濡備綍鎵惧埌鎴戜滑瀹氫箟鐨刴odule?鍦ㄦ爣鍑嗗寘sys涓�,path灞炴�ц褰曚簡Python鐨勫寘璺緞.浣犲彲浠ュ皢涔嬫墦鍗板嚭鏉�:

import sys

print sys.path

閫氬父鎴戜滑鍙互灏唌odule鐨勫寘璺緞鏀惧埌鐜鍙橀噺PYTHONPATH涓�,璇ョ幆澧冨彉閲忎細鑷姩娣诲姞鍒皊ys.path灞炴��.鍙︿竴绉嶆柟渚跨殑鏂规硶鏄紪绋嬩腑鐩存帴鎸囧畾鎴戜滑鐨刴odule璺緞鍒皊ys.path 涓�:

import sys

sys.path.append('D:\\download')

from parent.child.a import add_func

print sys.path

print "Import add_func from module a"

print "Result of 1 plus 2 is: "

print add_func(1,2)

鎬荤粨

浣犱細鍙戠幇杩欎釜鏁欑▼鐩稿綋鐨勭畝鍗�.璁稿Python鐗规�у湪浠g爜涓互闅愬惈鏂瑰紡鎻愬嚭,杩欎簺鐗规�у寘鎷�:Python涓嶉渶瑕佹樉寮忓0鏄庢暟鎹被鍨�,鍏抽敭瀛楄鏄�,瀛楃涓插嚱鏁扮殑瑙i噴绛夌瓑.鎴戣涓轰竴涓啛缁冪殑绋嬪簭鍛樺簲璇ュ杩欎簺姒傚康鐩稿綋浜嗚В,杩欐牱鍦ㄤ綘鎸ゅ嚭瀹濊吹鐨勪竴灏忔椂闃呰杩欑瘒鐭煭鐨勬暀绋嬩箣鍚�,浣犺兘澶熼�氳繃宸叉湁鐭ヨ瘑鐨勮縼绉荤被姣斿敖蹇啛鎮塒ython,鐒跺悗灏藉揩鑳界敤瀹冨紑濮嬬紪绋�.

褰撶劧,1灏忔椂瀛︿細Python棰囨湁鍝椾紬鍙栧疇涔嬪珜.纭垏鐨勮,缂栫▼璇█鍖呮嫭璇硶鍜屾爣鍑嗗簱.璇硶鐩稿綋浜庢鏈嫑寮�,鑰屾爣鍑嗗簱搴旂敤瀹炶返缁忛獙鍒欑被浼间簬鍐呭姛,闇�瑕侀暱鏈熼敾鐐�.Python瀛︿範浜咼ava鐨勯暱澶�,鎻愪緵浜嗗ぇ閲忔瀬鏂逛究鏄撶敤鐨勬爣鍑嗗簱渚涚▼搴忓憳"鎷挎潵涓讳箟".(杩欎篃鏄疨ython鎴愬姛鐨勫師鍥�),鍦ㄥ紑绡囨垜浠湅鍒颁簡Python濡備綍璋冪敤Windows cmd鐨勪緥瀛�,浠ュ悗鎴戜細灏介噺鍐欎笂鍚勬爣鍑嗗簱鐨勭敤娉曞拰涓�浜涘簲鐢ㄦ妧宸�,璁╁ぇ瀹剁湡姝f帉鎻ython.

你可能感兴趣的:(銆怭ython鍏ラ棬鏁欑▼銆� 瓒呰缁嗚瑙o紝1灏忔椂瀛︿細Python锛堜笉浼氭潵鎵炬垜!锛�)