WatchthisPythonTutorialVideoforBeginners:
Pythonisalsoacross-platformcompatiblelanguage.So,whatdoesthismeanWell,youcaninstallandrunPythononseveraloperatingsystems.WhetheryouhaveaWindows,Mac,orLinux,youcanrestassuredthatPythonwillworkonalltheseoperatingsystems.
LearningaPythonprogramminglanguageisfun.IfyoucomparePythonwithanyotherlanguage,forexample,JavaorC++,thenyouwillfindthatitssyntaxisawayloteasier.Youalsodon’thavetoworryaboutthemissingsemicolons(;)intheend!
Supposewewanttoprint“Welcometotheworldofprogramming”onourscreen.Let’scomparethesyntaxforPythonandJava:
PythonSyntax:
print(“Welcometotheworldofprogramming”)
JavaSyntax:
Now,inthisPythontutorial,wewilllookattheproceduretoinstallPython.
ThetwoversionsofPython-Python2andPython3arethemostwidelyusedPythonversionsandtherearemanydifferencesbetweentheseversionswhichareasfollows:
Youcanconsideravariabletobeatemporarystoragespacewhereyoucankeepchangingvalues.Let’stakethisexampletounderstandvariables:
So,let’ssay,wehavethiscartandinitiallywestoreanappleinit.
Again,aftersometime,wereplacethisbananawithamango.
So,herethiscartactslikeavariable,wherethevaluesstoredinitkeeponchanging.
Assigningvaluestoavariable:
ToassignvaluestoavariableinPython,wewillusetheassignment(=)operator.
Here,initially,wehavestoredanumericvalue->10inthevariable‘a’.Afterawhile,wehavestoredastringvalue->“sparta”inthesamevariable.Andthen,wehavestoredthelogicalvalueTrue.
Now,let’simplementthesamethinginJupyterNotebookandlookattheresult:
Assigningavalue10toa:
Allocating“sparta”toa:
AssigningTruetoa:
Goingaheadinthistutorial,wewilllearnaboutdatatypesinPython.
Now,let’sunderstandtheseindividualdatatypesbytheirimplementationintheJupyternotebook.
ThekeyconceptsofOopsareasfollows:
Let’sstartoffwithanexampleoninteger:
Here,wehaveassignedthevalue100tonum1andwhenwecheckthetypeofthevariable,weseethatitisaninteger.
Next,wehaveanexampleonfloating-pointnumber:
Thistime,wehaveassignedthevalue13.4tonum2,andcheckingthetypeofthevariable,tellsusthatitisfloat.
Finally,let’slookatanexampleofacomplexnumber:
Here,wehaveassignedthevalue10-10jtonum3.Now10-10jcomprisestwoparts->therealpartandtheimaginarypartandcombiningthesetwogivesusthecomplexnumber.
Now,let’sstartwithPythonStrings.
Now,let’sseehowcanweextractindividualcharactersfromastring.
So,I’dwanttoextractthefirsttwocharactersfrom‘str1’whichIhavecreatedabove:
Now,similarly,let’sextractthelasttwocharactersfromstr1:
Now,let’sheadontotuplesinPython:
Let’screateatuplewhereelementsareofthesamedatatype:
Now,let’saccessthefirstelementfromthistuple:
Extractingthelastelementfromthistuple:
Now,wewillgoaheadandlearnaboutPythonlists.
Now,let’screatealistwithdifferentdatatypes:
Now,let’sdosomeoperationonthelistwecreated:Fetchingthefirstelementfromthelist:
Addinganelementwhileremovingtheother:
Thebelowlineofcodewillreturnthelengthofthelist:
Thiswillreturnthelistinreversedorder.
Now,wewillfurtherlookatPythonSets.Get100%Hike!MasterMostinDemandSkillsNow!
Everyelementinasetisuniqueanditdoescontainduplicatevalues.
Setscanbeusedtoperformmathematicalcalculationssuchasunion,intersection,anddifferences.
Creatingaset:
1.Add:Thismethodaddsanelementtothesetifitisnotpresentinit.
2.Union:Itreturnstheunionoftwosets.
3.Intersection:Thismethodreturnstheintersectionoftwosets.
4.Difference:Thedifferenceoftwosets(set1,set2)willreturntheelementswhicharepresentonlyinset1.
Now,wewilllookatthePythonDictionary.
CreatingaDictionary:
Accessingelementsfromadictionary:
Removingelementsfromadictionary:
Replacingelementsinadictionary:
Weuseaconditionalstatementtorunasinglelineofcodeorasetofcodesifitsatisfiescertainconditions.Ifaconditionistrue,thecodeexecutes,otherwise,controlpassestothenextcontrolstatement.
Ifwehaveablockofcodethenstatementsinitwillbeexecutedsequentially.But,whenwewantastatementorasetofstatementstobeexecutedmultipletimesthenweuseloops.
loop:WeusethisloopwhenwewantastatementorasetofstatementtoexecuteaslongastheBooleanconditionassociatedwithitsatisfies.
Here,weknowthenumberofiterationsunlikewhileloop.Thisforloopisalsousedforiterationsofstatementsorasetofstatementsmultipletimes.
Thistypeofloopconsistsofaloopinsidealoop.Itcanbeforlooporcanbeacombinationofforandwhileloop.
Now,wewilllearnaboutuser-definedfunctionsinthisPythontutorialforBeginners.
Inanyprogramminglanguage,functionsareabetterandsystematicwayofwriting.Functionsprovideusthelibertytousethecodeinsideitwheneveritisneededjustbycallingthefunctionbyitsname.
Syntax:deffunction()
Goingaheadinthistutorial,wewilllearnaboutExceptionHandling.
Basically,anexceptionisanabnormalconditionorerrorthatoccursduringtheexecutionofaprogram.Wheneveranexceptionoccursinaprogram,theexecutionoftheprogramhaltsandthefurtherinstructionoftheprogramsarenotexecuted.
Weneedtohandletheseexceptionsinordertoensurethenormalexecutionoftheprogram.
SomeofthecommonexceptionsthatoccurinPythonprogramswhileexecutingare:
So,inPythonweusethetry,catch,except,andfinallytohandletheexceptions.
InthePythonprogram,thelineofcodethatmaythrowexceptionsisplacedinthetryblock.
Thetryblockshouldhaveanexceptblockwithittohandletheexception.Asanillustration–Ifanyexceptionoccursinthetryblockthenthestatementsintheexceptblockwillbeexecuted.
Syntax:
try:#lineofcodeexceptException:#lineofcode#restofthecode
Example:
Inthiscase,theelseblockwillbeexecutedifnoexceptionoccurs.
try:#lineofcodeexceptException:#lineofcodeelse:#Ifthereisnoexceptionthenthispieceofcodewillbeexecuted
Ifwewantapieceofexecutablecode,whichwecannotskipunderanycircumstances,thenweneedtoputthiscodeinthefinallyblock.
try:#lineofcodefinally:#Thispieceofcodewilldefinitelybeexecuted.
ThesearesomeapplicationsofPython:
YoucanstartlearningPythonbyreferringtovariousvideosandtutorialsthatarewidelyavailableontheInternet.ThisbestPythontutorialismorethansufficientforyoutobecomeproficientatPythononabeginner’slevel.
PythonisanObject-OrientedlanguagejustlikeJavaandC++.Ifyoualreadyhaveknowledgeofworkingonthem,thenit’sveryeasytopickup.However,ifyoudon’thaveanyknowledgeofOOPs,itisstillveryeasytolearnandworkonduetoeasysyntaxandhighreadability.
Owingtoitsdynamicfeatures,Pythonhasbeendominantlyactiveinthemarketplaceasanextremelyeasy-to-usehigh-levelandmulti-paradigmprogramminglanguagewhichisusedforapplicationdevelopment.Asaresultofitssupportivecommunity,Pythonwillmostlikelycontinuetodominateoverotherprogramminglanguagesintheupcomingyears.
PythonisbetterthanJavabecauseofitseaseofuseandsimplesyntax.Also,Pythonismoresuitedforquickprototyping,meaningthatPythoncanreducecomplexandlongprogramstoshortandcrisponeswithmorereadability.
CourseSchedule
SahilMattoo,aSeniorSoftwareEngineeratEliLillyandCompany,isanaccomplishedprofessionalwith14yearsofexperienceinlanguagessuchasJava,Python,andJavaScript.Sahilhasastrongfoundationinsystemarchitecture,databasemanagement,andAPIintegration.
CoursePreview
Expert-LedNo.1
PythonTutorialForBeginners
GetOurAppNow!
Courses
Tutorials
InterviewQuestions
TopTutorials
TopArticles
TopInterviewQuestions
Address:6thFloor,PrimecoTowers,ArekereGateJunction,BannerghattaMainRoad,Bengaluru,Karnataka560076,India.
Disclaimer:Thecertificationnamesarethetrademarksoftheirrespectiveowners.