Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

Python Assignment 4

Uploaded by

Amruta Avhale
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)
19 views

Python Assignment 4

Uploaded by

Amruta Avhale
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
You are on page 1/ 12
— > §A Function ‘is a ble Alssiqnroent oo - 0+ | | | | 2)_pefine Ranetion - : ce of code used te define | a speéific et tosle “A function: Ss.ca. blecle of exgactixed , veusable. code that “is used te perforrn & Single ye a acon - ere are oo type of Functions z- HeGvit In Punch ony » | '2) Uses! Define Function + Di How +o create _usex- defined function fn python . —> - Usex defined functions are reusable code bledky | | ther onby need to. be iwhten once they “hey cor be used eeabtiple time - = -Supatax For creo ng Function :- 2 | NT dee Fundion. name (poremeters): iu doesting ” i stakement { Stokement 3 Tetum (oC 4) > Example :- deg_add (a,b): vetum a +b result = add (314) pant (vesut) ~outpot - Shrikrupa 7 ™ oe Dp Bp io. she concep oF [pT OD EAENS passing to a a nr Function . TParamekeys are the names usesl when defining a Tfanciten of a methed § Sato which argument oSill be mapped - 1 Perameters ore “input +o he function. FF a_ function | Contains. parameters that at the me of counting compulsory we showd provcle vale, othesdise. woe dll get an evror- \- The parameters ave specified Aa the parentheses. T wd can qwe any numbes of aramekTs. but we hove to Separate” shen oSrth “cornet | 4) eet wore use of Lambda function “in python . - Anonymous Functions which ove also catied Laan fanctYon because they a2 Not declared oSrHy dhe standard clef beyword - Ft is defined ufing! lambda Reysner - lambda functions are often usecl when you neeel for a short penod of time F a Smple function named function ufing J you don't} want te define a dee - psqntax :- lambda arguments $ expression : = (lambda functiong can take any nUMbes OF arguments , bur they can onty have one expression A | oo Shrikrupa | Scan python funchen return the value 7 gash fy your answer rth example - : — |- Yes Python Funckons can yetwro values ding +h. FHUM Statement . |- when a funcfion Gs called and 3+ encounters q TeuM statement A+ Simmediettely exttS the Function and veturny the value sped fiecl an | she vehurn gtaternent to jhe caller. = Example i der add (a,b): zB Test = arb Tetuxn resut+ Survesulp = add (3,4) pant (sum resuh) = FN these example, the add funcion takes foo payameteys aiand 6, caladates 4hd4 gum £ Shen _veturns the Test ding the Tehuan Stotement - =We con also veturm putifiple vale, From a Function - i 6) | Explain jocad variables global vmable Sith siitable example - — 18) Local varralslen :- [= The local vanables ave yatables that are defined within a fuadion and are onty aceesSible Sto thot function's SOpe - ’ Za - They ave created when +he funcion Se cated T | Shrikrupa Tand ave destroyecl when the funciion exis local variables cannot be accessed Prom. outside the function . - They _ove useful Por stoAng temporary deda or for perfornting calawtotions atin the fa function oitthat affecting, omhey ports of Prog vem, F 3 Example. aS deF my -Fanetion (+ — Azl0 sb This is a tocal vamalle- 71 pane ( “Ensidle the function ais: x) rey FON enc ) 2) Globol Vomableas . . yanables are the vorables which ave - Global declared oursicle any function - gratement 0 the They ove accessed by any, 5 entire. ancien q Global vomables ave afive Fl the end of the programe « Global venable is destroyed when the enhrve rogram is perm note + = Example > ~ g\ebol = Nat = 7 leF oo = Function ¢) > pwint (YAnsile the Function globe yor ‘is: global 0) enyj- Fanon iv) porch not (Nousside. she _funcdien globe Nor “18: qlobal- vas) weAcceESSING the gqleb_vov Tousside fundon . Shrikrupa #)wWike a ghost note on: Functions sth cleat aqumeg —_ = Functions 29 Python can have default aTqumengy dkich ave paramekers assiqned a default volue SA the function defi miton » |. EF a value 38 not providel fos these porameey when the fundion “s_catled, the default | Volue Ig used + = You can _ossiqn His defaul+ volue by odith the assiqnment operator =). -tt we are rot passing any name +hen ony defoult value ‘will be consider. = When defining 2 fundion you can assiqn default yabter to pavameks 9A the fanciion's "Signature z False) = Exomple t- name: "Esha ” oge = #9 pant ChnName:", name "Age", oge) = |- A module LA pesjests ~!3)| Explain, Module. How to creste the module Yo python? “a a File conteli Finey a Python code - ~The code Stina module cos define Fanchony classes, and yorhablea that can be used io cthes python Files by “impor ng, she modute - = medutes help Sa organizing code into logical ants ai ng 54 easier = manage and _mantain lege, = Creating a moduwe 19 Python Ss Strolight Forwarrd » Senlo. create. a module IA Python sieps ave + Shrikrupa : Donic yours codes Fixst write the Poptheo code, contain’ the funcHong close , oF vonoble | you to Sineiude Fa yout module - This : es code can be saved %0 a. geparode - py_file + 2) gove the Ale: Save the Python file With q desemphive name endl ng Sn spy: DVSng the modute ; To use the mochule Sin another Python SeMPt, you “import oY otic i t 1 f the ‘import "gtadement - a) Runming the schpt : Run the Python soapy that user the moctule - =f £4) Expidin, Aipferent forms of Sen pest" Statement = The “img siatement “is _used to bRaq So Functionality from other modules oF postenjss ~TReve ave vatous Proms oF Sexport Stotemne =r) ) they ove ay follows '- 1) Basic Import : : This isthe Simplest Form, wheve_you “cept an ennre medule oF package: Senport modude name im post package - name 2) Emport eSrth Alay: we Ar We can pwide an alias to the Smpovied module oF package to Simp ii Fy 438 Wage | ! 4 { Shrikeupa __Aimpett moedute ~ name os atiog [import package-name os atfey ~|a) Eepest specifi c Seng: — | Ensteac oF mporing +he entive rnodule | you oO Simpor4 spedFic Mrems Prom Ste From modute name ‘import Steme, Hem2 From) _packsqe- name, Smpowt “eo Steen 4) Wildearad Erxpovd » This “impotts alt Stems from a module which can sometimes clutter, the namespace f shouts be uwged Sith caution = i from medute name Smport * From poead @ “heme, Sempoyt ¥ Ss) condifional Senpert + - Se condition: Smpott moedute name iS)| Explain python built -10 rnodute ~ yandom math . yi t Shrikrupa

You might also like