You are on page 1of 77
PYTHON \ PROGRAMMING ner = Date ... Bs Py THON PROGRAMING ms SCODING BUGS ENOTES GALLERY c- Into ducing Python - be: i — ea &_a_high- level genus = purpose and ves. it opular progr tng. tanga : 2 a! uate veh usd tn Gwe development, Machine learn. - -tag applied ete. eae ee Pytflon fuegrame ae Cd Smaller than the othes jin Hogrammin dong uages . — Puogrammers J have to type Helatively less and the indentation requirement of the Janquage , makes Mena é “ge ——adable_a Me time. Man ~S Veg Simple Syntax. 2 fasy to learn, v oT Cv ~ a rer ay | paint (“ Helto”) > = = General pusipose Janguage ( Simple yeh very powerful, 3 language) s Console Applfcatfon & Scripts iw C= f> Desktop Application : 2 [> Web Applicatfon ‘ ; * E> Game Development FE LL, iMachine Learning Deep! learning , AL, Of dala ef os Z — Muttinaradigen Support = \ [= Pavcedural Style Programming _I?ke C - = sl “s L>. oofs ike Java, © g Functional Programming (tke Usk. f => Portable or Platform Independent = Puoqrams are typreally first compiled nto an Utatermedfate cod¥, then the code Pe yun Me interpseten . —>_ Dynamically Typed. nyna 4y---NP ~= 10 4 ys “qeeks™ 4 uv . ace “python J 7 This featuve %&_dffferent from Ctr b Tava. they ‘ ae stateally Typed i In python we don't have to specify the vatable PY i name. But _the disadvantage & fm this the type & asstaned | ° i v in the vun time" of the paogram , so there avd f paeg move chances of yun bme evror. — Automate Garbage Collectfon ‘ \t _medns when you 4 dynamically allocate memory So you don't have fo worry: to yealfstng — th?s memory . g u \ § => Popular Apolfeatfon Butlt tn Python FF ~— + Youtube y Nettlix',) Quora 2 Instagram , DoopBox x 4 =r__batat O tn thon — % A__funclon & a sek of fnstructfons' that take x some fnput Prom | diing..dose somehiwork on those - wameteds and paoduce some output - > 4 0s al = = int [“ welcome”, end =“ ") = paint (“to GPG") Oulput- Weleome to GPG votre End parameter __oteates _ space and {ft tells » shoutd be printing after tks patating. - And tn sep pavantetet. when we haveY multiple = P—f input parameters we use sep to separate” them. — # f default they axe _Sepaxated by pace . o f AOE) Ab ry i patnt (“1o", “ut , “2o23", Sep =" "y EE Output -. 10-04-20a3 a EICODING BUGS EINOTES GALLERY “=> Nautables fn Python = 2 _Nawable & a name that Outed fo siefen to memory ° ° docatfon. Python veurfable also known at an identi -fiew and” used to hold value» ee In_python, we don't need to specffy the type of voxfable because Python & a finfew language and smant hte __qet fab enoug get __vosia le type - 4 4 } aH I E is coe © xample patce = loo ” tax = (8 = | ° 7 = L total - psitce = _fostce + tax i t ° 7 % putin ( total putce) On ‘ utpub- 1g up 4 How vaufablec wouk ¢ E)CODING BUGS EINOTES GALLERY © 3 4 “= 10 Uo]<~ * Yas “geeks! qeeks |e~ f % z= “20 [ao] z q it we say we=z Fi ‘ ‘ When soft 4 : Nwx 4 but FP We 30 Bole— w : “4 ® ie O}P- ios a Mm & ° s) geeks 4 putnt (2) 20 f . fF puto! (e) 20 “4 4 beet a saad ~~» ee Pathan Dynamically Type eee 5 a Bo m% +10 $ J putat (>) . nk: “geeks” U a is paint (>) ~ 10 ~ qeeks ~y 7 ~ 3 Note - using a_vaniable without assfaning fk cause error. ~ &q- paint (x) » oP- Name Exror : name ‘x' fs not assigned ~— ut) tn Python - 4 3 This functfon ‘u__used to take fn put fiom the user } t Example - name = ‘input (“Enter the name >") 8 ” psiink |" Welcome''+ name) t ole - Enter the name = Himanf Welcome Himanf Example - # Python program tor _additoa ' t | +t = fnput (" Enter Fiect Number :") 4s input (" Enter Second Number : a) m= fat (x) ys int (y) Hes ney F Spat print ("dm vee) vy lvl aoe lyl e blew lel SICODING BUGS EINOTES GALLERY Typed) In Python ~ Outpul = Enter Pirsh Number we rom string . Is d Type & a _bullh-in_function that Hells se h0 wherever we have to A you data lupe of a “vavfable or a value. i Eg a=10 : a (type (a)) = 10'S ‘ re Ciypecay) r C2 9437 puint (type Co) ; O)P = dais, tints § <= class, 'floatts <= class, ‘Complex'> T =n python we use "None" Io Whe value uel or we dfdn'h asstan any value fo thy voufable > g : é 4 indfeate that we don'h know § “ \n_ python there 9% h te no chav ype you w wank ft a. aga, reuld Eg - Sky_= “gtg" (Shing § fsa sequence of chav), F paint (type (atry) J [.10, 20, 30) (_@tray and dynamfe ally patol (type (19) add and vemove thee. st Irems are stored fa conte. ~nuous locaHon) Storae ~ re eae $= {10 30,30}, “difference fy modi Sek_& a _collecHon of _ Ss pint (type(s). where all thems are _ = t and set UW dike mathematical Set) y “ w Oyu », as J ~ dz fio: "gfg’s 20%" ide" J dicfonaries ore uscd to_do a paint (type (a)). ings. Jhe & a 7 3 J collecHton of key value pairs) > Eq- thems : porices rollnot Name. ai > p> Type conversion in Python - Type conversion > se 3 Implictt ExpltetF a fat !0 €g- 4 "135" os J= 1+ dnt (s) > ‘it (0) f= float (4) 3 d= true patat (3) es atd paint CL) 3 patat (e) : $ X opp = us ojp - 14s » u 135-0 fg- d= " geeks” wT SJ » paint ( Jist (8) > patnt (tuple (4)) Py pafot (sek (43) : p= tigi. tet fet ht Mt] a 1's") Date ..... 9-3 a= 20 patint ( bin (a) te = palnt (hex (ay) aaa + b (oct (a)) a | ¥ OP = Obio100_ | URIVE H 0094 => Comments fn Python ~ ate 1 We may with to deseuvbe the code we develop. We 5 might with~ to take nokes of why a J sechfon of sept p>, a for _fnstance. Formulas , pocedures , and sophistcaled business Jogtes ave v iypfeatly explained wfth comments, # TH 4 Single -Jine_ comment - # a Mult? -Jine comment - Hs. oe ie # te a Doc-stying - the string enclosed fn trfpte quol-es 4 that contes _ tmmedfatel after the dettned fm ane catied Python DoestYing, @ ¢ v E =P ib else and eli? in Python - ig a pans %e Shee come _sthuabous in sea) Jife when we need to do some Specific fask and bated on some Speci pte, é onditfons and, we declde whak should we do next. 7 Similarly, — there Come a. sthuatfon Ph potoqvammeng = whee a ‘pectpfe tak & te be performed if O spectPhe ie conditon _& true. & Seat Lh es & ~~ Date . wT auth cated cond?fonad vstatments can be _us conditional So ~ if. elve sel = Nested if s T= pe ait. statements. soem 3 a a 3 statement - tn thts ff the condition 4 true post executes else the ete pout excculer 3 " Syntax - if conditfon + s H Statements to execute ff # condition &_tHue ¥ Flowchaxt — 2 Se Fi Test False ¥ Thue % x. Body oF FL s —S y Statement fust below , Ce ey + Example - if lo 75+ y puink ("10 & greater than 8") F i paint (“ puogram ended) O[P - 10 U__greatty tan S- v Program ended ia J bt SICODING BUGS EINOTES GALLERY Date on. fe ( if-else statement ~ block of code te when If tend ip: Statement the additonal = slalement -whfch perfame! recutes this “block if condiNon & true ele: 7 # Executes thts block if # conditon & false Flowchant ~ 3S t a zy q Example = a= 3 a : ip we=4: eh! 4 pafok (yes) ‘s ; else: J paint (“No") 4 Fxample- You can also chain ifelse statement with more than” one _conditfon. : ¢ Jettey = "An" i “4 BP dettey 22 "a": pint (" JeHor & 9" a else 3 TE deter = ="c", paint (CY detter ey Seeat Sy ™~ we elses Sp iettes = : Span Craton ba") fi dneiu Sue ‘ ele ; Jette unk: AS and ce” it es cao = 3 Fo? - deter UA Nuled_Y Statement - if statement _can_alte be checked inside FY othen_ if statement. Thu _tondiHonal statement G called a —~J_nested if statement. thuu_means that tnnen if condition will be checked only iP outer it condition %& true and by thu, we ean ste _mullple condone fo be sahisffed > gyntoe - if Ceonattion 1): ~~ i, # Executes when condiHonl W_fuue . ip (condttion 2): ~~ Executes when condition 2. true ~~ # Wf Block t end here ~3- # if Block &_end hee Example = num = 10 it num > § : J putnt ("Biagen than Ss") ae it num 2= 18 5 paint (* Between § and (5") — ole - Bigqet than § + oetween § and iS ite eltf Statement - shortcut of theelse chatn, while using if -elff > statement at the end else block & added which “& _ S_peitormed ip none of the above iL- elf ctatement tb __frue. Y Spnat Date .... . Syntax - i? (conditfon) t = Statement elif (condtHfon) : stalement 4 4 ; = L . = Bs) elses « a statement a Et Example - —Jettey = "A" 3 ip dettem ss em: _paint (“Jeter & 9") elif JeHea == “ct: putok ("“JeHer uc”) elit Jette == SA": paint (" Jeter & A") elses pint (“ Jeter isn't A, 8 orc) OP - Jeter GA > Aulthmetfe opetators - They gHe | uted to peiform mathem-_. =aHea) operations Jtke. additonal, subtrackon, muthip. -Itation, division. ; _& That axe 1 onithmetfe operators tn_. Python - = O Addition _® subtraction “_@ Muiiipixation @_divteton @_Modutes © _Exponentiatton @_Floor _diviston i “ay Date I Onevator -_In__python, + 4 the addilfon operator. It TW y 5 0 tet odd 2 values: si 2 ~~ Example > voll = 2 val2 = 3 SS ess vol dt val 2 SF _puint (tt) —j_ fh + £ Sos SubtracHon Opevalor_- used fo subtract the second value from he fick value. 5 on -_ vod 3 val a2 2 Ss des = vobl- vad ~~ paiat ( He) So as Modulus Operator - sJo u_the mod ulus. opuigtor used to find ~yhe_Yemainder when {fest operand t_divided by the ~~) second 4. Example — vol 1= 3 vol? = 2 J dea = val l Yo val 2 ~S_ __ paint 9) > 0) Pace h +O Exponeatfatfon Operator — _** used to raise the finch _y operand to power of steond. x Example = voll +2 > vab2= 3 > ves = voll ** v2 a. prtat (ts) = oe - 8 Spnrat Date ... = a © Floor division - // %s used fo f¥nd the floor of Phe quolien) when first operand ts divided by the second. Example - valt s 3 as ea Es ae b val? + 2 % iz, Ne Si Lb Mes = val t// vat2 . -F patot (es) _ sees. - pee ojp - Pa Ba __# => Pulhon Logfead Operators with Example = : Opmators aye used to _patform _opetalfons on values and vaiables. Jhese ave the spectal symbols that cory _ouk arithmetic. +__and _Joaical computaions. Ihe value tht operator ~ opexata_on_& 4 known as. Operand . : SICODING BUGS E)NOTES GALLERY, ~ logical _and = logtead ov = legfeal not 4 A 24 7 Logfeal and~ True t} both the operands ae fue. >and y. X and y False Toe 4 oe True 1 False_Oulpub mass BS che cow Taye 4 + v ~ fatse~Outpat Forre—Oertp et Example - a=lo A b= 10 4 c= -lo 4 th a >0 and b>O: SS , ne ees piel. (ome oe WE es fparons and b> d 6 70% es ie : SF pata [2 oe mabe ou ane aye _guieator than") elves. ~ tol Tepmeast one number & 4 riot greatut thanso°} are greater than 0 FP = The numbers Aileost_one_ number U_nok geal than o. ~- ~¥ Example > a=to Se eR i cz 0 if a and b and c: i paint (“ALL the numbers have boolean value as True”) Sy 3 — >> else: Ss ~~ puta (“Atkast one number has boolean value _as False O]P - Atleast one number has” :boolean value _as False. =e ~~ Falte Output Tue Output ~ > Example - azto : pS be -10 es eae c20 ™ if a>o or boo z . then of the _numbew i agrestet than 0"). na ben ts ps s th b>o or emo: me: : z _puind ("ef Mhey of the numben gusset than 0") else —-paint_{"No_number lu greaten than 0) DIP = Eifhey of the number _U Greater_than o : No_umber i _greaten than 0 — Example - a=to +. brie 4 =o } iP aorb ore: pufot (“Atleast one number has # Tue") _ boolean value at else: pink (“AN the numbers have boolean . False") Value at S/P- Alleast one number has boolean value as True. | = Logieal Not operator — wouks with a _ Python Membership and Adentthy Operators - validate the _memberthiy ina sequence such at Shing Afsts or tuples . im_opevatoy = Jhe Mn! Python offers wo membership operators fo_check es of a_value, It tests for membersh, operator ‘u_used to check it « thera / substring | element Evaluate fo “true jf th finds. the apuitfied clement fra existe tna sequence or not, Sequence otherwise False. For eq - U | “Gy in_* Geksforqeeks! tt Checking. 'G! fn_shein | “Laue g d LL 4! Greksfor Geeks! %# Checking ‘9! fn strtng sfnee pythe tk case~ senuthive, veturn fale g ig False " Example diskt = J1,2,3,4 23] 4 isha = [6,7,8,9] them tn Jct: bs if fem tn Stet: i — paint (" oveilapping”) ; v elses paint (" not overlapping’) i * 4 nok 100 i nok Te a not a nok 4 oh t int opevalor = no! J Vasfable inthe does not fie J false other Example >. R224 ts a id _— ys 20 wt a ie “Yet = [10,20, 30,40, 50) _ = E(x nol fn dist): a ft NOT nt tn ven disk _pulat (. ats passe gf sk”) elses ace i 4 fs + in ven Jfst" int ( xis paesen i gis fs ‘) ety tn Wt): aint (“y fs puesent fa given Afr") -s else: ~~ patnt ( iy is Not puiesent fn given Ate") = °, ©. ° 2, OP - _% fs_NOT paresint In_given_Iist. ~ y fe present fn _afven_Itet SS wt v - \dentity operators - Thy ane used fo compane the object woh’ the obfeds ave actually of the same data type and shove the same memory Jocatfon. a There ote dif wank identity operators such ad Nis operator - Evaluate to True ff the vanfables on efther side of the operator joint to fhe same object and false a} f othutuice. Example - me S 43.5: portnt (oc & 4 fd) fa Sport } OJP = 1407045 86672032 —— isin SENS Ie, both the vay Hore, tn_the S asttgned to th fi “and which? why eure Ty Ss rot’ opwator - Evaluates fo folie ff the vaxfables on _effhen | stde : of the opwiator _pofnh te a ditfount objeck_and te othuwise, 4 Example. ~ KES S2a0p 4 iE (type (oc) fe nob int): | a putnk ("true") . a elses = paint ("fale") = b # Patnts Taue = & Ke Sb iy 3 It (type (x) fs not int): i . “ " = paint (“true”) i else: & t punt ("false") a . '. : > 1 ¢ 7 i ofP- False a Taye) 2 => Python Bihwise Operators: — & Operators are used to perform opeialt e4__ gS ie ea an perfo peatons. on vafu axiables . ese ane the Spectal tymbols that cawiy out _anfthnee and loatea) computaifens. he value ahd operator opevales on is_known as Optrand . i : i wy

You might also like