16个保姆级 Python 菜鸟教程:从小白到高手,轻松入门!
鍢匡紝澶у濂斤紒鎴戞槸浣犱滑鐨勬妧鏈ぇ鐗涘皬鍔╂墜锛屼粖澶╁皢甯︿綘浠蛋杩汸ython鐨勪笘鐣岋紝缁欎綘浠甫鏉ヤ竴浠姐16涓繚濮嗙骇 Python 鑿滈笩鏁欑▼銆嬨傛棤璁轰綘鏄垰鎺ヨЕ缂栫▼鐨勮彍楦燂紝杩樻槸鍑嗗璺宠穬鍒癙ython鐨勯珮鎵嬭鍒楋紝杩欑瘒鏁欑▼缁濆鑳借浣犺交鏉捐捣姝ワ紝涓嶅啀鎯ф曢偅浜涚湅浼煎鏉傜殑浠g爜锛佸噯澶囧ソ浜嗗悧锛熷揩鎷胯捣浣犵殑绗旇鏈紝涓璧锋潵鍐欎唬鐮佸惂锛
unsetunset浠涔堟槸銆16涓繚濮嗙骇 Python 鑿滈笩鏁欑▼銆嬶紵unsetunset
鈥16涓繚濮嗙骇 Python 鑿滈笩鏁欑▼鈥濆叾瀹炴槸涓洪偅浜涘畬鍏ㄦ病鏈夌紪绋嬬粡楠岀殑鏈嬪弸浠噺韬畾鍒剁殑涓濂楀涔犺祫鏂欍傛垜浠皢閫氳繃16涓畝鍗曘佸疄鐢ㄧ殑鏁欑▼锛岄愭甯︿綘浠庢渶鍩虹鐨勬蹇靛璧凤紝涓鐩村埌鑳藉鐙珛鍐欏嚭鏈夎叮鐨凱ython绋嬪簭銆傛瘡涓暀绋嬮兘閰嶆湁璇︾粏鐨勭ず渚嬩唬鐮侊紝甯姪浣犵悊瑙ython鐨勬牳蹇冩蹇点
鐗圭偣锛
- 浠庨浂寮濮嬶細姣忎釜鏁欑▼閮介潪甯稿熀纭锛岄傚悎瀹屽叏娌℃湁缂栫▼缁忛獙鐨勪汉銆
- 绠鍗曟槗鎳傦細灏介噺閬垮厤澶嶆潅鐨勬湳璇紝鐢ㄦ渶閫氫織鐨勮瑷瑙i噴姣忎釜姒傚康銆
- 浠g爜绀轰緥锛氭瘡涓蹇靛悗閮芥湁瀹炴垬浠g爜锛屽府鍔╀綘绔嬮┈涓婃墜銆
- 杞绘澗鎰夊揩锛氶氳繃骞介粯椋庤叮鐨勮姘旓紝璁╀綘鍦ㄥ涔犺繃绋嬩腑涓嶈寰楁灟鐕ャ
unsetunset濡備綍瀹夎骞跺紑濮嬩娇鐢 Python锛焨nsetunset
棣栧厛锛岃繘鍏ython鐨勪笘鐣岋紝浣犻渶瑕佸仛涓や欢浜嬶細
- 瀹夎 Python
- 閫夋嫨涓涓ソ鐢ㄧ殑缂栬緫鍣/IDE
瀹夎 Python
- 璁块棶 Python 瀹樻柟缃戠珯锛歨ttps://www.python.org/downloads/
- 鏍规嵁浣犵殑鎿嶄綔绯荤粺閫夋嫨鍚堥傜殑鐗堟湰杩涜涓嬭浇銆
- 瀹夎鏃讹紝纭繚鍕鹃夆淎dd Python to PATH鈥濋夐」锛岃繖鏍峰彲浠ュ湪鍛戒护琛岀洿鎺ヤ娇鐢 Python銆
閫夋嫨缂栬緫鍣/IDE
- VS Code锛氫竴涓交閲忕骇浣嗗姛鑳藉己澶х殑缂栬緫鍣紝鏀寔Python寮鍙戙
- PyCharm锛氬鏋滀綘鍑嗗鎴愪负Python澶у笀锛孭yCharm鏄竴涓潪甯稿己澶х殑IDE锛屼笓涓篜ython璁捐銆
- Jupyter Notebook锛氶傚悎鏁版嵁绉戝鍜屾満鍣ㄥ涔狅紝閫傚悎鐢ㄦ潵杩愯Python浠g爜鍧椼
unsetunset鏍稿績姒傚康璁茶Вunsetunset
1. 鍙橀噺锛歅ython涓殑鈥滃鍣ㄢ
灏卞儚浣犲閲岀殑鐩掑瓙鍙互瑁呬笢瑗夸竴鏍凤紝Python涓殑鍙橀噺涔熸槸鐢ㄦ潵瀛樻斁鏁版嵁鐨勨滃鍣ㄢ濄備綘鍙互鎶婁换浣曟暟鎹瓨杩涗竴涓彉閲忛噷锛岀劧鍚庡湪绋嬪簭涓殢鏃朵娇鐢ㄥ畠銆
浠g爜绀轰緥锛
x = 5
y = "Hello, Python!"
print(x)
print(y)
瑙i噴锛
x = 5
锛氳繖閲屾垜浠妸鏁板瓧5瀛樿繘浜嗗彉閲忋x
y = "Hello, Python!"
锛氳繖閲屾垜浠妸瀛楃涓插瓨杩涗簡鍙橀噺銆y
print(x)
锛氭墦鍗板嚭鐨勫硷紝鍗5銆x
print(y)
锛氭墦鍗板嚭鐨勫硷紝鍗"Hello, Python!"銆y
2. 鏁版嵁绫诲瀷锛氫笉鍚岀殑鈥滅洅瀛愨濊涓嶅悓鐨勪笢瑗
鍦≒ython涓紝鏁版嵁鏈変笉鍚岀殑绫诲瀷锛堟瘮濡傛暣鏁般佹诞鐐规暟銆佸瓧绗︿覆绛夛級锛屽氨鍍忎笉鍚岀殑鐩掑瓙閫傚悎瑁呬笉鍚岀殑涓滆タ銆
甯歌鐨勬暟鎹被鍨嬫湁锛
- 鏁存暟锛坕nt锛夛細鏁板瓧锛屾病鏈夊皬鏁扮偣銆
- 娴偣鏁帮紙float锛夛細鏁板瓧锛屾湁灏忔暟鐐广
- 瀛楃涓诧紙str锛夛細涓涓插瓧绗︼紝鍙互鐢ㄥ崟寮曞彿鎴栧弻寮曞彿鎷捣鏉ャ
浠g爜绀轰緥锛
a = 10 # 鏁存暟
b = 3.14 # 娴偣鏁
c = "Hello" # 瀛楃涓
print(type(a))
print(type(b))
print(type(c))
瑙i噴锛
type()
鍑芥暟杩斿洖鍙橀噺鐨勬暟鎹被鍨嬨a
鏄竴涓暣鏁帮紝鏄诞鐐规暟锛屾槸瀛楃涓层b
c
3.鏉′欢鍒ゆ柇锛氳绋嬪簭鈥滃仛鍐冲畾鈥
濡傛灉浣犳槸涓涓父鍋氬喅瀹氱殑浜猴紝閭d綘浼氬枩娆ython鐨勬潯浠跺垽鏂紒瀹冭绋嬪簭鏍规嵁涓嶅悓鐨勬潯浠垛滈夋嫨鈥濆仛涓嶅悓鐨勪簨鎯呫
浠g爜绀轰緥锛
age = 18
if age >= 18:
print("浣犳槸鎴愬勾浜猴紝鍙互寮濮嬪涔燩ython鍟︼紒")
else:
print("浣犺繕鏈垚骞达紝鍙互鍏堢帺鐜╃紪绋嬪皬娓告垙锛")
瑙i噴锛
if
璇彞鐢ㄤ簬鍒ゆ柇鏉′欢銆- 濡傛灉涓虹湡锛屾墦鍗扳滀綘鏄垚骞翠汉鈥濄
age >= 18
- 濡傛灉涓哄亣锛屾墦鍗扳滀綘杩樻湭鎴愬勾鈥濄
unsetunset瀹炴垬绀轰緥锛氬啓涓涓畝鍗曠殑鈥滅寽鏁板瓧鈥濇父鎴弖nsetunset
濂戒簡锛屾垜浠浜嗕竴浜涘熀纭姒傚康锛岀幇鍦ㄦ潵涓涓湁瓒g殑瀹炴垬鈥斺斺滅寽鏁板瓧鈥濇父鎴忋傛垜浠璁╃數鑴戦殢鏈洪変竴涓暟瀛楋紝鐒跺悗浣犳潵鐚滐紝鐩村埌鐚滃涓烘銆
瀹屾暣浠g爜锛
import random
def guess_number():
print("娆㈣繋鏉ュ埌鐚滄暟瀛楁父鎴忥紒")
print("鎴戝凡缁忔兂濂戒簡涓涓1鍒100涔嬮棿鐨勬暟瀛楋紝浣犳潵鐚滃惂锛")
number = random.randint(1, 100) # 鐢佃剳闅忔満閫変竴涓暟瀛
guess = None
while guess != number:
guess = int(input("璇疯緭鍏ヤ綘鐨勭寽娴嬶細"))
if guess < number:
print("澶皬浜嗭紒鍐嶈瘯涓娆°")
elif guess > number:
print("澶ぇ浜嗭紒鍐嶈瘯涓娆°")
else:
print("鎭枩浣狅紝鐚滃浜嗭紒")
guess_number()
瑙i噴锛
random.randint(1, 100)
鐢熸垚涓涓1鍒100涔嬮棿鐨勯殢鏈烘暣鏁般input()
鑾峰彇鐢ㄦ埛杈撳叆銆while
寰幆浼氫笉鏂繍琛岋紝鐩村埌鐢ㄦ埛鐚滃鏁板瓧銆
unsetunset瀹炵敤灏忚创澹玼nsetunset
- 鍠勭敤娉ㄩ噴锛氬湪浠g爜涓娇鐢ㄦ潵娣诲姞娉ㄩ噴锛岃繖鏍疯嚜宸卞拰浠栦汉闃呰浠g爜鏃惰兘鏇村ソ鍦扮悊瑙c
#
- 澶氬啓浠g爜锛屽缁冧範锛氱紪绋嬪氨鏄粌涔犲嚭鏉ョ殑锛屽鍋氱粌涔狅紝浣犱細瓒婃潵瓒婄啛缁冦
- Google灏辨槸浣犵殑濂芥湅鍙嬶細閬囧埌闂鏃讹紝Google涓涓嬶紝鍑犱箮鎬昏兘鎵惧埌瑙e喅鏂规銆
- 涓嶈瀹虫曞嚭閿欙細鍑洪敊鏄涔犵殑蹇呯粡涔嬭矾锛岄敊璇绘槸鑳借浣犲鍒版柊涓滆タ銆
灏辫繖鏍峰暒锛佸笇鏈涜繖绡囨暀绋嬭浣犲Python缂栫▼鏈変簡涓涓交鏉炬剦蹇殑鍏ラ棬浣撻獙銆傝浣忥紝缂栫▼灏卞儚瀛﹀紑杞︼紝鍒氬紑濮嬫湁鐐规檿锛屼絾澶氱粌涔犲悗锛屼綘浼氳寰楄秺鏉ヨ秺椤烘墜锛佺浣犲涔犳剦蹇紝鏈熷緟浣犳垚涓篜ython澶х锛侌煔