python鍜宻hell瀵规瘮

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python鍜宻hell瀵规瘮相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%97%b6%e9%97%b4' title='鏃堕棿'>鏃堕棿   浣嶇Щ杩愮畻   not   exp   鑾峰緱   鍗曡   lines   close   浣嶈繍绠楃   

python鍜宻hell閮芥槸鎴戜滑缁忓父浣跨敤鐨勮剼鏈瑷€锛屽钩鏃秔ython涓昏鐢ㄦ潵鍐欎竴浜涘皬鍨嬬殑浠诲姟锛宻hell鍒欏湪浣跨敤liunx绯荤粺閮ㄧ讲浠诲姟鐨勬椂鍊欑敤鐨勬瘮杈冨锛岀敱浜庝袱鑰呮湁涓€浜涚浉浼间箣澶勶紝鏃堕棿闀夸簡瀹规槗娣锋帀锛屾墍浠ヨ繖閲屼綔鏂囧涓€浜涘熀纭€鐨勮娉曢渶姹傛€荤粨銆佹瘮杈冦€佸綊绾筹紝浠ヤ负鍚庣敤銆?nbsp;

鍙橀噺

python瀹氫箟鍜屼娇鐢ㄥ彧闇€瑕佷娇鐢ㄥ彉閲忓悕锛屼緥濡?/p>

var=鈥渉ello鈥?print var
# hello
  • 1
  • 2
  • 3

shell瀹氫箟鍙橀噺鍙渶瑕佸彉閲忓悕锛岃€屼娇鐢ㄥ彉閲忕殑鍊煎垯闇€瑕佸姞$绗﹀彿锛屼緥濡?/p>

var="hello"
echo $var
# hello
  • 1
  • 2
  • 3

鏁扮粍鐨勫畾涔?/h2>

python涓病鏈夋暟缁勭殑姒傚康锛屼絾鏄湁list锛宼uple锛宒ict绛夊彲浠ヤ唬鏇垮叾鍔熻兘锛屽0鏄庝竴涓猯ist骞朵娇鐢ㄥ叾涓殑涓€涓厓绱犲涓?/p>

numbers=[1,2,3,4,5]
print numbers[0]
# 1
  • 1
  • 2
  • 3

shell涓彧鏈変竴缁存暟缁勶紝鍏跺0鏄庡拰浣跨敤鏂规硶濡備笅

numbers=(1 2 3 4 5)
print ${numbers[0]}
# 1
#杩斿洖鎵€鏈夌殑鍊肩敤
print ${numbers[*]}
# 1 2 3 4 5
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

娉ㄩ噴

python鍜宻hell閮藉彲浠ユ槸鐢?鍙锋坊鍔犲崟琛屾敞閲婏紝python鍙互浣跨敤鈥濃€欐敞閲娾€濃€欐坊鍔犲琛屾敞閲?/p>

閫昏緫鍒ゆ柇

鏉′欢鐨勫畾涔?/h3>

鍦╬ython涓紝闄や簡鎵€鏈夌殑绌哄€艰璁や负False澶栨墍鏈夌殑鍊奸兘琚В閲婁负鐪烼rue锛岃繖閲岀殑绌哄€煎寘鎷細None,0,鈥濃€?(),{},[]鍜孎alse,涓嬮潰鏄緥瀛?nbsp;
鎶€鏈垎浜浘鐗? title= 
鍏朵粬鐨勭敱姣旇緝杩愮畻绗﹁繘琛屾瘮杈冩敮鎸侊紝鍏朵腑姣旇緝鐗规畩鐨勬搷浣滅鏈?nbsp;
* x is y x鍜寉鏄悓涓€涓璞?nbsp;
* x is not y 
* x in y x鍦▂闆嗗悎涓?nbsp;
* x not in y

閫昏緫棰勭畻娉曠敤and,or,not.

鑰宻hell鐨勭悊蹇靛ぇ涓嶇浉鍚岋紝瀹冧娇鐢╰est鍛戒护鏉ヨ繘琛屾潯浠舵祴璇曪紝瀹冮€氬父鍐欐垚[ condition ]鐨勫舰寮忥紝娉ㄦ剰鏂规嫭鍙蜂袱杈归兘瑕佹湁绌烘牸锛屽畠鏀寔涓夌被娴嬭瘯锛?nbsp;
* 鏁板€兼瘮杈冿紝闇€瑕佺敤-eq,-gt鏉ヤ唬鏇?=锛?gt; 
* 瀛楃涓叉瘮杈冿紝鍙互鐢?锛岄渶瑕佺敤-n涔嬬被鐨勭煭鍛戒护锛屾瘮濡?n str鏉ュ垽瀹歴tr闀垮害鏄惁闈為浂 
* 鏂囦欢姣旇緝锛岄渶瑕佺敤鐭懡浠?渚嬪-e file鍒ゆ柇鏂囦欢鏄惁瀛樺湪

閫昏緫杩愮畻绗?amp;&锛寍|鏉ヨ繛鎺ヤ笉鍚岀殑[ condition ]

鍙﹀锛岄珮绾х殑鏁板琛ㄨ揪寮忔帹鑽愪娇鐢?( expression ))閫昏緫鍒ゅ畾锛屽彲浠ヤ娇鐢ㄨ嚜澧?+锛屼綅绉昏繍绠楃<<,浣嶈繍绠楃&锛岄€昏緫涓?amp;&绛夌瓑 
楂樼骇鐨勫瓧绗︿覆鍒ゅ畾浣跨敤[[ expression ]]閫昏緫鍒ゅ畾锛屽彲浠ョ敤绫讳技浜庢鍒欒〃杈惧紡濡俒[ $teststr== r* ]]鍒ゅ畾浠寮€澶寸殑瀛楃涓?nbsp;
shell鐨勬暟鍊艰繍绠楁帹鑽愪娇鐢?[expression]鐨勬柟寮忚绠楋紝娴偣鏁扮殑璁$畻浣跨敤echo 鈥渆xpression鈥潀 bc鐨勬柟寮忚幏鍙栬绠楀€?/p>

if else

python涓殑if else浣跨敤鏂瑰紡濡備笅锛屾潯浠惰鍙ュ悗鍔犲啋鍙凤紝骞朵笖浠ョ缉杩涘垝鍒嗚鍙ュ潡

if condition:
    statement
elif condition:
    statement
else:
    statement
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

shell涓垯涓嶅悓锛屽叾if else浣跨敤鏂瑰紡濡備笅,瀹冧娇鐢╰hen鍜宔lseif鎴杅i鏉ョ晫瀹氳鍙ュ潡锛岀悊璁轰笂涓嶉渶瑕佺缉杩?/p>

if [ condtion ]
then
 statement
elif [condtion]
then
 statement
else
 statement
fi
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

for寰幆

python鐨刦or寰幆浣跨敤濡備笅,浠嶇劧鐢ㄧ缉杩涘垝鍒嗚鍙ュ潡

for item in sequence:
    statement
  • 1
  • 2

shell涓璮or鐨勪娇鐢ㄦ柟寮忕被浼?/p>

for item in list
do
statement
done
  • 1
  • 2
  • 3
  • 4

鍏朵腑shell鍙杋tem鐨勫垎闅旂鍏ㄥ眬鍙橀噺涓篒FS锛坕nternal field separator锛夛紝鍏堕粯璁ゅ€间负绌烘牸锛屽埗琛ㄧ锛屾崲琛岀锛屾墍浠ュ鏋滈渶瑕佹寜琛屽鐞嗚€屽拷鐣ョ┖鏍肩殑璇濋渶瑕佹敼鍙業FS鐨勫€?nbsp;
鍙﹀shell杩樻敮鎸乧璇█褰㈠紡鐨刦or寰幆锛屽畠浣跨敤鍙屽皬鎷彿鏉ュ畬鎴愶紝褰㈠紡濡備笅

for (( i=1; i<=10;i++))
do
 statement
done
  • 1
  • 2
  • 3
  • 4

while寰幆

python鐨剋hile鍐欐硶濡備笅

while condition:
    statement
  • 1
  • 2

shell鐨剋hile鍐欐硶濡備笅

while [ conditon ]
do
 statement
done
  • 1
  • 2
  • 3
  • 4

寮傚父澶勭悊

python鐨勫紓甯稿鐞嗘柟寮?/p>

try:
    statement
except:
    statement
finally:
    finalStatement
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

shell涓病鏈夌浉搴旂殑寮傚父澶勭悊鏈哄埗锛屽彧鏈夋瘡鏉″懡鍚嶇殑閿欒杈撳嚭鎻忚堪绗?锛屽彲浠ラ€氳繃2>&1灏嗛敊璇緭鍑洪噸瀹氬悜鍒版爣鍑嗚緭鍑?/p>

鏂囦欢鎿嶄綔

python浣跨敤open鍑芥暟锛屽叾鏈夎鍐欐ā寮忔湁鍙r,鍙啓w,娣诲姞a

myfile=open("filename","r")
myfile.readlines()
myfile.close()
with open("finename","w") as myfile:
    myfile.write("line
")
  • 1
  • 2
  • 3
  • 4
  • 5

shell璇诲彇鏂囦欢鐨勫唴瀹逛娇鐢ㄥ懡浠ゆ潵璇诲彇锛岀劧鍚庝娇鐢ㄧ閬撴垨鑰呴噸瀹氬悜杩涜杈撳叆澶勭悊锛屽父鐢ㄧ殑鍛戒护鏈?nbsp;
1. cat filename 璇诲彇鏂囦欢鐨勫叏閮ㄥ唴瀹?nbsp;
2. tail -n 2 filename璇诲彇灏句袱琛?nbsp;
3. head -n 2 filename璇诲彇鍓嶄袱琛?/p>

涓€涓粡鍏哥殑闂锛岃鍙栦竴涓枃浠朵腑鐨勬瘡涓€琛屽仛澶勭悊锛屼娇鐢╬ython寰堢畝鍗?/p>

with open("filename","r") as myfile:
    for line in myfile.readlines():
        print line
  • 1
  • 2
  • 3

浣跨敤shell

cat filename|while read line
do
echo $line
done
#鎴栬€?IFS=鈥榎n鈥?for line in `cat  filename`
do
echo $line
done
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

鍑芥暟

python鐨勫嚱鏁板涓嬪舰寮?/p>

def functionname(params):
    鈥樺嚱鏁版枃妗b€?    statement
    return value
  • 1
  • 2
  • 3
  • 4

鍏朵腑鍑芥暟鏂囨。鍙互閫氳繃functionname.doc鑾峰緱 
shell涓嚱鏁板舰寮?/p>

function functionname {
    commands
    return value
}
  • 1
  • 2
  • 3
  • 4

浣跨敤鍙橀噺鑾峰彇杩斿洖鍊硷紝鍙傛暟閫氳繃鍑芥暟functionname param1 param2鐨勬柟寮忎紶閫掞紝閫氳繃$1,$2鐨勬柟寮忓彇鍑虹涓€绗簩涓弬鏁帮紝$#涓哄弬鏁版暟閲忥紝$*涓烘墍鏈夊弬鏁般€?/p>

















以上是关于python鍜宻hell瀵规瘮的主要内容,如果未能解决你的问题,请参考以下文章

闈炴浛浠e搧锛孧ongoDB涓嶮ySQL瀵规瘮鍒嗘瀽

Java7/8 涓?HashMap 鍜?ConcurrentHashMap婧愮爜瀵规瘮鍒嗘瀽

mysql涓璱nnodb鍜宮yisam瀵规瘮鍙婄储寮曞師鐞嗗尯鍒?杞?

锛堢瑪璁版湰鐢佃剳鍖呰瀵规瘮-绾稿VS鍚稿锛塎icrosoft VS Mac

PM璇讳骇鍝佸惎绀哄綍涓€涔?瀵规瘮寰蒋鏁忔嵎寮€鍙戞祦绋?鍘熶功PDF涓嬭浇)

Python-鍩轰簬socket鍜宻elect妯″潡瀹炵幇IO澶氳矾澶嶇敤