0% found this document useful (0 votes)
2 views18 pages

python notes

Python is a high-level, interpreted, and object-oriented programming language created by Guido van Rossum in 1991, known for its simple syntax and versatility across various applications like web development, AI, and data science. It is free, open-source, and has a large community with extensive libraries, making it popular among top companies. The document also covers Python's installation, data types, functions, modules, and file handling, along with examples and best practices.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
2 views18 pages

python notes

Python is a high-level, interpreted, and object-oriented programming language created by Guido van Rossum in 1991, known for its simple syntax and versatility across various applications like web development, AI, and data science. It is free, open-source, and has a large community with extensive libraries, making it popular among top companies. The document also covers Python's installation, data types, functions, modules, and file handling, along with examples and best practices.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
PYTHON PROGRAMMING 1. What. is Python? . Python is a high-level, interpreted, and object-oriented programming language. * It was created by Guido van Rossum and released in 1991. . Python is easy to learn because of its simple and readable syntax. « It is widely used in Web Development, AI, Data Science, Machine Learning, Cyber Security, Automation, Cloud Computing and Software Development. Za Why Python ? Easy to learn and write. Simple and readable syntax. Free and Open Source. Large community support. Huge collection of libraries. Platform independent. Supports Object-Oriented Programming. Used by top companies like Google, Netflix, Microsoft, Instagram, Spotify. PYTHON PROGRAMMING Description Python has simple and readable syntax class, objects, inheritance, etc. Python is a high-level language, close to human language. Python is free to use and open source. Python is platform independent. Tt runs on Windows, Linux and Mac, ; Python has a large collection of ae Stet Urey asa ce 4. Applications of Python + Web Development + Automation / Scripting | + Artificial Intelligence (AT) + Data Analysis + Machine Learning * Game Development + Cyber Security * Cloud Computing Ethical Hacking | | | Genk Bae : —+> Python = Simple + Powerful + Versatile | yee s ape Page 2 |___ PYTHON PROGRAMMING | 5. History of Python Python was created by Guide van Rossum. He started working on Python in December 1989. The first version, Python 0.9.0, was released in February 1994. Python 2.0 was released in 2000 with many new features. Python 3.0 was released in 2008, which is fully new and not backward. compatible. + Python 3.x is the latest and most used version. + Large community and support * Many built-in libraries and modules. _* Platform independent. + High-level language. + Supports... multiple. prograsaming _paredigns. + Increases productivity and reduces development time. PYTHON PROGRAMMING “** 4. Installation of Python Go to www. [Link] Click on the “Downloads” menu. Download the latest version of Python. Run the installer file, Important: Check the option “Add Python to PATH”. Click on “Install Now” and wait for installation Click on “Close” after successful. installation, Verify installation by opening Command Prompt and Senn ene python --version —> Tk will show the installed Python version. | 8. Python IDEs (Integrated Devel Environment) } © IDLE ~ Default IDE with Python installation. Simple and easy to use. + PyCharm ~ Powerful IDE with many advanced features. + VS Code = Lightweight and supports many extensions. + Jupyter Notebook Used for data science and machine learning + Spyder = Geod IDE for scientific programming | 9. Writing and Runnii thon ram Example: @ This is a simple Python program print ("Hello, Pelt) 4. Open any IDE (IDLE / VS Code /'PyCharm), 2. Create a naw file with .py extension PYTHON PROGRAMMING “*° 10. Variables in * A veriable is @ container used to store data values * In Python, we do not need to declare the type of variable Tk ia automatically taken care by Python, «The volue of a variable can be changed during program execution © Variable name must stark with a letter (a-2, A-2) or underscore (.) * Variable name cannot start with « number. © No special characters allowed except underscore (_) # Python is case-sensitive (age, Age and AGE ore different) Example : tame (starts with number) rame@123 (special character) ttotal-marks (hyphen not allowed) my name (space not allowed) lage (special character) it. ing and Assigning Variables Ty Python, we can declare a variable and assign a value in one line 12, Multiple Assignment We can assign values to multiple variables in a single line. Examples : 1. Aaslgping some voles atbeceS 2. Assigning different values yea + 10, 20, 30 print(a, bc) #555 printlx, yee) # 10 20 30 He Variables make our flexible and reusable. * progrema 1 Page 6 i PYTHON PROGRAMMING i 1 13. Data Types in | + Data types specify the type of value a variable can hold. © Python has the following built-in data types. [Integer Gob munber) [10,5 0, 00k | Fating print rumor | fal ae ee Fe et acs tin a awe Rnd te [ist | Ordered cellction (matable) | C3, 2,9, AT | tit tuple | Ordered collection (immutable) hy BALERS {1, 2, 3} | st rr e+ (1, 2, 3) f* (4, 2, 3) © tuple c= “Monika” # str g* (1, a, 3} © set # bool h = (name's Monika’,'age’:21} # diet | 14. Type Conversion in | © We can convert one data type inte anther date type. (+ This process is called type casting. a Cae ta Lae) | ere te srg | rt) —> 0" | PYTHON PROGRAMMING me! arb akb arr bo anh ate 5S an 5 a> 5 and b<10 —» True # Logical 1 [Link], Floss Slatemanks for & in ranges, 8): print (i) PYTHON PROGRAMMING Page 8 | 17. Functions in Python © A function is a block of code that performs a specific task. © Tk helps to reuse code and make programs more modular Sylar Example : def function_name (parameters): def add(a, b): # code to be executed return a + b return value result » add(S, 3) print (result) #* Output: 8 18. Th Functions 7 19. Modules in Python © A module is a file containing Python code (functions, variables, classes, ete.) © Tk helps to organize code in separate files and reuse it @ This is @ single-line comment oS a print (“Hello”) # This prints Hello print" ot {Ye Functions, modules and comments make the code organized, + \ readable and easy to maintain, | PYTHON PROGRAMMING et | 19. Tuples oh Python + A tuple is an ordered, immutable collection of items, + Taplas ore written using parentheses (), t+ (40, 20, 30, 20) pri (A, enunt(20)) #2 H a) | Rebene inden of ee Ta ee print (tindan(30)) 2 i ae jeer fe tate | : print (len(t)) ee eS See . print ment)? #0 | | print Gmin(t) 0 [ment | Returner ae meet) mvin(t) 20. Sets in Python =A nat ie an unordered callaction of snigon hams Sele do rot allow duplicate value Lo t+ 4, 2,9) sada) 0,23) t comowe( 2) *a4) sdinardlS) 0 14,3, 9) popped spp) Oe Dat (random) 4,2, 3.4) pris) © remaining vet dear) ) © A dadiarary tow clletion of hoy-valen. pair + Didienarie re muale and unendered (hefere Python 3.7) freee Pred getC'nane')) Monde Prinh Cd bagel) ak tga) pried eannl)) Het tes) Prekld stome()) © dit tama C3 | PYTHON PROGRAMMING = ®**° | 23, Functions in. Python a eee print(Len( “Python”)) Function print(max(10, 20, 5)) def add(a, b) 2. User-defined wee aa. Function Gables ace print(add(S, 7)) 3. Lambda square « lambda x: x oe Function yrint (square(6)) PYTHON PROGRAMMING = Px | 24. Modules in A medala 68 fila containing Python code (Functions, variables, clases ate) | © Modules halp in ergunining coda inko separate files and promata ressabili + We can import modules and ne Unsir condanta in sur program, File: math stil py (Moda) dof add(a, &) retern ae dof square (x): return PL = 3.1606 import math utile Unprting wade ramttt + math ite od (S, 7) randild + math shila. cquare(6) Print ("Addition «*, result) print ("Square #*, remlt2) print ("Valun of PL =", math stile. PE) Oates: ‘Addition = 12 Square = 36 Walaa of PE = 3.106 Explanation AA pushage io © ellen of wean, © Packages hap in organising ralated motilen in w directory hirarchy © Pechages are imported just Uke monduinn Creating and Using Packages li >i. tachi (mk PYTHON PROGRAMMING 25. in ‘Step Explanation 2 Grate a dreary Cake name) | MRIKAS mcg! (Th le a page) 2. Add init__.py i Pe 1 Alla Python Wha this retry antery = 3. Add mote ry fas) isin | mangle: apace / mgd py the puhege S Tapert ond aee Sxytan: from package same onport made name he modules From pacinge mame sutedin ame input same 26. File Handling in Python protmgradue adsl 10, 20)) Wate, Moi! » © Fila handling allows so te create, read, wile, update and delate files, Sao Pa ee © Python prods talh-in firclona to work with fl aa hacmeaee Opening « File rend(n) and fink = charac Mole Desrption | ‘Operation readtinat) Raa ona Kw tad eats Que fe by cary Oe hd ecdinaet) Rind oh tn wo 0 te . writer) Wha tring fle tain uk) a a of ie ima) Claw te fle ceshpes) Min fla pier to pion ok Return aaah file poictar trwreate() — Trwrrate the file 3. Appending to Fie © Ranting Line by Line £ © open (date tat, Ma") £ + pent “date tat” y toeeak yeas dude © Sanit) foneita(* Valen « sie dnl") | | for tine inf: f-sviio "Fla handing in oany”)| | prinkComstant) fstenat) eR ine.) Feel) Fidel) rrr Fielee) PYTHON PROGRAMMING = 8 per prink(” Romt =", «) ZareDiinrer Outpt Prt” Cane dvd thy sere!” Coeh dda by sort prinkC*Didalon sctmmnafel.”) Tee ee: hag a) Common Excaptions ais meeieonacan ' ' | Excaphion Description i Tes 1 | | ereBecaeniover | Sind wher Qasim w actin by ere ems wie | 6 ea tine | |e SS ace \ — | bo mapentiny” | CS a | Mach te rece, { POM) Oo at defo hoes i teat = 12.0 vole | 1 Prete memninghd | { Song ' ect © Bhi fae and ne ents Gen bred ge | Baw Syntan || [eolik = Ceopromion for tom ty Aree contin adel Uh es (mei, pt, od wena’ Te, 7 [sire = (rf rage) for 6 ge 91 Sel } © Ded plaation Tetle a.m ipl Oh 28. OL 6 F_Lembda Function (Anonymous Function) + Lenin Forclione ore snail anamramos. Farsi © Defined sing lambda Neyyuerd. Useful for shart aperations yeehuly Hi DD ad ecemsailesien 16k Limshae Seteiek “eae gl ok inte ook ine: Use in Badin Functions ht ng ht men srtCinyclamide 2) 0) prt) enn Fn mee wing late sacrum © mado. Megs 2 9) a) os PYTHON PROGRAMMING = &* 30, Map(), Filter() and Reduce() © Whe are ballnin Aighar ear fanos © Thay or ssa th tabla Lhe et, Rape, ae.) and ofan © They help to wile ean, shart and efficient ie, | | a | Filters tens of a arable iB fier sajet | filter Oak caltely © gen filter (Furultaon. derma) | ue pak asics | Avge « futon | adainly te te thems | reduselfiion sentence} A single vale = the fine retool) | Fo nena od ware | saeco 6 tage fk. soma (From fonctens module) | [tang mpl 2 Wing Fite’) 3 ng ott it Note | Liam et a8 87 me (8 OD from forchenia impurt reduce || # mapl) and filler() | ef oper def eenle) moma + (1, 2,3, 8) [este irate | | wns oe Pten a BDO af tats. 9) | Comet tok | | sqemre = LatnmpCnquare, muma)) | | esmns = Latter Ge cnmn, mama) rine ey fo ome romite. =f protagares) Print Geren) seca = rata eed | ty race) eaquione | print all) | oh beak tow: \ 2 | denede Ga Hiss 620 Bee es ]| ‘aaatiwr to | See i 5 J | pew | Bi Classes and Objects in Python - © Claman in Mamprink Camplate) for erunting ohyanln © pet on ctor of 0 dee © Ghnene bap in erpining. eobe and edn OOP eon | cilay ota so bef agegtntt) - Pes hare | (sah met”) i ocuiechciaen Mgt | teeth ge) verte mS eet a'd © ein) ba the construct | “ © vite sare date | ‘aden Mine", 20, 104) © Malte dei Sener | © Cabling matt © hata ars weatad wing | sh daphagt - dees the relnneld ey 32. Inheritance in Python © Leheritenee allem = lines (ubild alma) te inherit attrilnstes and mathadle frum anther clase (parent sass). © prenten cade reseblily and experts Navona raion } | | Sent thee 1 Sage Laotian 2. Alte Iara 2 Alken Iahesane Moen brome 5 Apel tte l ae * ee | Hie Thane concepts are powerful tools in fython. Practice then wll to build efficient ond reusable programe ir PYTHON PROGRAMMING Pais pe |S npiats wm mie ps fon oi he png gt © ee ete ks re Fen ok tn 2 tei fo pe import ow Carrant sarbing dicstary print "Carrant Directory”, at gutemdl)) ak fl an flere print("Canbanta:, an Listdte()) 4 Crate a nee dirmctry so dir ("Dome") print"Creatad "Dene" directory") 4 Raname « file (aomame Fla "date tat! ests) on panne dain at” “hana a") 4 Ramevt 1 fla (nama File “hang. tot” ena) ot renee et") | e © amene sop dirctery svemdet idle”) | | fm ore Teecoad pas uy | | Pint Sperling meme“) Ded (oometed | att 0) . ow ee | | Cententas Ut tnt", natant Dae) 4 Working with JSON in Python + SCN Camoge Ohta) a 6 amit ae forma © Re eng be va ead we fr Nena ad mei, ood Afton Nae halt tn Joon male te werk th JSON at SON Methods — Fg epee Crags) ae pains wad math dren inti seg Sneaks et eninege Common Regen Farehions “pet Se nt mete a ne a wie” | | Ey, Fk ona etet2 gel on, Fara eee fe tet 1 Fak lh apts (Gro mente) seresezie Poe > re fdr ae, At . Sorenteee payin eine ad Phone a waennnee i* Va eee Coe * | real-world appliations (de automation, data processing and web development Finn i a it ctw | (he sges ts was tp (hm rr mee bo tends rere den offinenty [A] Prestine meperty be bald strng prayer side co PYTHON PROGRAMMING 36 Working with Date and Time (8 he Nae nln mle lle akin No we th dite ad ee + hee formating, sling a mandating dee a ine aa tyne gpm 37. Virtual Environment in Python te ree a tale Ryton eminent fr 8 pry les naeates Aegan are iy te feet pt Gonman Commands (ssiog vane) prott — Exar ot a wine banding deine opt deehne, te, Kewl ~~ baa peta wang © are det wd te Sy, os, a dal mt Priel "Carreat Data & T°, see © Caren dete Anoy = date tek? ret Tay's Dake 7, eg? © Samah tate et Fema ate *, a efnat Me 83) at 0 dap mre te Prendvip givlgar errr feathas sho Bing +, facta) | gee ge | cee conchae palnbe meee oe Fs ext | Sat Coorg) vent Sate A Rng 2020-08-28 8 22 120886 Tedag's Sale 3004-08-25 Formatted Date © 208-2008 ate afar 10 dings 2020-08-08 Mr rapa gum | 2008-08-28 W698 a2 HEIN PYTHON PROGRAMMING 39. Tterators and Generators in Python © Reale we ciple Wak sine gn te bres Uren atthe sone Enanpla ts Using Serer © Gerad are 6 sng Se aren rans ing fictions © Thay wre my offen wk al fr larg ak mye ONO ot = erm tit) prinblneett)) A erate me cations (ihe ok tage, ct et, “ ut tnatsay) 08 posteettad) heer ode . aig yt etna Rivets fr larg dataete acticin fete ree prt C"R are the! \ y Tpeuiecs ao eo maa betel, nnn + Ratan ee lens of eed meen se» Artery Ek 5 Thay Ny ory Lrg pact i, | cps hee es Ore my | + | eat tate, 0: be pattem (art a mts es } care Fite, ewe | | retlngmtdn petra! | @ crip. ny i | petlnpette FD of semanas? PERS aly | re |. ‘lle, Renta! ng page - sasise | | fom enter aap, seg . prea lmathage a4atS. 1) ou Prothetrogeys sheathie"D) MEAD | 41 Unit Testing in Python | © en lg Se fas of ty naa dep «pape | Sacco poe \ Alen Nas le mle aa 2 Bihan & cenetey code eoresnae end diy ane Enargle wih unlent [6 ee meg apt mans nly sans a) am Sette Yn acca Sica ttt, 1, 2 | nt Senate Seat ater: 90,2 oe rn) * hte Sete Gc Mhe, Bek Ve Gita? Se Cyaan ta Analyst _ oc

You might also like