Koj puas xav kawm paub program? Kawm paub computer programming tuaj yeem nyuaj, thiab koj yuav xav txog kev kawm qee yam. Qhov no tej zaum yuav muaj tseeb rau qee cov lus programming, tab sis muaj ntau yam uas tsuas yog siv ib hnub lossis ob hnub kom nkag siab txog cov hauv paus. Python yog ib yam ntawm cov lus. Koj tuaj yeem ua cov haujlwm Python yooj yim hauv ob peb feeb xwb. Saib Kauj Ruam 1 hauv qab no kom paub seb yuav ua li cas.
Kauj ruam
Ntu 1 ntawm 5: Txhim Kho Python (Windows)
Kauj Ruam 1. Rub tawm Python rau Windows system
Tus neeg txhais lus Windows Python tuaj yeem rub tawm dawb los ntawm Python site. Nco ntsoov rub tawm qhov tseeb version rau koj lub operating system.
- Koj yuav tsum rub tawm qhov hloov tshiab kawg muaj, uas yog 3.4 thaum lub sijhawm sau ntawv no.
- OS X thiab Linux tau teeb tsa ua ntej nrog Python. Koj yuav tsis xav tau nruab ib qho Python cuam tshuam txog software, tab sis koj tuaj yeem teeb tsa cov ntawv sau.
- Feem ntau ntawm Linux kev faib tawm thiab OS X tseem siv Python 2.x. Muaj qee qhov sib txawv me me ntawm cov ntawv 2 & 3, tab sis qhov kev hloov pauv tseem ceeb tshaj plaws yog nyob hauv kab ntawv "luam tawm". Txhawm rau nruab qhov hloov tshiab kawg ntawm Python ntawm OS X lossis Linux, koj tuaj yeem rub tawm cov ntawv los ntawm Python lub vev xaib.
Kauj Ruam 2. Nruab tus neeg txhais lus Python
Cov neeg siv feem ntau tuaj yeem teeb tsa tus neeg txhais lus yam tsis hloov chaw. Koj tuaj yeem ua ke Python rau hauv Command Prompt los ntawm kev ua kom muaj qhov kev xaiv kawg hauv cov npe ntawm cov qauv muaj.
Kauj Ruam 3. Txhim kho cov ntawv sau
Thaum koj tuaj yeem tsim cov program Python los ntawm Notepad lossis TextEdit, nws yooj yim dua los nyeem thiab sau cov cai siv cov ntawv sau tshwj xeeb. Muaj ntau yam kev kho dawb uas koj tuaj yeem siv, xws li Notepad ++ (Windows), TextWrangler (Mac), lossis jEdit (Txhua lub kaw lus).
Kauj Ruam 4. Kuaj qhov kev teeb tsa
Qhib Command Prompt (Windows) los ntawm Terminal (Mac/Linux) thiab ntaus nab hab sej. Nab hab sej yuav raug thauj khoom thiab tus lej version yuav tshwm. Koj yuav raug coj mus rau tus neeg txhais lus Python hais kom sai, tshwm raws li >>>.
Ntaus cov ntawv ("Nyob Zoo, Ntiaj Teb!") Thiab nias Enter. Koj yuav pom cov ntawv pom hauv qab kab lus Python
Ntu 2 ntawm 5: Kawm Lub Tswv Yim Yooj Yim
Kauj Ruam 1. Nkag siab tias Python tsis tas yuav muab tso ua ke
Python yog hom lus txhais lus, txhais tau tias koj tuaj yeem khiav cov haujlwm sai li sai tau thaum koj hloov pauv cov ntaub ntawv. Qhov no ua rau cov txheej txheem rov ua dua, rov kho dua, thiab daws teeb meem cov haujlwm tau nrawm dua li ntau yam lus.
Nab hab sej yog ib hom lus yooj yim kawm, thiab koj tuaj yeem ua cov haujlwm yooj yim hauv ob peb feeb xwb
Kauj Ruam 2. Fiddle nrog tus neeg txhais lus
Koj tuaj yeem siv tus neeg txhais lus los ntsuas tus lej yam tsis tau ntxiv ua ntej rau hauv txoj haujlwm. Nov yog qhov zoo rau kev kawm paub cov lus txib tshwj xeeb ua haujlwm li cas, lossis sau cov phiaj xwm pov tseg.
Kauj Ruam 3. Kawm seb Python ua haujlwm li cas cov khoom thiab qhov txawv
Nab hab sej yog hom lus-hais lus, uas txhais tau tias txhua yam hauv qhov kev zov me nyuam raug kho raws li cov khoom. Qhov no txhais tau tias koj tsis tas yuav tshaj tawm cov kev hloov pauv thaum pib ntawm txoj haujlwm (koj tuaj yeem ua qhov no txhua lub sijhawm), thiab koj tsis tas yuav hais qhia hom kev hloov pauv (tus lej, cov hlua, thiab lwm yam).
Ntu 3 ntawm 5: Siv Tus Python Tus Neeg Txhais Lus Ua Tus Lej
Ua qee qhov kev suav ua haujlwm yooj yim yuav pab koj paub txog Python syntax thiab yuav ua li cas thiaj lis tau cov lej thiab cov hlua.
Kauj Ruam 1. Khiav tus neeg txhais lus
Qhib Command Prompt lossis Terminal. Ntaus nab hab sej ntawm qhov hais kom sai thiab nias Enter. Qhov no yuav thauj tus neeg txhais lus Python thiab koj yuav raug coj mus rau Python hais kom sai (>>>).
Yog tias koj tsis ua ke Python hauv qhov hais kom ua sai sai, koj yuav tsum tau mus rau Python nplaub tshev kom khiav tus neeg txhais lus
Kauj Ruam 2. Ua cov lej yooj yim
Koj tuaj yeem siv Python los ua cov lej yooj yim. Saib lub npov hauv qab no rau qee qhov piv txwv ntawm kev siv lub laij lej ua haujlwm li cas. Nco tseg: # yog cov lus hais hauv Python code, thiab lawv tsis ua tiav los ntawm tus neeg txhais lus.
>> 3 + 7 10 >>> 100 - 10*3 70 >>> (100 - 10*3) / 2 # Division yuav ib txwm xa rov qab tus lej ntab ntab (zauv) 35.0 >>> (100 - 10*3)) // 2 # Kev sib faib sib faib sib faib (ob qhov sib luag) yuav muab cov zauv pov tseg 35 >>> 23 % 4 # Qhov no yuav suav qhov faib faib seem 3 >>> 17.53 * 2.67 / 4.1 11.41587804878049
Kauj Ruam 3. Xam qhov qeb duas
Koj tuaj yeem siv tus neeg teb xov tooj ** los qhia tus neeg nthuav tawm. Nab hab sej tuaj yeem suav tau lub zog loj. Saib lub npov hauv qab no ua piv txwv.
>> 7 ** 2 # 7 sib npaug 49 >>> 5 ** 7 # 5 rau lub zog ntawm 7 78125
Kauj Ruam 4. Tsim thiab tswj cov kev hloov pauv
Koj tuaj yeem muab cov kev hloov pauv hauv Python los ua algebra yooj yim. Nov yog cov lus qhia zoo kom paub yuav ua li cas hloov pauv hauv Python cov haujlwm. Variables tau teev tseg siv = kos npe. Saib lub npov hauv qab no ua piv txwv.
>> a = 5 >>> b = 4 >>> a * b 20 >>> 20 * a // b 25 >>> b ** 2 16 >>> dav = 10 # Qhov txawv txav tuaj yeem yog ib txoj hlua> >> qhov siab = 5 >>> dav * siab 50
Kauj Ruam 5. Kaw tus neeg txhais lus
Thaum koj ua tiav siv tus neeg txhais lus, koj tuaj yeem kaw nws thiab rov qab mus rau qhov hais kom ua sai los ntawm nias Ctrl+Z (Windows) lossis Ctrl+D (Linux/Mac) thiab tom qab ntawd nyem Enter. Koj tseem tuaj yeem ntaus tawm () thiab nias Enter.
Ntu 4 ntawm 5: Tsim Thawj Txoj Haujlwm
Kauj Ruam 1. Qhib cov ntawv sau
Koj tuaj yeem los ntawm kev tsim cov phiaj xwm sim uas yuav ua rau koj paub txog cov hauv paus ntawm kev tsim thiab txuag cov haujlwm, tom qab ntawd khiav lawv los ntawm tus neeg txhais lus. Qhov no tseem yuav pab koj kuaj xyuas tias tus neeg txhais lus tau teeb tsa raug.
Kauj Ruam 2. Tsim kab lus "luam tawm"
"Luam tawm" yog ib qho ntawm cov haujlwm Python yooj yim siv los tso saib cov ntaub ntawv hauv lub davhlau ya nyob twg thaum lub sijhawm ua haujlwm. Nco tseg: "luam tawm" yog ib qho kev hloov pauv loj tshaj plaws los ntawm Python 2 txog Python 3. Hauv Python 2, koj tsuas yog yuav tsum sau "print" ua raws li qhov koj xav tso tawm. Hauv Python 3, "luam ntawv" tau dhau los ua haujlwm, yog li koj yuav tsum ntaus "print ()", thiab sau yam koj xav tau hauv kab lus.
Kauj Ruam 3. Ntxiv nqe lus
Ib qho ntawm txoj hauv kev feem ntau los ntsuas cov lus programming yog kom pom cov ntawv "Nyob Zoo, Ntiaj Teb!" Muab cov ntawv no sau rau hauv kab ntawv "luam tawm ()", suav nrog cov lus hais:
print ("Nyob zoo, Ntiaj Teb!")
Tsis zoo li feem ntau lwm yam lus, koj tsis tas yuav tshaj tawm txoj kab nrog;. Koj kuj tsis tas yuav siv cov kab zauv ({}) los sawv cev rau cov thaiv. Hloov chaw, qhov kev nkag mus yuav qhia qhov uas suav nrog hauv qhov thaiv
Kauj Ruam 4. Txuag cov ntawv
Nyem rau File menu hauv kab ntawv kho kom raug thiab xaiv Txuag Raws. Hauv cov ntawv qhia zaub mov nco-down hauv qab lub npe lub npe, xaiv hom ntaub ntawv Python. Yog tias koj siv Notepad (tab sis tsis pom zoo), xaiv "Txhua Cov Ntaub Ntawv" thiab tom qab ntawd ntxiv "py" mus rau qhov kawg ntawm lub npe ntawv.
- Nco ntsoov khaws cov ntaub ntawv mus rau qhov chaw yooj yim nkag tau, raws li koj yuav tau taw qhia nws ntawm qhov hais kom ua sai.
- Txog qhov piv txwv no, khaws cov ntawv ua "hello.py".
Kauj Ruam 5. Khiav qhov program
Qhib Command Prompt lossis Terminal thiab mus rau qhov chaw uas koj tau khaws cov ntaub ntawv. Thaum muaj, khiav cov ntawv los ntawm kev sau hello.py thiab Enter. Koj yuav pom cov ntawv pom hauv qab qhov hais kom ua.
Nyob ntawm seb koj teeb tsa Python li cas, koj yuav xav tau ntaus nab hab sej hello.py los ua haujlwm
Kauj Ruam 6. Kuaj qhov program nquag
Ib qho zoo ntawm Python yog tias koj tuaj yeem sim cov haujlwm tshiab tam sim ntawd. Lwm qhov zoo dua yog tias koj qhov kev hais kom ua sai thiab tus kws kho mob tau qhib. Tom qab txuag kev hloov pauv hauv tus kws kho mob, koj tuaj yeem ua haujlwm ncaj qha los ntawm kab hais kom ua, ua kom nws nrawm rau kev sim hloov pauv.
Ntu 5 ntawm 5: Txhim Kho Cov Txheej Txheem Zoo
Kauj Ruam 1. Kev sim nrog cov lus qhia tswj cov dej ntws yooj yim
Cov lus hais txog kev tswj ntws yog qhov muaj txiaj ntsig zoo rau kev tswj hwm qhov haujlwm ua haujlwm hauv qee yam xwm txheej. Cov nqe lus no yog qhov tseem ceeb ntawm Python programming, yog li koj tuaj yeem tsim cov haujlwm uas ua ntau yam, nyob ntawm kev nkag thiab cov xwm txheej. Cov nqe lus thaum pib yog qhov pib zoo rau kev kawm. Hauv qhov piv txwv no, koj siv nqe lus ib ntus los xam Fibonacci series txog 100:
# Txhua tus lej hauv Fibonacci sib lawv liag yog # cov lej ntawm ob tus lej dhau los a, b = 0, 1 thaum b <100: luam tawm (b, kawg = ``) a, b = b, a+b
- Cov ntu yuav khiav ntev li ntev tau (thaum) b tsawg dua (<) 100.
- Cov txiaj ntsig tau los ntawm qhov program yog 1 1 2 3 5 8 13 21 34 55 89
- Qhov hais kom ua qhov kawg = `` yuav muab cov ntawv tso rau ntawm tib txoj kab es tsis txhob muab txhua tus nqi tso rau ntawm ib kab.
-
Muaj ob peb yam yuav tsum nco ntsoov hauv txoj haujlwm yooj yim no, thiab lawv tseem ceeb heev rau kev tsim cov haujlwm nyuaj hauv Python:
- Them sai sai rau qhov indents.: qhia tias kab tom ntej yuav nkag mus thiab yog ib feem ntawm qhov thaiv. Hauv qhov piv txwv saum toj no, luam tawm (b) thiab a, b = b, a+b yog ib feem ntawm lub sijhawm thaiv. Kev nkag qhov raug yog qhov tseem ceeb rau txoj haujlwm kom ua haujlwm.
- Ntau qhov sib txawv tuaj yeem txhais tau ntawm tib kab. Hauv qhov piv txwv saum toj no, a thiab b. Ob qho no tau piav qhia nyob rau thawj kab
- Yog tias koj nkag mus rau txoj haujlwm no ncaj qha rau tus neeg txhais lus, koj yuav tsum ntxiv kab kab tom kawg kom tus neeg txhais lus paub tias txoj haujlwm tau ua tiav.
Kauj Ruam 2. Tsim txoj haujlwm hauv qhov program
Koj tuaj yeem txhais cov haujlwm uas tom qab tuaj yeem hu ua hauv qhov haujlwm. Qhov no muaj txiaj ntsig tshwj xeeb yog tias koj xav tau siv ntau txoj haujlwm hauv qhov kev txwv ntawm txoj haujlwm loj dua. Hauv qhov piv txwv hauv qab no, koj tuaj yeem tsim txoj haujlwm hu Fibonacci ib qib zuj zus zoo ib yam li koj tau sau ua ntej:
def fib (n): a, b = 0, 1 thaum a <n: print (a, end = ``) a, b = b, a+b print () # Koj tuaj yeem hu rau # Fibonacci muaj nuj nqi rau txhua tus nqi teev fib (1000)
Qhov no yuav rov qab 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
Kauj Ruam 3. Tsim kom muaj cov txheej txheem tswj cov txheej txheem nyuaj dua
Cov lus hais txog kev tswj ntws yog qhov muaj txiaj ntsig rau teeb tsa qee yam xwm txheej uas hloov pauv txoj haujlwm li cas. Qhov no yog qhov tshwj xeeb tshaj yog tias koj tab tom cuam tshuam nrog cov neeg siv tswv yim. Cov piv txwv hauv qab no yuav siv yog, elif (lwm yam yog), thiab lwm yam los tsim cov haujlwm yooj yim uas ntsuas tus neeg siv lub hnub nyoog.
hnub nyoog = int (input ("Nkag mus rau koj lub hnub nyoog:")) yog hnub nyoog <= 12: luam tawm ("Me nyuam yaus zoo heev!") elif hnub nyoog hauv thaj tsam (13, 20): luam tawm ("Koj yog ib tug tub hluas!"): luam tawm ("Lub sijhawm kom loj hlob") # Yog tias ib qho ntawm cov nqe lus no muaj tseeb # Cov lus sib tham yuav raug nthuav tawm. # Yog tias tsis muaj ib nqe lus twg muaj tseeb, # ib qho lus "lwm" yuav tshwm.
-
Txoj haujlwm no tseem qhia qee qhov lwm nqe lus tseem ceeb heev rau kev siv hauv ntau yam kev thov:
- input () - Qhov no hu cov neeg siv tswv yim los ntawm cov keyboard. Tus neeg siv yuav pom cov lus sau rau hauv kab zauv. Hauv qhov piv txwv no, cov tswv yim () nyob ib puag ncig los ntawm int () ua haujlwm, uas txhais tau tias txhua qhov kev nkag mus yuav raug kho raws li cov lej
- range () - Txoj haujlwm no tuaj yeem siv ntau txoj hauv kev. Hauv txoj haujlwm no txoj haujlwm no txheeb xyuas yog tias tus lej nyob hauv thaj tsam 13 thiab 20. Qhov kawg ntawm qhov ntau tsis suav nrog hauv kev suav.
Kauj Ruam 4. Kawm lwm yam kab zauv raws cai
Qhov piv txwv yav dhau los siv "tsawg dua lossis sib npaug" cim (<=) los txiav txim siab seb lub hnub nyoog nkag mus puas haum rau qhov xwm txheej. Koj tuaj yeem siv cov kab lus ib txwm muaj ib yam li hauv lej, tab sis txoj kev lawv raug ntaus yog txawv me ntsis:
Lub ntsiab lus | Cim | Python Cim | |
---|---|---|---|
Me dua | < | < | |
Loj tshaj | > | > | |
Tsawg dua lossis sib npaug rau | ≤ | <= | |
Ntau dua lossis sib npaug rau | ≥ | >= | |
Ua ke nrog | = | == | |
Tsis sib npaug rau | ≠ | != |
Kauj Ruam 5. Kawm tas li
Txhua qhov saum toj no tsuas yog siv Python. Thaum Python yog ib hom lus yooj yim kawm, muaj ntau yam nyob hauv nws uas koj tuaj yeem nkag mus. Txoj hauv kev zoo tshaj los kawm txuas ntxiv yog ua kom lub cajmeem! Nco ntsoov tias koj tuaj yeem sau cov program sai los ntawm kos ncaj qha los ntawm tus neeg txhais lus, thiab ntsuas koj qhov kev hloov pauv tau yooj yim ib yam li rov ua haujlwm dua los ntawm kab hais kom ua.
- Muaj ntau phau ntawv zoo muaj nyob ntawm Python programming, suav nrog "Python for Beginners", "Python Cookbook", thiab "Python Programming: Introduction to Computer Science".
- Muaj ntau qhov chaw muaj nyob hauv is taws nem, tab sis feem ntau tseem mob siab rau Python 2.x. Koj yuav tau hloov kho rau txhua qhov piv txwv.
- Ntau chav kawm muaj kev kawm Python. Nab hab sej feem ntau tau qhia hauv cov chav qhia, vim nws yog ib yam lus uas yooj yim kawm.