Partial preview of the text
Download Master Python in 1 day and more Study notes Programming Languages in PDF only on Docsity!
+e TH. UL TT? HANDWRSTT2N-STUL2 PU THON YOAS TRCLASS GUAT 4e eee eeecessessscessssass Created bu: Senior Sofware engineer + master Python 2ducctror tt “Targe+ Audience: Absolute Beginners +o Zlite Coding Wizards +E Design: Clean Leupurs, Real-World Anctogies, href Nels Pes 2222 22eee se sases see ee sees “Puthon is the language of clarity. J+ doesn’'+ force ype +o translate | | ypur Thoughts into machine semantics; iy lets ypu write ypur *noughts L__ | | Hirectly into executable instructions." | | | AINTRODUc TALON TO PU THON st EWRROMMENT S2TUP +t = Se ee oe ee ee Se ee Se ee ee, See eS Se Se ee. Whe is Python? Python Sa *Anigh-level, interpreted, general-purpose programming language *™* designed wih GN intense focus on code readability GNd developer produchviry. | required. ® K#Generai-Purpose:™™* Une can use it +0 build websites, serip+ system Gdministrative +o5Ks, process big dota, run Ask engines, or program games. ao ae + A Brief History A KK Creator-®™* Guido van Rossum * **EConception Year: ** Late IG8Os Ca+ CWA in the Netherlands) % *®*OFAcial Launch: e% February I9o Ceython O.9.0) * Why the name "Python" 26% Guido was a fain of the British comedy, show A mon+y Python's Flying Cireus"*. J was NOT named after the snake! ede is Why Python dominates the World Why is Purhon popular? | | 71 ’V vviy Righty Readable | | Massive Zcosustem | | A3d/ Dare Dorninance Syntax Vike | S+andard librar | | TensorFlovo, | | english. | | and pip packages | | Py Torch, Pandas. + +4 + Advantages Us. Disadvantages Feature / data scientists. 4. **DevO8ps + Automation: ** Systems GBAministration seripts, aUID-generating documents, Web Sereping (BeaurfulSoup). + 4b 14 Serting Up the Environment 4 4é de A. Windows ednsraliation |. Go +o pyrnon.org/ dovnloads. D2. Dovonload the latest installer Ce.g., Python 3134). 3. RACRATACAL STZ PR Tick the checkbox, ** fad Puthon.exe +0 PATH" A before clicking Anstall. +. Open Command Prompt and supe python --version +o verity, 4h de +e B. mac anstallation |. Snstall Homebrevo via Terminal: “bin/bash -¢ " Courl -£sSL n+tps:// FOLD Qithubusercontent.com/ Homebrew/ins+o\\/H2AD/ instalt.sh)" D2. Run: brew instal python 3. Verify in Terminal: python3 --version 4e 4k 4k CC. Linux ednstollorion On Ubun+e/Debian: bash sudo apt Update Sudo pt install pythons pythons -pip de tt 1S Usur First Program CReunning in VS Code) |. linstall **Visuat Studio Code VS Code)*®*. D. Go +0 2xtensions +ab CC+rtShifttX or Cd tSnifttX) and instal AE" Pusthon! Uby Microsoft). 3. Create a new Fle: hello.py and write the code block below: python +: The Classic Starter Program print" Hello, Puthon Wizard!) RETO con it: ®® Open ypur termine GN run: bo.sh python hello.py ®AQt PUT RE +ex+ Hello, Puthon Wizard! +e CHAP TZR O: SUN TAX BASLCS, VARBABLZS + Op2ZRATORS 4 =222-2scsccsccccsccsccscsccscsee eeccccecceceeeee + 4+ 2] Print, Comments -+ JSindentation + 4t 4k Print Statement The printO — correct swoevTataon | | “— — | INCORRECT SINDEN TA T-LON | | | 1 | -| | if True: | | if True: | | print" This is indented") | | print This erashes!") | | prntL'dlnside the block") | | print" misaligned space") 4¢ 4 gia Venables and Dynamic “Typing Voniables are named references storing memory locatons of | S Aare. python a Declaring Variables username = "Alice! 4k Sxring Csr) user_age = as + alnteger Gn) height = 35.9 4 Float CAoa+) is_actve = True +¢ Boolean Choot) +¢ Dynamical “Wyped: You do not declare variable types explicitly score = 1OO score = “One Hundred! +b Completely valid! Np error thrown. 4+ 4 3.3 Bnpur and Ootpur The inpurL) function captures date as & BES erie ee from the user. python +t Let's Ger User input and convert the Gata type Name = input "2nter ypur name: * age_siv = inputC"enter ypur G.ge: ") age = intLo.ge_s+r) 4 “Typece-sting String +0 integer printCf"Hello Lnames , next year you will be Lage +13 years old") + 4+ 34 Dato “Types oe “Type Conversion Operators Used for mathematical equations: * + CAddition), - CSubtracton), ® Cmultplication), / Floating division: S/D -> 3.5) * // Floor division: S//D -> D) * % Cmoduto / Remainder: S%D -> 1) * KE CByponentiation / Power: D**Z -> a+ + + B. Comparison Operctors Zvaluate expressions +0 “Irve or Folse: * == C2auat +o), t= Coy equal +o) *® > CGreater thon), < CLless than) ® S= Céreater than/ equal +0), <= CLess than/equat xo) + + + C. Logical Operators Used +0 combine conditional statements: * and: Returns True if both statements are True. ® or: Returns True if G+ least one Statement is True. * not: Reverses the boolean state. 4 4+¢ 4+ TD. ddentiry ~+ Membership Operators * is / is not: Checks if both variables point +o the exact same object in miele ™® in / nor in: Checks if G& specific sequence is present inside & collection. te dt Dile Mini-Project: edlnteractive Tip Caleulc-+or python 4k interactive Tip Calculator Python Code printL'=== W2LCOm?2 TO TH2 Tale CALCULA TAR ===") bit = Aoot+Unput "What was the tora bill? ")) +ip_percentage = intCinpusC" What percentage tip would ypu Ke +0 give? UO, 12, 15): ")) people = intGnpusC" How meny people are splitting this bit? ")) Hp_amouns = bill * C+ip_percentage /\OO) +o+0\_bilt = bill + Hp_amoun+ share_per_person = +oral_bill / people printCf"\n Total Hp amount! § p_amount:. 23") print Grand Toro: Sorel bill: O43") printLf'Zach person should pou é share__per_person:.2£3 ") *®*ESample Ourpur:** text =e= WZLCOMmze TO THE The CALCULATOR === What was the total bill? ISO.00 Whe-+ percentage tip would yp Wike +o give? UO, 1D, 15): 1S How printimsal 23) +e Output: ‘Pro’ CS+ep every > characters) printimsgl: tT) +e Qotput: ‘noh+yP’ CS+endord Woo. +O reverse @ string) 4 +t 3.2 key Sxring Methods python text = " python programming is Giwesome " 4+: Strip spaces printrert.senip)) 44 Output: “python programming is awesome" 4k Capitalize + Case Conversion prinrrext.upperO) +: Output: " PUTHON PROGRAMMaVE LS AW2SOm2." print Crex+tleO) Surpur: " Python Programming <> Awesome " 4k Find and Replace print rext+.replace "awesome", "fon")) 4 Qutput: " python programming is fon " +e Splitting Strings into lists words = “apple banana,cherru" splitl"") printLords) 4+ Surput: Lapple’, ‘bonane’, cherry) 4+ +t 33 Zscape Characters ® \n: New line ® \4: Tab indentation A \: Raw backslash 44 44 3.4 mini Practical Case Studies a+ +t 4+ A. AGtomatic 2mail Formatter python raw, _Arst = " jOnW " raw _las+ = " smith " domain = “company.com! +e Clean up input data Arst_name = roto_fAirs+strip©) lowerO \ost+__name = raw_las+.stripL) lowerL) email = £8 Lest _name} S\ast_nome § @ Ldomains " prints f" Generated Professional 2mail: Lemail$ m) 4 Output: John.smith@ company.com 4t +t +t B. Secure Password Checker python password = inpurL"Znter & password for evaluation: ") 4 Check length GNnk character conditions is_long_enough = lenLpassword) >= B has_spaces = "" in password if is_long_enoush GNA nor hes _spoces: prinsC" Password Security Checklist: PASSZD") else: print Password Security Checklist: FARLZD. mus+ be 8+ chars and have NO spaces.") a List LY | Yes | Yes Caditable) | Ues | | Tuple ©) | Yes | No Weekes | Yes | | Ser 3 | 1 | Yes LDypamic) | No Wrique Oniy| | Diet £03 Yes C3.74) | Yes Ckeys Unique) | tess No / Ve y| |_ 4 + 41 Uses Dynamic Arcos) Lists hold collections of ordered, mutable tems. +¢ 4t 4E CRUD Operations (Create, Read, Update, Delete) python 4E |. Create fruits = L"apple", “banana”, "cherry +4 DD. Read prints fruixsLOW +t Output: “apple” +: 3. Update frvitsLl] = “blueberry 4 4. Delete Fruits.removel'cherry") 4& removes specified element popped_item = fruits.pop©) 4 removes and returns last element print fruits) 4 Sutpurt: [apple] 4e 4 4b Common List methods * appendUitem): Adds an item +o the end of the list. * inser+Undex, item): Adds an item at & Specific index. * sortL): Sorts list trems in-place. *® extendLiterable): Appends multiple list items +0 the end of the list. 4 4 4.2 Tuples Cdlmmutable Constants) Tuples store static Sequences tha+ cannot be modified once declared. python 4 Tuples are declared with round brackets coordinates = (40.9198, -74.0040) 4+