Download Python handwritten course and more Study Guides, Projects, Research Computer science in PDF only on Docsity!
PYTHON \ PROGRAMMING \ “2 ay ~~} PyTHON. PROGRAMMING a | > * _Intuoducing Python - i U J > s > Puthon & o Wiah-leve, 1 geneial = purpose, and ve ; Pep ar__puoc Samnining Janquaqge. i> = is berg wd tm Yd development , Machtne learn- ‘ima 4 applied fous , etc, iy a > Pal on programs oye _genenally smaller than the othen programming — languadds . ae Bion raiivievis O have Shs Qivupe Helatively less and the J wt = indentation requirement of the dJangtuate , makes then T ‘ Headable: at the time. { Ug { — >) > Very Simple Syntax.2 Easy to learn, r ER : % we buint (“Hello”) ‘i “ . ~— General puripose aanoiaee 1 { Simple ue Very powerful - : f f «a f : es language\ ~- dv? ; [* Console Application R Scripts ; _ d |— Desktop Applfcatfon a na E> Web Appltcatfon ‘ ; - a [E> Game Development i = B t L-+ Machine | learning" Deep’ learning , AL + Blo data eke 2 CU ij U U = ‘ 2 o ke => Multtparadiqm Support ar 5 T U TY oe Ey [” Puocedural Style Programming —Ifke C~ i. Sy — = OOPs) ke Tava: NE ‘ } be FuncHonal programming Ake Uist. en ) ae a ~ ~j and psaoduce some output - _ —— eens puoduce some output - < Beial Be “ot tg") =: ———= o ° 7 are enjoying Python") 7 Weleome bo GEG = U as) — xs % Hope you are eofoys fag Python T — —s> enddQ end ond sep parameters fn pxint - T T ~ SS ee paint {“Weleome™, end =" "") " putat (“to GPa) _ Outpuk- Weleome to GPG Soe t cored End porameler oreakes © Space and th otells ~ shoutd — v ° be paialieis after tks pxtnting. — t And fn sep paramveten whrasswe have. multiple — T input pavanreteys we use sep to separate tiem. = T T T T Bu default they ane Sepanated by space. —_ J —T ¥
i 4 <= class, “Complex'> 1 s 4 —\n python we use “None” Io indfeate that we don't know § the value yet oy _we dfdn'h.assfan any value to thix . vastable. v : 4 \n_ python there %& no: char type | 4 H rp you want ian yous should & e@ “i sis "gts Te “thar tray (String ts “a Sequence ep chan) Sf o N) ‘ putnt ( type (tr) “ . d= [ 105-20, 307 buinl (type (J)) 1 wt 7 ( oray. and dynamfcatty # add_and vemove Then «” ® Items ave stored fa cont. 4g ~Nuous locaton ) Seeraé % = wre ’ nd Date v.cccccceseeee SN ereerernn verso) (a alin: Uke a Jhb, ie, A 7 1 | af 6 Soe bgt AE ference | you! ares. modify o once you created oO ~~ 3 spl: Inese_ ave fonmutable e) ae $2 {10,80 80,30} 2 (Sek a _collectfon of thems _ - here all thems are distinct t (s)) ~ at Spat (ype). and set UW dike mathematteedl_ ~ ie set) > dx $103 “gta” 90: “ide dreHonaries ore used to do = L J 0 5 x1 psint (type (d)) mappings. Tha ag a x collection of key: value patrs) 7 Eq- items : prices __rollnot name . oe = fe > ° I= Tupe conversion In Python = a ee 7 E 3 Impltcit Expltctk . &q- a=1l0 €q- 42135" + rr a d= t+ dnk(s) > tae Led f= float (4) f 3 uae 5 d= True putt (3) J eratd pyint Ch) > paint (e) > OJP = UNS o}t - WS r - 135-0 €qe dz" eek a me paint (dist (4)) - i pb! paint (tuple ()) 5 pafot (set (4)) ~ o}P- Etat; ‘et, Set, kel, vt] Y Speak ~~ - ha In is Snel 2 nes woes eS sale: i ae vo -_ip- elit stakements - 34 wD. wth, ° ° iy ft _ ghatemeat ~ tn tits tf the condition 4 brue then if “past executes else the _ else past execuker . 3 ‘ Syntax - if condion : § # Statements to execute if } # condtton uw tHue : Flowchayt = N y eat Sp Test False a Expression il Son 5 ¥ , Body of iF > — ) [Statement just below , mi Y + RY | Example - if lo7 54 y puint (“10 & greater than si) if paint ( pao gram ended) O}P - ou guest tart §. ae ended i: y & een cae 4 iF Date wu. . 1f ” A the else -cshabemenk acclh conditfonal if Statement ne addi tHonaf _ Lf _———S o T block of code fe ae at elee . shatement which per Pore u 2 _ { when if tonditfon %& false. i “ jan mae olax ~ if (cond?iton) : - a ___t# Executes thle block if —~. H+ condiion t& teue oo else: a | # Executes thts block if aft F # conditton Uy false 7 Et ct Flowchayt - { Ss 7 eis False “ + True. + ; Sody of if Body of tlre : |< il Et] jStatement just below if ; Example - ot = 3 a t if n==4: Hy i F | pate ("ye") : lee: ; A pint ("No") 5 Example- You can also chatn os one condition. tp. elge statement with more than” z Jettey = "A" 5 i 4 ip detey == "RB": : i pint (" Jetey ts 8") ; * Clses Te detHen earths pint (" Jetter ct) | He a Syntax - if (eonditon) : ; ld Tt he Statement —~; re elit (conditfon) : i aS fas statement 5 { ~ : = m Clee: 4 es statement 3 rt Example - Jette = A" JN Ei ip deen ss BM) | = ! paint (“Jeter UB") 2 | elif’ Jeteu == "ctl i f pint (" Jettey & 0”) cs y elif Jeon == “Att: 9% puint (" Jetten & A") : a f - else: 1 pint (" Jeter isn't A, 8 or c") a + 5 E : ‘ 5 1 O/P - Jeter & A ci ~ - = AuithmeHe openatore — They ane = used to puiform mathem - -atcal operations dike’: addihonal , sub lnadltanes mulHip-~ ” -Weation , divrston. “a There one T ontthmebe opevators tn Paitin - _ ()_ Addition Q Subtraction " @ Multiplication B®) > Divisfon © Modules © Exponentahion @)_ Floor diviston Sorat Me le Hig i. Opeiatr = In tion, +. W.. the addivion operator. It © Aadition. pylt or 5 used to add &_ values. , sis ~.) Exomple- val = 2 "g val2 +3 a Nest val 1+ val 2 ~ Se _puint (itt) att Subtrathon Operator = used fo subtract the second value from ws —F the fivst value. aa Example -_ Val l= $ ~ . val o> 2 a ves = vabl- vol 2 ~~ print ( xe) ~y . @ Medulus | Opetator: - fo the moa ius) operator used to find a the yemainder when first operand u divided by the SS Second ." 20 i + Example - val | = 3 ; vol 2 = 2 J Mea = val\ %o vol 2 JS. paint [ Hes) Pos A 3 | 0) Exponeatfation Operator — ** & used to ratse the tirst _* operand to power of seeond. we Example = Nal 2 = “f “s vab2= 3 3 yes = Voll ** val 2 ; ye print (wes) oe - 8 + oe Seanat S A ™) Date ......sesceree otal | = wink ("The inbolel one eatt than *) NES Sy ao ond and b>O and 670: - ~SJ pata ("Me numbon ¢ ane puts than '). elses beak Pe wint (CA Atleast one number not qreatot than 0”) ma ne 7" v ~~ - The numbers ane reates than O Atleast one number nok. qreatey than o. 5 U ~S Example - _azlo b= l2 “a ‘ —) RR cz 0 . fe a and b and c: —~ paint ("All the numbers have boolean value as True”) —~ alse: pufat ("Attest one number has boolean value as False - Logtead OR - tk vieturns Taue tf either of the operands G True. So 7 + T YS J OfP - Atleast one number has’: boolean value as False. ¥ ; : = — x id _> ; 2 True PR False a bene. sh A AR i ' = yt ~ False Output True Output os 3 Example - a=10 ‘ 1 za + -10 PS aa c= 0 aN Date ....... Ye a>o or boa: ° | u than 0") as pxint (" etthes of the numbew uv grecbe an T else: pain) ("No number & grsolen than _O 3 b>o. or ¢>O:. Tn ue a pyint ("erthe of the numbest es aL than 0 else 4 sie _puint [" No number u grates than o*) i, oP - Etthes of the number & reatty than O =r No number & Greater than O Example - azto be 12 c+ 0 TE aovrborc: PHINE ("Atleast one number has boolean value ag TxHue") ; else: J pytnt (' AN the Numbers haver: ‘boolean Value at 4 ‘ False") t OJP - Atleast one number has boolean value ag True. — legteal Not operator - wouks with a single ZF whe beclean value. %& Tue th Nvisa, , boolean value. fp + sehr ve “False and vice = ‘c Not 9c] : True a False . : : Bee T Sa False ofP True ofp Steraé Date.... => Puthon Membership and Operators - Identih Puthion of fers two membershtp operators to_ cheek 6M valPdaty the membership of a value, Ih bests for membership. Le Ree aegutnte tuth at stings, Atsts or Hupled . _ [ : ee in Operator = She Mm! operator ‘ui ured: to check if a thaw vi AubiNita) element extsts In a tequence or net, 4 Evaluate to “True if th finds the speattied element ina + ¥ v i Sequence _otherwtse False. For eo - t oT | ‘g' in * Geeks for Geeks! #£ Checking SG! fn_string 2 i Taue Y d ‘ ° e tn ~Geeksfor Geeks" cid sick is in string stnee pythe MM tase~ senstHve return Falie es 4 False ‘ a Example = dist! = [ 1, 2,3,4,87 : dish? = 1 6,7,8,4] jor item in Jfckls if fhemcth Stk: a paint (" overlapping") H else» ae pxint ("nok ove rlap ping'’) “ T ng : TU OP - nok 100 j make j not ey not a not id ot mp5 yy Date oe ——— a ie ‘nol | in’ o (a) era lor cy Eva We Mortable in Mme 3) spate sequence, a “and false or me a eee “F ~<" Gopi uF 7 fe Oe > ee Jest = [10, 20, 80,40, 50] =a if (x nol in Jfst): : paint ("2 ts NOT present {n ‘piven Sisk’) _ ~ sites mw paint (x ts puesent in given JPst'') —§ eat Pn lest): je paint (“y 4 y ts present in given isk") else: ~~ paint (“4 NOT puiestnt in given AREY) © ° ° o | oP - a ts NOT present in qtven list. 4 ts pacsent in ‘alven Ifet ~~ paint (ot 4) _& d(x) id (y) g ay :