Cov txheej txheem:

Intro to Python: 9 Kauj Ruam
Intro to Python: 9 Kauj Ruam

Video: Intro to Python: 9 Kauj Ruam

Video: Intro to Python: 9 Kauj Ruam
Video: Python Tutorial - Python Full Course for Beginners 2024, Lub Xya hli ntuj
Anonim
Qhia rau Python
Qhia rau Python

Nab hab sej yog qhov muaj txiaj ntsig zoo thiab hloov kho lub computer cov lus tsim los ntawm Guido van Rossum. Python kuj tseem yog daim ntawv thov ntawm Raspberry Pi uas txhais cov cai koj sau rau hauv qee yam Raspberry Pi tuaj yeem nkag siab thiab siv los siv. Ntawm koj lub khoos phis tawj desktop koj yuav xav tau nruab Python, tab sis nws twb dhau los ua ke nrog Raspbian yog li nws tau npaj rau koj siv.

Koj yuav xav tias Python muaj npe tom qab hom nab tab sis nws tau muaj npe tiag tiag tom qab Monty Python's Flying Circus TV qhia. Rau cov ntawm koj uas tsis paub, qhov no yog lus Askiv tso dag uas tau tshaj tawm thawj zaug hauv xyoo 1970. Kuv txhawb koj kom saib Ministry of Silly Walks sketch:)

Duab
Duab

Cov duab saum toj no tau tsim nyob rau hauv Terry Gilliam zam los ntawm Allison Parten thiab pom nyob hauv qab daim ntawv tso cai muaj txiaj ntsig zoo.

Piv rau lwm yam lus hauv computer, Nab hab sej tau yooj yim nyeem thiab nkag siab uas ua rau nws zoo rau cov pib tshiab. Tab sis tsis txhob yuam kev, Python kuj tseem muaj zog heev thiab tau siv los ua qhov nyuaj, muaj tswv yim thiab ua lag luam software. Cov laj thawj no thiab ntau ntxiv ua rau Python tus lej ib hom lus pib nrog thaum sau cov haujlwm rau Raspberry Pi (txawm hais tias siv ntau hom lus tuaj yeem siv tau).

Zoo li txhua yam lus, Python muaj cov qauv sau ntawv nrog rau kev txiav txim yuav ua li cas thiaj li hais cov lus thiab cim kab cim. Lub sij hawm siv rau cov kev cai yooj yim ntawm kev teeb tsa cov lus yog hu ua syntax. Kuv yuav taw qhia Python syntax thoob plaws zaj lus qhia no nrog rau piv txwv tus lej rau koj sim. Hauv chav kawm ntawm qhov loj me no, nws tsis yooj yim sua hla txhua yam Python tuaj yeem ua tau, tab sis zaj lus qhia no thiab tom ntej no, lub npe hu ua Siv GPIOs thiab Mus Ntxiv nrog Python, yuav ua rau koj pib nrog koj thawj cov haujlwm. Thaum koj nyeem thiab ua haujlwm dhau los ntawm cov piv txwv, koj yuav kawm paub qee qhov hauv paus ntsiab lus ntawm Python thiab kev tsim kho feem ntau.

Kauj Ruam 1: Python 2 lossis 3?

Muaj ob qhov qauv ntawm Python uas tam sim no tau siv thiab qhia. Thaum kuv yuav tsis nkag mus rau qhov tsis txaus ntseeg ntawm qhov lawv sib txawv, nws yog qhov tseem ceeb kom paub tias muaj qhov sib txawv. Txhawm rau paub qhov no yog cawm koj tus kheej ntau lub taub hau khawb thaum piv txwv pom hauv online tsis ua haujlwm.

Nab hab sej 2 tau tshaj tawm tias yuav tsum tau so haujlwm los ntawm 2020 raws li tau hais los ntawm Python Software Foundation. Lawv hais qhov no ntawm lawv lub xaib, "Ua zaum kawg ntawm 2.x series, 2.7 yuav muaj sijhawm txuas ntxiv ntawm kev saib xyuas. Tam sim no txoj kev npaj yog txhawb nws tsawg kawg 10 xyoo txij li thawj 2.7 tso tawm. Qhov no txhais tau tias yuav muaj bugfix tso tawm kom txog thaum 2020. " Muaj txawm tias suav suav suav txhua yam hauv kev lom zem zoo lossis tej zaum yog li cov tib neeg tuaj yeem npaj tawm mus sab nrauv kom ua tiav qhov kawg ntawm Python 2.

Hauv chav kawm no, peb siv ob qho tib si. Feem ntau, Kuv yuav saib mus rau yav tom ntej thiab siv Python 3 tab sis muaj qee yam tshwm sim thaum tsim cov piv txwv rau chav kawm no. Ib feem ntawm software kuv xav siv rau qhov program Python zaum kawg tsis tau hloov kho los txhawb Python 3 tseem. Uas ua tau zoo tshaj plaws qhia kuv lub ntsiab lus ntawm yuav ua li cas nws tsim nyog hais txog ob qho tib si tam sim no.

Kauj ruam 2: Python Sib tham sib Vs Script

Thaum ua haujlwm nrog Python daim ntawv thov, koj muaj ob txoj kev xaiv los ntawm: sib tham sib thiab tsab ntawv.

Hom kev sib tham siv lub Python plhaub los txhais lus Python code tam sim tom qab nws tau ntaus thiab koj ntaus Enter. Nws yog qhov zoo rau kev kawm thiab ntsuas cov program hauv. Peb yuav koom ob txoj hauv kev nkag mus rau Python plhaub hauv zaj lus qhia no.

Thaum koj sau tsab ntawv nab hab sej, tseem hu ua program, koj tsis siv lub plhaub sib tham tab sis hloov cov ntawv sau. Txoj hauv kev no koj tuaj yeem txuag, kho, thiab tom qab ua haujlwm ntau kab ntawm Python code thaum koj xav tau.

Kauj Ruam 3: IDLE: Sib tham sib

Duab
Duab

Nrog rau nruab Python software los Python tus txheej txheem txhim kho ib puag ncig hu ua IDLE (Integrated DeveLopment Environment). Nov yog qhov uas koj yuav pib sau koj thawj cov haujlwm Python!

Muaj ob ntu rau IDLE:

1) Python lub plhaub qhov rai, uas muab rau koj nkag mus rau Python hauv kev sib tham sib hom.

2) Tus kws kho ntaub ntawv uas tso cai rau koj tsim thiab kho cov ntawv Python uas twb muaj lawm, tseem hu ua hom ntawv.

Qhib Python 3 (IDLE) los ntawm Ntawv Qhia> Programming. Lub qhov rais koj pom hu ua Python tus neeg txhais lus lossis lub qhov rais plhaub. Peb qhov loj tshaj-cim ">>>" raug hu ua qhov hais kom sai. Thaum koj pom qhov nrawm uas txhais tau hais tias Python tab tom tos koj qhia nws ua qee yam. Cia peb muab qee qhov chaws!

Duab
Duab

Ntaus nqe lus hauv qab no kom nrov nrov rau lub nroog koj nyob. Kuv nyob hauv San Francisco yog li kuv cov lus yog:

print ("Nyob zoo, San Francisco!")

Ntaus nkag thiab txhua yam uas koj muab tso rau nruab nrab ntawm cov lus yuav luam tawm hauv lub plhaub hauv qab qhov hais kom sai. Cov ntawv () nqe lus sib txawv ntawm Python 2 thiab Python 3. Hauv Python 2 parenthesis tsis siv thiab zoo li no:

sau "Nyob zoo, San Francisco!"

Duab
Duab

Koj nyuam qhuav ua qhov pib "nyob zoo, ntiaj teb" ntawm Python tab sis nrog qhov me me (me me me) sib tw. Cia peb nres ntawm no thiab txheeb xyuas seb nws yog yam koj nyuam qhuav ntaus.

Kev ua haujlwm

Sau () yog lub luag haujlwm. Ib txoj haujlwm txo qis ntawm kev rov ua dua thiab pab ua kom txoj haujlwm raug teeb tsa los ntawm kev ua ib txoj cai ntawm txhua lub sijhawm nws hu ua. Thaum koj ntaus ntawv (), koj hu rau qhov ua haujlwm luam ntawv, uas tom qab ntawd sau kab ntawm cov lej tom qab qhov xwm txheej uas muab khoos phis tawj qhia kom pom cov lus koj muab nruab nrab ntawm kab lus. Sau () yog qhov ua haujlwm uas koj tuaj yeem hu txhua lub sijhawm hauv Python tab sis koj tuaj yeem sau koj tus kheej lub luag haujlwm ib yam nkaus.

Cia peb ua kev sib cuam tshuam ntawm koj thiab Python ntau dua los ntawm kev ntxiv cov neeg siv tswv yim. Hauv lub qhov rai tshiab, ntaus cov hauv qab no:

name = input ("Nyob zoo, koj lub npe hu li cas?")

Cov tswv yim () ua haujlwm siv cov neeg siv tswv yim los ntawm cov keyboard thiab muab txoj hauv kev rau koj kom sai sai rau tus neeg siv nrog cov lus. Hauv qhov no, cov lus ntawd yog kev tos txais thiab lus nug nug tus neeg siv (koj) koj lub npe yog dab tsi. Tom qab ntaus nkag, cov lus nug yuav luam tawm thiab tos koj cov lus teb. Mus tom ntej thiab teb nrog koj lub npe.

Duab
Duab
Duab
Duab

Lub npe = mus rau sab laug ntawm cov lus tawm tswv yim () muaj nuj nqi hu ua qhov sib txawv.

Ntau yam

Zoo li lub luag haujlwm, qhov sib txawv tseem yog lub hauv paus tseem ceeb ntawm txhua hom lus programming. Qhov sib txawv ua haujlwm zoo li lub thawv uas tsis muaj dab tsi uas koj tuaj yeem muab cov ntaub ntawv tso rau hauv. Thaum koj tso cov ntaub ntawv rau hauv, koj muab nws lub npe zoo li yog koj sau nws nyob sab nraum lub thawv. Lub npe tshwj xeeb uas koj muab rau nws tuaj yeem siv los siv cov ntaub ntawv sab hauv thoob plaws koj li haujlwm. Koj tuaj yeem sau lub npe sib txawv yuav luag txhua yam, tab sis nws yuav tsum piav qhia ntau li ntau tau. Qhov no ua rau koj txoj haujlwm nkag siab yooj yim dua thaum koj nyeem nws tom qab. Cov ntaub ntawv khaws cia hauv qhov sib txawv tuaj yeem hloov pauv; koj yuav pom qhov piv txwv ntawm qhov no hauv zaj lus qhia tom ntej.

Koj lub npe tau khaws cia hauv qhov sib txawv hu ua lub npe (sim hu nws lwm yam). Tam sim no koj tuaj yeem siv lub npe sib txawv hauv kev luam () ua haujlwm thiab ntxiv nws rau cov lus siv tus cim "+":

print ("Zoo siab tau ntsib koj," + npe)

Duab
Duab

Cia peb xyaum ua kom tau txais cov neeg siv cov tswv yim, khaws nws rau hauv cov ntawv hloov pauv, thiab luam tawm cov ntaub ntawv los ua qhov sib tham ntawm koj thiab koj lub khoos phis tawj.

city = input ("Koj nyob lub nroog twg?")

print ("Kuv tau hnov txog" + nroog + ". Koj xav li cas txog" + nroog + "," + npe + "?")

Hu rau cov tswv yim () ua haujlwm ntawm nws tus kheej tseem tos rau cov neeg siv khoom tab sis nws puas tsis tau luam tawm cov lus.

teb = input ()

Vim tias cov khoos phis tawj ua rau peb twv, kuv yuav ua kom lub khoos phis tawj pom zoo. Koj tuaj yeem ua rau nws tsis pom zoo yog tias koj nyiam. Qhov ntawd yog qhov zoo tshaj plaws ntawm kev ua haujlwm, nws nyob ntawm koj.

print ("Kuv pom zoo nrog koj." + teb)

Duab
Duab

Nws zoo yog tias koj pom qhov yuam kev txhua lub sijhawm hauv Python plhaub. Koj cov ntaub ntawv yuav nyob hauv koj qhov kev hloov pauv tsuav koj tsis kaw qhov kev sib tham.

Duab
Duab

Lub plhaub sib tham sib yog zoo meej rau kev sim cov lus txib thiab pom dab tsi ua haujlwm. Tab sis nws tsis txuag koj qhov program yog li koj tuaj yeem ua haujlwm tom qab. Koj qhov kev sib ntsib tuaj yeem raug cawm tab sis Python tseem khaws cov lus qhia sai, ua tsis raug, thiab txhua yam uas koj pom hauv lub plhaub qhov rai. Cov no yuav txhua qhov ua yuam kev yog tias koj sim kom Python khiav nws raws li qhov haujlwm tom qab.

Txhawm rau sim (thiab raws li chav kawm ib ce), ntxiv ob kab ntxiv rau txoj haujlwm no txuas ntxiv kev sib tham ntawm koj thiab koj lub khoos phis tawj. Tsim qhov sib txawv thiab luam tawm nqe lus siv koj qhov kev hloov pauv tshiab. Siv lub vijtsam lossis khaws yam koj tau ua hauv lub plhaub rau kev siv. Koj yuav siv nws hauv cov kauj ruam tom ntej.

Kauj ruam 4: IDLE: Script

IDLE: Tsab ntawv
IDLE: Tsab ntawv

Tsis zoo li lub plhaub, tus kws sau ntawv tau siv yog li koj tuaj yeem txuag thiab kho koj li Python cov haujlwm. Txawm hais tias muaj ntau tus kws kho koj tuaj yeem siv, IDLE los nrog ib qho yog li cia peb pib nrog qhov ntawd.

Tsim cov ntaub ntawv tshiab hauv IDLE los ntawm nias Ctrl + N lossis mus rau Cov Ntaub Ntawv> Tshiab.

Pom zoo li cas lub qhov rais koj pom tsis muaj qhov ">>>" hais kom ua. Tam sim no koj nyob hauv tus kws kho thiab npaj sau, txuag, thiab ua haujlwm Python program. Sau tawm qhov program koj tau tsim hauv lub plhaub nrog rau ob kab tshiab uas koj tuaj nrog hauv ntu kawg. Ua piv txwv, ntawm no yog kuv tsab ntawv kawg:

name = input ("Nyob zoo, koj lub npe hu li cas?")

print ("Nws zoo siab tau ntsib koj," + lub npe) lub nroog = nkag mus ("Koj nyob lub nroog twg?") luam tawm ("Kuv tau hnov txog" + nroog + ". Koj xav li cas txog" + nroog + ", " + Lub npe +"? ") Teb = tawm tswv yim () luam tawm (" Kuv pom zoo nrog koj, " + teb) favSpot = cov tswv yim (" Koj nyiam qhov chaw twg? ") Luam tawm (" Tsis tau, tab sis kuv nyiam mus qee lub sijhawm rau " + favSpot)

Duab
Duab

Txuag nws li city.py. Qhov chaw nyob tsis raug yog koj phau ntawv qhia tsev.

Duab
Duab

Kauj Ruam 5: Khiav Qhov Kev Pabcuam Python

Koj qhov program tau txais kev cawmdim thiab npaj tau los khiav. Muaj ob peb txoj hauv kev sib txawv los ua haujlwm Python ntawm Raspberry Pi. Cia mus tshaj ob. Xaiv ib qho los ua haujlwm rau koj qhov program thiab thaij duab tom qab koj tham tas.

1) Khiav los ntawm IDLE

Nias F5 lossis nce mus rau qhov toolbar thiab nyem Run> Run Module. Nab hab sej yuav luam tawm cov txiaj ntsig hauv lub plhaub qhov rai. Txhawm rau nres qhov haujlwm, nyem Ctrl+F6 lossis mus rau Plhaub> Pib dua Plhaub.

2) Khiav los ntawm Linux Plhaub

Cov haujlwm tau txais kev cawmdim rau Raspberry Pi tuaj yeem khiav los ntawm txhua lub zog Linux lub plhaub, ib yam nkaus. Txhawm rau kom ua haujlwm Python los ntawm kab hais kom ua, koj txoj haujlwm yuav tsum nyob hauv cov npe ua haujlwm tam sim no. LXTerminal pib koj hauv pi lub tsev qhia chaw uas nab nab tau txuag, yog li koj yuav tsum tau nyob hauv daim nplaub tshev nrog koj cov ntaub ntawv. Txhawm rau txheeb xyuas, ntaus:

ls ua

Koj yuav tsum pom koj cov kev pabcuam city.py tau teev tseg.

Txhawm rau kom ua haujlwm Python kab lus hais kom ua kab hom python3 ntxiv rau lub npe ntawm koj tsab ntawv:

nab hab sej3 city.py

Rau ib tsab ntawv sau hauv Python 2 koj yuav siv nab nab es tsis txhob nab nab 3:

nab nab npeOfScript.py

Thawj kab ntawm txoj haujlwm yuav ua haujlwm tos koj ntaus koj lub npe. Nws tseem yuav ua tiav los ntawm sab saum toj mus rau hauv qab kom txog thaum nws mus txog qhov kawg luam () muaj nuj nqi.

Duab
Duab
Duab
Duab

Nres Txoj Haujlwm Python

Txhawm rau tso tseg txoj haujlwm Python hauv kab hais kom ua Ctrl + Z.

Kauj Ruam 6: Python + Linux Plhaub

Txawm hais tias Python yuav yog hom lus tseem ceeb uas koj siv los ua haujlwm Raspberry Pi, qee zaum koj yuav xav siv cov cuab yeej hais kom ua lossis thov kom tau txais qee yam ua tiav. Koj tuaj yeem ua ke cov kab hais kom ua thiab Python los siv koj cov lus txib nyiam siv tus qauv hu ua os.

Tus qauv yog sau los ntawm cov lej ua ntej uas koj tuaj yeem siv los ua haujlwm ntxiv rau koj cov haujlwm. Siv tus qauv kuj tseem tuaj yeem txuag koj qhov teeb meem ntawm kev txiav txim siab yuav ua li cas sau qee yam khoom zoo nkauj. Piv txwv li, hais tias koj xav taug qab lub ntiaj teb thiab kawm txog lawv cov orbits nrog koj li Raspberry Pi. Hloov chaw ntawm kev xam tawm cov lej sib txawv tom qab pinning txoj haujlwm ntawm Lub Hnub, koj tuaj yeem siv tus qauv* uas twb muaj qhov ntawd tawm los lawm.

Txhawm rau siv os, koj yuav tsum xub ua ntej nws. Mus tom ntej thiab ua raws li hauv Python plhaub:

import os

Thauj cov lus txib koj xav khiav hauv Linux plhaub rau hauv qhov sib txawv. Ntawm no peb tab tom siv kab lus hais kom ua kab ntawv video player omxplayer los ua cov vis dis aus uas los nrog Raspbian:

playVideo = "omxplayer /opt/vc/src/hello_pi/hello_video/test.h264"

Siv os.system () xa cov lus txib Linux plhaub:

os.system (playVideo)

* Muaj tiag tiag yog Python module uas tuaj yeem taug qab lub ntiaj teb, nws hu ua PyEphem.

Kauj Ruam 7: Siv Duab thiab Ua Si Suab Nrog Python

Picamera

Txog tam sim no koj tau siv Raspistill daim ntawv thov kab kab kom thaij cov duab nrog. Muaj Python module muaj hu ua Picamera koj tuaj yeem siv hloov qhov ntawd kuj tseem muaj cov yam ntxwv ntxiv uas yuav pab tau thaum tsim koj lub rooj muag khoom zaum kawg. Cov theem hauv qab no tuaj yeem npog koj lub vijtsam, yog li nco ntsoov tias kom tsis txhob ua cov txheej txheem, ntaus Ctrl + F6.

Qhib Python plhaub thiab ntaus kab hauv qab no:

ntshuam picamera

lub koob yees duab = picamera. PiCamera ()

camera.resolution = (640, 480)

camera.start_preview ()

Siv qhov kev saib ua ntej nyob rau hauv txhawm rau muab tso rau hauv koj lub koob yees duab txhawm rau thaij duab ntawm koj tus kheej lossis koj lub rooj. Lub koob yees duab saib ua ntej yuav siv ntau dua ntawm koj lub vijtsam. Rov hais dua thawj peb cov lus txib, tom qab ntawd hla mus ntes thiab khaws cov duab:

camera.capture ('testImage.jpg')

Sim hloov qhov ci ntawm lub koob yees duab uas siv tus cwj pwm ci ci. Nws tuaj yeem teeb tsa tus lej ntawm 0 thiab 100 thiab lub neej ntawd yog 50. Sim teeb nws mus rau lwm tus lej, tom qab ntawd thaij ib daim duab tshiab los hloov thawj:

camera.brightness = 60

camera.capture ('testImage.jpg')

Nco ntsoov siv sijhawm los tshuaj xyuas Picamera cov ntaub ntawv.

Siv os module

Yog tias koj xav siv daim ntawv thov kab lus zoo li Raspistill koj tuaj yeem siv os module. Ntaus Cmd + F6 kom tawm ntawm txoj kev khiav thiab tso lub koob yees duab los ntawm Picamera. Tom qab ntawd ntaus cov lus txib hauv qab no:

import os

takePhoto = "raspistill -o testImage.jpg"

os.system (takePhoto)

Pygame.mixer

Txoj hauv kev yooj yim thiab muaj zog ntawm kev ua cov ntaub ntawv suab yog siv Pygame. Pygame yog txheej Python cov qauv uas los nrog Raspbian yog li tsis tas yuav teeb tsa nws.

Pygame yog qhov nrov thiab lom zem yog li muaj kev txhawb nqa thiab kev txhim kho ib puag ncig nws. Nco ntsoov xyuas lub vev xaib rau cov piv txwv thiab cov ntaub ntawv. Dhau li tau siv los tsim kev ua si, nws yog txoj hauv kev yooj yim los ua suab, tso duab thiab ntau ntxiv. Txhawm rau ua suab nrov siv lub suab kwv hauv pygame.mixer module.

Qhib tus neeg txhais lus Python 3 thiab ntaus kab hauv qab no:

ntshuam pygame.mixer

los ntawm pygame.mixer ntshuam suab

pygame.mixer.init ()

ntses bass = Suab ('bass3.wav')

bass.play ()

Kauj Ruam 8: Siv Python Sab nraum IDLE

Desktop EditorsIDLE yog txoj hauv kev zoo los pib sau cov program Python tab sis koj tsis tas yuav siv nws. Koj tuaj yeem tsim txoj haujlwm Python siv cov ntawv sau ntev npaum li nws tau khaws nrog.py thaum kawg. IDLE yog ib qho piv txwv ntawm lub khoos phis tawj kab ntawv nyeem kab lus. Lwm qhov uas los nrog Raspbian hu ua Leafpad. Koj tuaj yeem pom Leafpad hauv qab Cov Khoom Siv hauv Start Menu.

Duab
Duab
Duab
Duab

Hais kom-kab Editor

Koj twb tau tshaj tawm rau Nano cov kab hais kom ua kab ntawv sau. Ib yam li tus kws kho duab, koj tuaj yeem siv nano los tsim cov ntawv. Tom qab qhib Nano khaws cov ntaub ntawv tshiab nrog.py cov ntawv ntxiv. Txuag nws ua cov ntaub ntawv nab npawb ua ntej yuav ua kom tus khoos phis tawj sau cov ntsiab lus tseem ceeb raws li koj ntaus ntawv.

Duab
Duab
Duab
Duab

Tsis muaj cov ntsiab lus tseem ceeb ua ntej khaws cov npe cov ntaub ntawv nrog.py txuas ntxiv.

Duab
Duab

Nrog syntax highlighting.

Python Tus Neeg Txhais Lus los ntawm Command-line

IDLE tsis yog qhov chaw koj tuaj yeem siv tus neeg txhais lus Python sib tham sib. Nws tuaj yeem raug hu los ntawm Linux plhaub ib yam nkaus! Tsuas ntaus:

nab hab sej 3

Los yog rau tus neeg txhais lus Python 2:

nab hab sej

Txhawm rau tawm ntawm tus neeg txhais lus nias Ctrl + D lossis ntaus:

tawm ()

Kauj Ruam 9: Screenshot ntawm Txoj Haujlwm

Qhia tawm koj li Python lub peev xwm ua haujlwm! Rub tawm lub vijtsam tom qab koj qhov kev ua haujlwm city.py kawg ua tiav thiab qhia rau peb yog tias koj tau khiav los ntawm IDLE lossis Linux plhaub.

Pom zoo: