1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
1.10
1.11
1.12
1.13
1.14
1.15
1.16
1.17
1.18
1.19
1.20
1.21
1.22
1.23
1.24
1.25
TableofContents
Introduction
Dedication
Preface
AboutPython
Installation
FirstSteps
Basics
OperatorsandExpressions
Controlflow
Functions
Modules
DataStructures
ProblemSolving
ObjectOrientedProgramming
InputandOutput
Exceptions
StandardLibrary
More
WhatNext
Appendix:FLOSS
Appendix:About
Appendix:RevisionHistory
Appendix:Translations
Appendix:TranslationHow-to
Feedback
1
AByteofPython
"AByteofPython"isafreebookonprogrammingusingthePythonlanguage.Itservesasa
tutorialorguidetothePythonlanguageforabeginneraudience.Ifallyouknowabout
computersishowtosavetextfiles,thenthisisthebookforyou.
ForPythonversion3
ThisbookwillteachyoutousePythonversion3.Therewillalsobeguidanceforyouto
adapttotheolderandmorecommonPythonversion2inthebook.
WhoreadsAByteofPython?
Herearewhatpeoplearesayingaboutthebook:
Thisisthebestbeginner'stutorialI'veeverseen!Thankyouforyoureffort.--Walt
Michalik
Thebestthingifoundwas"AByteofPython",whichissimplyabrilliantbookfora
beginner.It'swellwritten,theconceptsarewellexplainedwithselfevidentexamples.--
JoshuaRobin
Excellentgentleintroductiontoprogramming#Pythonforbeginners--Shan
Rajasekaran
Bestnewbieguidetopython--NicksonKaigi
starttolovepythonwitheverysinglepageread--HerbertFeutl
perfectbeginnersguideforpython,willgiveukeytounlockmagicalworldofpython--
Dilip
Ishouldbedoingmyactual"work"butjustfound"AByteofPython".Agreatguidewith
greatexamples.--BiologistJohn
RecentlystartedreadingaByteofpython.Awesomework.Andthattooforfree.Highly
recommendedforaspiringpythonistas.--Mangesh
AByteofPython,writtenbySwaroop.(thisisthebookI'mcurrentlyreading).Probably
thebesttostartwith,andprobablythebestintheworldforeverynewbieorevena
moreexperienceduser.--Apostolos
Introduction
2
EnjoyingReading#ByteOfPythonby@swaroopchbestbookever--YuvrajSharma
ThankyousomuchforwritingAByteOfPython.Ijuststartedlearninghowtocodetwo
daysagoandI'malreadybuildingsomesimplegames.Yourguidehasbeenadream
andIjustwantedtoletyouknowhowvaluableithasbeen.--Franklin
I'mfromDayanandasagarCollegeofEngineering(7thsem,CSE).Firstlyiwanttosay
thatyourbook"Thebyteofpython"istoogoodabookforabeginnerinpythonlike
me.Theconceptsaresowellexplainedwithsimpleexamplesthathelpedmetoeasily
learnpython.Thankyousomuch.--Madhura
Iama18yearoldITstudentstudyingatUniversityinIreland.Iwouldliketoexpress
mygratitudetoyouforwritingyourbook"AByteofPython",Ialreadyhadknowledgeof
3programminglangagues-C,JavaandJavascript,andPythonwasbyfartheeasiest
langagueIhaveeverlearned,andthatwasmainlybecauseyourbookwasfantastic
andmadelearningpythonverysimpleandinteresting.Itisoneofthebestwrittenand
easytofollowprogrammingbooksIhaveeverread.Congratulationsandkeepupthe
greatwork.--Matt
Hi,I'mfromDominicanRepublic.MynameisPavel,recentlyIreadyourbookAByteof
PythonandIconsideritexcellent!!:).Ilearntmuchfromalltheexamples.Yourbookis
ofgreathelpfornewbieslikeme...--PavelSimo
IamastudentfromChina,Now,IhavereadyoubookAbyteofPython,Ohit's
beautiful.Thebookisverysimplebutcanhelpallthefirstlearnners.YouknowIam
interestinginJavaandcloudcomputingmanytimes,ihavetocodingprogrammforthe
server,soithinkpythonisagoodchoice,finishyourbook,ithinkitsnotonlyagood
choiceitsmustusethePython.MyEnglishisnotverywell,theemailtoyou,ijust
wannathankyou!BestWishesforyouandyourfamily.--RoyLau
IrecentlyfinishedreadingByteofPython,andIthoughtIreallyoughttothankyou.I
wasverysadtoreachthefinalpagesasInowhavetogobacktodull,tediousoreillyor
etc.manualsforlearningaboutpython.Anyway,Ireallyappreciateyourbook.Samuel
Young
DearSwaroop,Iamtakingaclassfromaninstructorthathasnointerestinteaching.
WeareusingLearningPython,secondedition,byO'Reilly.Itisnotatextforbeginner
withoutanyprogrammingknowledge,andaninstructorthatshouldbeworkingin
anotherfield.Thankyouverymuchforyourbook,withoutitIwouldbecluelessabout
Pythonandprogramming.Thanksamillion,youareabletobreakthemessagedownto
alevelthatbeginnerscanunderstandandnoteveryonecan.--JosephDuarte
Iloveyourbook!ItisthegreatestPythontutorialever,andaveryusefulreference.
Brilliant,atruemasterpiece!Keepupthegoodwork!--Chris-AndréSommerseth
Introduction
3
Firstofall,Iwanttosaythankstoyouforthisgreatebook.Ithinkitisagoodbookfor
thosewhoarelookingforabeginner'stutorialforPython.Itisabouttwoorthereyears
ago,Ithink,whenIfirstheardofthisbook.Atthattime,Iamnotabletoreadsome
bookinEnglishyet,soIgotachinesetranslation,whichtookmeintothegateof
Pythonprogramming.Recently,Irereadthisbook.Thistime,ofcourse,theenglish
version.Icouldn'tbelievethatIcanreadthewholebookwithoutmydictionaryathand.
Ofcourse,itallduestoyourefforttomakethisbookaneasy-to-understandone.--
myd7349
I'mjuste-mailingyoutothankyouforwritingByteofPythononline.Ihadbeen
attemptingPythonforafewmonthspriortostumblingacrossyourbook,andalthoughI
madelimitedsuccesswithpyGame,Inevercompletedaprogram.Thankstoyour
simplificationofthecategories,Pythonactuallyseemsareachablegoal.ItseemslikeI
havefinallylearnedthefoundationsandIcancontinueintomyrealgoal,game
development....Onceagain,thanksVERYmuchforplacingsuchastructuredand
helpfulguidetobasicprogrammingontheweb.ItshovedmeintoandoutofOOPwith
anunderstandingwheretwotextbookshadfailed.--MattGallivan
IwouldliketothankyouforyourbookAByteofPythonwhichimyselffindthebestway
tolearnpython.Iama15yearoldiliveinegyptmynameisAhmed.Pythonwasmy
secondprogramminglanguageilearnvisualbasic6atschoolbutdidn'tenjoyit,
howeverireallyenjoyedlearningpython.Imadetheaddressbookprogramandiwas
sucessful.iwilltrytostartmakemoreprogramsandreadpythonprograms(ifyoucould
tellmesourcethatwouldbehelpful).Iwillalsostartonlearningjavaandifyoucantell
mewheretofindatutorialasgoodasyoursforjavathatwouldhelpmealot.Thanx.--
AhmedMohammed
AwonderfulresourceforbeginnerswantingtolearnmoreaboutPythonisthe110-page
PDFtutorialAByteofPythonbySwaroopCH.Itiswell-written,easytofollow,and
maybethebestintroductiontoPythonprogrammingavailable.--DrewAmes
YesterdayIgotthroughmostofByteofPythononmyNokiaN800andit'stheeasiest
andmostconciseintroductiontoPythonIhaveyetencountered.Highlyrecommended
asastartingpointforlearningPython.--JasonDelport
ByteofVimandPythonby@swaroopchisbyfarthebestworksintechnicalwritingto
me.Excellentreads#FeelGoodFactor--Surendran
"Byteofpython"bestonebyfarman(inresponsetothequestion"Cananyonesuggest
agood,inexpensiveresourceforlearningthebasicsofPython?")--JustinLoveTrue
TheBookByteofpythonwasveryhelpful..Thanksbigtime:)Chinmay
Introduction
4
AlwaysbeenafanofAByteofPython-madeforbothnewandexperienced
programmers.--PatrickHarrington
Istartedlearningpythonfewdaysagofromyourbook..thanksforsuchanicebook.itis
sowellwritten,youmademylifeeasy..soyoufoundanewfanofyours..thatsme:)
tonsofthanks.--GadadhariBheem
BeforeIstartedtolearnPython,I'veacquiredbasicprogrammingskillsinAssembly,C,
C++,C#andJava.TheveryreasonIwantedtolearnPythonisit'spopular(peopleare
talkingaboutit)andpowerful(reality).ThisbookwrittenbyMr.Swaroopisaverygood
guideforbothbrand-newprogrammersandnewpythonprogrammers.Took10half
daystogothroughit.GreatHelp!--FangBiyi(PhDCandidateECE,MichiganState
University)
Thankyoueversomuchforthisbook!!ThisbookclearedupmanyquestionsIhad
aboutcertainaspectsofPythonsuchasobjectorientedprogramming.Idonotfeellike
anexpertatOObutIknowthisbookhelpedmeonafirststeportwo.Ihavenow
writtenseveralpythonprogramsthatactuallydorealthingsformeasasystem
administrator.Theyareallproceduralorientedbuttheyaresmallbymostpeoples
standards.Again,thanksforthisbook.Thankyouforhavingitontheweb.--Bob
IjustwanttothankyouforwritingthefirstbookonprogrammingI'veeverreallyread.
Pythonisnowmyfirstlanguage,andIcanjustimagineallthepossibilities.Sothank
youforgivingmethetoolstocreatethingsIneverwouldhaveimaginedIcoulddo
before.--"TheWalrus"
IwantedtothankyouforwritingAByteOfPython(2&3Versions).Ithasbeen
invaluabletomylearningexperienceinPython&Programmingingeneral.Needlessto
say,Iamabeginnerintheprogrammingworld,acoupleofmonthsofselfstudyupto
thispoint.Ihadbeenusingyoutubetutorials&someotheronlinetutorialsincluding
otherfreebooks.Idecidedtodigintoyourbookyesterday,&I'velearnedmoreonthe
firstfewpagesthananyotherbookortutorial.AfewthingsIhadbeenconfusedabout,
wereclearedrightupwithaGREATexample&explanation.Can'twaittoread(and
learn)more!!Thankyousomuchfornotonlywritingthebook,butforputtingitunder
thecreativecommonslicense(free).Thankgoodnessthereareunselfishpeoplelike
yououttheretohelp&teachtherestofus.--Chris
Introduction
5