0% found this document useful (0 votes)
5 views9 pages

Python Functions

The document provides an overview of functions in Python, including built-in functions, user-defined functions, and their parameters. It explains the concepts of function definition, calling, and various types of arguments such as default, positional, and keyword arguments. Additionally, it covers the return statement and the scope of variables within functions.

Uploaded by

born4gaming007
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)
5 views9 pages

Python Functions

The document provides an overview of functions in Python, including built-in functions, user-defined functions, and their parameters. It explains the concepts of function definition, calling, and various types of arguments such as default, positional, and keyword arguments. Additionally, it covers the return statement and the scope of variables within functions.

Uploaded by

born4gaming007
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
Introduction Whatis a Function? ‘A function is a block of code that performs a specific task. FUNCTIONS Built in Functions Built-in Functions ? Those functions which are predefined in python We have to just call them to use. Function Explanation print) Displays output Inpurd Takes user putas string abs) —_-Retums absolute (positive) value round) Rounds anumber pow) Returns power (x"y) ming Finds minimum value max) Finds maximum value sum) Adds elements lend Returns length of object sorted() Returns sorted lst reversed() Reverses a sequence type) ——_—Returns datatype Cee ar EeU ncn) meee ae PSC Crs) meee rues) eae Eorrr =n eth eras Pere Deuce ae eae #6 print(round(3.6)) #4 print(pow(2, 3)) #8 ‘* Random Module Functions ~ Functions Defined in Modules - Module : Code Library random) Returns random float (0 to 2) randint() Returns random integer in range choiced Picks random element from lst» ® Math Module ~ shuffled Shuffles elements of a ist sqrt) Returns square root of a number NRE eae) ceil) Rounds number up to nearest integer hai tetnaatie! floor() Rounds number down to nearest integer # Datetime Module Functions - factorial) Returns factorial of a number datetime) Creates date and time object ged) Finds greatest common divisor dated Creates date object, aera timed) Creates time object, Done today() Returns current date Prsceamesse(Cees}) EeasCeemserce)) print(math. factorial (5)) Ge eo) oor Peercstre To) print(now) User Defined Functions - ary (arn Functions which are created by user as per the requirement. erect() Block of statements that perform a specific task Petcare) ‘def * keyword is used. ssquare(5) Function Definition / Creation ? def cube(x): ( input ): poeta) cube(3) F greet() esac Function Calling ? print("Hello”, naney Parameter vs Argument Parameter ? Input given at time of definition These are values which are used by the function to do a task Also called (Formal Parameter) calculate_cube(a) print(atata) ‘Argument? Input given at time of function calling Value which are used in execution ‘Also called (Actual Parameter). calculate_cube(2) ? Types of Parameters ? Default ? If input not provided takes default set value Volues that ore provided while defining Functions. def greet(nane="5 Petes omens) Pr anrea) greet() prota Positional ? ‘Arguments passed should be in order of parameters eeu Poms eer KEYWORD ARGUMENTS ? ‘Arguments passed using parameter names. def student (name, age): een > name) print(“age:", age) eee Order does NOT matter here. Mix of positional + keyword ? Positional arguments must come before keyword arguments. Bees are Ber Cr es Ces VARIABLE LENGTH ARGUMENTS? Used when number of inputs is unknown. “args (Non-keyword variable arguments) ? a Stores multiple values as a tuple Pearle print (numbers) add(1, 2, 3, 4) **kwargs (Keyword variable arguments) Stores data as a dictionary def details(**data): Un CLIe) SECTION 1 RETURN STATEMENT ? Return? Sends value back to function caller Ends function execution Iai e ey FLOW OF EXECUTION ? Peay rate SCOPE ? LOCAL SCOPE? Variable inside Function. def fun2() ts crit) ae . ee print (x) peint(*start") funa() Eee} func() Crue cs Pearson RLQBAL SCORE? raed Caan eae Paraes PSU eer re antes func()

You might also like