0% found this document useful (0 votes)
19 views

Week 5 Python

The document covers an introduction to functions in Python including defining and calling functions, function parameters and arguments, returning values from functions, and using functions with input statements. It also discusses more examples on functions including finding minimum, maximum, and average values in lists as well as sorting lists using functions. The document covers matrix multiplication, recursion, and different types of function arguments including positional, keyword, and default arguments. It concludes with discussing the scope of variables and different types of functions.

Uploaded by

mayank
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Week 5 Python

The document covers an introduction to functions in Python including defining and calling functions, function parameters and arguments, returning values from functions, and using functions with input statements. It also discusses more examples on functions including finding minimum, maximum, and average values in lists as well as sorting lists using functions. The document covers matrix multiplication, recursion, and different types of function arguments including positional, keyword, and default arguments. It concludes with discussing the scope of variables and different types of functions.

Uploaded by

mayank
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Week 5

Lecture 5.1

Introduction to functions :-

If we run the block [1] code, we will not get any output. Functions are not executed
unless they are called.

‘add(10,12)’ is a function call, and


‘10,12’ are the values that are passed to the function in the call and are known as
arguments.

‘a,b’ are the parameters.


Here we wanted to add 10 and 12 first and then add 10 to it.
Why couldn’t we do it?

Earlier we used ‘print(ans)’ but now we used ‘return ans’


Return statement simply returns the values as output whereas the print() function simply
prints the value.

Using a function with input statements -

Code :-

Output :-
Lecture 5.2

More examples on function :-

Finding the minimum element in the list -

Finding the maximum element in the list -

Appending a list in the beginning -


Appending a list in the end -

Finding average of elements in list -

Lecture 5.3

Sorting using functions :-

Obvious sort :-
find the minimum element in the list l
append that element in the list x
remove that element in the list l
Another way -
Lecture 5.4

Matrix Multiplication using functions :-


Lecture 5.5

Theoretical intro to recursion :-

Sum of n numbers -
sum(n)=sum(n-1)+n

Factorial -
#fact(n)=fact(n-1).n

Lecture 5.6

Recursion :-

Sum of first n numbers -

Compound interest by assuming the interest 10% (f(n)=f(n-1)(1.1)) -


Factorial of a number -

Lecture 5.7

Types of function arguments :-

Positional arguments :-
All functions that we have seen so far have used positional arguments. Here, the
position of an argument in the function call determines the parameter to which it is
passed.
Keyword arguments :-
Keyword arguments introduce more flexibility while passing arguments.

Default arguments :-
Parameters that are assigned a value in the function definition are called default
parameters.
An argument corresponding to a default parameter can be passed as a positional
argument or as a keyword argument.
Default parameters always come at the end of the parameter list in a function definition.
Lecture 5.8

Scope of a variable :-
The region in the code where a name can be referenced is called its scope. If we try to
reference a variable outside its scope, the interpreter will throw a NameError.

Local vs Global -

In the above example, the scope of the name x is local to the function; x has a
meaningful existence only inside the function and any attempt to access it from outside
the function is going to result in an error

The name y is accessible from within the function as well. We say that the scope of y is
global. That is, it can be referenced from anywhere within the program — even inside a
function — after it has been defined for the first time.
Lecture 5.9

Types of functions :-

Built-in functions -
So far we have been freely using built-in functions like -
print(), int(), len() and so on.

Library functions -
These functions are the built-in functions i.e., they are predefined in the library of the
python, such as -
sqrt(), log() , random() and so on.

String methods or functions -


String functions are used to manipulate a string , some of them are -
lower(), upper(), strip() and so on.

User defined functions -


User-defined functions are functions that we use to organize our code.

You might also like